X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fbytestring%2Ftest%2Frun.c;h=3b563de3466812229ecf2b3a7bede2083a43de5a;hp=23415df58c3f4c5fa11d631f8951c6eb47a02c8f;hb=cb2d540f764b225a068a3cdd1396df4fc8dc6824;hpb=b529270bf1ce198a310046d83c8e5d9356dcfe15 diff --git a/ccan/bytestring/test/run.c b/ccan/bytestring/test/run.c index 23415df5..3b563de3 100644 --- a/ccan/bytestring/test/run.c +++ b/ccan/bytestring/test/run.c @@ -13,10 +13,11 @@ const char *str2 = TEST_STRING; int main(void) { - struct bytestring bs, bs1, bs2, bs3, bs4, bs5, bs6; + struct bytestring bs, bs1, bs2, bs3, bs4, bs5, bs6, bs7; + int n; /* This is how many tests you plan to run */ - plan_tests(53); + plan_tests(89); bs = bytestring(str1, sizeof(str1) - 1); ok1(bs.ptr == str1); @@ -100,6 +101,70 @@ int main(void) ok1(bytestring_spn(bs1, BYTESTRING("eginrst ")) == bs1.len); ok1(bytestring_cspn(bs2, BYTESTRING("z")) == bs2.len); + bs7 = bytestring_splitchr_first(bs, ' '); + ok1(bs7.ptr == bs.ptr); + ok1(bytestring_eq(bs7, BYTESTRING("test"))); + bs7 = bytestring_splitchr_next(bs, ' ', bs7); + ok1(bs7.ptr == bs.ptr + 5); + ok1(bytestring_eq(bs7, BYTESTRING("string"))); + bs7 = bytestring_splitchr_next(bs, ' ', bs7); + ok1(!bs7.ptr); + bs7 = bytestring_splitchr_next(bs, ' ', bs7); + ok1(!bs7.ptr); + + bs7 = bytestring_splitchr_first(bs2, '\0'); + ok1(bs7.ptr = bs2.ptr); + ok1(bytestring_eq(bs7, BYTESTRING("abc"))); + bs7 = bytestring_splitchr_next(bs2, '\0', bs7); + ok1(bs7.ptr == bs2.ptr + 4); + ok1(bytestring_eq(bs7, BYTESTRING("def"))); + bs7 = bytestring_splitchr_next(bs2, '\0', bs7); + ok1(!bs7.ptr); + bs7 = bytestring_splitchr_next(bs2, ' ', bs7); + ok1(!bs7.ptr); + + bs7 = bytestring_splitchr_first(bs, 's'); + ok1(bs7.ptr == bs.ptr); + ok1(bytestring_eq(bs7, BYTESTRING("te"))); + bs7 = bytestring_splitchr_next(bs, 's', bs7); + ok1(bs7.ptr == bs.ptr + 3); + ok1(bytestring_eq(bs7, BYTESTRING("t "))); + bs7 = bytestring_splitchr_next(bs, 's', bs7); + ok1(bs7.ptr == bs.ptr + 6); + ok1(bytestring_eq(bs7, BYTESTRING("tring"))); + bs7 = bytestring_splitchr_next(bs, 's', bs7); + ok1(!bs7.ptr); + bs7 = bytestring_splitchr_next(bs, ' ', bs7); + ok1(!bs7.ptr); + + bs7 = bytestring_splitchr_first(bs2, 'f'); + ok1(bs7.ptr = bs2.ptr); + ok1(bytestring_eq(bs7, BYTESTRING("abc\0de"))); + bs7 = bytestring_splitchr_next(bs2, 'f', bs7); + ok1(bs7.ptr == bs2.ptr + 7); + ok1(bs7.len == 0); + bs7 = bytestring_splitchr_next(bs2, 'f', bs7); + ok1(!bs7.ptr); + bs7 = bytestring_splitchr_next(bs2, 'f', bs7); + ok1(!bs7.ptr); + + bs7 = bytestring_splitchr_first(BYTESTRING(""), 'q'); + ok1(!bs7.ptr); + + n = 0; + bytestring_foreach_splitchr(bs7, BYTESTRING(" "), ' ') { + n++; + ok1(bs7.ptr && !bs7.len); + } + ok1(n == 3); + + n = 0; + bytestring_foreach_splitchr(bs7, BYTESTRING("\0\0\0"), '\0') { + n++; + ok1(bs7.ptr && !bs7.len); + } + ok1(n == 4); + /* This exits depending on whether all tests passed */ return exit_status(); }