github.com/alibabacloud-go/tea@v1.3.10/dara/array_test.go (about) 1 package dara 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 type MyStruct struct { 9 Name string 10 } 11 12 func TestArrContains(t *testing.T) { 13 // Create test data 14 str1 := "Hello" 15 str2 := "World" 16 ptrStrArr := []*string{&str1, &str2} 17 18 // Test with string pointer array 19 if !ArrContains(ptrStrArr, "World") { 20 t.Errorf("Expected true, but got false") 21 } 22 23 if ArrContains(ptrStrArr, "Go") { 24 t.Errorf("Expected false, but got true") 25 } 26 27 // Create integer pointer values 28 num1 := 1 29 num2 := 2 30 ptrIntArr := []*int{&num1, &num2} 31 32 // Test with integer pointer array 33 if !ArrContains(ptrIntArr, 2) { 34 t.Errorf("Expected true, but got false") 35 } 36 37 if ArrContains(ptrIntArr, 3) { 38 t.Errorf("Expected false, but got true") 39 } 40 41 // Create struct pointers 42 struct1 := &MyStruct{Name: "One"} 43 struct2 := &MyStruct{Name: "Two"} 44 structPtrArr := []*MyStruct{struct1, struct2} 45 46 // Test struct pointer array 47 if ArrContains(structPtrArr, &MyStruct{Name: "One"}) { 48 t.Errorf("Expected false, but got true") 49 } 50 51 // Check for existence by value 52 if ArrContains(structPtrArr, "One") { 53 t.Errorf("Expected false, but got true") 54 } 55 56 if !ArrContains(structPtrArr, struct1) { 57 t.Errorf("Expected true, but got false") 58 } 59 60 interfaceArr := []interface{}{str1, num1, struct1} 61 62 if !ArrContains(interfaceArr, "Hello") { 63 t.Errorf("Expected true, but got false") 64 } 65 66 if ArrContains(interfaceArr, "World") { 67 t.Errorf("Expected false, but got true") 68 } 69 70 if !ArrContains(interfaceArr, 1) { 71 t.Errorf("Expected true, but got false") 72 } 73 74 if ArrContains(interfaceArr, 2) { 75 t.Errorf("Expected false, but got true") 76 } 77 78 if ArrContains(interfaceArr, &MyStruct{Name: "One"}) { 79 t.Errorf("Expected false, but got true") 80 } 81 82 if !ArrContains(interfaceArr, struct1) { 83 t.Errorf("Expected true, but got false") 84 } 85 } 86 87 func TestArrIndex(t *testing.T) { 88 // Create test data for string pointer array 89 str1 := "Hello" 90 str2 := "World" 91 ptrStrArr := []*string{&str1, &str2} 92 93 // Test with string pointer array 94 if index := ArrIndex(ptrStrArr, "World"); index != 1 { 95 t.Errorf("Expected index 1, but got %d", index) 96 } 97 98 if index := ArrIndex(ptrStrArr, "Go"); index != -1 { 99 t.Errorf("Expected index -1, but got %d", index) 100 } 101 102 // Create integer pointer values 103 num1 := 1 104 num2 := 2 105 ptrIntArr := []*int{&num1, &num2} 106 107 // Test with integer pointer array 108 if index := ArrIndex(ptrIntArr, 2); index != 1 { 109 t.Errorf("Expected index 1, but got %d", index) 110 } 111 112 if index := ArrIndex(ptrIntArr, 3); index != -1 { 113 t.Errorf("Expected index -1, but got %d", index) 114 } 115 116 // Create struct pointers 117 struct1 := &MyStruct{Name: "One"} 118 struct2 := &MyStruct{Name: "Two"} 119 structPtrArr := []*MyStruct{struct1, struct2} 120 121 // Test struct pointer array 122 if index := ArrIndex(structPtrArr, &MyStruct{Name: "One"}); index != -1 { 123 t.Errorf("Expected index -1, but got %d", index) 124 } 125 126 interfaceArr := []interface{}{str1, num1, struct1} 127 128 if index := ArrIndex(interfaceArr, 1); index != 1 { 129 t.Errorf("Expected index 1, but got %d", index) 130 } 131 132 if index := ArrIndex(interfaceArr, "Hello"); index != 0 { 133 t.Errorf("Expected index 0, but got %d", index) 134 } 135 136 if index := ArrIndex(interfaceArr, struct1); index != 2 { 137 t.Errorf("Expected index 2, but got %d", index) 138 } 139 } 140 141 func TestArrJoin(t *testing.T) { 142 // Create test data 143 str1 := "Hello" 144 str2 := "World" 145 ptrStrArr := []*string{&str1, &str2} 146 147 // Test joining strings 148 result := ArrJoin(ptrStrArr, ", ") 149 expected := "Hello, World" 150 if result != expected { 151 t.Errorf("Expected '%s', but got '%s'", expected, result) 152 } 153 154 // Create integer pointer values 155 num1 := 1 156 num2 := 2 157 ptrIntArr := []*int{&num1, &num2} 158 159 // Test joining integers 160 result = ArrJoin(ptrIntArr, " + ") 161 expected = "1 + 2" 162 if result != expected { 163 t.Errorf("Expected '%s', but got '%s'", expected, result) 164 } 165 166 // Create mixed types (if needed) 167 struct1 := &MyStruct{Name: "One"} 168 ptrMixedArr := []interface{}{str1, num1, struct1} 169 170 // Test joining mixed types 171 result = ArrJoin(ptrMixedArr, " | ") 172 expected = "Hello | 1" 173 if result != expected { 174 t.Errorf("Expected '%s', but got '%s'", expected, result) 175 } 176 } 177 178 func TestArrShift(t *testing.T) { 179 // Create test data for string pointer array 180 str1 := "Hello" 181 str2 := "World" 182 ptrStrArr := []*string{&str1, &str2} 183 184 // Test shifting strings 185 removed := ArrShift(&ptrStrArr) 186 if removed != &str1 { 187 t.Errorf("Expected '%v', but got '%v'", &str1, removed) 188 } 189 190 // After shifting, the first element should now be "World" 191 if ptrStrArr[0] != &str2 { 192 t.Errorf("Expected next element to be '%v', but got '%v'", &str2, ptrStrArr[0]) 193 } 194 195 // Create integer pointer values 196 num1 := 1 197 num2 := 2 198 ptrIntArr := []*int{&num1, &num2} 199 200 // Test shifting integers 201 removedInt := ArrShift(&ptrIntArr) 202 if removedInt != &num1 { 203 t.Errorf("Expected '%v', but got '%v'", &num1, removedInt) 204 } 205 206 // Create struct pointers 207 struct1 := &MyStruct{Name: "One"} 208 struct2 := &MyStruct{Name: "Two"} 209 structPtrArr := []*MyStruct{struct1, struct2} 210 211 // Test struct pointer array 212 removedStruct := ArrShift(&structPtrArr) 213 if removedStruct != struct1 { 214 t.Errorf("Expected '%v', but got '%v'", struct1, removedStruct) 215 } 216 217 interfaceArr := []interface{}{str1, num1, struct1} 218 219 removedStr := ArrShift(&interfaceArr) 220 221 if removedStr != str1 { 222 t.Errorf("Expected '%v', but got '%v'", str1, removedStr) 223 } 224 225 removedInt = ArrShift(&interfaceArr) 226 227 if removedInt != num1 { 228 t.Errorf("Expected '%v', but got '%v'", removedInt, num1) 229 } 230 231 if interfaceArr[0] != struct1 { 232 t.Errorf("Expected next element to be '%v', but got '%v'", struct1, interfaceArr[0]) 233 } 234 } 235 236 func TestArrPop(t *testing.T) { 237 // Create test data for string pointer array 238 str1 := "Hello" 239 str2 := "World" 240 ptrStrArr := []*string{&str1, &str2} 241 242 // Test popping strings 243 removed := ArrPop(&ptrStrArr) 244 if removed != &str2 { 245 t.Errorf("Expected '%v', but got '%v'", &str2, removed) 246 } 247 248 // After popping, the array should only contain "Hello" 249 if len(ptrStrArr) != 1 || ptrStrArr[0] != &str1 { 250 t.Errorf("Expected remaining element to be '%v', but got '%v'", &str1, ptrStrArr) 251 } 252 253 // Create integer pointer values 254 num1 := 1 255 num2 := 2 256 ptrIntArr := []*int{&num1, &num2} 257 258 // Test popping integers 259 removedInt := ArrPop(&ptrIntArr) 260 if removedInt != &num2 { 261 t.Errorf("Expected '%v', but got '%v'", &num2, removedInt) 262 } 263 264 // Create struct pointers 265 struct1 := &MyStruct{Name: "One"} 266 struct2 := &MyStruct{Name: "Two"} 267 structPtrArr := []*MyStruct{struct1, struct2} 268 269 // Test struct pointer array 270 removedStruct := ArrPop(&structPtrArr) 271 if removedStruct != struct2 { 272 t.Errorf("Expected '%v', but got '%v'", struct2, removedStruct) 273 } 274 } 275 276 func TestArrUnshift(t *testing.T) { 277 // Create test data for string pointer array 278 str1 := "World" 279 ptrStrArr := []*string{&str1} 280 281 // New string to be added 282 str2 := "Hello" 283 284 // Test unshifting strings 285 length := ArrUnshift(&ptrStrArr, &str2) 286 if length != 2 { 287 t.Fatalf("Expected array length is 2 but %d", length) 288 } 289 290 if ptrStrArr[0] != &str2 || ptrStrArr[1] != &str1 { 291 t.Errorf("Expected '%v', '%v', but got '%v'", &str2, &str1, ptrStrArr) 292 } 293 294 // Test unshifting integers 295 num1 := 2 296 ptrIntArr := []*int{&num1} 297 num2 := 1 298 299 length = ArrUnshift(&ptrIntArr, &num2) 300 if length != 2 { 301 t.Fatalf("Expected array length is 2 but %d", length) 302 } 303 304 if ptrIntArr[0] != &num2 || ptrIntArr[1] != &num1 { 305 t.Errorf("Expected '%v', '%v', but got '%v'", &num2, &num1, ptrIntArr) 306 } 307 308 ptrMixedArr := []interface{}{str1, num1} 309 struct1 := &MyStruct{Name: "One"} 310 311 length = ArrUnshift(&ptrMixedArr, struct1) 312 313 if length != 3 { 314 t.Fatalf("Expected array length is 3 but %d", length) 315 } 316 317 if ptrMixedArr[0] != struct1 { 318 t.Errorf("Expected ptrMixedArr index 2 is '%v', but got '%v'", struct1, ptrMixedArr[0]) 319 } 320 } 321 322 func TestArrPush(t *testing.T) { 323 // Create test data for string pointer array 324 str1 := "Hello" 325 ptrStrArr := []*string{&str1} 326 327 // New string to be added 328 str2 := "World" 329 330 // Test pushing strings 331 length := ArrPush(&ptrStrArr, &str2) 332 if length != 2 { 333 t.Fatalf("Expected array length is 2 but %d", length) 334 } 335 336 if ptrStrArr[0] != &str1 || ptrStrArr[1] != &str2 { 337 t.Errorf("Expected '%v', '%v', but got '%v'", &str1, &str2, ptrStrArr) 338 } 339 340 // Test pushing integers 341 num1 := 1 342 ptrIntArr := []*int{&num1} 343 num2 := 2 344 345 length = ArrPush(&ptrIntArr, &num2) 346 if length != 2 { 347 t.Fatalf("Expected array length is 2 but %d", length) 348 } 349 350 if ptrIntArr[0] != &num1 || ptrIntArr[1] != &num2 { 351 t.Errorf("Expected '%v', '%v', but got '%v'", &num1, &num2, ptrIntArr) 352 } 353 354 ptrMixedArr := []interface{}{str1, num1} 355 struct1 := &MyStruct{Name: "One"} 356 357 length = ArrPush(&ptrMixedArr, struct1) 358 359 if length != 3 { 360 t.Fatalf("Expected array length is 3 but %d", length) 361 } 362 363 if ptrMixedArr[2] != struct1 { 364 t.Errorf("Expected ptrMixedArr index 2 is '%v', but got '%v'", struct1, ptrMixedArr[2]) 365 } 366 } 367 368 func TestConcatArr(t *testing.T) { 369 str1, str2, str3, str4 := "A", "B", "C", "D" 370 // String arrays 371 strArr1 := []*string{&str1, &str2} 372 strArr2 := []*string{&str3, &str4} 373 374 // Test concatenating string arrays 375 result := ConcatArr(strArr1, strArr2) 376 expected := []*string{&str1, &str2, &str3, &str4} 377 378 if !reflect.DeepEqual(result, expected) { 379 t.Errorf("Expected '%v', but got '%v'", expected, result) 380 } 381 382 num1, num2, num3, num4 := 1, 2, 3, 4 383 // Integer arrays 384 intArr1 := []*int{&num1, &num2} 385 intArr2 := []*int{&num3, &num4} 386 387 // Test concatenating integer arrays 388 result = ConcatArr(intArr1, intArr2) 389 expectedInts := []*int{&num1, &num2, &num3, &num4} 390 391 if !reflect.DeepEqual(result, expectedInts) { 392 t.Errorf("Expected '%v', but got '%v'", expectedInts, result) 393 } 394 395 // Mixed type arrays 396 mixedArr1 := []interface{}{1, "two"} 397 mixedArr2 := []interface{}{3.0, "four"} 398 399 // Test concatenating mixed type arrays 400 result = ConcatArr(mixedArr1, mixedArr2) 401 expectedMixed := []interface{}{1, "two", 3.0, "four"} 402 403 if !reflect.DeepEqual(result, expectedMixed) { 404 t.Errorf("Expected '%v', but got '%v'", expectedMixed, result) 405 } 406 } 407 408 func TestArrAppend(t *testing.T) { 409 // 测试用例1:插入中间位置 410 t.Run("Append to middle of an array", func(t *testing.T) { 411 numbers := []*int{new(int), new(int), new(int)} 412 for i := range numbers { 413 *numbers[i] = i + 1 414 } 415 416 // 将 9 插入到索引 1 417 valueToInsert := new(int) 418 *valueToInsert = 9 419 420 // 期望的结果 421 expected := []*int{new(int), new(int), new(int), new(int)} 422 *expected[0], *expected[1], *expected[2], *expected[3] = 1, 9, 2, 3 423 424 defer func() { 425 if !reflect.DeepEqual(numbers, expected) { 426 t.Errorf("Expected %+v, but got %+v", expected, numbers) 427 } 428 }() 429 430 ArrAppend(&numbers, valueToInsert, 1) 431 }) 432 433 // 测试用例2: 尝试在越界处插入 434 t.Run("Index out of bounds", func(t *testing.T) { 435 numbers := []*int{new(int), new(int), new(int)} 436 for i := range numbers { 437 *numbers[i] = i + 1 438 } 439 440 defer func() { 441 // Defer 检查:越界情况下,数组应保持不变 442 expected := []*int{new(int), new(int), new(int)} 443 *expected[0], *expected[1], *expected[2] = 1, 2, 3 444 if !reflect.DeepEqual(numbers, expected) { 445 t.Errorf("Index out of bounds should not modify array, but got %+v", numbers) 446 } 447 }() 448 449 valueToInsert := new(int) 450 *valueToInsert = 9 451 ArrAppend(&numbers, valueToInsert, 10) // 超出范围 452 }) 453 } 454 455 456 func TestArrRemove(t *testing.T) { 457 // Create test data for string pointer array 458 str1 := "A" 459 str2 := "B" 460 str3 := "C" 461 ptrStrArr := []*string{&str1, &str2, &str3} 462 463 // Test removing an element in the middle 464 ArrRemove(&ptrStrArr, "B") 465 466 if len(ptrStrArr) != 2 || ptrStrArr[0] != &str1 || ptrStrArr[1] != &str3 { 467 t.Errorf("Expected '%v', '%v', but got '%v'", &str1, &str3, ptrStrArr) 468 } 469 470 // Test removing the first element 471 ArrRemove(&ptrStrArr, "A") 472 if len(ptrStrArr) != 1 || ptrStrArr[0] != &str3 { 473 t.Errorf("Expected '%v', but got '%v'", &str3, ptrStrArr) 474 } 475 476 num1 := 1 477 num2 := 2 478 num3 := 3 479 ptrIntArr := []*int{&num1, &num2, &num3} 480 481 ArrRemove(&ptrIntArr, 2) 482 483 if len(ptrIntArr) != 2 || ptrIntArr[0] != &num1 || ptrIntArr[1] != &num3 { 484 t.Errorf("Expected '%v', '%v', but got '%v'", &num1, &num3, ptrIntArr) 485 } 486 487 // Test removing the first element 488 ArrRemove(&ptrIntArr, 3) 489 if len(ptrIntArr) != 1 || ptrIntArr[0] != &num1 { 490 t.Errorf("Expected '%v', but got '%v'", &num1, ptrIntArr) 491 } 492 493 struct1 := &MyStruct{Name: "One"} 494 struct2 := &MyStruct{Name: "Two"} 495 struct3 := &MyStruct{Name: "Three"} 496 structPtrArr := []*MyStruct{struct1, struct2, struct3} 497 498 ArrRemove(&structPtrArr, struct2) 499 if len(structPtrArr) != 2 || structPtrArr[0] != struct1 || structPtrArr[1] != struct3 { 500 t.Errorf("Expected '%v', '%v', but got '%v'", struct1, struct3, structPtrArr) 501 } 502 503 interfaceArr := []interface{}{str1, num1, struct1} 504 505 ArrRemove(&interfaceArr, num1) 506 if len(interfaceArr) != 2 || interfaceArr[0] != str1 || interfaceArr[1] != struct1 { 507 t.Errorf("Expected '%s', '%v', but got '%v'", str1, struct1, interfaceArr) 508 } 509 510 ArrRemove(&interfaceArr, struct1) 511 if len(interfaceArr) != 1 || interfaceArr[0] != str1 { 512 t.Errorf("Expected '%s', but got '%v'", str1, interfaceArr) 513 } 514 } 515 func TestSortArrIntPtr(t *testing.T) { 516 num1, num2, num3, num4 := 5, 3, 4, 6 517 intPtrArr := []*int{&num1, &num2, &num3, &num4} 518 sortedAsc := SortArr(intPtrArr, "ASC").([]*int) // 获取排序结果 519 if !reflect.DeepEqual([]int{*sortedAsc[0], *sortedAsc[1], *sortedAsc[2]}, []int{3, 4, 5}) { 520 t.Errorf("Ascending sort failed, expected: %v, but got %v", []*int{&num1, &num3, &num2}, sortedAsc) 521 } 522 sortedDesc := SortArr(intPtrArr, "DESC").([]*int) // 获取排序结果 523 if !reflect.DeepEqual([]int{*sortedDesc[0], *sortedDesc[1], *sortedDesc[2]}, []int{6, 5, 4}) { 524 t.Errorf("Descending sort failed, got %v", sortedDesc) 525 } 526 } 527 528 // TestSortArrStrPtr tests the SortArr function with an array of string pointers 529 func TestSortArrStrPtr(t *testing.T) { 530 str1, str2, str3 := "banana", "apple", "cherry" 531 strPtrArr := []*string{&str1, &str2, &str3} 532 sortedAsc := SortArr(strPtrArr, "AsC").([]*string) // 获取排序结果 533 if !reflect.DeepEqual([]string{*sortedAsc[0], *sortedAsc[1], *sortedAsc[2]}, []string{"apple", "banana", "cherry"}) { 534 t.Errorf("Ascending sort failed, got %v", sortedAsc) 535 } 536 sortedDesc := SortArr(strPtrArr, "dEsC").([]*string) // 获取排序结果 537 if !reflect.DeepEqual([]string{*sortedDesc[0], *sortedDesc[1], *sortedDesc[2]}, []string{"cherry", "banana", "apple"}) { 538 t.Errorf("Descending sort failed, got %v", sortedDesc) 539 } 540 } 541 542 // TestSortArrStructPtr tests the SortArr function with an array of struct pointers 543 func TestSortArrStructPtr(t *testing.T) { 544 type Person struct { 545 Name string 546 Age int 547 } 548 person1 := &Person{"Alice", 30} 549 person2 := &Person{"Bob", 25} 550 person3 := &Person{"Charlie", 35} 551 personArr := []*Person{person2, person1, person3} 552 // Ascending sort by Age (first field) 553 sortedAsc := SortArr(personArr, "aSc").([]*Person) // 获取排序结果 554 expectedAsc := []*Person{person1, person2, person3} 555 for i, p := range expectedAsc { 556 if sortedAsc[i].Name != p.Name || sortedAsc[i].Age != p.Age { 557 t.Errorf("Ascending sort failed, got %v", sortedAsc) 558 } 559 } 560 // Descending sort by Age (first field) 561 sortedDesc := SortArr(personArr, "DEsc").([]*Person) // 获取排序结果 562 expectedDesc := []*Person{person3, person2, person1} 563 for i, p := range expectedDesc { 564 if sortedDesc[i].Name != p.Name || sortedDesc[i].Age != p.Age { 565 t.Errorf("Descending sort failed, got %v", sortedDesc) 566 } 567 } 568 } 569 570 // TestSortArrInterface tests the SortArr function with an array of mixed interface{} types 571 func TestSortArrInterface(t *testing.T) { 572 type Person struct { 573 Name string 574 Age int 575 } 576 person1 := &Person{"Alice", 30} 577 person2 := &Person{"Bob", 25} 578 str1 := "banana" 579 str2 := "apple" 580 num1 := 5 581 num2 := 3 582 interfaceArr := []interface{}{str1, num1, person1, str2, num2, person2} 583 sortedAsc := SortArr(interfaceArr, "asc").([]interface{}) // 获取排序结果 584 expectedAsc := []interface{}{num2, num1, str2, str1, person1, person2} 585 for i, v := range expectedAsc { 586 if !reflect.DeepEqual(sortedAsc[i], v) { 587 t.Errorf("Ascending sort failed, got %v", sortedAsc) 588 } 589 } 590 sortedDesc := SortArr(interfaceArr, "desc").([]interface{}) // 获取排序结果 591 expectedDesc := []interface{}{person2, person1, str1, str2, num1, num2} 592 for i, v := range expectedDesc { 593 if !reflect.DeepEqual(sortedDesc[i], v) { 594 t.Errorf("Descending sort failed, got %v", sortedDesc) 595 } 596 } 597 }