z_all_test.go 14 KB

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