-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathdisplay_test.go
265 lines (239 loc) · 5.83 KB
/
display_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
package display
import (
"io"
"net"
"os"
"reflect"
"sync"
"testing"
"gopl.io/ch7/eval"
)
func Example_expr() {
e, _ := eval.Parse("sqrt(A / pi)")
Display("e", e)
// Output:
// Display e (eval.call):
// e.fn = "sqrt"
// e.args[0].type = eval.binary
// e.args[0].value.op = 47
// e.args[0].value.x.type = eval.Var
// e.args[0].value.x.value = "A"
// e.args[0].value.y.type = eval.Var
// e.args[0].value.y.value = "pi"
}
func Example_slice() {
Display("slice", []*int{new(int), nil})
// Output:
// Display slice ([]*int):
// (*slice[0]) = 0
// slice[1] = nil
}
func Example_nilInterface() {
var w io.Writer
Display("w", w)
// Output:
// Display w (<nil>):
// w = invalid
}
func Example_ptrToInterface() {
var w io.Writer
Display("&w", &w)
// Output:
// Display &w (*io.Writer):
// (*&w) = nil
}
func Example_struct() {
Display("x", struct{ x interface{} }{3})
// Output:
// Display x (struct { x interface {} }):
// x.x.type = int
// x.x.value = 3
}
func Example_interface() {
var i interface{} = 3
Display("i", i)
// Output:
// Display i (int):
// i = 3
}
func Example_ptrToInterface2() {
var i interface{} = 3
Display("&i", &i)
// Output:
// Display &i (*interface {}):
// (*&i).type = int
// (*&i).value = 3
}
func Example_array() {
Display("x", [1]interface{}{3})
// Output:
// Display x ([1]interface {}):
// x[0].type = int
// x[0].value = 3
}
func Example_movie() {
//!+movie
type Movie struct {
Title, Subtitle string
Year int
Color bool
Actor map[string]string
Oscars []string
Sequel *string
}
//!-movie
//!+strangelove
strangelove := Movie{
Title: "Dr. Strangelove",
Subtitle: "How I Learned to Stop Worrying and Love the Bomb",
Year: 1964,
Color: false,
Actor: map[string]string{
"Dr. Strangelove": "Peter Sellers",
"Grp. Capt. Lionel Mandrake": "Peter Sellers",
"Pres. Merkin Muffley": "Peter Sellers",
"Gen. Buck Turgidson": "George C. Scott",
"Brig. Gen. Jack D. Ripper": "Sterling Hayden",
`Maj. T.J. "King" Kong`: "Slim Pickens",
},
Oscars: []string{
"Best Actor (Nomin.)",
"Best Adapted Screenplay (Nomin.)",
"Best Director (Nomin.)",
"Best Picture (Nomin.)",
},
}
//!-strangelove
Display("strangelove", strangelove)
// We don't use an Output: comment since displaying
// a map is nondeterministic.
/*
//!+output
Display strangelove (display.Movie):
strangelove.Title = "Dr. Strangelove"
strangelove.Subtitle = "How I Learned to Stop Worrying and Love the Bomb"
strangelove.Year = 1964
strangelove.Color = false
strangelove.Actor["Gen. Buck Turgidson"] = "George C. Scott"
strangelove.Actor["Brig. Gen. Jack D. Ripper"] = "Sterling Hayden"
strangelove.Actor["Maj. T.J. \"King\" Kong"] = "Slim Pickens"
strangelove.Actor["Dr. Strangelove"] = "Peter Sellers"
strangelove.Actor["Grp. Capt. Lionel Mandrake"] = "Peter Sellers"
strangelove.Actor["Pres. Merkin Muffley"] = "Peter Sellers"
strangelove.Oscars[0] = "Best Actor (Nomin.)"
strangelove.Oscars[1] = "Best Adapted Screenplay (Nomin.)"
strangelove.Oscars[2] = "Best Director (Nomin.)"
strangelove.Oscars[3] = "Best Picture (Nomin.)"
strangelove.Sequel = nil
//!-output
*/
}
// This test ensures that the program terminates without crashing.
func Test(t *testing.T) {
// Some other values (YMMV)
Display("os.Stderr", os.Stderr)
// Output:
// Display os.Stderr (*os.File):
// (*(*os.Stderr).file).fd = 2
// (*(*os.Stderr).file).name = "/dev/stderr"
// (*(*os.Stderr).file).nepipe = 0
var w io.Writer = os.Stderr
Display("&w", &w)
// Output:
// Display &w (*io.Writer):
// (*&w).type = *os.File
// (*(*(*&w).value).file).fd = 2
// (*(*(*&w).value).file).name = "/dev/stderr"
// (*(*(*&w).value).file).nepipe = 0
var locker sync.Locker = new(sync.Mutex)
Display("(&locker)", &locker)
// Output:
// Display (&locker) (*sync.Locker):
// (*(&locker)).type = *sync.Mutex
// (*(*(&locker)).value).state = 0
// (*(*(&locker)).value).sema = 0
Display("locker", locker)
// Output:
// Display locker (*sync.Mutex):
// (*locker).state = 0
// (*locker).sema = 0
// (*(&locker)) = nil
locker = nil
Display("(&locker)", &locker)
// Output:
// Display (&locker) (*sync.Locker):
// (*(&locker)) = nil
ips, _ := net.LookupHost("golang.org")
Display("ips", ips)
// Output:
// Display ips ([]string):
// ips[0] = "173.194.68.141"
// ips[1] = "2607:f8b0:400d:c06::8d"
// Even metarecursion! (YMMV)
Display("rV", reflect.ValueOf(os.Stderr))
// Output:
// Display rV (reflect.Value):
// (*rV.typ).size = 8
// (*rV.typ).ptrdata = 8
// (*rV.typ).hash = 871609668
// (*rV.typ)._ = 0
// ...
// a pointer that points to itself
type P *P
var p P
p = &p
Display("p", p)
// Output:
// Display p (display.P):
// (*(*(*(*(*(*p)))))) = display.P 0xc820028018
// a map that contains itself
type M map[string]M
m := make(M)
m[""] = m
Display("m", m)
// Output:
// Display m (display.M):
// m[""][""][""][""][""][""] = display.M 0xc8200c0840
// a slice that contains itself
type S []S
s := make(S, 1)
s[0] = s
Display("s", s)
// Output:
// Display s (display.S):
// s[0][0][0][0][0][0] = display.S 0xc8200d6800
// a linked list that eats its own tail
type Cycle struct {
Value int
Tail *Cycle
}
var c Cycle
c = Cycle{42, &c}
Display("c", c)
// Output:
// Display c (display.Cycle):
// c.Value = 42
// (*c.Tail).Value = 42
// (*(*c.Tail).Tail).Value = 42
// (*(*(*c.Tail).Tail).Tail) = display.Cycle value
}
func TestMapKeys(t *testing.T) {
sm := map[struct{ x int }]int{
{1}: 2,
{2}: 3,
}
Display("sm", sm)
// Output:
// Display sm (map[struct { x int }]int):
// sm[{x: 2}] = 3
// sm[{x: 1}] = 2
am := map[[3]int]int{
{1, 2, 3}: 3,
{2, 3, 4}: 4,
}
Display("am", am)
// Output:
// Display am (map[[3]int]int):
// am[{1, 2, 3}] = 3
// am[{2, 3, 4}] = 4
}