z_all_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // +build alltests
  2. // +build go1.7
  3. package codec
  4. // Run this using:
  5. // go test -tags=alltests -run=Suite -coverprofile=cov.out
  6. // go tool cover -html=cov.out
  7. //
  8. // Because build tags are a build time parameter, we will have to test out the
  9. // different tags separately.
  10. // Tags: x codecgen safe appengine notfastpath
  11. //
  12. // These tags should be added to alltests, e.g.
  13. // go test '-tags=alltests x codecgen' -run=Suite -coverprofile=cov.out
  14. //
  15. // To run all tests before submitting code, run:
  16. // a=( "" "safe" "codecgen" "notfastpath" "codecgen notfastpath" "codecgen safe" "safe notfastpath" )
  17. // for i in "${a[@]}"; do echo ">>>> TAGS: $i"; go test "-tags=alltests $i" -run=Suite; done
  18. //
  19. // This only works on go1.7 and above. This is when subtests and suites were supported.
  20. import "testing"
  21. // func TestMain(m *testing.M) {
  22. // println("calling TestMain")
  23. // // set some parameters
  24. // exitcode := m.Run()
  25. // os.Exit(exitcode)
  26. // }
  27. func testGroupResetFlags() {
  28. testUseMust = false
  29. testCanonical = false
  30. testUseMust = false
  31. testInternStr = false
  32. testUseIoEncDec = -1
  33. testStructToArray = false
  34. testCheckCircRef = false
  35. testUseReset = false
  36. testMaxInitLen = 0
  37. testUseIoWrapper = false
  38. testNumRepeatString = 8
  39. }
  40. func testSuite(t *testing.T, f func(t *testing.T)) {
  41. // find . -name "*_test.go" | xargs grep -e 'flag.' | cut -d '&' -f 2 | cut -d ',' -f 1 | grep -e '^test'
  42. // Disregard the following: testVerbose, testInitDebug, testSkipIntf, testJsonIndent (Need a test for it)
  43. testReinit() // so flag.Parse() is called first, and never called again
  44. testDecodeOptions = DecodeOptions{}
  45. testEncodeOptions = EncodeOptions{}
  46. testGroupResetFlags()
  47. testReinit()
  48. t.Run("optionsFalse", f)
  49. testCanonical = true
  50. testUseMust = true
  51. testInternStr = true
  52. testUseIoEncDec = 0
  53. testStructToArray = true
  54. testCheckCircRef = true
  55. testUseReset = true
  56. testDecodeOptions.MapValueReset = true
  57. testReinit()
  58. t.Run("optionsTrue", f)
  59. testEncodeOptions.AsSymbols = AsSymbolAll
  60. testUseIoWrapper = true
  61. testReinit()
  62. t.Run("optionsTrue-ioWrapper", f)
  63. testUseIoEncDec = -1
  64. testDepth = 6
  65. testReinit()
  66. t.Run("optionsTrue-deepstruct", f)
  67. // make buffer small enough so that we have to re-fill multiple times.
  68. testSkipRPCTests = true
  69. testUseIoEncDec = 128
  70. // testDecodeOptions.ReaderBufferSize = 128
  71. // testEncodeOptions.WriterBufferSize = 128
  72. testReinit()
  73. t.Run("optionsTrue-bufio", f)
  74. // testDecodeOptions.ReaderBufferSize = 0
  75. // testEncodeOptions.WriterBufferSize = 0
  76. testUseIoEncDec = -1
  77. testSkipRPCTests = false
  78. testNumRepeatString = 32
  79. testReinit()
  80. t.Run("optionsTrue-largestrings", f)
  81. // The following here MUST be tested individually, as they create
  82. // side effects i.e. the decoded value is different.
  83. // testDecodeOptions.MapValueReset = true // ok - no side effects
  84. // testDecodeOptions.InterfaceReset = true // error??? because we do deepEquals to verify
  85. // testDecodeOptions.ErrorIfNoField = true // error, as expected, as fields not there
  86. // testDecodeOptions.ErrorIfNoArrayExpand = true // no error, but no error case either
  87. // testDecodeOptions.PreferArrayOverSlice = true // error??? because slice != array.
  88. // .... however, update deepEqual to take this option
  89. // testReinit()
  90. // t.Run("optionsTrue-resetOptions", f)
  91. testGroupResetFlags()
  92. }
  93. /*
  94. z='codec_test.go'
  95. find . -name "$z" | xargs grep -e '^func Test' | \
  96. cut -d '(' -f 1 | cut -d ' ' -f 2 | \
  97. while read f; do echo "t.Run(\"$f\", $f)"; done
  98. */
  99. func testCodecGroup(t *testing.T) {
  100. // println("running testcodecsuite")
  101. // <setup code>
  102. t.Run("TestBincCodecsTable", TestBincCodecsTable)
  103. t.Run("TestBincCodecsMisc", TestBincCodecsMisc)
  104. t.Run("TestBincCodecsEmbeddedPointer", TestBincCodecsEmbeddedPointer)
  105. t.Run("TestBincStdEncIntf", TestBincStdEncIntf)
  106. t.Run("TestBincMammoth", TestBincMammoth)
  107. t.Run("TestSimpleCodecsTable", TestSimpleCodecsTable)
  108. t.Run("TestSimpleCodecsMisc", TestSimpleCodecsMisc)
  109. t.Run("TestSimpleCodecsEmbeddedPointer", TestSimpleCodecsEmbeddedPointer)
  110. t.Run("TestSimpleStdEncIntf", TestSimpleStdEncIntf)
  111. t.Run("TestSimpleMammoth", TestSimpleMammoth)
  112. t.Run("TestMsgpackCodecsTable", TestMsgpackCodecsTable)
  113. t.Run("TestMsgpackCodecsMisc", TestMsgpackCodecsMisc)
  114. t.Run("TestMsgpackCodecsEmbeddedPointer", TestMsgpackCodecsEmbeddedPointer)
  115. t.Run("TestMsgpackStdEncIntf", TestMsgpackStdEncIntf)
  116. t.Run("TestMsgpackMammoth", TestMsgpackMammoth)
  117. t.Run("TestCborCodecsTable", TestCborCodecsTable)
  118. t.Run("TestCborCodecsMisc", TestCborCodecsMisc)
  119. t.Run("TestCborCodecsEmbeddedPointer", TestCborCodecsEmbeddedPointer)
  120. t.Run("TestCborMapEncodeForCanonical", TestCborMapEncodeForCanonical)
  121. t.Run("TestCborCodecChan", TestCborCodecChan)
  122. t.Run("TestCborStdEncIntf", TestCborStdEncIntf)
  123. t.Run("TestCborMammoth", TestCborMammoth)
  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("TestBincRaw", TestBincRaw)
  132. t.Run("TestMsgpackRaw", TestMsgpackRaw)
  133. t.Run("TestSimpleRaw", TestSimpleRaw)
  134. t.Run("TestCborRaw", TestCborRaw)
  135. t.Run("TestAllEncCircularRef", TestAllEncCircularRef)
  136. t.Run("TestAllAnonCycle", TestAllAnonCycle)
  137. t.Run("TestBincRpcGo", TestBincRpcGo)
  138. t.Run("TestSimpleRpcGo", TestSimpleRpcGo)
  139. t.Run("TestMsgpackRpcGo", TestMsgpackRpcGo)
  140. t.Run("TestCborRpcGo", TestCborRpcGo)
  141. t.Run("TestJsonRpcGo", TestJsonRpcGo)
  142. t.Run("TestMsgpackRpcSpec", TestMsgpackRpcSpec)
  143. t.Run("TestBincUnderlyingType", TestBincUnderlyingType)
  144. t.Run("TestJsonLargeInteger", TestJsonLargeInteger)
  145. t.Run("TestJsonDecodeNonStringScalarInStringContext", TestJsonDecodeNonStringScalarInStringContext)
  146. t.Run("TestJsonEncodeIndent", TestJsonEncodeIndent)
  147. t.Run("TestJsonSwallowAndZero", TestJsonSwallowAndZero)
  148. t.Run("TestCborSwallowAndZero", TestCborSwallowAndZero)
  149. t.Run("TestMsgpackSwallowAndZero", TestMsgpackSwallowAndZero)
  150. t.Run("TestBincSwallowAndZero", TestBincSwallowAndZero)
  151. t.Run("TestSimpleSwallowAndZero", TestSimpleSwallowAndZero)
  152. t.Run("TestJsonRawExt", TestJsonRawExt)
  153. t.Run("TestCborRawExt", TestCborRawExt)
  154. t.Run("TestMsgpackRawExt", TestMsgpackRawExt)
  155. t.Run("TestBincRawExt", TestBincRawExt)
  156. t.Run("TestSimpleRawExt", TestSimpleRawExt)
  157. t.Run("TestJsonMapStructKey", TestJsonMapStructKey)
  158. t.Run("TestCborMapStructKey", TestCborMapStructKey)
  159. t.Run("TestMsgpackMapStructKey", TestMsgpackMapStructKey)
  160. t.Run("TestBincMapStructKey", TestBincMapStructKey)
  161. t.Run("TestSimpleMapStructKey", TestSimpleMapStructKey)
  162. t.Run("TestJsonDecodeNilMapValue", TestJsonDecodeNilMapValue)
  163. t.Run("TestCborDecodeNilMapValue", TestCborDecodeNilMapValue)
  164. t.Run("TestMsgpackDecodeNilMapValue", TestMsgpackDecodeNilMapValue)
  165. t.Run("TestBincDecodeNilMapValue", TestBincDecodeNilMapValue)
  166. t.Run("TestSimpleDecodeNilMapValue", TestSimpleDecodeNilMapValue)
  167. t.Run("TestJsonEmbeddedFieldPrecedence", TestJsonEmbeddedFieldPrecedence)
  168. t.Run("TestCborEmbeddedFieldPrecedence", TestCborEmbeddedFieldPrecedence)
  169. t.Run("TestMsgpackEmbeddedFieldPrecedence", TestMsgpackEmbeddedFieldPrecedence)
  170. t.Run("TestBincEmbeddedFieldPrecedence", TestBincEmbeddedFieldPrecedence)
  171. t.Run("TestSimpleEmbeddedFieldPrecedence", TestSimpleEmbeddedFieldPrecedence)
  172. t.Run("TestJsonLargeContainerLen", TestJsonLargeContainerLen)
  173. t.Run("TestCborLargeContainerLen", TestCborLargeContainerLen)
  174. t.Run("TestMsgpackLargeContainerLen", TestMsgpackLargeContainerLen)
  175. t.Run("TestBincLargeContainerLen", TestBincLargeContainerLen)
  176. t.Run("TestSimpleLargeContainerLen", TestSimpleLargeContainerLen)
  177. t.Run("TestJsonMammothMapsAndSlices", TestJsonMammothMapsAndSlices)
  178. t.Run("TestCborMammothMapsAndSlices", TestCborMammothMapsAndSlices)
  179. t.Run("TestMsgpackMammothMapsAndSlices", TestMsgpackMammothMapsAndSlices)
  180. t.Run("TestBincMammothMapsAndSlices", TestBincMammothMapsAndSlices)
  181. t.Run("TestSimpleMammothMapsAndSlices", TestSimpleMammothMapsAndSlices)
  182. t.Run("TestJsonTime", TestJsonTime)
  183. t.Run("TestCborTime", TestCborTime)
  184. t.Run("TestMsgpackTime", TestMsgpackTime)
  185. t.Run("TestBincTime", TestBincTime)
  186. t.Run("TestSimpleTime", TestSimpleTime)
  187. t.Run("TestJsonUintToInt", TestJsonUintToInt)
  188. t.Run("TestCborUintToInt", TestCborUintToInt)
  189. t.Run("TestMsgpackUintToInt", TestMsgpackUintToInt)
  190. t.Run("TestBincUintToInt", TestBincUintToInt)
  191. t.Run("TestSimpleUintToInt", TestSimpleUintToInt)
  192. t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
  193. t.Run("TestCborHalfFloat", TestCborHalfFloat)
  194. // <tear-down code>
  195. }
  196. func testJsonGroup(t *testing.T) {
  197. t.Run("TestJsonCodecsTable", TestJsonCodecsTable)
  198. t.Run("TestJsonCodecsMisc", TestJsonCodecsMisc)
  199. t.Run("TestJsonCodecsEmbeddedPointer", TestJsonCodecsEmbeddedPointer)
  200. t.Run("TestJsonCodecChan", TestJsonCodecChan)
  201. t.Run("TestJsonStdEncIntf", TestJsonStdEncIntf)
  202. t.Run("TestJsonMammoth", TestJsonMammoth)
  203. t.Run("TestJsonRaw", TestJsonRaw)
  204. t.Run("TestJsonRpcGo", TestJsonRpcGo)
  205. t.Run("TestJsonLargeInteger", TestJsonLargeInteger)
  206. t.Run("TestJsonDecodeNonStringScalarInStringContext", TestJsonDecodeNonStringScalarInStringContext)
  207. t.Run("TestJsonEncodeIndent", TestJsonEncodeIndent)
  208. t.Run("TestJsonSwallowAndZero", TestJsonSwallowAndZero)
  209. t.Run("TestJsonRawExt", TestJsonRawExt)
  210. t.Run("TestJsonMapStructKey", TestJsonMapStructKey)
  211. t.Run("TestJsonDecodeNilMapValue", TestJsonDecodeNilMapValue)
  212. t.Run("TestJsonEmbeddedFieldPrecedence", TestJsonEmbeddedFieldPrecedence)
  213. t.Run("TestJsonLargeContainerLen", TestJsonLargeContainerLen)
  214. t.Run("TestJsonMammothMapsAndSlices", TestJsonMammothMapsAndSlices)
  215. t.Run("TestJsonInvalidUnicode", TestJsonInvalidUnicode)
  216. t.Run("TestJsonTime", TestJsonTime)
  217. t.Run("TestJsonUintToInt", TestJsonUintToInt)
  218. }
  219. func testBincGroup(t *testing.T) {
  220. t.Run("TestBincCodecsTable", TestBincCodecsTable)
  221. t.Run("TestBincCodecsMisc", TestBincCodecsMisc)
  222. t.Run("TestBincCodecsEmbeddedPointer", TestBincCodecsEmbeddedPointer)
  223. t.Run("TestBincStdEncIntf", TestBincStdEncIntf)
  224. t.Run("TestBincMammoth", TestBincMammoth)
  225. t.Run("TestBincRaw", TestBincRaw)
  226. t.Run("TestSimpleRpcGo", TestSimpleRpcGo)
  227. t.Run("TestBincUnderlyingType", TestBincUnderlyingType)
  228. t.Run("TestBincSwallowAndZero", TestBincSwallowAndZero)
  229. t.Run("TestBincRawExt", TestBincRawExt)
  230. t.Run("TestBincMapStructKey", TestBincMapStructKey)
  231. t.Run("TestBincDecodeNilMapValue", TestBincDecodeNilMapValue)
  232. t.Run("TestBincEmbeddedFieldPrecedence", TestBincEmbeddedFieldPrecedence)
  233. t.Run("TestBincLargeContainerLen", TestBincLargeContainerLen)
  234. t.Run("TestBincMammothMapsAndSlices", TestBincMammothMapsAndSlices)
  235. t.Run("TestBincTime", TestBincTime)
  236. t.Run("TestBincUintToInt", TestBincUintToInt)
  237. }
  238. func testCborGroup(t *testing.T) {
  239. t.Run("TestCborCodecsTable", TestCborCodecsTable)
  240. t.Run("TestCborCodecsMisc", TestCborCodecsMisc)
  241. t.Run("TestCborCodecsEmbeddedPointer", TestCborCodecsEmbeddedPointer)
  242. t.Run("TestCborMapEncodeForCanonical", TestCborMapEncodeForCanonical)
  243. t.Run("TestCborCodecChan", TestCborCodecChan)
  244. t.Run("TestCborStdEncIntf", TestCborStdEncIntf)
  245. t.Run("TestCborMammoth", TestCborMammoth)
  246. t.Run("TestCborRaw", TestCborRaw)
  247. t.Run("TestCborRpcGo", TestCborRpcGo)
  248. t.Run("TestCborSwallowAndZero", TestCborSwallowAndZero)
  249. t.Run("TestCborRawExt", TestCborRawExt)
  250. t.Run("TestCborMapStructKey", TestCborMapStructKey)
  251. t.Run("TestCborDecodeNilMapValue", TestCborDecodeNilMapValue)
  252. t.Run("TestCborEmbeddedFieldPrecedence", TestCborEmbeddedFieldPrecedence)
  253. t.Run("TestCborLargeContainerLen", TestCborLargeContainerLen)
  254. t.Run("TestCborMammothMapsAndSlices", TestCborMammothMapsAndSlices)
  255. t.Run("TestCborTime", TestCborTime)
  256. t.Run("TestCborUintToInt", TestCborUintToInt)
  257. t.Run("TestCborHalfFloat", TestCborHalfFloat)
  258. }
  259. func testMsgpackGroup(t *testing.T) {
  260. t.Run("TestMsgpackCodecsTable", TestMsgpackCodecsTable)
  261. t.Run("TestMsgpackCodecsMisc", TestMsgpackCodecsMisc)
  262. t.Run("TestMsgpackCodecsEmbeddedPointer", TestMsgpackCodecsEmbeddedPointer)
  263. t.Run("TestMsgpackStdEncIntf", TestMsgpackStdEncIntf)
  264. t.Run("TestMsgpackMammoth", TestMsgpackMammoth)
  265. t.Run("TestMsgpackRaw", TestMsgpackRaw)
  266. t.Run("TestMsgpackRpcGo", TestMsgpackRpcGo)
  267. t.Run("TestMsgpackRpcSpec", TestMsgpackRpcSpec)
  268. t.Run("TestMsgpackSwallowAndZero", TestMsgpackSwallowAndZero)
  269. t.Run("TestMsgpackRawExt", TestMsgpackRawExt)
  270. t.Run("TestMsgpackMapStructKey", TestMsgpackMapStructKey)
  271. t.Run("TestMsgpackDecodeNilMapValue", TestMsgpackDecodeNilMapValue)
  272. t.Run("TestMsgpackEmbeddedFieldPrecedence", TestMsgpackEmbeddedFieldPrecedence)
  273. t.Run("TestMsgpackLargeContainerLen", TestMsgpackLargeContainerLen)
  274. t.Run("TestMsgpackMammothMapsAndSlices", TestMsgpackMammothMapsAndSlices)
  275. t.Run("TestMsgpackTime", TestMsgpackTime)
  276. t.Run("TestMsgpackUintToInt", TestMsgpackUintToInt)
  277. }
  278. func TestCodecSuite(t *testing.T) {
  279. testSuite(t, testCodecGroup)
  280. testGroupResetFlags()
  281. oldIndent, oldCharsAsis, oldPreferFloat, oldMapKeyAsString :=
  282. testJsonH.Indent, testJsonH.HTMLCharsAsIs, testJsonH.PreferFloat, testJsonH.MapKeyAsString
  283. testMaxInitLen = 10
  284. testJsonH.Indent = 8
  285. testJsonH.HTMLCharsAsIs = true
  286. testJsonH.MapKeyAsString = true
  287. // testJsonH.PreferFloat = true
  288. testReinit()
  289. t.Run("json-spaces-htmlcharsasis-initLen10", testJsonGroup)
  290. testMaxInitLen = 10
  291. testJsonH.Indent = -1
  292. testJsonH.HTMLCharsAsIs = false
  293. testJsonH.MapKeyAsString = true
  294. // testJsonH.PreferFloat = false
  295. testReinit()
  296. t.Run("json-tabs-initLen10", testJsonGroup)
  297. testJsonH.Indent, testJsonH.HTMLCharsAsIs, testJsonH.PreferFloat, testJsonH.MapKeyAsString =
  298. oldIndent, oldCharsAsis, oldPreferFloat, oldMapKeyAsString
  299. oldIndefLen := testCborH.IndefiniteLength
  300. testCborH.IndefiniteLength = true
  301. testReinit()
  302. t.Run("cbor-indefinitelength", testCborGroup)
  303. testCborH.IndefiniteLength = oldIndefLen
  304. oldSymbols := testBincH.getBasicHandle().AsSymbols
  305. testBincH.getBasicHandle().AsSymbols = AsSymbolNone
  306. testReinit()
  307. t.Run("binc-no-symbols", testBincGroup)
  308. testBincH.getBasicHandle().AsSymbols = AsSymbolAll
  309. testReinit()
  310. t.Run("binc-all-symbols", testBincGroup)
  311. testBincH.getBasicHandle().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. testGroupResetFlags()
  323. }
  324. // func TestCodecSuite(t *testing.T) {
  325. // testReinit() // so flag.Parse() is called first, and never called again
  326. // testDecodeOptions, testEncodeOptions = DecodeOptions{}, EncodeOptions{}
  327. // testGroupResetFlags()
  328. // testReinit()
  329. // t.Run("optionsFalse", func(t *testing.T) {
  330. // t.Run("TestJsonMammothMapsAndSlices", TestJsonMammothMapsAndSlices)
  331. // })
  332. // }