-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmpVal.go
314 lines (278 loc) · 8 KB
/
cmpVal.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package testhelper
import (
"fmt"
"math"
"reflect"
"testing"
"time"
"golang.org/x/exp/constraints"
)
// almostEqual returns true if a and b are within epsilon of one
// another. Copied from github.com/nickwells/mathutil.mod/mathutil
func almostEqual[T constraints.Float](a, b, epsilon T) bool {
if a == b {
return true
}
return math.Abs(float64(a-b)) < float64(epsilon)
}
// reportFloatDiff reports the difference between two float values
func reportFloatDiff[T constraints.Float](t *testing.T, name string,
act, exp T,
) {
t.Helper()
t.Logf("\t: expected %s: %5g\n", name, exp)
t.Logf("\t: actual %s: %5g\n", name, act)
charCnt := len(name) + len("expected") + 1
t.Logf("\t: %*s: %5g\n", charCnt, "diff", math.Abs(float64(act-exp)))
t.Errorf("\t: %s is incorrect\n", name)
}
// DiffFloat compares the actual against the expected value and reports
// an error if they differ by more than epsilon
func DiffFloat[T constraints.Float](t *testing.T, id, name string,
act, exp, epsilon T,
) bool {
t.Helper()
if !almostEqual(act, exp, epsilon) {
t.Log(id)
reportFloatDiff(t, name, act, exp)
return true
}
return false
}
// DiffInt compares the actual against the expected value and reports an
// error if they differ
func DiffInt[T constraints.Integer](t *testing.T, id, name string,
act, exp T,
) bool {
t.Helper()
if act != exp {
t.Log(id)
t.Logf("\t: expected %s: %5d\n", name, exp)
t.Logf("\t: actual %s: %5d\n", name, act)
charCnt := len(name) + len("expected") + 1
t.Logf("\t: %*s: %5d\n", charCnt, "diff",
int64(math.Abs(float64(act-exp))))
t.Errorf("\t: %s is incorrect\n", name)
return true
}
return false
}
// stringFirstDiff returns the index of the first differing rune between act
// and exp.
func stringFirstDiff[S ~string](act, exp S) int {
actRunes := []rune(act)
expRunes := []rune(exp)
for i, r := range actRunes {
if i >= len(expRunes) {
return i
}
if r != expRunes[i] {
return i
}
}
return len(actRunes)
}
// reportStringDiff reports the difference between two strings
func reportStringDiff[S ~string](t *testing.T, name string, act, exp S) {
t.Helper()
t.Logf("\t: expected %s (length: %4d): %q\n", name, len(exp), exp)
t.Logf("\t: actual %s (length: %4d): %q\n", name, len(act), act)
t.Logf("\t: first difference at rune %d\n", stringFirstDiff(act, exp))
t.Errorf("\t: %s is incorrect\n", name)
}
// DiffString compares the actual against the expected value and reports an
// error if they differ
func DiffString[S ~string](t *testing.T, id, name string, act, exp S) bool {
t.Helper()
if act != exp {
t.Log(id)
reportStringDiff(t, name, act, exp)
return true
}
return false
}
// DiffStringer compares the actual against the expected value and reports an
// error if they differ. It will report them as different if one is nil or
// has a nil value and the other isn't/doesn't or if they are both non-nil
// and the string values differ.
func DiffStringer(t *testing.T, id, name string, actS, expS fmt.Stringer) bool {
t.Helper()
actIsNil := actS == nil || reflect.ValueOf(actS).IsNil()
expIsNil := expS == nil || reflect.ValueOf(expS).IsNil()
if actIsNil && expIsNil {
return false
}
if actIsNil {
t.Log(id)
t.Logf("\t: expected %s is non-nil\n", name)
t.Logf("\t: actual %s is nil\n", name)
t.Errorf("\t: %s is incorrect\n", name)
return true
}
if expIsNil {
t.Log(id)
t.Logf("\t: expected %s is nil\n", name)
t.Logf("\t: actual %s is non-nil\n", name)
t.Errorf("\t: %s is incorrect\n", name)
return true
}
return DiffString(t, id, name, actS.String(), expS.String())
}
// DiffBool compares the actual against the expected value and reports an
// error if they differ
func DiffBool(t *testing.T, id, name string, act, exp bool) bool {
t.Helper()
if act != exp {
t.Log(id)
t.Logf("\t: expected %s: %v\n", name, exp)
t.Logf("\t: actual %s: %v\n", name, act)
t.Errorf("\t: %s is incorrect\n", name)
return true
}
return false
}
// DiffTime compares the actual against the expected value and reports an
// error if they differ
func DiffTime(t *testing.T, id, name string, act, exp time.Time) bool {
t.Helper()
if d := act.Sub(exp); d != 0 {
t.Log(id)
t.Logf("\t: expected %s: %v\n", name, exp)
t.Logf("\t: actual %s: %v\n", name, act)
t.Logf("\t: difference: %v\n", d)
t.Errorf("\t: %s is incorrect\n", name)
return true
}
return false
}
// DiffErr compares the actual against the expected value and reports an
// error if they differ. Note that it compares the string representation and
// not the error type so there might be a mismatch.
func DiffErr(t *testing.T, id, name string, act, exp error) bool {
t.Helper()
if act == nil && exp == nil {
return false
}
if act == nil && exp != nil ||
act != nil && exp == nil ||
act.Error() != exp.Error() {
t.Log(id)
t.Logf("\t: expected %s: %v\n", name, exp)
t.Logf("\t: actual %s: %v\n", name, act)
t.Errorf("\t: %s is incorrect\n", name)
return true
}
return false
}
const MaxReportedDiffs = 5
// reportDiffCount reports the number of differences found
func reportDiffCount(t *testing.T, name string, diffCount int) {
t.Helper()
if diffCount > 0 {
diffStr := "differences"
if diffCount == 1 {
diffStr = "difference"
}
t.Logf("\t: %d %s found\n", diffCount, diffStr)
t.Errorf("\t: %s is incorrect\n", name)
}
}
// reportMaxDiffsShown logs an elipsis to show that more differences have
// been found but that they have been elided. This is only done for the first
// elided difference.
func reportMaxDiffsShown(t *testing.T, diffCount int) {
t.Helper()
if diffCount == (MaxReportedDiffs + 1) {
t.Log("\t: ...\n")
}
}
// reportSliceLenDiff reports if the lengths of the slices differ. It returns
// true if so and false otherwise.
func reportSliceLenDiff(t *testing.T, id, name string, act, exp int) bool {
t.Helper()
if act != exp {
t.Log(id)
t.Logf("\t: expected %s length: %4d\n", name, exp)
t.Logf("\t: actual %s length: %4d\n", name, act)
t.Errorf("\t: %s is incorrect\n", name)
return true
}
return false
}
// DiffSlice compares the actual against the expected value and reports
// an error if they differ. At most MaxReportedDiffs are reported.
func DiffSlice[C comparable](t *testing.T, id, name string, act, exp []C) bool {
t.Helper()
if reportSliceLenDiff(t, id, name, len(act), len(exp)) {
return true
}
diffCount := 0
for i, v := range act {
if v != exp[i] {
diffCount++
if diffCount > MaxReportedDiffs {
reportMaxDiffsShown(t, diffCount)
continue
}
if diffCount == 1 {
t.Log(id)
}
t.Logf("\t: expected %s [%d]: %v\n", name, i, exp[i])
t.Logf("\t: actual %s [%d]: %v\n", name, i, v)
}
}
reportDiffCount(t, name, diffCount)
return diffCount > 0
}
// DiffFloat64Slice compares the actual against the expected value and reports
// an error if they differ. At most MaxReportedDiffs are reported.
func DiffFloatSlice[F constraints.Float](t *testing.T, id, name string,
act, exp []F, epsilon F,
) bool {
t.Helper()
if reportSliceLenDiff(t, id, name, len(act), len(exp)) {
return true
}
diffCount := 0
for i, v := range act {
if !almostEqual(v, exp[i], epsilon) {
diffCount++
if diffCount > MaxReportedDiffs {
reportMaxDiffsShown(t, diffCount)
continue
}
if diffCount == 1 {
t.Log(id)
}
reportFloatDiff(t, fmt.Sprintf("%s [%d]", name, i), v, exp[i])
}
}
reportDiffCount(t, name, diffCount)
return diffCount > 0
}
// DiffStringSlice compares the actual against the expected value and reports
// an error if they differ. At most MaxReportedDiffs are reported.
func DiffStringSlice[S ~string](t *testing.T, id, name string,
act, exp []S,
) bool {
t.Helper()
if reportSliceLenDiff(t, id, name, len(act), len(exp)) {
return true
}
diffCount := 0
for i, s := range act {
if s != exp[i] {
diffCount++
if diffCount > MaxReportedDiffs {
reportMaxDiffsShown(t, diffCount)
continue
}
if diffCount == 1 {
t.Log(id)
}
reportStringDiff(t, fmt.Sprintf("%s [%d]", name, i), s, exp[i])
}
}
reportDiffCount(t, name, diffCount)
return diffCount > 0
}