blob: 3bd14b6df50c7346863f2b1d1d77d48353d564d9 [file] [log] [blame]
Yuxuan 'fishy' Wangb0b35312021-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 (
wangtieju4aaef752021-02-04 11:26:44 +080023 "strconv"
24 "testing"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070025
26 "github.com/apache/thrift/lib/go/test/gopath/src/equalstest"
wangtieju4aaef752021-02-04 11:26:44 +080027)
28
29func TestEquals(t *testing.T) {
30 // test basic field
31 basicTgt, basicSrc := genBasicFoo(), genBasicFoo()
32 if !basicTgt.Equals(basicSrc) {
33 t.Error("BasicEqualsFoo.Equals() test failed")
34 }
35 basicSrc.EnumFoo = equalstest.EnumFoo_e2
36 if basicTgt.Equals(basicSrc) {
37 t.Error("BasicEqualsFoo.Equals() test failed")
38 }
39 basicSrc = genBasicFoo()
40 basicSrc.OptBoolFoo = nil
41 if basicTgt.Equals(basicSrc) || basicSrc.Equals(basicTgt) {
42 t.Error("BasicEqualsFoo.Equals() test failed")
43 }
44 if !(&equalstest.BasicEqualsFoo{}).Equals(&equalstest.BasicEqualsFoo{}) {
45 t.Error("BasicEqualsFoo.Equals() test failed")
46 }
47 // test struct field
48 structTgt, structSrc := genStructFoo(), genStructFoo()
49 if !structTgt.Equals(structSrc) {
50 t.Error("StructEqualsFoo.Equals() test failed")
51 }
52 structSrc.OptStructFoo.EnumFoo = equalstest.EnumFoo_e2
53 if structTgt.Equals(structSrc) {
54 t.Error("StructEqualsFoo.Equals() test failed")
55 }
56 structSrc = genStructFoo()
57 structSrc.OptStructFoo = nil
58 if structTgt.Equals(structSrc) || structSrc.Equals(structTgt) {
59 t.Error("StructEqualsFoo.Equals() test failed")
60 }
61 if !(&equalstest.StructEqualsFoo{}).Equals(&equalstest.StructEqualsFoo{}) {
62 t.Error("StructEqualsFoo.Equals() test failed")
63 }
64 // test list field
65 listTgt, listSrc := genListFoo(), genListFoo()
66 if !listTgt.Equals(listSrc) {
67 t.Error("ListEqualsFoo.Equals() test failed")
68 }
69 listSrc.OptI64StringMapListFoo[0][1] = "0"
70 if listTgt.Equals(listSrc) {
71 t.Error("ListEqualsFoo.Equals() test failed")
72 }
73 listSrc = genListFoo()
74 listSrc.OptI64StringMapListFoo = nil
75 if listTgt.Equals(listSrc) || listSrc.Equals(listTgt) {
76 t.Error("ListEqualsFoo.Equals() test failed")
77 }
78 if !(&equalstest.ListEqualsFoo{}).Equals(&equalstest.ListEqualsFoo{}) {
79 t.Error("ListEqualsFoo.Equals() test failed")
80 }
81 // test set field
82 setTgt, setSrc := genSetFoo(), genSetFoo()
83 if !setTgt.Equals(setSrc) {
84 t.Error("SetEqualsFoo.Equals() test failed")
85 }
86 setSrc.OptI64StringMapSetFoo[0][1] = "0"
87 if setTgt.Equals(setSrc) {
88 t.Error("SetEqualsFoo.Equals() test failed")
89 }
90 setSrc = genSetFoo()
91 setSrc.OptI64StringMapSetFoo = nil
92 if setTgt.Equals(setSrc) || setSrc.Equals(setTgt) {
93 t.Error("SetEqualsFoo.Equals() test failed")
94 }
95 if !(&equalstest.SetEqualsFoo{}).Equals(&equalstest.SetEqualsFoo{}) {
96 t.Error("SetEqualsFoo.Equals() test failed")
97 }
98 // test map field
99 mapTgt, mapSrc := genMapFoo(), genMapFoo()
100 if !mapTgt.Equals(mapSrc) {
101 t.Error("MapEqualsFoo.Equals() test failed")
102 }
103 mapSrc.OptI64I64StringMapMapFoo[1][1] = "0"
104 if mapTgt.Equals(mapSrc) {
105 t.Error("MapEqualsFoo.Equals() test failed")
106 }
107 mapSrc = genMapFoo()
108 mapSrc.OptI64I64StringMapMapFoo = nil
109 if mapTgt.Equals(mapSrc) || mapSrc.Equals(mapTgt) {
110 t.Error("MapEqualsFoo.Equals() test failed")
111 }
112 if !(&equalstest.MapEqualsFoo{}).Equals(&equalstest.MapEqualsFoo{}) {
113 t.Error("MapEqualsFoo.Equals() test failed")
114 }
115}
116
117func genBasicFoo() *equalstest.BasicEqualsFoo {
118 return &equalstest.BasicEqualsFoo{
119 BoolFoo: true,
120 OptBoolFoo: &(&struct{ x bool }{true}).x,
121 I8Foo: 1,
122 OptI8Foo: &(&struct{ x int8 }{1}).x,
123 I16Foo: 2,
124 OptI16Foo: &(&struct{ x int16 }{2}).x,
125 I32Foo: 3,
126 OptI32Foo: &(&struct{ x int32 }{3}).x,
127 I64Foo: 4,
128 OptI64Foo: &(&struct{ x int64 }{4}).x,
129 DoubleFoo: 5,
130 OptDoubleFoo: &(&struct{ x float64 }{5}).x,
131 StrFoo: "6",
132 OptStrFoo: &(&struct{ x string }{"6"}).x,
133 BinFoo: []byte("7"),
134 OptBinFoo: []byte("7"),
135 EnumFoo: equalstest.EnumFoo_e1,
136 OptEnumFoo: equalstest.EnumFooPtr(equalstest.EnumFoo_e1),
137 MyByteFoo: equalstest.Mybyte(8),
138 OptMyByteFoo: equalstest.MybytePtr(8),
139 MyStrFoo: equalstest.Mystr("9"),
140 OptMyStrFoo: equalstest.MystrPtr(equalstest.Mystr("9")),
141 MyBinFoo: equalstest.Mybin("10"),
142 OptMyBinFoo: equalstest.Mybin("10"),
143 }
144}
145
146func genStructFoo() *equalstest.StructEqualsFoo {
147 return &equalstest.StructEqualsFoo{
148 StructFoo: genBasicFoo(),
149 OptStructFoo: genBasicFoo(),
150 }
151}
152
153func genListFoo() *equalstest.ListEqualsFoo {
154 return &equalstest.ListEqualsFoo{
155 I64ListFoo: genInt64Slice(6),
156 OptI64ListFoo: genInt64Slice(6),
157 StrListFoo: genStringSlice(6),
158 OptStrListFoo: genStringSlice(6),
159 BinListFoo: genBytesSlice(6),
160 OptBinListFoo: genBytesSlice(6),
161 StructListFoo: []*equalstest.BasicEqualsFoo{genBasicFoo(), {}},
162 OptStructListFoo: []*equalstest.BasicEqualsFoo{genBasicFoo(), {}},
163 I64ListListFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
164 OptI64ListListFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
165 I64SetListFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
166 OptI64SetListFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
167 I64StringMapListFoo: []map[int64]string{{6: "6"}, {5: "5"}, {4: "4"}, {3: "3"}, {2: "2"}, {1: "1"}},
168 OptI64StringMapListFoo: []map[int64]string{{6: "6"}, {5: "5"}, {4: "4"}, {3: "3"}, {2: "2"}, {1: "1"}},
169 MyByteListFoo: []equalstest.Mybyte{6, 5, 4, 3, 2, 1},
170 OptMyByteListFoo: []equalstest.Mybyte{6, 5, 4, 3, 2, 1},
171 MyStrListFoo: []equalstest.Mystr{equalstest.Mystr("6"), equalstest.Mystr("5"), equalstest.Mystr("4"), equalstest.Mystr("3"), equalstest.Mystr("2"), equalstest.Mystr("1")},
172 OptMyStrListFoo: []equalstest.Mystr{equalstest.Mystr("6"), equalstest.Mystr("5"), equalstest.Mystr("4"), equalstest.Mystr("3"), equalstest.Mystr("2"), equalstest.Mystr("1")},
173 MyBinListFoo: []equalstest.Mybin{equalstest.Mybin("6"), equalstest.Mybin("5"), equalstest.Mybin("4"), equalstest.Mybin("3"), equalstest.Mybin("2"), equalstest.Mybin("1")},
174 OptMyBinListFoo: []equalstest.Mybin{equalstest.Mybin("6"), equalstest.Mybin("5"), equalstest.Mybin("4"), equalstest.Mybin("3"), equalstest.Mybin("2"), equalstest.Mybin("1")},
175 }
176}
177
178func genSetFoo() *equalstest.SetEqualsFoo {
179 return &equalstest.SetEqualsFoo{
180 I64SetFoo: genInt64Slice(6),
181 OptI64SetFoo: genInt64Slice(6),
182 StrSetFoo: genStringSlice(6),
183 OptStrSetFoo: genStringSlice(6),
184 BinSetFoo: genBytesSlice(6),
185 OptBinSetFoo: genBytesSlice(6),
186 StructSetFoo: []*equalstest.BasicEqualsFoo{genBasicFoo(), {}},
187 OptStructSetFoo: []*equalstest.BasicEqualsFoo{genBasicFoo(), {}},
188 I64ListSetFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
189 OptI64ListSetFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
190 I64SetSetFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
191 OptI64SetSetFoo: [][]int64{genInt64Slice(6), genInt64Slice(5), genInt64Slice(4), genInt64Slice(3), genInt64Slice(2), genInt64Slice(1)},
192 I64StringMapSetFoo: []map[int64]string{{6: "6"}, {5: "5"}, {4: "4"}, {3: "3"}, {2: "2"}, {1: "1"}},
193 OptI64StringMapSetFoo: []map[int64]string{{6: "6"}, {5: "5"}, {4: "4"}, {3: "3"}, {2: "2"}, {1: "1"}},
194 MyByteSetFoo: []equalstest.Mybyte{6, 5, 4, 3, 2, 1},
195 OptMyByteSetFoo: []equalstest.Mybyte{6, 5, 4, 3, 2, 1},
196 MyStrSetFoo: []equalstest.Mystr{equalstest.Mystr("6"), equalstest.Mystr("5"), equalstest.Mystr("4"), equalstest.Mystr("3"), equalstest.Mystr("2"), equalstest.Mystr("1")},
197 OptMyStrSetFoo: []equalstest.Mystr{equalstest.Mystr("6"), equalstest.Mystr("5"), equalstest.Mystr("4"), equalstest.Mystr("3"), equalstest.Mystr("2"), equalstest.Mystr("1")},
198 MyBinSetFoo: []equalstest.Mybin{equalstest.Mybin("6"), equalstest.Mybin("5"), equalstest.Mybin("4"), equalstest.Mybin("3"), equalstest.Mybin("2"), equalstest.Mybin("1")},
199 OptMyBinSetFoo: []equalstest.Mybin{equalstest.Mybin("6"), equalstest.Mybin("5"), equalstest.Mybin("4"), equalstest.Mybin("3"), equalstest.Mybin("2"), equalstest.Mybin("1")},
200 }
201}
202
203var (
204 structMapKey0 = genBasicFoo()
205 structMapKey1 = &equalstest.BasicEqualsFoo{}
206)
207
208func genMapFoo() *equalstest.MapEqualsFoo {
209 return &equalstest.MapEqualsFoo{
210 I64StrMapFoo: genInt64StringMap(6),
211 OptI64StrMapFoo: genInt64StringMap(6),
212 StrI64MapFoo: map[string]int64{"6": 6, "5": 5, "4": 4, "3": 3, "2": 2, "1": 1},
213 OptStrI64MapFoo: map[string]int64{"6": 6, "5": 5, "4": 4, "3": 3, "2": 2, "1": 1},
214 StructBinMapFoo: map[*equalstest.BasicEqualsFoo][]byte{structMapKey0: []byte("0"), structMapKey1: []byte("1")},
215 OptStructBinMapFoo: map[*equalstest.BasicEqualsFoo][]byte{structMapKey0: []byte("0"), structMapKey1: []byte("1")},
216 BinStructMapFoo: map[string]*equalstest.BasicEqualsFoo{"1": genBasicFoo(), "0": {}},
217 OptBinStructMapFoo: map[string]*equalstest.BasicEqualsFoo{"1": genBasicFoo(), "0": {}},
218 I64I64ListMapFoo: map[int64][]int64{6: genInt64Slice(6), 5: genInt64Slice(5), 4: genInt64Slice(4), 3: genInt64Slice(3), 2: genInt64Slice(2), 1: genInt64Slice(1)},
219 OptI64I64ListMapFoo: map[int64][]int64{6: genInt64Slice(6), 5: genInt64Slice(5), 4: genInt64Slice(4), 3: genInt64Slice(3), 2: genInt64Slice(2), 1: genInt64Slice(1)},
220 I64I64SetMapFoo: map[int64][]int64{6: genInt64Slice(6), 5: genInt64Slice(5), 4: genInt64Slice(4), 3: genInt64Slice(3), 2: genInt64Slice(2), 1: genInt64Slice(1)},
221 OptI64I64SetMapFoo: map[int64][]int64{6: genInt64Slice(6), 5: genInt64Slice(5), 4: genInt64Slice(4), 3: genInt64Slice(3), 2: genInt64Slice(2), 1: genInt64Slice(1)},
222 I64I64StringMapMapFoo: map[int64]map[int64]string{6: genInt64StringMap(6), 5: genInt64StringMap(5), 4: genInt64StringMap(4), 3: genInt64StringMap(3), 2: genInt64StringMap(2), 1: genInt64StringMap(1)},
223 OptI64I64StringMapMapFoo: map[int64]map[int64]string{6: genInt64StringMap(6), 5: genInt64StringMap(5), 4: genInt64StringMap(4), 3: genInt64StringMap(3), 2: genInt64StringMap(2), 1: genInt64StringMap(1)},
224 MyStrMyBinMapFoo: map[equalstest.Mystr]equalstest.Mybin{equalstest.Mystr("1"): equalstest.Mybin("1"), equalstest.Mystr("0"): equalstest.Mybin("0")},
225 OptMyStrMyBinMapFoo: map[equalstest.Mystr]equalstest.Mybin{equalstest.Mystr("1"): equalstest.Mybin("1"), equalstest.Mystr("0"): equalstest.Mybin("0")},
226 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)},
227 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)},
228 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},
229 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},
230 }
231}
232
233func genInt64Slice(length int) []int64 {
234 ret := make([]int64, length)
235 for i := 0; i < length; i++ {
236 ret[i] = int64(length - i)
237 }
238 return ret
239}
240
241func genStringSlice(length int) []string {
242 ret := make([]string, length)
243 for i := 0; i < length; i++ {
244 ret[i] = strconv.Itoa(length - i)
245 }
246 return ret
247}
248
249func genBytesSlice(length int) [][]byte {
250 ret := make([][]byte, length)
251 for i := 0; i < length; i++ {
252 ret[i] = []byte(strconv.Itoa(length - i))
253 }
254 return ret
255}
256
257func genInt64StringMap(length int) map[int64]string {
258 ret := make(map[int64]string, length)
259 for i := 0; i < length; i++ {
260 ret[int64(i)] = strconv.Itoa(i)
261 }
262 return ret
263}