MCPcopy
hub / github.com/dropbox/godropbox / TestIntern

Method TestIntern

strings/intern_test.go:23–74  ·  view source on GitHub ↗
(c *C)

Source from the content-addressed store, hash-verified

21var _ = Suite(&InternStringSuite{})
22
23func (i *InternStringSuite) TestIntern(c *C) {
24
25 s := "foo bar"
26
27 // Creating a string using byte arrays
28 var buffer bytes.Buffer
29 buffer.WriteString("f")
30 buffer.WriteString("o")
31 buffer.WriteString("o")
32 buffer.WriteString(" ")
33 buffer.WriteString("b")
34 buffer.WriteString("a")
35 buffer.WriteString("r")
36 b := buffer.String()
37
38 // Get StringHeaders for the two strings
39 sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
40 bh := (*reflect.StringHeader)(unsafe.Pointer(&b))
41
42 // Assert that their StringHeader data are unequal
43 c.Assert(sh.Data, Not(Equals), bh.Data)
44 // The StringHeader Length should be equal
45 c.Assert(sh.Len, Equals, bh.Len)
46 // The two strings should be equal
47 c.Assert(s, Equals, b)
48
49 // Now to intern these two strings into the intern pool
50 i1 := Intern(s)
51 i2 := Intern(b)
52
53 ih1 := (*reflect.StringHeader)(unsafe.Pointer(&i1))
54 ih2 := (*reflect.StringHeader)(unsafe.Pointer(&i2))
55
56 // Assert that interned strings have equal StringHeader Data
57 c.Assert(ih1.Data, Equals, ih2.Data)
58 // The StringHeader Length should be equal
59 c.Assert(ih1.Len, Equals, ih2.Len)
60 // The two strings should be equal
61 c.Assert(i1, Equals, i2)
62
63 // Intern byte array of characters
64 ba := buffer.Bytes()
65 i3 := InternBytes(ba)
66 ih3 := (*reflect.StringHeader)(unsafe.Pointer(&i3))
67
68 // Assert that interned strings have equal StringHeader Data
69 c.Assert(ih3.Data, Equals, ih2.Data)
70 // The StringHeader Length should be equal
71 c.Assert(ih3.Len, Equals, ih2.Len)
72 // The two strings should be equal
73 c.Assert(i3, Equals, i2)
74}

Callers

nothing calls this directly

Calls 5

NotFunction · 0.85
InternFunction · 0.85
InternBytesFunction · 0.85
StringMethod · 0.65
BytesMethod · 0.65

Tested by

no test coverage detected