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