mammoth-test.go.tmpl 7.4 KB

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