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
|
package testutils
import (
"testing"
"github.com/go-quicktest/qt"
)
func TestIsDeepCopy(t *testing.T) {
type s struct {
basic int
array [1]*int
array0 [0]int
ptr *int
slice []*int
ifc any
m map[*int]*int
rec *s
}
key := 1
copy := func() *s {
v := &s{
0,
[...]*int{new(int)},
[...]int{},
new(int),
[]*int{new(int)},
new(int),
map[*int]*int{&key: new(int)},
nil,
}
v.rec = v
return v
}
a, b := copy(), copy()
qt.Check(t, qt.IsNil(IsDeepCopy(a, b).Check(nil)))
a.basic++
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"basic": .*`))
a = copy()
(*a.array[0])++
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"array": index 0: .*`))
a = copy()
a.array[0] = nil
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"array": index 0: .*`))
a = copy()
a.array = b.array
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"array": index 0: .*`))
a = copy()
(*a.ptr)++
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"ptr": .*`))
a = copy()
a.ptr = b.ptr
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"ptr": .*`))
a = copy()
(*a.slice[0])++
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"slice": .*`))
a = copy()
a.slice[0] = nil
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"slice": .*`))
a = copy()
a.slice = nil
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"slice": .*`))
a = copy()
a.slice = b.slice
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"slice": .*`))
a = copy()
*(a.ifc.(*int))++
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"ifc": .*`))
a = copy()
a.ifc = b.ifc
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"ifc": .*`))
a = copy()
a.rec = b.rec
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"rec": .*`))
a = copy()
a.m = b.m
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"m": .*`))
a = copy()
(*a.m[&key])++
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"m": .*`))
a = copy()
a.m[new(int)] = new(int)
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"m": .*`))
a = copy()
delete(a.m, &key)
qt.Check(t, qt.ErrorMatches(IsDeepCopy(a, b).Check(nil), `"m": .*`))
}
|