github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bslice/comparable_test.go (about) 1 package bslice 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 func TestUnsafeComparableBSlice_Contains(t *testing.T) { 9 type fields struct { 10 es []int 11 } 12 type args struct { 13 e int 14 } 15 tests := []struct { 16 name string 17 fields fields 18 args args 19 want bool 20 }{ 21 { 22 name: "nil", 23 fields: fields{ 24 es: nil, 25 }, 26 args: args{ 27 e: 0, 28 }, 29 want: false, 30 }, 31 { 32 name: "", 33 fields: fields{ 34 es: []int{}, 35 }, 36 args: args{ 37 e: 0, 38 }, 39 want: false, 40 }, 41 { 42 name: "", 43 fields: fields{ 44 es: []int{1, 2, 3}, 45 }, 46 args: args{ 47 e: 0, 48 }, 49 want: false, 50 }, 51 { 52 name: "", 53 fields: fields{ 54 es: []int{1, 2, 3}, 55 }, 56 args: args{ 57 e: 1, 58 }, 59 want: true, 60 }, 61 { 62 name: "", 63 fields: fields{ 64 es: []int{1, 2, 3}, 65 }, 66 args: args{ 67 e: 2, 68 }, 69 want: true, 70 }, 71 { 72 name: "", 73 fields: fields{ 74 es: []int{1, 2, 3}, 75 }, 76 args: args{ 77 e: 3, 78 }, 79 want: true, 80 }, 81 { 82 name: "", 83 fields: fields{ 84 es: []int{1, 2, 3}, 85 }, 86 args: args{ 87 e: 4, 88 }, 89 want: false, 90 }, 91 } 92 for _, tt := range tests { 93 t.Run(tt.name, func(t *testing.T) { 94 x := NewUnsafeComparableBSliceBySlice(tt.fields.es) 95 assert.Equalf(t, tt.want, x.Contains(tt.args.e), "Contains(%v)", tt.args.e) 96 }) 97 } 98 } 99 100 func TestUnsafeComparableBSlice_Equal(t *testing.T) { 101 type fields struct { 102 es []int 103 } 104 type args struct { 105 es []int 106 } 107 tests := []struct { 108 name string 109 fields fields 110 args args 111 want bool 112 }{ 113 { 114 name: "nil", 115 fields: fields{ 116 es: nil, 117 }, 118 args: args{ 119 es: nil, 120 }, 121 want: true, 122 }, 123 { 124 name: "", 125 fields: fields{ 126 es: []int{}, 127 }, 128 args: args{ 129 es: []int{}, 130 }, 131 want: true, 132 }, 133 { 134 name: "", 135 fields: fields{ 136 es: []int{}, 137 }, 138 args: args{ 139 es: nil, 140 }, 141 want: true, 142 }, 143 { 144 name: "", 145 fields: fields{ 146 es: nil, 147 }, 148 args: args{ 149 es: []int{}, 150 }, 151 want: true, 152 }, 153 { 154 name: "", 155 fields: fields{ 156 es: []int{1, 2, 3}, 157 }, 158 args: args{ 159 es: []int{1, 2}, 160 }, 161 want: false, 162 }, 163 { 164 name: "", 165 fields: fields{ 166 es: []int{1, 2, 3}, 167 }, 168 args: args{ 169 es: []int{1, 2, 3, 4}, 170 }, 171 want: false, 172 }, 173 { 174 name: "", 175 fields: fields{ 176 es: []int{1, 2, 3}, 177 }, 178 args: args{ 179 es: []int{1, 2, 3}, 180 }, 181 want: true, 182 }, 183 } 184 for _, tt := range tests { 185 t.Run(tt.name, func(t *testing.T) { 186 x := NewUnsafeComparableBSliceBySlice(tt.fields.es) 187 assert.Equalf(t, tt.want, x.Equal(tt.args.es), "Equal(%v)", tt.args.es) 188 }) 189 } 190 } 191 192 func TestUnsafeComparableBSlice_Compact(t *testing.T) { 193 type fields struct { 194 es []int 195 } 196 tests := []struct { 197 name string 198 fields fields 199 want []int 200 }{ 201 { 202 name: "nil", 203 fields: fields{ 204 es: nil, 205 }, 206 want: nil, 207 }, 208 { 209 name: "", 210 fields: fields{ 211 es: []int{}, 212 }, 213 want: []int{}, 214 }, 215 { 216 name: "", 217 fields: fields{ 218 es: []int{1, 1, 2, 2, 3, 3, 3}, 219 }, 220 want: []int{1, 2, 3}, 221 }, 222 { 223 name: "", 224 fields: fields{ 225 es: []int{1, 2, 3, 3, 2, 1}, 226 }, 227 228 want: []int{1, 2, 3, 2, 1}, 229 }, 230 } 231 for _, tt := range tests { 232 t.Run(tt.name, func(t *testing.T) { 233 x := NewUnsafeComparableBSliceBySlice(tt.fields.es) 234 x.Compact() 235 assert.Equalf(t, tt.want, x.ToMetaSlice(), "Compact") 236 }) 237 } 238 } 239 240 func TestSafeComparableBSlice_Contains(t *testing.T) { 241 type fields struct { 242 es []int 243 } 244 type args struct { 245 e int 246 } 247 tests := []struct { 248 name string 249 fields fields 250 args args 251 want bool 252 }{ 253 { 254 name: "nil", 255 fields: fields{ 256 es: nil, 257 }, 258 args: args{ 259 e: 0, 260 }, 261 want: false, 262 }, 263 { 264 name: "", 265 fields: fields{ 266 es: []int{}, 267 }, 268 args: args{ 269 e: 0, 270 }, 271 want: false, 272 }, 273 { 274 name: "", 275 fields: fields{ 276 es: []int{1, 2, 3}, 277 }, 278 args: args{ 279 e: 0, 280 }, 281 want: false, 282 }, 283 { 284 name: "", 285 fields: fields{ 286 es: []int{1, 2, 3}, 287 }, 288 args: args{ 289 e: 1, 290 }, 291 want: true, 292 }, 293 { 294 name: "", 295 fields: fields{ 296 es: []int{1, 2, 3}, 297 }, 298 args: args{ 299 e: 2, 300 }, 301 want: true, 302 }, 303 { 304 name: "", 305 fields: fields{ 306 es: []int{1, 2, 3}, 307 }, 308 args: args{ 309 e: 3, 310 }, 311 want: true, 312 }, 313 { 314 name: "", 315 fields: fields{ 316 es: []int{1, 2, 3}, 317 }, 318 args: args{ 319 e: 4, 320 }, 321 want: false, 322 }, 323 } 324 for _, tt := range tests { 325 t.Run(tt.name, func(t *testing.T) { 326 x := NewSafeComparableBSliceBySlice(tt.fields.es) 327 assert.Equalf(t, tt.want, x.Contains(tt.args.e), "Contains(%v)", tt.args.e) 328 }) 329 } 330 } 331 332 func TestSafeComparableBSlice_Equal(t *testing.T) { 333 type fields struct { 334 es []int 335 } 336 type args struct { 337 es []int 338 } 339 tests := []struct { 340 name string 341 fields fields 342 args args 343 want bool 344 }{ 345 { 346 name: "nil", 347 fields: fields{ 348 es: nil, 349 }, 350 args: args{ 351 es: nil, 352 }, 353 want: true, 354 }, 355 { 356 name: "", 357 fields: fields{ 358 es: []int{}, 359 }, 360 args: args{ 361 es: []int{}, 362 }, 363 want: true, 364 }, 365 { 366 name: "", 367 fields: fields{ 368 es: []int{}, 369 }, 370 args: args{ 371 es: nil, 372 }, 373 want: true, 374 }, 375 { 376 name: "", 377 fields: fields{ 378 es: nil, 379 }, 380 args: args{ 381 es: []int{}, 382 }, 383 want: true, 384 }, 385 { 386 name: "", 387 fields: fields{ 388 es: []int{1, 2, 3}, 389 }, 390 args: args{ 391 es: []int{1, 2}, 392 }, 393 want: false, 394 }, 395 { 396 name: "", 397 fields: fields{ 398 es: []int{1, 2, 3}, 399 }, 400 args: args{ 401 es: []int{1, 2, 3, 4}, 402 }, 403 want: false, 404 }, 405 { 406 name: "", 407 fields: fields{ 408 es: []int{1, 2, 3}, 409 }, 410 args: args{ 411 es: []int{1, 2, 3}, 412 }, 413 want: true, 414 }, 415 } 416 for _, tt := range tests { 417 t.Run(tt.name, func(t *testing.T) { 418 x := NewSafeComparableBSliceBySlice(tt.fields.es) 419 assert.Equalf(t, tt.want, x.Equal(tt.args.es), "Equal(%v)", tt.args.es) 420 }) 421 } 422 } 423 424 func TestSafeComparableBSlice_Compact(t *testing.T) { 425 type fields struct { 426 es []int 427 } 428 tests := []struct { 429 name string 430 fields fields 431 want []int 432 }{ 433 { 434 name: "nil", 435 fields: fields{ 436 es: nil, 437 }, 438 want: nil, 439 }, 440 { 441 name: "", 442 fields: fields{ 443 es: []int{}, 444 }, 445 want: []int{}, 446 }, 447 { 448 name: "", 449 fields: fields{ 450 es: []int{1, 1, 2, 2, 3, 3, 3}, 451 }, 452 want: []int{1, 2, 3}, 453 }, 454 { 455 name: "", 456 fields: fields{ 457 es: []int{1, 2, 3, 3, 2, 1}, 458 }, 459 460 want: []int{1, 2, 3, 2, 1}, 461 }, 462 } 463 for _, tt := range tests { 464 t.Run(tt.name, func(t *testing.T) { 465 x := NewSafeComparableBSliceBySlice(tt.fields.es) 466 x.Compact() 467 assert.Equalf(t, tt.want, x.ToMetaSlice(), "Compact") 468 }) 469 } 470 }