github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xslice/xslice_test.go (about) 1 package xslice 2 3 import ( 4 "fmt" 5 "github.com/Aoi-hosizora/ahlib/xtesting" 6 "reflect" 7 "strconv" 8 "testing" 9 "time" 10 "unsafe" 11 ) 12 13 // ============================= 14 // testing on internal functions 15 // ============================= 16 17 func TestCheckParam(t *testing.T) { 18 // checkInterfaceSliceParam 19 for _, tc := range []struct { 20 give []interface{} 21 want []interface{} 22 }{ 23 {nil, []interface{}{}}, 24 {[]interface{}{}, []interface{}{}}, 25 {[]interface{}{1}, []interface{}{1}}, 26 {[]interface{}{1, 1, 1}, []interface{}{1, 1, 1}}, 27 {[]interface{}{1, nil, "2", false, 3.3}, []interface{}{1, nil, "2", false, 3.3}}, 28 } { 29 xtesting.Equal(t, checkInterfaceSliceParam(tc.give).(*interfaceItemSlice).origin, tc.want) 30 } 31 32 // checkSliceInterfaceParam 33 for _, tc := range []struct { 34 give interface{} 35 want interface{} 36 wantPanic bool 37 }{ 38 {nil, nil, true}, 39 {0, nil, true}, 40 {[]interface{}(nil), []interface{}{}, false}, 41 {[]int(nil), []int{}, false}, 42 {[]int{}, []int{}, false}, 43 {[]int{1}, []int{1}, false}, 44 {[]int{1, 1, 1}, []int{1, 1, 1}, false}, 45 {[]int{1, 3, 0, 2}, []int{1, 3, 0, 2}, false}, 46 } { 47 if tc.wantPanic { 48 xtesting.Panic(t, func() { checkSliceInterfaceParam(tc.give) }) 49 } else { 50 xtesting.Equal(t, checkSliceInterfaceParam(tc.give).(*interfaceWrappedSlice).val.Interface(), tc.want) 51 } 52 } 53 54 // checkTwoSliceInterfaceParam 55 for _, tc := range []struct { 56 give1 interface{} 57 give2 interface{} 58 want1 interface{} 59 want2 interface{} 60 wantPanic bool 61 }{ 62 {nil, []interface{}{}, nil, nil, true}, 63 {[]interface{}{}, nil, nil, nil, true}, 64 {nil, 0, nil, nil, true}, 65 {0, nil, nil, nil, true}, 66 {0, []int{}, nil, nil, true}, 67 {[]int{}, 0, nil, nil, true}, 68 {[]int{}, []string{}, nil, nil, true}, 69 {[]int(nil), []int(nil), []int{}, []int{}, false}, 70 {[]int{}, []int{}, []int{}, []int{}, false}, 71 {[]int{0}, []int{1}, []int{0}, []int{1}, false}, 72 {[]int{0, 0, 0}, []int{1, 1, 1}, []int{0, 0, 0}, []int{1, 1, 1}, false}, 73 } { 74 if tc.wantPanic { 75 xtesting.Panic(t, func() { checkTwoSliceInterfaceParam(tc.give1, tc.give2) }) 76 } else { 77 s1, s2 := checkTwoSliceInterfaceParam(tc.give1, tc.give2) 78 xtesting.Equal(t, s1.(*interfaceWrappedSlice).val.Interface(), tc.want1) 79 xtesting.Equal(t, s2.(*interfaceWrappedSlice).val.Interface(), tc.want2) 80 } 81 } 82 83 // checkSliceInterfaceAndElemParam 84 for _, tc := range []struct { 85 give1 interface{} 86 give2 interface{} 87 want1 interface{} 88 want2 interface{} 89 wantPanic bool 90 }{ 91 {nil, 0, nil, nil, true}, 92 {0, 0, nil, nil, true}, 93 {[]int{}, "0", nil, nil, true}, 94 {[]int{}, nil, []int{}, 0, false}, 95 {[]int{}, 0, []int{}, 0, false}, 96 {[]int(nil), 0, []int{}, 0, false}, 97 {[]int{0}, 0, []int{0}, 0, false}, 98 {[]int{1, 1, 1}, 1, []int{1, 1, 1}, 1, false}, 99 } { 100 if tc.wantPanic { 101 xtesting.Panic(t, func() { checkSliceInterfaceAndElemParam(tc.give1, tc.give2) }) 102 } else { 103 s, v := checkSliceInterfaceAndElemParam(tc.give1, tc.give2) 104 xtesting.Equal(t, s.(*interfaceWrappedSlice).val.Interface(), tc.want1) 105 xtesting.Equal(t, v, tc.want2) 106 } 107 } 108 } 109 110 func TestInterfaceItemSlice(t *testing.T) { 111 slice := checkInterfaceSliceParam(append(make([]interface{}, 0, 10), 1, 2, 3, 4, 5, 6)) 112 // actual 113 xtesting.Equal(t, slice.actual(), []interface{}{1, 2, 3, 4, 5, 6}) 114 // length 115 xtesting.Equal(t, slice.length(), 6) 116 // capacity 117 xtesting.Equal(t, slice.capacity(), 10) 118 // get 119 xtesting.Equal(t, slice.get(0), 1) 120 xtesting.Equal(t, slice.get(5), 6) 121 xtesting.Panic(t, func() { slice.get(-1) }) 122 xtesting.Panic(t, func() { slice.get(6) }) 123 // slice 124 xtesting.Equal(t, slice.slice(0, 0).actual(), []interface{}{}) 125 xtesting.Equal(t, slice.slice(0, slice.length()).actual(), []interface{}{1, 2, 3, 4, 5, 6}) 126 xtesting.Equal(t, slice.slice(3, slice.capacity()).actual(), []interface{}{4, 5, 6, nil, nil, nil, nil}) 127 xtesting.Panic(t, func() { slice.slice(2, 1) }) 128 xtesting.Panic(t, func() { slice.slice(3, slice.capacity()+1) }) 129 // set 130 slice.set(0, 11) 131 xtesting.Equal(t, slice.get(0), 11) 132 slice.set(5, 66) 133 xtesting.Equal(t, slice.get(5), 66) 134 xtesting.Panic(t, func() { slice.set(-1, 0) }) 135 xtesting.Panic(t, func() { slice.set(6, 0) }) 136 // insert 137 slice.insert(-1, &interfaceItemSlice{[]interface{}{-3, -2, -1}}) 138 xtesting.Equal(t, slice.actual(), []interface{}{-3, -2, -1, 11, 2, 3, 4, 5, 66}) 139 xtesting.Equal(t, slice.capacity(), 10) 140 slice.insert(3, &interfaceItemSlice{[]interface{}{}}) 141 xtesting.Equal(t, slice.actual(), []interface{}{-3, -2, -1, 11, 2, 3, 4, 5, 66}) 142 slice.insert(3, &interfaceItemSlice{[]interface{}{0, 0}}) 143 xtesting.Equal(t, slice.actual(), []interface{}{-3, -2, -1, 0, 0, 11, 2, 3, 4, 5, 66}) 144 xtesting.NotEqual(t, slice.capacity(), 10) 145 slice.insert(1000, &interfaceItemSlice{[]interface{}{7, 8}}) 146 xtesting.Equal(t, slice.actual(), []interface{}{-3, -2, -1, 0, 0, 11, 2, 3, 4, 5, 66, 7, 8}) 147 slice.insert(6, &interfaceItemSlice{[]interface{}{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}) 148 xtesting.Equal(t, slice.actual(), []interface{}{-3, -2, -1, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 66, 7, 8}) 149 xtesting.Panic(t, func() { slice.insert(0, nil) }) 150 xtesting.Panic(t, func() { slice.insert(0, &interfaceWrappedSlice{}) }) 151 152 slice.(*interfaceItemSlice).origin = []interface{}{11, 2, 3, 4, 5, 66} 153 // remove 154 slice.remove(5) 155 xtesting.Equal(t, slice.actual(), []interface{}{11, 2, 3, 4, 5}) 156 slice.remove(0) 157 xtesting.Equal(t, slice.actual(), []interface{}{2, 3, 4, 5}) 158 slice.remove(2) 159 xtesting.Equal(t, slice.actual(), []interface{}{2, 3, 5}) 160 xtesting.Panic(t, func() { slice.remove(-1) }) 161 xtesting.Panic(t, func() { slice.remove(3) }) 162 // append 163 slice.append(7) 164 xtesting.Equal(t, slice.actual(), []interface{}{2, 3, 5, 7}) 165 slice.append(nil) 166 xtesting.Equal(t, slice.actual(), []interface{}{2, 3, 5, 7, nil}) 167 slice.append("0") 168 xtesting.Equal(t, slice.actual(), []interface{}{2, 3, 5, 7, nil, "0"}) 169 } 170 171 func TestInterfaceWrappedSlice(t *testing.T) { 172 slice := checkSliceInterfaceParam(append(make([]int, 0, 10), 1, 2, 3, 4, 5, 6)) 173 // actual 174 xtesting.Equal(t, slice.actual(), []int{1, 2, 3, 4, 5, 6}) 175 // length 176 xtesting.Equal(t, slice.length(), 6) 177 // capacity 178 xtesting.Equal(t, slice.capacity(), 10) 179 // get 180 xtesting.Equal(t, slice.get(0), 1) 181 xtesting.Equal(t, slice.get(5), 6) 182 xtesting.Panic(t, func() { slice.get(-1) }) 183 xtesting.Panic(t, func() { slice.get(6) }) 184 // slice 185 xtesting.Equal(t, slice.slice(0, 0).actual(), []int{}) 186 xtesting.Equal(t, slice.slice(0, slice.length()).actual(), []int{1, 2, 3, 4, 5, 6}) 187 xtesting.Equal(t, slice.slice(3, slice.capacity()).actual(), []int{4, 5, 6, 0, 0, 0, 0}) 188 xtesting.Panic(t, func() { slice.slice(2, 1) }) 189 xtesting.Panic(t, func() { slice.slice(3, slice.capacity()+1) }) 190 // set 191 slice.set(0, nil) 192 xtesting.Equal(t, slice.get(0), 0) 193 slice.set(0, 11) 194 xtesting.Equal(t, slice.get(0), 11) 195 slice.set(5, 66) 196 xtesting.Equal(t, slice.get(5), 66) 197 xtesting.Panic(t, func() { slice.set(-1, 0) }) 198 xtesting.Panic(t, func() { slice.set(6, 0) }) 199 xtesting.Panic(t, func() { slice.set(0, "") }) 200 // insert 201 typ := reflect.TypeOf([]int{}) 202 slice.insert(-1, &interfaceWrappedSlice{reflect.ValueOf([]int{-3, -2, -1}), typ}) 203 xtesting.Equal(t, slice.actual(), []int{-3, -2, -1, 11, 2, 3, 4, 5, 66}) 204 xtesting.Equal(t, slice.capacity(), 10) 205 slice.insert(3, &interfaceWrappedSlice{reflect.ValueOf([]int{}), typ}) 206 xtesting.Equal(t, slice.actual(), []int{-3, -2, -1, 11, 2, 3, 4, 5, 66}) 207 slice.insert(3, &interfaceWrappedSlice{reflect.ValueOf([]int{0, 0}), typ}) 208 xtesting.Equal(t, slice.actual(), []int{-3, -2, -1, 0, 0, 11, 2, 3, 4, 5, 66}) 209 xtesting.NotEqual(t, slice.capacity(), 10) 210 slice.insert(1000, &interfaceWrappedSlice{reflect.ValueOf([]int{7, 8}), typ}) 211 xtesting.Equal(t, slice.actual(), []int{-3, -2, -1, 0, 0, 11, 2, 3, 4, 5, 66, 7, 8}) 212 slice.insert(6, &interfaceWrappedSlice{reflect.ValueOf([]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), typ}) 213 xtesting.Equal(t, slice.actual(), []int{-3, -2, -1, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 66, 7, 8}) 214 xtesting.Panic(t, func() { slice.insert(0, nil) }) 215 xtesting.Panic(t, func() { slice.insert(0, &interfaceItemSlice{}) }) 216 217 slice.(*interfaceWrappedSlice).val = reflect.ValueOf([]int{11, 2, 3, 4, 5, 66}) 218 // remove 219 slice.remove(5) 220 xtesting.Equal(t, slice.actual(), []int{11, 2, 3, 4, 5}) 221 slice.remove(0) 222 xtesting.Equal(t, slice.actual(), []int{2, 3, 4, 5}) 223 slice.remove(2) 224 xtesting.Equal(t, slice.actual(), []int{2, 3, 5}) 225 xtesting.Panic(t, func() { slice.remove(-1) }) 226 xtesting.Panic(t, func() { slice.remove(3) }) 227 // append 228 slice.append(7) 229 xtesting.Equal(t, slice.actual(), []int{2, 3, 5, 7}) 230 slice.append(nil) 231 xtesting.Equal(t, slice.actual(), []int{2, 3, 5, 7, 0}) 232 xtesting.Panic(t, func() { slice.append("0") }) 233 } 234 235 func TestCloneAndMakeSlice(t *testing.T) { 236 // cloneInterfaceSlice 237 for _, tc := range []struct { 238 give []interface{} 239 want []interface{} 240 }{ 241 {nil, []interface{}{}}, 242 {[]interface{}{}, []interface{}{}}, 243 {[]interface{}{1}, []interface{}{1}}, 244 {[]interface{}{1, 1, 1}, []interface{}{1, 1, 1}}, 245 {[]interface{}{1, nil, "2", false, 3.3}, []interface{}{1, nil, "2", false, 3.3}}, 246 } { 247 xtesting.Equal(t, cloneInterfaceSlice(tc.give), tc.want) 248 } 249 250 // cloneSliceInterface 251 for _, tc := range []struct { 252 give interface{} 253 want interface{} 254 wantPanic bool 255 }{ 256 {nil, nil, true}, 257 {0, nil, true}, 258 {[]interface{}(nil), []interface{}{}, false}, 259 {[]int(nil), []int{}, false}, 260 {[]int{}, []int{}, false}, 261 {[]int{1}, []int{1}, false}, 262 {[]int{1, 1, 1}, []int{1, 1, 1}, false}, 263 {[]int{1, 3, 0, 2}, []int{1, 3, 0, 2}, false}, 264 {[]interface{}{1, 3, nil, 2}, []interface{}{1, 3, nil, 2}, false}, 265 } { 266 if tc.wantPanic { 267 xtesting.Panic(t, func() { cloneSliceInterface(tc.give) }) 268 } else { 269 xtesting.Equal(t, cloneSliceInterface(tc.give), tc.want) 270 } 271 } 272 273 // cloneSliceInterfaceFromInterfaceSlice 274 for _, tc := range []struct { 275 give1 []interface{} 276 give2 interface{} 277 want interface{} 278 wantPanic bool 279 }{ 280 {nil, nil, nil, true}, 281 {nil, 0, nil, true}, 282 {nil, []int{}, []int{}, false}, 283 {[]interface{}{nil}, []int{}, []int{0}, false}, 284 {[]interface{}{1, 2}, []int{}, []int{1, 2}, false}, 285 {[]interface{}{uint(1), "2"}, []uint{}, nil, true}, 286 {[]interface{}{uint(1), uint(2)}, []uint32{}, nil, true}, 287 {[]interface{}{uint(1), uint(2)}, []uint{}, []uint{1, 2}, false}, 288 {[]interface{}{nil, nil, "x"}, []string{}, []string{"", "", "x"}, false}, 289 {[]interface{}{nil, 1, "x", false}, []interface{}{}, []interface{}{nil, 1, "x", false}, false}, 290 } { 291 if tc.wantPanic { 292 xtesting.Panic(t, func() { cloneSliceInterfaceFromInterfaceSlice(tc.give1, tc.give2) }) 293 } else { 294 xtesting.Equal(t, cloneSliceInterfaceFromInterfaceSlice(tc.give1, tc.give2), tc.want) 295 } 296 } 297 298 // makeSameTypeInnerSlice 299 for _, tc := range []struct { 300 giveType innerSlice 301 giveLen int 302 giveCap int 303 want interface{} 304 wantPanic bool 305 }{ 306 {nil, 0, 0, nil, true}, 307 {&interfaceItemSlice{}, -1, 0, []interface{}{}, false}, // no panic 308 {&interfaceItemSlice{}, 0, 0, []interface{}{}, false}, 309 {&interfaceItemSlice{}, 1, 1, []interface{}{nil}, false}, 310 {&interfaceItemSlice{}, 3, 0, []interface{}{nil, nil, nil}, false}, 311 {&interfaceWrappedSlice{typ: reflect.TypeOf([]int{})}, -1, 0, []int{}, false}, // no panic 312 {&interfaceWrappedSlice{typ: reflect.TypeOf([]int{})}, 0, 0, []int{}, false}, 313 {&interfaceWrappedSlice{typ: reflect.TypeOf([]int{})}, 1, 1, []int{0}, false}, 314 {&interfaceWrappedSlice{typ: reflect.TypeOf([]int{})}, 3, 1, []int{0, 0, 0}, false}, 315 } { 316 if tc.wantPanic { 317 xtesting.Panic(t, func() { makeSameTypeInnerSlice(tc.giveType, tc.giveLen, tc.giveCap) }) 318 } else { 319 xtesting.Equal(t, makeSameTypeInnerSlice(tc.giveType, tc.giveLen, tc.giveCap).actual(), tc.want) 320 } 321 } 322 323 // makeItemTypeInnerSlice 324 for _, tc := range []struct { 325 giveValue interface{} 326 giveG bool 327 giveLen int 328 giveCap int 329 want interface{} 330 wantPanic bool 331 }{ 332 {nil, true, 0, 0, nil, true}, 333 {0, false, -1, 0, []interface{}{}, false}, // no panic 334 {0, false, 0, 0, []interface{}{}, false}, 335 {0, false, 1, 1, []interface{}{nil}, false}, 336 {0, false, 3, 0, []interface{}{nil, nil, nil}, false}, 337 {struct{}{}, false, -1, 0, []interface{}{}, false}, // no panic 338 {struct{}{}, false, 0, 0, []interface{}{}, false}, 339 {struct{}{}, false, 1, 1, []interface{}{nil}, false}, 340 {struct{}{}, false, 3, 0, []interface{}{nil, nil, nil}, false}, 341 {0, true, -1, 0, []int{}, false}, // no panic 342 {0, true, 0, 0, []int{}, false}, 343 {0, true, 1, 1, []int{0}, false}, 344 {0, true, 3, 0, []int{0, 0, 0}, false}, 345 {struct{}{}, true, -1, 0, []struct{}{}, false}, // no panic 346 {struct{}{}, true, 0, 0, []struct{}{}, false}, 347 {struct{}{}, true, 1, 1, []struct{}{{}}, false}, 348 {struct{}{}, true, 3, 1, []struct{}{{}, {}, {}}, false}, 349 } { 350 if tc.wantPanic { 351 xtesting.Panic(t, func() { makeItemTypeInnerSlice(tc.giveValue, tc.giveLen, tc.giveCap, tc.giveG) }) 352 } else { 353 xtesting.Equal(t, makeItemTypeInnerSlice(tc.giveValue, tc.giveLen, tc.giveCap, tc.giveG).actual(), tc.want) 354 } 355 } 356 357 // cloneInnerSliceItems 358 typ := reflect.TypeOf([]int{}) 359 for _, tc := range []struct { 360 give innerSlice 361 giveExtra int 362 want interface{} 363 wantCap int 364 wantPanic bool 365 }{ 366 {nil, 0, nil, 0, true}, 367 368 {&interfaceItemSlice{[]interface{}{}}, 0, []interface{}{}, 0, false}, 369 {&interfaceItemSlice{[]interface{}{1}}, 1, []interface{}{1}, 2, false}, 370 {&interfaceItemSlice{[]interface{}{1, 1, 1}}, -1, []interface{}{1, 1, 1}, 3, false}, 371 {&interfaceItemSlice{[]interface{}{1, nil, "2", false, 3.3}}, 20, []interface{}{1, nil, "2", false, 3.3}, 25, false}, 372 373 {&interfaceWrappedSlice{reflect.ValueOf([]int{}), typ}, 0, []int{}, 0, false}, 374 {&interfaceWrappedSlice{reflect.ValueOf([]int{1}), typ}, 1, []int{1}, 2, false}, 375 {&interfaceWrappedSlice{reflect.ValueOf([]int{1, 1, 1}), typ}, -1, []int{1, 1, 1}, 3, false}, 376 {&interfaceWrappedSlice{reflect.ValueOf([]int{1, 3, 0, 2}), typ}, 5, []int{1, 3, 0, 2}, 9, false}, 377 } { 378 if tc.wantPanic { 379 xtesting.Panic(t, func() { cloneInnerSliceItems(tc.give, tc.giveExtra) }) 380 } else { 381 ii := cloneInnerSliceItems(tc.give, tc.giveExtra) 382 xtesting.Equal(t, ii.actual(), tc.want) 383 xtesting.Equal(t, ii.capacity(), tc.wantCap) 384 } 385 } 386 } 387 388 // ============================= 389 // testing on exported functions 390 // ============================= 391 392 func TestShuffle(t *testing.T) { 393 for _, tc := range []struct { 394 give []interface{} 395 }{ 396 {[]interface{}{}}, 397 {[]interface{}{1, 2, 3, 4, 5, 6, 7, 8}}, 398 {[]interface{}{"1", 2, 3.0, uint(4)}}, 399 } { 400 me := make([]interface{}, 0, len(tc.give)) 401 for _, item := range tc.give { 402 me = append(me, item) 403 } 404 for i := 0; i < 2; i++ { 405 time.Sleep(2 * time.Nanosecond) 406 result := Shuffle(tc.give) 407 xtesting.Equal(t, tc.give, me) 408 xtesting.ElementMatch(t, result, me) 409 fmt.Println(result) 410 } 411 for i := 0; i < 2; i++ { 412 time.Sleep(2 * time.Nanosecond) 413 ShuffleSelf(tc.give) 414 xtesting.ElementMatch(t, tc.give, me) 415 fmt.Println(tc.give) 416 } 417 } 418 419 for _, tc := range []struct { 420 give []int 421 }{ 422 {[]int{}}, 423 {[]int{1, 2, 3, 4, 5, 6, 7, 8}}, 424 {[]int{1, 2, 3, 4}}, 425 } { 426 me := make([]int, 0, len(tc.give)) 427 for _, item := range tc.give { 428 me = append(me, item) 429 } 430 for i := 0; i < 2; i++ { 431 time.Sleep(2 * time.Nanosecond) 432 result := ShuffleG(tc.give) 433 xtesting.Equal(t, tc.give, me) 434 xtesting.ElementMatch(t, result, me) 435 fmt.Println(result) 436 } 437 for i := 0; i < 2; i++ { 438 time.Sleep(2 * time.Nanosecond) 439 ShuffleSelfG(tc.give) 440 xtesting.ElementMatch(t, tc.give, me) 441 fmt.Println(tc.give) 442 } 443 } 444 } 445 446 func TestReverse(t *testing.T) { 447 for _, tc := range []struct { 448 give []interface{} 449 want []interface{} 450 }{ 451 {[]interface{}{}, []interface{}{}}, 452 {[]interface{}{0}, []interface{}{0}}, 453 {[]interface{}{1, 2, 3}, []interface{}{3, 2, 1}}, 454 } { 455 me := make([]interface{}, 0, len(tc.give)) 456 for _, item := range tc.give { 457 me = append(me, item) 458 } 459 result := Reverse(tc.give) 460 xtesting.Equal(t, tc.give, me) 461 xtesting.Equal(t, result, tc.want) 462 ReverseSelf(tc.give) 463 xtesting.Equal(t, tc.give, tc.want) 464 } 465 466 for _, tc := range []struct { 467 give []int 468 want []int 469 }{ 470 {[]int{}, []int{}}, 471 {[]int{0}, []int{0}}, 472 {[]int{1, 2, 3}, []int{3, 2, 1}}, 473 } { 474 me := make([]int, 0, len(tc.give)) 475 for _, item := range tc.give { 476 me = append(me, item) 477 } 478 result := ReverseG(tc.give) 479 xtesting.Equal(t, tc.give, me) 480 xtesting.Equal(t, result, tc.want) 481 ReverseSelfG(tc.give) 482 xtesting.Equal(t, tc.give, tc.want) 483 } 484 } 485 486 func TestSort(t *testing.T) { 487 le := func(i, j interface{}) bool { return i.(int) < j.(int) } 488 489 for _, tc := range []struct { 490 give []interface{} 491 giveLess Lesser 492 want []interface{} 493 }{ 494 {[]interface{}{}, le, []interface{}{}}, 495 {[]interface{}{0}, le, []interface{}{0}}, 496 {[]interface{}{1, 1, 1}, le, []interface{}{1, 1, 1}}, 497 {[]interface{}{4, 3, 2, 1}, le, []interface{}{1, 2, 3, 4}}, 498 {[]interface{}{8, 1, 6, 8, 1, 2}, le, []interface{}{1, 1, 2, 6, 8, 8}}, 499 } { 500 me := make([]interface{}, 0, len(tc.give)) 501 for _, item := range tc.give { 502 me = append(me, item) 503 } 504 result := Sort(tc.give, tc.giveLess) 505 xtesting.Equal(t, tc.give, me) 506 xtesting.Equal(t, result, tc.want) 507 SortSelf(tc.give, tc.giveLess) 508 xtesting.Equal(t, tc.give, tc.want) 509 } 510 511 for _, tc := range []struct { 512 give []int 513 giveLess Lesser 514 want []int 515 }{ 516 {[]int{}, le, []int{}}, 517 {[]int{0}, le, []int{0}}, 518 {[]int{1, 1, 1}, le, []int{1, 1, 1}}, 519 {[]int{4, 3, 2, 1}, le, []int{1, 2, 3, 4}}, 520 {[]int{8, 1, 6, 8, 1, 2}, le, []int{1, 1, 2, 6, 8, 8}}, 521 } { 522 me := make([]int, 0, len(tc.give)) 523 for _, item := range tc.give { 524 me = append(me, item) 525 } 526 result := SortG(tc.give, tc.giveLess) 527 xtesting.Equal(t, tc.give, me) 528 xtesting.Equal(t, result, tc.want) 529 SortSelfG(tc.give, tc.giveLess) 530 xtesting.Equal(t, tc.give, tc.want) 531 } 532 } 533 534 func TestStableSort(t *testing.T) { 535 type tuple = [2]int 536 new1 := func(v int) tuple { return tuple{v, 0} } 537 new2 := func(v, o int) tuple { return tuple{v, o} } 538 le := func(i, j interface{}) bool { return i.(tuple)[0] < j.(tuple)[0] } 539 540 for _, tc := range []struct { 541 give []interface{} 542 giveLess Lesser 543 want []interface{} 544 }{ 545 {[]interface{}{}, le, []interface{}{}}, 546 {[]interface{}{new1(0)}, le, []interface{}{new1(0)}}, 547 {[]interface{}{new2(1, 3), new2(1, 2), new2(1, 1)}, le, 548 []interface{}{new2(1, 3), new2(1, 2), new2(1, 1)}}, 549 {[]interface{}{new1(4), new1(3), new1(2), new1(1)}, le, 550 []interface{}{new1(1), new1(2), new1(3), new1(4)}}, 551 {[]interface{}{new2(8, 2), new2(1, 2), new1(6), new2(8, 1), new2(1, 1), new1(2)}, le, 552 []interface{}{new2(1, 2), new2(1, 1), new1(2), new1(6), new2(8, 2), new2(8, 1)}}, 553 } { 554 me := make([]interface{}, 0, len(tc.give)) 555 for _, item := range tc.give { 556 me = append(me, item) 557 } 558 result := StableSort(tc.give, tc.giveLess) 559 xtesting.Equal(t, tc.give, me) 560 xtesting.Equal(t, result, tc.want) 561 StableSortSelf(tc.give, tc.giveLess) 562 xtesting.Equal(t, tc.give, tc.want) 563 } 564 565 for _, tc := range []struct { 566 give []tuple 567 giveLess Lesser 568 want []tuple 569 }{ 570 {[]tuple{}, le, []tuple{}}, 571 {[]tuple{new1(0)}, le, []tuple{new1(0)}}, 572 {[]tuple{new2(1, 3), new2(1, 2), new2(1, 1)}, le, 573 []tuple{new2(1, 3), new2(1, 2), new2(1, 1)}}, 574 {[]tuple{new1(4), new1(3), new1(2), new1(1)}, le, 575 []tuple{new1(1), new1(2), new1(3), new1(4)}}, 576 {[]tuple{new2(8, 2), new2(1, 2), new1(6), new2(8, 1), new2(1, 1), new1(2)}, le, 577 []tuple{new2(1, 2), new2(1, 1), new1(2), new1(6), new2(8, 2), new2(8, 1)}}, 578 } { 579 me := make([]tuple, 0, len(tc.give)) 580 for _, item := range tc.give { 581 me = append(me, item) 582 } 583 result := StableSortG(tc.give, tc.giveLess) 584 xtesting.Equal(t, tc.give, me) 585 xtesting.Equal(t, result, tc.want) 586 StableSortSelfG(tc.give, tc.giveLess) 587 xtesting.Equal(t, tc.give, tc.want) 588 } 589 } 590 591 type testStruct struct { 592 value int 593 } 594 595 func (t testStruct) String() string { 596 return strconv.Itoa(t.value) 597 } 598 599 func newTestStruct(value int) testStruct { 600 return testStruct{value: value} 601 } 602 603 func newTestSlice1(s []interface{}) []interface{} { 604 newSlice := make([]interface{}, len(s)) 605 for idx, item := range s { 606 newSlice[idx] = newTestStruct(item.(int)) 607 } 608 return newSlice 609 } 610 611 func newTestSlice2(s []int) []testStruct { 612 newSlice := make([]testStruct, len(s)) 613 for idx, item := range s { 614 newSlice[idx] = newTestStruct(item) 615 } 616 return newSlice 617 } 618 619 func testToItfSlice(s []interface{}) []interface{} { 620 out := make([]interface{}, len(s)) 621 for idx, item := range s { 622 out[idx] = item.(testStruct).value 623 } 624 return out 625 } 626 627 func testToIntSlice(s interface{}) []int { 628 out := make([]int, len(s.([]testStruct))) 629 for idx, item := range s.([]testStruct) { 630 out[idx] = item.value 631 } 632 return out 633 } 634 635 func TestIndexOf(t *testing.T) { 636 s1 := []interface{}{1, 5, 2, 1, 2, 3} 637 s2 := []int{1, 5, 2, 1, 2, 3} 638 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 639 640 for _, tc := range []struct { 641 give []interface{} 642 giveValue int 643 want int 644 }{ 645 {[]interface{}{}, 0, -1}, 646 {s1, -1, -1}, 647 {s1, 0, -1}, 648 {s1, 1, 0}, 649 {s1, 2, 2}, 650 {s1, 3, 5}, 651 {s1, 4, -1}, 652 {s1, 5, 1}, 653 {s1, 6, -1}, 654 } { 655 xtesting.Equal(t, IndexOf(tc.give, tc.giveValue), tc.want) 656 give, giveValue := newTestSlice1(tc.give), newTestStruct(tc.giveValue) 657 xtesting.Equal(t, IndexOfWith(give, giveValue, eq), tc.want) 658 } 659 660 for _, tc := range []struct { 661 give []int 662 giveValue int 663 want int 664 }{ 665 {[]int{}, 0, -1}, 666 {s2, -1, -1}, 667 {s2, 0, -1}, 668 {s2, 1, 0}, 669 {s2, 2, 2}, 670 {s2, 3, 5}, 671 {s2, 4, -1}, 672 {s2, 5, 1}, 673 {s2, 6, -1}, 674 } { 675 xtesting.Equal(t, IndexOfG(tc.give, tc.giveValue), tc.want) 676 give, giveValue := newTestSlice2(tc.give), newTestStruct(tc.giveValue) 677 xtesting.Equal(t, IndexOfWithG(give, giveValue, eq), tc.want) 678 } 679 } 680 681 func TestLastIndexOf(t *testing.T) { 682 s1 := []interface{}{1, 5, 2, 1, 2, 3} 683 s2 := []int{1, 5, 2, 1, 2, 3} 684 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 685 686 for _, tc := range []struct { 687 give []interface{} 688 giveValue int 689 want int 690 }{ 691 {[]interface{}{}, 0, -1}, 692 {s1, -1, -1}, 693 {s1, 0, -1}, 694 {s1, 1, 3}, 695 {s1, 2, 4}, 696 {s1, 3, 5}, 697 {s1, 4, -1}, 698 {s1, 5, 1}, 699 {s1, 6, -1}, 700 } { 701 xtesting.Equal(t, LastIndexOf(tc.give, tc.giveValue), tc.want) 702 give, giveValue := newTestSlice1(tc.give), newTestStruct(tc.giveValue) 703 xtesting.Equal(t, LastIndexOfWith(give, giveValue, eq), tc.want) 704 } 705 706 for _, tc := range []struct { 707 give []int 708 giveValue int 709 want int 710 }{ 711 {[]int{}, 0, -1}, 712 {s2, -1, -1}, 713 {s2, 0, -1}, 714 {s2, 1, 3}, 715 {s2, 2, 4}, 716 {s2, 3, 5}, 717 {s2, 4, -1}, 718 {s2, 5, 1}, 719 {s2, 6, -1}, 720 } { 721 xtesting.Equal(t, LastIndexOfG(tc.give, tc.giveValue), tc.want) 722 give, giveValue := newTestSlice2(tc.give), newTestStruct(tc.giveValue) 723 xtesting.Equal(t, LastIndexOfWithG(give, giveValue, eq), tc.want) 724 } 725 } 726 727 func TestContains(t *testing.T) { 728 s1 := []interface{}{1, 5, 2, 1, 2, 3} 729 s2 := []int{1, 5, 2, 1, 2, 3} 730 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 731 732 for _, tc := range []struct { 733 give []interface{} 734 giveValue int 735 want bool 736 }{ 737 {[]interface{}{}, 0, false}, 738 {s1, -1, false}, 739 {s1, 0, false}, 740 {s1, 1, true}, 741 {s1, 2, true}, 742 {s1, 3, true}, 743 {s1, 4, false}, 744 {s1, 5, true}, 745 {s1, 6, false}, 746 } { 747 xtesting.Equal(t, Contains(tc.give, tc.giveValue), tc.want) 748 give, giveValue := newTestSlice1(tc.give), newTestStruct(tc.giveValue) 749 xtesting.Equal(t, ContainsWith(give, giveValue, eq), tc.want) 750 } 751 752 for _, tc := range []struct { 753 give []int 754 giveValue int 755 want bool 756 }{ 757 {[]int{}, 0, false}, 758 {s2, -1, false}, 759 {s2, 0, false}, 760 {s2, 1, true}, 761 {s2, 2, true}, 762 {s2, 3, true}, 763 {s2, 4, false}, 764 {s2, 5, true}, 765 {s2, 6, false}, 766 } { 767 xtesting.Equal(t, ContainsG(tc.give, tc.giveValue), tc.want) 768 give, giveValue := newTestSlice2(tc.give), newTestStruct(tc.giveValue) 769 xtesting.Equal(t, ContainsWithG(give, giveValue, eq), tc.want) 770 } 771 } 772 773 func TestCount(t *testing.T) { 774 s1 := []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2} 775 s2 := []int{1, 5, 2, 1, 5, 2, 6, 3, 2} 776 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 777 778 for _, tc := range []struct { 779 give []interface{} 780 giveValue int 781 want int 782 }{ 783 {[]interface{}{}, 0, 0}, 784 {s1, -1, 0}, 785 {s1, 0, 0}, 786 {s1, 1, 2}, 787 {s1, 2, 3}, 788 {s1, 3, 1}, 789 {s1, 4, 0}, 790 {s1, 5, 2}, 791 {s1, 6, 1}, 792 {s1, 7, 0}, 793 } { 794 xtesting.Equal(t, Count(tc.give, tc.giveValue), tc.want) 795 give, giveValue := newTestSlice1(tc.give), newTestStruct(tc.giveValue) 796 xtesting.Equal(t, CountWith(give, giveValue, eq), tc.want) 797 } 798 799 for _, tc := range []struct { 800 give []int 801 giveValue int 802 want int 803 }{ 804 {[]int{}, 0, 0}, 805 {s2, -1, 0}, 806 {s2, 0, 0}, 807 {s2, 1, 2}, 808 {s2, 2, 3}, 809 {s2, 3, 1}, 810 {s2, 4, 0}, 811 {s2, 5, 2}, 812 {s2, 6, 1}, 813 {s2, 7, 0}, 814 } { 815 xtesting.Equal(t, CountG(tc.give, tc.giveValue), tc.want) 816 give, giveValue := newTestSlice2(tc.give), newTestStruct(tc.giveValue) 817 xtesting.Equal(t, CountWithG(give, giveValue, eq), tc.want) 818 } 819 } 820 821 func TestInsert(t *testing.T) { 822 for _, tc := range []struct { 823 give []interface{} 824 giveValues []interface{} 825 giveIndex int 826 want []interface{} 827 }{ 828 {[]interface{}{}, []interface{}{}, -2, []interface{}{}}, 829 {[]interface{}{}, []interface{}{1, 2}, -1, []interface{}{1, 2}}, 830 {[]interface{}{}, []interface{}{0, 0, 0}, 0, []interface{}{0, 0, 0}}, 831 {[]interface{}{}, []interface{}{3}, 1, []interface{}{3}}, 832 {[]interface{}{1}, []interface{}{9}, -1, []interface{}{9, 1}}, 833 {[]interface{}{1}, []interface{}{9, 9, 9}, 0, []interface{}{9, 9, 9, 1}}, 834 {[]interface{}{1}, []interface{}{}, 1, []interface{}{1}}, 835 {[]interface{}{1}, []interface{}{0, 9}, 2, []interface{}{1, 0, 9}}, 836 {[]interface{}{1, 2}, []interface{}{-1}, -1, []interface{}{-1, 1, 2}}, 837 {[]interface{}{1, 2}, []interface{}{9, 9}, 0, []interface{}{9, 9, 1, 2}}, 838 {[]interface{}{1, 2}, []interface{}{3, 2, 1}, 1, []interface{}{1, 3, 2, 1, 2}}, 839 {[]interface{}{1, 2}, []interface{}{9, 9, 9}, 2, []interface{}{1, 2, 9, 9, 9}}, 840 {[]interface{}{1, 2, 3}, []interface{}{nil}, -1, []interface{}{nil, 1, 2, 3}}, 841 {[]interface{}{1, 2, 3}, []interface{}{"9", "8", "7"}, 0, []interface{}{"9", "8", "7", 1, 2, 3}}, 842 {[]interface{}{1, 2, 3}, []interface{}{}, 1, []interface{}{1, 2, 3}}, 843 {[]interface{}{1, 2, 3}, []interface{}{-2, -1}, 2, []interface{}{1, 2, -2, -1, 3}}, 844 {[]interface{}{1, 2, 3}, []interface{}{0, 9999, 999, 99, 9}, 4, []interface{}{1, 2, 3, 0, 9999, 999, 99, 9}}, 845 } { 846 xtesting.Equal(t, Insert(tc.give, tc.giveIndex, tc.giveValues...), tc.want) 847 xtesting.Equal(t, InsertSelf(tc.give, tc.giveIndex, tc.giveValues...), tc.want) 848 } 849 850 for _, tc := range []struct { 851 give []int 852 giveValues []interface{} // <<< 853 giveIndex int 854 want []int 855 }{ 856 {[]int{}, []interface{}{}, -2, []int{}}, 857 {[]int{}, []interface{}{1, 2}, -1, []int{1, 2}}, 858 {[]int{}, []interface{}{0, 0, 0}, 0, []int{0, 0, 0}}, 859 {[]int{}, []interface{}{3}, 1, []int{3}}, 860 {[]int{1}, []interface{}{9}, -1, []int{9, 1}}, 861 {[]int{1}, []interface{}{9, 9, 9}, 0, []int{9, 9, 9, 1}}, 862 {[]int{1}, []interface{}{}, 1, []int{1}}, 863 {[]int{1}, []interface{}{0, 9}, 2, []int{1, 0, 9}}, 864 {[]int{1, 2}, []interface{}{-1}, -1, []int{-1, 1, 2}}, 865 {[]int{1, 2}, []interface{}{9, 9}, 0, []int{9, 9, 1, 2}}, 866 {[]int{1, 2}, []interface{}{3, 2, 1}, 1, []int{1, 3, 2, 1, 2}}, 867 {[]int{1, 2}, []interface{}{9, 9, 9}, 2, []int{1, 2, 9, 9, 9}}, 868 {[]int{1, 2, 3}, []interface{}{-9}, -1, []int{-9, 1, 2, 3}}, 869 {[]int{1, 2, 3}, []interface{}{9, 8, 7}, 0, []int{9, 8, 7, 1, 2, 3}}, 870 {[]int{1, 2, 3}, []interface{}{}, 1, []int{1, 2, 3}}, 871 {[]int{1, 2, 3}, []interface{}{-2, -1}, 2, []int{1, 2, -2, -1, 3}}, 872 {[]int{1, 2, 3}, []interface{}{0, 9999, 999, 99, 9}, 4, []int{1, 2, 3, 0, 9999, 999, 99, 9}}, 873 } { 874 xtesting.Equal(t, InsertG(tc.give, tc.giveIndex, tc.giveValues...), tc.want) 875 xtesting.Equal(t, InsertSelfG(tc.give, tc.giveIndex, tc.giveValues...), tc.want) 876 } 877 878 xtesting.NotPanic(t, func() { InsertG([]int{}, 0, nil) }) 879 xtesting.Panic(t, func() { InsertG([]int{}, 0, []uint{}) }) 880 xtesting.Panic(t, func() { InsertG([]int{}, 0, []interface{}{1, 2, 3}) }) 881 882 give1 := append(make([]interface{}, 0, 6), 1, 2, 3) 883 addr1 := (*reflect.SliceHeader)(unsafe.Pointer(&give1)).Data 884 give1_ := Insert(give1, 0) 885 xtesting.NotEqual(t, addr1, (*reflect.SliceHeader)(unsafe.Pointer(&give1_)).Data) 886 give1 = InsertSelf(give1, 1, 4, 5) 887 xtesting.Equal(t, cap(give1), 6) 888 xtesting.Equal(t, addr1, (*reflect.SliceHeader)(unsafe.Pointer(&give1)).Data) 889 give1 = InsertSelf(give1, 0, 6, 7, 8) 890 xtesting.NotEqual(t, cap(give1), 6) 891 xtesting.NotEqual(t, addr1, (*reflect.SliceHeader)(unsafe.Pointer(&give1)).Data) 892 893 give2 := append(make([]int, 0, 6), 1, 2, 3) 894 addr2 := (*reflect.SliceHeader)(unsafe.Pointer(&give2)).Data 895 addr2_ := InsertG(give2, 0) 896 xtesting.NotEqual(t, addr2, (*reflect.SliceHeader)(unsafe.Pointer(&addr2_)).Data) 897 give2 = InsertSelfG(give2, 1, 4, 5).([]int) 898 xtesting.Equal(t, cap(give2), 6) 899 xtesting.Equal(t, addr2, (*reflect.SliceHeader)(unsafe.Pointer(&give2)).Data) 900 give2 = InsertSelfG(give2, 0, 4, 5).([]int) 901 xtesting.NotEqual(t, cap(give2), 6) 902 xtesting.NotEqual(t, addr2, (*reflect.SliceHeader)(unsafe.Pointer(&give2)).Data) 903 } 904 905 func TestDelete(t *testing.T) { 906 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 907 908 for _, tc := range []struct { 909 give []interface{} 910 giveValue int 911 giveN int 912 want []interface{} 913 }{ 914 {[]interface{}{}, 0, 1, []interface{}{}}, 915 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, -1, 1, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 916 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 0, 1, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 917 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 1, 1, []interface{}{5, 2, 1, 5, 2, 6, 3, 2}}, 918 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 1, 2, []interface{}{5, 2, 5, 2, 6, 3, 2}}, 919 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 2, 1, []interface{}{1, 5, 1, 5, 2, 6, 3, 2}}, 920 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 2, 2, []interface{}{1, 5, 1, 5, 6, 3, 2}}, 921 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 2, -1, []interface{}{1, 5, 1, 5, 6, 3}}, 922 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 3, 1, []interface{}{1, 5, 2, 1, 5, 2, 6, 2}}, 923 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 4, 1, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 924 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 5, 1, []interface{}{1, 2, 1, 5, 2, 6, 3, 2}}, 925 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 6, 1, []interface{}{1, 5, 2, 1, 5, 2, 3, 2}}, 926 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 7, 1, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 927 } { 928 xtesting.Equal(t, Delete(tc.give, tc.giveValue, tc.giveN), tc.want) 929 give, giveValue := newTestSlice1(tc.give), newTestStruct(tc.giveValue) 930 d1 := DeleteSelf(tc.give, tc.giveValue, tc.giveN) 931 xtesting.Equal(t, d1, tc.want) 932 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d1)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&tc.give)).Data) 933 xtesting.Equal(t, testToItfSlice(DeleteWith(give, giveValue, tc.giveN, eq)), tc.want) 934 d2 := DeleteSelfWith(give, giveValue, tc.giveN, eq) 935 xtesting.Equal(t, testToItfSlice(d2), tc.want) 936 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d2)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&give)).Data) 937 } 938 939 for _, tc := range []struct { 940 give []int 941 giveValue int 942 giveN int 943 want []int 944 }{ 945 {[]int{}, 0, 1, []int{}}, 946 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, -1, 1, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 947 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 0, 1, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 948 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 1, 1, []int{5, 2, 1, 5, 2, 6, 3, 2}}, 949 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 1, 2, []int{5, 2, 5, 2, 6, 3, 2}}, 950 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 2, 1, []int{1, 5, 1, 5, 2, 6, 3, 2}}, 951 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 2, 2, []int{1, 5, 1, 5, 6, 3, 2}}, 952 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 2, -1, []int{1, 5, 1, 5, 6, 3}}, 953 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 3, 1, []int{1, 5, 2, 1, 5, 2, 6, 2}}, 954 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 4, 1, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 955 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 5, 1, []int{1, 2, 1, 5, 2, 6, 3, 2}}, 956 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 6, 1, []int{1, 5, 2, 1, 5, 2, 3, 2}}, 957 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 7, 1, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 958 } { 959 xtesting.Equal(t, DeleteG(tc.give, tc.giveValue, tc.giveN), tc.want) 960 give, giveValue := newTestSlice2(tc.give), newTestStruct(tc.giveValue) 961 d1 := DeleteSelfG(tc.give, tc.giveValue, tc.giveN).([]int) 962 xtesting.Equal(t, d1, tc.want) 963 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d1)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&tc.give)).Data) 964 xtesting.Equal(t, testToIntSlice(DeleteWithG(give, giveValue, tc.giveN, eq)), tc.want) 965 d2 := DeleteSelfWithG(give, giveValue, tc.giveN, eq).([]testStruct) 966 xtesting.Equal(t, testToIntSlice(d2), tc.want) 967 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d2)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&give)).Data) 968 } 969 } 970 971 func TestDeleteAll(t *testing.T) { 972 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 973 974 for _, tc := range []struct { 975 give []interface{} 976 giveValue int 977 want []interface{} 978 }{ 979 {[]interface{}{}, 0, []interface{}{}}, 980 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, -1, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 981 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 0, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 982 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 1, []interface{}{5, 2, 5, 2, 6, 3, 2}}, 983 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 2, []interface{}{1, 5, 1, 5, 6, 3}}, 984 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 3, []interface{}{1, 5, 2, 1, 5, 2, 6, 2}}, 985 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 4, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 986 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 5, []interface{}{1, 2, 1, 2, 6, 3, 2}}, 987 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 6, []interface{}{1, 5, 2, 1, 5, 2, 3, 2}}, 988 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, 7, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 989 } { 990 xtesting.Equal(t, DeleteAll(tc.give, tc.giveValue), tc.want) 991 give, giveValue := newTestSlice1(tc.give), newTestStruct(tc.giveValue) 992 d1 := DeleteAllSelf(tc.give, tc.giveValue) 993 xtesting.Equal(t, d1, tc.want) 994 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d1)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&tc.give)).Data) 995 xtesting.Equal(t, testToItfSlice(DeleteAllWith(give, giveValue, eq)), tc.want) 996 d2 := DeleteAllSelfWith(give, giveValue, eq) 997 xtesting.Equal(t, testToItfSlice(d2), tc.want) 998 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d2)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&give)).Data) 999 } 1000 1001 for _, tc := range []struct { 1002 give []int 1003 giveValue int 1004 want []int 1005 }{ 1006 {[]int{}, 0, []int{}}, 1007 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, -1, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 1008 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 0, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 1009 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 1, []int{5, 2, 5, 2, 6, 3, 2}}, 1010 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 2, []int{1, 5, 1, 5, 6, 3}}, 1011 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 3, []int{1, 5, 2, 1, 5, 2, 6, 2}}, 1012 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 4, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 1013 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 5, []int{1, 2, 1, 2, 6, 3, 2}}, 1014 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 6, []int{1, 5, 2, 1, 5, 2, 3, 2}}, 1015 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, 7, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 1016 } { 1017 xtesting.Equal(t, DeleteAllG(tc.give, tc.giveValue), tc.want) 1018 give, giveValue := newTestSlice2(tc.give), newTestStruct(tc.giveValue) 1019 d1 := DeleteAllSelfG(tc.give, tc.giveValue).([]int) 1020 xtesting.Equal(t, d1, tc.want) 1021 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d1)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&tc.give)).Data) 1022 xtesting.Equal(t, testToIntSlice(DeleteAllWithG(give, giveValue, eq)), tc.want) 1023 d2 := DeleteAllSelfWithG(give, giveValue, eq).([]testStruct) 1024 xtesting.Equal(t, testToIntSlice(d2), tc.want) 1025 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d2)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&give)).Data) 1026 } 1027 } 1028 1029 func TestContainsAll(t *testing.T) { 1030 s1 := []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2} 1031 s2 := []int{1, 5, 2, 1, 5, 2, 6, 3, 2} 1032 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 1033 1034 for _, tc := range []struct { 1035 give1 []interface{} 1036 give2 []interface{} 1037 want bool 1038 }{ 1039 {[]interface{}{}, []interface{}{}, true}, 1040 {[]interface{}{}, []interface{}{1, 1, 1}, false}, 1041 {s1, []interface{}{}, true}, 1042 {s1, []interface{}{1}, true}, 1043 {s1, []interface{}{1, 0}, false}, 1044 {s1, []interface{}{5, 2, 1}, true}, 1045 {s1, []interface{}{5, 5, 5, 5}, true}, 1046 {s1, []interface{}{2, 2, 2, 1, 5, 2, 1, 5, 2, 6, 3, 2}, true}, 1047 {s1, []interface{}{1, 2, 3, 4, 5, 6}, false}, 1048 } { 1049 xtesting.Equal(t, ContainsAll(tc.give1, tc.give2), tc.want) 1050 give1, give2 := newTestSlice1(tc.give1), newTestSlice1(tc.give2) 1051 xtesting.Equal(t, ContainsAllWith(give1, give2, eq), tc.want) 1052 } 1053 1054 for _, tc := range []struct { 1055 give1 []int 1056 give2 []int 1057 want bool 1058 }{ 1059 {[]int{}, []int{}, true}, 1060 {[]int{}, []int{1, 1, 1}, false}, 1061 {s2, []int{}, true}, 1062 {s2, []int{1}, true}, 1063 {s2, []int{1, 0}, false}, 1064 {s2, []int{5, 2, 1}, true}, 1065 {s2, []int{5, 5, 5, 5}, true}, 1066 {s2, []int{2, 2, 2, 1, 5, 2, 1, 5, 2, 6, 3, 2}, true}, 1067 {s2, []int{1, 2, 3, 4, 5, 6}, false}, 1068 } { 1069 xtesting.Equal(t, ContainsAllG(tc.give1, tc.give2), tc.want) 1070 give1, give2 := newTestSlice2(tc.give1), newTestSlice2(tc.give2) 1071 xtesting.Equal(t, ContainsAllWithG(give1, give2, eq), tc.want) 1072 } 1073 } 1074 1075 func TestDiff(t *testing.T) { 1076 s1 := []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2} 1077 s2 := []int{1, 5, 2, 1, 5, 2, 6, 3, 2} 1078 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 1079 1080 for _, tc := range []struct { 1081 give1 []interface{} 1082 give2 []interface{} 1083 want []interface{} 1084 }{ 1085 {[]interface{}{}, []interface{}{}, []interface{}{}}, 1086 {s1, []interface{}{}, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 1087 {s1, []interface{}{1}, []interface{}{5, 2, 5, 2, 6, 3, 2}}, 1088 {s1, []interface{}{1, 2}, []interface{}{5, 5, 6, 3}}, 1089 {s1, []interface{}{1, 2, 3}, []interface{}{5, 5, 6}}, 1090 {s1, []interface{}{1, 2, 3, 4}, []interface{}{5, 5, 6}}, 1091 {s1, []interface{}{1, 2, 3, 4, 5}, []interface{}{6}}, 1092 {s1, []interface{}{1, 2, 3, 4, 5, 6}, []interface{}{}}, 1093 {s1, []interface{}{6}, []interface{}{1, 5, 2, 1, 5, 2, 3, 2}}, 1094 {s1, []interface{}{6, 5}, []interface{}{1, 2, 1, 2, 3, 2}}, 1095 {s1, []interface{}{6, 5, 4}, []interface{}{1, 2, 1, 2, 3, 2}}, 1096 {s1, []interface{}{6, 5, 4, 3}, []interface{}{1, 2, 1, 2, 2}}, 1097 {s1, []interface{}{6, 5, 4, 3, 2}, []interface{}{1, 1}}, 1098 {s1, []interface{}{6, 5, 4, 3, 2, 1}, []interface{}{}}, 1099 } { 1100 xtesting.Equal(t, Diff(tc.give1, tc.give2), tc.want) 1101 give1, give2 := newTestSlice1(tc.give1), newTestSlice1(tc.give2) 1102 xtesting.Equal(t, testToItfSlice(DiffWith(give1, give2, eq)), tc.want) 1103 } 1104 1105 for _, tc := range []struct { 1106 give1 []int 1107 give2 []int 1108 want []int 1109 }{ 1110 {[]int{}, []int{}, []int{}}, 1111 {s2, []int{}, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 1112 {s2, []int{1}, []int{5, 2, 5, 2, 6, 3, 2}}, 1113 {s2, []int{1, 2}, []int{5, 5, 6, 3}}, 1114 {s2, []int{1, 2, 3}, []int{5, 5, 6}}, 1115 {s2, []int{1, 2, 3, 4}, []int{5, 5, 6}}, 1116 {s2, []int{1, 2, 3, 4, 5}, []int{6}}, 1117 {s2, []int{1, 2, 3, 4, 5, 6}, []int{}}, 1118 {s2, []int{6}, []int{1, 5, 2, 1, 5, 2, 3, 2}}, 1119 {s2, []int{6, 5}, []int{1, 2, 1, 2, 3, 2}}, 1120 {s2, []int{6, 5, 4}, []int{1, 2, 1, 2, 3, 2}}, 1121 {s2, []int{6, 5, 4, 3}, []int{1, 2, 1, 2, 2}}, 1122 {s2, []int{6, 5, 4, 3, 2}, []int{1, 1}}, 1123 {s2, []int{6, 5, 4, 3, 2, 1}, []int{}}, 1124 } { 1125 xtesting.Equal(t, DiffG(tc.give1, tc.give2), tc.want) 1126 give1, give2 := newTestSlice2(tc.give1), newTestSlice2(tc.give2) 1127 xtesting.Equal(t, testToIntSlice(DiffWithG(give1, give2, eq)), tc.want) 1128 } 1129 } 1130 1131 func TestUnion(t *testing.T) { 1132 s1 := []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2} 1133 s2 := []int{1, 5, 2, 1, 5, 2, 6, 3, 2} 1134 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 1135 1136 for _, tc := range []struct { 1137 give1 []interface{} 1138 give2 []interface{} 1139 want []interface{} 1140 }{ 1141 {[]interface{}{}, []interface{}{}, []interface{}{}}, 1142 {s1, []interface{}{}, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 1143 {s1, []interface{}{11}, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2, 11}}, 1144 {s1, []interface{}{11, 2}, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2, 11}}, 1145 {s1, []interface{}{11, 2, 13}, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2, 11, 13}}, 1146 {s1, []interface{}{11, 2, 13, 14}, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2, 11, 13, 14}}, 1147 {s1, []interface{}{11, 2, 13, 14, 5}, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2, 11, 13, 14}}, 1148 {s1, []interface{}{11, 2, 13, 14, 5, 16}, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2, 11, 13, 14, 16}}, 1149 } { 1150 xtesting.Equal(t, Union(tc.give1, tc.give2), tc.want) 1151 give1, give2 := newTestSlice1(tc.give1), newTestSlice1(tc.give2) 1152 xtesting.Equal(t, testToItfSlice(UnionWith(give1, give2, eq)), tc.want) 1153 } 1154 1155 for _, tc := range []struct { 1156 give1 []int 1157 give2 []int 1158 want []int 1159 }{ 1160 {[]int{}, []int{}, []int{}}, 1161 {s2, []int{}, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 1162 {s2, []int{11}, []int{1, 5, 2, 1, 5, 2, 6, 3, 2, 11}}, 1163 {s2, []int{11, 2}, []int{1, 5, 2, 1, 5, 2, 6, 3, 2, 11}}, 1164 {s2, []int{11, 2, 13}, []int{1, 5, 2, 1, 5, 2, 6, 3, 2, 11, 13}}, 1165 {s2, []int{11, 2, 13, 14}, []int{1, 5, 2, 1, 5, 2, 6, 3, 2, 11, 13, 14}}, 1166 {s2, []int{11, 2, 13, 14, 5}, []int{1, 5, 2, 1, 5, 2, 6, 3, 2, 11, 13, 14}}, 1167 {s2, []int{11, 2, 13, 14, 5, 16}, []int{1, 5, 2, 1, 5, 2, 6, 3, 2, 11, 13, 14, 16}}, 1168 } { 1169 xtesting.Equal(t, UnionG(tc.give1, tc.give2), tc.want) 1170 give1, give2 := newTestSlice2(tc.give1), newTestSlice2(tc.give2) 1171 xtesting.Equal(t, testToIntSlice(UnionWithG(give1, give2, eq)), tc.want) 1172 } 1173 } 1174 1175 func TestIntersect(t *testing.T) { 1176 s1 := []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2} 1177 s2 := []int{1, 5, 2, 1, 5, 2, 6, 3, 2} 1178 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 1179 1180 for _, tc := range []struct { 1181 give1 []interface{} 1182 give2 []interface{} 1183 want []interface{} 1184 }{ 1185 {[]interface{}{}, []interface{}{}, []interface{}{}}, 1186 {s1, []interface{}{}, []interface{}{}}, 1187 {s1, []interface{}{1}, []interface{}{1, 1}}, 1188 {s1, []interface{}{1, 2}, []interface{}{1, 2, 1, 2, 2}}, 1189 {s1, []interface{}{1, 2, 3}, []interface{}{1, 2, 1, 2, 3, 2}}, 1190 {s1, []interface{}{1, 2, 3, 4}, []interface{}{1, 2, 1, 2, 3, 2}}, 1191 {s1, []interface{}{1, 2, 3, 4, 5}, []interface{}{1, 5, 2, 1, 5, 2, 3, 2}}, 1192 {s1, []interface{}{1, 2, 3, 4, 5, 6}, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 1193 } { 1194 xtesting.Equal(t, Intersect(tc.give1, tc.give2), tc.want) 1195 give1, give2 := newTestSlice1(tc.give1), newTestSlice1(tc.give2) 1196 xtesting.Equal(t, testToItfSlice(IntersectWith(give1, give2, eq)), tc.want) 1197 } 1198 1199 for _, tc := range []struct { 1200 give1 []int 1201 give2 []int 1202 want []int 1203 }{ 1204 {[]int{}, []int{}, []int{}}, 1205 {s2, []int{}, []int{}}, 1206 {s2, []int{1}, []int{1, 1}}, 1207 {s2, []int{1, 2}, []int{1, 2, 1, 2, 2}}, 1208 {s2, []int{1, 2, 3}, []int{1, 2, 1, 2, 3, 2}}, 1209 {s2, []int{1, 2, 3, 4}, []int{1, 2, 1, 2, 3, 2}}, 1210 {s2, []int{1, 2, 3, 4, 5}, []int{1, 5, 2, 1, 5, 2, 3, 2}}, 1211 {s2, []int{1, 2, 3, 4, 5, 6}, []int{1, 5, 2, 1, 5, 2, 6, 3, 2}}, 1212 } { 1213 xtesting.Equal(t, IntersectG(tc.give1, tc.give2), tc.want) 1214 give1, give2 := newTestSlice2(tc.give1), newTestSlice2(tc.give2) 1215 xtesting.Equal(t, testToIntSlice(IntersectWithG(give1, give2, eq)), tc.want) 1216 } 1217 } 1218 1219 func TestDeduplicate(t *testing.T) { 1220 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 1221 1222 for _, tc := range []struct { 1223 give []interface{} 1224 want []interface{} 1225 }{ 1226 {[]interface{}{}, []interface{}{}}, 1227 {[]interface{}{1}, []interface{}{1}}, 1228 {[]interface{}{1, 1, 1}, []interface{}{1}}, 1229 {[]interface{}{1, 1, 2, 1}, []interface{}{1, 2}}, 1230 {[]interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2}, []interface{}{1, 5, 2, 6, 3}}, 1231 } { 1232 xtesting.Equal(t, Deduplicate(tc.give), tc.want) 1233 give := newTestSlice1(tc.give) 1234 d1 := DeduplicateSelf(tc.give) 1235 xtesting.Equal(t, d1, tc.want) 1236 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d1)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&tc.give)).Data) 1237 xtesting.Equal(t, testToItfSlice(DeduplicateWith(give, eq)), tc.want) 1238 d2 := DeduplicateSelfWith(give, eq) 1239 xtesting.Equal(t, testToItfSlice(d2), tc.want) 1240 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d2)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&give)).Data) 1241 } 1242 1243 for _, tc := range []struct { 1244 give []int 1245 want []int 1246 }{ 1247 {[]int{}, []int{}}, 1248 {[]int{1}, []int{1}}, 1249 {[]int{1, 1, 1}, []int{1}}, 1250 {[]int{1, 1, 2, 1}, []int{1, 2}}, 1251 {[]int{1, 5, 2, 1, 5, 2, 6, 3, 2}, []int{1, 5, 2, 6, 3}}, 1252 } { 1253 xtesting.Equal(t, DeduplicateG(tc.give), tc.want) 1254 give := newTestSlice2(tc.give) 1255 d1 := DeduplicateSelfG(tc.give).([]int) 1256 xtesting.Equal(t, d1, tc.want) 1257 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d1)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&tc.give)).Data) 1258 xtesting.Equal(t, testToIntSlice(DeduplicateWithG(give, eq)), tc.want) 1259 d2 := DeduplicateSelfWithG(give, eq).([]testStruct) 1260 xtesting.Equal(t, testToIntSlice(d2), tc.want) 1261 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d2)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&give)).Data) 1262 } 1263 } 1264 1265 func TestCompact(t *testing.T) { 1266 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 1267 1268 for _, tc := range []struct { 1269 give []interface{} 1270 want []interface{} 1271 }{ 1272 {[]interface{}{}, []interface{}{}}, 1273 {[]interface{}{1}, []interface{}{1}}, 1274 {[]interface{}{1, 1, 1}, []interface{}{1}}, 1275 {[]interface{}{2, 2, 1, 1, 1, 2, 1, 3}, []interface{}{2, 1, 2, 1, 3}}, 1276 {[]interface{}{1, 5, 5, 2, 1, 5, 2, 2, 6, 6, 6, 3, 2, 1, 1}, []interface{}{1, 5, 2, 1, 5, 2, 6, 3, 2, 1}}, 1277 } { 1278 xtesting.Equal(t, Compact(tc.give), tc.want) 1279 give := newTestSlice1(tc.give) 1280 d1 := CompactSelf(tc.give) 1281 xtesting.Equal(t, d1, tc.want) 1282 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d1)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&tc.give)).Data) 1283 xtesting.Equal(t, testToItfSlice(CompactWith(give, eq)), tc.want) 1284 d2 := CompactSelfWith(give, eq) 1285 xtesting.Equal(t, testToItfSlice(d2), tc.want) 1286 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d2)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&give)).Data) 1287 } 1288 1289 for _, tc := range []struct { 1290 give []int 1291 want []int 1292 }{ 1293 {[]int{}, []int{}}, 1294 {[]int{1}, []int{1}}, 1295 {[]int{1, 1, 1}, []int{1}}, 1296 {[]int{2, 2, 1, 1, 1, 2, 1, 3}, []int{2, 1, 2, 1, 3}}, 1297 {[]int{1, 5, 5, 2, 1, 5, 2, 2, 6, 6, 6, 3, 2, 1, 1}, []int{1, 5, 2, 1, 5, 2, 6, 3, 2, 1}}, 1298 } { 1299 xtesting.Equal(t, CompactG(tc.give), tc.want) 1300 give := newTestSlice2(tc.give) 1301 d1 := CompactSelfG(tc.give).([]int) 1302 xtesting.Equal(t, d1, tc.want) 1303 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d1)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&tc.give)).Data) 1304 xtesting.Equal(t, testToIntSlice(CompactWithG(give, eq)), tc.want) 1305 d2 := CompactSelfWithG(give, eq).([]testStruct) 1306 xtesting.Equal(t, testToIntSlice(d2), tc.want) 1307 xtesting.Equal(t, (*reflect.SliceHeader)(unsafe.Pointer(&d2)).Data, (*reflect.SliceHeader)(unsafe.Pointer(&give)).Data) 1308 } 1309 } 1310 1311 func TestEqual(t *testing.T) { 1312 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 1313 1314 for _, tc := range []struct { 1315 give1 []interface{} 1316 give2 []interface{} 1317 want bool 1318 }{ 1319 {[]interface{}{}, []interface{}{}, true}, 1320 {[]interface{}{0}, []interface{}{}, false}, 1321 {[]interface{}{}, []interface{}{0}, false}, 1322 {[]interface{}{0}, []interface{}{0}, true}, 1323 {[]interface{}{1, 1, 1}, []interface{}{1}, false}, 1324 {[]interface{}{1}, []interface{}{1, 1, 1}, false}, 1325 {[]interface{}{1, 1, 1}, []interface{}{1, 1, 1}, true}, 1326 {[]interface{}{1, 2, 1}, []interface{}{1, 1, 2}, false}, 1327 {[]interface{}{1, 1, 2, 3}, []interface{}{1, 2, 3, 1}, false}, 1328 {[]interface{}{1, 1, 2, 2}, []interface{}{1, 1, 2}, false}, 1329 {[]interface{}{1, 1, 2, 3}, []interface{}{1, 1, 2, 3}, true}, 1330 } { 1331 xtesting.Equal(t, Equal(tc.give1, tc.give2), tc.want) 1332 give1, give2 := newTestSlice1(tc.give1), newTestSlice1(tc.give2) 1333 xtesting.Equal(t, EqualWith(give1, give2, eq), tc.want) 1334 } 1335 1336 for _, tc := range []struct { 1337 give1 []int 1338 give2 []int 1339 want bool 1340 }{ 1341 {[]int{}, []int{}, true}, 1342 {[]int{0}, []int{}, false}, 1343 {[]int{}, []int{0}, false}, 1344 {[]int{0}, []int{0}, true}, 1345 {[]int{1, 1, 1}, []int{1}, false}, 1346 {[]int{1}, []int{1, 1, 1}, false}, 1347 {[]int{1, 1, 1}, []int{1, 1, 1}, true}, 1348 {[]int{1, 2, 1}, []int{1, 1, 2}, false}, 1349 {[]int{1, 1, 2, 3}, []int{1, 2, 3, 1}, false}, 1350 {[]int{1, 1, 2, 2}, []int{1, 1, 2}, false}, 1351 {[]int{1, 1, 2, 3}, []int{1, 1, 2, 3}, true}, 1352 } { 1353 xtesting.Equal(t, EqualG(tc.give1, tc.give2), tc.want) 1354 give1, give2 := newTestSlice2(tc.give1), newTestSlice2(tc.give2) 1355 xtesting.Equal(t, EqualWithG(give1, give2, eq), tc.want) 1356 } 1357 } 1358 1359 func TestElementMatch(t *testing.T) { 1360 eq := func(i, j interface{}) bool { return i.(testStruct).value == j.(testStruct).value } 1361 1362 for _, tc := range []struct { 1363 give1 []interface{} 1364 give2 []interface{} 1365 want bool 1366 }{ 1367 {[]interface{}{}, []interface{}{}, true}, 1368 {[]interface{}{0}, []interface{}{}, false}, 1369 {[]interface{}{}, []interface{}{0}, false}, 1370 {[]interface{}{0}, []interface{}{0}, true}, 1371 {[]interface{}{1, 1, 1}, []interface{}{1}, false}, 1372 {[]interface{}{1}, []interface{}{1, 1, 1}, false}, 1373 {[]interface{}{1, 1, 1}, []interface{}{1, 1, 1}, true}, 1374 {[]interface{}{1, 2, 1}, []interface{}{1, 1, 2}, true}, 1375 {[]interface{}{1, 2, 3}, []interface{}{1, 2, 2}, false}, 1376 {[]interface{}{1, 2, 2}, []interface{}{1, 2, 3}, false}, 1377 } { 1378 xtesting.Equal(t, ElementMatch(tc.give1, tc.give2), tc.want) 1379 give1, give2 := newTestSlice1(tc.give1), newTestSlice1(tc.give2) 1380 xtesting.Equal(t, ElementMatchWith(give1, give2, eq), tc.want) 1381 } 1382 1383 for _, tc := range []struct { 1384 give1 []int 1385 give2 []int 1386 want bool 1387 }{ 1388 {[]int{}, []int{}, true}, 1389 {[]int{0}, []int{}, false}, 1390 {[]int{}, []int{0}, false}, 1391 {[]int{0}, []int{0}, true}, 1392 {[]int{1, 1, 1}, []int{1}, false}, 1393 {[]int{1}, []int{1, 1, 1}, false}, 1394 {[]int{1, 1, 1}, []int{1, 1, 1}, true}, 1395 {[]int{1, 2, 1}, []int{1, 1, 2}, true}, 1396 {[]int{1, 2, 3}, []int{1, 2, 2}, false}, 1397 {[]int{1, 2, 2}, []int{1, 2, 3}, false}, 1398 } { 1399 xtesting.Equal(t, ElementMatchG(tc.give1, tc.give2), tc.want) 1400 give1, give2 := newTestSlice2(tc.give1), newTestSlice2(tc.give2) 1401 xtesting.Equal(t, ElementMatchWithG(give1, give2, eq), tc.want) 1402 } 1403 } 1404 1405 type testError struct{ m string } 1406 1407 func (t *testError) Error() string { return t.m } 1408 1409 func TestRepeat(t *testing.T) { 1410 for _, tc := range []struct { 1411 giveValue interface{} 1412 giveCount uint 1413 want []interface{} 1414 }{ 1415 {nil, 0, []interface{}{}}, 1416 {nil, 2, []interface{}{nil, nil}}, 1417 {true, 0, []interface{}{}}, 1418 {true, 1, []interface{}{true}}, 1419 {5, 5, []interface{}{5, 5, 5, 5, 5}}, 1420 {"", 5, []interface{}{"", "", "", "", ""}}, 1421 {uint(0), 2, []interface{}{uint(0), uint(0)}}, 1422 {[]float64{1.1, 2.2}, 3, []interface{}{[]float64{1.1, 2.2}, []float64{1.1, 2.2}, []float64{1.1, 2.2}}}, 1423 {error(nil), 2, []interface{}{nil, nil}}, // <<< 1424 {error((*testError)(nil)), 2, []interface{}{(*testError)(nil), (*testError)(nil)}}, // <<< 1425 {(*testError)(nil), 2, []interface{}{(*testError)(nil), (*testError)(nil)}}, 1426 {&testError{"test"}, 2, []interface{}{&testError{"test"}, &testError{"test"}}}, 1427 } { 1428 xtesting.Equal(t, Repeat(tc.giveValue, tc.giveCount), tc.want) 1429 } 1430 1431 for _, tc := range []struct { 1432 giveValue interface{} 1433 giveCount uint 1434 want interface{} 1435 }{ 1436 {nil, 0, []interface{}{}}, 1437 {nil, 2, []interface{}{nil, nil}}, 1438 {true, 0, []bool{}}, 1439 {true, 1, []bool{true}}, 1440 {5, 5, []int{5, 5, 5, 5, 5}}, 1441 {"", 5, []string{"", "", "", "", ""}}, 1442 {uint(0), 2, []uint{uint(0), uint(0)}}, 1443 {[]float64{1.1, 2.2}, 3, [][]float64{{1.1, 2.2}, {1.1, 2.2}, {1.1, 2.2}}}, 1444 {error(nil), 2, []interface{}{nil, nil}}, // <<< 1445 {error((*testError)(nil)), 2, []*testError{(*testError)(nil), (*testError)(nil)}}, // <<< 1446 {(*testError)(nil), 2, []*testError{(*testError)(nil), (*testError)(nil)}}, 1447 {&testError{"test"}, 2, []*testError{{"test"}, {"test"}}}, 1448 } { 1449 xtesting.Equal(t, RepeatG(tc.giveValue, tc.giveCount), tc.want) 1450 } 1451 }