-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
memviz_test.go
280 lines (248 loc) · 3.94 KB
/
memviz_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
279
280
package memviz_test
import (
"bytes"
"fmt"
"os"
"reflect"
"testing"
"github.com/bradleyjkemp/cupaloy/v2"
"github.com/bradleyjkemp/memviz"
)
//nolint:structcheck,megacheck
type basicNumerics struct {
uint8 uint8
uint32 uint32
uint64 uint64
int8 int8
int16 int16
int32 int32
int64 int64
float32 float32
float64 float64
complex64 complex64
complex128 complex128
byte byte
rune rune
uint uint
int int
uintptr uintptr
Ptruint32 *uint32
Ptruint64 *uint64
Ptrint8 *int8
Ptrint16 *int16
Ptrint32 *int32
Ptrint64 *int64
Ptrfloat32 *float32
Ptrfloat64 *float64
Ptrcomplex64 *complex64
Ptrcomplex128 *complex128
Ptrbyte *byte
Ptrrune *rune
Ptruint *uint
Ptrint *int
Ptruintptr *uintptr
}
type basics struct {
numerics *basicNumerics
string string
slice []string
ptr *string
iface interface{}
}
func TestBasicTypes(t *testing.T) {
str := "Hello"
b := &basics{
new(basicNumerics),
"Hi",
[]string{"Hello", "World"},
&str,
"interfaceValue",
}
v := reflect.ValueOf(b.numerics).Elem()
for i := 0; i < v.NumField(); i++ {
if f := v.Field(i); f.Kind() == reflect.Ptr {
fv := reflect.New(f.Type().Elem())
f.Set(fv)
}
}
buf := &bytes.Buffer{}
memviz.Map(buf, b)
fmt.Println(buf.String())
cupaloy.SnapshotT(t, buf.Bytes())
}
type tree struct {
id int
left *tree
right *tree
}
func TestTree(t *testing.T) {
root := &tree{
id: 0,
left: &tree{
id: 1,
},
right: &tree{
id: 2,
},
}
leaf := &tree{
id: 3,
}
root.left.right = leaf
root.right.left = leaf
b := &bytes.Buffer{}
memviz.Map(b, root)
fmt.Println(b.String())
cupaloy.SnapshotT(t, b.Bytes())
}
func TestVariadicArguments(t *testing.T) {
leaf := &tree{
0,
nil,
nil,
}
inner1 := &tree{
1,
nil,
leaf,
}
inner2 := &tree{
2,
leaf,
nil,
}
root1 := &tree{
3,
inner1,
inner2,
}
root2 := &tree{
4,
inner2,
nil,
}
b := &bytes.Buffer{}
memviz.Map(b, root1, root2)
fmt.Println(b.String())
cupaloy.SnapshotT(t, b.Bytes())
}
func TestSliceTree(t *testing.T) {
root := &tree{
id: 0,
left: &tree{
id: 1,
},
right: &tree{
id: 2,
},
}
leaf := &tree{
id: 3,
}
root.left.right = leaf
root.right.left = leaf
slice := []*tree{root, root.left, root.right, leaf}
b := &bytes.Buffer{}
memviz.Map(b, &slice)
fmt.Println(b.String())
cupaloy.SnapshotT(t, b.Bytes())
}
type fib struct {
index int
prev *fib
prevprev *fib
}
func TestFib(t *testing.T) {
f0 := &fib{
0,
nil,
nil,
}
f1 := &fib{
1,
f0,
nil,
}
f2 := &fib{
2,
f1,
f0,
}
f3 := &fib{
3,
f2,
f1,
}
f4 := &fib{
4,
f3,
f2,
}
f5 := &fib{
5,
f4,
f3,
}
b := &bytes.Buffer{}
memviz.Map(b, f5)
fmt.Println(b.String())
cupaloy.SnapshotT(t, b.Bytes())
}
type structMap struct {
id string
links map[*structMap]bool
}
func TestMap(t *testing.T) {
leaf := &structMap{
"leaf",
nil,
}
leaf2 := &structMap{
"leaf2",
nil,
}
parent := &structMap{
"parent",
map[*structMap]bool{
leaf: true,
leaf2: true,
},
}
leaf.links = map[*structMap]bool{parent: true}
parent.links[parent] = true
b := &bytes.Buffer{}
memviz.Map(b, parent)
fmt.Println(b.String())
// TODO: enable snapshot assertion once map keys are sorted (and so this has stable output)
err := cupaloy.Snapshot(b.Bytes())
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
func TestPointerChain(t *testing.T) {
str := "Hello world"
str2 := &str
str3 := &str2
str4 := &str3
b := &bytes.Buffer{}
memviz.Map(b, &str4)
fmt.Println(b.String())
cupaloy.SnapshotT(t, b.Bytes())
}
func TestPointerAliasing(t *testing.T) {
leaf := "leaf"
parent0 := &leaf
parent1 := &parent0
parent2 := &leaf
root := struct {
left **string
right *string
}{
parent1,
parent2,
}
b := &bytes.Buffer{}
memviz.Map(b, &root)
fmt.Println(b.String())
cupaloy.SnapshotT(t, b.Bytes())
}