github.com/enetx/g@v1.0.80/tests/bytes_test.go (about) 1 package g_test 2 3 import ( 4 "bytes" 5 "io" 6 "reflect" 7 "regexp" 8 "testing" 9 "unicode" 10 11 "github.com/enetx/g" 12 ) 13 14 func TestNewBytes(t *testing.T) { 15 // Test case with string input 16 strInput := "hello" 17 bytesFromStr := g.NewBytes(strInput) 18 expectedBytesFromStr := g.Bytes("hello") 19 if !bytes.Equal(bytesFromStr, expectedBytesFromStr) { 20 t.Errorf("Conversion from string failed. Expected: %s, Got: %s", expectedBytesFromStr, bytesFromStr) 21 } 22 23 // Test case with []byte input 24 byteSliceInput := []byte{104, 101, 108, 108, 111} 25 bytesFromSlice := g.NewBytes(byteSliceInput) 26 expectedBytesFromSlice := g.Bytes("hello") 27 if !bytes.Equal(bytesFromSlice, expectedBytesFromSlice) { 28 t.Errorf("Conversion from byte slice failed. Expected: %s, Got: %s", expectedBytesFromSlice, bytesFromSlice) 29 } 30 } 31 32 func TestBytesReplace(t *testing.T) { 33 // Test case where old byte sequence exists and is replaced 34 bs1 := g.Bytes("hello world") 35 oldB1 := g.Bytes("world") 36 newB1 := g.Bytes("gopher") 37 replaced1 := bs1.Replace(oldB1, newB1, -1) 38 expected1 := g.Bytes("hello gopher") 39 if !bytes.Equal(replaced1, expected1) { 40 t.Errorf("Replacement failed. Expected: %s, Got: %s", expected1, replaced1) 41 } 42 43 // Test case with multiple occurrences of old byte sequence 44 bs2 := g.Bytes("hello world hello world") 45 oldB2 := g.Bytes("world") 46 newB2 := g.Bytes("gopher") 47 replaced2 := bs2.Replace(oldB2, newB2, -1) 48 expected2 := g.Bytes("hello gopher hello gopher") 49 if !bytes.Equal(replaced2, expected2) { 50 t.Errorf("Replacement failed. Expected: %s, Got: %s", expected2, replaced2) 51 } 52 53 // Test case with limited replacements 54 bs3 := g.Bytes("hello world hello world") 55 oldB3 := g.Bytes("world") 56 newB3 := g.Bytes("gopher") 57 replaced3 := bs3.Replace(oldB3, newB3, 1) 58 expected3 := g.Bytes("hello gopher hello world") 59 if !bytes.Equal(replaced3, expected3) { 60 t.Errorf("Replacement failed. Expected: %s, Got: %s", expected3, replaced3) 61 } 62 63 // Test case where old byte sequence doesn't exist 64 bs4 := g.Bytes("hello world") 65 oldB4 := g.Bytes("gopher") 66 newB4 := g.Bytes("earth") 67 replaced4 := bs4.Replace(oldB4, newB4, -1) 68 if !bytes.Equal(replaced4, bs4) { 69 t.Errorf("Expected no change when old byte sequence doesn't exist. Got: %s", replaced4) 70 } 71 } 72 73 func TestReplaceAll(t *testing.T) { 74 // Test case where old byte sequence exists and is replaced 75 bs1 := g.Bytes("hello world") 76 oldB1 := g.Bytes("world") 77 newB1 := g.Bytes("gopher") 78 replaced1 := bs1.ReplaceAll(oldB1, newB1) 79 expected1 := g.Bytes("hello gopher") 80 if !bytes.Equal(replaced1, expected1) { 81 t.Errorf("Replacement failed. Expected: %s, Got: %s", expected1, replaced1) 82 } 83 84 // Test case with multiple occurrences of old byte sequence 85 bs2 := g.Bytes("hello world hello world") 86 oldB2 := g.Bytes("world") 87 newB2 := g.Bytes("gopher") 88 replaced2 := bs2.ReplaceAll(oldB2, newB2) 89 expected2 := g.Bytes("hello gopher hello gopher") 90 if !bytes.Equal(replaced2, expected2) { 91 t.Errorf("Replacement failed. Expected: %s, Got: %s", expected2, replaced2) 92 } 93 94 // Test case where old byte sequence doesn't exist 95 bs3 := g.Bytes("hello world") 96 oldB3 := g.Bytes("gopher") 97 newB3 := g.Bytes("earth") 98 replaced3 := bs3.ReplaceAll(oldB3, newB3) 99 if !bytes.Equal(replaced3, bs3) { 100 t.Errorf("Expected no change when old byte sequence doesn't exist. Got: %s", replaced3) 101 } 102 } 103 104 func TestBytesReplaceRegexp(t *testing.T) { 105 // Test case where pattern matches and is replaced 106 bs1 := g.Bytes("hello world hello world") 107 pattern1 := regexp.MustCompile("world") 108 newB1 := g.Bytes("gopher") 109 replaced1 := bs1.ReplaceRegexp(pattern1, newB1) 110 expected1 := g.Bytes("hello gopher hello gopher") 111 if !bytes.Equal(replaced1, expected1) { 112 t.Errorf("Replacement failed. Expected: %s, Got: %s", expected1, replaced1) 113 } 114 115 // Test case where pattern matches and is replaced with capture group 116 bs2 := g.Bytes("apple apple apple") 117 pattern2 := regexp.MustCompile(`(\w+)`) 118 newB2 := g.Bytes("${1}s") 119 replaced2 := bs2.ReplaceRegexp(pattern2, newB2) 120 expected2 := g.Bytes("apples apples apples") 121 if !bytes.Equal(replaced2, expected2) { 122 t.Errorf("Replacement with capture group failed. Expected: %s, Got: %s", expected2, replaced2) 123 } 124 125 // Test case where pattern doesn't match 126 bs3 := g.Bytes("hello world") 127 pattern3 := regexp.MustCompile("gopher") 128 newB3 := g.Bytes("earth") 129 replaced3 := bs3.ReplaceRegexp(pattern3, newB3) 130 if !bytes.Equal(replaced3, bs3) { 131 t.Errorf("Expected no change when pattern doesn't match. Got: %s", replaced3) 132 } 133 } 134 135 func TestBytesFindRegexp(t *testing.T) { 136 // Test case where pattern matches and is found 137 bs1 := g.Bytes("hello world") 138 pattern1 := regexp.MustCompile("world") 139 found1 := bs1.FindRegexp(pattern1) 140 expected1 := g.Bytes("world") 141 if found1.IsNone() { 142 t.Errorf("Expected to find matching pattern, but found none") 143 } else if !bytes.Equal(found1.Unwrap(), expected1) { 144 t.Errorf("Found pattern does not match expected result. Expected: %s, Got: %s", expected1, found1.Unwrap()) 145 } 146 147 // Test case where pattern doesn't match 148 bs2 := g.Bytes("hello world") 149 pattern2 := regexp.MustCompile("gopher") 150 found2 := bs2.FindRegexp(pattern2) 151 if found2.IsSome() { 152 t.Errorf("Expected not to find matching pattern, but found one") 153 } 154 } 155 156 func TestBytesTrim(t *testing.T) { 157 // Test case where cutset matches characters at the beginning and end 158 bs1 := g.Bytes("!!hello world!!") 159 cutset1 := g.String("! ") 160 trimmed1 := bs1.Trim(cutset1) 161 expected1 := g.Bytes("hello world") 162 if !bytes.Equal(trimmed1, expected1) { 163 t.Errorf("Trimming failed. Expected: %s, Got: %s", expected1, trimmed1) 164 } 165 166 // Test case where cutset matches characters only at the beginning 167 bs2 := g.Bytes("!!!hello world") 168 cutset2 := g.String("!") 169 trimmed2 := bs2.Trim(cutset2) 170 expected2 := g.Bytes("hello world") 171 if !bytes.Equal(trimmed2, expected2) { 172 t.Errorf("Trimming failed. Expected: %s, Got: %s", expected2, trimmed2) 173 } 174 175 // Test case where cutset matches characters only at the end 176 bs3 := g.Bytes("hello world!!!") 177 cutset3 := g.String("!") 178 trimmed3 := bs3.Trim(cutset3) 179 expected3 := g.Bytes("hello world") 180 if !bytes.Equal(trimmed3, expected3) { 181 t.Errorf("Trimming failed. Expected: %s, Got: %s", expected3, trimmed3) 182 } 183 184 // Test case where cutset doesn't match any characters 185 bs4 := g.Bytes("hello world") 186 cutset4 := g.String("-") 187 trimmed4 := bs4.Trim(cutset4) 188 if !bytes.Equal(trimmed4, bs4) { 189 t.Errorf("Expected no change when cutset doesn't match any characters. Got: %s", trimmed4) 190 } 191 } 192 193 func TestBytesTrimLeft(t *testing.T) { 194 // Test case where cutset matches characters at the beginning 195 bs1 := g.Bytes("!!hello world!!") 196 cutset1 := g.String("! ") 197 trimmed1 := bs1.TrimLeft(cutset1) 198 expected1 := g.Bytes("hello world!!") 199 if !bytes.Equal(trimmed1, expected1) { 200 t.Errorf("Trimming left failed. Expected: %s, Got: %s", expected1, trimmed1) 201 } 202 203 // Test case where cutset doesn't match any characters at the beginning 204 bs2 := g.Bytes("hello world") 205 cutset2 := g.String("-") 206 trimmed2 := bs2.TrimLeft(cutset2) 207 if !bytes.Equal(trimmed2, bs2) { 208 t.Errorf("Expected no change when cutset doesn't match any characters. Got: %s", trimmed2) 209 } 210 } 211 212 func TestBytesTrimRight(t *testing.T) { 213 // Test case where cutset matches characters at the end 214 bs1 := g.Bytes("!!hello world!!") 215 cutset1 := g.String("! ") 216 trimmed1 := bs1.TrimRight(cutset1) 217 expected1 := g.Bytes("!!hello world") 218 if !bytes.Equal(trimmed1, expected1) { 219 t.Errorf("Trimming right failed. Expected: %s, Got: %s", expected1, trimmed1) 220 } 221 222 // Test case where cutset doesn't match any characters at the end 223 bs2 := g.Bytes("hello world") 224 cutset2 := g.String("-") 225 trimmed2 := bs2.TrimRight(cutset2) 226 if !bytes.Equal(trimmed2, bs2) { 227 t.Errorf("Expected no change when cutset doesn't match any characters. Got: %s", trimmed2) 228 } 229 } 230 231 func TestBytesTrimPrefix(t *testing.T) { 232 // Test case where cutset matches the prefix 233 bs1 := g.Bytes("prefix_hello world") 234 cutset1 := g.Bytes("prefix_") 235 trimmed1 := bs1.TrimPrefix(cutset1) 236 expected1 := g.Bytes("hello world") 237 if !bytes.Equal(trimmed1, expected1) { 238 t.Errorf("Trimming prefix failed. Expected: %s, Got: %s", expected1, trimmed1) 239 } 240 241 // Test case where cutset doesn't match the prefix 242 bs2 := g.Bytes("hello world") 243 cutset2 := g.Bytes("nonexistent_") 244 trimmed2 := bs2.TrimPrefix(cutset2) 245 if !bytes.Equal(trimmed2, bs2) { 246 t.Errorf("Expected no change when cutset doesn't match the prefix. Got: %s", trimmed2) 247 } 248 } 249 250 func TestBytesTrimSuffix(t *testing.T) { 251 // Test case where cutset matches the suffix 252 bs1 := g.Bytes("hello world_suffix") 253 cutset1 := g.Bytes("_suffix") 254 trimmed1 := bs1.TrimSuffix(cutset1) 255 expected1 := g.Bytes("hello world") 256 if !bytes.Equal(trimmed1, expected1) { 257 t.Errorf("Trimming suffix failed. Expected: %s, Got: %s", expected1, trimmed1) 258 } 259 260 // Test case where cutset doesn't match the suffix 261 bs2 := g.Bytes("hello world") 262 cutset2 := g.Bytes("_nonexistent") 263 trimmed2 := bs2.TrimSuffix(cutset2) 264 if !bytes.Equal(trimmed2, bs2) { 265 t.Errorf("Expected no change when cutset doesn't match the suffix. Got: %s", trimmed2) 266 } 267 } 268 269 func TestBytesSplit(t *testing.T) { 270 // Test case where separator exists 271 bs1 := g.Bytes("hello world gopher") 272 separator1 := g.NewBytes(" ") 273 split1 := bs1.Split(separator1).Collect() 274 expected1 := g.SliceOf(g.NewBytes("hello"), g.NewBytes("world"), g.NewBytes("gopher")) 275 if !reflect.DeepEqual(split1, expected1) { 276 t.Errorf("Split failed. Expected: %v, Got: %v", expected1, split1) 277 } 278 279 // Test case where separator doesn't exist 280 bs2 := g.Bytes("helloworldgopher") 281 separator2 := g.NewBytes(" ") 282 split2 := bs2.Split(separator2).Collect() 283 expected2 := g.Slice[g.Bytes]{g.NewBytes("helloworldgopher")} 284 if !reflect.DeepEqual(split2, expected2) { 285 t.Errorf("Split failed. Expected: %v, Got: %v", expected2, split2) 286 } 287 } 288 289 func TestBytesAdd(t *testing.T) { 290 // Test case where bytes are added 291 bs1 := g.Bytes("hello") 292 obs1 := g.NewBytes(" world") 293 added1 := bs1.Add(obs1) 294 expected1 := g.Bytes("hello world") 295 if !bytes.Equal(added1, expected1) { 296 t.Errorf("Add failed. Expected: %s, Got: %s", expected1, added1) 297 } 298 } 299 300 func TestBytesAddPrefix(t *testing.T) { 301 // Test case where bytes are added as a prefix 302 bs1 := g.Bytes("world") 303 obs1 := g.NewBytes("hello ") 304 prefixed1 := bs1.AddPrefix(obs1) 305 expected1 := g.Bytes("hello world") 306 if !bytes.Equal(prefixed1, expected1) { 307 t.Errorf("AddPrefix failed. Expected: %s, Got: %s", expected1, prefixed1) 308 } 309 } 310 311 func TestBytesStd(t *testing.T) { 312 // Test case where Bytes is converted to a byte slice 313 bs1 := g.Bytes("hello world") 314 std1 := bs1.Std() 315 expected1 := []byte("hello world") 316 if !bytes.Equal(std1, expected1) { 317 t.Errorf("Std failed. Expected: %v, Got: %v", expected1, std1) 318 } 319 } 320 321 func TestBytesClone(t *testing.T) { 322 // Test case where Bytes is cloned 323 bs1 := g.Bytes("hello world") 324 cloned1 := bs1.Clone() 325 if !bytes.Equal(cloned1, bs1) { 326 t.Errorf("Clone failed. Expected: %s, Got: %s", bs1, cloned1) 327 } 328 } 329 330 func TestBytesContainsAnyChars(t *testing.T) { 331 // Test case where Bytes contains any characters from the input String 332 bs1 := g.Bytes("hello") 333 chars1 := g.String("aeiou") 334 contains1 := bs1.ContainsAnyChars(chars1) 335 if !contains1 { 336 t.Errorf("ContainsAnyChars failed. Expected: true, Got: %t", contains1) 337 } 338 339 // Test case where Bytes doesn't contain any characters from the input String 340 bs2 := g.Bytes("hello") 341 chars2 := g.String("xyz") 342 contains2 := bs2.ContainsAnyChars(chars2) 343 if contains2 { 344 t.Errorf("ContainsAnyChars failed. Expected: false, Got: %t", contains2) 345 } 346 } 347 348 func TestBytesContainsRune(t *testing.T) { 349 // Test case where Bytes contains the specified rune 350 bs1 := g.Bytes("hello") 351 rune1 := 'e' 352 contains1 := bs1.ContainsRune(rune1) 353 if !contains1 { 354 t.Errorf("ContainsRune failed. Expected: true, Got: %t", contains1) 355 } 356 357 // Test case where Bytes doesn't contain the specified rune 358 bs2 := g.Bytes("hello") 359 rune2 := 'x' 360 contains2 := bs2.ContainsRune(rune2) 361 if contains2 { 362 t.Errorf("ContainsRune failed. Expected: false, Got: %t", contains2) 363 } 364 } 365 366 func TestBytesCount(t *testing.T) { 367 // Test case where Bytes contains multiple occurrences of the specified Bytes 368 bs1 := g.Bytes("hello hello hello") 369 obs1 := g.Bytes("hello") 370 count1 := bs1.Count(obs1) 371 expected1 := g.Int(3) 372 if count1 != expected1 { 373 t.Errorf("Count failed. Expected: %d, Got: %d", expected1, count1) 374 } 375 376 // Test case where Bytes doesn't contain the specified Bytes 377 bs2 := g.Bytes("hello") 378 obs2 := g.Bytes("world") 379 count2 := bs2.Count(obs2) 380 expected2 := g.Int(0) 381 if count2 != expected2 { 382 t.Errorf("Count failed. Expected: %d, Got: %d", expected2, count2) 383 } 384 } 385 386 func TestBytesCompare(t *testing.T) { 387 testCases := []struct { 388 bs1 g.Bytes 389 bs2 g.Bytes 390 expected g.Int 391 }{ 392 {[]byte("apple"), []byte("banana"), -1}, 393 {[]byte("banana"), []byte("apple"), 1}, 394 {[]byte("banana"), []byte("banana"), 0}, 395 {[]byte("apple"), []byte("Apple"), 1}, 396 {[]byte(""), []byte(""), 0}, 397 } 398 399 for _, tc := range testCases { 400 result := g.Int(tc.bs1.Cmp(tc.bs2)) 401 if result != tc.expected { 402 t.Errorf( 403 "Bytes.Compare(%q, %q): expected %d, got %d", 404 tc.bs1, 405 tc.bs2, 406 tc.expected, 407 result, 408 ) 409 } 410 } 411 } 412 413 func TestBytesEqFold(t *testing.T) { 414 // Test case where the byte slices are equal regardless of case 415 bs1 := g.Bytes("Hello World") 416 obs1 := g.Bytes("hello world") 417 eqFold1 := bs1.EqFold(obs1) 418 if !eqFold1 { 419 t.Errorf("EqFold failed. Expected: true, Got: %t", eqFold1) 420 } 421 422 // Test case where the byte slices are not equal regardless of case 423 bs2 := g.Bytes("Hello World") 424 obs2 := g.Bytes("gopher") 425 eqFold2 := bs2.EqFold(obs2) 426 if eqFold2 { 427 t.Errorf("EqFold failed. Expected: false, Got: %t", eqFold2) 428 } 429 } 430 431 func TestBytesEq(t *testing.T) { 432 testCases := []struct { 433 bs1 g.Bytes 434 bs2 g.Bytes 435 expected bool 436 }{ 437 {[]byte("apple"), []byte("banana"), false}, 438 {[]byte("banana"), []byte("banana"), true}, 439 {[]byte("Apple"), []byte("apple"), false}, 440 {[]byte(""), []byte(""), true}, 441 } 442 443 for _, tc := range testCases { 444 result := tc.bs1.Eq(tc.bs2) 445 if result != tc.expected { 446 t.Errorf( 447 "Bytes.Eq(%q, %q): expected %t, got %t", 448 tc.bs1, 449 tc.bs2, 450 tc.expected, 451 result, 452 ) 453 } 454 } 455 } 456 457 func TestBytesNe(t *testing.T) { 458 testCases := []struct { 459 bs1 g.Bytes 460 bs2 g.Bytes 461 expected bool 462 }{ 463 {[]byte("apple"), []byte("banana"), true}, 464 {[]byte("banana"), []byte("banana"), false}, 465 {[]byte("Apple"), []byte("apple"), true}, 466 {[]byte(""), []byte(""), false}, 467 } 468 469 for _, tc := range testCases { 470 result := tc.bs1.Ne(tc.bs2) 471 if result != tc.expected { 472 t.Errorf( 473 "Bytes.Ne(%q, %q): expected %t, got %t", 474 tc.bs1, 475 tc.bs2, 476 tc.expected, 477 result, 478 ) 479 } 480 } 481 } 482 483 func TestBytesGt(t *testing.T) { 484 testCases := []struct { 485 bs1 g.Bytes 486 bs2 g.Bytes 487 expected bool 488 }{ 489 {[]byte("apple"), []byte("banana"), false}, 490 {[]byte("banana"), []byte("apple"), true}, 491 {[]byte("Apple"), []byte("apple"), false}, 492 {[]byte("banana"), []byte("banana"), false}, 493 {[]byte(""), []byte(""), false}, 494 } 495 496 for _, tc := range testCases { 497 result := tc.bs1.Gt(tc.bs2) 498 if result != tc.expected { 499 t.Errorf( 500 "Bytes.Gt(%q, %q): expected %t, got %t", 501 tc.bs1, 502 tc.bs2, 503 tc.expected, 504 result, 505 ) 506 } 507 } 508 } 509 510 func TestBytesLt(t *testing.T) { 511 testCases := []struct { 512 bs1 g.Bytes 513 bs2 g.Bytes 514 expected bool 515 }{ 516 {[]byte("apple"), []byte("banana"), true}, 517 {[]byte("banana"), []byte("apple"), false}, 518 {[]byte("Apple"), []byte("apple"), true}, 519 {[]byte("banana"), []byte("banana"), false}, 520 {[]byte(""), []byte(""), false}, 521 } 522 523 for _, tc := range testCases { 524 result := tc.bs1.Lt(tc.bs2) 525 if result != tc.expected { 526 t.Errorf( 527 "Bytes.Lt(%q, %q): expected %t, got %t", 528 tc.bs1, 529 tc.bs2, 530 tc.expected, 531 result, 532 ) 533 } 534 } 535 } 536 537 func TestBytesNormalizeNFC(t *testing.T) { 538 testCases := []struct { 539 input g.Bytes 540 expected g.Bytes 541 }{ 542 {[]byte("Mëtàl Hëàd"), []byte("Mëtàl Hëàd")}, 543 {[]byte("Café"), []byte("Café")}, 544 {[]byte("Ĵūņě"), []byte("Ĵūņě")}, 545 {[]byte("A\u0308"), []byte("Ä")}, 546 {[]byte("o\u0308"), []byte("ö")}, 547 {[]byte("u\u0308"), []byte("ü")}, 548 {[]byte("O\u0308"), []byte("Ö")}, 549 {[]byte("U\u0308"), []byte("Ü")}, 550 } 551 552 for _, tc := range testCases { 553 t.Run("", func(t *testing.T) { 554 output := tc.input.NormalizeNFC() 555 if string(output) != string(tc.expected) { 556 t.Errorf("Bytes.NormalizeNFC(%q) = %q; want %q", tc.input, output, tc.expected) 557 } 558 }) 559 } 560 } 561 562 func TestBytesReader(t *testing.T) { 563 tests := []struct { 564 name string 565 bs g.Bytes 566 expected []byte 567 }{ 568 {"Empty Bytes", g.Bytes{}, []byte{}}, 569 {"Single byte Bytes", g.Bytes{0x41}, []byte{0x41}}, 570 { 571 "Multiple bytes Bytes", 572 g.Bytes{0x48, 0x65, 0x6c, 0x6c, 0x6f}, 573 []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f}, 574 }, 575 { 576 "Bytes with various values", 577 g.Bytes{0x00, 0xff, 0x80, 0x7f}, 578 []byte{0x00, 0xff, 0x80, 0x7f}, 579 }, 580 } 581 582 for _, test := range tests { 583 t.Run(test.name, func(t *testing.T) { 584 reader := test.bs.Reader() 585 resultBytes, err := io.ReadAll(reader) 586 if err != nil { 587 t.Fatalf("Error reading from *bytes.Reader: %v", err) 588 } 589 590 if !bytes.Equal(resultBytes, test.expected) { 591 t.Errorf("Bytes.Reader() content = %v, expected %v", resultBytes, test.expected) 592 } 593 }) 594 } 595 } 596 597 func TestBytesContainsAny(t *testing.T) { 598 testCases := []struct { 599 bs g.Bytes 600 bss []g.Bytes 601 expected bool 602 }{ 603 { 604 bs: g.Bytes("Hello, world!"), 605 bss: []g.Bytes{g.Bytes("world"), g.Bytes("Go")}, 606 expected: true, 607 }, 608 { 609 bs: g.Bytes("Welcome to the HumanGo-1!"), 610 bss: []g.Bytes{g.Bytes("Go-3"), g.Bytes("Go-4")}, 611 expected: false, 612 }, 613 { 614 bs: g.Bytes("Have a great day!"), 615 bss: []g.Bytes{g.Bytes(""), g.Bytes(" ")}, 616 expected: true, 617 }, 618 { 619 bs: g.Bytes(""), 620 bss: []g.Bytes{g.Bytes("Hello"), g.Bytes("world")}, 621 expected: false, 622 }, 623 { 624 bs: g.Bytes(""), 625 bss: []g.Bytes{}, 626 expected: false, 627 }, 628 } 629 630 for _, tc := range testCases { 631 result := tc.bs.ContainsAny(tc.bss...) 632 if result != tc.expected { 633 t.Errorf( 634 "Bytes.ContainsAny(%v, %v) = %v; want %v", 635 tc.bs, 636 tc.bss, 637 result, 638 tc.expected, 639 ) 640 } 641 } 642 } 643 644 func TestBytesContainsAll(t *testing.T) { 645 testCases := []struct { 646 bs g.Bytes 647 bss []g.Bytes 648 expected bool 649 }{ 650 { 651 bs: g.Bytes("Hello, world!"), 652 bss: []g.Bytes{g.Bytes("Hello"), g.Bytes("world")}, 653 expected: true, 654 }, 655 { 656 bs: g.Bytes("Welcome to the HumanGo-1!"), 657 bss: []g.Bytes{g.Bytes("Go-3"), g.Bytes("Go-4")}, 658 expected: false, 659 }, 660 { 661 bs: g.Bytes("Have a great day!"), 662 bss: []g.Bytes{g.Bytes("Have"), g.Bytes("a")}, 663 expected: true, 664 }, 665 { 666 bs: g.Bytes(""), 667 bss: []g.Bytes{g.Bytes("Hello"), g.Bytes("world")}, 668 expected: false, 669 }, 670 { 671 bs: g.Bytes("Hello, world!"), 672 bss: []g.Bytes{}, 673 expected: true, 674 }, 675 } 676 677 for _, tc := range testCases { 678 result := tc.bs.ContainsAll(tc.bss...) 679 if result != tc.expected { 680 t.Errorf( 681 "Bytes.ContainsAll(%v, %v) = %v; want %v", 682 tc.bs, 683 tc.bss, 684 result, 685 tc.expected, 686 ) 687 } 688 } 689 } 690 691 func TestBytesIndex(t *testing.T) { 692 // Test case where obs is present in bs 693 bs := g.Bytes("hello world") 694 obs := g.Bytes("world") 695 idx := bs.Index(obs) 696 expected := g.Int(6) 697 if idx != expected { 698 t.Errorf("Index failed. Expected: %d, Got: %d", expected, idx) 699 } 700 701 // Test case where obs is not present in bs 702 bs = g.Bytes("hello world") 703 obs = g.Bytes("gopher") 704 idx = bs.Index(obs) 705 expected = g.Int(-1) 706 if idx != expected { 707 t.Errorf("Index failed. Expected: %d, Got: %d", expected, idx) 708 } 709 } 710 711 func TestBytesIndexRegexp(t *testing.T) { 712 // Test case where a match is found 713 bs := g.Bytes("apple banana") 714 pattern := regexp.MustCompile(`banana`) 715 idx := bs.IndexRegexp(pattern) 716 expected := g.Some(g.Slice[g.Int]{6, 12}) 717 if idx.IsNone() || !reflect.DeepEqual(idx.Some(), expected.Some()) { 718 t.Errorf("IndexRegexp failed. Expected: %v, Got: %v", expected, idx) 719 } 720 721 // Test case where no match is found 722 bs = g.Bytes("apple banana") 723 pattern = regexp.MustCompile(`orange`) 724 idx = bs.IndexRegexp(pattern) 725 expected = g.None[g.Slice[g.Int]]() 726 if idx.IsSome() || !reflect.DeepEqual(idx.IsNone(), expected.IsNone()) { 727 t.Errorf("IndexRegexp failed. Expected: %v, Got: %v", expected, idx) 728 } 729 } 730 731 func TestBytesRepeat(t *testing.T) { 732 // Test case where the Bytes are repeated 3 times 733 bs := g.Bytes("hello") 734 repeated := bs.Repeat(3) 735 expected := g.Bytes("hellohellohello") 736 if !bytes.Equal(repeated, expected) { 737 t.Errorf("Repeat failed. Expected: %s, Got: %s", expected, repeated) 738 } 739 740 // Test case where the Bytes are repeated 0 times 741 bs = g.Bytes("hello") 742 repeated = bs.Repeat(0) 743 expected = g.Bytes("") 744 if !bytes.Equal(repeated, expected) { 745 t.Errorf("Repeat failed. Expected: %s, Got: %s", expected, repeated) 746 } 747 } 748 749 func TestToRunes(t *testing.T) { 750 // Test case where the Bytes are converted to runes 751 bs := g.Bytes("hello") 752 runes := bs.ToRunes() 753 expected := []rune{'h', 'e', 'l', 'l', 'o'} 754 if !reflect.DeepEqual(runes, expected) { 755 t.Errorf("ToRunes failed. Expected: %v, Got: %v", expected, runes) 756 } 757 } 758 759 func TestBytesLower(t *testing.T) { 760 // Test case where the Bytes are converted to lowercase 761 bs := g.Bytes("Hello World") 762 lower := bs.Lower() 763 expected := g.Bytes("hello world") 764 if !reflect.DeepEqual(lower, expected) { 765 t.Errorf("Lower failed. Expected: %s, Got: %s", expected, lower) 766 } 767 } 768 769 func TestBytesUpper(t *testing.T) { 770 // Test case where the Bytes are converted to uppercase 771 bs := g.Bytes("hello world") 772 upper := bs.Upper() 773 expected := g.Bytes("HELLO WORLD") 774 if !reflect.DeepEqual(upper, expected) { 775 t.Errorf("Upper failed. Expected: %s, Got: %s", expected, upper) 776 } 777 } 778 779 func TestBytesTrimSpace(t *testing.T) { 780 // Test case where white space characters are trimmed from the beginning and end 781 bs := g.Bytes(" hello world ") 782 trimmed := bs.TrimSpace() 783 expected := g.Bytes("hello world") 784 if !bytes.Equal(trimmed, expected) { 785 t.Errorf("TrimSpace failed. Expected: %s, Got: %s", expected, trimmed) 786 } 787 788 // Test case where there are no white space characters 789 bs = g.Bytes("hello world") 790 trimmed = bs.TrimSpace() 791 expected = g.Bytes("hello world") 792 if !bytes.Equal(trimmed, expected) { 793 t.Errorf("TrimSpace failed. Expected: %s, Got: %s", expected, trimmed) 794 } 795 796 // Test case where the Bytes is empty 797 bs = g.Bytes("") 798 trimmed = bs.TrimSpace() 799 expected = g.Bytes("") 800 if !bytes.Equal(trimmed, expected) { 801 t.Errorf("TrimSpace failed. Expected: %s, Got: %s", expected, trimmed) 802 } 803 } 804 805 func TestBytesTitle(t *testing.T) { 806 // Test case where the Bytes are converted to title case 807 bs := g.Bytes("hello world") 808 title := bs.Title() 809 expected := g.Bytes("Hello World") 810 if !reflect.DeepEqual(title, expected) { 811 t.Errorf("Title failed. Expected: %s, Got: %s", expected, title) 812 } 813 814 // Test case where the Bytes are already in title case 815 bs = g.Bytes("Hello World") 816 title = bs.Title() 817 if !reflect.DeepEqual(title, bs) { 818 t.Errorf("Title failed. Expected: %s, Got: %s", bs, title) 819 } 820 821 // Test case where the Bytes are empty 822 bs = g.Bytes("") 823 title = bs.Title() 824 if !reflect.DeepEqual(title, bs) { 825 t.Errorf("Title failed. Expected: %s, Got: %s", bs, title) 826 } 827 } 828 829 func TestBytesNotEmpty(t *testing.T) { 830 // Test case where the Bytes is not empty 831 bs := g.Bytes("hello") 832 if !bs.NotEmpty() { 833 t.Errorf("NotEmpty failed. Expected: true, Got: false") 834 } 835 836 // Test case where the Bytes is empty 837 bs = g.Bytes("") 838 if bs.NotEmpty() { 839 t.Errorf("NotEmpty failed. Expected: false, Got: true") 840 } 841 } 842 843 func TestBytesMap(t *testing.T) { 844 // Test case where the function converts each rune to uppercase 845 bs := g.Bytes("hello") 846 uppercase := bs.Map(func(r rune) rune { 847 return unicode.ToUpper(r) 848 }) 849 850 expected := g.Bytes("HELLO") 851 if !bytes.Equal(uppercase, expected) { 852 t.Errorf("Map failed. Expected: %s, Got: %s", expected, uppercase) 853 } 854 855 // Test case where the function removes spaces 856 bs = g.Bytes("hello world") 857 noSpaces := bs.Map(func(r rune) rune { 858 if unicode.IsSpace(r) { 859 return -1 // Remove rune 860 } 861 return r 862 }) 863 864 expected = g.Bytes("helloworld") 865 if !bytes.Equal(noSpaces, expected) { 866 t.Errorf("Map failed. Expected: %s, Got: %s", expected, noSpaces) 867 } 868 } 869 870 func TestBytesLenRunes(t *testing.T) { 871 // Test case where the Bytes contain ASCII characters 872 bs := g.Bytes("hello world") 873 lenRunes := bs.LenRunes() 874 expected := g.Int(11) 875 if lenRunes != expected { 876 t.Errorf("LenRunes failed. Expected: %d, Got: %d", expected, lenRunes) 877 } 878 879 // Test case where the Bytes contain Unicode characters 880 bs = g.Bytes("你好,世界") 881 lenRunes = bs.LenRunes() 882 expected = g.Int(5) 883 if lenRunes != expected { 884 t.Errorf("LenRunes failed. Expected: %d, Got: %d", expected, lenRunes) 885 } 886 887 // Test case where the Bytes are empty 888 bs = g.Bytes("") 889 lenRunes = bs.LenRunes() 890 expected = g.Int(0) 891 if lenRunes != expected { 892 t.Errorf("LenRunes failed. Expected: %d, Got: %d", expected, lenRunes) 893 } 894 } 895 896 func TestBytesLastIndex(t *testing.T) { 897 // Test case where obs is present in bs 898 bs := g.Bytes("hello world") 899 obs := g.Bytes("o") 900 lastIndex := bs.LastIndex(obs) 901 expected := g.Int(7) 902 if lastIndex != expected { 903 t.Errorf("LastIndex failed. Expected: %d, Got: %d", expected, lastIndex) 904 } 905 906 // Test case where obs is not present in bs 907 bs = g.Bytes("hello world") 908 obs = g.Bytes("z") 909 lastIndex = bs.LastIndex(obs) 910 expected = g.Int(-1) 911 if lastIndex != expected { 912 t.Errorf("LastIndex failed. Expected: %d, Got: %d", expected, lastIndex) 913 } 914 } 915 916 func TestBytesIndexByte(t *testing.T) { 917 // Test case where b is present in bs 918 bs := g.Bytes("hello world") 919 b := byte('o') 920 indexByte := bs.IndexByte(b) 921 expected := g.Int(4) 922 if indexByte != expected { 923 t.Errorf("IndexByte failed. Expected: %d, Got: %d", expected, indexByte) 924 } 925 926 // Test case where b is not present in bs 927 bs = g.Bytes("hello world") 928 b = byte('z') 929 indexByte = bs.IndexByte(b) 930 expected = -1 931 if indexByte != expected { 932 t.Errorf("IndexByte failed. Expected: %d, Got: %d", expected, indexByte) 933 } 934 } 935 936 func TestBytesLastIndexByte(t *testing.T) { 937 // Test case where b is present in bs 938 bs := g.Bytes("hello world") 939 b := byte('o') 940 lastIndexByte := bs.LastIndexByte(b) 941 expected := g.Int(7) 942 if lastIndexByte != expected { 943 t.Errorf("LastIndexByte failed. Expected: %d, Got: %d", expected, lastIndexByte) 944 } 945 946 // Test case where b is not present in bs 947 bs = g.Bytes("hello world") 948 b = byte('z') 949 lastIndexByte = bs.LastIndexByte(b) 950 expected = -1 951 if lastIndexByte != expected { 952 t.Errorf("LastIndexByte failed. Expected: %d, Got: %d", expected, lastIndexByte) 953 } 954 } 955 956 func TestBytesIndexRune(t *testing.T) { 957 // Test case where r is present in bs 958 bs := g.Bytes("hello world") 959 r := 'o' 960 indexRune := bs.IndexRune(r) 961 expected := g.Int(4) 962 if indexRune != expected { 963 t.Errorf("IndexRune failed. Expected: %d, Got: %d", expected, indexRune) 964 } 965 966 // Test case where r is not present in bs 967 bs = g.Bytes("hello world") 968 r = 'z' 969 indexRune = bs.IndexRune(r) 970 expected = -1 971 if indexRune != expected { 972 t.Errorf("IndexRune failed. Expected: %d, Got: %d", expected, indexRune) 973 } 974 } 975 976 func TestBytesFindAllSubmatchRegexpN(t *testing.T) { 977 // Test case where matches are found 978 bs := g.Bytes("hello world") 979 pattern := regexp.MustCompile(`\b\w+\b`) 980 matches := bs.FindAllSubmatchRegexpN(pattern, -1) 981 if matches.IsSome() { 982 expected := g.Slice[g.Slice[g.Bytes]]{ 983 {g.Bytes("hello")}, 984 {g.Bytes("world")}, 985 } 986 if !matches.Some().Eq(expected) { 987 t.Errorf("FindAllSubmatchRegexpN failed. Expected: %s, Got: %s", expected, matches.Some()) 988 } 989 } else { 990 t.Errorf("FindAllSubmatchRegexpN failed. Expected matches, Got None") 991 } 992 993 // Test case where no matches are found 994 bs = g.Bytes("") 995 pattern = regexp.MustCompile(`\b\w+\b`) 996 matches = bs.FindAllSubmatchRegexpN(pattern, -1) 997 if matches.IsSome() { 998 t.Errorf("FindAllSubmatchRegexpN failed. Expected None, Got matches") 999 } 1000 } 1001 1002 func TestBytesFindAllRegexp(t *testing.T) { 1003 // Test case where matches are found 1004 bs := g.Bytes("hello world") 1005 pattern := regexp.MustCompile(`\b\w+\b`) 1006 matches := bs.FindAllRegexp(pattern) 1007 if matches.IsSome() { 1008 expected := g.Slice[g.Bytes]{ 1009 g.Bytes("hello"), 1010 g.Bytes("world"), 1011 } 1012 if !matches.Some().Eq(expected) { 1013 t.Errorf("FindAllRegexp failed. Expected: %s, Got: %s", expected, matches.Some()) 1014 } 1015 } else { 1016 t.Errorf("FindAllRegexp failed. Expected matches, Got None") 1017 } 1018 1019 // Test case where no matches are found 1020 bs = g.Bytes("") 1021 pattern = regexp.MustCompile(`\b\w+\b`) 1022 matches = bs.FindAllRegexp(pattern) 1023 if matches.IsSome() { 1024 t.Errorf("FindAllRegexp failed. Expected None, Got matches") 1025 } 1026 } 1027 1028 func TestBytesFindSubmatchRegexp(t *testing.T) { 1029 // Test case where a match is found 1030 bs := g.Bytes("hello world") 1031 pattern := regexp.MustCompile(`\b\w+\b`) 1032 match := bs.FindSubmatchRegexp(pattern) 1033 if match.IsSome() { 1034 expected := g.SliceOf(g.Bytes("hello")) 1035 if !match.Some().Eq(expected) { 1036 t.Errorf("FindSubmatchRegexp failed. Expected: %s, Got: %s", expected, match.Some()) 1037 } 1038 } else { 1039 t.Errorf("FindSubmatchRegexp failed. Expected match, Got None") 1040 } 1041 1042 // Test case where no match is found 1043 bs = g.Bytes("") 1044 pattern = regexp.MustCompile(`\b\w+\b`) 1045 match = bs.FindSubmatchRegexp(pattern) 1046 if match.IsSome() { 1047 t.Errorf("FindSubmatchRegexp failed. Expected None, Got match") 1048 } 1049 } 1050 1051 func TestBytesFindAllSubmatchRegexp(t *testing.T) { 1052 // Test case where matches are found 1053 bs := g.Bytes("hello world") 1054 pattern := regexp.MustCompile(`\b\w+\b`) 1055 matches := bs.FindAllSubmatchRegexp(pattern) 1056 if matches.IsSome() { 1057 expected := g.Slice[g.Slice[g.Bytes]]{ 1058 {g.Bytes("hello")}, 1059 {g.Bytes("world")}, 1060 } 1061 if !matches.Some().Eq(expected) { 1062 t.Errorf("FindAllSubmatchRegexp failed. Expected: %s, Got: %s", expected, matches.Some()) 1063 } 1064 } else { 1065 t.Errorf("FindAllSubmatchRegexp failed. Expected matches, Got None") 1066 } 1067 1068 // Test case where no matches are found 1069 bs = g.Bytes("") 1070 pattern = regexp.MustCompile(`\b\w+\b`) 1071 matches = bs.FindAllSubmatchRegexp(pattern) 1072 if matches.IsSome() { 1073 t.Errorf("FindAllSubmatchRegexp failed. Expected None, Got matches") 1074 } 1075 } 1076 1077 func TestBytesHashingFunctions(t *testing.T) { 1078 // Test case for MD5 hashing 1079 input := g.Bytes("hello world") 1080 expectedMD5 := g.Bytes("5eb63bbbe01eeed093cb22bb8f5acdc3") 1081 md5Hash := input.Hash().MD5() 1082 if md5Hash.Ne(expectedMD5) { 1083 t.Errorf("MD5 hashing failed. Expected: %s, Got: %s", expectedMD5, md5Hash) 1084 } 1085 1086 // Test case for SHA1 hashing 1087 expectedSHA1 := g.Bytes("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed") 1088 sha1Hash := input.Hash().SHA1() 1089 if sha1Hash.Ne(expectedSHA1) { 1090 t.Errorf("SHA1 hashing failed. Expected: %s, Got: %s", expectedSHA1, sha1Hash) 1091 } 1092 1093 // Test case for SHA256 hashing 1094 expectedSHA256 := g.Bytes("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9") 1095 sha256Hash := input.Hash().SHA256() 1096 if sha256Hash.Ne(expectedSHA256) { 1097 t.Errorf("SHA256 hashing failed. Expected: %s, Got: %s", expectedSHA256, sha256Hash) 1098 } 1099 1100 // Test case for SHA512 hashing 1101 expectedSHA512 := g.Bytes( 1102 "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f", 1103 ) 1104 sha512Hash := input.Hash().SHA512() 1105 if sha512Hash.Ne(expectedSHA512) { 1106 t.Errorf("SHA512 hashing failed. Expected: %s, Got: %s", expectedSHA512, sha512Hash) 1107 } 1108 } 1109 1110 func TestBytesSplitAfter(t *testing.T) { 1111 testCases := []struct { 1112 input g.Bytes 1113 separator g.Bytes 1114 expected g.Slice[g.Bytes] 1115 }{ 1116 { 1117 g.Bytes("hello,world,how,are,you"), 1118 g.Bytes(","), 1119 g.Slice[g.Bytes]{g.Bytes("hello,"), g.Bytes("world,"), g.Bytes("how,"), g.Bytes("are,"), g.Bytes("you")}, 1120 }, 1121 { 1122 g.Bytes("apple banana cherry"), 1123 g.Bytes(" "), 1124 g.Slice[g.Bytes]{g.Bytes("apple "), g.Bytes("banana "), g.Bytes("cherry")}, 1125 }, 1126 1127 { 1128 g.Bytes("a-b-c-d-e"), 1129 g.Bytes("-"), 1130 g.Slice[g.Bytes]{g.Bytes("a-"), g.Bytes("b-"), g.Bytes("c-"), g.Bytes("d-"), g.Bytes("e")}, 1131 }, 1132 {g.Bytes("abcd"), g.Bytes("a"), g.Slice[g.Bytes]{g.Bytes("a"), g.Bytes("bcd")}}, 1133 {g.Bytes("thisistest"), g.Bytes("is"), g.Slice[g.Bytes]{g.Bytes("this"), g.Bytes("is"), g.Bytes("test")}}, 1134 {g.Bytes("☺☻☹"), g.Bytes(""), g.Slice[g.Bytes]{g.Bytes("☺"), g.Bytes("☻"), g.Bytes("☹")}}, 1135 {g.Bytes("☺☻☹"), g.Bytes("☹"), g.Slice[g.Bytes]{g.Bytes("☺☻☹"), g.Bytes("")}}, 1136 {g.Bytes("123"), g.Bytes(""), g.Slice[g.Bytes]{g.Bytes("1"), g.Bytes("2"), g.Bytes("3")}}, 1137 } 1138 1139 for _, tc := range testCases { 1140 actual := tc.input.SplitAfter(tc.separator).Collect() 1141 1142 if !reflect.DeepEqual(actual, tc.expected) { 1143 t.Errorf( 1144 "Unexpected result for input: %s, separator: %s\nExpected: %v\nGot: %v", 1145 tc.input, 1146 tc.separator, 1147 tc.expected, 1148 actual, 1149 ) 1150 } 1151 } 1152 } 1153 1154 func TestBytesFields(t *testing.T) { 1155 bs1 := g.NewBytes("hello world how are you") 1156 expected1 := g.Slice[g.Bytes]{g.Bytes("hello"), g.Bytes("world"), g.Bytes("how"), g.Bytes("are"), g.Bytes("you")} 1157 result1 := bs1.Fields().Collect() 1158 if !reflect.DeepEqual(result1, expected1) { 1159 t.Errorf("Test case 1 failed: Expected %v, got %v", expected1, result1) 1160 } 1161 1162 bs2 := g.NewBytes("hello") 1163 expected2 := g.Slice[g.Bytes]{g.Bytes("hello")} 1164 result2 := bs2.Fields().Collect() 1165 if !reflect.DeepEqual(result2, expected2) { 1166 t.Errorf("Test case 2 failed: Expected %v, got %v", expected2, result2) 1167 } 1168 1169 bs3 := g.NewBytes("") 1170 expected3 := g.Slice[g.Bytes]{} 1171 result3 := bs3.Fields().Collect() 1172 if !reflect.DeepEqual(result3, expected3) { 1173 t.Errorf("Test case 3 failed: Expected %v, got %v", expected3, result3) 1174 } 1175 1176 bs4 := g.NewBytes(" hello world ") 1177 expected4 := g.Slice[g.Bytes]{g.Bytes("hello"), g.Bytes("world")} 1178 result4 := bs4.Fields().Collect() 1179 if !reflect.DeepEqual(result4, expected4) { 1180 t.Errorf("Test case 4 failed: Expected %v, got %v", expected4, result4) 1181 } 1182 } 1183 1184 func TestBytesFieldsBy(t *testing.T) { 1185 testCases := []struct { 1186 input g.Bytes 1187 fn func(r rune) bool 1188 expected g.Slice[g.Bytes] 1189 }{ 1190 {g.Bytes("hello world"), unicode.IsSpace, g.Slice[g.Bytes]{g.Bytes("hello"), g.Bytes("world")}}, 1191 { 1192 g.Bytes("1,2,3,4,5"), 1193 func(r rune) bool { return r == ',' }, 1194 g.Slice[g.Bytes]{g.Bytes("1"), g.Bytes("2"), g.Bytes("3"), g.Bytes("4"), g.Bytes("5")}, 1195 }, 1196 {g.Bytes("camelCcase"), unicode.IsUpper, g.Slice[g.Bytes]{g.Bytes("camel"), g.Bytes("case")}}, 1197 } 1198 1199 for _, tc := range testCases { 1200 actual := tc.input.FieldsBy(tc.fn).Collect() 1201 1202 if !reflect.DeepEqual(actual, tc.expected) { 1203 t.Errorf("Unexpected result for input: %s\nExpected: %v\nGot: %v", tc.input, tc.expected, actual) 1204 } 1205 } 1206 }