|
| 1 | +package _generated |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "math" |
| 6 | + "math/rand/v2" |
| 7 | + "reflect" |
| 8 | + "strconv" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/tinylib/msgp/msgp" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAutoShimKeys(t *testing.T) { |
| 15 | + rng := rand.New(rand.NewPCG(0, 0)) |
| 16 | + |
| 17 | + // Generate a bunch of random maps |
| 18 | + for i := range 500 { |
| 19 | + var test MyMapKeyStruct3 |
| 20 | + if i != 0 { |
| 21 | + // Don't add anything to the first object |
| 22 | + test.MapString = make(map[string]int) |
| 23 | + test.MapString2 = make(map[ExternalInt]int) |
| 24 | + test.MapString3 = make(map[ExternalString]int) |
| 25 | + test.MapFloat32 = make(map[float32]int) |
| 26 | + test.MapFloat64 = make(map[float64]int) |
| 27 | + test.MapUint = make(map[uint]int) |
| 28 | + test.MapUint8 = make(map[uint8]int) |
| 29 | + test.MapUint16 = make(map[uint16]int) |
| 30 | + test.MapUint32 = make(map[uint32]int) |
| 31 | + test.MapUint64 = make(map[uint64]int) |
| 32 | + test.MapByte = make(map[byte]int) |
| 33 | + test.MapInt = make(map[int]int) |
| 34 | + test.MapInt8 = make(map[int8]int) |
| 35 | + test.MapInt16 = make(map[int16]int) |
| 36 | + test.MapInt32 = make(map[int32]int) |
| 37 | + test.MapInt64 = make(map[int64]int) |
| 38 | + test.MapBool = make(map[bool]int) |
| 39 | + test.MapMapInt = make(map[int]map[int]int) |
| 40 | + |
| 41 | + for range rng.IntN(50) { |
| 42 | + test.MapString[string(strconv.Itoa(rng.IntN(math.MaxInt32)))] = rng.IntN(100) |
| 43 | + } |
| 44 | + for range rng.IntN(50) { |
| 45 | + test.MapString3[ExternalString(strconv.Itoa(rng.IntN(math.MaxInt32)))] = rng.IntN(100) |
| 46 | + } |
| 47 | + for range rng.IntN(50) { |
| 48 | + test.MapString2[ExternalInt(rng.IntN(math.MaxInt32))] = rng.IntN(100) |
| 49 | + } |
| 50 | + for range rng.IntN(50) { |
| 51 | + test.MapFloat32[float32(rng.Float32())] = rng.IntN(100) |
| 52 | + } |
| 53 | + for range rng.IntN(50) { |
| 54 | + test.MapFloat64[rng.Float64()] = rng.IntN(100) |
| 55 | + } |
| 56 | + for range rng.IntN(50) { |
| 57 | + test.MapUint[uint(rng.Uint64())] = rng.IntN(100) |
| 58 | + } |
| 59 | + for range rng.IntN(50) { |
| 60 | + test.MapUint8[uint8(rng.Uint64())] = rng.IntN(100) |
| 61 | + } |
| 62 | + for range rng.IntN(50) { |
| 63 | + test.MapUint16[uint16(rng.Uint64())] = rng.IntN(100) |
| 64 | + } |
| 65 | + for range rng.IntN(50) { |
| 66 | + test.MapUint32[rng.Uint32()] = rng.IntN(100) |
| 67 | + } |
| 68 | + for range rng.IntN(50) { |
| 69 | + test.MapUint64[rng.Uint64()] = rng.IntN(100) |
| 70 | + } |
| 71 | + for range rng.IntN(50) { |
| 72 | + test.MapByte[byte(uint8(rng.Uint64()))] = rng.IntN(100) |
| 73 | + } |
| 74 | + for range rng.IntN(50) { |
| 75 | + test.MapInt[rng.IntN(math.MaxInt32)] = rng.IntN(100) |
| 76 | + } |
| 77 | + for range rng.IntN(50) { |
| 78 | + test.MapInt8[int8(rng.IntN(int(math.MaxInt8)+1))] = rng.IntN(100) |
| 79 | + } |
| 80 | + for range rng.IntN(50) { |
| 81 | + test.MapInt16[int16(rng.IntN(int(math.MaxInt16)+1))] = rng.IntN(100) |
| 82 | + } |
| 83 | + for range rng.IntN(50) { |
| 84 | + test.MapInt32[int32(rng.IntN(int(math.MaxInt32)))] = rng.IntN(100) |
| 85 | + } |
| 86 | + for range rng.IntN(50) { |
| 87 | + // Use only non-negative values to stay within IntN capabilities |
| 88 | + test.MapInt64[int64(rng.IntN(int(math.MaxInt32)))] = rng.IntN(100) |
| 89 | + } |
| 90 | + if rng.IntN(2) == 0 { |
| 91 | + test.MapBool[true] = rng.IntN(100) |
| 92 | + } |
| 93 | + if rng.IntN(2) == 0 { |
| 94 | + test.MapBool[false] = rng.IntN(100) |
| 95 | + } |
| 96 | + |
| 97 | + for range rng.IntN(50) { |
| 98 | + dst := make(map[int]int, 50) |
| 99 | + test.MapMapInt[rng.Int()] = dst |
| 100 | + for range rng.IntN(50) { |
| 101 | + dst[rng.Int()] = rng.IntN(100) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + var encoded [][]byte |
| 106 | + b, err := test.MarshalMsg(nil) |
| 107 | + if err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + encoded = append(encoded, b) |
| 111 | + var buf bytes.Buffer |
| 112 | + en := msgp.NewWriter(&buf) |
| 113 | + err = test.EncodeMsg(en) |
| 114 | + if err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | + err = en.Flush() |
| 118 | + if err != nil { |
| 119 | + t.Fatal(err) |
| 120 | + } |
| 121 | + encoded = append(encoded, buf.Bytes()) |
| 122 | + for _, enc := range encoded { |
| 123 | + var decoded, decoded2 MyMapKeyStruct3 |
| 124 | + _, err = decoded.UnmarshalMsg(enc) |
| 125 | + if err != nil { |
| 126 | + t.Fatal(err) |
| 127 | + } |
| 128 | + if !reflect.DeepEqual(&decoded, &test) { |
| 129 | + t.Errorf("decoded != test") |
| 130 | + } |
| 131 | + dec := msgp.NewReader(bytes.NewReader(enc)) |
| 132 | + err = decoded2.DecodeMsg(dec) |
| 133 | + if err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + if !reflect.DeepEqual(&decoded2, &test) { |
| 137 | + t.Errorf("decoded2 != test") |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments