values_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* // +build testing */
  2. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a MIT license found in the LICENSE file.
  4. package codec
  5. // This file contains values used by tests and benchmarks.
  6. // The benchmarks will test performance against other libraries (encoding/json, json-iterator, bson, gob, etc).
  7. // Consequently, we only use values that will parse well in all engines,
  8. // and only leverage features that work across multiple libraries for a truer comparison.
  9. // For example,
  10. // - JSON/BSON do not like maps with keys that are not strings,
  11. // so we only use maps with string keys here.
  12. // - _struct options are not honored by other libraries,
  13. // so we don't use them in this file.
  14. import (
  15. "math"
  16. "strings"
  17. )
  18. type wrapSliceUint64 []uint64
  19. type wrapSliceString []string
  20. type wrapUint64 uint64
  21. type wrapString string
  22. type wrapUint64Slice []wrapUint64
  23. type wrapStringSlice []wrapString
  24. type stringUint64T struct {
  25. S string
  26. U uint64
  27. }
  28. type AnonInTestStruc struct {
  29. AS string
  30. AI64 int64
  31. AI16 int16
  32. AUi64 uint64
  33. ASslice []string
  34. AI64slice []int64
  35. AUi64slice []uint64
  36. AF64slice []float64
  37. AF32slice []float32
  38. // AMI32U32 map[int32]uint32
  39. // AMU32F64 map[uint32]float64 // json/bson do not like it
  40. AMSU16 map[string]uint16
  41. // use these to test 0-len or nil slices/maps/arrays
  42. AI64arr0 [0]int64
  43. A164slice0 []int64
  44. AUi64sliceN []uint64
  45. AMSU16N map[string]uint16
  46. AMSU16E map[string]uint16
  47. }
  48. type testSimpleFields struct {
  49. S string
  50. I64 int64
  51. I32 int32
  52. I16 int16
  53. I8 int8
  54. I64n int64
  55. I32n int32
  56. I16n int16
  57. I8n int8
  58. Ui64 uint64
  59. Ui32 uint32
  60. Ui16 uint16
  61. Ui8 uint8
  62. F64 float64
  63. F32 float32
  64. B bool
  65. By uint8 // byte: msgp doesn't like byte
  66. Sslice []string
  67. I64slice []int64
  68. I16slice []int16
  69. Ui64slice []uint64
  70. Ui8slice []uint8
  71. Bslice []bool
  72. Byslice []byte
  73. Iptrslice []*int64
  74. WrapSliceInt64 wrapSliceUint64
  75. WrapSliceString wrapSliceString
  76. Msi64 map[string]int64
  77. }
  78. type TestStrucCommon struct {
  79. S string
  80. I64 int64
  81. I32 int32
  82. I16 int16
  83. I8 int8
  84. I64n int64
  85. I32n int32
  86. I16n int16
  87. I8n int8
  88. Ui64 uint64
  89. Ui32 uint32
  90. Ui16 uint16
  91. Ui8 uint8
  92. F64 float64
  93. F32 float32
  94. B bool
  95. By uint8 // byte: msgp doesn't like byte
  96. Sslice []string
  97. I64slice []int64
  98. I16slice []int16
  99. Ui64slice []uint64
  100. Ui8slice []uint8
  101. Bslice []bool
  102. Byslice []byte
  103. Iptrslice []*int64
  104. WrapSliceInt64 wrapSliceUint64
  105. WrapSliceString wrapSliceString
  106. Msi64 map[string]int64
  107. Simplef testSimpleFields
  108. SstrUi64T []stringUint64T
  109. AnonInTestStruc
  110. NotAnon AnonInTestStruc
  111. // R Raw // Testing Raw must be explicitly turned on, so use standalone test
  112. // Rext RawExt // Testing RawExt is tricky, so use standalone test
  113. Nmap map[string]bool //don't set this, so we can test for nil
  114. Nslice []byte //don't set this, so we can test for nil
  115. Nint64 *int64 //don't set this, so we can test for nil
  116. }
  117. type TestStruc struct {
  118. // _struct struct{} `json:",omitempty"` //set omitempty for every field
  119. TestStrucCommon
  120. Mtsptr map[string]*TestStruc
  121. Mts map[string]TestStruc
  122. Its []*TestStruc
  123. Nteststruc *TestStruc
  124. }
  125. func populateTestStrucCommon(ts *TestStrucCommon, n int, bench, useInterface, useStringKeyOnly bool) {
  126. var i64a, i64b, i64c, i64d int64 = 64, 6464, 646464, 64646464
  127. // if bench, do not use uint64 values > math.MaxInt64, as bson, etc cannot decode them
  128. var a = AnonInTestStruc{
  129. // There's more leeway in altering this.
  130. AS: strRpt(n, "A-String"),
  131. AI64: -64646464,
  132. AI16: 1616,
  133. AUi64: 64646464,
  134. // (U+1D11E)G-clef character may be represented in json as "\uD834\uDD1E".
  135. // single reverse solidus character may be represented in json as "\u005C".
  136. // include these in ASslice below.
  137. ASslice: []string{
  138. strRpt(n, "Aone"),
  139. strRpt(n, "Atwo"),
  140. strRpt(n, "Athree"),
  141. strRpt(n, "Afour.reverse_solidus.\u005c"),
  142. strRpt(n, "Afive.Gclef.\U0001d11E\"ugorji\"done.")},
  143. AI64slice: []int64{
  144. 0, 1, -1, -22, 333, -4444, 55555, -666666,
  145. // msgpack ones
  146. -48, -32, -24, -8, 32, 127, 192, 255,
  147. // standard ones
  148. 0, -1, 1,
  149. math.MaxInt8, math.MaxInt8 + 4, math.MaxInt8 - 4,
  150. math.MaxInt16, math.MaxInt16 + 4, math.MaxInt16 - 4,
  151. math.MaxInt32, math.MaxInt32 + 4, math.MaxInt32 - 4,
  152. math.MaxInt64, math.MaxInt64 - 4,
  153. math.MinInt8, math.MinInt8 + 4, math.MinInt8 - 4,
  154. math.MinInt16, math.MinInt16 + 4, math.MinInt16 - 4,
  155. math.MinInt32, math.MinInt32 + 4, math.MinInt32 - 4,
  156. math.MinInt64, math.MinInt64 + 4,
  157. },
  158. AUi64slice: []uint64{
  159. 0, 1, 22, 333, 4444, 55555, 666666,
  160. // standard ones
  161. math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
  162. math.MaxUint16, math.MaxUint16 + 4, math.MaxUint16 - 4,
  163. math.MaxUint32, math.MaxUint32 + 4, math.MaxUint32 - 4,
  164. },
  165. AMSU16: map[string]uint16{strRpt(n, "1"): 1, strRpt(n, "22"): 2, strRpt(n, "333"): 3, strRpt(n, "4444"): 4},
  166. // Note: +/- inf, NaN, and other non-representable numbers should not be explicitly tested here
  167. AF64slice: []float64{
  168. 11.11e-11, -11.11e+11,
  169. 2.222E+12, -2.222E-12,
  170. -555.55E-5, 555.55E+5,
  171. 666.66E-6, -666.66E+6,
  172. 7777.7777E-7, -7777.7777E-7,
  173. -8888.8888E+8, 8888.8888E+8,
  174. -99999.9999E+9, 99999.9999E+9,
  175. // these below are hairy enough to need strconv.ParseFloat
  176. 33.33E-33, -33.33E+33,
  177. 44.44e+44, -44.44e-44,
  178. // standard ones
  179. 0, -1, 1,
  180. // math.Inf(1), math.Inf(-1),
  181. math.Pi, math.Phi, math.E,
  182. math.MaxFloat64, math.SmallestNonzeroFloat64,
  183. },
  184. AF32slice: []float32{
  185. 11.11e-11, -11.11e+11,
  186. 2.222E+12, -2.222E-12,
  187. -555.55E-5, 555.55E+5,
  188. 666.66E-6, -666.66E+6,
  189. 7777.7777E-7, -7777.7777E-7,
  190. -8888.8888E+8, 8888.8888E+8,
  191. -99999.9999E+9, 99999.9999E+9,
  192. // these below are hairy enough to need strconv.ParseFloat
  193. 33.33E-33, -33.33E+33,
  194. // standard ones
  195. 0, -1, 1,
  196. // math.Float32frombits(0x7FF00000), math.Float32frombits(0xFFF00000), //+inf and -inf
  197. math.MaxFloat32, math.SmallestNonzeroFloat32,
  198. },
  199. A164slice0: []int64{},
  200. AUi64sliceN: nil,
  201. AMSU16N: nil,
  202. AMSU16E: map[string]uint16{},
  203. }
  204. if !bench {
  205. a.AUi64slice = append(a.AUi64slice, math.MaxUint64, math.MaxUint64-4)
  206. }
  207. *ts = TestStrucCommon{
  208. S: strRpt(n, `some really really cool names that are nigerian and american like "ugorji melody nwoke" - get it? `),
  209. // set the numbers close to the limits
  210. I8: math.MaxInt8 * 2 / 3, // 8,
  211. I8n: math.MinInt8 * 2 / 3, // 8,
  212. I16: math.MaxInt16 * 2 / 3, // 16,
  213. I16n: math.MinInt16 * 2 / 3, // 16,
  214. I32: math.MaxInt32 * 2 / 3, // 32,
  215. I32n: math.MinInt32 * 2 / 3, // 32,
  216. I64: math.MaxInt64 * 2 / 3, // 64,
  217. I64n: math.MinInt64 * 2 / 3, // 64,
  218. Ui64: math.MaxUint64 * 2 / 3, // 64
  219. Ui32: math.MaxUint32 * 2 / 3, // 32
  220. Ui16: math.MaxUint16 * 2 / 3, // 16
  221. Ui8: math.MaxUint8 * 2 / 3, // 8
  222. F32: 3.402823e+38, // max representable float32 without losing precision
  223. F64: 3.40281991833838838338e+53,
  224. B: true,
  225. By: 5,
  226. Sslice: []string{strRpt(n, "one"), strRpt(n, "two"), strRpt(n, "three")},
  227. I64slice: []int64{1111, 2222, 3333},
  228. I16slice: []int16{44, 55, 66},
  229. Ui64slice: []uint64{12121212, 34343434, 56565656},
  230. Ui8slice: []uint8{210, 211, 212},
  231. Bslice: []bool{true, false, true, false},
  232. Byslice: []byte{13, 14, 15},
  233. Msi64: map[string]int64{
  234. strRpt(n, "one"): 1,
  235. strRpt(n, "two"): 2,
  236. strRpt(n, "\"three\""): 3,
  237. },
  238. WrapSliceInt64: []uint64{4, 16, 64, 256},
  239. WrapSliceString: []string{strRpt(n, "4"), strRpt(n, "16"), strRpt(n, "64"), strRpt(n, "256")},
  240. // R: Raw([]byte("goodbye")),
  241. // Rext: RawExt{ 120, []byte("hello"), }, // TODO: don't set this - it's hard to test
  242. // DecodeNaked bombs here, because the stringUint64T is decoded as a map,
  243. // and a map cannot be the key type of a map.
  244. // Thus, don't initialize this here.
  245. // Msu2wss: map[stringUint64T]wrapStringSlice{
  246. // {"5", 5}: []wrapString{"1", "2", "3", "4", "5"},
  247. // {"3", 3}: []wrapString{"1", "2", "3"},
  248. // },
  249. // make Simplef same as top-level
  250. // TODO: should this have slightly different values???
  251. Simplef: testSimpleFields{
  252. S: strRpt(n, `some really really cool names that are nigerian and american like "ugorji melody nwoke" - get it? `),
  253. // set the numbers close to the limits
  254. I8: math.MaxInt8 * 2 / 3, // 8,
  255. I8n: math.MinInt8 * 2 / 3, // 8,
  256. I16: math.MaxInt16 * 2 / 3, // 16,
  257. I16n: math.MinInt16 * 2 / 3, // 16,
  258. I32: math.MaxInt32 * 2 / 3, // 32,
  259. I32n: math.MinInt32 * 2 / 3, // 32,
  260. I64: math.MaxInt64 * 2 / 3, // 64,
  261. I64n: math.MinInt64 * 2 / 3, // 64,
  262. Ui64: math.MaxUint64 * 2 / 3, // 64
  263. Ui32: math.MaxUint32 * 2 / 3, // 32
  264. Ui16: math.MaxUint16 * 2 / 3, // 16
  265. Ui8: math.MaxUint8 * 2 / 3, // 8
  266. F32: 3.402823e+38, // max representable float32 without losing precision
  267. F64: 3.40281991833838838338e+53,
  268. B: true,
  269. By: 5,
  270. Sslice: []string{strRpt(n, "one"), strRpt(n, "two"), strRpt(n, "three")},
  271. I64slice: []int64{1111, 2222, 3333},
  272. I16slice: []int16{44, 55, 66},
  273. Ui64slice: []uint64{12121212, 34343434, 56565656},
  274. Ui8slice: []uint8{210, 211, 212},
  275. Bslice: []bool{true, false, true, false},
  276. Byslice: []byte{13, 14, 15},
  277. Msi64: map[string]int64{
  278. strRpt(n, "one"): 1,
  279. strRpt(n, "two"): 2,
  280. strRpt(n, "\"three\""): 3,
  281. },
  282. WrapSliceInt64: []uint64{4, 16, 64, 256},
  283. WrapSliceString: []string{strRpt(n, "4"), strRpt(n, "16"), strRpt(n, "64"), strRpt(n, "256")},
  284. },
  285. SstrUi64T: []stringUint64T{{"1", 1}, {"2", 2}, {"3", 3}, {"4", 4}},
  286. AnonInTestStruc: a,
  287. NotAnon: a,
  288. }
  289. if bench {
  290. ts.Ui64 = math.MaxInt64 * 2 / 3
  291. ts.Simplef.Ui64 = ts.Ui64
  292. }
  293. //For benchmarks, some things will not work.
  294. if !bench {
  295. //json and bson require string keys in maps
  296. //ts.M = map[interface{}]interface{}{
  297. // true: "true",
  298. // int8(9): false,
  299. //}
  300. //gob cannot encode nil in element in array (encodeArray: nil element)
  301. ts.Iptrslice = []*int64{nil, &i64a, nil, &i64b, nil, &i64c, nil, &i64d, nil}
  302. // ts.Iptrslice = nil
  303. }
  304. if !useStringKeyOnly {
  305. // ts.AnonInTestStruc.AMU32F64 = map[uint32]float64{1: 1, 2: 2, 3: 3} // Json/Bson barf
  306. }
  307. }
  308. func newTestStruc(depth, n int, bench, useInterface, useStringKeyOnly bool) (ts *TestStruc) {
  309. ts = &TestStruc{}
  310. populateTestStrucCommon(&ts.TestStrucCommon, n, bench, useInterface, useStringKeyOnly)
  311. if depth > 0 {
  312. depth--
  313. if ts.Mtsptr == nil {
  314. ts.Mtsptr = make(map[string]*TestStruc)
  315. }
  316. if ts.Mts == nil {
  317. ts.Mts = make(map[string]TestStruc)
  318. }
  319. ts.Mtsptr[strRpt(n, "0")] = newTestStruc(depth, n, bench, useInterface, useStringKeyOnly)
  320. ts.Mts[strRpt(n, "0")] = *(ts.Mtsptr[strRpt(n, "0")])
  321. ts.Its = append(ts.Its, ts.Mtsptr[strRpt(n, "0")])
  322. }
  323. return
  324. }
  325. var testStrRptMap = make(map[int]map[string]string)
  326. func strRpt(n int, s string) string {
  327. if false {
  328. // fmt.Printf(">>>> calling strings.Repeat on n: %d, key: %s\n", n, s)
  329. return strings.Repeat(s, n)
  330. }
  331. m1, ok := testStrRptMap[n]
  332. if !ok {
  333. // fmt.Printf(">>>> making new map for n: %v\n", n)
  334. m1 = make(map[string]string)
  335. testStrRptMap[n] = m1
  336. }
  337. v1, ok := m1[s]
  338. if !ok {
  339. // fmt.Printf(">>>> creating new entry for key: %s\n", s)
  340. v1 = strings.Repeat(s, n)
  341. m1[s] = v1
  342. }
  343. return v1
  344. }
  345. // func wstrRpt(n int, s string) wrapBytes {
  346. // return wrapBytes(bytes.Repeat([]byte(s), n))
  347. // }