1 | // This interpreter test is designed to test the test copy of DeepEqual. |
---|---|
2 | // |
3 | // Validate this file with 'go run' after editing. |
4 | |
5 | package main |
6 | |
7 | import "reflect" |
8 | |
9 | func assert(cond bool) { |
10 | if !cond { |
11 | panic("failed") |
12 | } |
13 | } |
14 | |
15 | type X int |
16 | type Y struct { |
17 | y *Y |
18 | z [3]int |
19 | } |
20 | |
21 | var ( |
22 | a = []int{0, 1, 2, 3} |
23 | b = []X{0, 1, 2, 3} |
24 | c = map[int]string{0: "zero", 1: "one"} |
25 | d = map[X]string{0: "zero", 1: "one"} |
26 | e = &Y{} |
27 | f = (*Y)(nil) |
28 | g = &Y{y: e} |
29 | h *Y |
30 | ) |
31 | |
32 | func init() { |
33 | h = &Y{} // h->h |
34 | h.y = h |
35 | } |
36 | |
37 | func main() { |
38 | assert(reflect.DeepEqual(nil, nil)) |
39 | assert(reflect.DeepEqual((*int)(nil), (*int)(nil))) |
40 | assert(!reflect.DeepEqual(nil, (*int)(nil))) |
41 | |
42 | assert(reflect.DeepEqual(0, 0)) |
43 | assert(!reflect.DeepEqual(0, int64(0))) |
44 | |
45 | assert(!reflect.DeepEqual("", 0)) |
46 | |
47 | assert(reflect.DeepEqual(a, []int{0, 1, 2, 3})) |
48 | assert(!reflect.DeepEqual(a, []int{0, 1, 2})) |
49 | assert(!reflect.DeepEqual(a, []int{0, 1, 0, 3})) |
50 | |
51 | assert(reflect.DeepEqual(b, []X{0, 1, 2, 3})) |
52 | assert(!reflect.DeepEqual(b, []X{0, 1, 0, 3})) |
53 | |
54 | assert(reflect.DeepEqual(c, map[int]string{0: "zero", 1: "one"})) |
55 | assert(!reflect.DeepEqual(c, map[int]string{0: "zero", 1: "one", 2: "two"})) |
56 | assert(!reflect.DeepEqual(c, map[int]string{1: "one", 2: "two"})) |
57 | assert(!reflect.DeepEqual(c, map[int]string{1: "one"})) |
58 | |
59 | assert(reflect.DeepEqual(d, map[X]string{0: "zero", 1: "one"})) |
60 | assert(!reflect.DeepEqual(d, map[int]string{0: "zero", 1: "one"})) |
61 | |
62 | assert(reflect.DeepEqual(e, &Y{})) |
63 | assert(reflect.DeepEqual(e, &Y{z: [3]int{0, 0, 0}})) |
64 | assert(!reflect.DeepEqual(e, &Y{z: [3]int{0, 1, 0}})) |
65 | |
66 | assert(reflect.DeepEqual(f, (*Y)(nil))) |
67 | assert(!reflect.DeepEqual(f, nil)) |
68 | |
69 | // eq_h -> eq_h. Pointer structure and elements are equal so DeepEqual. |
70 | eq_h := &Y{} |
71 | eq_h.y = eq_h |
72 | assert(reflect.DeepEqual(h, eq_h)) |
73 | |
74 | // deepeq_h->h->h. Pointed to elem of (deepeq_h, h) are (h,h). (h,h) are deep equal so h and deepeq_h are DeepEqual. |
75 | deepeq_h := &Y{} |
76 | deepeq_h.y = h |
77 | assert(reflect.DeepEqual(h, deepeq_h)) |
78 | |
79 | distinct := []interface{}{a, b, c, d, e, f, g, h} |
80 | for x := range distinct { |
81 | for y := range distinct { |
82 | assert((x == y) == reflect.DeepEqual(distinct[x], distinct[y])) |
83 | } |
84 | } |
85 | |
86 | // anonymous struct types. |
87 | assert(reflect.DeepEqual(struct{}{}, struct{}{})) |
88 | assert(reflect.DeepEqual(struct{ x int }{1}, struct{ x int }{1})) |
89 | assert(!reflect.DeepEqual(struct{ x int }{}, struct{ x int }{5})) |
90 | assert(!reflect.DeepEqual(struct{ x, y int }{0, 1}, struct{ x int }{0})) |
91 | assert(reflect.DeepEqual(struct{ x, y int }{2, 3}, struct{ x, y int }{2, 3})) |
92 | assert(!reflect.DeepEqual(struct{ x, y int }{4, 5}, struct{ x, y int }{4, 6})) |
93 | } |
94 |
Members