test/test_string.cpp

changeset 645
ec50abb285ad
parent 628
1e2be40f0cb5
--- a/test/test_string.cpp	Wed Feb 01 18:07:16 2023 +0100
+++ b/test/test_string.cpp	Thu Feb 02 20:25:34 2023 +0100
@@ -653,3 +653,213 @@
     EXPECT_STREQ(str.ptr, "this 1s @ te$t");
     cx_strfree(&str);
 }
+
+TEST(String, strtok) {
+    cxstring str = cx_str("a,comma,separated,string");
+    cxstring delim = cx_str(",");
+    CxStrtokCtx ctx = cx_strtok(str, delim, 3);
+    EXPECT_EQ(ctx.str.ptr, str.ptr);
+    EXPECT_EQ(ctx.str.length, str.length);
+    EXPECT_EQ(ctx.delim.ptr, delim.ptr);
+    EXPECT_EQ(ctx.delim.length, delim.length);
+    EXPECT_EQ(ctx.limit, 3);
+    EXPECT_EQ(ctx.found, 0);
+    EXPECT_EQ(ctx.pos, 0);
+    EXPECT_EQ(ctx.next_pos, 0);
+    EXPECT_EQ(ctx.delim_more, nullptr);
+    EXPECT_EQ(ctx.delim_more_count, 0);
+}
+
+TEST(String, strtok_m) {
+    cxmutstr str = cx_strdup(cx_str("a,comma,separated,string"));
+    cxstring delim = cx_str(",");
+    CxStrtokCtx ctx = cx_strtok_m(str, delim, 3);
+    EXPECT_EQ(ctx.str.ptr, str.ptr);
+    EXPECT_EQ(ctx.str.length, str.length);
+    EXPECT_EQ(ctx.delim.ptr, delim.ptr);
+    EXPECT_EQ(ctx.delim.length, delim.length);
+    EXPECT_EQ(ctx.limit, 3);
+    EXPECT_EQ(ctx.found, 0);
+    EXPECT_EQ(ctx.pos, 0);
+    EXPECT_EQ(ctx.next_pos, 0);
+    EXPECT_EQ(ctx.delim_more, nullptr);
+    EXPECT_EQ(ctx.delim_more_count, 0);
+    cx_strfree(&str);
+}
+
+TEST(String, strtok_delim) {
+    cxstring str = cx_str("an,arbitrarily|separated;string");
+    cxstring delim = cx_str(",");
+    cxstring delim_more[2] = {CX_STR("|"), CX_STR(";")};
+    CxStrtokCtx ctx = cx_strtok(str, delim, 3);
+    cx_strtok_delim(&ctx, delim_more, 2);
+    EXPECT_EQ(ctx.str.ptr, str.ptr);
+    EXPECT_EQ(ctx.str.length, str.length);
+    EXPECT_EQ(ctx.delim.ptr, delim.ptr);
+    EXPECT_EQ(ctx.delim.length, delim.length);
+    EXPECT_EQ(ctx.limit, 3);
+    EXPECT_EQ(ctx.found, 0);
+    EXPECT_EQ(ctx.pos, 0);
+    EXPECT_EQ(ctx.next_pos, 0);
+    EXPECT_EQ(ctx.delim_more, delim_more);
+    EXPECT_EQ(ctx.delim_more_count, 2);
+}
+
+TEST(String, strtok_next_easy) {
+    cxstring str = cx_str("a,comma,separated,string");
+    cxstring delim = cx_str(",");
+    CxStrtokCtx ctx = cx_strtok(str, delim, 3);
+    bool ret;
+    cxstring tok;
+
+    ret = cx_strtok_next(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(tok, cx_str("a")), 0);
+    EXPECT_EQ(ctx.pos, 0);
+    EXPECT_EQ(ctx.next_pos, 2);
+    EXPECT_EQ(ctx.delim_pos, 1);
+    EXPECT_EQ(ctx.found, 1);
+
+    ret = cx_strtok_next(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(tok, cx_str("comma")), 0);
+    EXPECT_EQ(ctx.pos, 2);
+    EXPECT_EQ(ctx.next_pos, 8);
+    EXPECT_EQ(ctx.delim_pos, 7);
+    EXPECT_EQ(ctx.found, 2);
+
+    ret = cx_strtok_next(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(tok, cx_str("separated")), 0);
+    EXPECT_EQ(ctx.pos, 8);
+    EXPECT_EQ(ctx.next_pos, 18);
+    EXPECT_EQ(ctx.delim_pos, 17);
+    EXPECT_EQ(ctx.found, 3);
+
+    ret = cx_strtok_next(&ctx, &tok);
+    ASSERT_FALSE(ret);
+    EXPECT_EQ(ctx.pos, 8);
+    EXPECT_EQ(ctx.next_pos, 18);
+    EXPECT_EQ(ctx.delim_pos, 17);
+    EXPECT_EQ(ctx.found, 3);
+}
+
+TEST(String, strtok_next_unlimited) {
+    cxstring str = cx_str("some;-;otherwise;-;separated;-;string;-;");
+    cxstring delim = cx_str(";-;");
+    CxStrtokCtx ctx = cx_strtok(str, delim, SIZE_MAX);
+    bool ret;
+    cxstring tok;
+
+    ret = cx_strtok_next(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(tok, cx_str("some")), 0);
+    EXPECT_EQ(ctx.pos, 0);
+    EXPECT_EQ(ctx.next_pos, 7);
+    EXPECT_EQ(ctx.delim_pos, 4);
+    EXPECT_EQ(ctx.found, 1);
+
+    ret = cx_strtok_next(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(tok, cx_str("otherwise")), 0);
+    EXPECT_EQ(ctx.pos, 7);
+    EXPECT_EQ(ctx.next_pos, 19);
+    EXPECT_EQ(ctx.delim_pos, 16);
+    EXPECT_EQ(ctx.found, 2);
+
+    ret = cx_strtok_next(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(tok, cx_str("separated")), 0);
+    EXPECT_EQ(ctx.pos, 19);
+    EXPECT_EQ(ctx.next_pos, 31);
+    EXPECT_EQ(ctx.delim_pos, 28);
+    EXPECT_EQ(ctx.found, 3);
+
+    ret = cx_strtok_next(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(tok, cx_str("string")), 0);
+    EXPECT_EQ(ctx.pos, 31);
+    EXPECT_EQ(ctx.next_pos, 40);
+    EXPECT_EQ(ctx.delim_pos, 37);
+    EXPECT_EQ(ctx.found, 4);
+
+    ret = cx_strtok_next(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(tok, cx_str("")), 0);
+    EXPECT_EQ(ctx.pos, 40);
+    EXPECT_EQ(ctx.next_pos, 40);
+    EXPECT_EQ(ctx.delim_pos, 40);
+    EXPECT_EQ(ctx.found, 5);
+
+    ret = cx_strtok_next(&ctx, &tok);
+    ASSERT_FALSE(ret);
+    EXPECT_EQ(ctx.pos, 40);
+    EXPECT_EQ(ctx.delim_pos, 40);
+    EXPECT_EQ(ctx.found, 5);
+}
+
+TEST(String, strtok_next_advanced) {
+    cxmutstr str = cx_strdup(cx_str("an,arbitrarily;||separated;string"));
+    cxstring delim = cx_str(",");
+    cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")};
+    CxStrtokCtx ctx = cx_strtok_m(str, delim, 10);
+    cx_strtok_delim(&ctx, delim_more, 2);
+    bool ret;
+    cxmutstr tok;
+
+    ret = cx_strtok_next_m(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("an")), 0);
+    EXPECT_EQ(ctx.pos, 0);
+    EXPECT_EQ(ctx.next_pos, 3);
+    EXPECT_EQ(ctx.delim_pos, 2);
+    EXPECT_EQ(ctx.found, 1);
+    cx_strupper(tok);
+
+    ret = cx_strtok_next_m(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("arbitrarily")), 0);
+    EXPECT_EQ(ctx.pos, 3);
+    EXPECT_EQ(ctx.next_pos, 15);
+    EXPECT_EQ(ctx.delim_pos, 14);
+    EXPECT_EQ(ctx.found, 2);
+    cx_strupper(tok);
+
+    ret = cx_strtok_next_m(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("")), 0);
+    EXPECT_EQ(ctx.pos, 15);
+    EXPECT_EQ(ctx.next_pos, 17);
+    EXPECT_EQ(ctx.delim_pos, 15);
+    EXPECT_EQ(ctx.found, 3);
+    cx_strupper(tok);
+
+    ret = cx_strtok_next_m(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("separated")), 0);
+    EXPECT_EQ(ctx.pos, 17);
+    EXPECT_EQ(ctx.next_pos, 27);
+    EXPECT_EQ(ctx.delim_pos, 26);
+    EXPECT_EQ(ctx.found, 4);
+    cx_strupper(tok);
+
+    ret = cx_strtok_next_m(&ctx, &tok);
+    ASSERT_TRUE(ret);
+    EXPECT_EQ(cx_strcmp(cx_strcast(tok), cx_str("string")), 0);
+    EXPECT_EQ(ctx.pos, 27);
+    EXPECT_EQ(ctx.next_pos, 33);
+    EXPECT_EQ(ctx.delim_pos, 33);
+    EXPECT_EQ(ctx.found, 5);
+    cx_strupper(tok);
+
+    ret = cx_strtok_next_m(&ctx, &tok);
+    ASSERT_FALSE(ret);
+    EXPECT_EQ(ctx.pos, 27);
+    EXPECT_EQ(ctx.next_pos, 33);
+    EXPECT_EQ(ctx.delim_pos, 33);
+    EXPECT_EQ(ctx.found, 5);
+
+    EXPECT_EQ(cx_strcmp(cx_strcast(str), cx_str("AN,ARBITRARILY;||SEPARATED;STRING")), 0);
+
+    cx_strfree(&str);
+}

mercurial