blob: deecb77eeb8e877e8a9cdc3593d017a1cb737217 [file] [log] [blame]
Yuxuan 'fishy' Wangbb8fec72021-02-18 09:09:20 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
wangtieju4aaef752021-02-04 11:26:44 +080020package tests
21
22import (
23 "equalstest"
24 "strconv"
25 "testing"
26)
27
28func TestEquals(t *testing.T) {
29 // test basic field
30 basicTgt, basicSrc := genBasicFoo(), genBasicFoo()
31 if !basicTgt.Equals(basicSrc) {
32 t.Error("BasicEqualsFoo.Equals() test failed")
33 }
34 basicSrc.EnumFoo = equalstest.EnumFoo_e2
35 if basicTgt.Equals(basicSrc) {
36 t.Error("BasicEqualsFoo.Equals() test failed")
37 }
38 basicSrc = genBasicFoo()
39 basicSrc.OptBoolFoo = nil
40 if basicTgt.Equals(basicSrc) || basicSrc.Equals(basicTgt) {
41 t.Error("BasicEqualsFoo.Equals() test failed")
42 }
43 if !(&equalstest.BasicEqualsFoo{}).Equals(&equalstest.BasicEqualsFoo{}) {
44 t.Error("BasicEqualsFoo.Equals() test failed")
45 }
46 // test struct field
47 structTgt, structSrc := genStructFoo(), genStructFoo()
48 if !structTgt.Equals(structSrc) {
49 t.Error("StructEqualsFoo.Equals() test failed")
50 }
51 structSrc.OptStructFoo.EnumFoo = equalstest.EnumFoo_e2
52 if structTgt.Equals(structSrc) {
53 t.Error("StructEqualsFoo.Equals() test failed")
54 }
55 structSrc = genStructFoo()
56 structSrc.OptStructFoo = nil
57 if structTgt.Equals(structSrc) || structSrc.Equals(structTgt) {
58 t.Error("StructEqualsFoo.Equals() test failed")
59 }
60 if !(&equalstest.StructEqualsFoo{}).Equals(&equalstest.StructEqualsFoo{}) {
61 t.Error("StructEqualsFoo.Equals() test failed")
62 }
63 // test list field
64 listTgt, listSrc := genListFoo(), genListFoo()
65 if !listTgt.Equals(listSrc) {
66 t.Error("ListEqualsFoo.Equals() test failed")
67 }
68 listSrc.OptI64StringMapListFoo[0][1] = "0"
69 if listTgt.Equals(listSrc) {
70 t.Error("ListEqualsFoo.Equals() test failed")
71 }
72 listSrc = genListFoo()
73 listSrc.OptI64StringMapListFoo = nil
74 if listTgt.Equals(listSrc) || listSrc.Equals(listTgt) {
75 t.Error("ListEqualsFoo.Equals() test failed")
76 }
77 if !(&equalstest.ListEqualsFoo{}).Equals(&equalstest.ListEqualsFoo{}) {
78 t.Error("ListEqualsFoo.Equals() test failed")
79 }
80 // test set field
81 setTgt, setSrc := genSetFoo(), genSetFoo()
82 if !setTgt.Equals(setSrc) {
83 t.Error("SetEqualsFoo.Equals() test failed")
84 }
85 setSrc.OptI64StringMapSetFoo[0][1] = "0"
86 if setTgt.Equals(setSrc) {
87 t.Error("SetEqualsFoo.Equals() test failed")
88 }
89 setSrc = genSetFoo()
90 setSrc.OptI64StringMapSetFoo = nil
91 if setTgt.Equals(setSrc) || setSrc.Equals(setTgt) {
92 t.Error("SetEqualsFoo.Equals() test failed")
93 }
94 if !(&equalstest.SetEqualsFoo{}).Equals(&equalstest.SetEqualsFoo{}) {
95 t.Error("SetEqualsFoo.Equals() test failed")
96 }
97 // test map field
98 mapTgt, mapSrc := genMapFoo(), genMapFoo()
99 if !mapTgt.Equals(mapSrc) {
100 t.Error("MapEqualsFoo.Equals() test failed")
101 }
102 mapSrc.OptI64I64StringMapMapFoo[1][1] = "0"
103 if mapTgt.Equals(mapSrc) {
104 t.Error("MapEqualsFoo.Equals() test failed")
105 }
106 mapSrc = genMapFoo()
107 mapSrc.OptI64I64StringMapMapFoo = nil
108 if mapTgt.Equals(mapSrc) || mapSrc.Equals(mapTgt) {
109 t.Error("MapEqualsFoo.Equals() test failed")
110 }
111 if !(&equalstest.MapEqualsFoo{}).Equals(&equalstest.MapEqualsFoo{}) {
112 t.Error("MapEqualsFoo.Equals() test failed")
113 }
114}
115
116func genBasicFoo() *equalstest.BasicEqualsFoo {
117 return &equalstest.BasicEqualsFoo{
118 BoolFoo: true,
119 OptBoolFoo: &(&struct{ x bool }{true}).x,
120 I8Foo: 1,
121 OptI8Foo: &(&struct{ x int8 }{1}).x,
122 I16Foo: 2,
123 OptI16Foo: &(&struct{ x int16 }{2}).x,
124 I32Foo: 3,
125 OptI32Foo: &(&struct{ x int32 }{3}).x,
126 I64Foo: 4,
127 OptI64Foo: &(&struct{ x int64 }{4}).x,
128 DoubleFoo: 5,
129 OptDoubleFoo: &(&struct{ x float64 }{5}).x,
130 StrFoo: "6",
131 OptStrFoo: &(&struct{ x string }{"6"}).x,
132 BinFoo: []byte("7"),
133 OptBinFoo: []byte("7"),
134 EnumFoo: equalstest.EnumFoo_e1,
135 OptEnumFoo: equalstest.EnumFooPtr(equalstest.EnumFoo_e1),
136 MyByteFoo: equalstest.Mybyte(8),
137 OptMyByteFoo: equalstest.MybytePtr(8),
138 MyStrFoo: equalstest.Mystr("9"),
139 OptMyStrFoo: equalstest.MystrPtr(equalstest.Mystr("9")),
140 MyBinFoo: equalstest.Mybin("10"),
141 OptMyBinFoo: equalstest.Mybin("10"),
142 }
143}
144
145func genStructFoo() *equalstest.StructEqualsFoo {
146 return &equalstest.StructEqualsFoo{
147 StructFoo: genBasicFoo(),
148 OptStructFoo: genBasicFoo(),
149 }
150}
151
152func genListFoo() *equalstest.ListEqualsFoo {
153 return &equalstest.ListEqualsFoo{
154 I64ListFoo: genInt64Slice(6),
155 OptI64ListFoo: genInt64Slice(6),
156 StrListFoo: genStringSlice(6),
157 OptStrListFoo: genStringSlice(6),
158 BinListFoo: genBytesSlice(6),
159 OptBinListFoo: genBytesSlice(6),
160 StructListFoo: []*equalstest.BasicEqualsFoo{genBasicFoo(), {}},
161 OptStructListFoo: []*equalstest.BasicEqualsFoo{genBasicFoo(), {}},
162 I64ListListFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
163 OptI64ListListFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
164 I64SetListFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
165 OptI64SetListFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
166 I64StringMapListFoo: []map[int64]string{{6: "6"}, {5: "5"}, {4: "4"}, {3: "3"}, {2: "2"}, {1: "1"}},
167 OptI64StringMapListFoo: []map[int64]string{{6: "6"}, {5: "5"}, {4: "4"}, {3: "3"}, {2: "2"}, {1: "1"}},
168 MyByteListFoo: []equalstest.Mybyte{6, 5, 4, 3, 2, 1},
169 OptMyByteListFoo: []equalstest.Mybyte{6, 5, 4, 3, 2, 1},
170 MyStrListFoo: []equalstest.Mystr{equalstest.Mystr("6"), equalstest.Mystr("5"), equalstest.Mystr("4"), equalstest.Mystr("3"), equalstest.Mystr("2"), equalstest.Mystr("1")},
171 OptMyStrListFoo: []equalstest.Mystr{equalstest.Mystr("6"), equalstest.Mystr("5"), equalstest.Mystr("4"), equalstest.Mystr("3"), equalstest.Mystr("2"), equalstest.Mystr("1")},
172 MyBinListFoo: []equalstest.Mybin{equalstest.Mybin("6"), equalstest.Mybin("5"), equalstest.Mybin("4"), equalstest.Mybin("3"), equalstest.Mybin("2"), equalstest.Mybin("1")},
173 OptMyBinListFoo: []equalstest.Mybin{equalstest.Mybin("6"), equalstest.Mybin("5"), equalstest.Mybin("4"), equalstest.Mybin("3"), equalstest.Mybin("2"), equalstest.Mybin("1")},
174 }
175}
176
177func genSetFoo() *equalstest.SetEqualsFoo {
178 return &equalstest.SetEqualsFoo{
179 I64SetFoo: genInt64Slice(6),
180 OptI64SetFoo: genInt64Slice(6),
181 StrSetFoo: genStringSlice(6),
182 OptStrSetFoo: genStringSlice(6),
183 BinSetFoo: genBytesSlice(6),
184 OptBinSetFoo: genBytesSlice(6),
185 StructSetFoo: []*equalstest.BasicEqualsFoo{genBasicFoo(), {}},
186 OptStructSetFoo: []*equalstest.BasicEqualsFoo{genBasicFoo(), {}},
187 I64ListSetFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
188 OptI64ListSetFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
189 I64SetSetFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
190 OptI64SetSetFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
191 I64StringMapSetFoo: []map[int64]string{{6: "6"}, {5: "5"}, {4: "4"}, {3: "3"}, {2: "2"}, {1: "1"}},
192 OptI64StringMapSetFoo: []map[int64]string{{6: "6"}, {5: "5"}, {4: "4"}, {3: "3"}, {2: "2"}, {1: "1"}},
193 MyByteSetFoo: []equalstest.Mybyte{6, 5, 4, 3, 2, 1},
194 OptMyByteSetFoo: []equalstest.Mybyte{6, 5, 4, 3, 2, 1},
195 MyStrSetFoo: []equalstest.Mystr{equalstest.Mystr("6"), equalstest.Mystr("5"), equalstest.Mystr("4"), equalstest.Mystr("3"), equalstest.Mystr("2"), equalstest.Mystr("1")},
196 OptMyStrSetFoo: []equalstest.Mystr{equalstest.Mystr("6"), equalstest.Mystr("5"), equalstest.Mystr("4"), equalstest.Mystr("3"), equalstest.Mystr("2"), equalstest.Mystr("1")},
197 MyBinSetFoo: []equalstest.Mybin{equalstest.Mybin("6"), equalstest.Mybin("5"), equalstest.Mybin("4"), equalstest.Mybin("3"), equalstest.Mybin("2"), equalstest.Mybin("1")},
198 OptMyBinSetFoo: []equalstest.Mybin{equalstest.Mybin("6"), equalstest.Mybin("5"), equalstest.Mybin("4"), equalstest.Mybin("3"), equalstest.Mybin("2"), equalstest.Mybin("1")},
199 }
200}
201
202var (
203 structMapKey0 = genBasicFoo()
204 structMapKey1 = &equalstest.BasicEqualsFoo{}
205)
206
207func genMapFoo() *equalstest.MapEqualsFoo {
208 return &equalstest.MapEqualsFoo{
209 I64StrMapFoo: genInt64StringMap(6),
210 OptI64StrMapFoo: genInt64StringMap(6),
211 StrI64MapFoo: map[string]int64{"6": 6, "5": 5, "4": 4, "3": 3, "2": 2, "1": 1},
212 OptStrI64MapFoo: map[string]int64{"6": 6, "5": 5, "4": 4, "3": 3, "2": 2, "1": 1},
213 StructBinMapFoo: map[*equalstest.BasicEqualsFoo][]byte{structMapKey0: []byte("0"), structMapKey1: []byte("1")},
214 OptStructBinMapFoo: map[*equalstest.BasicEqualsFoo][]byte{structMapKey0: []byte("0"), structMapKey1: []byte("1")},
215 BinStructMapFoo: map[string]*equalstest.BasicEqualsFoo{"1": genBasicFoo(), "0": {}},
216 OptBinStructMapFoo: map[string]*equalstest.BasicEqualsFoo{"1": genBasicFoo(), "0": {}},
217 I64I64ListMapFoo: map[int64][]int64{6: genInt64Slice(6), 5: genInt64Slice(5), 4: genInt64Slice(4), 3: genInt64Slice(3), 2: genInt64Slice(2), 1: genInt64Slice(1)},
218 OptI64I64ListMapFoo: map[int64][]int64{6: genInt64Slice(6), 5: genInt64Slice(5), 4: genInt64Slice(4), 3: genInt64Slice(3), 2: genInt64Slice(2), 1: genInt64Slice(1)},
219 I64I64SetMapFoo: map[int64][]int64{6: genInt64Slice(6), 5: genInt64Slice(5), 4: genInt64Slice(4), 3: genInt64Slice(3), 2: genInt64Slice(2), 1: genInt64Slice(1)},
220 OptI64I64SetMapFoo: map[int64][]int64{6: genInt64Slice(6), 5: genInt64Slice(5), 4: genInt64Slice(4), 3: genInt64Slice(3), 2: genInt64Slice(2), 1: genInt64Slice(1)},
221 I64I64StringMapMapFoo: map[int64]map[int64]string{6: genInt64StringMap(6), 5: genInt64StringMap(5), 4: genInt64StringMap(4), 3: genInt64StringMap(3), 2: genInt64StringMap(2), 1: genInt64StringMap(1)},
222 OptI64I64StringMapMapFoo: map[int64]map[int64]string{6: genInt64StringMap(6), 5: genInt64StringMap(5), 4: genInt64StringMap(4), 3: genInt64StringMap(3), 2: genInt64StringMap(2), 1: genInt64StringMap(1)},
223 MyStrMyBinMapFoo: map[equalstest.Mystr]equalstest.Mybin{equalstest.Mystr("1"): equalstest.Mybin("1"), equalstest.Mystr("0"): equalstest.Mybin("0")},
224 OptMyStrMyBinMapFoo: map[equalstest.Mystr]equalstest.Mybin{equalstest.Mystr("1"): equalstest.Mybin("1"), equalstest.Mystr("0"): equalstest.Mybin("0")},
225 Int64MyByteMapFoo: map[int64]equalstest.Mybyte{6: equalstest.Mybyte(6), 5: equalstest.Mybyte(5), 4: equalstest.Mybyte(4), 3: equalstest.Mybyte(3), 2: equalstest.Mybyte(2), 1: equalstest.Mybyte(1)},
226 OptInt64MyByteMapFoo: map[int64]equalstest.Mybyte{6: equalstest.Mybyte(6), 5: equalstest.Mybyte(5), 4: equalstest.Mybyte(4), 3: equalstest.Mybyte(3), 2: equalstest.Mybyte(2), 1: equalstest.Mybyte(1)},
227 MyByteInt64MapFoo: map[equalstest.Mybyte]int64{equalstest.Mybyte(6): 6, equalstest.Mybyte(5): 5, equalstest.Mybyte(4): 4, equalstest.Mybyte(3): 3, equalstest.Mybyte(2): 2, equalstest.Mybyte(1): 1},
228 OptMyByteInt64MapFoo: map[equalstest.Mybyte]int64{equalstest.Mybyte(6): 6, equalstest.Mybyte(5): 5, equalstest.Mybyte(4): 4, equalstest.Mybyte(3): 3, equalstest.Mybyte(2): 2, equalstest.Mybyte(1): 1},
229 }
230}
231
232func genInt64Slice(length int) []int64 {
233 ret := make([]int64, length)
234 for i := 0; i < length; i++ {
235 ret[i] = int64(length - i)
236 }
237 return ret
238}
239
240func genStringSlice(length int) []string {
241 ret := make([]string, length)
242 for i := 0; i < length; i++ {
243 ret[i] = strconv.Itoa(length - i)
244 }
245 return ret
246}
247
248func genBytesSlice(length int) [][]byte {
249 ret := make([][]byte, length)
250 for i := 0; i < length; i++ {
251 ret[i] = []byte(strconv.Itoa(length - i))
252 }
253 return ret
254}
255
256func genInt64StringMap(length int) map[int64]string {
257 ret := make(map[int64]string, length)
258 for i := 0; i < length; i++ {
259 ret[int64(i)] = strconv.Itoa(i)
260 }
261 return ret
262}