Browse Source

codec: test: move unique codec-only features into values_flex_test.go

This way, benchmarks will be a true apple-to-apples comparison.

For example, we move the types and fields which are there for
testing configured extensions, and the maps with non-string keys.
Ugorji Nwoke 8 years ago
parent
commit
06aece491b
2 changed files with 63 additions and 30 deletions
  1. 39 1
      codec/values_flex_test.go
  2. 24 29
      codec/values_test.go

+ 39 - 1
codec/values_flex_test.go

@@ -5,7 +5,24 @@
 
 package codec
 
-// This file contains values used by tests and benchmarks.
+// This file contains values used by tests alone.
+// This is where we may try out different things,
+// that other engines may not support or may barf upon
+// e.g. custom extensions for wrapped types, maps with non-string keys, etc.
+
+type wrapInt64 int64
+type wrapUint8 uint8
+type wrapBytes []uint8
+
+var testWRepeated512 wrapBytes
+
+func init() {
+	var testARepeated512 [512]byte
+	for i := range testARepeated512 {
+		testARepeated512[i] = 'A'
+	}
+	testWRepeated512 = wrapBytes(testARepeated512[:])
+}
 
 type TestStrucFlex struct {
 	_struct struct{} `codec:",omitempty"` //set omitempty for every field
@@ -19,6 +36,10 @@ type TestStrucFlex struct {
 	Mui2wss map[uint64]wrapStringSlice
 	Msu2wss map[stringUint64T]wrapStringSlice
 
+	Ci64       wrapInt64
+	Swrapbytes []wrapBytes
+	Swrapuint8 []wrapUint8
+
 	//M map[interface{}]interface{}  `json:"-",bson:"-"`
 	Mtsptr     map[string]*TestStrucFlex
 	Mts        map[string]TestStrucFlex
@@ -53,6 +74,23 @@ func newTestStrucFlex(depth, n int, bench, useInterface, useStringKeyOnly bool)
 			-44: "minus forty four",
 		},
 		Mbu64: map[bool]struct{}{false: {}, true: {}},
+
+		Ci64: -22,
+		Swrapbytes: []wrapBytes{ // lengths of 1, 2, 4, 8, 16, 32, 64, 128, 256,
+			testWRepeated512[:1],
+			testWRepeated512[:2],
+			testWRepeated512[:4],
+			testWRepeated512[:8],
+			testWRepeated512[:16],
+			testWRepeated512[:32],
+			testWRepeated512[:64],
+			testWRepeated512[:128],
+			testWRepeated512[:256],
+			testWRepeated512[:512],
+		},
+		Swrapuint8: []wrapUint8{
+			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
+		},
 	}
 	populateTestStrucCommon(&ts.testStrucCommon, n, bench, useInterface, useStringKeyOnly)
 	if depth > 0 {

+ 24 - 29
codec/values_test.go

@@ -6,11 +6,11 @@
 package codec
 
 // This file contains values used by tests and benchmarks.
-// JSON/BSON do not like maps with keys that are not strings,
+// Consequently, we only use values that will parse well in all engines.
+// For example, JSON/BSON do not like maps with keys that are not strings,
 // so we only use maps with string keys here.
 
 import (
-	"bytes"
 	"math"
 	"strings"
 	"time"
@@ -18,13 +18,10 @@ import (
 
 type wrapSliceUint64 []uint64
 type wrapSliceString []string
-type wrapUint8 uint8
-type wrapInt64 int64
 type wrapUint64 uint64
 type wrapString string
 type wrapUint64Slice []wrapUint64
 type wrapStringSlice []wrapString
-type wrapBytes []uint8
 type wrapBytesSlice []wrapBytes
 
 type stringUint64T struct {
@@ -162,10 +159,6 @@ type testStrucCommon struct {
 	// make this one omitempty (so it is excluded if nil).
 	*AnonInTestStrucIntf `codec:",omitempty"`
 
-	Ci64       wrapInt64
-	Swrapbytes []wrapBytes
-	Swrapuint8 []wrapUint8
-
 	// R          Raw // Testing Raw must be explicitly turned on, so use standalone test
 	// Rext RawExt // Testing RawExt is tricky, so use standalone test
 
@@ -355,22 +348,6 @@ func populateTestStrucCommon(ts *testStrucCommon, n int, bench, useInterface, us
 		WrapSliceInt64:  []uint64{4, 16, 64, 256},
 		WrapSliceString: []string{strRpt(n, "4"), strRpt(n, "16"), strRpt(n, "64"), strRpt(n, "256")},
 
-		Ci64: -22,
-		Swrapbytes: []wrapBytes{ // lengths of 1, 2, 4, 8, 16, 32, 64, 128, 256,
-			wstrRpt(1, "A"),
-			wstrRpt(2, "A"),
-			wstrRpt(4, "A"),
-			wstrRpt(8, "A"),
-			wstrRpt(16, "A"),
-			wstrRpt(32, "A"),
-			wstrRpt(64, "A"),
-			wstrRpt(128, "A"),
-			wstrRpt(256, "A"),
-			wstrRpt(512, "A"),
-		},
-		Swrapuint8: []wrapUint8{
-			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
-		},
 		// R: Raw([]byte("goodbye")),
 		// Rext: RawExt{ 120, []byte("hello"), }, // TODO: don't set this - it's hard to test
 
@@ -479,10 +456,28 @@ func newTestStruc(depth, n int, bench, useInterface, useStringKeyOnly bool) (ts
 	return
 }
 
+var testStrRptMap = make(map[int]map[string]string)
+
 func strRpt(n int, s string) string {
-	return strings.Repeat(s, n)
+	if false {
+		// fmt.Printf(">>>> calling strings.Repeat on n: %d, key: %s\n", n, s)
+		return strings.Repeat(s, n)
+	}
+	m1, ok := testStrRptMap[n]
+	if !ok {
+		// fmt.Printf(">>>> making new map for n: %v\n", n)
+		m1 = make(map[string]string)
+		testStrRptMap[n] = m1
+	}
+	v1, ok := m1[s]
+	if !ok {
+		// fmt.Printf(">>>> creating new entry for key: %s\n", s)
+		v1 = strings.Repeat(s, n)
+		m1[s] = v1
+	}
+	return v1
 }
 
-func wstrRpt(n int, s string) wrapBytes {
-	return wrapBytes(bytes.Repeat([]byte(s), n))
-}
+// func wstrRpt(n int, s string) wrapBytes {
+// 	 return wrapBytes(bytes.Repeat([]byte(s), n))
+// }