Debian Codegraph @

internal/compare/compare_test.go

raw
1 // Copyright (c) 2019-2025, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package compare_test
8
9 import (
10 "fmt"
11 "math"
12 "reflect"
13 "testing"
14
15 "github.com/maxatome/go-testdeep/internal/compare"
16 "github.com/maxatome/go-testdeep/internal/visited"
17 )
18
19 type cmp1 int
20
21 func (cmp1) Compare(cmp1) bool { return false }
22
23 type cmp2 int
24
25 func (cmp2) Compare(int) int { return 0 }
26
27 type cmp3 int
28
29 func (cmp3) Compare(...int) int { return 0 }
30
31 type cmpPanic int
32
33 func (cmpPanic) Compare(cmpPanic) { panic("boom!") }
34
35 type cmpOK int
36
37 func (a cmpOK) Compare(b cmpOK) int {
38 if a == b {
39 return 0
40 }
41 if a > b {
42 return -1
43 }
44 return 1
45 }
46
47 type cmpOKp int
48
49 func newCmpOKp(x cmpOKp) *cmpOKp {
50 return &x
51 }
52
53 func (a *cmpOKp) Compare(b *cmpOKp) int {
54 if *a == *b {
55 return 0
56 }
57 if *a > *b {
58 return -1
59 }
60 return 1
61 }
62
63 func TestCompare(t *testing.T) {
64 type myStruct struct {
65 n int
66 s string
67 p *myStruct
68 }
69 a, b := 12, 42
70 ma, mb := map[int]bool{12: true}, map[int]bool{12: true, 13: false}
71
72 checkCompare := func(t *testing.T, a, b any, expected int) {
73 t.Helper()
74 got := compare.Compare(visited.NewVisited(), reflect.ValueOf(a), reflect.ValueOf(b))
75 if got != expected {
76 t.Errorf("Compare() failed: got=%d expected=%d\n", got, expected)
77 }
78 }
79
80 testCases := []struct {
81 a, b any
82 expected int
83 }{
84 // nil
85 {nil, 12, -1},
86 {nil, nil, 0},
87 {12, nil, 1},
88 // type mismatch: int is before string
89 {42, "str", -1},
90 {"str", 42, 1},
91 // Compare
92 {cmpOK(99), cmpOK(11), -1},
93 {cmpOK(11), cmpOK(99), 1},
94 {cmpOK(99), cmpOK(99), 0},
95 {newCmpOKp(99), newCmpOKp(11), -1},
96 {newCmpOKp(11), newCmpOKp(99), 1},
97 {newCmpOKp(99), newCmpOKp(99), 0},
98 // Compare does not match expected signature
99 {cmp1(99), cmp1(11), 1},
100 {cmp1(11), cmp1(99), -1},
101 {cmp1(99), cmp1(99), 0},
102 {cmp2(99), cmp2(11), 1},
103 {cmp2(11), cmp2(99), -1},
104 {cmp2(99), cmp2(99), 0},
105 {cmp3(99), cmp3(11), 1},
106 {cmp3(11), cmp3(99), -1},
107 {cmp3(99), cmp3(99), 0},
108 // Compare panics fallback
109 {cmpPanic(99), cmpPanic(11), 1},
110 {cmpPanic(11), cmpPanic(99), -1},
111 {cmpPanic(99), cmpPanic(99), 0},
112 // bool
113 {true, true, 0},
114 {true, false, 1},
115 {false, true, -1},
116 {false, false, 0},
117 // int
118 {12, 42, -1},
119 {42, 12, 1},
120 {12, 12, 0},
121 {int8(12), int8(42), -1},
122 {int8(42), int8(12), 1},
123 {int8(12), int8(12), 0},
124 {int16(12), int16(42), -1},
125 {int16(42), int16(12), 1},
126 {int16(12), int16(12), 0},
127 {int32(12), int32(42), -1},
128 {int32(42), int32(12), 1},
129 {int32(12), int32(12), 0},
130 {int64(12), int64(42), -1},
131 {int64(42), int64(12), 1},
132 {int64(12), int64(12), 0},
133 // uint
134 {uint(12), uint(42), -1},
135 {uint(42), uint(12), 1},
136 {uint(12), uint(12), 0},
137 {uint8(12), uint8(42), -1},
138 {uint8(42), uint8(12), 1},
139 {uint8(12), uint8(12), 0},
140 {uint16(12), uint16(42), -1},
141 {uint16(42), uint16(12), 1},
142 {uint16(12), uint16(12), 0},
143 {uint32(12), uint32(42), -1},
144 {uint32(42), uint32(12), 1},
145 {uint32(12), uint32(12), 0},
146 {uint64(12), uint64(42), -1},
147 {uint64(42), uint64(12), 1},
148 {uint64(12), uint64(12), 0},
149 {uintptr(12), uintptr(42), -1},
150 {uintptr(42), uintptr(12), 1},
151 {uintptr(12), uintptr(12), 0},
152 // float
153 {float32(12), float32(42), -1},
154 {float32(42), float32(12), 1},
155 {float32(12), float32(12), 0},
156 {float64(12), float64(42), -1},
157 {float64(42), float64(12), 1},
158 {float64(12), float64(12), 0},
159 {float64(12), float64(12), 0},
160 {math.NaN(), float64(12), -1},
161 {math.NaN(), math.NaN(), -1},
162 {float64(12), math.NaN(), 1},
163 // complex
164 {complex(12, 0), complex(42, 0), -1},
165 {complex(42, 0), complex(12, 0), 1},
166 {complex(0, 12), complex(0, 42), -1},
167 {complex(0, 42), complex(0, 12), 1},
168 {complex(12, 0), complex(12, 0), 0},
169 {complex(float32(12), 0), complex(float32(42), 0), -1},
170 {complex(float32(42), 0), complex(float32(12), 0), 1},
171 {complex(float32(0), 12), complex(float32(0), 42), -1},
172 {complex(float32(0), 42), complex(float32(0), 12), 1},
173 {complex(float32(12), 0), complex(float32(12), 0), 0},
174 // string
175 {"aaa", "bbb", -1},
176 {"bbb", "aaa", 1},
177 {"aaa", "aaa", 0},
178 // array
179 {[3]byte{1, 2, 3}, [3]byte{3, 1, 2}, -1},
180 {[3]byte{3, 1, 2}, [3]byte{1, 2, 3}, 1},
181 {[3]byte{1, 2, 3}, [3]byte{1, 2, 3}, 0},
182 // slice
183 {[]byte{1, 2, 3}, []byte{3, 1, 2}, -1},
184 {[]byte{3, 1, 2}, []byte{1, 2, 3}, 1},
185 {[]byte{1, 2, 3}, []byte{1, 2, 3}, 0},
186 {[]byte{1, 2, 3}, []byte{1, 2, 3, 4}, -1},
187 {[]byte{1, 2, 3, 4}, []byte{1, 2, 3}, 1},
188 // interface
189 {[]any{1}, []any{3}, -1},
190 {[]any{3}, []any{1}, 1},
191 {[]any{1}, []any{1}, 0},
192 {[]any{nil}, []any{nil}, 0},
193 {[]any{nil}, []any{1}, -1},
194 {[]any{1}, []any{nil}, 1},
195 // struct
196 {myStruct{n: 12, s: "a"}, myStruct{n: 12, s: "b"}, -1},
197 {myStruct{n: 12, s: "b"}, myStruct{n: 12, s: "a"}, 1},
198 {myStruct{n: 12, s: "a"}, myStruct{n: 12, s: "a"}, 0},
199 // ptr
200 {&a, &a, 0},
201 {(*int)(nil), (*int)(nil), 0},
202 {&a, &b, -1},
203 {(*int)(nil), &b, -1},
204 {&b, &a, 1},
205 {&b, (*int)(nil), 1},
206 // map
207 {ma, mb, -1},
208 {mb, ma, 1},
209 {ma, ma, 0},
210 {(map[int]bool)(nil), (map[int]bool)(nil), 0},
211 {(map[int]bool)(nil), ma, -1},
212 {ma, (map[int]bool)(nil), 1},
213 }
214 for i, tc := range testCases {
215 t.Run(fmt.Sprintf("#%d %[1]T(%[1]v) %[1]T(%[1]v)", i, tc.a, tc.b),
216 func(t *testing.T) {
217 checkCompare(t, tc.a, tc.b, tc.expected)
218 })
219 }
220
221 // cyclic references protection
222 pa := &myStruct{n: 42, p: &myStruct{n: 18}}
223 pa.p.p = pa.p
224 pb := &myStruct{n: 42, p: &myStruct{n: 18}}
225 pb.p.p = pb.p
226 checkCompare(t, pa, pb, 0)
227 }