z_all_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. // +build alltests
  4. // +build go1.7
  5. package codec
  6. // Run this using:
  7. // go test -tags=alltests -run=Suite -coverprofile=cov.out
  8. // go tool cover -html=cov.out
  9. //
  10. // Because build tags are a build time parameter, we will have to test out the
  11. // different tags separately.
  12. // Tags: x codecgen safe appengine notfastpath
  13. //
  14. // These tags should be added to alltests, e.g.
  15. // go test '-tags=alltests x codecgen' -run=Suite -coverprofile=cov.out
  16. //
  17. // To run all tests before submitting code, run:
  18. // a=( "" "safe" "codecgen" "notfastpath" "codecgen notfastpath" "codecgen safe" "safe notfastpath" )
  19. // for i in "${a[@]}"; do echo ">>>> TAGS: $i"; go test "-tags=alltests $i" -run=Suite; done
  20. //
  21. // This only works on go1.7 and above. This is when subtests and suites were supported.
  22. import "testing"
  23. // func TestMain(m *testing.M) {
  24. // println("calling TestMain")
  25. // // set some parameters
  26. // exitcode := m.Run()
  27. // os.Exit(exitcode)
  28. // }
  29. func testGroupResetFlags() {
  30. testUseMust = false
  31. testUseIoEncDec = -1
  32. testUseReset = false
  33. testMaxInitLen = 0
  34. testUseIoWrapper = false
  35. testNumRepeatString = 8
  36. testUseIoEncDec = -1
  37. testDepth = 0
  38. testDecodeOptions = DecodeOptions{}
  39. testEncodeOptions = EncodeOptions{}
  40. }
  41. func testSuite(t *testing.T, f func(t *testing.T)) {
  42. // find . -name "*_test.go" | xargs grep -e 'flag.' | cut -d '&' -f 2 | cut -d ',' -f 1 | grep -e '^test'
  43. // Disregard the following: testInitDebug, testSkipIntf, testJsonIndent (Need a test for it)
  44. testReinit() // so flag.Parse() is called first, and never called again
  45. testGroupResetFlags()
  46. testReinit()
  47. t.Run("optionsFalse", f)
  48. testUseMust = true
  49. testUseIoEncDec = 0
  50. testUseReset = true
  51. // xdebugf("with StructToArray=true")
  52. testDecodeOptions.InternString = true
  53. testDecodeOptions.MapValueReset = true
  54. // testDecodeOptions.SignedInteger = true
  55. // testDecodeOptions.SliceElementReset = true
  56. // testDecodeOptions.InterfaceReset = true
  57. // testDecodeOptions.PreferArrayOverSlice = true
  58. // testDecodeOptions.DeleteOnNilMapValue = true
  59. // testDecodeOptions.RawToString = true
  60. testEncodeOptions.StructToArray = true
  61. testEncodeOptions.Canonical = true
  62. testEncodeOptions.CheckCircularRef = true
  63. testEncodeOptions.RecursiveEmptyCheck = true
  64. // testEncodeOptions.Raw = true
  65. // testEncodeOptions.StringToRaw = true
  66. testReinit()
  67. t.Run("optionsTrue", f)
  68. // xdebugf("setting StructToArray=false")
  69. testEncodeOptions.StructToArray = false
  70. testDepth = 6
  71. testReinit()
  72. t.Run("optionsTrue-deepstruct", f)
  73. testDepth = 0
  74. // testEncodeOptions.AsSymbols = AsSymbolAll
  75. testUseIoWrapper = true
  76. testReinit()
  77. t.Run("optionsTrue-ioWrapper", f)
  78. testUseIoEncDec = -1
  79. // make buffer small enough so that we have to re-fill multiple times.
  80. testSkipRPCTests = true
  81. testUseIoEncDec = 128
  82. // testDecodeOptions.ReaderBufferSize = 128
  83. // testEncodeOptions.WriterBufferSize = 128
  84. testReinit()
  85. t.Run("optionsTrue-bufio", f)
  86. // testDecodeOptions.ReaderBufferSize = 0
  87. // testEncodeOptions.WriterBufferSize = 0
  88. testUseIoEncDec = -1
  89. testSkipRPCTests = false
  90. testNumRepeatString = 32
  91. testReinit()
  92. t.Run("optionsTrue-largestrings", f)
  93. // The following here MUST be tested individually, as they create
  94. // side effects i.e. the decoded value is different.
  95. // testDecodeOptions.MapValueReset = true // ok - no side effects
  96. // testDecodeOptions.InterfaceReset = true // error??? because we do deepEquals to verify
  97. // testDecodeOptions.ErrorIfNoField = true // error, as expected, as fields not there
  98. // testDecodeOptions.ErrorIfNoArrayExpand = true // no error, but no error case either
  99. // testDecodeOptions.PreferArrayOverSlice = true // error??? because slice != array.
  100. // .... however, update deepEqual to take this option
  101. // testReinit()
  102. // t.Run("optionsTrue-resetOptions", f)
  103. testGroupResetFlags()
  104. }
  105. /*
  106. find . -name "codec_test.go" | xargs grep -e '^func Test' | \
  107. cut -d '(' -f 1 | cut -d ' ' -f 2 | \
  108. while read f; do echo "t.Run(\"$f\", $f)"; done
  109. */
  110. func testCodecGroup(t *testing.T) {
  111. // println("running testcodecsuite")
  112. // <setup code>
  113. testJsonGroup(t)
  114. testBincGroup(t)
  115. testCborGroup(t)
  116. testMsgpackGroup(t)
  117. testSimpleGroup(t)
  118. // testSimpleMammothGroup(t)
  119. // testRpcGroup(t)
  120. testNonHandlesGroup(t)
  121. // <tear-down code>
  122. }
  123. func testJsonGroup(t *testing.T) {
  124. t.Run("TestJsonCodecsTable", TestJsonCodecsTable)
  125. t.Run("TestJsonCodecsMisc", TestJsonCodecsMisc)
  126. t.Run("TestJsonCodecsEmbeddedPointer", TestJsonCodecsEmbeddedPointer)
  127. t.Run("TestJsonCodecChan", TestJsonCodecChan)
  128. t.Run("TestJsonStdEncIntf", TestJsonStdEncIntf)
  129. t.Run("TestJsonMammoth", TestJsonMammoth)
  130. t.Run("TestJsonRaw", TestJsonRaw)
  131. t.Run("TestJsonRpcGo", TestJsonRpcGo)
  132. t.Run("TestJsonLargeInteger", TestJsonLargeInteger)
  133. t.Run("TestJsonDecodeNonStringScalarInStringContext", TestJsonDecodeNonStringScalarInStringContext)
  134. t.Run("TestJsonEncodeIndent", TestJsonEncodeIndent)
  135. t.Run("TestJsonSwallowAndZero", TestJsonSwallowAndZero)
  136. t.Run("TestJsonRawExt", TestJsonRawExt)
  137. t.Run("TestJsonMapStructKey", TestJsonMapStructKey)
  138. t.Run("TestJsonDecodeNilMapValue", TestJsonDecodeNilMapValue)
  139. t.Run("TestJsonEmbeddedFieldPrecedence", TestJsonEmbeddedFieldPrecedence)
  140. t.Run("TestJsonLargeContainerLen", TestJsonLargeContainerLen)
  141. t.Run("TestJsonMammothMapsAndSlices", TestJsonMammothMapsAndSlices)
  142. t.Run("TestJsonTime", TestJsonTime)
  143. t.Run("TestJsonUintToInt", TestJsonUintToInt)
  144. t.Run("TestJsonDifferentMapOrSliceType", TestJsonDifferentMapOrSliceType)
  145. t.Run("TestJsonScalars", TestJsonScalars)
  146. t.Run("TestJsonOmitempty", TestJsonOmitempty)
  147. t.Run("TestJsonIntfMapping", TestJsonIntfMapping)
  148. t.Run("TestJsonMissingFields", TestJsonMissingFields)
  149. t.Run("TestJsonMaxDepth", TestJsonMaxDepth)
  150. t.Run("TestJsonSelfExt", TestJsonSelfExt)
  151. t.Run("TestJsonBytesEncodedAsArray", TestJsonBytesEncodedAsArray)
  152. t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
  153. }
  154. func testBincGroup(t *testing.T) {
  155. t.Run("TestBincCodecsTable", TestBincCodecsTable)
  156. t.Run("TestBincCodecsMisc", TestBincCodecsMisc)
  157. t.Run("TestBincCodecsEmbeddedPointer", TestBincCodecsEmbeddedPointer)
  158. t.Run("TestBincStdEncIntf", TestBincStdEncIntf)
  159. t.Run("TestBincMammoth", TestBincMammoth)
  160. t.Run("TestBincRaw", TestBincRaw)
  161. t.Run("TestBincRpcGo", TestBincRpcGo)
  162. t.Run("TestBincUnderlyingType", TestBincUnderlyingType)
  163. t.Run("TestBincSwallowAndZero", TestBincSwallowAndZero)
  164. t.Run("TestBincRawExt", TestBincRawExt)
  165. t.Run("TestBincMapStructKey", TestBincMapStructKey)
  166. t.Run("TestBincDecodeNilMapValue", TestBincDecodeNilMapValue)
  167. t.Run("TestBincEmbeddedFieldPrecedence", TestBincEmbeddedFieldPrecedence)
  168. t.Run("TestBincLargeContainerLen", TestBincLargeContainerLen)
  169. t.Run("TestBincMammothMapsAndSlices", TestBincMammothMapsAndSlices)
  170. t.Run("TestBincTime", TestBincTime)
  171. t.Run("TestBincUintToInt", TestBincUintToInt)
  172. t.Run("TestBincDifferentMapOrSliceType", TestBincDifferentMapOrSliceType)
  173. t.Run("TestBincScalars", TestBincScalars)
  174. t.Run("TestBincOmitempty", TestBincOmitempty)
  175. t.Run("TestBincIntfMapping", TestBincIntfMapping)
  176. t.Run("TestBincMissingFields", TestBincMissingFields)
  177. t.Run("TestBincMaxDepth", TestBincMaxDepth)
  178. t.Run("TestBincSelfExt", TestBincSelfExt)
  179. t.Run("TestBincBytesEncodedAsArray", TestBincBytesEncodedAsArray)
  180. }
  181. func testCborGroup(t *testing.T) {
  182. t.Run("TestCborCodecsTable", TestCborCodecsTable)
  183. t.Run("TestCborCodecsMisc", TestCborCodecsMisc)
  184. t.Run("TestCborCodecsEmbeddedPointer", TestCborCodecsEmbeddedPointer)
  185. t.Run("TestCborMapEncodeForCanonical", TestCborMapEncodeForCanonical)
  186. t.Run("TestCborCodecChan", TestCborCodecChan)
  187. t.Run("TestCborStdEncIntf", TestCborStdEncIntf)
  188. t.Run("TestCborMammoth", TestCborMammoth)
  189. t.Run("TestCborRaw", TestCborRaw)
  190. t.Run("TestCborRpcGo", TestCborRpcGo)
  191. t.Run("TestCborSwallowAndZero", TestCborSwallowAndZero)
  192. t.Run("TestCborRawExt", TestCborRawExt)
  193. t.Run("TestCborMapStructKey", TestCborMapStructKey)
  194. t.Run("TestCborDecodeNilMapValue", TestCborDecodeNilMapValue)
  195. t.Run("TestCborEmbeddedFieldPrecedence", TestCborEmbeddedFieldPrecedence)
  196. t.Run("TestCborLargeContainerLen", TestCborLargeContainerLen)
  197. t.Run("TestCborMammothMapsAndSlices", TestCborMammothMapsAndSlices)
  198. t.Run("TestCborTime", TestCborTime)
  199. t.Run("TestCborUintToInt", TestCborUintToInt)
  200. t.Run("TestCborDifferentMapOrSliceType", TestCborDifferentMapOrSliceType)
  201. t.Run("TestCborScalars", TestCborScalars)
  202. t.Run("TestCborOmitempty", TestCborOmitempty)
  203. t.Run("TestCborIntfMapping", TestCborIntfMapping)
  204. t.Run("TestCborMissingFields", TestCborMissingFields)
  205. t.Run("TestCborMaxDepth", TestCborMaxDepth)
  206. t.Run("TestCborSelfExt", TestCborSelfExt)
  207. t.Run("TestCborBytesEncodedAsArray", TestCborBytesEncodedAsArray)
  208. t.Run("TestCborHalfFloat", TestCborHalfFloat)
  209. t.Run("TestCborSkipTags", TestCborSkipTags)
  210. }
  211. func testMsgpackGroup(t *testing.T) {
  212. t.Run("TestMsgpackCodecsTable", TestMsgpackCodecsTable)
  213. t.Run("TestMsgpackCodecsMisc", TestMsgpackCodecsMisc)
  214. t.Run("TestMsgpackCodecsEmbeddedPointer", TestMsgpackCodecsEmbeddedPointer)
  215. t.Run("TestMsgpackStdEncIntf", TestMsgpackStdEncIntf)
  216. t.Run("TestMsgpackMammoth", TestMsgpackMammoth)
  217. t.Run("TestMsgpackRaw", TestMsgpackRaw)
  218. t.Run("TestMsgpackRpcGo", TestMsgpackRpcGo)
  219. t.Run("TestMsgpackRpcSpec", TestMsgpackRpcSpec)
  220. t.Run("TestMsgpackSwallowAndZero", TestMsgpackSwallowAndZero)
  221. t.Run("TestMsgpackRawExt", TestMsgpackRawExt)
  222. t.Run("TestMsgpackMapStructKey", TestMsgpackMapStructKey)
  223. t.Run("TestMsgpackDecodeNilMapValue", TestMsgpackDecodeNilMapValue)
  224. t.Run("TestMsgpackEmbeddedFieldPrecedence", TestMsgpackEmbeddedFieldPrecedence)
  225. t.Run("TestMsgpackLargeContainerLen", TestMsgpackLargeContainerLen)
  226. t.Run("TestMsgpackMammothMapsAndSlices", TestMsgpackMammothMapsAndSlices)
  227. t.Run("TestMsgpackTime", TestMsgpackTime)
  228. t.Run("TestMsgpackUintToInt", TestMsgpackUintToInt)
  229. t.Run("TestMsgpackDifferentMapOrSliceType", TestMsgpackDifferentMapOrSliceType)
  230. t.Run("TestMsgpackScalars", TestMsgpackScalars)
  231. t.Run("TestMsgpackOmitempty", TestMsgpackOmitempty)
  232. t.Run("TestMsgpackIntfMapping", TestMsgpackIntfMapping)
  233. t.Run("TestMsgpackMissingFields", TestMsgpackMissingFields)
  234. t.Run("TestMsgpackMaxDepth", TestMsgpackMaxDepth)
  235. t.Run("TestMsgpackSelfExt", TestMsgpackSelfExt)
  236. t.Run("TestMsgpackBytesEncodedAsArray", TestMsgpackBytesEncodedAsArray)
  237. t.Run("TestMsgpackDecodeMapAndExtSizeMismatch", TestMsgpackDecodeMapAndExtSizeMismatch)
  238. }
  239. func testSimpleGroup(t *testing.T) {
  240. t.Run("TestSimpleCodecsTable", TestSimpleCodecsTable)
  241. t.Run("TestSimpleCodecsMisc", TestSimpleCodecsMisc)
  242. t.Run("TestSimpleCodecsEmbeddedPointer", TestSimpleCodecsEmbeddedPointer)
  243. t.Run("TestSimpleStdEncIntf", TestSimpleStdEncIntf)
  244. t.Run("TestSimpleMammoth", TestSimpleMammoth)
  245. t.Run("TestSimpleRaw", TestSimpleRaw)
  246. t.Run("TestSimpleRpcGo", TestSimpleRpcGo)
  247. t.Run("TestSimpleSwallowAndZero", TestSimpleSwallowAndZero)
  248. t.Run("TestSimpleRawExt", TestSimpleRawExt)
  249. t.Run("TestSimpleMapStructKey", TestSimpleMapStructKey)
  250. t.Run("TestSimpleDecodeNilMapValue", TestSimpleDecodeNilMapValue)
  251. t.Run("TestSimpleEmbeddedFieldPrecedence", TestSimpleEmbeddedFieldPrecedence)
  252. t.Run("TestSimpleLargeContainerLen", TestSimpleLargeContainerLen)
  253. t.Run("TestSimpleMammothMapsAndSlices", TestSimpleMammothMapsAndSlices)
  254. t.Run("TestSimpleTime", TestSimpleTime)
  255. t.Run("TestSimpleUintToInt", TestSimpleUintToInt)
  256. t.Run("TestSimpleDifferentMapOrSliceType", TestSimpleDifferentMapOrSliceType)
  257. t.Run("TestSimpleScalars", TestSimpleScalars)
  258. t.Run("TestSimpleOmitempty", TestSimpleOmitempty)
  259. t.Run("TestSimpleIntfMapping", TestSimpleIntfMapping)
  260. t.Run("TestSimpleMissingFields", TestSimpleMissingFields)
  261. t.Run("TestSimpleMaxDepth", TestSimpleMaxDepth)
  262. t.Run("TestSimpleSelfExt", TestSimpleSelfExt)
  263. t.Run("TestSimpleBytesEncodedAsArray", TestSimpleBytesEncodedAsArray)
  264. }
  265. func testSimpleMammothGroup(t *testing.T) {
  266. t.Run("TestSimpleMammothMapsAndSlices", TestSimpleMammothMapsAndSlices)
  267. }
  268. func testRpcGroup(t *testing.T) {
  269. t.Run("TestBincRpcGo", TestBincRpcGo)
  270. t.Run("TestSimpleRpcGo", TestSimpleRpcGo)
  271. t.Run("TestMsgpackRpcGo", TestMsgpackRpcGo)
  272. t.Run("TestCborRpcGo", TestCborRpcGo)
  273. t.Run("TestJsonRpcGo", TestJsonRpcGo)
  274. t.Run("TestMsgpackRpcSpec", TestMsgpackRpcSpec)
  275. }
  276. func testNonHandlesGroup(t *testing.T) {
  277. // grep "func Test" codec_test.go | grep -v -E '(Cbor|Json|Simple|Msgpack|Binc)'
  278. t.Run("TestBufioDecReader", TestBufioDecReader)
  279. t.Run("TestAtomic", TestAtomic)
  280. t.Run("TestAllEncCircularRef", TestAllEncCircularRef)
  281. t.Run("TestAllAnonCycle", TestAllAnonCycle)
  282. t.Run("TestMultipleEncDec", TestMultipleEncDec)
  283. t.Run("TestAllErrWriter", TestAllErrWriter)
  284. t.Run("TestMapRangeIndex", TestMapRangeIndex)
  285. }
  286. func TestCodecSuite(t *testing.T) {
  287. testSuite(t, testCodecGroup)
  288. testGroupResetFlags()
  289. oldIndent, oldCharsAsis, oldPreferFloat, oldMapKeyAsString :=
  290. testJsonH.Indent, testJsonH.HTMLCharsAsIs, testJsonH.PreferFloat, testJsonH.MapKeyAsString
  291. testMaxInitLen = 10
  292. testJsonH.Indent = 8
  293. testJsonH.HTMLCharsAsIs = true
  294. testJsonH.MapKeyAsString = true
  295. // testJsonH.PreferFloat = true
  296. testReinit()
  297. t.Run("json-spaces-htmlcharsasis-initLen10", testJsonGroup)
  298. testMaxInitLen = 10
  299. testJsonH.Indent = -1
  300. testJsonH.HTMLCharsAsIs = false
  301. testJsonH.MapKeyAsString = true
  302. // testJsonH.PreferFloat = false
  303. testReinit()
  304. t.Run("json-tabs-initLen10", testJsonGroup)
  305. testJsonH.Indent, testJsonH.HTMLCharsAsIs, testJsonH.PreferFloat, testJsonH.MapKeyAsString =
  306. oldIndent, oldCharsAsis, oldPreferFloat, oldMapKeyAsString
  307. oldIndefLen := testCborH.IndefiniteLength
  308. testCborH.IndefiniteLength = true
  309. testReinit()
  310. t.Run("cbor-indefinitelength", testCborGroup)
  311. testCborH.IndefiniteLength = oldIndefLen
  312. oldTimeRFC3339 := testCborH.TimeRFC3339
  313. testCborH.TimeRFC3339 = !testCborH.TimeRFC3339
  314. testReinit()
  315. t.Run("cbor-rfc3339", testCborGroup)
  316. testCborH.TimeRFC3339 = oldTimeRFC3339
  317. oldSkipUnexpectedTags := testCborH.SkipUnexpectedTags
  318. testCborH.SkipUnexpectedTags = !testCborH.SkipUnexpectedTags
  319. testReinit()
  320. t.Run("cbor-skip-tags", testCborGroup)
  321. testCborH.SkipUnexpectedTags = oldSkipUnexpectedTags
  322. oldSymbols := testBincH.AsSymbols
  323. testBincH.AsSymbols = 2 // AsSymbolNone
  324. testReinit()
  325. t.Run("binc-no-symbols", testBincGroup)
  326. testBincH.AsSymbols = 1 // AsSymbolAll
  327. testReinit()
  328. t.Run("binc-all-symbols", testBincGroup)
  329. testBincH.AsSymbols = oldSymbols
  330. oldWriteExt := testMsgpackH.WriteExt
  331. oldNoFixedNum := testMsgpackH.NoFixedNum
  332. testMsgpackH.WriteExt = !testMsgpackH.WriteExt
  333. testReinit()
  334. t.Run("msgpack-inverse-writeext", testMsgpackGroup)
  335. testMsgpackH.WriteExt = oldWriteExt
  336. testMsgpackH.NoFixedNum = !testMsgpackH.NoFixedNum
  337. testReinit()
  338. t.Run("msgpack-fixednum", testMsgpackGroup)
  339. testMsgpackH.NoFixedNum = oldNoFixedNum
  340. oldEncZeroValuesAsNil := testSimpleH.EncZeroValuesAsNil
  341. testSimpleH.EncZeroValuesAsNil = !testSimpleH.EncZeroValuesAsNil
  342. testUseMust = true
  343. testReinit()
  344. t.Run("simple-enczeroasnil", testSimpleMammothGroup) // testSimpleGroup
  345. testSimpleH.EncZeroValuesAsNil = oldEncZeroValuesAsNil
  346. oldRpcBufsize := testRpcBufsize
  347. testRpcBufsize = 0
  348. t.Run("rpc-buf-0", testRpcGroup)
  349. testRpcBufsize = 0
  350. t.Run("rpc-buf-00", testRpcGroup)
  351. testRpcBufsize = 0
  352. t.Run("rpc-buf-000", testRpcGroup)
  353. testRpcBufsize = 16
  354. t.Run("rpc-buf-16", testRpcGroup)
  355. testRpcBufsize = 2048
  356. t.Run("rpc-buf-2048", testRpcGroup)
  357. testRpcBufsize = oldRpcBufsize
  358. testGroupResetFlags()
  359. }
  360. // func TestCodecSuite(t *testing.T) {
  361. // testReinit() // so flag.Parse() is called first, and never called again
  362. // testDecodeOptions, testEncodeOptions = DecodeOptions{}, EncodeOptions{}
  363. // testGroupResetFlags()
  364. // testReinit()
  365. // t.Run("optionsFalse", func(t *testing.T) {
  366. // t.Run("TestJsonMammothMapsAndSlices", TestJsonMammothMapsAndSlices)
  367. // })
  368. // }