| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package deepequal_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"math" |
| 9 |
"reflect" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/powerman/deepequal" |
| 13 |
) |
| 14 |
|
| 15 |
type Basic struct { |
| 16 |
x int |
| 17 |
y float32 |
| 18 |
} |
| 19 |
|
| 20 |
type NotBasic Basic |
| 21 |
|
| 22 |
type MyByte byte |
| 23 |
|
| 24 |
type MyBytes []byte |
| 25 |
|
| 26 |
type DeepEqualTest struct { |
| 27 |
a, b any |
| 28 |
eq bool |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
var ( |
| 33 |
fn1 func() |
| 34 |
fn2 func() |
| 35 |
fn3 = func() { fn1() } |
| 36 |
) |
| 37 |
|
| 38 |
type self struct{} |
| 39 |
|
| 40 |
type Loop *Loop |
| 41 |
|
| 42 |
type Loopy any |
| 43 |
|
| 44 |
var loop1, loop2 Loop |
| 45 |
|
| 46 |
var loopy1, loopy2 Loopy |
| 47 |
|
| 48 |
var cycleMap1, cycleMap2, cycleMap3 map[string]any |
| 49 |
|
| 50 |
type structWithSelfPtr struct { |
| 51 |
p *structWithSelfPtr |
| 52 |
s string |
| 53 |
} |
| 54 |
|
| 55 |
func init() { |
| 56 |
loop1 = &loop2 |
| 57 |
loop2 = &loop1 |
| 58 |
|
| 59 |
loopy1 = &loopy2 |
| 60 |
loopy2 = &loopy1 |
| 61 |
|
| 62 |
cycleMap1 = map[string]any{} |
| 63 |
cycleMap1["cycle"] = cycleMap1 |
| 64 |
cycleMap2 = map[string]any{} |
| 65 |
cycleMap2["cycle"] = cycleMap2 |
| 66 |
cycleMap3 = map[string]any{} |
| 67 |
cycleMap3["different"] = cycleMap3 |
| 68 |
} |
| 69 |
|
| 70 |
var deepEqualTests = []DeepEqualTest{ |
| 71 |
|
| 72 |
{nil, nil, true}, |
| 73 |
{1, 1, true}, |
| 74 |
{int32(1), int32(1), true}, |
| 75 |
{0.5, 0.5, true}, |
| 76 |
{float32(0.5), float32(0.5), true}, |
| 77 |
{"hello", "hello", true}, |
| 78 |
{make([]int, 10), make([]int, 10), true}, |
| 79 |
{&[3]int{1, 2, 3}, &[3]int{1, 2, 3}, true}, |
| 80 |
{Basic{1, 0.5}, Basic{1, 0.5}, true}, |
| 81 |
{error(nil), error(nil), true}, |
| 82 |
{map[int]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, true}, |
| 83 |
{fn1, fn2, true}, |
| 84 |
{[]byte{1, 2, 3}, []byte{1, 2, 3}, true}, |
| 85 |
{[]MyByte{1, 2, 3}, []MyByte{1, 2, 3}, true}, |
| 86 |
{MyBytes{1, 2, 3}, MyBytes{1, 2, 3}, true}, |
| 87 |
|
| 88 |
|
| 89 |
{1, 2, false}, |
| 90 |
{int32(1), int32(2), false}, |
| 91 |
{0.5, 0.6, false}, |
| 92 |
{float32(0.5), float32(0.6), false}, |
| 93 |
{"hello", "hey", false}, |
| 94 |
{make([]int, 10), make([]int, 11), false}, |
| 95 |
{&[3]int{1, 2, 3}, &[3]int{1, 2, 4}, false}, |
| 96 |
{Basic{1, 0.5}, Basic{1, 0.6}, false}, |
| 97 |
{Basic{1, 0}, Basic{2, 0}, false}, |
| 98 |
{map[int]string{1: "one", 3: "two"}, map[int]string{2: "two", 1: "one"}, false}, |
| 99 |
{map[int]string{1: "one", 2: "txo"}, map[int]string{2: "two", 1: "one"}, false}, |
| 100 |
{map[int]string{1: "one"}, map[int]string{2: "two", 1: "one"}, false}, |
| 101 |
{map[int]string{2: "two", 1: "one"}, map[int]string{1: "one"}, false}, |
| 102 |
{nil, 1, false}, |
| 103 |
{1, nil, false}, |
| 104 |
{fn1, fn3, false}, |
| 105 |
{fn3, fn3, false}, |
| 106 |
{[][]int{{1}}, [][]int{{2}}, false}, |
| 107 |
{&structWithSelfPtr{p: &structWithSelfPtr{s: "a"}}, &structWithSelfPtr{p: &structWithSelfPtr{s: "b"}}, false}, |
| 108 |
|
| 109 |
|
| 110 |
{math.NaN(), math.NaN(), false}, |
| 111 |
{&[1]float64{math.NaN()}, &[1]float64{math.NaN()}, false}, |
| 112 |
{&[1]float64{math.NaN()}, self{}, true}, |
| 113 |
{[]float64{math.NaN()}, []float64{math.NaN()}, false}, |
| 114 |
{[]float64{math.NaN()}, self{}, true}, |
| 115 |
{map[float64]float64{math.NaN(): 1}, map[float64]float64{1: 2}, false}, |
| 116 |
{map[float64]float64{math.NaN(): 1}, self{}, true}, |
| 117 |
|
| 118 |
|
| 119 |
{[]int{}, []int(nil), false}, |
| 120 |
{[]int{}, []int{}, true}, |
| 121 |
{[]int(nil), []int(nil), true}, |
| 122 |
{map[int]int{}, map[int]int(nil), false}, |
| 123 |
{map[int]int{}, map[int]int{}, true}, |
| 124 |
{map[int]int(nil), map[int]int(nil), true}, |
| 125 |
|
| 126 |
|
| 127 |
{1, 1.0, false}, |
| 128 |
{int32(1), int64(1), false}, |
| 129 |
{0.5, "hello", false}, |
| 130 |
{[]int{1, 2, 3}, [3]int{1, 2, 3}, false}, |
| 131 |
{&[3]any{1, 2, 4}, &[3]any{1, 2, "s"}, false}, |
| 132 |
{Basic{1, 0.5}, NotBasic{1, 0.5}, false}, |
| 133 |
{map[uint]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, false}, |
| 134 |
{[]byte{1, 2, 3}, []MyByte{1, 2, 3}, false}, |
| 135 |
{[]MyByte{1, 2, 3}, MyBytes{1, 2, 3}, false}, |
| 136 |
{[]byte{1, 2, 3}, MyBytes{1, 2, 3}, false}, |
| 137 |
|
| 138 |
|
| 139 |
{&loop1, &loop1, true}, |
| 140 |
{&loop1, &loop2, true}, |
| 141 |
{&loopy1, &loopy1, true}, |
| 142 |
{&loopy1, &loopy2, true}, |
| 143 |
{&cycleMap1, &cycleMap2, true}, |
| 144 |
{&cycleMap1, &cycleMap3, false}, |
| 145 |
} |
| 146 |
|
| 147 |
func TestDeepEqual(t *testing.T) { |
| 148 |
for _, test := range deepEqualTests { |
| 149 |
if test.b == (self{}) { |
| 150 |
test.b = test.a |
| 151 |
} |
| 152 |
if r := deepequal.DeepEqual(test.a, test.b); r != test.eq { |
| 153 |
t.Errorf("DeepEqual(%#v, %#v) = %v, want %v", test.a, test.b, r, test.eq) |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
type Recursive struct { |
| 159 |
x int |
| 160 |
r *Recursive |
| 161 |
} |
| 162 |
|
| 163 |
func TestDeepEqualRecursiveStruct(t *testing.T) { |
| 164 |
a, b := new(Recursive), new(Recursive) |
| 165 |
*a = Recursive{12, a} |
| 166 |
*b = Recursive{12, b} |
| 167 |
if !deepequal.DeepEqual(a, b) { |
| 168 |
t.Error("DeepEqual(recursive same) = false, want true") |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
type _Complex struct { |
| 173 |
a int |
| 174 |
b [3]*_Complex |
| 175 |
c *string |
| 176 |
d map[float64]float64 |
| 177 |
} |
| 178 |
|
| 179 |
func TestDeepEqualComplexStruct(t *testing.T) { |
| 180 |
m := make(map[float64]float64) |
| 181 |
stra, strb := "hello", "hello" |
| 182 |
a, b := new(_Complex), new(_Complex) |
| 183 |
*a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m} |
| 184 |
*b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m} |
| 185 |
if !deepequal.DeepEqual(a, b) { |
| 186 |
t.Error("DeepEqual(complex same) = false, want true") |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestDeepEqualComplexStructInequality(t *testing.T) { |
| 191 |
m := make(map[float64]float64) |
| 192 |
stra, strb := "hello", "helloo" |
| 193 |
a, b := new(_Complex), new(_Complex) |
| 194 |
*a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m} |
| 195 |
*b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m} |
| 196 |
if deepequal.DeepEqual(a, b) { |
| 197 |
t.Error("DeepEqual(complex different) = true, want false") |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
type UnexpT struct { |
| 202 |
m map[int]int |
| 203 |
} |
| 204 |
|
| 205 |
func TestDeepEqualUnexportedMap(t *testing.T) { |
| 206 |
|
| 207 |
x1 := UnexpT{map[int]int{1: 2}} |
| 208 |
x2 := UnexpT{map[int]int{1: 2}} |
| 209 |
if !deepequal.DeepEqual(&x1, &x2) { |
| 210 |
t.Error("DeepEqual(x1, x2) = false, want true") |
| 211 |
} |
| 212 |
|
| 213 |
y1 := UnexpT{map[int]int{2: 3}} |
| 214 |
if deepequal.DeepEqual(&x1, &y1) { |
| 215 |
t.Error("DeepEqual(x1, y1) = true, want false") |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
var deepEqualPerfTests = []struct { |
| 220 |
x, y any |
| 221 |
}{ |
| 222 |
{x: int8(99), y: int8(99)}, |
| 223 |
{x: []int8{99}, y: []int8{99}}, |
| 224 |
{x: int16(99), y: int16(99)}, |
| 225 |
{x: []int16{99}, y: []int16{99}}, |
| 226 |
{x: int32(99), y: int32(99)}, |
| 227 |
{x: []int32{99}, y: []int32{99}}, |
| 228 |
{x: int64(99), y: int64(99)}, |
| 229 |
{x: []int64{99}, y: []int64{99}}, |
| 230 |
{x: int(999999), y: int(999999)}, |
| 231 |
{x: []int{999999}, y: []int{999999}}, |
| 232 |
|
| 233 |
{x: uint8(99), y: uint8(99)}, |
| 234 |
{x: []uint8{99}, y: []uint8{99}}, |
| 235 |
{x: uint16(99), y: uint16(99)}, |
| 236 |
{x: []uint16{99}, y: []uint16{99}}, |
| 237 |
{x: uint32(99), y: uint32(99)}, |
| 238 |
{x: []uint32{99}, y: []uint32{99}}, |
| 239 |
{x: uint64(99), y: uint64(99)}, |
| 240 |
{x: []uint64{99}, y: []uint64{99}}, |
| 241 |
{x: uint(999999), y: uint(999999)}, |
| 242 |
{x: []uint{999999}, y: []uint{999999}}, |
| 243 |
{x: uintptr(999999), y: uintptr(999999)}, |
| 244 |
{x: []uintptr{999999}, y: []uintptr{999999}}, |
| 245 |
|
| 246 |
{x: float32(1.414), y: float32(1.414)}, |
| 247 |
{x: []float32{1.414}, y: []float32{1.414}}, |
| 248 |
{x: float64(1.414), y: float64(1.414)}, |
| 249 |
{x: []float64{1.414}, y: []float64{1.414}}, |
| 250 |
|
| 251 |
{x: complex64(1.414), y: complex64(1.414)}, |
| 252 |
{x: []complex64{1.414}, y: []complex64{1.414}}, |
| 253 |
{x: complex128(1.414), y: complex128(1.414)}, |
| 254 |
{x: []complex128{1.414}, y: []complex128{1.414}}, |
| 255 |
|
| 256 |
{x: true, y: true}, |
| 257 |
{x: []bool{true}, y: []bool{true}}, |
| 258 |
|
| 259 |
{x: "abcdef", y: "abcdef"}, |
| 260 |
{x: []string{"abcdef"}, y: []string{"abcdef"}}, |
| 261 |
|
| 262 |
{x: []byte("abcdef"), y: []byte("abcdef")}, |
| 263 |
{x: [][]byte{[]byte("abcdef")}, y: [][]byte{[]byte("abcdef")}}, |
| 264 |
|
| 265 |
{x: [6]byte{'a', 'b', 'c', 'a', 'b', 'c'}, y: [6]byte{'a', 'b', 'c', 'a', 'b', 'c'}}, |
| 266 |
{x: [][6]byte{{'a', 'b', 'c', 'a', 'b', 'c'}}, y: [][6]byte{{'a', 'b', 'c', 'a', 'b', 'c'}}}, |
| 267 |
} |
| 268 |
|
| 269 |
func TestDeepEqualAllocs(t *testing.T) { |
| 270 |
for _, tt := range deepEqualPerfTests { |
| 271 |
t.Run(reflect.ValueOf(tt.x).Type().String(), func(t *testing.T) { |
| 272 |
got := testing.AllocsPerRun(100, func() { |
| 273 |
if !deepequal.DeepEqual(tt.x, tt.y) { |
| 274 |
t.Errorf("DeepEqual(%v, %v)=false", tt.x, tt.y) |
| 275 |
} |
| 276 |
}) |
| 277 |
if int(got) != 0 { |
| 278 |
t.Errorf("DeepEqual(%v, %v) allocated %d times", tt.x, tt.y, int(got)) |
| 279 |
} |
| 280 |
}) |
| 281 |
} |
| 282 |
} |