Skip to content

Commit 1bd7e92

Browse files
committed
codec: fix testEqual support for diff
- introduce testEqualOpts to generalize testEqual function: testEqual is for basic diff support, while testEqualFlex adds more config specific to these files - use testEqualFlex all over values_test_flex, which tracks many known types with "ignore unexported" - simplify one-off "one pass" execution of encode/decode - move how wraps and other types will continue to be supported
1 parent 047fd14 commit 1bd7e92

File tree

7 files changed

+121
-82
lines changed

7 files changed

+121
-82
lines changed

codec/1_init_run_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
// . "github.com/ugorji/go/codec"
1414

1515
gocmp "github.com/google/go-cmp/cmp"
16+
"github.com/google/go-cmp/cmp/cmpopts"
1617
)
1718

1819
type testBenchVars struct {
@@ -264,12 +265,28 @@ var errDeepEqualNotMatch = errors.New("not match")
264265

265266
// perform a simple DeepEqual expecting same values
266267
func testEqual(v1, v2 interface{}) (err error) {
267-
if !reflect.DeepEqual(v1, v2) {
268-
if testv.UseDiff {
269-
err = errors.New(gocmp.Diff(v1, v2))
270-
} else {
271-
err = errDeepEqualNotMatch
268+
return testEqualOpts(v1, v2, false, nil)
269+
}
270+
271+
func testEqualOpts(v1, v2 interface{}, nilEmptyEqual bool, ignoreUnexportedTypes []interface{}) error {
272+
if reflect.DeepEqual(v1, v2) {
273+
return nil
274+
}
275+
if testv.UseDiff {
276+
var optarr [4]gocmp.Option
277+
var opts = optarr[:0]
278+
if nilEmptyEqual {
279+
opts = append(opts, cmpopts.EquateEmpty())
272280
}
281+
if len(ignoreUnexportedTypes) > 0 {
282+
// MARKER 2025 - failing (not sure why)
283+
opts = append(opts, cmpopts.IgnoreUnexported(ignoreUnexportedTypes...))
284+
}
285+
s := gocmp.Diff(v1, v2, opts...)
286+
if s == "" {
287+
return nil
288+
}
289+
return errors.New(s)
273290
}
274-
return
291+
return errDeepEqualNotMatch
275292
}

codec/2_init_bench_test.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !codec.nobench && go1.24
1+
//go:build !codec.nobench && !nobench && go1.24
22

33
// Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
44
// Use of this source code is governed by a MIT license found in the LICENSE file.
@@ -155,6 +155,8 @@ func benchOnePassCheck(t *testing.T, name string, encfn benchEncFn, decfn benchD
155155
// benchOnePassLogf("-------------- %s ----------------", name)
156156
// }
157157
defer benchOnePassRecoverPanic(name)
158+
defer func(b bool) { testv.UseDiff = b }(testv.UseDiff)
159+
testv.UseDiff = true // show diffs if not equal
158160
runtime.GC()
159161
tnow := time.Now()
160162
buf, err := encfn(benchTs, nil)
@@ -176,17 +178,8 @@ func benchOnePassCheck(t *testing.T, name string, encfn benchEncFn, decfn benchD
176178
return
177179
}
178180
decDur := time.Since(tnow)
181+
benchOnePassLogf("\t%10s: len: %d bytes,\t encode: %v,\t decode: %v, diff: %v", name, encLen, encDur, decDur, testEqualOpts(benchTs, &ts2, true, []interface{}{ts2}))
179182
// if benchCheckDoDeepEqual {
180-
if benchVerify {
181-
if reflect.DeepEqual(benchTs, &ts2) {
182-
benchOnePassLogf("\t%10s: len: %d bytes,\t encode: %v,\t decode: %v,\tencoded == decoded", name, encLen, encDur, decDur)
183-
} else {
184-
err = errDeepEqualNotMatch
185-
benchOnePassLogf("\t%10s: len: %d bytes,\t encode: %v,\t decode: %v,\tencoded != decoded: %v", name, encLen, encDur, decDur, err)
186-
}
187-
} else {
188-
benchOnePassLogf("\t%10s: len: %d bytes,\t encode: %v,\t decode: %v", name, encLen, encDur, decDur)
189-
}
190183
}
191184

192185
func benchOnePassLogf(format string, args ...interface{}) {

codec/codec_bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !codec.nobench && go1.24
1+
//go:build !codec.nobench && !nobench && go1.24
22

33
// Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
44
// Use of this source code is governed by a MIT license found in the LICENSE file.

codec/codec_run_test.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ import (
5050
"sync/atomic"
5151
"testing"
5252
"time"
53-
54-
gocmp "github.com/google/go-cmp/cmp"
5553
)
5654

5755
func init() {
@@ -533,6 +531,10 @@ func testSetupWithChecks(t *testing.T, _ *Handle, allowParallel bool) (fn func()
533531
return
534532
}
535533

534+
func testEqualFlex(v1, v2 interface{}) error {
535+
return testEqualOpts(v1, v2, false, testStructsWithStructInfoField)
536+
}
537+
536538
func testCodecEncode(ts interface{}, bsIn []byte, fn func([]byte) *bytes.Buffer, h Handle, useMust bool) (bs []byte, err error) {
537539
return testSharedCodecEncode(ts, bsIn, fn, h, useMust)
538540
}
@@ -550,7 +552,7 @@ func testCheckErr(t *testing.T, err error) {
550552

551553
func testCheckEqual(t *testing.T, v1 interface{}, v2 interface{}, desc string) {
552554
t.Helper()
553-
if err := testEqual(v1, v2); err != nil {
555+
if err := testEqualFlex(v1, v2); err != nil {
554556
t.Logf("Not Equal: %s: %v", desc, err)
555557
if testv.Verbose {
556558
t.Logf("\tv1: %v, v2: %v", v1, v2)
@@ -922,7 +924,7 @@ func testUnmarshalErr(v interface{}, data []byte, h Handle, t *testing.T, name s
922924

923925
func testDeepEqualErr(v1, v2 interface{}, t *testing.T, name string) {
924926
t.Helper()
925-
testDeepEqual4Err(v1, v2, t, name, testEqual(v1, v2))
927+
testDeepEqual4Err(v1, v2, t, name, testEqualFlex(v1, v2))
926928
}
927929

928930
func testDeepEqualErrHandle(v1, v2 interface{}, h Handle, t *testing.T, name string) {
@@ -1092,7 +1094,7 @@ func testCodecTableOne(t *testing.T, testNil bool, h Handle,
10921094
}
10931095
continue
10941096
}
1095-
if err = testEqual(v0check, v1); err == nil {
1097+
if err = testEqualFlex(v0check, v1); err == nil {
10961098
if testv.Verbose {
10971099
t.Logf("++++++++ Before and After marshal matched\n")
10981100
}
@@ -1242,15 +1244,15 @@ func doTestCodecMiscOne(t *testing.T, h Handle) {
12421244

12431245
testCheckEqual(t, p, p2, "p=p2")
12441246
testCheckEqual(t, m, m2, "m=m2")
1245-
if err = testEqual(p, p2); err == nil {
1247+
if err = testEqualFlex(p, p2); err == nil {
12461248
if testv.Verbose {
12471249
t.Logf("p and p2 match")
12481250
}
12491251
} else {
12501252
t.Logf("Not Equal: %v. p: %v, p2: %v", err, p, p2)
12511253
t.FailNow()
12521254
}
1253-
if err = testEqual(m, m2); err == nil {
1255+
if err = testEqualFlex(m, m2); err == nil {
12541256
if testv.Verbose {
12551257
t.Logf("m and m2 match")
12561258
}
@@ -1396,7 +1398,7 @@ func doTestCodecChan(t *testing.T, h Handle) {
13961398
for j := range ch2 {
13971399
sl2 = append(sl2, j)
13981400
}
1399-
if err := testEqual(sl1, sl2); err != nil {
1401+
if err := testEqualFlex(sl1, sl2); err != nil {
14001402
t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
14011403
if testv.Verbose {
14021404
t.Logf("sl1: %#v, sl2: %#v", sl1, sl2)
@@ -1429,7 +1431,7 @@ func doTestCodecChan(t *testing.T, h Handle) {
14291431
// t.Logf(">>>> from chan: is nil? %v, %v", j == nil, j)
14301432
sl2 = append(sl2, j)
14311433
}
1432-
if err := testEqual(sl1, sl2); err != nil {
1434+
if err := testEqualFlex(sl1, sl2); err != nil {
14331435
t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
14341436
if testv.Verbose {
14351437
t.Logf("sl1: %#v, sl2: %#v", sl1, sl2)
@@ -1460,7 +1462,7 @@ func doTestCodecChan(t *testing.T, h Handle) {
14601462
for j := range ch2 {
14611463
sl2 = append(sl2, j)
14621464
}
1463-
if err := testEqual(sl1, sl2); err != nil {
1465+
if err := testEqualFlex(sl1, sl2); err != nil {
14641466
t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
14651467
t.FailNow()
14661468
}
@@ -1488,7 +1490,7 @@ func doTestCodecChan(t *testing.T, h Handle) {
14881490
for j := range ch2 {
14891491
sl2 = append(sl2, j)
14901492
}
1491-
if err := testEqual(sl1, sl2); err != nil {
1493+
if err := testEqualFlex(sl1, sl2); err != nil {
14921494
t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
14931495
t.FailNow()
14941496
}
@@ -1724,7 +1726,7 @@ func doTestStdEncIntf(t *testing.T, h Handle) {
17241726
e.MustEncode(a[0])
17251727
d := NewDecoderBytes(b, h)
17261728
d.MustDecode(a[1])
1727-
if err := testEqual(a[0], a[1]); err == nil {
1729+
if err := testEqualFlex(a[0], a[1]); err == nil {
17281730
if testv.Verbose {
17291731
t.Logf("++++ Objects match")
17301732
}
@@ -1957,7 +1959,7 @@ func doTestPythonGenStreams(t *testing.T, h Handle) {
19571959
}
19581960
//no need to indirect, because we pass a nil ptr, so we already have the value
19591961
//if v1 != nil { v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface() }
1960-
if err = testEqual(v, v1); err == nil {
1962+
if err = testEqualFlex(v, v1); err == nil {
19611963
if testv.Verbose {
19621964
t.Logf("++++++++ Objects match: %T, %v", v, v)
19631965
}
@@ -1976,7 +1978,7 @@ func doTestPythonGenStreams(t *testing.T, h Handle) {
19761978
t.FailNow()
19771979
continue
19781980
}
1979-
if err = testEqual(bsb, bss); err == nil {
1981+
if err = testEqualFlex(bsb, bss); err == nil {
19801982
if testv.Verbose {
19811983
t.Logf("++++++++ Bytes match")
19821984
}
@@ -3194,8 +3196,8 @@ func doTestStrucEncDec(t *testing.T, h Handle) {
31943196
name := h.Name()
31953197

31963198
{
3197-
var ts1 = newTestStruc(2, testv.NumRepeatString, false, !testv.SkipIntf, testv.MapStringKeyOnly)
3198-
var ts2 TestStruc
3199+
var ts1 = newTestStrucPlus(2, testv.NumRepeatString, false, !testv.SkipIntf, testv.MapStringKeyOnly)
3200+
var ts2 TestStrucPlus
31993201
bs := testMarshalErr(ts1, h, t, name)
32003202
testUnmarshalErr(&ts2, bs, h, t, name)
32013203
testDeepEqualErrHandle(ts1, &ts2, h, t, name)
@@ -3501,7 +3503,7 @@ func doTestNextValueBytes(t *testing.T, h Handle) {
35013503
[]string{"1", "22", "333", "4444"},
35023504
// use *TestStruc, not *TestStrucFlex, as *TestStrucFlex is harder to compare with deep equal
35033505
// Remember: *TestStruc was separated for this reason, affording comparing against other libraries
3504-
newTestStruc(testv.Depth, testv.NumRepeatString, false, false, true),
3506+
newTestStrucPlus(testv.Depth, testv.NumRepeatString, false, false, true),
35053507
"1223334444",
35063508
}
35073509
var out []byte
@@ -4169,12 +4171,5 @@ func testEqualH(v1, v2 interface{}, h Handle) (err error) {
41694171
if v1 != nil {
41704172
v1 = deepcopy(v1).Interface()
41714173
}
4172-
if !reflect.DeepEqual(v1, v2) {
4173-
if testv.UseDiff {
4174-
err = errors.New(gocmp.Diff(v1, v2))
4175-
} else {
4176-
err = errDeepEqualNotMatch
4177-
}
4178-
}
4179-
return
4174+
return testEqualOpts(v1, v2, false, []interface{}{v1})
41804175
}

codec/github_issue_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
)
1313

1414
func TestGH417(t *testing.T) {
15-
const numMapEntries = 1000 // 1000
16-
const numGoroutines = 100 // 100
15+
const numMapEntries = 1024 // 1000
16+
const numGoroutines = 64 // 100
1717

1818
type S string
1919
type E struct {

codec/values_flex_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ func init() {
1818
testWRepeated512 = wrapBytes(testARepeated512[:])
1919
}
2020

21+
// these have _struct struct{} fields in them
22+
var testStructsWithStructInfoField = []interface{}{
23+
codecgenC{},
24+
testStrucKeyTypeT0{},
25+
testStrucKeyTypeT1{},
26+
testStrucKeyTypeT2{},
27+
testStrucKeyTypeT3{},
28+
testStrucKeyTypeT4{},
29+
Sstructbig{},
30+
SstructbigToArray{},
31+
TestStrucFlex{},
32+
// testSelfExtHelper{},
33+
// TestStrucPlus{},
34+
}
35+
2136
const teststrucflexChanCap = 64
2237

2338
// This file contains values used by tests alone.

0 commit comments

Comments
 (0)