另客网go项目公用的代码库

mammoth-test.go.tmpl 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. */}}
  37. var v{{$i}}va [8]{{ .Elem }}
  38. for _, v := range [][]{{ .Elem }}{ nil, {}, { {{ nonzerocmd .Elem }}, {{ zerocmd .Elem }}, {{ zerocmd .Elem }}, {{ nonzerocmd .Elem }} } } { {{/*
  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. v{{$i}}v1 = v
  54. bs{{$i}} := testMarshalErr(v{{$i}}v1, h, t, "enc-slice-v{{$i}}")
  55. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make([]{{ .Elem }}, len(v)) }
  56. testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}")
  57. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}")
  58. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make([]{{ .Elem }}, len(v)) }
  59. testUnmarshalErr(reflect.ValueOf(v{{$i}}v2), bs{{$i}}, h, t, "dec-slice-v{{$i}}-noaddr") // non-addressable value
  60. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-noaddr")
  61. // ...
  62. bs{{$i}} = testMarshalErr(&v{{$i}}v1, h, t, "enc-slice-v{{$i}}-p")
  63. v{{$i}}v2 = nil
  64. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p")
  65. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p")
  66. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  67. v{{$i}}v2 = v{{$i}}va[:1:1]
  68. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-1")
  69. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-1")
  70. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  71. v{{$i}}v2 = v{{$i}}va[:len(v{{$i}}v1):len(v{{$i}}v1)]
  72. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-len")
  73. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-len")
  74. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  75. v{{$i}}v2 = v{{$i}}va[:]
  76. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-cap")
  77. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-cap")
  78. if len(v{{$i}}v1) > 1 {
  79. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  80. testUnmarshalErr((&v{{$i}}va)[:len(v{{$i}}v1)], bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-len-noaddr")
  81. testDeepEqualErr(v{{$i}}v1, v{{$i}}va[:len(v{{$i}}v1)], t, "equal-slice-v{{$i}}-p-len-noaddr")
  82. v{{$i}}va = [8]{{ .Elem }}{} // clear the array
  83. testUnmarshalErr((&v{{$i}}va)[:], bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-cap-noaddr")
  84. testDeepEqualErr(v{{$i}}v1, v{{$i}}va[:len(v{{$i}}v1)], t, "equal-slice-v{{$i}}-p-cap-noaddr")
  85. }
  86. // ...
  87. var v{{$i}}v3, v{{$i}}v4 {{ .MethodNamePfx "typMbs" false }}
  88. v{{$i}}v2 = nil
  89. if v != nil { v{{$i}}v2 = make([]{{ .Elem }}, len(v)) }
  90. v{{$i}}v3 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v1)
  91. v{{$i}}v4 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v2)
  92. bs{{$i}} = testMarshalErr(v{{$i}}v3, h, t, "enc-slice-v{{$i}}-custom")
  93. testUnmarshalErr(v{{$i}}v4, bs{{$i}}, h, t, "dec-slice-v{{$i}}-custom")
  94. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-slice-v{{$i}}-custom")
  95. bs{{$i}} = testMarshalErr(&v{{$i}}v3, h, t, "enc-slice-v{{$i}}-custom-p")
  96. v{{$i}}v2 = nil
  97. v{{$i}}v4 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v2)
  98. testUnmarshalErr(&v{{$i}}v4, bs{{$i}}, h, t, "dec-slice-v{{$i}}-custom-p")
  99. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-slice-v{{$i}}-custom-p")
  100. }
  101. {{end}}{{end}}{{end}}
  102. }
  103. func doTestMammothMaps(t *testing.T, h Handle) {
  104. {{range $i, $e := .Values }}{{if not .Primitive }}{{if .MapKey }}{{/*
  105. */}}
  106. for _, v := range []map[{{ .MapKey }}]{{ .Elem }}{ nil, {}, { {{ nonzerocmd .MapKey }}:{{ zerocmd .Elem }} {{if ne "bool" .MapKey}}, {{ nonzerocmd .MapKey }}:{{ nonzerocmd .Elem }} {{end}} } } {
  107. // fmt.Printf(">>>> running mammoth map v{{$i}}: %v\n", v)
  108. var v{{$i}}v1, v{{$i}}v2 map[{{ .MapKey }}]{{ .Elem }}
  109. v{{$i}}v1 = v
  110. bs{{$i}} := testMarshalErr(v{{$i}}v1, h, t, "enc-map-v{{$i}}")
  111. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  112. testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}")
  113. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}")
  114. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  115. testUnmarshalErr(reflect.ValueOf(v{{$i}}v2), bs{{$i}}, h, t, "dec-map-v{{$i}}-noaddr") // decode into non-addressable map value
  116. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-noaddr")
  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}}-p-len")
  119. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-p-len")
  120. bs{{$i}} = testMarshalErr(&v{{$i}}v1, h, t, "enc-map-v{{$i}}-p")
  121. v{{$i}}v2 = nil
  122. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-nil")
  123. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-p-nil")
  124. // ...
  125. if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map
  126. var v{{$i}}v3, v{{$i}}v4 {{ .MethodNamePfx "typMap" false }}
  127. v{{$i}}v3 = {{ .MethodNamePfx "typMap" false }}(v{{$i}}v1)
  128. v{{$i}}v4 = {{ .MethodNamePfx "typMap" false }}(v{{$i}}v2)
  129. bs{{$i}} = testMarshalErr(v{{$i}}v3, h, t, "enc-map-v{{$i}}-custom")
  130. testUnmarshalErr(v{{$i}}v4, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-len")
  131. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-map-v{{$i}}-p-len")
  132. }
  133. {{end}}{{end}}{{end}}
  134. }
  135. func doTestMammothMapsAndSlices(t *testing.T, h Handle) {
  136. doTestMammothSlices(t, h)
  137. doTestMammothMaps(t, h)
  138. }