1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
package _generated
import (
"bytes"
"reflect"
"testing"
"github.com/tinylib/msgp/msgp"
)
func TestGenericsMarshal(t *testing.T) {
x := GenericTest[Fixed, *Fixed]{}
x.B = &Fixed{A: 1.5}
x.C = append(x.C, Fixed{A: 2.5})
x.D = map[string]Fixed{"hello": Fixed{A: 2.5}}
x.E.A = Fixed{A: 2.5}
x.F = append(x.F, GenericTest2[Fixed, *Fixed, string]{A: Fixed{A: 3.5}})
x.G = map[string]GenericTest2[Fixed, *Fixed, string]{"hello": {A: Fixed{A: 3.5}}}
bts, err := x.MarshalMsg(nil)
if err != nil {
t.Fatal(err)
}
got := GenericTest[Fixed, *Fixed]{}
got.B = x.B // We must initialize this.
*got.B = Fixed{}
bts, err = got.UnmarshalMsg(bts)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(x, got) {
t.Errorf("\n got=%#v\nwant=%#v", got, x)
}
}
func TestGenericsEncode(t *testing.T) {
x := GenericTest[Fixed, *Fixed]{}
x.B = &Fixed{A: 1.5}
x.C = append(x.C, Fixed{A: 2.5})
x.D = map[string]Fixed{"hello": Fixed{A: 2.5}}
x.E.A = Fixed{A: 2.5}
x.F = append(x.F, GenericTest2[Fixed, *Fixed, string]{A: Fixed{A: 3.5}})
x.G = map[string]GenericTest2[Fixed, *Fixed, string]{"hello": {A: Fixed{A: 3.5}}}
var buf bytes.Buffer
w := msgp.NewWriter(&buf)
err := x.EncodeMsg(w)
if err != nil {
t.Fatal(err)
}
w.Flush()
got := GenericTest[Fixed, *Fixed]{}
got.B = x.B // We must initialize this.
*got.B = Fixed{}
r := msgp.NewReader(&buf)
err = got.DecodeMsg(r)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(x, got) {
t.Errorf("\n got=%#v\nwant=%#v", got, x)
}
}
// Test for generic alias types (slices, maps, arrays) - these verify the type parameter fix
func TestGenericTest3List(t *testing.T) {
// Test GenericTest3List
list := GenericTest3List[Fixed, Fixed, *Fixed, *Fixed]{
{A: Fixed{A: 1.5}, B: Fixed{A: 2.5}},
{A: Fixed{A: 3.5}, B: Fixed{A: 4.5}},
}
// Test marshaling
data, err := list.MarshalMsg(nil)
if err != nil {
t.Fatalf("GenericTest3List.MarshalMsg failed: %v", err)
}
// Test unmarshaling
var list2 GenericTest3List[Fixed, Fixed, *Fixed, *Fixed]
_, err = list2.UnmarshalMsg(data)
if err != nil {
t.Fatalf("GenericTest3List.UnmarshalMsg failed: %v", err)
}
// Verify round-trip
if len(list2) != 2 || list2[0].A.A != 1.5 || list2[0].B.A != 2.5 {
t.Errorf("GenericTest3List round-trip failed")
}
}
func TestGenericTest3Map(t *testing.T) {
// Test GenericTest3Map
testMap := GenericTest3Map[Fixed, Fixed, *Fixed, *Fixed]{
"key1": {A: Fixed{A: 1.5}, B: Fixed{A: 2.5}},
"key2": {A: Fixed{A: 3.5}, B: Fixed{A: 4.5}},
}
// Test marshaling
data, err := testMap.MarshalMsg(nil)
if err != nil {
t.Fatalf("GenericTest3Map.MarshalMsg failed: %v", err)
}
// Test unmarshaling
var testMap2 GenericTest3Map[Fixed, Fixed, *Fixed, *Fixed]
_, err = testMap2.UnmarshalMsg(data)
if err != nil {
t.Fatalf("GenericTest3Map.UnmarshalMsg failed: %v", err)
}
// Verify round-trip
if len(testMap2) != 2 || testMap2["key1"].A.A != 1.5 || testMap2["key1"].B.A != 2.5 {
t.Errorf("GenericTest3Map round-trip failed")
}
}
func TestGenericTest3Array(t *testing.T) {
// Test GenericTest3Array
testArray := GenericTest3Array[Fixed, Fixed, *Fixed, *Fixed]{
{A: Fixed{A: 1.5}, B: Fixed{A: 2.5}},
{A: Fixed{A: 3.5}, B: Fixed{A: 4.5}},
{A: Fixed{A: 5.5}, B: Fixed{A: 6.5}},
}
// Test marshaling
data, err := testArray.MarshalMsg(nil)
if err != nil {
t.Fatalf("GenericTest3Array.MarshalMsg failed: %v", err)
}
// Test unmarshaling
var testArray2 GenericTest3Array[Fixed, Fixed, *Fixed, *Fixed]
_, err = testArray2.UnmarshalMsg(data)
if err != nil {
t.Fatalf("GenericTest3Array.UnmarshalMsg failed: %v", err)
}
// Verify round-trip
if testArray2[0].A.A != 1.5 || testArray2[0].B.A != 2.5 || testArray2[2].A.A != 5.5 {
t.Errorf("GenericTest3Array round-trip failed")
}
}
|