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