github.com/ugorji/go/codec@v1.2.13-0.20240307214044-07c54c229a5a/mammoth-test.go.tmpl (about) 1 // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved. 2 // Use of this source code is governed by a MIT license found in the LICENSE file. 3 4 //go:build !codec.notmammoth 5 // +build codec.notmammoth 6 7 // Code generated from mammoth-test.go.tmpl - DO NOT EDIT. 8 9 package codec 10 11 import "testing" 12 import "fmt" 13 import "reflect" 14 15 // TestMammoth has all the different paths optimized in fast-path 16 // It has all the primitives, slices and maps. 17 // 18 // For each of those types, it has a pointer and a non-pointer field. 19 20 func init() { _ = fmt.Printf } // so we can include fmt as needed 21 22 type TestMammoth struct { 23 24 {{range .Values }}{{if .Primitive -}} 25 {{ .MethodNamePfx "F" true }} {{ .Primitive }} 26 {{ .MethodNamePfx "Fptr" true }} *{{ .Primitive }} 27 {{end}}{{end}} 28 29 {{range .Values }}{{if not .Primitive }}{{if not .MapKey -}} 30 {{ .MethodNamePfx "F" false }} []{{ .Elem }} 31 {{ .MethodNamePfx "Fptr" false }} *[]{{ .Elem }} 32 {{ .MethodNamePfx "Farr4" false }} [4]{{ .Elem }} 33 {{end}}{{end}}{{end}} 34 35 {{range .Values }}{{if not .Primitive }}{{if .MapKey -}} 36 {{ .MethodNamePfx "F" false }} map[{{ .MapKey }}]{{ .Elem }} 37 {{ .MethodNamePfx "Fptr" false }} *map[{{ .MapKey }}]{{ .Elem }} 38 {{end}}{{end}}{{end}} 39 40 } 41 42 {{range .Values }}{{if not .Primitive }}{{if not .MapKey -}} 43 type {{ .MethodNamePfx "typMbs" false }} []{{ .Elem }} 44 func (_ {{ .MethodNamePfx "typMbs" false }}) MapBySlice() { } 45 {{end}}{{end}}{{end}} 46 47 {{range .Values }}{{if not .Primitive }}{{if .MapKey -}} 48 type {{ .MethodNamePfx "typMap" false }} map[{{ .MapKey }}]{{ .Elem }} 49 {{end}}{{end}}{{end}} 50 51 func __doTestMammothSlices(t *testing.T, h Handle) { 52 {{range $i, $e := .Values }}{{if not .Primitive }}{{if not .MapKey -}} 53 var v{{$i}}va [8]{{ .Elem }} 54 for _, v := range [][]{{ .Elem }}{ nil, {}, { {{ nonzerocmd .Elem }}, {{ zerocmd .Elem }}, {{ zerocmd .Elem }}, {{ nonzerocmd .Elem }} } } { 55 {{/* 56 // fmt.Printf(">>>> running mammoth slice v{{$i}}: %v\n", v) 57 // - encode value to some []byte 58 // - decode into a length-wise-equal []byte 59 // - check if equal to initial slice 60 // - encode ptr to the value 61 // - check if encode bytes are same 62 // - decode into ptrs to: nil, then 1-elem slice, equal-length, then large len slice 63 // - decode into non-addressable slice of equal length, then larger len 64 // - for each decode, compare elem-by-elem to the original slice 65 // - 66 // - rinse and repeat for a MapBySlice version 67 // - 68 */ -}} 69 var v{{$i}}v1, v{{$i}}v2 []{{ .Elem }} 70 var bs{{$i}} []byte 71 v{{$i}}v1 = v 72 bs{{$i}} = testMarshalErr(v{{$i}}v1, h, t, "enc-slice-v{{$i}}") 73 if v == nil { 74 v{{$i}}v2 = make([]{{ .Elem }}, 2) 75 testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}") 76 testDeepEqualErr(v{{$i}}v2[0], v{{$i}}v2[1], t, "equal-slice-v{{$i}}") // should not change 77 testDeepEqualErr(len(v{{$i}}v2), 2, t, "equal-slice-v{{$i}}") // should not change 78 v{{$i}}v2 = make([]{{ .Elem }}, 2) 79 testUnmarshalErr(reflect.ValueOf(v{{$i}}v2), bs{{$i}}, h, t, "dec-slice-v{{$i}}-noaddr") // non-addressable value 80 testDeepEqualErr(v{{$i}}v2[0], v{{$i}}v2[1], t, "equal-slice-v{{$i}}-noaddr") // should not change 81 testDeepEqualErr(len(v{{$i}}v2), 2, t, "equal-slice-v{{$i}}") // should not change 82 } else { 83 v{{$i}}v2 = make([]{{ .Elem }}, len(v)) 84 testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}") 85 testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}") 86 v{{$i}}v2 = make([]{{ .Elem }}, len(v)) 87 testUnmarshalErr(reflect.ValueOf(v{{$i}}v2), bs{{$i}}, h, t, "dec-slice-v{{$i}}-noaddr") // non-addressable value 88 testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-noaddr") 89 } 90 testReleaseBytes(bs{{$i}}) 91 // ... 92 bs{{$i}} = testMarshalErr(&v{{$i}}v1, h, t, "enc-slice-v{{$i}}-p") 93 v{{$i}}v2 = nil 94 testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p") 95 testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p") 96 v{{$i}}va = [8]{{ .Elem }}{} // clear the array 97 testUnmarshalErr(&v{{$i}}va, bs{{$i}}, h, t, "dec-array-v{{$i}}-p-1") 98 if v{{$i}}v1 == nil && v{{$i}}v2 == nil { v{{$i}}v2 = []{{ .Elem }}{} } // so we can compare to zero len slice below 99 testDeepEqualErr(v{{$i}}va[:len(v{{$i}}v2)], v{{$i}}v2, t, "equal-array-v{{$i}}-p-1") 100 v{{$i}}va = [8]{{ .Elem }}{} // clear the array 101 v{{$i}}v2 = v{{$i}}va[:1:1] 102 testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-1") 103 testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-1") 104 v{{$i}}va = [8]{{ .Elem }}{} // clear the array 105 v{{$i}}v2 = v{{$i}}va[:len(v{{$i}}v1):len(v{{$i}}v1)] 106 testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-len") 107 testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-len") 108 v{{$i}}va = [8]{{ .Elem }}{} // clear the array 109 v{{$i}}v2 = v{{$i}}va[:] 110 testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-cap") 111 testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-slice-v{{$i}}-p-cap") 112 if len(v{{$i}}v1) > 1 { 113 v{{$i}}va = [8]{{ .Elem }}{} // clear the array 114 testUnmarshalErr((&v{{$i}}va)[:len(v{{$i}}v1)], bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-len-noaddr") 115 testDeepEqualErr(v{{$i}}v1, v{{$i}}va[:len(v{{$i}}v1)], t, "equal-slice-v{{$i}}-p-len-noaddr") 116 v{{$i}}va = [8]{{ .Elem }}{} // clear the array 117 testUnmarshalErr((&v{{$i}}va)[:], bs{{$i}}, h, t, "dec-slice-v{{$i}}-p-cap-noaddr") 118 testDeepEqualErr(v{{$i}}v1, v{{$i}}va[:len(v{{$i}}v1)], t, "equal-slice-v{{$i}}-p-cap-noaddr") 119 } 120 testReleaseBytes(bs{{$i}}) 121 // ... 122 var v{{$i}}v3, v{{$i}}v4 {{ .MethodNamePfx "typMbs" false }} 123 v{{$i}}v2 = nil 124 if v != nil { v{{$i}}v2 = make([]{{ .Elem }}, len(v)) } 125 v{{$i}}v3 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v1) 126 v{{$i}}v4 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v2) 127 if v != nil { 128 bs{{$i}} = testMarshalErr(v{{$i}}v3, h, t, "enc-slice-v{{$i}}-custom") 129 testUnmarshalErr(v{{$i}}v4, bs{{$i}}, h, t, "dec-slice-v{{$i}}-custom") 130 testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-slice-v{{$i}}-custom") 131 testReleaseBytes(bs{{$i}}) 132 } 133 bs{{$i}} = testMarshalErr(&v{{$i}}v3, h, t, "enc-slice-v{{$i}}-custom-p") 134 v{{$i}}v2 = nil 135 v{{$i}}v4 = {{ .MethodNamePfx "typMbs" false }}(v{{$i}}v2) 136 testUnmarshalErr(&v{{$i}}v4, bs{{$i}}, h, t, "dec-slice-v{{$i}}-custom-p") 137 testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-slice-v{{$i}}-custom-p") 138 testReleaseBytes(bs{{$i}}) 139 } 140 {{end}}{{end}}{{end}} 141 } 142 143 func __doTestMammothMaps(t *testing.T, h Handle) { 144 {{range $i, $e := .Values }}{{if not .Primitive }}{{if .MapKey -}} 145 for _, v := range []map[{{ .MapKey }}]{{ .Elem }}{ nil, {}, { {{ nonzerocmd .MapKey }}:{{ zerocmd .Elem }} {{if ne "bool" .MapKey}}, {{ nonzerocmd .MapKey }}:{{ nonzerocmd .Elem }} {{end}} } } { 146 // fmt.Printf(">>>> running mammoth map v{{$i}}: %v\n", v) 147 var v{{$i}}v1, v{{$i}}v2 map[{{ .MapKey }}]{{ .Elem }} 148 var bs{{$i}} []byte 149 v{{$i}}v1 = v 150 bs{{$i}} = testMarshalErr(v{{$i}}v1, h, t, "enc-map-v{{$i}}") 151 if v != nil { 152 if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map 153 testUnmarshalErr(v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}") 154 testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}") 155 if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map 156 testUnmarshalErr(reflect.ValueOf(v{{$i}}v2), bs{{$i}}, h, t, "dec-map-v{{$i}}-noaddr") // decode into non-addressable map value 157 testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-noaddr") 158 } 159 if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map 160 testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-len") 161 testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-p-len") 162 testReleaseBytes(bs{{$i}}) 163 bs{{$i}} = testMarshalErr(&v{{$i}}v1, h, t, "enc-map-v{{$i}}-p") 164 v{{$i}}v2 = nil 165 testUnmarshalErr(&v{{$i}}v2, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-nil") 166 testDeepEqualErr(v{{$i}}v1, v{{$i}}v2, t, "equal-map-v{{$i}}-p-nil") 167 testReleaseBytes(bs{{$i}}) 168 // ... 169 if v == nil { v{{$i}}v2 = nil } else { v{{$i}}v2 = make(map[{{ .MapKey }}]{{ .Elem }}, len(v)) } // reset map 170 var v{{$i}}v3, v{{$i}}v4 {{ .MethodNamePfx "typMap" false }} 171 v{{$i}}v3 = {{ .MethodNamePfx "typMap" false }}(v{{$i}}v1) 172 v{{$i}}v4 = {{ .MethodNamePfx "typMap" false }}(v{{$i}}v2) 173 if v != nil { 174 bs{{$i}} = testMarshalErr(v{{$i}}v3, h, t, "enc-map-v{{$i}}-custom") 175 testUnmarshalErr(v{{$i}}v4, bs{{$i}}, h, t, "dec-map-v{{$i}}-p-len") 176 testDeepEqualErr(v{{$i}}v3, v{{$i}}v4, t, "equal-map-v{{$i}}-p-len") 177 testReleaseBytes(bs{{$i}}) 178 } 179 } 180 {{end}}{{end}}{{end}} 181 182 } 183 184 func doTestMammothMapsAndSlices(t *testing.T, h Handle) { 185 defer testSetup(t, &h)() 186 if mh, ok := h.(*MsgpackHandle); ok { 187 defer func(b bool) { mh.RawToString = b }(mh.RawToString) 188 mh.RawToString = true 189 } 190 __doTestMammothSlices(t, h) 191 __doTestMammothMaps(t, h) 192 } 193 194 func doTestMammoth(t *testing.T, h Handle) { 195 defer testSetup(t, &h)() 196 if mh, ok := h.(*MsgpackHandle); ok { 197 defer func(b bool) { mh.RawToString = b }(mh.RawToString) 198 mh.RawToString = true 199 } 200 201 name := h.Name() 202 var b []byte 203 204 var m, m2 TestMammoth 205 testRandomFillRV(reflect.ValueOf(&m).Elem()) 206 b = testMarshalErr(&m, h, t, "mammoth-"+name) 207 208 testUnmarshalErr(&m2, b, h, t, "mammoth-"+name) 209 testDeepEqualErr(&m, &m2, t, "mammoth-"+name) 210 testReleaseBytes(b) 211 212 if testing.Short() { 213 t.Skipf("skipping rest of mammoth test in -short mode") 214 } 215 216 var mm, mm2 TestMammoth2Wrapper 217 testRandomFillRV(reflect.ValueOf(&mm).Elem()) 218 b = testMarshalErr(&mm, h, t, "mammoth2-"+name) 219 // os.Stderr.Write([]byte("\n\n\n\n" + string(b) + "\n\n\n\n")) 220 testUnmarshalErr(&mm2, b, h, t, "mammoth2-"+name) 221 testDeepEqualErr(&mm, &mm2, t, "mammoth2-"+name) 222 // testMammoth2(t, name, h) 223 testReleaseBytes(b) 224 } 225 226 {{range $i, $e := .Formats -}} 227 func Test{{ . }}Mammoth(t *testing.T) { 228 doTestMammoth(t, test{{ . }}H) 229 } 230 {{end}} 231 {{range $i, $e := .Formats -}} 232 func Test{{ . }}MammothMapsAndSlices(t *testing.T) { 233 doTestMammothMapsAndSlices(t, test{{ . }}H) 234 } 235 {{end}}