-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattachment_test.go
278 lines (233 loc) · 5.11 KB
/
attachment_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2022 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package magicstring
import (
"fmt"
"reflect"
"testing"
"unsafe"
"github.com/huandu/go-assert"
)
func TestAttachData(t *testing.T) {
a := assert.New(t)
cases := []interface{}{
1, "abcd", true, 1.2, complex(1, 2),
struct{}{}, []byte("123"), &testStruct{},
map[string][]int{"foo": {1, 2, 3}},
}
for _, c := range cases {
iterateStrings(func(s string) {
copied := Attach(s, c)
a.Equal(s, copied)
a.Assert(Is(copied))
data := Read(copied)
a.Equal(data, c)
})
}
}
func TestAttachNilData(t *testing.T) {
a := assert.New(t)
s1 := "nil data"
s2 := Attach(s1, nil)
s3 := Attach(s1, 123)
s4 := Attach(s3, nil)
a.Equal(s1, s2)
a.Equal(s1, s3)
a.Equal(s1, s4)
// Nil data is a kind of data.
a.Assert(Is(s2))
// The buffer in s1 and s2 must be different.
data1 := (*reflect.StringHeader)(unsafe.Pointer(&s1)).Data
data2 := (*reflect.StringHeader)(unsafe.Pointer(&s2)).Data
a.NotEqual(data1, data2)
// The buffer in s3 and s4 must be different.
data3 := (*reflect.StringHeader)(unsafe.Pointer(&s3)).Data
data4 := (*reflect.StringHeader)(unsafe.Pointer(&s4)).Data
a.NotEqual(data3, data4)
}
func TestReadInvalidString(t *testing.T) {
a := assert.New(t)
s := "dummy"
a.Assert(!Is(s))
a.Assert(Read(s) == nil)
}
func TestAttachReplaceData(t *testing.T) {
a := assert.New(t)
s := "sample string"
data1 := &testStruct{
Foo: 123,
}
data2 := &testStruct{
Foo: 567,
}
s1 := Attach(s, data1)
a.Equal(s, s1)
payload := Read(s1)
a.Equal(data1, payload)
s2 := Attach(s1, data2)
a.Equal(s, s2)
a.Equal(s1, s2)
payload = Read(s2)
a.NotEqual(data1, payload)
a.Equal(data2, payload)
}
func TestAttachMapKey(t *testing.T) {
a := assert.New(t)
key := "foo"
m := map[string]int{
key: 123,
}
// If a key exists in a map, map will replace old key with the magic string.
// WARNING: It's not guaranteed by Go languange spec. Don't rely on this behavior.
data := 567
foo := Attach(key, data)
m[foo] = data
for k, v := range m {
a.Assert(Is(k))
a.Equal(Read(k), v)
}
// If a key absents in a map, map will use the magic string as key.
data = 999
bar := Attach("bar", data)
m[bar] = data
delete(m, key)
for k, v := range m {
a.Assert(Is(k))
a.Equal(Read(k), v)
}
}
func TestReplace(t *testing.T) {
a := assert.New(t)
data1 := 123
data2 := "foo"
iterateStrings(func(s string) {
success := Replace(s, data1)
a.Assert(!success)
attached := Attach(s, data1)
a.Assert(Is(attached))
a.Equal(Read(attached), data1)
success = Replace(attached, nil)
a.Assert(success)
a.Equal(Read(attached), nil)
success = Replace(attached, data2)
a.Assert(success)
a.Equal(Read(attached), data2)
})
}
func TestDetach(t *testing.T) {
a := assert.New(t)
iterateStrings(func(s string) {
data := &testStruct{
Foo: 398,
}
attached := Attach(s, data)
a.Equal(s, attached)
// Call detach will not affect attached string.
detached := Detach(attached)
a.Equal(detached, s)
a.Assert(Is(attached))
a.Assert(!Is(detached))
// It's OK to detach twice.
detached = Detach(detached)
a.Equal(detached, s)
payload := Read(detached)
a.Assert(payload == nil)
})
}
func TestSlice(t *testing.T) {
a := assert.New(t)
type T struct {
Foo string
Bar int
}
data := &T{
Foo: "foo",
Bar: 123,
}
s0 := "a plain string"
s1 := Attach("a magic string which should be long enough for test", data)
s2 := Slice(s1, 15, 22)
s3 := s1[3:20]
s4 := s1[21:27]
s5 := Slice(s0, 3, 6)
s6 := Slice(s1, 21, 21)
// Magic strings.
a.Assert(Is(s1))
a.Assert(Is(s2))
a.Assert(Is(s3))
a.Assert(Is(s6))
// All magic strings reference to the same data by pointer.
a.Equal(Read(s1), data)
a.Equal(Read(s2), data)
a.Equal(Read(s3), data)
a.Equal(Read(s6), data)
data.Bar = 999
a.Equal(Read(s1), data)
a.Equal(Read(s2), data)
a.Equal(Read(s3), data)
a.Equal(Read(s6), data)
// Ordinary strings.
a.Assert(!Is(s0))
a.Assert(!Is(s4))
a.Assert(!Is(s5))
func() {
defer func() {
a.Assert(recover())
}()
Slice(s1, 13, 12)
}()
}
func ExampleAttach() {
type T struct {
Name string
}
s1 := "Hello, world!"
data := &T{Name: "Kanon"}
s2 := Attach(s1, data)
attached := Read(s2).(*T)
fmt.Println(s1 == s2)
fmt.Println(attached == data)
// Output:
// true
// true
}
func ExampleIs() {
s1 := "ordinary string"
s2 := Attach("magic string", 123)
s3 := s2
s4 := fmt.Sprint(s2)
fmt.Println(Is(s1))
fmt.Println(Is(s2))
fmt.Println(Is(s3))
fmt.Println(Is(s4))
// Output:
// false
// true
// true
// false
}
func Example_destroyMagicString() {
magicString := Attach("magic string", 123)
buf := make([]byte, len(magicString))
copy(buf, magicString)
ordinaryString := string(buf)
detachedString := Detach(magicString)
fmt.Println(Is(magicString))
fmt.Println(Is(ordinaryString))
fmt.Println(Is(detachedString))
// Output:
// true
// false
// false
}
func ExampleReplace() {
s := Attach("magic string", 123)
fmt.Println(Read(s))
success := Replace(s, "replaced")
fmt.Println(success)
fmt.Println(Read(s))
// Output:
// 123
// true
// replaced
}