mammoth-test.go.tmpl 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. //go:build !codec.notmammoth
  4. // +build codec.notmammoth
  5. // Code generated from mammoth-test.go.tmpl - DO NOT EDIT.
  6. package codec
  7. import "testing"
  8. import "fmt"
  9. import "reflect"
  10. // TestMammoth has all the different paths optimized in fast-path
  11. // It has all the primitives, slices and maps.
  12. //
  13. // For each of those types, it has a pointer and a non-pointer field.
  14. func init() { _ = fmt.Printf } // so we can include fmt as needed
  15. type TestMammoth struct {
  16. {{range .Values }}{{if .Primitive -}}
  17. {{ .MethodNamePfx "F" true }} {{ .Primitive }}
  18. {{ .MethodNamePfx "Fptr" true }} *{{ .Primitive }}
  19. {{end}}{{end}}
  20. {{range .Values }}{{if not .Primitive }}{{if not .MapKey -}}
  21. {{ .MethodNamePfx "F" false }} []{{ .Elem }}
  22. {{ .MethodNamePfx "Fptr" false }} *[]{{ .Elem }}
  23. {{ .MethodNamePfx "Farr4" false }} [4]{{ .Elem }}
  24. {{end}}{{end}}{{end}}
  25. {{range .Values }}{{if not .Primitive }}{{if .MapKey -}}
  26. {{ .MethodNamePfx "F" false }} map[{{ .MapKey }}]{{ .Elem }}
  27. {{ .MethodNamePfx "Fptr" false }} *map[{{ .MapKey }}]{{ .Elem }}
  28. {{end}}{{end}}{{end}}
  29. }
  30. {{range .Values }}{{if not .Primitive }}{{if not .MapKey -}}
  31. type {{ .MethodNamePfx "typMbs" false }} []{{ .Elem }}
  32. func (_ {{ .MethodNamePfx "typMbs" false }}) MapBySlice() { }
  33. {{end}}{{end}}{{end}}
  34. {{range .Values }}{{if not .Primitive }}{{if .MapKey -}}
  35. type {{ .MethodNamePfx "typMap" false }} map[{{ .MapKey }}]{{ .Elem }}
  36. {{end}}{{end}}{{end}}
  37. func __doTestMammothSlices(t *testing.T, h Handle) {
  38. {{range $i, $e := .Values }}{{if not .Primitive }}{{if not .MapKey -}}
  39. var v{{$i}}va [8]{{ .Elem }}
  40. for _, v := range [][]{{ .Elem }}{ nil, {}, { {{ nonzerocmd .Elem }}, {{ zerocmd .Elem }}, {{ zerocmd .Elem }}, {{ nonzerocmd .Elem }} } } {
  41. {{/*
  42. // fmt.Printf(">>>> running mammoth slice v{{$i}}: %v\n", v)
  43. // - encode value to some []byte
  44. // - decode into a length-wise-equal []byte
  45. // - check if equal to initial slice
  46. // - encode ptr to the value
  47. // - check if encode bytes are same
  48. // - decode into ptrs to: nil, then 1-elem slice, equal-length, then large len slice
  49. // - decode into non-addressable slice of equal length, then larger len
  50. // - for each decode, compare elem-by-elem to the original slice
  51. // -
  52. // - rinse and repeat for a MapBySlice version
  53. // -
  54. */ -}}
  55. var v{{$i}}v1, v{{$i}}v2 []{{ .Elem }}
  56. var bs{{$i}} []byte
  57. v{{$i}}v1 = v
  58. bs{{$i}} = testMarshalErr(v{{$i}}v1, h, t, "enc-slice-v{{$i}}")
  59. if v == nil {
  60. v{{$i}}v2 = make([]{{ .Elem }}, 2)
  61. testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}")
  62. testDeepEqualErr(v{{$i}}v2[0], v{{$i}}v2[1], t, "equal-slice-v{{$i}}") // should not change
  63. testDeepEqualErr(len(v{{$i}}v2), 2, t, "equal-slice-v{{$i}}") // should not change
  64. v{{$i}}v2 = make([]{{ .Elem }}, 2)
  65. testUnmarshalErr(reflect.ValueOf(v{{$i}}v2), bs{{$i}}, h, t, "dec-slice-v{{$i}}-noaddr") // non-addressable value
  66. testDeepEqualErr(v{{$i}}v2[0], v{{$i}}v2[1], t, "equal-slice-v{{$i}}-noaddr") // should not change
  67. testDeepEqualErr(len(v{{$i}}v2), 2, t, "equal-slice-v{{$i}}") // should not change
  68. } else {
  69. v{{$i}}v2 = make([]{{ .Elem }}, len(v))
  70. testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}")
  71. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}")
  72. v{{$i}}v2 = make([]{{ .Elem }}, len(v))
  73. testUnmarshalErr(reflect.ValueOf(v{{$i}}v2), bs{{$i}}, h, t, "dec-slice-v{{$i}}-noaddr") // non-addressable value
  74. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-noaddr")
  75. }
  76. testReleaseBytes(bs{{$i}})
  77. // ...
  78. bs{{$i}} = testMarshalErr(&v{{$i}}v1, h, t, "enc-slice-v{{$i}}-p")
  79. v{{$i}}v2 = nil
  80. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p")
  81. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p")
  82. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  83. testUnmarshalErr(&v{{$i}}va, bs{{$i}}, h, t, "dec-array-v{{$i}}-p-1")
  84. if v{{$i}}v1 == nil && v{{$i}}v2 == nil { v{{$i}}v2 = []{{ .Elem }}{} } // so we can compare to zero len slice below
  85. testDeepEqualErr(v{{$i}}va[:len(v{{$i}}v2)], v{{$i}}v2, t, "equal-array-v{{$i}}-p-1")
  86. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  87. v{{$i}}v2 = v{{$i}}va[:1:1]
  88. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-1")
  89. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-1")
  90. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  91. v{{$i}}v2 = v{{$i}}va[:len(v{{$i}}v1):len(v{{$i}}v1)]
  92. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-len")
  93. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-len")
  94. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  95. v{{$i}}v2 = v{{$i}}va[:]
  96. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-cap")
  97. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-cap")
  98. if len(v{{$i}}v1) > 1 {
  99. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  100. testUnmarshalErr((&v{{$i}}va)[:len(v{{$i}}v1)], bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-len-noaddr")
  101. testDeepEqualErr(v{{$i}}v1, v{{$i}}va[:len(v{{$i}}v1)], t, "equal-slice-v{{$i}}-p-len-noaddr")
  102. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  103. testUnmarshalErr((&v{{$i}}va)[:], bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-cap-noaddr")
  104. testDeepEqualErr(v{{$i}}v1, v{{$i}}va[:len(v{{$i}}v1)], t, "equal-slice-v{{$i}}-p-cap-noaddr")
  105. }
  106. testReleaseBytes(bs{{$i}})
  107. // ...
  108. var v{{$i}}v3, v{{$i}}v4 {{ .MethodNamePfx "typMbs" false }}
  109. v{{$i}}v2 = nil
  110. if v != nil { v{{$i}}v2 = make([]{{ .Elem }}, len(v)) }
  111. v{{$i}}v3 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v1)
  112. v{{$i}}v4 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v2)
  113. if v != nil {
  114. bs{{$i}} = testMarshalErr(v{{$i}}v3, h, t, "enc-slice-v{{$i}}-custom")
  115. testUnmarshalErr(v{{$i}}v4, bs{{$i}}, h, t, "dec-slice-v{{$i}}-custom")
  116. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-slice-v{{$i}}-custom")
  117. testReleaseBytes(bs{{$i}})
  118. }
  119. bs{{$i}} = testMarshalErr(&v{{$i}}v3, h, t, "enc-slice-v{{$i}}-custom-p")
  120. v{{$i}}v2 = nil
  121. v{{$i}}v4 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v2)
  122. testUnmarshalErr(&v{{$i}}v4, bs{{$i}}, h, t, "dec-slice-v{{$i}}-custom-p")
  123. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-slice-v{{$i}}-custom-p")
  124. testReleaseBytes(bs{{$i}})
  125. }
  126. {{end}}{{end}}{{end}}
  127. }
  128. func __doTestMammothMaps(t *testing.T, h Handle) {
  129. {{range $i, $e := .Values }}{{if not .Primitive }}{{if .MapKey -}}
  130. for _, v := range []map[{{ .MapKey }}]{{ .Elem }}{ nil, {}, { {{ nonzerocmd .MapKey }}:{{ zerocmd .Elem }} {{if ne "bool" .MapKey}}, {{ nonzerocmd .MapKey }}:{{ nonzerocmd .Elem }} {{end}} } } {
  131. // fmt.Printf(">>>> running mammoth map v{{$i}}: %v\n", v)
  132. var v{{$i}}v1, v{{$i}}v2 map[{{ .MapKey }}]{{ .Elem }}
  133. var bs{{$i}} []byte
  134. v{{$i}}v1 = v
  135. bs{{$i}} = testMarshalErr(v{{$i}}v1, h, t, "enc-map-v{{$i}}")
  136. if v != nil {
  137. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  138. testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}")
  139. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}")
  140. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  141. testUnmarshalErr(reflect.ValueOf(v{{$i}}v2), bs{{$i}}, h, t, "dec-map-v{{$i}}-noaddr") // decode into non-addressable map value
  142. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-noaddr")
  143. }
  144. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  145. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-len")
  146. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-p-len")
  147. testReleaseBytes(bs{{$i}})
  148. bs{{$i}} = testMarshalErr(&v{{$i}}v1, h, t, "enc-map-v{{$i}}-p")
  149. v{{$i}}v2 = nil
  150. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-nil")
  151. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-p-nil")
  152. testReleaseBytes(bs{{$i}})
  153. // ...
  154. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  155. var v{{$i}}v3, v{{$i}}v4 {{ .MethodNamePfx "typMap" false }}
  156. v{{$i}}v3 = {{ .MethodNamePfx "typMap" false }}(v{{$i}}v1)
  157. v{{$i}}v4 = {{ .MethodNamePfx "typMap" false }}(v{{$i}}v2)
  158. if v != nil {
  159. bs{{$i}} = testMarshalErr(v{{$i}}v3, h, t, "enc-map-v{{$i}}-custom")
  160. testUnmarshalErr(v{{$i}}v4, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-len")
  161. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-map-v{{$i}}-p-len")
  162. testReleaseBytes(bs{{$i}})
  163. }
  164. }
  165. {{end}}{{end}}{{end}}
  166. }
  167. func doTestMammothMapsAndSlices(t *testing.T, h Handle) {
  168. defer testSetup(t, &h)()
  169. if mh, ok := h.(*MsgpackHandle); ok {
  170. defer func(b bool) { mh.RawToString = b }(mh.RawToString)
  171. mh.RawToString = true
  172. }
  173. __doTestMammothSlices(t, h)
  174. __doTestMammothMaps(t, h)
  175. }
  176. func doTestMammoth(t *testing.T, h Handle) {
  177. defer testSetup(t, &h)()
  178. if mh, ok := h.(*MsgpackHandle); ok {
  179. defer func(b bool) { mh.RawToString = b }(mh.RawToString)
  180. mh.RawToString = true
  181. }
  182. name := h.Name()
  183. var b []byte
  184. var m, m2 TestMammoth
  185. testRandomFillRV(reflect.ValueOf(&m).Elem())
  186. b = testMarshalErr(&m, h, t, "mammoth-"+name)
  187. testUnmarshalErr(&m2, b, h, t, "mammoth-"+name)
  188. testDeepEqualErr(&m, &m2, t, "mammoth-"+name)
  189. testReleaseBytes(b)
  190. if testing.Short() {
  191. t.Skipf("skipping rest of mammoth test in -short mode")
  192. }
  193. var mm, mm2 TestMammoth2Wrapper
  194. testRandomFillRV(reflect.ValueOf(&mm).Elem())
  195. b = testMarshalErr(&mm, h, t, "mammoth2-"+name)
  196. // os.Stderr.Write([]byte("\n\n\n\n" + string(b) + "\n\n\n\n"))
  197. testUnmarshalErr(&mm2, b, h, t, "mammoth2-"+name)
  198. testDeepEqualErr(&mm, &mm2, t, "mammoth2-"+name)
  199. // testMammoth2(t, name, h)
  200. testReleaseBytes(b)
  201. }
  202. {{range $i, $e := .Formats -}}
  203. func Test{{ . }}Mammoth(t *testing.T) {
  204. doTestMammoth(t, test{{ . }}H)
  205. }
  206. {{end}}
  207. {{range $i, $e := .Formats -}}
  208. func Test{{ . }}MammothMapsAndSlices(t *testing.T) {
  209. doTestMammothMapsAndSlices(t, test{{ . }}H)
  210. }
  211. {{end}}