Skip to content

Commit 734c5ce

Browse files
committed
Add advanced tests.
The new tests covers: * Map of string -> string * Map of int32 -> vector of struct * Map of struct -> vector of int32 * The encoding is canonical. Two maps with same content but different in-memory order, they should serialize with the exact same bytes.
1 parent acf8c70 commit 734c5ce

File tree

1 file changed

+79
-127
lines changed

1 file changed

+79
-127
lines changed

codec/test_codec.go

Lines changed: 79 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ var Tests = []func(c GeneralCodec, t testing.TB){
4242
TestExtraSpace,
4343
TestSliceLengthOverflow,
4444
TestMap,
45-
TestMap2,
46-
TestMap3,
47-
TestMapSorting,
4845
}
4946

5047
var MultipleTagsTests = []func(c GeneralCodec, t testing.TB){
@@ -88,29 +85,36 @@ type MyInnerStruct3 struct {
8885
}
8986

9087
type myStruct struct {
91-
InnerStruct MyInnerStruct `serialize:"true"`
92-
InnerStruct2 *MyInnerStruct `serialize:"true"`
93-
Member1 int64 `serialize:"true"`
94-
Member2 uint16 `serialize:"true"`
95-
MyArray2 [5]string `serialize:"true"`
96-
MyArray3 [3]MyInnerStruct `serialize:"true"`
97-
MyArray4 [2]*MyInnerStruct2 `serialize:"true"`
98-
MySlice []byte `serialize:"true"`
99-
MySlice2 []string `serialize:"true"`
100-
MySlice3 []MyInnerStruct `serialize:"true"`
101-
MySlice4 []*MyInnerStruct2 `serialize:"true"`
102-
MyArray [4]byte `serialize:"true"`
103-
MyInterface Foo `serialize:"true"`
104-
MySlice5 []Foo `serialize:"true"`
105-
InnerStruct3 MyInnerStruct3 `serialize:"true"`
106-
MyPointer *Foo `serialize:"true"`
88+
InnerStruct MyInnerStruct `serialize:"true"`
89+
InnerStruct2 *MyInnerStruct `serialize:"true"`
90+
Member1 int64 `serialize:"true"`
91+
Member2 uint16 `serialize:"true"`
92+
MyArray2 [5]string `serialize:"true"`
93+
MyArray3 [3]MyInnerStruct `serialize:"true"`
94+
MyArray4 [2]*MyInnerStruct2 `serialize:"true"`
95+
MySlice []byte `serialize:"true"`
96+
MySlice2 []string `serialize:"true"`
97+
MySlice3 []MyInnerStruct `serialize:"true"`
98+
MySlice4 []*MyInnerStruct2 `serialize:"true"`
99+
MyArray [4]byte `serialize:"true"`
100+
MyInterface Foo `serialize:"true"`
101+
MySlice5 []Foo `serialize:"true"`
102+
InnerStruct3 MyInnerStruct3 `serialize:"true"`
103+
MyPointer *Foo `serialize:"true"`
104+
MyMap1 map[string]string `serialize:"true"`
105+
MyMap2 map[int32][]MyInnerStruct3 `serialize:"true"`
106+
MyMap3 map[MyInnerStruct2][]int32 `serialize:"true"`
107107
}
108108

109109
// Test marshaling/unmarshaling a complicated struct
110110
func TestStruct(codec GeneralCodec, t testing.TB) {
111111
require := require.New(t)
112112

113113
temp := Foo(&MyInnerStruct{})
114+
myMap3 := make(map[MyInnerStruct2][]int32)
115+
myMap3[MyInnerStruct2{false}] = []int32{991, 12}
116+
myMap3[MyInnerStruct2{true}] = []int32{1911, 1921}
117+
114118
myStructInstance := myStruct{
115119
InnerStruct: MyInnerStruct{"hello"},
116120
InnerStruct2: &MyInnerStruct{"yello"},
@@ -134,6 +138,44 @@ func TestStruct(codec GeneralCodec, t testing.TB) {
134138
F: &MyInnerStruct2{},
135139
},
136140
MyPointer: &temp,
141+
MyMap1: map[string]string{
142+
"test": "test",
143+
},
144+
MyMap2: map[int32][]MyInnerStruct3{
145+
199921: {
146+
{
147+
Str: "str-1",
148+
M1: MyInnerStruct{
149+
Str: "other str",
150+
},
151+
F: &MyInnerStruct2{},
152+
},
153+
{
154+
Str: "str-2",
155+
M1: MyInnerStruct{
156+
Str: "other str",
157+
},
158+
F: &MyInnerStruct2{},
159+
},
160+
},
161+
1921: {
162+
{
163+
Str: "str0",
164+
M1: MyInnerStruct{
165+
Str: "other str",
166+
},
167+
F: &MyInnerStruct2{},
168+
},
169+
{
170+
Str: "str1",
171+
M1: MyInnerStruct{
172+
Str: "other str",
173+
},
174+
F: &MyInnerStruct2{},
175+
},
176+
},
177+
},
178+
MyMap3: myMap3,
137179
}
138180

139181
manager := NewDefaultManager()
@@ -895,124 +937,34 @@ func TestMultipleTags(codec GeneralCodec, t testing.TB) {
895937
func TestMap(codec GeneralCodec, t testing.TB) {
896938
require := require.New(t)
897939

898-
data := make(map[string]int32)
899-
data["test"] = 12
900-
data["bar"] = 33
901-
902-
manager := NewDefaultManager()
903-
require.NoError(manager.RegisterCodec(0, codec))
904-
905-
bytes, err := manager.Marshal(0, data)
906-
require.NoError(err)
907-
908-
bytesLen, err := manager.Size(0, data)
909-
require.NoError(err)
910-
require.Equal(len(bytes), bytesLen)
911-
912-
var output map[string]int32
913-
_, err = manager.Unmarshal(bytes, &output)
914-
require.NoError(err)
915-
916-
require.Equal(data, output)
917-
}
918-
919-
func TestMap2(codec GeneralCodec, t testing.TB) {
920-
require := require.New(t)
921-
922-
type Foo struct {
923-
A int32 `serialize:"true"`
924-
B string `serialize:"true"`
925-
}
926-
927-
data := make(map[int32]Foo)
928-
data[12] = Foo{A: 1, B: "test"}
929-
data[13] = Foo{A: 2, B: "more test"}
930-
931-
manager := NewDefaultManager()
932-
require.NoError(manager.RegisterCodec(0, codec))
933-
934-
bytes, err := manager.Marshal(0, data)
935-
require.NoError(err)
936-
937-
bytesLen, err := manager.Size(0, data)
938-
require.NoError(err)
939-
require.Equal(len(bytes), bytesLen)
940-
941-
var output map[int32]Foo
942-
_, err = manager.Unmarshal(bytes, &output)
943-
require.NoError(err)
944-
945-
require.Equal(data, output)
946-
}
947-
948-
func TestMap3(codec GeneralCodec, t testing.TB) {
949-
require := require.New(t)
950-
951-
type Foo struct {
952-
A int32 `serialize:"true"`
953-
B string `serialize:"true"`
954-
E map[int32]string `serialize:"true"`
955-
}
940+
data1 := make(map[string]int32)
941+
data1["test"] = 12
942+
data1["bar"] = 33
956943

957-
data := Foo{
958-
A: 1,
959-
B: "test",
960-
E: make(map[int32]string, 2),
961-
}
962-
data.E[12] = "test"
963-
data.E[13] = "test"
944+
data2 := make(map[string]int32)
945+
data2["bar"] = 33
946+
data2["test"] = 12
964947

965948
manager := NewDefaultManager()
966949
require.NoError(manager.RegisterCodec(0, codec))
967950

968-
bytes, err := manager.Marshal(0, data)
969-
require.NoError(err)
970-
971-
bytesLen, err := manager.Size(0, data)
951+
bytes1, err := manager.Marshal(0, data1)
972952
require.NoError(err)
973-
require.Equal(len(bytes), bytesLen)
974953

975-
var output Foo
976-
_, err = manager.Unmarshal(bytes, &output)
954+
// data1 and data2, although the data has a different order, should be
955+
// serialized using the same bytes. This will allow the serialized data to
956+
// be used safely to hash data, to be used by nodes to exchange hashes and
957+
// only request the whole data when the hash is not different than expected
958+
bytes2, err := manager.Marshal(0, data2)
977959
require.NoError(err)
960+
require.Equal(bytes1, bytes2)
978961

979-
require.Equal(data, output)
980-
}
981-
982-
func TestMapSorting(codec GeneralCodec, t testing.TB) {
983-
require := require.New(t)
984-
985-
type Foo struct {
986-
A int32 `serialize:"true"`
987-
B string `serialize:"true"`
988-
E map[int32]string `serialize:"true"`
989-
}
990-
991-
manager := NewDefaultManager()
992-
require.NoError(manager.RegisterCodec(0, codec))
993-
994-
// test sorting
995-
data1 := Foo{
996-
A: 1,
997-
B: "test",
998-
E: make(map[int32]string, 2),
999-
}
1000-
data1.E[12] = "test-12"
1001-
data1.E[13] = "test-13"
1002-
1003-
data2 := Foo{
1004-
A: 1,
1005-
B: "test",
1006-
E: make(map[int32]string, 2),
1007-
}
1008-
data2.E[13] = "test-13"
1009-
data2.E[12] = "test-12"
1010-
1011-
bytes1, err := manager.Marshal(0, data1)
962+
bytesLen, err := manager.Size(0, data1)
1012963
require.NoError(err)
964+
require.Equal(len(bytes1), bytesLen)
1013965

1014-
bytes2, err := manager.Marshal(0, data2)
966+
var output map[string]int32
967+
_, err = manager.Unmarshal(bytes1, &output)
1015968
require.NoError(err)
1016-
1017-
require.Equal(bytes1, bytes2)
969+
require.Equal(data1, output)
1018970
}

0 commit comments

Comments
 (0)