mammoth-test.go.tmpl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // Code generated from mammoth-test.go.tmpl - DO NOT EDIT.
  4. package codec
  5. import "testing"
  6. import "fmt"
  7. // TestMammoth has all the different paths optimized in fast-path
  8. // It has all the primitives, slices and maps.
  9. //
  10. // For each of those types, it has a pointer and a non-pointer field.
  11. func init() { _ = fmt.Printf } // so we can include fmt as needed
  12. type TestMammoth struct {
  13. {{range .Values }}{{if .Primitive -}}
  14. {{ .MethodNamePfx "F" true }} {{ .Primitive }}
  15. {{ .MethodNamePfx "Fptr" true }} *{{ .Primitive }}
  16. {{end}}{{end}}
  17. {{range .Values }}{{if not .Primitive }}{{if not .MapKey -}}
  18. {{ .MethodNamePfx "F" false }} []{{ .Elem }}
  19. {{ .MethodNamePfx "Fptr" false }} *[]{{ .Elem }}
  20. {{end}}{{end}}{{end}}
  21. {{range .Values }}{{if not .Primitive }}{{if .MapKey -}}
  22. {{ .MethodNamePfx "F" false }} map[{{ .MapKey }}]{{ .Elem }}
  23. {{ .MethodNamePfx "Fptr" false }} *map[{{ .MapKey }}]{{ .Elem }}
  24. {{end}}{{end}}{{end}}
  25. }
  26. {{range .Values }}{{if not .Primitive }}{{if not .MapKey -}}
  27. type {{ .MethodNamePfx "typMbs" false }} []{{ .Elem }}
  28. func (_ {{ .MethodNamePfx "typMbs" false }}) MapBySlice() { }
  29. {{end}}{{end}}{{end}}
  30. {{range .Values }}{{if not .Primitive }}{{if .MapKey -}}
  31. type {{ .MethodNamePfx "typMap" false }} map[{{ .MapKey }}]{{ .Elem }}
  32. {{end}}{{end}}{{end}}
  33. func doTestMammothSlices(t *testing.T, h Handle) {
  34. {{range $i, $e := .Values }}{{if not .Primitive }}{{if not .MapKey -}}
  35. var v{{$i}}va [8]{{ .Elem }}
  36. for _, v := range [][]{{ .Elem }}{ nil, {}, { {{ nonzerocmd .Elem }}, {{ zerocmd .Elem }}, {{ zerocmd .Elem }}, {{ nonzerocmd .Elem }} } } {
  37. {{/*
  38. // fmt.Printf(">>>> running mammoth slice v{{$i}}: %v\n", v)
  39. // - encode value to some []byte
  40. // - decode into a length-wise-equal []byte
  41. // - check if equal to initial slice
  42. // - encode ptr to the value
  43. // - check if encode bytes are same
  44. // - decode into ptrs to: nil, then 1-elem slice, equal-length, then large len slice
  45. // - decode into non-addressable slice of equal length, then larger len
  46. // - for each decode, compare elem-by-elem to the original slice
  47. // -
  48. // - rinse and repeat for a MapBySlice version
  49. // -
  50. */ -}}
  51. var v{{$i}}v1, v{{$i}}v2 []{{ .Elem }}
  52. var bs{{$i}} []byte
  53. v{{$i}}v1 = v
  54. bs{{$i}} = testMarshalErr(v{{$i}}v1, h, t, "enc-slice-v{{$i}}")
  55. if v != nil {
  56. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make([]{{ .Elem }}, len(v)) }
  57. testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}")
  58. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}")
  59. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make([]{{ .Elem }}, len(v)) }
  60. testUnmarshalErr(rv4i(v{{$i}}v2), bs{{$i}}, h, t, "dec-slice-v{{$i}}-noaddr") // non-addressable value
  61. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-noaddr")
  62. }
  63. // ...
  64. bs{{$i}} = testMarshalErr(&v{{$i}}v1, h, t, "enc-slice-v{{$i}}-p")
  65. v{{$i}}v2 = nil
  66. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p")
  67. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p")
  68. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  69. v{{$i}}v2 = v{{$i}}va[:1:1]
  70. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-1")
  71. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-1")
  72. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  73. v{{$i}}v2 = v{{$i}}va[:len(v{{$i}}v1):len(v{{$i}}v1)]
  74. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-len")
  75. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-len")
  76. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  77. v{{$i}}v2 = v{{$i}}va[:]
  78. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-cap")
  79. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-cap")
  80. if len(v{{$i}}v1) > 1 {
  81. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  82. testUnmarshalErr((&v{{$i}}va)[:len(v{{$i}}v1)], bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-len-noaddr")
  83. testDeepEqualErr(v{{$i}}v1, v{{$i}}va[:len(v{{$i}}v1)], t, "equal-slice-v{{$i}}-p-len-noaddr")
  84. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  85. testUnmarshalErr((&v{{$i}}va)[:], bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-cap-noaddr")
  86. testDeepEqualErr(v{{$i}}v1, v{{$i}}va[:len(v{{$i}}v1)], t, "equal-slice-v{{$i}}-p-cap-noaddr")
  87. }
  88. // ...
  89. var v{{$i}}v3, v{{$i}}v4 {{ .MethodNamePfx "typMbs" false }}
  90. v{{$i}}v2 = nil
  91. if v != nil { v{{$i}}v2 = make([]{{ .Elem }}, len(v)) }
  92. v{{$i}}v3 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v1)
  93. v{{$i}}v4 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v2)
  94. if v != nil {
  95. bs{{$i}} = testMarshalErr(v{{$i}}v3, h, t, "enc-slice-v{{$i}}-custom")
  96. testUnmarshalErr(v{{$i}}v4, bs{{$i}}, h, t, "dec-slice-v{{$i}}-custom")
  97. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-slice-v{{$i}}-custom")
  98. }
  99. bs{{$i}} = testMarshalErr(&v{{$i}}v3, h, t, "enc-slice-v{{$i}}-custom-p")
  100. v{{$i}}v2 = nil
  101. v{{$i}}v4 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v2)
  102. testUnmarshalErr(&v{{$i}}v4, bs{{$i}}, h, t, "dec-slice-v{{$i}}-custom-p")
  103. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-slice-v{{$i}}-custom-p")
  104. }
  105. {{end}}{{end}}{{end}}
  106. }
  107. func doTestMammothMaps(t *testing.T, h Handle) {
  108. {{range $i, $e := .Values }}{{if not .Primitive }}{{if .MapKey -}}
  109. for _, v := range []map[{{ .MapKey }}]{{ .Elem }}{ nil, {}, { {{ nonzerocmd .MapKey }}:{{ zerocmd .Elem }} {{if ne "bool" .MapKey}}, {{ nonzerocmd .MapKey }}:{{ nonzerocmd .Elem }} {{end}} } } {
  110. // fmt.Printf(">>>> running mammoth map v{{$i}}: %v\n", v)
  111. var v{{$i}}v1, v{{$i}}v2 map[{{ .MapKey }}]{{ .Elem }}
  112. var bs{{$i}} []byte
  113. v{{$i}}v1 = v
  114. bs{{$i}} = testMarshalErr(v{{$i}}v1, h, t, "enc-map-v{{$i}}")
  115. if v != nil {
  116. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  117. testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}")
  118. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}")
  119. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  120. testUnmarshalErr(rv4i(v{{$i}}v2), bs{{$i}}, h, t, "dec-map-v{{$i}}-noaddr") // decode into non-addressable map value
  121. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-noaddr")
  122. }
  123. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  124. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-len")
  125. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-p-len")
  126. bs{{$i}} = testMarshalErr(&v{{$i}}v1, h, t, "enc-map-v{{$i}}-p")
  127. v{{$i}}v2 = nil
  128. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-nil")
  129. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-p-nil")
  130. // ...
  131. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  132. var v{{$i}}v3, v{{$i}}v4 {{ .MethodNamePfx "typMap" false }}
  133. v{{$i}}v3 = {{ .MethodNamePfx "typMap" false }}(v{{$i}}v1)
  134. v{{$i}}v4 = {{ .MethodNamePfx "typMap" false }}(v{{$i}}v2)
  135. if v != nil {
  136. bs{{$i}} = testMarshalErr(v{{$i}}v3, h, t, "enc-map-v{{$i}}-custom")
  137. testUnmarshalErr(v{{$i}}v4, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-len")
  138. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-map-v{{$i}}-p-len")
  139. }
  140. }
  141. {{end}}{{end}}{{end}}
  142. }
  143. func doTestMammothMapsAndSlices(t *testing.T, h Handle) {
  144. doTestMammothSlices(t, h)
  145. doTestMammothMaps(t, h)
  146. }