test/test_string.cpp

changeset 645
ec50abb285ad
parent 628
1e2be40f0cb5
equal deleted inserted replaced
644:fcaa0891ef28 645:ec50abb285ad
651 cxmutstr str = cx_strdup(cx_str("thIs 1s @ Te$t")); 651 cxmutstr str = cx_strdup(cx_str("thIs 1s @ Te$t"));
652 cx_strlower(str); 652 cx_strlower(str);
653 EXPECT_STREQ(str.ptr, "this 1s @ te$t"); 653 EXPECT_STREQ(str.ptr, "this 1s @ te$t");
654 cx_strfree(&str); 654 cx_strfree(&str);
655 } 655 }
656
657 TEST(String, strtok) {
658 cxstring str = cx_str("a,comma,separated,string");
659 cxstring delim = cx_str(",");
660 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
661 EXPECT_EQ(ctx.str.ptr, str.ptr);
662 EXPECT_EQ(ctx.str.length, str.length);
663 EXPECT_EQ(ctx.delim.ptr, delim.ptr);
664 EXPECT_EQ(ctx.delim.length, delim.length);
665 EXPECT_EQ(ctx.limit, 3);
666 EXPECT_EQ(ctx.found, 0);
667 EXPECT_EQ(ctx.pos, 0);
668 EXPECT_EQ(ctx.next_pos, 0);
669 EXPECT_EQ(ctx.delim_more, nullptr);
670 EXPECT_EQ(ctx.delim_more_count, 0);
671 }
672
673 TEST(String, strtok_m) {
674 cxmutstr str = cx_strdup(cx_str("a,comma,separated,string"));
675 cxstring delim = cx_str(",");
676 CxStrtokCtx ctx = cx_strtok_m(str, delim, 3);
677 EXPECT_EQ(ctx.str.ptr, str.ptr);
678 EXPECT_EQ(ctx.str.length, str.length);
679 EXPECT_EQ(ctx.delim.ptr, delim.ptr);
680 EXPECT_EQ(ctx.delim.length, delim.length);
681 EXPECT_EQ(ctx.limit, 3);
682 EXPECT_EQ(ctx.found, 0);
683 EXPECT_EQ(ctx.pos, 0);
684 EXPECT_EQ(ctx.next_pos, 0);
685 EXPECT_EQ(ctx.delim_more, nullptr);
686 EXPECT_EQ(ctx.delim_more_count, 0);
687 cx_strfree(&str);
688 }
689
690 TEST(String, strtok_delim) {
691 cxstring str = cx_str("an,arbitrarily|separated;string");
692 cxstring delim = cx_str(",");
693 cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")};
694 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
695 cx_strtok_delim(&ctx, delim_more, 2);
696 EXPECT_EQ(ctx.str.ptr, str.ptr);
697 EXPECT_EQ(ctx.str.length, str.length);
698 EXPECT_EQ(ctx.delim.ptr, delim.ptr);
699 EXPECT_EQ(ctx.delim.length, delim.length);
700 EXPECT_EQ(ctx.limit, 3);
701 EXPECT_EQ(ctx.found, 0);
702 EXPECT_EQ(ctx.pos, 0);
703 EXPECT_EQ(ctx.next_pos, 0);
704 EXPECT_EQ(ctx.delim_more, delim_more);
705 EXPECT_EQ(ctx.delim_more_count, 2);
706 }
707
708 TEST(String, strtok_next_easy) {
709 cxstring str = cx_str("a,comma,separated,string");
710 cxstring delim = cx_str(",");
711 CxStrtokCtx ctx = cx_strtok(str, delim, 3);
712 bool ret;
713 cxstring tok;
714
715 ret = cx_strtok_next(&ctx, &tok);
716 ASSERT_TRUE(ret);
717 EXPECT_EQ(cx_strcmp(tok, cx_str("a")), 0);
718 EXPECT_EQ(ctx.pos, 0);
719 EXPECT_EQ(ctx.next_pos, 2);
720 EXPECT_EQ(ctx.delim_pos, 1);
721 EXPECT_EQ(ctx.found, 1);
722
723 ret = cx_strtok_next(&ctx, &tok);
724 ASSERT_TRUE(ret);
725 EXPECT_EQ(cx_strcmp(tok, cx_str("comma")), 0);
726 EXPECT_EQ(ctx.pos, 2);
727 EXPECT_EQ(ctx.next_pos, 8);
728 EXPECT_EQ(ctx.delim_pos, 7);
729 EXPECT_EQ(ctx.found, 2);
730
731 ret = cx_strtok_next(&ctx, &tok);
732 ASSERT_TRUE(ret);
733 EXPECT_EQ(cx_strcmp(tok, cx_str("separated")), 0);
734 EXPECT_EQ(ctx.pos, 8);
735 EXPECT_EQ(ctx.next_pos, 18);
736 EXPECT_EQ(ctx.delim_pos, 17);
737 EXPECT_EQ(ctx.found, 3);
738
739 ret = cx_strtok_next(&ctx, &tok);
740 ASSERT_FALSE(ret);
741 EXPECT_EQ(ctx.pos, 8);
742 EXPECT_EQ(ctx.next_pos, 18);
743 EXPECT_EQ(ctx.delim_pos, 17);
744 EXPECT_EQ(ctx.found, 3);
745 }
746
747 TEST(String, strtok_next_unlimited) {
748 cxstring str = cx_str("some;-;otherwise;-;separated;-;string;-;");
749 cxstring delim = cx_str(";-;");
750 CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX);
751 bool ret;
752 cxstring tok;
753
754 ret = cx_strtok_next(&ctx, &tok);
755 ASSERT_TRUE(ret);
756 EXPECT_EQ(cx_strcmp(tok, cx_str("some")), 0);
757 EXPECT_EQ(ctx.pos, 0);
758 EXPECT_EQ(ctx.next_pos, 7);
759 EXPECT_EQ(ctx.delim_pos, 4);
760 EXPECT_EQ(ctx.found, 1);
761
762 ret = cx_strtok_next(&ctx, &tok);
763 ASSERT_TRUE(ret);
764 EXPECT_EQ(cx_strcmp(tok, cx_str("otherwise")), 0);
765 EXPECT_EQ(ctx.pos, 7);
766 EXPECT_EQ(ctx.next_pos, 19);
767 EXPECT_EQ(ctx.delim_pos, 16);
768 EXPECT_EQ(ctx.found, 2);
769
770 ret = cx_strtok_next(&ctx, &tok);
771 ASSERT_TRUE(ret);
772 EXPECT_EQ(cx_strcmp(tok, cx_str("separated")), 0);
773 EXPECT_EQ(ctx.pos, 19);
774 EXPECT_EQ(ctx.next_pos, 31);
775 EXPECT_EQ(ctx.delim_pos, 28);
776 EXPECT_EQ(ctx.found, 3);
777
778 ret = cx_strtok_next(&ctx, &tok);
779 ASSERT_TRUE(ret);
780 EXPECT_EQ(cx_strcmp(tok, cx_str("string")), 0);
781 EXPECT_EQ(ctx.pos, 31);
782 EXPECT_EQ(ctx.next_pos, 40);
783 EXPECT_EQ(ctx.delim_pos, 37);
784 EXPECT_EQ(ctx.found, 4);
785
786 ret = cx_strtok_next(&ctx, &tok);
787 ASSERT_TRUE(ret);
788 EXPECT_EQ(cx_strcmp(tok, cx_str("")), 0);
789 EXPECT_EQ(ctx.pos, 40);
790 EXPECT_EQ(ctx.next_pos, 40);
791 EXPECT_EQ(ctx.delim_pos, 40);
792 EXPECT_EQ(ctx.found, 5);
793
794 ret = cx_strtok_next(&ctx, &tok);
795 ASSERT_FALSE(ret);
796 EXPECT_EQ(ctx.pos, 40);
797 EXPECT_EQ(ctx.delim_pos, 40);
798 EXPECT_EQ(ctx.found, 5);
799 }
800
801 TEST(String, strtok_next_advanced) {
802 cxmutstr str = cx_strdup(cx_str("an,arbitrarily;||separated;string"));
803 cxstring delim = cx_str(",");
804 cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")};
805 CxStrtokCtx ctx = cx_strtok_m(str, delim, 10);
806 cx_strtok_delim(&ctx, delim_more, 2);
807 bool ret;
808 cxmutstr tok;
809
810 ret = cx_strtok_next_m(&ctx, &tok);
811 ASSERT_TRUE(ret);
812 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("an")), 0);
813 EXPECT_EQ(ctx.pos, 0);
814 EXPECT_EQ(ctx.next_pos, 3);
815 EXPECT_EQ(ctx.delim_pos, 2);
816 EXPECT_EQ(ctx.found, 1);
817 cx_strupper(tok);
818
819 ret = cx_strtok_next_m(&ctx, &tok);
820 ASSERT_TRUE(ret);
821 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("arbitrarily")), 0);
822 EXPECT_EQ(ctx.pos, 3);
823 EXPECT_EQ(ctx.next_pos, 15);
824 EXPECT_EQ(ctx.delim_pos, 14);
825 EXPECT_EQ(ctx.found, 2);
826 cx_strupper(tok);
827
828 ret = cx_strtok_next_m(&ctx, &tok);
829 ASSERT_TRUE(ret);
830 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("")), 0);
831 EXPECT_EQ(ctx.pos, 15);
832 EXPECT_EQ(ctx.next_pos, 17);
833 EXPECT_EQ(ctx.delim_pos, 15);
834 EXPECT_EQ(ctx.found, 3);
835 cx_strupper(tok);
836
837 ret = cx_strtok_next_m(&ctx, &tok);
838 ASSERT_TRUE(ret);
839 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("separated")), 0);
840 EXPECT_EQ(ctx.pos, 17);
841 EXPECT_EQ(ctx.next_pos, 27);
842 EXPECT_EQ(ctx.delim_pos, 26);
843 EXPECT_EQ(ctx.found, 4);
844 cx_strupper(tok);
845
846 ret = cx_strtok_next_m(&ctx, &tok);
847 ASSERT_TRUE(ret);
848 EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("string")), 0);
849 EXPECT_EQ(ctx.pos, 27);
850 EXPECT_EQ(ctx.next_pos, 33);
851 EXPECT_EQ(ctx.delim_pos, 33);
852 EXPECT_EQ(ctx.found, 5);
853 cx_strupper(tok);
854
855 ret = cx_strtok_next_m(&ctx, &tok);
856 ASSERT_FALSE(ret);
857 EXPECT_EQ(ctx.pos, 27);
858 EXPECT_EQ(ctx.next_pos, 33);
859 EXPECT_EQ(ctx.delim_pos, 33);
860 EXPECT_EQ(ctx.found, 5);
861
862 EXPECT_EQ(cx_strcmp(cx_strcast(str), cx_str("AN,ARBITRARILY;||SEPARATED;STRING")), 0);
863
864 cx_strfree(&str);
865 }

mercurial