diff options
Diffstat (limited to 'src/tc-string.c')
| -rw-r--r-- | src/tc-string.c | 68 |
1 files changed, 42 insertions, 26 deletions
diff --git a/src/tc-string.c b/src/tc-string.c index d777268..1c67ede 100644 --- a/src/tc-string.c +++ b/src/tc-string.c @@ -108,23 +108,22 @@ static void t_strncat(void) printf("String: >%s<\n", data); } -static void t_strnlen(void) -{ - static const char s[] = "Hello world"; - printf("# strnlen: %" HX_SIZET_FMT "u %" HX_SIZET_FMT "u " - "%" HX_SIZET_FMT "u %" HX_SIZET_FMT "u %" HX_SIZET_FMT "u\n", - HX_strnlen(s, -1), HX_strnlen(s, 0), HX_strnlen(s, 1), - HX_strnlen(s, strlen(s)), HX_strnlen(s, 999)); -} - static void t_strsep(void) { - char b[] = "jengelh:x:1500:100:Jan Engelhardt:/home/jengelh:/bin/bash"; + char orig[] = "jengelh:x:1500:100:Jan Engelhardt:/home/jengelh:/bin/bash"; + char b[sizeof(orig)]; char *wp = b, *ret; printf("# strsep\n"); + memcpy(b, orig, sizeof(orig)); while ((ret = HX_strsep2(&wp, ":")) != NULL) printf("%s\n", ret); + + printf("# strtok_r\n"); + memcpy(b, orig, sizeof(orig)); + wp = b; + while ((ret = strtok_r(nullptr, ":", &wp)) != nullptr) + printf("%s\n", ret); } static void t_strtrim(void) @@ -132,11 +131,6 @@ static void t_strtrim(void) char a[] = " a and b ", aexp[] = "a and b "; char b[] = " a and b ", bexp[] = " a and b"; char c[] = "a&b", cexp[] = "a&b"; - const char *r; - - r = HX_stpltrim(a); - printf("HX_stpltrim(\"%s\") = \"%s\"\n", a, r); - assert(strcmp(r, aexp) == 0); printf("HX_strltrim(\"%s\") = ", a); printf("\"%s\"\n", (HX_strltrim(a), a)); @@ -146,7 +140,6 @@ static void t_strtrim(void) printf("\"%s\"\n", (HX_strrtrim(b), b)); assert(strcmp(b, bexp) == 0); - assert(strcmp(cexp, HX_stpltrim(c)) == 0); assert(strcmp(cexp, (HX_strltrim(c), c)) == 0); assert(strcmp(cexp, (HX_strrtrim(c), c)) == 0); } @@ -399,14 +392,19 @@ static int t_time_units(void) static int t_time_strto(void) { #define NS_PER_S 1000000000ULL + #define S_PER_Y 31557600 + enum { + NO_NSEC = 0x2U, + }; static const struct { const char *input; unsigned long long expect_s, expect_ns; const char expect_rem[16]; + unsigned int flags; } vt[] = { {"29µs", 0, 29000, ""}, - {"1y", 31557600, NS_PER_S * 31557600, ""}, - {"1y1month1week1d1h1min1s ", 31557600+2629800+86400*8+3600+60+1, NS_PER_S * (31557600+2629800+86400*8+3600+60+1), ""}, + {"1y", S_PER_Y, NS_PER_S * S_PER_Y, ""}, + {"1y1month1week1d1h1min1s ", S_PER_Y+2629800+86400*8+3600+60+1, NS_PER_S * (S_PER_Y+2629800+86400*8+3600+60+1), ""}, {" -1d", 0, 0, "-1d"}, {"1 -", 0, 0, "1 -"}, {"12.5 hours .5 hours 240 minutes 25200 seconds", 86400, NS_PER_S * 86400, ""}, @@ -418,23 +416,42 @@ static int t_time_strto(void) {"1s0.0", 1, NS_PER_S, ""}, {"1s1s", 2, 2 * NS_PER_S, ""}, {"1s1", 1, 1 * NS_PER_S, "1"}, + {"584542046090y", 584542046090ULL * S_PER_Y, ULLONG_MAX, "", NO_NSEC}, + {"584542046090y19767615s", ULLONG_MAX, ULLONG_MAX, "", NO_NSEC}, + {"584542046090y19767616s", ULLONG_MAX, ULLONG_MAX, "19767616s", NO_NSEC}, + {"584542046090y", 584542046090ULL * S_PER_Y, ULLONG_MAX, "", NO_NSEC}, {"584542046091y", ULLONG_MAX, ULLONG_MAX, "584542046091y"}, + {"584542046091.0y", ULLONG_MAX, ULLONG_MAX, "584542046091.0y"}, + {"P1M", S_PER_Y / 12, S_PER_Y / 12 * NS_PER_S, ""}, + {"p1m", 0, 0, "p1m"}, + {"PT1M", 60, 60 * NS_PER_S, ""}, + {"P584542046090Y", 584542046090ULL * S_PER_Y, ULLONG_MAX, "", NO_NSEC}, + {"P0DT0H0M0.0S", 0, 0}, + {"P0DT12H45M0.0S", 12 * 3600 + 45 * 60, (12 * 3600 + 45 * 60) * NS_PER_S}, + {"P1D", 86400, 86400 * NS_PER_S}, }; - char *end; printf("===== t_time_strto\n"); for (size_t i = 0; i < ARRAY_SIZE(vt); ++i) { + char *end, *end_ns; unsigned long long q = HX_strtoull_sec(vt[i].input, &end); - unsigned long long qn = HX_strtoull_nsec(vt[i].input, &end); - printf("Observed: \"%s\" => %llus [%lluns] + \"%s\"\n", vt[i].input, q, qn, end); - if (q != vt[i].expect_s || qn != vt[i].expect_ns) { - printf("Expected: %llus [%lluns]\n", vt[i].expect_s, vt[i].expect_ns); + unsigned long long qn = HX_strtoull_nsec(vt[i].input, &end_ns); + printf("Observed: \"%s\" => %llus + \"%s\" [%lluns + \"%s\"]\n", + vt[i].input, q, end, qn, end_ns); + if (q != vt[i].expect_s || strcmp(end, vt[i].expect_rem) != 0) { + printf("-!- Expected: %llus + \"%s\"\n", vt[i].expect_s, vt[i].expect_rem); return EXIT_FAILURE; } - if (strcmp(end, vt[i].expect_rem) != 0) { - printf("Expected: remainder \"%s\"\n", vt[i].expect_rem); + if (vt[i].flags & NO_NSEC) + continue; + if (qn != vt[i].expect_ns || strcmp(end_ns, vt[i].expect_rem) != 0) { + printf("-!- Expected: %llus + \"%s\"\n", vt[i].expect_ns, vt[i].expect_rem); return EXIT_FAILURE; } } + if (HX_strtoull8601p_sec("P1D", nullptr) != 86400) { + printf("8601p_sec failed\n"); + return EXIT_FAILURE; + } return EXIT_SUCCESS; } @@ -496,7 +513,6 @@ static int runner(int argc, char **argv) if (ret != EXIT_SUCCESS) return EXIT_FAILURE; t_strncat(); - t_strnlen(); t_strdup(); t_strsep(); t_strtrim(); |
