1
2
3
4
5
6
7 package test_helpers
8
9 import (
10 "fmt"
11 "math/rand/v2"
12 "reflect"
13 "slices"
14 "testing"
15 )
16
17
18
19
20
21
22 type EqVector[T any, M EqMask] interface {
23 comparable
24 Equal(T) M
25 Len() int
26 }
27
28
29 type EqMask interface {
30 comparable
31 All() bool
32 }
33
34
35
36
37
38
39
40
41 func CheckVectorEqual[T EqVector[T, M], M EqMask, E number](t *testing.T, load func([]E) T, eq, ne func(x, y T) bool, values []E) {
42 t.Helper()
43 values = distinct(values)
44 var zero T
45 a, b := make([]E, zero.Len()), make([]E, zero.Len())
46
47 check := func(a, b []E) {
48 t.Helper()
49 x, y := load(a), load(b)
50 want := slices.Equal(a, b)
51 if got := eq(x, y); got != want {
52 t.Fatalf("%v == %v is %v, want %v", a, b, got, want)
53 }
54 if got := ne(x, y); got == want {
55 t.Fatalf("%v != %v is %v, want %v", a, b, got, !want)
56 }
57 if got := x.Equal(y).All(); got != want {
58 t.Fatalf("%v.Equal(%v).All() is %v, want %v", a, b, got, want)
59 }
60 checkEqualForms(t, x, y, want, func() string { return fmt.Sprintf("%v and %v", a, b) })
61 }
62
63
64
65
66 r := rand.New(rand.NewPCG(1, 2))
67 for range 10 {
68 pickN(r, a, values)
69 copy(b, a)
70 check(a, b)
71 for i := range b {
72 for _, v := range values {
73 b[i] = v
74 check(a, b)
75 }
76 b[i] = a[i]
77 }
78 }
79
80
81 for range 20 {
82 pickN(r, a, values)
83 x := load(a)
84 m := map[T]int{x: 1}
85 m[load(a)]++
86
87
88
89 if x == x {
90 if len(m) != 1 || m[x] != 2 {
91 t.Fatalf("map keyed by %v twice is %v, want one key with value 2", a, m)
92 }
93 } else {
94 if len(m) != 2 {
95 t.Fatalf("map keyed by %v (which has a NaN) twice has %d keys, want 2", a, len(m))
96 }
97 if _, ok := m[x]; ok {
98 t.Fatalf("found key %v, which has a NaN, in map", a)
99 }
100 }
101 }
102
103 if !reflect.TypeFor[T]().Comparable() {
104 t.Errorf("reflect reports %T as not comparable", zero)
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117 func CheckMaskEqual[T EqVector[T, M], M EqMask, E number](t *testing.T, load func([]E) T, eq, ne func(x, y M) bool) {
118 t.Helper()
119 var zeroVec T
120 n := zeroVec.Len()
121 a, b, zeros := make([]bool, n), make([]bool, n), load(make([]E, n))
122 toMask := func(s []bool) M {
123 v := make([]E, n)
124 for i, b := range s {
125 if !b {
126 v[i] = 1
127 }
128 }
129 return load(v).Equal(zeros)
130 }
131
132 check := func(a, b []bool) {
133 t.Helper()
134 x, y := toMask(a), toMask(b)
135 want := slices.Equal(a, b)
136 if got := eq(x, y); got != want {
137 t.Fatalf("mask %v == %v is %v, want %v", a, b, got, want)
138 }
139 if got := ne(x, y); got == want {
140 t.Fatalf("mask %v != %v is %v, want %v", a, b, got, !want)
141 }
142 checkEqualForms(t, x, y, want, func() string { return fmt.Sprintf("masks %v and %v", a, b) })
143 }
144
145
146
147 r := rand.New(rand.NewPCG(1, 2))
148 for range 10 {
149 pickN(r, a, []bool{false, true})
150 copy(b, a)
151 check(a, b)
152 for i := range b {
153 b[i] = !b[i]
154 check(a, b)
155 b[i] = a[i]
156 }
157 }
158
159
160 for range 20 {
161 pickN(r, a, []bool{false, true})
162 m := map[M]int{toMask(a): 1}
163 m[toMask(a)]++
164 if len(m) != 1 || m[toMask(a)] != 2 {
165 t.Fatalf("map keyed by mask %v twice is %v, want one key with value 2", a, m)
166 }
167 }
168
169 if !reflect.TypeFor[M]().Comparable() {
170 t.Errorf("reflect reports %T as not comparable", zeroVec.Equal(zeroVec))
171 }
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185 func checkEqualForms[V comparable](t *testing.T, x, y V, want bool, desc func() string) {
186 t.Helper()
187 if got := x == y; got != want {
188 t.Fatalf("generic == on %s is %v, want %v", desc(), got, want)
189 }
190 if got := any(x) == any(y); got != want {
191 t.Fatalf("== on %s as interfaces is %v, want %v", desc(), got, want)
192 }
193 if got := (struct {
194 v V
195 n int
196 }{x, 1} == struct {
197 v V
198 n int
199 }{y, 1}); got != want {
200 t.Fatalf("== on structs containing %s is %v, want %v", desc(), got, want)
201 }
202 if got := [2]V{x, x} == [2]V{x, y}; got != want {
203 t.Fatalf("== on arrays containing %s is %v, want %v", desc(), got, want)
204 }
205 if got := reflect.ValueOf(x).Equal(reflect.ValueOf(y)); got != want {
206 t.Fatalf("reflect.Value.Equal on %s is %v, want %v", desc(), got, want)
207 }
208 if !reflect.ValueOf(x).Comparable() {
209 t.Fatalf("reflect.Value.Comparable on %s is false", desc())
210 }
211 }
212
213
214 func pickN[E any](r *rand.Rand, out, options []E) {
215 for i := range out {
216 out[i] = options[r.IntN(len(options))]
217 }
218 }
219
220
221
222 func distinct[E number](values []E) []E {
223 same := func(x, y E) bool {
224
225 if x != y {
226 return isSameFloat(x, y, 0)
227 }
228 return !isDifferentSignZero(x, y)
229 }
230 var out []E
231 for _, v := range values {
232 if !slices.ContainsFunc(out, func(w E) bool { return same(v, w) }) {
233 out = append(out, v)
234 }
235 }
236 return out
237 }
238
View as plain text