github.com/PandaGoAdmin/utils@v0.0.0-20211208134815-d5461603a00f/string_test.go (about) 1 package kgo 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "net/url" 6 "testing" 7 ) 8 9 func TestString_Md5Byte_Md5_IsMd5(t *testing.T) { 10 var res1, res2 []byte 11 var res3, res4 string 12 var chk bool 13 14 res1 = KStr.Md5Byte(bytsHello, 16) 15 assert.Equal(t, len(res1), 16) 16 17 res1 = KStr.Md5Byte(bytsHello, 0) 18 res2 = KStr.Md5Byte(bytsHello, 32) 19 assert.Equal(t, res1, res2) 20 21 res3 = KStr.Md5(strHello, 0) 22 res4 = KStr.Md5(strHello, 32) 23 assert.Equal(t, res3, res4) 24 25 res2 = KStr.Md5Byte(bytsHello) 26 res4 = KStr.Md5(strHello) 27 assert.Equal(t, string(res2), res4) 28 29 res3 = KStr.Md5(strHello, 16) 30 chk = KStr.IsMd5(res3) 31 assert.False(t, chk) 32 33 chk = KStr.IsMd5(res4) 34 assert.True(t, chk) 35 } 36 37 func BenchmarkString_Md5Byte(b *testing.B) { 38 b.ResetTimer() 39 for i := 0; i < b.N; i++ { 40 KStr.Md5Byte(bytsHello) 41 } 42 } 43 44 func BenchmarkString_Md5(b *testing.B) { 45 b.ResetTimer() 46 for i := 0; i < b.N; i++ { 47 KStr.Md5(strHello) 48 } 49 } 50 51 func BenchmarkString_IsMd5(b *testing.B) { 52 b.ResetTimer() 53 for i := 0; i < b.N; i++ { 54 KStr.IsMd5(strHello) 55 } 56 } 57 58 func TestString_ShaXByte_ShaX_IsSha1_IsSha256_IsSha512(t *testing.T) { 59 var res1, res2 []byte 60 var res3, res4 string 61 var chk bool 62 63 res1 = KStr.ShaXByte(bytsHello, 1) 64 res3 = KStr.ShaX(strHello, 1) 65 chk = KStr.IsSha1(res3) 66 assert.Equal(t, res3, string(res1)) 67 assert.True(t, chk) 68 69 res2 = KStr.ShaXByte(bytsHello, 256) 70 res4 = KStr.ShaX(strHello, 256) 71 chk = KStr.IsSha256(res4) 72 assert.Equal(t, res4, string(res2)) 73 assert.True(t, chk) 74 75 res1 = KStr.ShaXByte(bytsHello, 512) 76 res3 = KStr.ShaX(strHello, 512) 77 chk = KStr.IsSha512(res3) 78 assert.Equal(t, res3, string(res1)) 79 assert.True(t, chk) 80 } 81 82 func TestString_ShaXByte_Panic(t *testing.T) { 83 defer func() { 84 r := recover() 85 assert.NotEmpty(t, r) 86 }() 87 KStr.ShaXByte(bytsHello, 32) 88 } 89 90 func TestString_ShaX_Panic(t *testing.T) { 91 defer func() { 92 r := recover() 93 assert.NotEmpty(t, r) 94 }() 95 KStr.ShaX(strHello, 64) 96 } 97 98 func BenchmarkString_ShaXByte1(b *testing.B) { 99 b.ResetTimer() 100 for i := 0; i < b.N; i++ { 101 KStr.ShaXByte(bytsHello, 1) 102 } 103 } 104 105 func BenchmarkString_ShaXByte256(b *testing.B) { 106 b.ResetTimer() 107 for i := 0; i < b.N; i++ { 108 KStr.ShaXByte(bytsHello, 256) 109 } 110 } 111 112 func BenchmarkString_ShaXByte512(b *testing.B) { 113 b.ResetTimer() 114 for i := 0; i < b.N; i++ { 115 KStr.ShaXByte(bytsHello, 512) 116 } 117 } 118 119 func BenchmarkString_ShaX1(b *testing.B) { 120 b.ResetTimer() 121 for i := 0; i < b.N; i++ { 122 KStr.ShaX(strHello, 1) 123 } 124 } 125 126 func BenchmarkString_ShaX256(b *testing.B) { 127 b.ResetTimer() 128 for i := 0; i < b.N; i++ { 129 KStr.ShaX(strHello, 256) 130 } 131 } 132 133 func BenchmarkString_ShaX512(b *testing.B) { 134 b.ResetTimer() 135 for i := 0; i < b.N; i++ { 136 KStr.ShaX(strHello, 512) 137 } 138 } 139 140 func BenchmarkString_IsSha1(b *testing.B) { 141 b.ResetTimer() 142 for i := 0; i < b.N; i++ { 143 KStr.IsSha1(strSha1) 144 } 145 } 146 147 func BenchmarkString_strSha256(b *testing.B) { 148 b.ResetTimer() 149 for i := 0; i < b.N; i++ { 150 KStr.IsSha256(strSha256) 151 } 152 } 153 154 func BenchmarkString_strSha512(b *testing.B) { 155 b.ResetTimer() 156 for i := 0; i < b.N; i++ { 157 KStr.IsSha512(strSha512) 158 } 159 } 160 161 func TestString_AddslashesStripslashes(t *testing.T) { 162 var res1, res2 string 163 164 res1 = KStr.Addslashes(tesStr5) 165 assert.Contains(t, res1, "\\") 166 167 res2 = KStr.Stripslashes(res1) 168 assert.Equal(t, res2, tesStr5) 169 assert.NotContains(t, res2, "\\") 170 171 res2 = KStr.Stripslashes(tesStr6) 172 assert.NotContains(t, res2, '\\') 173 } 174 175 func BenchmarkString_Addslashes(b *testing.B) { 176 b.ResetTimer() 177 for i := 0; i < b.N; i++ { 178 KStr.Addslashes(tesStr5) 179 } 180 } 181 182 func BenchmarkString_Stripslashes(b *testing.B) { 183 b.ResetTimer() 184 for i := 0; i < b.N; i++ { 185 KStr.Stripslashes(tesStr6) 186 } 187 } 188 189 func TestString_JsonEncodeJsonDecode(t *testing.T) { 190 var res1 []byte 191 var res2 []interface{} 192 var err error 193 194 //编码 195 res1, err = KStr.JsonEncode(personMps) 196 assert.Nil(t, err) 197 198 //解码 199 err = KStr.JsonDecode(res1, &res2) 200 assert.Nil(t, err) 201 assert.Equal(t, string(res1), personsArrJson) 202 } 203 204 func BenchmarkString_JsonEncode(b *testing.B) { 205 b.ResetTimer() 206 for i := 0; i < b.N; i++ { 207 _, _ = KStr.JsonEncode(personMps) 208 } 209 } 210 211 func BenchmarkString_JsonDecode(b *testing.B) { 212 b.ResetTimer() 213 var res []interface{} 214 for i := 0; i < b.N; i++ { 215 _ = KStr.JsonDecode([]byte(personsArrJson), &res) 216 } 217 } 218 219 func TestString_Utf8ToGbkGbkToUtf8_IsUtf8IsGbk(t *testing.T) { 220 var res1, res2 []byte 221 var chk1, chk2 bool 222 var err error 223 224 //utf8 -> gbk 225 chk1 = KStr.IsUtf8(bytsUtf8Hello) 226 res1, err = KStr.Utf8ToGbk(bytsUtf8Hello) 227 assert.True(t, chk1) 228 assert.Nil(t, err) 229 230 //gbk -> utf8 231 chk2 = KStr.IsGbk(res1) 232 res2, err = KStr.GbkToUtf8(res1) 233 assert.True(t, chk2) 234 assert.Nil(t, err) 235 236 assert.Equal(t, res1, bytsGbkHello) 237 assert.Equal(t, res2, bytsUtf8Hello) 238 } 239 240 func BenchmarkString_Utf8ToGbk(b *testing.B) { 241 b.ResetTimer() 242 for i := 0; i < b.N; i++ { 243 _, _ = KStr.Utf8ToGbk(bytsUtf8Hello) 244 } 245 } 246 247 func BenchmarkString_GbkToUtf8(b *testing.B) { 248 b.ResetTimer() 249 for i := 0; i < b.N; i++ { 250 _, _ = KStr.GbkToUtf8(bytsGbkHello) 251 } 252 } 253 254 func BenchmarkString_IsUtf8(b *testing.B) { 255 b.ResetTimer() 256 for i := 0; i < b.N; i++ { 257 KStr.IsUtf8(bytsUtf8Hello) 258 } 259 } 260 261 func BenchmarkString_IsGbk(b *testing.B) { 262 b.ResetTimer() 263 for i := 0; i < b.N; i++ { 264 KStr.IsGbk(bytsGbkHello) 265 } 266 } 267 268 func TestString_Nl2br_Br2nl(t *testing.T) { 269 var res1, res2 string 270 271 res1 = KStr.Nl2br(tesStr7) 272 assert.Contains(t, res1, "<br />") 273 274 res2 = KStr.Br2nl(res1) 275 assert.Equal(t, res2, tesStr7) 276 277 res2 = KStr.Br2nl(tesStr8) 278 assert.NotContains(t, res2, "br") 279 assert.NotContains(t, res2, "BR") 280 } 281 282 func BenchmarkString_Nl2br(b *testing.B) { 283 b.ResetTimer() 284 for i := 0; i < b.N; i++ { 285 KStr.Nl2br(tesStr7) 286 } 287 } 288 289 func BenchmarkString_Br2nl(b *testing.B) { 290 b.ResetTimer() 291 for i := 0; i < b.N; i++ { 292 KStr.Br2nl(tesStr8) 293 } 294 } 295 296 func TestString_RemoveSpace(t *testing.T) { 297 var res string 298 299 //移除所有空格 300 res = KStr.RemoveSpace(tesStr9, true) 301 assert.NotContains(t, res, " ") 302 303 //移除连续空格 304 res = KStr.RemoveSpace(tesStr9, false) 305 assert.NotContains(t, res, " ") 306 } 307 308 func BenchmarkString_RemoveSpace(b *testing.B) { 309 b.ResetTimer() 310 for i := 0; i < b.N; i++ { 311 KStr.RemoveSpace(tesStr9, true) 312 } 313 } 314 315 func TestString_StripTags(t *testing.T) { 316 var res string 317 318 res = KStr.StripTags(tesStr10) 319 assert.NotContains(t, res, "script>") 320 } 321 322 func BenchmarkString_StripTags(b *testing.B) { 323 b.ResetTimer() 324 for i := 0; i < b.N; i++ { 325 KStr.StripTags(tesStr10) 326 } 327 } 328 329 func TestString_Html2Text(t *testing.T) { 330 var res string 331 332 res = KStr.Html2Text(tesHtmlDoc) 333 assert.NotEmpty(t, res) 334 assert.NotContains(t, res, "<") 335 assert.NotContains(t, res, ">") 336 } 337 338 func BenchmarkString_Html2Text(b *testing.B) { 339 b.ResetTimer() 340 for i := 0; i < b.N; i++ { 341 KStr.Html2Text(tesHtmlDoc) 342 } 343 } 344 345 func TestString_ParseStr(t *testing.T) { 346 var res map[string]interface{} 347 var err error 348 349 res = KArr.NewStrMapItf() 350 err = KStr.ParseStr(tesUri1, res) 351 assert.Nil(t, err) 352 353 res = KArr.NewStrMapItf() 354 err = KStr.ParseStr(tesUri2, res) 355 assert.Nil(t, err) 356 357 res = KArr.NewStrMapItf() 358 err = KStr.ParseStr(tesUri3, res) 359 assert.Nil(t, err) 360 361 res = KArr.NewStrMapItf() 362 err = KStr.ParseStr(tesUri4, res) 363 assert.Nil(t, err) 364 365 res = KArr.NewStrMapItf() 366 err = KStr.ParseStr(tesUri5, res) 367 assert.Nil(t, err) 368 369 res = KArr.NewStrMapItf() 370 err = KStr.ParseStr(tesUri6, res) 371 assert.Nil(t, err) 372 373 res = KArr.NewStrMapItf() 374 err = KStr.ParseStr(tesUri7, res) 375 assert.Nil(t, err) 376 377 //将不合法的参数名转换 378 res = KArr.NewStrMapItf() 379 err = KStr.ParseStr(tesUri8, res) 380 assert.Nil(t, err) 381 382 //错误的 383 res = KArr.NewStrMapItf() 384 err = KStr.ParseStr(tesUri9, res) 385 assert.NotNil(t, err) 386 387 //错误的 388 res = KArr.NewStrMapItf() 389 err = KStr.ParseStr(tesUri10, res) 390 assert.NotNil(t, err) 391 392 //错误的 393 res = KArr.NewStrMapItf() 394 err = KStr.ParseStr(tesUri11, res) 395 assert.NotNil(t, err) 396 397 res = KArr.NewStrMapItf() 398 err = KStr.ParseStr(tesUri12, res) 399 assert.Nil(t, err) 400 401 res = KArr.NewStrMapItf() 402 err = KStr.ParseStr(tesUri13, res) 403 assert.Nil(t, err) 404 405 res = KArr.NewStrMapItf() 406 err = KStr.ParseStr(tesUri14, res) 407 assert.NotNil(t, err) 408 409 res = KArr.NewStrMapItf() 410 err = KStr.ParseStr(tesUri15, res) 411 assert.NotNil(t, err) 412 413 res = KArr.NewStrMapItf() 414 err = KStr.ParseStr(tesUri16, res) 415 assert.Nil(t, err) 416 417 res = KArr.NewStrMapItf() 418 err = KStr.ParseStr(tesUri17, res) 419 assert.Nil(t, err) 420 421 res = KArr.NewStrMapItf() 422 err = KStr.ParseStr(tesUri18, res) 423 assert.NotNil(t, err) 424 425 res = KArr.NewStrMapItf() 426 err = KStr.ParseStr(tesUri19, res) 427 assert.Nil(t, err) 428 429 res = KArr.NewStrMapItf() 430 err = KStr.ParseStr(tesUri20, res) 431 assert.Nil(t, err) 432 433 //key nvalid URL escape "%" 434 res = KArr.NewStrMapItf() 435 err = KStr.ParseStr(tesUri21, res) 436 assert.NotNil(t, err) 437 438 res = KArr.NewStrMapItf() 439 err = KStr.ParseStr(tesUri22, res) 440 assert.Nil(t, err) 441 442 res = KArr.NewStrMapItf() 443 err = KStr.ParseStr(tesUri23, res) 444 assert.Nil(t, err) 445 446 res = KArr.NewStrMapItf() 447 err = KStr.ParseStr(tesUri24, res) 448 assert.Nil(t, err) 449 450 res = KArr.NewStrMapItf() 451 err = KStr.ParseStr(tesUri25, res) 452 assert.Nil(t, err) 453 454 res = KArr.NewStrMapItf() 455 err = KStr.ParseStr(tesUri26, res) 456 assert.Nil(t, err) 457 458 res = KArr.NewStrMapItf() 459 err = KStr.ParseStr(tesUri27, res) 460 assert.Nil(t, err) 461 462 //key nvalid URL escape "%" 463 res = KArr.NewStrMapItf() 464 err = KStr.ParseStr(tesUri28, res) 465 assert.NotNil(t, err) 466 } 467 468 func BenchmarkString_ParseStr(b *testing.B) { 469 b.ResetTimer() 470 res := KArr.NewStrMapItf() 471 for i := 0; i < b.N; i++ { 472 _ = KStr.ParseStr(tesUri1, res) 473 } 474 } 475 476 func TestString_ParseUrl(t *testing.T) { 477 var res map[string]string 478 var err error 479 var chk bool 480 481 res, err = KStr.ParseUrl(tesUrl01, -1) 482 assert.Nil(t, err) 483 484 res, err = KStr.ParseUrl(strHello, -1) 485 assert.Nil(t, err) 486 487 //错误的URL 488 res, err = KStr.ParseUrl(tesUrl02, -1) 489 assert.NotNil(t, err) 490 assert.Empty(t, res) 491 492 res, err = KStr.ParseUrl(tesUrl01, 1) 493 _, chk = res["scheme"] 494 assert.True(t, chk) 495 496 res, err = KStr.ParseUrl(tesUrl01, 2) 497 _, chk = res["host"] 498 assert.True(t, chk) 499 500 res, err = KStr.ParseUrl(tesUrl01, 4) 501 _, chk = res["port"] 502 assert.True(t, chk) 503 504 res, err = KStr.ParseUrl(tesUrl01, 8) 505 _, chk = res["user"] 506 assert.True(t, chk) 507 508 res, err = KStr.ParseUrl(tesUrl01, 16) 509 _, chk = res["pass"] 510 assert.True(t, chk) 511 512 res, err = KStr.ParseUrl(tesUrl01, 32) 513 _, chk = res["path"] 514 assert.True(t, chk) 515 516 res, err = KStr.ParseUrl(tesUrl01, 64) 517 _, chk = res["query"] 518 assert.True(t, chk) 519 520 res, err = KStr.ParseUrl(tesUrl01, 128) 521 _, chk = res["fragment"] 522 assert.True(t, chk) 523 } 524 525 func BenchmarkString_ParseUrl(b *testing.B) { 526 b.ResetTimer() 527 for i := 0; i < b.N; i++ { 528 _, _ = KStr.ParseUrl(tesUrl01, -1) 529 } 530 } 531 532 func TestString_UrlEncodeUrlDecode(t *testing.T) { 533 var res1, res2 string 534 var err error 535 536 res1 = KStr.UrlEncode(tesStr1) 537 res2, err = KStr.UrlDecode(res1) 538 assert.Equal(t, res2, tesStr1) 539 assert.Nil(t, err) 540 } 541 542 func BenchmarkString_UrlEncode(b *testing.B) { 543 b.ResetTimer() 544 for i := 0; i < b.N; i++ { 545 KStr.UrlEncode(tesStr1) 546 } 547 } 548 549 func BenchmarkString_UrlDecode(b *testing.B) { 550 b.ResetTimer() 551 for i := 0; i < b.N; i++ { 552 _, _ = KStr.UrlDecode(tesStr2) 553 } 554 } 555 556 func TestString_RawUrlEncodeRawUrlDecode(t *testing.T) { 557 var res1, res2 string 558 var err error 559 560 res1 = KStr.RawUrlEncode(tesStr3) 561 res2, err = KStr.RawUrlDecode(res1) 562 assert.Equal(t, res2, tesStr3) 563 assert.Nil(t, err) 564 } 565 566 func BenchmarkString_RawUrlEncode(b *testing.B) { 567 b.ResetTimer() 568 for i := 0; i < b.N; i++ { 569 KStr.RawUrlEncode(tesStr3) 570 } 571 } 572 573 func BenchmarkString_RawUrlDecode(b *testing.B) { 574 b.ResetTimer() 575 for i := 0; i < b.N; i++ { 576 _, _ = KStr.RawUrlDecode(tesStr4) 577 } 578 } 579 580 func TestString_HttpBuildQuery(t *testing.T) { 581 var res string 582 params := url.Values{} 583 params.Add("a", "abc") 584 params.Add("b", "123") 585 params.Add("c", "你好") 586 587 res = KStr.HttpBuildQuery(params) 588 assert.Contains(t, res, "&") 589 } 590 591 func BenchmarkString_HttpBuildQuery(b *testing.B) { 592 b.ResetTimer() 593 params := url.Values{} 594 params.Add("a", "abc") 595 params.Add("b", "123") 596 params.Add("c", "你好") 597 for i := 0; i < b.N; i++ { 598 KStr.HttpBuildQuery(params) 599 } 600 } 601 602 func TestString_FormatUrl(t *testing.T) { 603 var res string 604 605 res = KStr.FormatUrl("") 606 assert.Empty(t, res) 607 608 res = KStr.FormatUrl(tesUrl03) 609 assert.Contains(t, res, "://") 610 611 res = KStr.FormatUrl(tesUrl04) 612 assert.Contains(t, res, "://") 613 614 res = KStr.FormatUrl(tesUrl05) 615 assert.NotContains(t, res, '\\') 616 } 617 618 func BenchmarkString_FormatUrl(b *testing.B) { 619 b.ResetTimer() 620 for i := 0; i < b.N; i++ { 621 KStr.FormatUrl(tesUrl05) 622 } 623 } 624 625 func TestString_GetDomain(t *testing.T) { 626 var tests = []struct { 627 param string 628 isMain bool 629 expected string 630 }{ 631 {"", false, ""}, 632 {strHello, false, ""}, 633 {strSpeedLight, false, ""}, 634 {tesUrl05, false, "login.localhost"}, 635 {tesUrl06, false, "play.golang.com"}, 636 {tesUrl07, true, "github.io"}, 637 {tesUrl08, false, "foobar.中文网"}, 638 {tesUrl09, false, "foobar.com"}, 639 {localIp, false, "127.0.0.1"}, 640 } 641 for _, test := range tests { 642 actual := KStr.GetDomain(test.param, test.isMain) 643 assert.Equal(t, actual, test.expected) 644 } 645 } 646 647 func BenchmarkString_GetDomain(b *testing.B) { 648 b.ResetTimer() 649 for i := 0; i < b.N; i++ { 650 KStr.GetDomain(tesUrl10) 651 } 652 } 653 654 func TestString_ClearUrlPrefix(t *testing.T) { 655 var tests = []struct { 656 url string 657 prefix string 658 expected string 659 }{ 660 {"", "", ""}, 661 {tesUrl10, "https://", "github.com/PandaGoAdmin/utils"}, 662 {tesUrl11, "/", "google.com/test?name=hello"}, 663 } 664 for _, test := range tests { 665 actual := KStr.ClearUrlPrefix(test.url, test.prefix) 666 assert.Equal(t, actual, test.expected) 667 } 668 } 669 670 func BenchmarkString_ClearUrlPrefix(b *testing.B) { 671 b.ResetTimer() 672 for i := 0; i < b.N; i++ { 673 KStr.ClearUrlPrefix(tesUrl10) 674 } 675 } 676 677 func TestString_ClearUrlSuffix(t *testing.T) { 678 var tests = []struct { 679 url string 680 prefix string 681 expected string 682 }{ 683 {"", "", ""}, 684 {tesUrl10, "/kgo", "https://github.com/kakuilan"}, 685 {tesUrl12, "/", "google.com/test?name=hello"}, 686 } 687 for _, test := range tests { 688 actual := KStr.ClearUrlSuffix(test.url, test.prefix) 689 assert.Equal(t, actual, test.expected) 690 } 691 } 692 693 func BenchmarkString_ClearUrlSuffix(b *testing.B) { 694 b.ResetTimer() 695 for i := 0; i < b.N; i++ { 696 KStr.ClearUrlSuffix(tesUrl12) 697 } 698 } 699 700 func TestString_IsEmpty(t *testing.T) { 701 var res bool 702 703 res = KStr.IsEmpty("") 704 assert.True(t, res) 705 706 res = KStr.IsEmpty(" ") 707 assert.True(t, res) 708 709 res = KStr.IsEmpty(strHello) 710 assert.False(t, res) 711 } 712 713 func BenchmarkString_IsEmpty(b *testing.B) { 714 b.ResetTimer() 715 for i := 0; i < b.N; i++ { 716 KStr.IsEmpty(strHello) 717 } 718 } 719 720 func TestString_IsLetters(t *testing.T) { 721 var res bool 722 723 res = KStr.IsLetters(tesStr11) 724 assert.True(t, res) 725 726 res = KStr.IsLetters(tesStr12) 727 assert.False(t, res) 728 729 res = KStr.IsLetters("") 730 assert.False(t, res) 731 } 732 733 func BenchmarkString_IsLetters(b *testing.B) { 734 b.ResetTimer() 735 for i := 0; i < b.N; i++ { 736 KStr.IsLetters(tesStr11) 737 } 738 } 739 740 func TestString_IsUpper(t *testing.T) { 741 var res bool 742 743 res = KStr.IsUpper(tesStr13) 744 assert.True(t, res) 745 746 res = KStr.IsUpper(strHello) 747 assert.False(t, res) 748 749 res = KStr.IsUpper("") 750 assert.False(t, res) 751 } 752 753 func BenchmarkString_IsUpper(b *testing.B) { 754 b.ResetTimer() 755 for i := 0; i < b.N; i++ { 756 KStr.IsUpper(tesStr13) 757 } 758 } 759 760 func TestString_IsLower(t *testing.T) { 761 var res bool 762 763 res = KStr.IsLower(tesStr14) 764 assert.True(t, res) 765 766 res = KStr.IsLower(strHello) 767 assert.False(t, res) 768 769 res = KStr.IsLower("") 770 assert.False(t, res) 771 } 772 773 func BenchmarkString_IsLower(b *testing.B) { 774 b.ResetTimer() 775 for i := 0; i < b.N; i++ { 776 KStr.IsLower(tesStr14) 777 } 778 } 779 780 func TestString_HasLetter(t *testing.T) { 781 var res bool 782 783 res = KStr.HasLetter(strHello) 784 assert.True(t, res) 785 786 res = KStr.HasLetter(strSpeedLight) 787 assert.False(t, res) 788 } 789 790 func BenchmarkString_HasLetter(b *testing.B) { 791 b.ResetTimer() 792 for i := 0; i < b.N; i++ { 793 KStr.HasLetter(strHello) 794 } 795 } 796 797 func TestString_IsASCII(t *testing.T) { 798 var tests = []struct { 799 param string 800 expected bool 801 }{ 802 {"", false}, 803 {tesStr15, false}, 804 {tesStr16, false}, 805 {tesStr17, false}, 806 {utf8Hello, false}, 807 {tesStr18, true}, 808 {otcAstronomicalUnit, true}, 809 {tesEmail1, true}, 810 {strHelloHex, true}, 811 } 812 for _, test := range tests { 813 actual := KStr.IsASCII(test.param) 814 assert.Equal(t, actual, test.expected) 815 } 816 } 817 818 func BenchmarkString_IsASCII(b *testing.B) { 819 b.ResetTimer() 820 for i := 0; i < b.N; i++ { 821 KStr.IsASCII(tesStr11) 822 } 823 } 824 825 func TestString_IsMultibyte(t *testing.T) { 826 var tests = []struct { 827 param string 828 expected bool 829 }{ 830 {"", false}, 831 {tesStr11, false}, 832 {strSpeedLight, false}, 833 {strPunctuation1, false}, 834 {tesEmail1, false}, 835 {strKor, true}, 836 {strNoGbk, true}, 837 {strJap, true}, 838 {strHello, true}, 839 {tesStr16, true}, 840 } 841 for _, test := range tests { 842 actual := KStr.IsMultibyte(test.param) 843 assert.Equal(t, actual, test.expected) 844 } 845 } 846 847 func BenchmarkString_IsMultibyte(b *testing.B) { 848 b.ResetTimer() 849 for i := 0; i < b.N; i++ { 850 KStr.IsMultibyte(strNoGbk) 851 } 852 } 853 854 func TestString_HasFullWidth(t *testing.T) { 855 var tests = []struct { 856 param string 857 expected bool 858 }{ 859 {"", false}, 860 {tesStr11, false}, 861 {strSpeedLight, false}, 862 {tesStr5, false}, 863 {strPunctuation2, false}, 864 {strJap, true}, 865 {strKor, true}, 866 {strHello, true}, 867 {tesStr15, true}, 868 {tesStr16, true}, 869 } 870 for _, test := range tests { 871 actual := KStr.HasFullWidth(test.param) 872 assert.Equal(t, actual, test.expected) 873 } 874 } 875 876 func BenchmarkString_HasFullWidth(b *testing.B) { 877 b.ResetTimer() 878 for i := 0; i < b.N; i++ { 879 KStr.HasFullWidth(strHello) 880 } 881 } 882 883 func TestString_HasHalfWidth(t *testing.T) { 884 var tests = []struct { 885 param string 886 expected bool 887 }{ 888 {"", false}, 889 {tesStr11, true}, 890 {strSpeedLight, true}, 891 {tesStr5, true}, 892 {strPunctuation2, true}, 893 {strJap, false}, 894 {strKor, false}, 895 {strHello, true}, 896 {tesStr15, true}, 897 {tesStr16, false}, 898 } 899 for _, test := range tests { 900 actual := KStr.HasHalfWidth(test.param) 901 assert.Equal(t, actual, test.expected) 902 } 903 } 904 905 func BenchmarkString_HasHalfWidth(b *testing.B) { 906 b.ResetTimer() 907 for i := 0; i < b.N; i++ { 908 KStr.HasHalfWidth(strHello) 909 } 910 } 911 912 func TestString_IsEnglish(t *testing.T) { 913 var tests = []struct { 914 str string 915 cas LkkCaseSwitch 916 expected bool 917 }{ 918 {"", CASE_NONE, false}, 919 {strPi6, CASE_NONE, false}, 920 {strHello, CASE_NONE, false}, 921 {b64Hello, CASE_NONE, false}, 922 {helloEngICase, CASE_NONE, true}, 923 {helloEngICase, 9, true}, 924 {helloEngICase, CASE_LOWER, false}, 925 {helloEngICase, CASE_UPPER, false}, 926 {helloEngLower, CASE_LOWER, true}, 927 {helloEngUpper, CASE_UPPER, true}, 928 } 929 for _, test := range tests { 930 actual := KStr.IsEnglish(test.str, test.cas) 931 assert.Equal(t, actual, test.expected) 932 } 933 } 934 935 func BenchmarkString_IsEnglish(b *testing.B) { 936 b.ResetTimer() 937 for i := 0; i < b.N; i++ { 938 KStr.IsEnglish(helloEngICase, CASE_NONE) 939 } 940 } 941 942 func TestString_HasEnglish(t *testing.T) { 943 var tests = []struct { 944 param string 945 expected bool 946 }{ 947 {"", false}, 948 {strPi6, false}, 949 {utf8Hello, false}, 950 {strHello, true}, 951 {helloEngICase, true}, 952 } 953 for _, test := range tests { 954 actual := KStr.HasEnglish(test.param) 955 assert.Equal(t, actual, test.expected) 956 } 957 } 958 959 func TestString_HasChinese(t *testing.T) { 960 var tests = []struct { 961 param string 962 expected bool 963 }{ 964 {"", false}, 965 {strPi6, false}, 966 {helloEngICase, false}, 967 {strKor, false}, 968 {utf8Hello, true}, 969 {strHello, true}, 970 } 971 for _, test := range tests { 972 actual := KStr.HasChinese(test.param) 973 assert.Equal(t, actual, test.expected) 974 } 975 } 976 977 func BenchmarkString_HasChinese(b *testing.B) { 978 b.ResetTimer() 979 for i := 0; i < b.N; i++ { 980 KStr.HasChinese(strHello) 981 } 982 } 983 984 func TestString_IsChinese(t *testing.T) { 985 var tests = []struct { 986 param string 987 expected bool 988 }{ 989 {"", false}, 990 {strPi6, false}, 991 {helloEngICase, false}, 992 {strKor, false}, 993 {utf8Hello, false}, 994 {helloCn, true}, 995 } 996 for _, test := range tests { 997 actual := KStr.IsChinese(test.param) 998 assert.Equal(t, actual, test.expected) 999 } 1000 } 1001 1002 func BenchmarkString_IsChinese(b *testing.B) { 1003 b.ResetTimer() 1004 for i := 0; i < b.N; i++ { 1005 KStr.IsChinese(helloCn) 1006 } 1007 } 1008 1009 func TestString_IsChineseName(t *testing.T) { 1010 var tests = []struct { 1011 param string 1012 expected bool 1013 }{ 1014 {"", false}, 1015 {strPi6, false}, 1016 {strKor, false}, 1017 {helloEngICase, false}, 1018 {utf8Hello, false}, 1019 {helloCn, true}, 1020 {tesChineseName1, true}, 1021 {tesChineseName2, false}, 1022 {tesChineseName3, true}, 1023 {tesChineseName4, true}, 1024 {tesChineseName5, true}, 1025 {tesChineseName6, true}, 1026 {tesChineseName7, true}, 1027 } 1028 for _, test := range tests { 1029 actual := KStr.IsChineseName(test.param) 1030 assert.Equal(t, actual, test.expected) 1031 } 1032 } 1033 1034 func BenchmarkString_IsChineseName(b *testing.B) { 1035 b.ResetTimer() 1036 for i := 0; i < b.N; i++ { 1037 KStr.IsChineseName(tesChineseName3) 1038 } 1039 } 1040 1041 func TestString_IsWord(t *testing.T) { 1042 var tests = []struct { 1043 str string 1044 expected bool 1045 }{ 1046 {"", false}, 1047 {tesStr19, false}, 1048 {tesStr20, false}, 1049 {tesStr21, false}, 1050 {tesStr12, false}, 1051 {helloCn, true}, 1052 {tesStr13, true}, 1053 {tesStr22, true}, 1054 {tesStr23, true}, 1055 {tesStr24, true}, 1056 {tesStr25, false}, 1057 } 1058 for _, test := range tests { 1059 actual := KStr.IsWord(test.str) 1060 assert.Equal(t, actual, test.expected) 1061 } 1062 } 1063 1064 func BenchmarkString_IsWord(b *testing.B) { 1065 b.ResetTimer() 1066 for i := 0; i < b.N; i++ { 1067 KStr.IsWord(helloCn) 1068 } 1069 } 1070 1071 func TestString_HasSpecialChar(t *testing.T) { 1072 var tests = []struct { 1073 str string 1074 expected bool 1075 }{ 1076 {"", false}, 1077 {helloCn, false}, 1078 {helloEngICase, false}, 1079 {tesStr15, false}, 1080 {tesStr16, false}, 1081 {strHello, true}, 1082 {tesStr12, true}, 1083 {tesStr19, true}, 1084 {tesStr20, true}, 1085 {tesStr26, true}, 1086 {strPunctuation3, true}, 1087 } 1088 for _, test := range tests { 1089 actual := KStr.HasSpecialChar(test.str) 1090 assert.Equal(t, actual, test.expected) 1091 } 1092 } 1093 1094 func BenchmarkString_HasSpecialChar(b *testing.B) { 1095 b.ResetTimer() 1096 for i := 0; i < b.N; i++ { 1097 KStr.HasSpecialChar(strPunctuation3) 1098 } 1099 } 1100 1101 func TestString_IsJSON_Jsonp2Json(t *testing.T) { 1102 var res string 1103 var chk bool 1104 var err error 1105 1106 chk = KStr.IsJSON("") 1107 assert.False(t, chk) 1108 1109 chk = KStr.IsJSON(strHello) 1110 assert.False(t, chk) 1111 1112 chk = KStr.IsJSON(strJson5) 1113 assert.True(t, chk) 1114 1115 chk = KStr.IsJSON(strJson6) 1116 assert.True(t, chk) 1117 1118 res, err = KStr.Jsonp2Json(strJson1) 1119 chk = KStr.IsJSON(res) 1120 assert.True(t, chk) 1121 assert.Nil(t, err) 1122 1123 res, err = KStr.Jsonp2Json(strJson2) 1124 chk = KStr.IsJSON(res) 1125 assert.True(t, chk) 1126 assert.Nil(t, err) 1127 1128 //错误格式 1129 res, err = KStr.Jsonp2Json("") 1130 chk = KStr.IsJSON(res) 1131 assert.False(t, chk) 1132 assert.NotNil(t, err) 1133 1134 res, err = KStr.Jsonp2Json(strHello) 1135 chk = KStr.IsJSON(res) 1136 assert.False(t, chk) 1137 assert.NotNil(t, err) 1138 1139 res, err = KStr.Jsonp2Json(strJson3) 1140 chk = KStr.IsJSON(res) 1141 assert.False(t, chk) 1142 assert.NotNil(t, err) 1143 } 1144 1145 func BenchmarkString_IsJSON(b *testing.B) { 1146 b.ResetTimer() 1147 for i := 0; i < b.N; i++ { 1148 KStr.IsJSON(strJson6) 1149 } 1150 } 1151 1152 func BenchmarkString_Jsonp2Json(b *testing.B) { 1153 b.ResetTimer() 1154 for i := 0; i < b.N; i++ { 1155 _, _ = KStr.Jsonp2Json(strJson4) 1156 } 1157 } 1158 1159 func TestString_IsNumeric(t *testing.T) { 1160 var tests = []struct { 1161 str string 1162 expected bool 1163 }{ 1164 {"", false}, 1165 {helloCn, false}, 1166 {helloEngICase, false}, 1167 {strSpeedLight, true}, 1168 {strPi6, true}, 1169 } 1170 for _, test := range tests { 1171 actual := KStr.IsNumeric(test.str) 1172 assert.Equal(t, actual, test.expected) 1173 } 1174 } 1175 1176 func BenchmarkString_IsNumeric(b *testing.B) { 1177 b.ResetTimer() 1178 for i := 0; i < b.N; i++ { 1179 KStr.IsNumeric(strPi6) 1180 } 1181 } 1182 1183 func TestString_IsAlphaNumeric(t *testing.T) { 1184 var tests = []struct { 1185 str string 1186 expected bool 1187 }{ 1188 {"", false}, 1189 {strHello, false}, 1190 {helloCn, false}, 1191 {strPi6, false}, 1192 {helloEngICase, true}, 1193 {strSpeedLight, true}, 1194 {tesStr27, true}, 1195 } 1196 for _, test := range tests { 1197 actual := KStr.IsAlphaNumeric(test.str) 1198 assert.Equal(t, actual, test.expected) 1199 } 1200 } 1201 1202 func BenchmarkString_IsAlphaNumeric(b *testing.B) { 1203 b.ResetTimer() 1204 for i := 0; i < b.N; i++ { 1205 KStr.IsAlphaNumeric(tesStr27) 1206 } 1207 } 1208 1209 func TestString_IsIP(t *testing.T) { 1210 var tests = []struct { 1211 param string 1212 expected bool 1213 }{ 1214 {"", false}, 1215 {localIp, true}, 1216 {noneIp, true}, 1217 {lanIp, true}, 1218 {dockerIp, true}, 1219 {publicIp1, true}, 1220 {publicIp2, true}, 1221 {tesIp1, true}, 1222 {tesIp2, true}, 1223 {tesIp3, true}, 1224 {tesIp4, false}, 1225 } 1226 for _, test := range tests { 1227 actual := KStr.IsIP(test.param) 1228 assert.Equal(t, actual, test.expected) 1229 } 1230 } 1231 1232 func BenchmarkString_IsIP(b *testing.B) { 1233 b.ResetTimer() 1234 for i := 0; i < b.N; i++ { 1235 KStr.IsIP(lanIp) 1236 } 1237 } 1238 1239 func TestString_IsIPv4(t *testing.T) { 1240 var tests = []struct { 1241 param string 1242 expected bool 1243 }{ 1244 {"", false}, 1245 {localIp, true}, 1246 {noneIp, true}, 1247 {lanIp, true}, 1248 {baiduIpv4, true}, 1249 {googleIpv4, true}, 1250 {googleIpv6, false}, 1251 {tesIp2, false}, 1252 {tesIp4, false}, 1253 {tesIp5, false}, 1254 {tesIp6, false}, 1255 {tesIp7, false}, 1256 } 1257 for _, test := range tests { 1258 actual := KStr.IsIPv4(test.param) 1259 assert.Equal(t, actual, test.expected) 1260 } 1261 } 1262 1263 func BenchmarkString_IsIPv4(b *testing.B) { 1264 b.ResetTimer() 1265 for i := 0; i < b.N; i++ { 1266 KStr.IsIPv4(googleIpv4) 1267 } 1268 } 1269 1270 func TestString_IsIPv6(t *testing.T) { 1271 var tests = []struct { 1272 param string 1273 expected bool 1274 }{ 1275 {"", false}, 1276 {localIp, false}, 1277 {noneIp, false}, 1278 {lanIp, false}, 1279 {baiduIpv4, false}, 1280 {googleIpv4, false}, 1281 {googleIpv6, true}, 1282 {tesIp2, true}, 1283 {tesIp4, false}, 1284 {tesIp5, false}, 1285 {tesIp6, true}, 1286 {tesIp7, true}, 1287 } 1288 for _, test := range tests { 1289 actual := KStr.IsIPv6(test.param) 1290 assert.Equal(t, actual, test.expected) 1291 } 1292 } 1293 1294 func BenchmarkString_IsIPv6(b *testing.B) { 1295 b.ResetTimer() 1296 for i := 0; i < b.N; i++ { 1297 KStr.IsIPv6(googleIpv6) 1298 } 1299 } 1300 1301 func TestString_IsDNSName(t *testing.T) { 1302 var tests = []struct { 1303 param string 1304 expected bool 1305 }{ 1306 {strHello, false}, 1307 {localIp, false}, 1308 {localHost, true}, 1309 {tesDomain01, false}, 1310 {tesDomain02, false}, 1311 {tesDomain03, true}, 1312 {tesDomain04, true}, 1313 {tesDomain05, false}, 1314 {tesDomain06, true}, 1315 {tesDomain07, true}, 1316 {tesDomain08, false}, 1317 {tesDomain09, true}, 1318 {tesDomain10, true}, 1319 {tesDomain11, false}, 1320 {tesDomain12, true}, 1321 {tesDomain13, false}, 1322 {tesDomain14, true}, 1323 {tesDomain15, false}, 1324 {tesDomain16, true}, 1325 {tesDomain17, false}, 1326 {tesDomain18, false}, 1327 {tesDomain19, true}, 1328 {tesDomain20, false}, 1329 {tesDomain21, false}, 1330 {tesDomain22, true}, 1331 } 1332 1333 for _, test := range tests { 1334 actual := KStr.IsDNSName(test.param) 1335 assert.Equal(t, actual, test.expected) 1336 } 1337 } 1338 1339 func BenchmarkString_IsDNSName(b *testing.B) { 1340 b.ResetTimer() 1341 for i := 0; i < b.N; i++ { 1342 KStr.IsDNSName(tesDomain22) 1343 } 1344 } 1345 1346 func TestString_IsDialAddr(t *testing.T) { 1347 var tests = []struct { 1348 param string 1349 expected bool 1350 }{ 1351 {localHost, false}, 1352 {tesDomain23, true}, 1353 {tesDomain24, true}, 1354 {tesDomain25, true}, 1355 {tesDomain26, false}, 1356 {tesDomain27, false}, 1357 {tesDomain28, false}, 1358 {tesDomain29, false}, 1359 } 1360 1361 for _, test := range tests { 1362 actual := KStr.IsDialAddr(test.param) 1363 assert.Equal(t, actual, test.expected) 1364 } 1365 } 1366 1367 func BenchmarkString_IsDialAddr(b *testing.B) { 1368 b.ResetTimer() 1369 for i := 0; i < b.N; i++ { 1370 KStr.IsDialAddr(tesDomain23) 1371 } 1372 } 1373 1374 func TestString_IsMACAddr(t *testing.T) { 1375 var tests = []struct { 1376 param string 1377 expected bool 1378 }{ 1379 {"", false}, 1380 {strHello, false}, 1381 {helloEngICase, false}, 1382 {tesMac01, false}, 1383 {tesMac02, false}, 1384 {tesMac03, true}, 1385 {tesMac04, true}, 1386 {tesMac05, true}, 1387 {tesMac06, true}, 1388 {tesMac07, true}, 1389 {tesMac08, true}, 1390 {tesMac09, true}, 1391 {tesMac10, true}, 1392 {tesMac11, true}, 1393 {tesMac12, true}, 1394 {tesMac13, true}, 1395 {tesMac14, true}, 1396 } 1397 for _, test := range tests { 1398 actual := KStr.IsMACAddr(test.param) 1399 assert.Equal(t, actual, test.expected) 1400 } 1401 } 1402 1403 func BenchmarkString_IsMACAddr(b *testing.B) { 1404 b.ResetTimer() 1405 for i := 0; i < b.N; i++ { 1406 KStr.IsMACAddr(tesMac14) 1407 } 1408 } 1409 1410 func TestString_IsHost(t *testing.T) { 1411 var tests = []struct { 1412 param string 1413 expected bool 1414 }{ 1415 {"", false}, 1416 {strHello, false}, 1417 {localIp, true}, 1418 {localHost, true}, 1419 {tesDomain06, true}, 1420 {tesIp3, true}, 1421 {tesIp2, true}, 1422 {tesDomain22, true}, 1423 {tesDomain08, false}, 1424 {tesDomain13, false}, 1425 {tesDomain20, false}, 1426 {tesDomain28, false}, 1427 } 1428 for _, test := range tests { 1429 actual := KStr.IsHost(test.param) 1430 assert.Equal(t, actual, test.expected) 1431 } 1432 } 1433 1434 func BenchmarkString_IsHost(b *testing.B) { 1435 b.ResetTimer() 1436 for i := 0; i < b.N; i++ { 1437 KStr.IsHost(localHost) 1438 } 1439 } 1440 1441 func TestString_IsEmail(t *testing.T) { 1442 var res bool 1443 var err error 1444 1445 //长度验证 1446 res, _ = KStr.IsEmail(tesEmail2, false) 1447 assert.False(t, res) 1448 res, _ = KStr.IsEmail(tesEmail3, false) 1449 assert.False(t, res) 1450 1451 //无效的格式 1452 res, _ = KStr.IsEmail(tesEmail4, false) 1453 assert.False(t, res) 1454 1455 //不验证主机 1456 res, _ = KStr.IsEmail(tesEmail1, false) 1457 assert.True(t, res) 1458 res, _ = KStr.IsEmail(tesEmail7, false) 1459 assert.True(t, res) 1460 1461 //有效的账号 1462 res, err = KStr.IsEmail(tesEmail6, true) 1463 assert.True(t, res) 1464 assert.Nil(t, err) 1465 res, err = KStr.IsEmail(tesEmail8, true) 1466 assert.True(t, res) 1467 assert.Nil(t, err) 1468 1469 //无效的域名 1470 _, _ = KStr.IsEmail(tesEmail5, true) 1471 } 1472 1473 func BenchmarkString_IsEmail(b *testing.B) { 1474 b.ResetTimer() 1475 for i := 0; i < b.N; i++ { 1476 _, _ = KStr.IsEmail(tesEmail1, false) 1477 } 1478 } 1479 1480 func TestString_Random(t *testing.T) { 1481 var res string 1482 var chk bool 1483 1484 res = KStr.Random(0, RAND_STRING_ALPHA) 1485 assert.Empty(t, res) 1486 1487 //字母 1488 res = KStr.Random(6, RAND_STRING_ALPHA) 1489 chk = KStr.IsLetters(res) 1490 assert.True(t, chk) 1491 1492 res = KStr.Random(6, 90) 1493 chk = KStr.IsLetters(res) 1494 assert.True(t, chk) 1495 1496 //数字 1497 res = KStr.Random(6, RAND_STRING_NUMERIC) 1498 chk = KStr.IsNumeric(res) 1499 assert.True(t, chk) 1500 1501 //字母数字 1502 res = KStr.Random(6, RAND_STRING_ALPHANUM) 1503 chk = KStr.IsAlphaNumeric(res) 1504 assert.True(t, chk) 1505 1506 //有特殊字符 1507 res = KStr.Random(32, RAND_STRING_SPECIAL) 1508 chk = KStr.IsAlphaNumeric(res) 1509 if !chk { 1510 chk = KStr.HasSpecialChar(res) 1511 assert.True(t, chk) 1512 } 1513 1514 //中文 1515 res = KStr.Random(6, RAND_STRING_CHINESE) 1516 chk = KStr.IsChinese(res) 1517 assert.True(t, chk) 1518 } 1519 1520 func BenchmarkString_Random_Alpha(b *testing.B) { 1521 b.ResetTimer() 1522 for i := 0; i < b.N; i++ { 1523 KStr.Random(6, RAND_STRING_ALPHA) 1524 } 1525 } 1526 1527 func BenchmarkString_Random_Numeric(b *testing.B) { 1528 b.ResetTimer() 1529 for i := 0; i < b.N; i++ { 1530 KStr.Random(6, RAND_STRING_NUMERIC) 1531 } 1532 } 1533 1534 func BenchmarkString_Random_Alphanum(b *testing.B) { 1535 b.ResetTimer() 1536 for i := 0; i < b.N; i++ { 1537 KStr.Random(6, RAND_STRING_ALPHANUM) 1538 } 1539 } 1540 1541 func BenchmarkString_Random_Special(b *testing.B) { 1542 b.ResetTimer() 1543 for i := 0; i < b.N; i++ { 1544 KStr.Random(6, RAND_STRING_SPECIAL) 1545 } 1546 } 1547 1548 func BenchmarkString_Random_Chinese(b *testing.B) { 1549 b.ResetTimer() 1550 for i := 0; i < b.N; i++ { 1551 KStr.Random(6, RAND_STRING_CHINESE) 1552 } 1553 } 1554 1555 func TestString_IsMobilecn(t *testing.T) { 1556 var tests = []struct { 1557 param string 1558 expected bool 1559 }{ 1560 {"", false}, 1561 {strHello, false}, 1562 {tesMobilecn1, true}, 1563 {tesMobilecn2, true}, 1564 {tesMobilecn3, true}, 1565 {tesMobilecn4, true}, 1566 {tesMobilecn5, false}, 1567 } 1568 for _, test := range tests { 1569 actual := KStr.IsMobilecn(test.param) 1570 assert.Equal(t, actual, test.expected) 1571 } 1572 } 1573 1574 func BenchmarkString_IsMobilecn(b *testing.B) { 1575 b.ResetTimer() 1576 for i := 0; i < b.N; i++ { 1577 KStr.IsMobilecn(tesMobilecn1) 1578 } 1579 } 1580 1581 func TestString_IsTel(t *testing.T) { 1582 var tests = []struct { 1583 param string 1584 expected bool 1585 }{ 1586 {"", false}, 1587 {strHello, false}, 1588 {tesTel01, false}, 1589 {tesTel02, true}, 1590 {tesTel03, true}, 1591 {tesTel04, true}, 1592 {tesTel05, true}, 1593 {tesTel06, true}, 1594 {tesTel07, true}, 1595 {tesTel08, true}, 1596 {tesTel09, true}, 1597 {tesTel10, true}, 1598 {tesTel11, true}, 1599 {tesTel12, true}, 1600 {tesTel13, false}, 1601 {tesTel14, false}, 1602 {tesTel15, true}, 1603 {tesTel16, true}, 1604 } 1605 for _, test := range tests { 1606 actual := KStr.IsTel(test.param) 1607 assert.Equal(t, actual, test.expected) 1608 } 1609 } 1610 1611 func BenchmarkString_IsTel(b *testing.B) { 1612 b.ResetTimer() 1613 for i := 0; i < b.N; i++ { 1614 KStr.IsTel(tesTel02) 1615 } 1616 } 1617 1618 func TestString_IsPhone(t *testing.T) { 1619 var tests = []struct { 1620 param string 1621 expected bool 1622 }{ 1623 {"", false}, 1624 {strHello, false}, 1625 {tesTel01, false}, 1626 {tesTel02, true}, 1627 {tesMobilecn1, true}, 1628 } 1629 for _, test := range tests { 1630 actual := KStr.IsPhone(test.param) 1631 assert.Equal(t, actual, test.expected) 1632 } 1633 } 1634 1635 func BenchmarkString_IsPhone(b *testing.B) { 1636 b.ResetTimer() 1637 for i := 0; i < b.N; i++ { 1638 KStr.IsPhone(tesTel02) 1639 } 1640 } 1641 1642 func TestString_IsCreditNo(t *testing.T) { 1643 var tests = []struct { 1644 param string 1645 expected bool 1646 }{ 1647 {"", false}, 1648 {strHello, false}, 1649 {tesCredno01, false}, 1650 {tesCredno02, true}, 1651 {tesCredno03, true}, 1652 {tesCredno04, true}, 1653 {tesCredno05, false}, 1654 {tesCredno06, true}, 1655 {tesCredno07, false}, 1656 {tesCredno08, true}, 1657 {tesCredno09, true}, 1658 {tesCredno10, true}, 1659 {tesCredno11, true}, 1660 {tesCredno12, false}, 1661 {tesCredno13, false}, 1662 {tesCredno14, false}, 1663 {tesCredno15, true}, 1664 {tesCredno16, true}, 1665 } 1666 for _, test := range tests { 1667 chk, _ := KStr.IsCreditNo(test.param) 1668 assert.Equal(t, chk, test.expected) 1669 } 1670 } 1671 1672 func BenchmarkString_IsCreditNo(b *testing.B) { 1673 b.ResetTimer() 1674 for i := 0; i < b.N; i++ { 1675 KStr.IsCreditNo(tesCredno02) 1676 } 1677 } 1678 1679 func TestString_IsHexcolor(t *testing.T) { 1680 var tests = []struct { 1681 param string 1682 expected bool 1683 }{ 1684 {"", false}, 1685 {strHello, false}, 1686 {tesColor01, false}, 1687 {tesColor02, false}, 1688 {tesColor03, false}, 1689 {tesColor04, true}, 1690 {tesColor05, true}, 1691 {tesColor06, true}, 1692 {tesColor07, true}, 1693 {tesColor08, true}, 1694 } 1695 for _, test := range tests { 1696 actual, _ := KStr.IsHexcolor(test.param) 1697 assert.Equal(t, actual, test.expected) 1698 } 1699 } 1700 1701 func BenchmarkString_IsHexcolor(b *testing.B) { 1702 b.ResetTimer() 1703 for i := 0; i < b.N; i++ { 1704 _, _ = KStr.IsHexcolor(tesColor08) 1705 } 1706 } 1707 1708 func TestString_IsRGBcolor(t *testing.T) { 1709 var tests = []struct { 1710 param string 1711 expected bool 1712 }{ 1713 {"", false}, 1714 {strHello, false}, 1715 {tesColor09, true}, 1716 {tesColor10, true}, 1717 {tesColor11, true}, 1718 {tesColor12, false}, 1719 {tesColor13, false}, 1720 {tesColor14, false}, 1721 {tesColor15, false}, 1722 } 1723 for _, test := range tests { 1724 actual := KStr.IsRGBcolor(test.param) 1725 assert.Equal(t, actual, test.expected) 1726 } 1727 } 1728 1729 func BenchmarkString_IsRGBcolor(b *testing.B) { 1730 b.ResetTimer() 1731 for i := 0; i < b.N; i++ { 1732 KStr.IsRGBcolor(tesColor11) 1733 } 1734 } 1735 1736 func TestString_IsBlank(t *testing.T) { 1737 var tests = []struct { 1738 param string 1739 expected bool 1740 }{ 1741 {"", true}, 1742 {blankChars, true}, 1743 {"0", false}, 1744 {strHello, false}, 1745 } 1746 for _, test := range tests { 1747 actual := KStr.IsBlank(test.param) 1748 assert.Equal(t, actual, test.expected) 1749 } 1750 } 1751 1752 func BenchmarkString_IsBlank(b *testing.B) { 1753 b.ResetTimer() 1754 for i := 0; i < b.N; i++ { 1755 KStr.IsBlank(blankChars) 1756 } 1757 } 1758 1759 func TestString_IsWhitespaces(t *testing.T) { 1760 var tests = []struct { 1761 param string 1762 expected bool 1763 }{ 1764 {strHello, false}, 1765 {"", false}, 1766 {tesStr28, true}, 1767 {tesStr29, true}, 1768 {tesStr30, true}, 1769 {tesStr31, false}, 1770 {tesStr32, true}, 1771 {tesStr33, false}, 1772 {tesStr34, true}, 1773 } 1774 for _, test := range tests { 1775 actual := KStr.IsWhitespaces(test.param) 1776 assert.Equal(t, actual, test.expected) 1777 } 1778 } 1779 1780 func BenchmarkString_IsWhitespaces(b *testing.B) { 1781 b.ResetTimer() 1782 for i := 0; i < b.N; i++ { 1783 KStr.IsWhitespaces(tesStr30) 1784 } 1785 } 1786 1787 func TestString_HasWhitespace(t *testing.T) { 1788 var tests = []struct { 1789 param string 1790 expected bool 1791 }{ 1792 {strHello, true}, 1793 {helloEngICase, false}, 1794 {"", false}, 1795 {tesStr28, true}, 1796 {tesStr29, true}, 1797 {tesStr30, true}, 1798 {tesStr31, true}, 1799 {tesStr32, true}, 1800 {tesStr33, true}, 1801 {tesStr34, true}, 1802 } 1803 for _, test := range tests { 1804 actual := KStr.HasWhitespace(test.param) 1805 assert.Equal(t, actual, test.expected) 1806 } 1807 } 1808 1809 func BenchmarkString_HasWhitespace(b *testing.B) { 1810 b.ResetTimer() 1811 for i := 0; i < b.N; i++ { 1812 KStr.HasWhitespace(strHello) 1813 } 1814 } 1815 1816 func TestString_IsBase64(t *testing.T) { 1817 var tests = []struct { 1818 param string 1819 expected bool 1820 }{ 1821 {"", false}, 1822 {strHello, false}, 1823 {tesBase64_01, false}, 1824 {tesBase64_02, true}, 1825 {tesBase64_03, true}, 1826 {tesBase64_04, true}, 1827 {tesBase64_05, true}, 1828 } 1829 for _, test := range tests { 1830 actual := KStr.IsBase64(test.param) 1831 assert.Equal(t, actual, test.expected) 1832 } 1833 } 1834 1835 func BenchmarkString_IsBase64(b *testing.B) { 1836 b.ResetTimer() 1837 for i := 0; i < b.N; i++ { 1838 KStr.IsBase64(tesBase64_02) 1839 } 1840 } 1841 1842 func TestString_IsBase64Image(t *testing.T) { 1843 var tests = []struct { 1844 param string 1845 expected bool 1846 }{ 1847 {"", false}, 1848 {strHello, false}, 1849 {tesBase64_06, false}, 1850 {tesBase64_07, false}, 1851 {tesBase64_08, false}, 1852 {tesBase64_09, true}, 1853 {tesBase64_10, false}, 1854 {tesBase64_11, true}, 1855 {tesBase64_12, false}, 1856 } 1857 for _, test := range tests { 1858 actual := KStr.IsBase64Image(test.param) 1859 assert.Equal(t, actual, test.expected) 1860 } 1861 } 1862 1863 func BenchmarkString_IsBase64Image(b *testing.B) { 1864 b.ResetTimer() 1865 for i := 0; i < b.N; i++ { 1866 KStr.IsBase64Image(tesBase64_11) 1867 } 1868 } 1869 1870 func TestString_IsRsaPublicKey(t *testing.T) { 1871 var tests = []struct { 1872 rsastr string 1873 keylen uint16 1874 expected bool 1875 }{ 1876 {strHello, 2048, false}, 1877 {tesRsaPubKey01, 2048, true}, 1878 {tesRsaPubKey01, 1024, false}, 1879 {tesRsaPubKey02, 4096, false}, 1880 {tesRsaPubKey03, 1024, false}, 1881 {tesRsaPubKey04, 2048, false}, 1882 {tesRsaPubKey05, 2048, false}, 1883 } 1884 for _, test := range tests { 1885 actual := KStr.IsRsaPublicKey(test.rsastr, test.keylen) 1886 assert.Equal(t, actual, test.expected) 1887 } 1888 } 1889 1890 func BenchmarkString_IsRsaPublicKey(b *testing.B) { 1891 b.ResetTimer() 1892 for i := 0; i < b.N; i++ { 1893 KStr.IsRsaPublicKey(tesRsaPubKey01, 2048) 1894 } 1895 } 1896 1897 func TestString_IsUrl(t *testing.T) { 1898 //并行测试 1899 t.Parallel() 1900 1901 var tests = []struct { 1902 param string 1903 expected bool 1904 }{ 1905 {"", false}, 1906 {strHello, false}, 1907 {tesUrl01, true}, 1908 {tesUrl02, false}, 1909 {tesUrl04, false}, 1910 {tesUrl05, false}, 1911 {tesUrl06, true}, 1912 {tesUrl07, true}, 1913 {tesUrl08, true}, 1914 {tesUrl10, true}, 1915 {tesUrl11, false}, 1916 {tesUrl13, false}, 1917 {tesUrl14, true}, 1918 {tesUrl15, true}, 1919 {tesUrl16, true}, 1920 {tesUrl17, true}, 1921 {tesUrl18, true}, 1922 {tesUrl19, true}, 1923 {tesUrl20, true}, 1924 {tesUrl21, true}, 1925 {tesUrl22, true}, 1926 {tesUrl23, true}, 1927 {tesUrl24, true}, 1928 {tesUrl25, true}, 1929 {tesUrl26, true}, 1930 {tesUrl27, true}, 1931 {tesUrl28, true}, 1932 {tesUrl29, true}, 1933 {tesUrl30, true}, 1934 {tesUrl31, true}, 1935 {tesUrl32, true}, 1936 {tesUrl33, true}, 1937 {tesUrl34, false}, 1938 {tesUrl35, true}, 1939 {tesUrl36, true}, 1940 {tesUrl37, true}, 1941 {tesUrl38, true}, 1942 } 1943 for _, test := range tests { 1944 actual := KStr.IsUrl(test.param) 1945 assert.Equal(t, actual, test.expected) 1946 } 1947 } 1948 1949 func BenchmarkString_IsUrl(b *testing.B) { 1950 b.ResetTimer() 1951 for i := 0; i < b.N; i++ { 1952 KStr.IsUrl(tesUrl01) 1953 } 1954 } 1955 1956 func TestString_IsUrlExists(t *testing.T) { 1957 var tests = []struct { 1958 param string 1959 expected bool 1960 }{ 1961 {"", false}, 1962 {strHello, false}, 1963 {tesUrl05, false}, 1964 {tesUrl39, true}, 1965 } 1966 for _, test := range tests { 1967 actual := KStr.IsUrlExists(test.param) 1968 assert.Equal(t, actual, test.expected) 1969 } 1970 } 1971 1972 func BenchmarkString_IsUrlExists(b *testing.B) { 1973 b.ResetTimer() 1974 for i := 0; i < b.N; i++ { 1975 KStr.IsUrlExists(tesUrl10) 1976 } 1977 } 1978 1979 func TestString_Strrpos(t *testing.T) { 1980 var tests = []struct { 1981 str string 1982 needle string 1983 offset int 1984 expected int 1985 }{ 1986 {"", "world", 0, -1}, 1987 {helloEng, "world", 0, 6}, 1988 {helloEng, "world", 1, 6}, 1989 {helloEng, "world", -1, 6}, 1990 {helloEng, "World", 0, -1}, 1991 } 1992 for _, test := range tests { 1993 actual := KStr.Strrpos(test.str, test.needle, test.offset) 1994 assert.Equal(t, actual, test.expected) 1995 } 1996 } 1997 1998 func BenchmarkString_Strrpos(b *testing.B) { 1999 b.ResetTimer() 2000 for i := 0; i < b.N; i++ { 2001 KStr.Strrpos(helloEng, "world", 0) 2002 } 2003 } 2004 2005 func TestString_Strripos(t *testing.T) { 2006 var tests = []struct { 2007 str string 2008 needle string 2009 offset int 2010 expected int 2011 }{ 2012 {"", "world", 0, -1}, 2013 {helloEng, "world", 0, 6}, 2014 {helloEng, "world", 1, 6}, 2015 {helloEng, "world", -1, 6}, 2016 {helloEng, "World", 0, 6}, 2017 {helloEng, "haha", 0, -1}, 2018 } 2019 for _, test := range tests { 2020 actual := KStr.Strripos(test.str, test.needle, test.offset) 2021 assert.Equal(t, actual, test.expected) 2022 } 2023 } 2024 2025 func BenchmarkString_Strripos(b *testing.B) { 2026 b.ResetTimer() 2027 for i := 0; i < b.N; i++ { 2028 KStr.Strripos(helloEng, "World", 0) 2029 } 2030 } 2031 2032 func TestString_Ucfirst(t *testing.T) { 2033 var res string 2034 2035 res = KStr.Ucfirst("") 2036 assert.Empty(t, res) 2037 2038 res = KStr.Ucfirst(helloEng) 2039 assert.Equal(t, string(res[0]), "H") 2040 } 2041 2042 func BenchmarkString_Ucfirst(b *testing.B) { 2043 b.ResetTimer() 2044 for i := 0; i < b.N; i++ { 2045 KStr.Ucfirst(helloEng) 2046 } 2047 } 2048 2049 func TestString_Lcfirst(t *testing.T) { 2050 var res string 2051 2052 res = KStr.Lcfirst("") 2053 assert.Empty(t, res) 2054 2055 res = KStr.Lcfirst(helloEngUpper) 2056 assert.Equal(t, string(res[0]), "h") 2057 } 2058 2059 func BenchmarkString_Lcfirst(b *testing.B) { 2060 b.ResetTimer() 2061 for i := 0; i < b.N; i++ { 2062 KStr.Lcfirst(helloEngUpper) 2063 } 2064 } 2065 2066 func TestString_Ucwords_Lcwords(t *testing.T) { 2067 var res1, res2 string 2068 2069 res1 = KStr.Ucwords(helloOther) 2070 res2 = KStr.Lcwords(helloOther) 2071 2072 assert.Equal(t, string(res1[0]), "H") 2073 assert.Equal(t, string(res2[0]), "h") 2074 } 2075 2076 func BenchmarkString_Ucwords(b *testing.B) { 2077 b.ResetTimer() 2078 for i := 0; i < b.N; i++ { 2079 KStr.Ucwords(helloOther) 2080 } 2081 } 2082 2083 func BenchmarkString_Lcwords(b *testing.B) { 2084 b.ResetTimer() 2085 for i := 0; i < b.N; i++ { 2086 KStr.Lcwords(helloOther) 2087 } 2088 } 2089 2090 func TestString_Substr(t *testing.T) { 2091 var res string 2092 2093 res = KStr.Substr("", 0) 2094 assert.Empty(t, res) 2095 2096 res = KStr.Substr(helloEng, 0) 2097 assert.Equal(t, res, helloEng) 2098 2099 var tests = []struct { 2100 param string 2101 start int 2102 length int 2103 expected string 2104 }{ 2105 {helloEngICase, 0, 4, "Hell"}, 2106 {helloEngICase, -2, 4, "ld"}, 2107 {helloEngICase, 0, -2, "HelloWor"}, 2108 {helloEngICase, -11, 8, ""}, 2109 {helloEngICase, 5, 16, "World"}, 2110 } 2111 for _, test := range tests { 2112 actual := KStr.Substr(test.param, test.start, test.length) 2113 assert.Equal(t, actual, test.expected) 2114 } 2115 } 2116 2117 func BenchmarkString_Substr(b *testing.B) { 2118 b.ResetTimer() 2119 for i := 0; i < b.N; i++ { 2120 KStr.Substr(helloEngICase, 5, 10) 2121 } 2122 } 2123 2124 func TestString_MbSubstr(t *testing.T) { 2125 var res string 2126 2127 res = KStr.MbSubstr("", 0) 2128 assert.Empty(t, res) 2129 2130 res = KStr.MbSubstr(helloOther, 0) 2131 assert.Equal(t, res, helloOther) 2132 2133 var tests = []struct { 2134 param string 2135 start int 2136 length int 2137 expected string 2138 }{ 2139 {helloOther, 0, 15, "Hello world. 你好"}, 2140 {helloOther, -3, 4, "on."}, 2141 {helloOther, 0, -37, "Hello world. 你好,"}, 2142 {helloOther, -40, 9, "你好,世界。I`m"}, 2143 {helloOther, 6, 16, "world. 你好,世界。I`m"}, 2144 } 2145 for _, test := range tests { 2146 actual := KStr.MbSubstr(test.param, test.start, test.length) 2147 assert.Equal(t, actual, test.expected) 2148 } 2149 } 2150 2151 func BenchmarkString_MbSubstr(b *testing.B) { 2152 b.ResetTimer() 2153 for i := 0; i < b.N; i++ { 2154 KStr.MbSubstr(helloOther, 6, 16) 2155 } 2156 } 2157 2158 func TestString_SubstrCount(t *testing.T) { 2159 var res int 2160 2161 res = KStr.SubstrCount(tesStr9, "world") 2162 assert.Equal(t, res, 1) 2163 2164 res = KStr.SubstrCount(tesStr9, "World") 2165 assert.Equal(t, res, 2) 2166 2167 res = KStr.SubstrCount(tesStr9, "ello") 2168 assert.Equal(t, res, 3) 2169 } 2170 2171 func BenchmarkString_SubstrCount(b *testing.B) { 2172 b.ResetTimer() 2173 for i := 0; i < b.N; i++ { 2174 KStr.SubstrCount(tesStr9, "ello") 2175 } 2176 } 2177 2178 func TestString_SubstriCount(t *testing.T) { 2179 var res int 2180 2181 res = KStr.SubstriCount(tesStr9, "world") 2182 assert.Equal(t, res, 3) 2183 2184 res = KStr.SubstriCount(tesStr9, "World") 2185 assert.Equal(t, res, 3) 2186 2187 res = KStr.SubstriCount(tesStr9, "or") 2188 assert.Equal(t, res, 4) 2189 } 2190 2191 func BenchmarkString_SubstriCount(b *testing.B) { 2192 b.ResetTimer() 2193 for i := 0; i < b.N; i++ { 2194 KStr.SubstriCount(tesStr9, "or") 2195 } 2196 } 2197 2198 func TestString_Reverse(t *testing.T) { 2199 var res string 2200 2201 res = KStr.Reverse("") 2202 assert.Empty(t, res) 2203 2204 res = KStr.Reverse(strHello) 2205 assert.Equal(t, res, "!好你 !dlroW olleH") 2206 } 2207 2208 func BenchmarkString_Reverse(b *testing.B) { 2209 b.ResetTimer() 2210 for i := 0; i < b.N; i++ { 2211 KStr.Reverse(strHello) 2212 } 2213 } 2214 2215 func TestString_ChunkSplit(t *testing.T) { 2216 var res string 2217 2218 res = KStr.ChunkSplit(helloOther, 4, "") 2219 assert.Equal(t, res, helloOther) 2220 2221 res = KStr.ChunkSplit(helloOther, 4, "\r\n") 2222 assert.Greater(t, len(res), len(helloOther)) 2223 } 2224 2225 func BenchmarkString_ChunkSplit(b *testing.B) { 2226 b.ResetTimer() 2227 for i := 0; i < b.N; i++ { 2228 KStr.ChunkSplit(helloOther, 4, "\r\n") 2229 } 2230 } 2231 2232 func TestString_Strlen(t *testing.T) { 2233 var tests = []struct { 2234 param string 2235 expected int 2236 }{ 2237 {"", 0}, 2238 {strHello, 22}, 2239 {utf8Hello, 18}, 2240 {helloEng, 12}, 2241 {helloOther, 65}, 2242 {strNoGbk, 106}, 2243 {strJap, 39}, 2244 {strKor, 15}, 2245 } 2246 for _, test := range tests { 2247 actual := KStr.Strlen(test.param) 2248 assert.Equal(t, actual, test.expected) 2249 } 2250 } 2251 2252 func BenchmarkString_Strlen(b *testing.B) { 2253 b.ResetTimer() 2254 for i := 0; i < b.N; i++ { 2255 KStr.Strlen(strHello) 2256 } 2257 } 2258 2259 func TestString_MbStrlen(t *testing.T) { 2260 var tests = []struct { 2261 param string 2262 expected int 2263 }{ 2264 {"", 0}, 2265 {strHello, 16}, 2266 {utf8Hello, 6}, 2267 {helloEng, 12}, 2268 {helloOther, 53}, 2269 {strNoGbk, 36}, 2270 {strJap, 13}, 2271 {strKor, 5}, 2272 } 2273 for _, test := range tests { 2274 actual := KStr.MbStrlen(test.param) 2275 assert.Equal(t, actual, test.expected) 2276 } 2277 } 2278 2279 func BenchmarkString_MbStrlen(b *testing.B) { 2280 b.ResetTimer() 2281 for i := 0; i < b.N; i++ { 2282 KStr.MbStrlen(strHello) 2283 } 2284 } 2285 2286 func TestString_Shuffle(t *testing.T) { 2287 var res string 2288 2289 res = KStr.Shuffle("") 2290 assert.Empty(t, res) 2291 2292 res = KStr.Shuffle(strHello) 2293 assert.Equal(t, len(strHello), len(res)) 2294 assert.NotEqual(t, res, strHello) 2295 } 2296 2297 func BenchmarkString_Shuffle(b *testing.B) { 2298 b.ResetTimer() 2299 for i := 0; i < b.N; i++ { 2300 KStr.Shuffle(strHello) 2301 } 2302 } 2303 2304 func TestString_Trim(t *testing.T) { 2305 var res string 2306 2307 res = KStr.Trim(tesStr28) 2308 assert.Empty(t, res) 2309 2310 res = KStr.Trim(tesStr29) 2311 assert.Empty(t, res) 2312 2313 res = KStr.Trim(tesStr30) 2314 assert.Empty(t, res) 2315 2316 res = KStr.Trim(tesStr32) 2317 assert.Empty(t, res) 2318 2319 res = KStr.Trim(tesStr34) 2320 assert.Empty(t, res) 2321 2322 res = KStr.Trim(tesStr31) 2323 assert.Equal(t, res, "abc") 2324 } 2325 2326 func BenchmarkString_Trim(b *testing.B) { 2327 b.ResetTimer() 2328 for i := 0; i < b.N; i++ { 2329 KStr.Trim(tesStr31) 2330 } 2331 } 2332 2333 func TestString_Ltrim(t *testing.T) { 2334 var res string 2335 2336 res = KStr.Ltrim(tesStr28) 2337 assert.Empty(t, res) 2338 2339 res = KStr.Ltrim(tesStr29) 2340 assert.Empty(t, res) 2341 2342 res = KStr.Ltrim(tesStr30) 2343 assert.Empty(t, res) 2344 2345 res = KStr.Ltrim(tesStr32) 2346 assert.Empty(t, res) 2347 2348 res = KStr.Ltrim(tesStr34) 2349 assert.Empty(t, res) 2350 2351 res = KStr.Ltrim(tesStr31) 2352 assert.Equal(t, string(res[0]), "a") 2353 } 2354 2355 func BenchmarkString_Ltrim(b *testing.B) { 2356 b.ResetTimer() 2357 for i := 0; i < b.N; i++ { 2358 KStr.Ltrim(tesStr31) 2359 } 2360 } 2361 2362 func TestString_Rtrim(t *testing.T) { 2363 var res string 2364 2365 res = KStr.Rtrim(tesStr28) 2366 assert.Empty(t, res) 2367 2368 res = KStr.Rtrim(tesStr29) 2369 assert.Empty(t, res) 2370 2371 res = KStr.Rtrim(tesStr30) 2372 assert.Empty(t, res) 2373 2374 res = KStr.Rtrim(tesStr32) 2375 assert.Empty(t, res) 2376 2377 res = KStr.Rtrim(tesStr34) 2378 assert.Empty(t, res) 2379 2380 res = KStr.Rtrim(tesStr31) 2381 assert.Equal(t, string(res[len(res)-1]), "c") 2382 } 2383 2384 func BenchmarkString_Rtrim(b *testing.B) { 2385 b.ResetTimer() 2386 for i := 0; i < b.N; i++ { 2387 KStr.Rtrim(tesStr31) 2388 } 2389 } 2390 2391 func TestString_TrimBOM(t *testing.T) { 2392 var tests = []struct { 2393 param string 2394 expected string 2395 }{ 2396 {"", ""}, 2397 {strHello, strHello}, 2398 {bomChars, ""}, 2399 {tesBom1, ""}, 2400 {tesBom2, "hello"}, 2401 {tesBom3, "world"}, 2402 } 2403 for _, test := range tests { 2404 actual := KStr.TrimBOM([]byte(test.param)) 2405 assert.Equal(t, string(actual), test.expected) 2406 } 2407 } 2408 2409 func BenchmarkString_TrimBOM(b *testing.B) { 2410 b.ResetTimer() 2411 cont := []byte(tesBom2) 2412 for i := 0; i < b.N; i++ { 2413 KStr.TrimBOM(cont) 2414 } 2415 } 2416 2417 func TestString_Ord_Chr(t *testing.T) { 2418 var res1 rune 2419 var res2 string 2420 2421 res1 = KStr.Ord("") 2422 assert.Equal(t, int(res1), 65533) 2423 2424 res1 = KStr.Ord("a") 2425 assert.Equal(t, int(res1), 97) 2426 res2 = KStr.Chr(97) 2427 assert.Equal(t, res2, "a") 2428 2429 res1 = KStr.Ord(strHello) 2430 assert.Equal(t, int(res1), 72) 2431 res2 = KStr.Chr(72) 2432 assert.Equal(t, string(strHello[0]), res2) 2433 } 2434 2435 func BenchmarkString_Ord(b *testing.B) { 2436 b.ResetTimer() 2437 for i := 0; i < b.N; i++ { 2438 KStr.Ord(strHello) 2439 } 2440 } 2441 2442 func BenchmarkString_Chr(b *testing.B) { 2443 b.ResetTimer() 2444 for i := 0; i < b.N; i++ { 2445 KStr.Chr(72) 2446 } 2447 } 2448 2449 func TestString_Serialize_UnSerialize(t *testing.T) { 2450 var res []byte 2451 var obj interface{} 2452 var err error 2453 var objStr string 2454 var objInt int 2455 var objPs sPersons 2456 2457 //序列化字符串 2458 res, err = KStr.Serialize(strHello) 2459 assert.Nil(t, err) 2460 //反序列化字符串 2461 obj, err = KStr.UnSerialize(res) 2462 assert.Nil(t, err) 2463 assert.Equal(t, strHello, toStr(obj)) 2464 obj, err = KStr.UnSerialize(res, objStr) 2465 assert.Nil(t, err) 2466 assert.Equal(t, strHello, toStr(obj)) 2467 2468 //序列化整型 2469 res, err = KStr.Serialize(intSpeedLight) 2470 assert.Nil(t, err) 2471 //反序列化整型 2472 obj, err = KStr.UnSerialize(res) 2473 assert.Nil(t, err) 2474 assert.Equal(t, intSpeedLight, toInt(obj)) 2475 obj, err = KStr.UnSerialize(res, objInt) 2476 assert.Nil(t, err) 2477 assert.Equal(t, intSpeedLight, toInt(obj)) 2478 2479 //序列化对象 2480 res, err = KStr.Serialize(crowd) 2481 assert.Nil(t, err) 2482 //反序列化对象 2483 obj, err = KStr.UnSerialize(res) 2484 assert.Equal(t, toStr(crowd), toStr(obj)) 2485 obj, err = KStr.UnSerialize(res, objPs) 2486 assert.Equal(t, toStr(crowd), toStr(obj)) 2487 } 2488 2489 func BenchmarkString_Serialize(b *testing.B) { 2490 b.ResetTimer() 2491 for i := 0; i < b.N; i++ { 2492 _, _ = KStr.Serialize(crowd) 2493 } 2494 } 2495 2496 func BenchmarkString_UnSerialize(b *testing.B) { 2497 b.ResetTimer() 2498 str, _ := KStr.Serialize(crowd) 2499 for i := 0; i < b.N; i++ { 2500 _, _ = KStr.UnSerialize(str) 2501 } 2502 } 2503 2504 func TestString_Quotemeta(t *testing.T) { 2505 var res string 2506 2507 res = KStr.Quotemeta("") 2508 assert.Empty(t, res) 2509 2510 res = KStr.Quotemeta(tesStr35) 2511 assert.Contains(t, res, "\\") 2512 } 2513 2514 func BenchmarkString_Quotemeta(b *testing.B) { 2515 b.ResetTimer() 2516 for i := 0; i < b.N; i++ { 2517 KStr.Quotemeta(tesStr35) 2518 } 2519 } 2520 2521 func TestString_Htmlentities_HtmlentityDecode(t *testing.T) { 2522 var res string 2523 2524 res = KStr.Htmlentities(tesStr36) 2525 assert.Contains(t, res, "&") 2526 2527 res = KStr.HtmlentityDecode(res) 2528 assert.Equal(t, res, tesStr36) 2529 } 2530 2531 func BenchmarkString_Htmlentities(b *testing.B) { 2532 b.ResetTimer() 2533 for i := 0; i < b.N; i++ { 2534 KStr.Htmlentities(tesStr36) 2535 } 2536 } 2537 2538 func BenchmarkString_HtmlentityDecode(b *testing.B) { 2539 b.ResetTimer() 2540 for i := 0; i < b.N; i++ { 2541 KStr.HtmlentityDecode(tesStr37) 2542 } 2543 } 2544 2545 func TestString_Crc32(t *testing.T) { 2546 var res uint32 2547 2548 res = KStr.Crc32(tesStr38) 2549 assert.Greater(t, int(res), 0) 2550 } 2551 2552 func BenchmarkString_Crc32(b *testing.B) { 2553 b.ResetTimer() 2554 for i := 0; i < b.N; i++ { 2555 KStr.Crc32(tesStr38) 2556 } 2557 } 2558 2559 func TestString_SimilarText(t *testing.T) { 2560 var percent float64 2561 var res int 2562 2563 res, percent = KStr.SimilarText(similarStr1, similarStr2) 2564 assert.Greater(t, res, 0) 2565 assert.Greater(t, percent, 0.0) 2566 2567 res, percent = KStr.SimilarText(utf8Hello, helloCn) 2568 assert.Greater(t, percent, 50.0) 2569 2570 res, percent = KStr.SimilarText("", strKor) 2571 assert.Equal(t, res, 0) 2572 assert.Equal(t, percent, 0.0) 2573 2574 res, percent = KStr.SimilarText(helloOther, helloOther) 2575 assert.Equal(t, percent, 100.0) 2576 } 2577 2578 func BenchmarkString_SimilarText(b *testing.B) { 2579 b.ResetTimer() 2580 for i := 0; i < b.N; i++ { 2581 _, _ = KStr.SimilarText(utf8Hello, helloCn) 2582 } 2583 } 2584 2585 func TestString_Explode(t *testing.T) { 2586 var res []string 2587 2588 res = KStr.Explode("") 2589 assert.Empty(t, res) 2590 2591 //没有提供分隔符 2592 res = KStr.Explode(helloOther) 2593 assert.Equal(t, len(res), 1) 2594 2595 //多个分隔符 2596 res = KStr.Explode(helloOther, ",", " ") 2597 assert.Greater(t, len(res), 1) 2598 2599 res = KStr.Explode(helloOther, []string{",", " ", "."}...) 2600 assert.Greater(t, len(res), 1) 2601 } 2602 2603 func BenchmarkString_Explode(b *testing.B) { 2604 b.ResetTimer() 2605 for i := 0; i < b.N; i++ { 2606 KStr.Explode(helloOther, ",", " ") 2607 } 2608 } 2609 2610 func TestString_Uniqid(t *testing.T) { 2611 var res1, res2 string 2612 2613 res1 = KStr.Uniqid(helloEngICase) 2614 assert.True(t, KStr.StartsWith(res1, helloEngICase, false)) 2615 2616 res2 = KStr.Uniqid(helloEngICase) 2617 assert.NotEqual(t, res1, res2) 2618 } 2619 2620 func BenchmarkString_Uniqid(b *testing.B) { 2621 b.ResetTimer() 2622 for i := 0; i < b.N; i++ { 2623 KStr.Uniqid(helloEngICase) 2624 } 2625 } 2626 2627 func TestString_UuidV4(t *testing.T) { 2628 var res1, res2 string 2629 var err error 2630 2631 res1, err = KStr.UuidV4() 2632 assert.Nil(t, err) 2633 assert.Equal(t, len(res1), 36) 2634 2635 res2, err = KStr.UuidV4() 2636 assert.Nil(t, err) 2637 assert.NotEqual(t, res1, res2) 2638 } 2639 2640 func BenchmarkString_UuidV4(b *testing.B) { 2641 b.ResetTimer() 2642 for i := 0; i < b.N; i++ { 2643 _, _ = KStr.UuidV4() 2644 } 2645 } 2646 2647 func TestString_VersionCompare(t *testing.T) { 2648 var err error 2649 2650 //错误的比较符 2651 _, err = KStr.VersionCompare("1.0", "1.2", "dd") 2652 assert.NotNil(t, err) 2653 2654 var tests = []struct { 2655 v1 string 2656 v2 string 2657 op string 2658 expected bool 2659 }{ 2660 {"", "", "=", true}, 2661 {"", "0", "<", true}, 2662 {"0", "", ">", true}, 2663 {"9", "10", "<", true}, 2664 {"09", "10", "<", true}, 2665 {"10", "#10", "<", true}, 2666 {"#9", "#10", "<", true}, 2667 {"#09", "#10", "<", true}, 2668 {"tes09", "tes10", "<", true}, 2669 {"0.9", "1.0", "=", false}, 2670 {"0.9", "1.0", "<=", true}, 2671 {"dev11.0", "dev2.0", ">=", true}, 2672 {"dev-1.0", "21.0", ">", true}, 2673 {"dev-1.0", "1.0", "!=", true}, 2674 {"dev-21.0.summer", "1.0", "<", false}, 2675 {"beta-11.0", "dev-12.0", ">", true}, 2676 {"1.2.3-alpha", "1.2.3alph.123", ">", true}, 2677 {"1.2.3-alpha", "1.2.3alph.num", ">", true}, 2678 {"1.2.3alph.123", "1.2.3-alpha", "<", true}, 2679 {"1.2.3alph.sum", "1.2.3-alpha", "<", true}, 2680 {"1.2.3alph.sum", "1.2.3-alpha.", "<", true}, 2681 } 2682 for _, test := range tests { 2683 actual, _ := KStr.VersionCompare(test.v1, test.v2, test.op) 2684 assert.Equal(t, actual, test.expected) 2685 } 2686 2687 } 2688 2689 func BenchmarkString_VersionCompare(b *testing.B) { 2690 b.ResetTimer() 2691 for i := 0; i < b.N; i++ { 2692 _, _ = KStr.VersionCompare("1.2.3alph.sum", "1.2.3-alpha.", "<") 2693 } 2694 } 2695 2696 func TestString_ToCamelCase(t *testing.T) { 2697 var tests = []struct { 2698 param string 2699 expected string 2700 }{ 2701 {"", ""}, 2702 {"some_words", "SomeWords"}, 2703 {"http_server", "HttpServer"}, 2704 {"no_https", "NoHttps"}, 2705 {"_complex__case_", "_Complex_Case_"}, 2706 {"some words", "SomeWords"}, 2707 {"sayHello", "SayHello"}, 2708 {"SayHello", "SayHello"}, 2709 {"SayHelloWorld", "SayHelloWorld"}, 2710 {"DOYouOK", "DoYouOk"}, 2711 {"AReYouOK", "AreYouOk"}, 2712 } 2713 for _, test := range tests { 2714 actual := KStr.ToCamelCase(test.param) 2715 assert.Equal(t, actual, test.expected) 2716 } 2717 } 2718 2719 func BenchmarkString_ToCamelCase(b *testing.B) { 2720 b.ResetTimer() 2721 for i := 0; i < b.N; i++ { 2722 KStr.ToCamelCase(helloOther) 2723 } 2724 } 2725 2726 func TestString_ToSnakeCase(t *testing.T) { 2727 var tests = []struct { 2728 param string 2729 expected string 2730 }{ 2731 {"", ""}, 2732 {"FirstName", "first_name"}, 2733 {"HTTPServer", "http_server"}, 2734 {"NoHTTPS", "no_https"}, 2735 {"GO_PATH", "go_path"}, 2736 {"GO PATH", "go_path"}, 2737 {"GO-PATH", "go_path"}, 2738 {"HTTP2XX", "http_2xx"}, 2739 {"http2xx", "http_2xx"}, 2740 {"HTTP20xOK", "http_20x_ok"}, 2741 } 2742 for _, test := range tests { 2743 actual := KStr.ToSnakeCase(test.param) 2744 assert.Equal(t, actual, test.expected) 2745 } 2746 } 2747 2748 func BenchmarkString_ToSnakeCase(b *testing.B) { 2749 b.ResetTimer() 2750 for i := 0; i < b.N; i++ { 2751 KStr.ToSnakeCase(helloOther) 2752 } 2753 } 2754 2755 func TestString_ToKebabCase(t *testing.T) { 2756 var tests = []struct { 2757 param string 2758 expected string 2759 }{ 2760 {"", ""}, 2761 {"�helloWorld", "hello-world"}, 2762 {"A", "a"}, 2763 {"HellOW�orld", "hell-oworld"}, 2764 {"-FirstName", "-first-name"}, 2765 {"FirstName", "first-name"}, 2766 {"HTTPServer", "http-server"}, 2767 {"NoHTTPS", "no-https"}, 2768 {"GO_PATH", "go-path"}, 2769 {"GO PATH", "go-path"}, 2770 {"GO-PATH", "go-path"}, 2771 {"HTTP2XX", "http-2xx"}, 2772 {"http2xx", "http-2xx"}, 2773 {"HTTP20xOK", "http-20x-ok"}, 2774 } 2775 for _, test := range tests { 2776 actual := KStr.ToKebabCase(test.param) 2777 assert.Equal(t, actual, test.expected) 2778 } 2779 } 2780 2781 func BenchmarkString_ToKebabCase(b *testing.B) { 2782 b.ResetTimer() 2783 for i := 0; i < b.N; i++ { 2784 KStr.ToKebabCase(helloOther) 2785 } 2786 } 2787 2788 func TestString_RemoveBefore(t *testing.T) { 2789 var tests = []struct { 2790 str string 2791 sub string 2792 include bool 2793 ignoreCase bool 2794 expected string 2795 }{ 2796 {"", "", false, false, ""}, 2797 {helloEng, "", false, false, helloEng}, 2798 {helloOther2, "world", false, false, helloOther2}, 2799 {helloOther2, "World", false, false, "World 世界!"}, 2800 {helloOther2, "World", true, false, " 世界!"}, 2801 {helloOther2, "world", false, true, "World 世界!"}, 2802 {helloOther2, "world 世", false, true, "World 世界!"}, 2803 {helloOther2, "world 世", true, true, "界!"}, 2804 } 2805 for _, test := range tests { 2806 actual := KStr.RemoveBefore(test.str, test.sub, test.include, test.ignoreCase) 2807 assert.Equal(t, actual, test.expected) 2808 } 2809 } 2810 2811 func BenchmarkString_RemoveBefore(b *testing.B) { 2812 b.ResetTimer() 2813 for i := 0; i < b.N; i++ { 2814 KStr.RemoveBefore(helloOther2, "world", false, false) 2815 } 2816 } 2817 2818 func TestString_RemoveAfter(t *testing.T) { 2819 var tests = []struct { 2820 str string 2821 sub string 2822 include bool 2823 ignoreCase bool 2824 expected string 2825 }{ 2826 {"", "", false, false, ""}, 2827 {helloEng, "", false, false, helloEng}, 2828 {helloOther2, "world", false, false, helloOther2}, 2829 {helloOther2, "World", false, false, "Hello 你好, World"}, 2830 {helloOther2, "World", true, false, "Hello 你好, "}, 2831 {helloOther2, "world", false, true, "Hello 你好, World"}, 2832 {helloOther2, "world 世", false, true, "Hello 你好, World 世"}, 2833 {helloOther2, "world 世", true, true, "Hello 你好, "}, 2834 } 2835 for _, test := range tests { 2836 actual := KStr.RemoveAfter(test.str, test.sub, test.include, test.ignoreCase) 2837 assert.Equal(t, actual, test.expected) 2838 } 2839 } 2840 2841 func BenchmarkString_RemoveAfter(b *testing.B) { 2842 b.ResetTimer() 2843 for i := 0; i < b.N; i++ { 2844 KStr.RemoveAfter(helloOther2, "world 世", true, true) 2845 } 2846 } 2847 2848 func TestString_DBC2SBC(t *testing.T) { 2849 var res string 2850 res = KStr.DBC2SBC(helloEng) 2851 assert.Greater(t, len(res), len(helloEng)) 2852 } 2853 2854 func BenchmarkString_DBC2SBC(b *testing.B) { 2855 b.ResetTimer() 2856 for i := 0; i < b.N; i++ { 2857 KStr.DBC2SBC(helloEng) 2858 } 2859 } 2860 2861 func TestString_SBC2DBC(t *testing.T) { 2862 var res string 2863 res = KStr.SBC2DBC(helloWidth) 2864 assert.Less(t, len(res), len(helloWidth)) 2865 } 2866 2867 func BenchmarkString_SBC2DBC(b *testing.B) { 2868 b.ResetTimer() 2869 for i := 0; i < b.N; i++ { 2870 KStr.SBC2DBC(helloWidth) 2871 } 2872 } 2873 2874 func TestString_Levenshtein(t *testing.T) { 2875 var res int 2876 2877 res = KStr.Levenshtein(helloEng, strHello) 2878 assert.Greater(t, res, 0) 2879 2880 res = KStr.Levenshtein(helloEng, helloEngICase) 2881 assert.Greater(t, res, 0) 2882 2883 res = KStr.Levenshtein(strHello, strHello) 2884 assert.Equal(t, res, 0) 2885 2886 res = KStr.Levenshtein(strHello, tesHtmlDoc) 2887 assert.Equal(t, res, -1) 2888 2889 res = KStr.Levenshtein(tesStr39, tesStr40) 2890 assert.Greater(t, res, 1) 2891 2892 res = KStr.Levenshtein(tesStr40, tesStr41) 2893 assert.Greater(t, res, 1) 2894 } 2895 2896 func BenchmarkString_Levenshtein(b *testing.B) { 2897 b.ResetTimer() 2898 for i := 0; i < b.N; i++ { 2899 KStr.Levenshtein(helloEng, helloEngICase) 2900 } 2901 } 2902 2903 func TestString_ClosestWord(t *testing.T) { 2904 res, dis := KStr.ClosestWord("hello,golang", strSl3) 2905 assert.Equal(t, res, "Hello,go language") 2906 assert.Greater(t, dis, 0) 2907 } 2908 2909 func BenchmarkString_ClosestWord(b *testing.B) { 2910 b.ResetTimer() 2911 for i := 0; i < b.N; i++ { 2912 _, _ = KStr.ClosestWord("hello,golang", strSl3) 2913 } 2914 } 2915 2916 func TestString_Utf8ToBig5_Big5ToUtf8(t *testing.T) { 2917 var res []byte 2918 var err error 2919 2920 res, err = KStr.Utf8ToBig5(bytsUtf8Hello) 2921 assert.Nil(t, err) 2922 2923 res, err = KStr.Big5ToUtf8(res) 2924 assert.Nil(t, err) 2925 assert.Equal(t, string(res), utf8Hello) 2926 } 2927 2928 func BenchmarkString_Utf8ToBig5(b *testing.B) { 2929 b.ResetTimer() 2930 for i := 0; i < b.N; i++ { 2931 _, _ = KStr.Utf8ToBig5(bytsUtf8Hello) 2932 } 2933 } 2934 2935 func BenchmarkString_Big5ToUtf8(b *testing.B) { 2936 b.ResetTimer() 2937 bs, _ := KStr.Utf8ToBig5(bytsUtf8Hello) 2938 for i := 0; i < b.N; i++ { 2939 _, _ = KStr.Big5ToUtf8(bs) 2940 } 2941 } 2942 2943 func TestString_FirstLetter(t *testing.T) { 2944 var tests = []struct { 2945 str string 2946 expected string 2947 }{ 2948 {helloEng, "h"}, 2949 {helloOther2, "H"}, 2950 {utf8Hello, "N"}, 2951 {"啊哈,world", "A"}, 2952 {"布料", "B"}, 2953 {"从来", "C"}, 2954 {"到达", "D"}, 2955 {"饿了", "E"}, 2956 {"发展", "F"}, 2957 {"改革", "G"}, 2958 {"好啊", "H"}, 2959 {"将来", "J"}, 2960 {"开心", "K"}, 2961 {"里面", "L"}, 2962 {"名字", "M"}, 2963 {"哪里", "N"}, 2964 {"欧洲", "O"}, 2965 {"品尝", "P"}, 2966 {"前进", "Q"}, 2967 {"人类", "R"}, 2968 {"是的", "S"}, 2969 {"天天", "T"}, 2970 {"问题", "W"}, 2971 {"西安", "X"}, 2972 {"用途", "Y"}, 2973 {"这里", "Z"}, 2974 {"", ""}, 2975 {"~!@", ""}, 2976 } 2977 for _, test := range tests { 2978 actual := KStr.FirstLetter(test.str) 2979 assert.Equal(t, actual, test.expected) 2980 } 2981 } 2982 2983 func BenchmarkString_FirstLetter(b *testing.B) { 2984 b.ResetTimer() 2985 for i := 0; i < b.N; i++ { 2986 KStr.FirstLetter(helloOther) 2987 } 2988 } 2989 2990 func TestString_HideCard(t *testing.T) { 2991 var res string 2992 2993 res = KStr.HideCard("") 2994 assert.NotEmpty(t, res) 2995 2996 res = KStr.HideCard(tesTel01) 2997 assert.Greater(t, len(res), len(tesTel01)) 2998 2999 res = KStr.HideCard(tesCredno01) 3000 assert.Contains(t, res, "1231") 3001 3002 res = KStr.HideCard(tesCredno02) 3003 assert.Contains(t, res, "2551") 3004 } 3005 3006 func BenchmarkString_HideCard(b *testing.B) { 3007 b.ResetTimer() 3008 for i := 0; i < b.N; i++ { 3009 KStr.HideCard(tesCredno02) 3010 } 3011 } 3012 3013 func TestString_HideMobile(t *testing.T) { 3014 var res string 3015 3016 res = KStr.HideMobile("") 3017 assert.NotEmpty(t, res) 3018 3019 res = KStr.HideMobile(tesTel01) 3020 assert.Less(t, len(res), len(tesTel01)) 3021 3022 res = KStr.HideMobile(tesCredno01) 3023 assert.Contains(t, res, "123") 3024 3025 res = KStr.HideMobile(tesCredno02) 3026 assert.Contains(t, res, "551") 3027 } 3028 3029 func BenchmarkString_HideMobile(b *testing.B) { 3030 b.ResetTimer() 3031 for i := 0; i < b.N; i++ { 3032 KStr.HideMobile(tesCredno02) 3033 } 3034 } 3035 3036 func TestString_HideTrueName(t *testing.T) { 3037 var tests = []struct { 3038 param string 3039 }{ 3040 {""}, 3041 {helloEngICase}, 3042 {tesChineseName1}, 3043 {tesChineseName2}, 3044 {tesChineseName3}, 3045 {tesChineseName5}, 3046 {tesCompName1}, 3047 {tesCompName2}, 3048 {tesCompName3}, 3049 {strNoGbk}, 3050 } 3051 for _, test := range tests { 3052 actual := KStr.HideTrueName(test.param) 3053 assert.NotEmpty(t, actual) 3054 assert.Contains(t, actual, "*") 3055 } 3056 } 3057 3058 func BenchmarkString_HideTrueName(b *testing.B) { 3059 b.ResetTimer() 3060 for i := 0; i < b.N; i++ { 3061 KStr.HideTrueName(strNoGbk) 3062 } 3063 } 3064 3065 func TestString_CountBase64Byte(t *testing.T) { 3066 var res int 3067 3068 str, _ := KFile.Img2Base64(imgPng) 3069 res = KStr.CountBase64Byte(str) 3070 assert.Greater(t, res, 100) 3071 3072 res = KStr.CountBase64Byte(helloEng) 3073 assert.Equal(t, res, 0) 3074 } 3075 3076 func BenchmarkString_CountBase64Byte(b *testing.B) { 3077 b.ResetTimer() 3078 str, _ := KFile.Img2Base64(imgPng) 3079 for i := 0; i < b.N; i++ { 3080 KStr.CountBase64Byte(str) 3081 } 3082 } 3083 3084 func TestString_StrpadLeft_StrpadRight_StrpadBoth(t *testing.T) { 3085 var res string 3086 3087 //指定长度小于实际长度 3088 res = KStr.Strpad(helloEng, "-", 1, PAD_BOTH) 3089 assert.Equal(t, res, helloEng) 3090 3091 res = KStr.Strpad(helloEng, "-", 17, PAD_BOTH) 3092 assert.NotEqual(t, res, helloEng) 3093 3094 res = KStr.StrpadLeft(strHello, "-", 45) 3095 assert.Equal(t, KStr.MbStrlen(res), 45) 3096 3097 res = KStr.StrpadRight(strHello, "。", 50) 3098 assert.Equal(t, KStr.MbStrlen(res), 50) 3099 3100 res = KStr.StrpadBoth(strHello, "-。", 50) 3101 assert.Equal(t, KStr.MbStrlen(res), 50) 3102 } 3103 3104 func BenchmarkString_StrpadLeft(b *testing.B) { 3105 b.ResetTimer() 3106 for i := 0; i < b.N; i++ { 3107 KStr.StrpadLeft(strHello, "-", 45) 3108 } 3109 } 3110 3111 func BenchmarkString_StrpadRight(b *testing.B) { 3112 b.ResetTimer() 3113 for i := 0; i < b.N; i++ { 3114 KStr.StrpadRight(strHello, "。", 50) 3115 } 3116 } 3117 3118 func BenchmarkString_StrpadBoth(b *testing.B) { 3119 b.ResetTimer() 3120 for i := 0; i < b.N; i++ { 3121 KStr.StrpadBoth(strHello, "-。", 50) 3122 } 3123 } 3124 3125 func TestString_CountWords(t *testing.T) { 3126 var total, words int 3127 var res map[string]int 3128 var cont []byte 3129 3130 cont, _ = KFile.ReadFile(fileDante) 3131 total, res = KStr.CountWords(toStr(cont)) 3132 words = len(res) 3133 assert.Greater(t, words, 0) 3134 assert.Greater(t, total, words) 3135 } 3136 3137 func BenchmarkString_CountWords(b *testing.B) { 3138 b.ResetTimer() 3139 for i := 0; i < b.N; i++ { 3140 _, _ = KStr.CountWords(helloOther) 3141 } 3142 } 3143 3144 func TestString_StartsWith(t *testing.T) { 3145 var tests = []struct { 3146 str string 3147 sub string 3148 ignoreCase bool 3149 expected bool 3150 }{ 3151 {"", "", false, false}, 3152 {helloEng, "", false, false}, 3153 {helloOther2, "hello", false, false}, 3154 {helloOther2, "Hello", false, true}, 3155 {helloOther2, "hello", true, true}, 3156 {helloOther2, "Hello 你好", false, true}, 3157 {helloOther2, "hello 你好", true, true}, 3158 {helloOther2, "world 世", true, false}, 3159 } 3160 for _, test := range tests { 3161 actual := KStr.StartsWith(test.str, test.sub, test.ignoreCase) 3162 assert.Equal(t, actual, test.expected) 3163 } 3164 } 3165 3166 func BenchmarkString_StartsWith(b *testing.B) { 3167 b.ResetTimer() 3168 for i := 0; i < b.N; i++ { 3169 KStr.StartsWith(helloOther2, "hello", true) 3170 } 3171 } 3172 3173 func TestString_StartsWiths(t *testing.T) { 3174 var tests = []struct { 3175 str string 3176 subs []string 3177 ignoreCase bool 3178 expected bool 3179 }{ 3180 {"", []string{"", "a"}, false, false}, 3181 {helloOther2, []string{""}, false, false}, 3182 {helloOther2, []string{helloCn, "hello"}, false, false}, 3183 {helloOther2, []string{helloCn, "Hello"}, false, true}, 3184 {helloOther2, []string{helloCn, "hello"}, true, true}, 3185 {helloOther2, []string{helloCn, "Hello 你好"}, false, true}, 3186 {helloOther2, []string{helloCn, "hello 你好"}, true, true}, 3187 {helloOther2, []string{helloCn, "world 世"}, true, false}, 3188 } 3189 for _, test := range tests { 3190 actual := KStr.StartsWiths(test.str, test.subs, test.ignoreCase) 3191 assert.Equal(t, actual, test.expected) 3192 } 3193 } 3194 3195 func BenchmarkString_StartsWiths(b *testing.B) { 3196 b.ResetTimer() 3197 for i := 0; i < b.N; i++ { 3198 KStr.StartsWiths(helloOther2, []string{helloCn, "hello 你好"}, true) 3199 } 3200 } 3201 3202 func TestString_EndsWith(t *testing.T) { 3203 var tests = []struct { 3204 str string 3205 sub string 3206 ignoreCase bool 3207 expected bool 3208 }{ 3209 {"", "", false, false}, 3210 {helloEng, "", false, false}, 3211 {helloOther2, "World", false, false}, 3212 {helloOther2, "World", true, false}, 3213 {helloOther2, "World 世界!", false, true}, 3214 {helloOther2, "world 世界!", true, true}, 3215 } 3216 for _, test := range tests { 3217 actual := KStr.EndsWith(test.str, test.sub, test.ignoreCase) 3218 assert.Equal(t, actual, test.expected) 3219 } 3220 } 3221 3222 func BenchmarkString_EndsWith(b *testing.B) { 3223 b.ResetTimer() 3224 for i := 0; i < b.N; i++ { 3225 KStr.EndsWith(helloOther2, "World 世界!", false) 3226 } 3227 } 3228 3229 func TestString_EndsWiths(t *testing.T) { 3230 var tests = []struct { 3231 str string 3232 subs []string 3233 ignoreCase bool 3234 expected bool 3235 }{ 3236 {"", []string{""}, false, false}, 3237 {helloEng, []string{""}, false, false}, 3238 {helloOther2, []string{"", "World"}, false, false}, 3239 {helloOther2, []string{"", "World"}, true, false}, 3240 {helloOther2, []string{"", "World 世界!"}, false, true}, 3241 {helloOther2, []string{"", "world 世界!"}, true, true}, 3242 } 3243 for _, test := range tests { 3244 actual := KStr.EndsWiths(test.str, test.subs, test.ignoreCase) 3245 assert.Equal(t, actual, test.expected) 3246 } 3247 } 3248 3249 func BenchmarkString_EndsWiths(b *testing.B) { 3250 b.ResetTimer() 3251 for i := 0; i < b.N; i++ { 3252 KStr.EndsWiths(helloOther2, []string{"", "World 世界!"}, false) 3253 } 3254 } 3255 3256 func TestString_HasEmoji_RemoveEmoji(t *testing.T) { 3257 var res string 3258 var chk bool 3259 3260 chk = KStr.HasEmoji(strHello) 3261 assert.False(t, chk) 3262 3263 chk = KStr.HasEmoji(tesEmoji1) 3264 assert.True(t, chk) 3265 3266 res = KStr.RemoveEmoji(tesEmoji1) 3267 chk = KStr.HasEmoji(res) 3268 assert.False(t, chk) 3269 } 3270 3271 func BenchmarkString_HasEmoji(b *testing.B) { 3272 b.ResetTimer() 3273 for i := 0; i < b.N; i++ { 3274 KStr.HasEmoji(strHello) 3275 } 3276 } 3277 3278 func BenchmarkString_RemoveEmoji(b *testing.B) { 3279 b.ResetTimer() 3280 for i := 0; i < b.N; i++ { 3281 KStr.RemoveEmoji(tesEmoji2) 3282 } 3283 } 3284 3285 func TestString_Gravatar(t *testing.T) { 3286 var res string 3287 3288 res = KStr.Gravatar("", 100) 3289 assert.NotEmpty(t, res) 3290 3291 res = KStr.Gravatar(tesEmail1, 150) 3292 assert.NotEmpty(t, res) 3293 } 3294 3295 func BenchmarkString_Gravatar(b *testing.B) { 3296 b.ResetTimer() 3297 for i := 0; i < b.N; i++ { 3298 KStr.Gravatar(tesEmail1, 150) 3299 } 3300 } 3301 3302 func TestString_AtWho(t *testing.T) { 3303 var tests = []struct { 3304 name string 3305 leng int 3306 expected []string 3307 }{ 3308 {"", 0, []string{}}, 3309 {"@hellowor", 3, []string{"hellowor"}}, 3310 {"@hellowor", 5, []string{"hellowor"}}, 3311 {" @hellowor", 5, []string{"hellowor"}}, 3312 {"Hi, @hellowor", 5, []string{"hellowor"}}, 3313 {"Hi,@hellowor", 5, []string{"hellowor"}}, 3314 {"Hi, @hellowor, @tom", 3, []string{"tom"}}, 3315 {"Hi, @hellowor and @tom and @hellowor again", 3, []string{"hellowor", "tom"}}, 3316 {"@hellowor\nanother line @john", 3, []string{"hellowor", "john"}}, 3317 {"hellowor@gmail.com", 0, []string{}}, 3318 {"hellowor@gmail.com @test", 3, []string{"test"}}, 3319 } 3320 for _, test := range tests { 3321 actual := KStr.AtWho(test.name, test.leng) 3322 assert.Equal(t, actual, test.expected) 3323 } 3324 } 3325 3326 func BenchmarkString_AtWho(b *testing.B) { 3327 b.ResetTimer() 3328 for i := 0; i < b.N; i++ { 3329 KStr.AtWho("Hi, @hellowor", 6) 3330 } 3331 } 3332 3333 func TestString_MatchEquations(t *testing.T) { 3334 res := KStr.MatchEquations(equationStr03) 3335 assert.NotEmpty(t, res) 3336 assert.Greater(t, len(res), 10) 3337 } 3338 3339 func BenchmarkString_MatchEquations(b *testing.B) { 3340 b.ResetTimer() 3341 for i := 0; i < b.N; i++ { 3342 KStr.MatchEquations(equationStr03) 3343 } 3344 } 3345 3346 func TestSring_GetEquationValue(t *testing.T) { 3347 var res string 3348 3349 res = KStr.GetEquationValue(equationStr01, "hello") 3350 assert.Empty(t, res) 3351 3352 res = KStr.GetEquationValue(equationStr01, "utm_source") 3353 assert.NotEmpty(t, res) 3354 3355 res = KStr.GetEquationValue(equationStr02, "str") 3356 assert.NotEmpty(t, res) 3357 } 3358 3359 func BenchmarkString_GetEquationValue(b *testing.B) { 3360 b.ResetTimer() 3361 for i := 0; i < b.N; i++ { 3362 KStr.GetEquationValue(equationStr01, "utm_source") 3363 } 3364 }