mammoth-test.go.tmpl 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2012-2015 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 "type" false }} []{{ .Elem }}
  28. func (_ {{ .MethodNamePfx "type" false }}) MapBySlice() { }
  29. {{end}}{{end}}{{end}}
  30. func doTestMammothSlices(t *testing.T, h Handle) {
  31. {{range $i, $e := .Values }}{{if not .Primitive }}{{if not .MapKey }}{{/*
  32. */}}
  33. for _, v := range [][]{{ .Elem }}{ nil, []{{ .Elem }}{}, []{{ .Elem }}{ {{ nonzerocmd .Elem }}, {{ nonzerocmd .Elem }} } } {
  34. // fmt.Printf(">>>> running mammoth slice v{{$i}}: %v\n", v)
  35. var v{{$i}}v1, v{{$i}}v2, v{{$i}}v3, v{{$i}}v4 []{{ .Elem }}
  36. v{{$i}}v1 = v
  37. bs{{$i}} := testMarshalErr(v{{$i}}v1, h, t, "enc-slice-v{{$i}}")
  38. if v != nil { v{{$i}}v2 = make([]{{ .Elem }}, len(v)) }
  39. testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}")
  40. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}")
  41. bs{{$i}} = testMarshalErr(&v{{$i}}v1, h, t, "enc-slice-v{{$i}}-p")
  42. v{{$i}}v2 = nil
  43. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p")
  44. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p")
  45. // ...
  46. v{{$i}}v2 = nil
  47. if v != nil { v{{$i}}v2 = make([]{{ .Elem }}, len(v)) }
  48. v{{$i}}v3 = {{ .MethodNamePfx "type" false }}(v{{$i}}v1)
  49. bs{{$i}} = testMarshalErr(v{{$i}}v3, h, t, "enc-slice-v{{$i}}-custom")
  50. v{{$i}}v4 = {{ .MethodNamePfx "type" false }}(v{{$i}}v2)
  51. testUnmarshalErr(v{{$i}}v4, bs{{$i}}, h, t, "dec-slice-v{{$i}}-custom")
  52. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-slice-v{{$i}}-custom")
  53. v{{$i}}v2 = nil
  54. bs{{$i}} = testMarshalErr(&v{{$i}}v3, h, t, "enc-slice-v{{$i}}-custom-p")
  55. v{{$i}}v4 = {{ .MethodNamePfx "type" false }}(v{{$i}}v2)
  56. testUnmarshalErr(&v{{$i}}v4, bs{{$i}}, h, t, "dec-slice-v{{$i}}-custom-p")
  57. testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-slice-v{{$i}}-custom-p")
  58. }
  59. {{end}}{{end}}{{end}}
  60. }
  61. func doTestMammothMaps(t *testing.T, h Handle) {
  62. {{range $i, $e := .Values }}{{if not .Primitive }}{{if .MapKey }}{{/*
  63. */}}
  64. for _, v := range []map[{{ .MapKey }}]{{ .Elem }}{ nil, map[{{ .MapKey }}]{{ .Elem }}{}, map[{{ .MapKey }}]{{ .Elem }}{ {{ nonzerocmd .MapKey }}:{{ nonzerocmd .Elem }} } } {
  65. // fmt.Printf(">>>> running mammoth map v{{$i}}: %v\n", v)
  66. var v{{$i}}v1, v{{$i}}v2 map[{{ .MapKey }}]{{ .Elem }}
  67. v{{$i}}v1 = v
  68. bs{{$i}} := testMarshalErr(v{{$i}}v1, h, t, "enc-map-v{{$i}}")
  69. if v != nil { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) }
  70. testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}")
  71. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}")
  72. bs{{$i}} = testMarshalErr(&v{{$i}}v1, h, t, "enc-map-v{{$i}}-p")
  73. v{{$i}}v2 = nil
  74. testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}-p")
  75. testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-p")
  76. }
  77. {{end}}{{end}}{{end}}
  78. }
  79. func doTestMammothMapsAndSlices(t *testing.T, h Handle) {
  80. doTestMammothSlices(t, h)
  81. doTestMammothMaps(t, h)
  82. }