diff options
Diffstat (limited to 'src/tc-string.c')
-rw-r--r-- | src/tc-string.c | 173 |
1 files changed, 60 insertions, 113 deletions
diff --git a/src/tc-string.c b/src/tc-string.c index 1c7ed09..e7f90c2 100644 --- a/src/tc-string.c +++ b/src/tc-string.c @@ -197,93 +197,6 @@ static void t_split2(void) HX_zvecfree(a); } -/* avoid these being inlined */ -extern char *f_strlcpy_str(char *, const char *, size_t); -extern char *f_strlcpy_mem(char *, const char *, size_t); - -EXPORT_SYMBOL char *f_strlcpy_str(char *d, const char *s, size_t n) -{ - if (n == 0) - return d; - strncpy(d, s, n); - d[n-1] = '\0'; - return d; -} - -EXPORT_SYMBOL char *f_strlcpy_mem(char *dest, const char *src, size_t dsize) -{ - size_t slen = strlen(src); - if (slen < dsize) - return static_cast(char *, memcpy(dest, src, slen + 1)); - if (dsize > 0) { - memcpy(dest, src, dsize - 1); - dest[dsize-1] = '\0'; - } - return dest; -} - -static const char s_lorem_ipsum[] = /* 1368 chars */ -"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo " -"ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis " -"parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, " -"pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec " -"pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, " -"rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede " -"mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper " -"nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, " -"consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra " -"quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. " -"Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur " -"ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, " -"tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing " -"sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit " -"id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut " -"libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros " -"faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec " -"sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit " -"cursus nunc,"; - -static void t_strlcpy(void) -{ - static const size_t picksizes[] = - {4, 8, 16, 32, 64, 80, 128, 256, 1024, 2048}; - char ibuf[2048], obuf[2048]; - size_t ipick, opick, k, runs = 10000000 + HX_irand(0, 1); - struct timespec start, stop, d1, d2, d3; - - for (ipick = 0; ipick < ARRAY_SIZE(picksizes); ++ipick) { - /* Select string size */ - HX_strlcpy(ibuf, s_lorem_ipsum, picksizes[ipick]); - - for (opick = 0; opick < ARRAY_SIZE(picksizes); ++opick) { - /* Select buffer size */ - clock_gettime(CLOCK_MONOTONIC, &start); - for (k = 0; k < runs; ++k) - f_strlcpy_str(reinterpret_cast(char *, obuf), - ibuf, picksizes[opick]); - clock_gettime(CLOCK_MONOTONIC, &stop); - HX_timespec_sub(&d1, &stop, &start); - - clock_gettime(CLOCK_MONOTONIC, &start); - for (k = 0; k < runs; ++k) - f_strlcpy_mem(reinterpret_cast(char *, obuf), - ibuf, picksizes[opick]); - clock_gettime(CLOCK_MONOTONIC, &stop); - HX_timespec_sub(&d2, &stop, &start); - - HX_timespec_sub(&d3, &d1, &d2); - printf("%4zu->%4zu: " HX_TIMESPEC_FMT - " (str=" HX_TIMESPEC_FMT - " mem=" HX_TIMESPEC_FMT ")\n", - strlen(ibuf), picksizes[opick], - HX_TIMESPEC_EXP(&d3), - HX_TIMESPEC_EXP(&d1), - HX_TIMESPEC_EXP(&d2) - ); - } - } -} - static void t_strlcpy2(void) { char a[3] = {49, 49, 49}; @@ -381,8 +294,8 @@ static int t_units_strto(void) unsigned long long expect_out; const char expect_rem[8]; } vt[] = { - {"-5k", 1000, ULLONG_MAX, "-5k"}, - {" -5.2k", 1000, ULLONG_MAX, "-5.2k"}, + {"-5k", 1000, -5000ULL, ""}, + {" -5.2k", 1000, -5200ULL, ""}, {"1", 9999, 1, ""}, {"1024", 9999, 1ULL << 10, ""}, {"1048576", 9999, 1ULL << 20, ""}, @@ -403,6 +316,10 @@ static int t_units_strto(void) {"1T", 1024, 1ULL << 40, ""}, {"1P", 1024, 1ULL << 50, ""}, {"1E", 1024, 1ULL << 60, ""}, + {"15E", 1024, 15ULL << 60, ""}, + {"16E", 1024, ULLONG_MAX, ""}, + {"16.0E", 1024, ULLONG_MAX, ""}, + {"1Z", 1024, ULLONG_MAX, ""}, {"0", 0, 0, ""}, {"0k", 0, 0, ""}, {"0 Z", 0, 0, ""}, @@ -412,18 +329,28 @@ static int t_units_strto(void) {"0.00000000000000001E", 1024, 11, ""}, {"1.525444GiB", 1000, 1525444000, "iB"}, {"1.525444GiB", 1024, 1637933022, "iB"}, + {"2M4k", 1000, 2000000, "4k"}, + {"18446744073709551614", 0, 18446744073709551614ULL, ""}, + {"18446744073709551615", 0, ULLONG_MAX, ""}, + {"18446744073709551616", 0, ULLONG_MAX, ""}, + {"-18446744073709551614", 0, 2, ""}, + {"-18446744073709551615", 0, 1, ""}, + {"-18446744073709551616", 0, ULLONG_MAX, ""}, }; char *end; for (size_t i = 0; i < ARRAY_SIZE(vt); ++i) { unsigned long long q = HX_strtoull_unit(vt[i].input, &end, vt[i].exponent); - printf("%s -> %llu __ %s\n", vt[i].input, q, end); - if (q != vt[i].expect_out || strcmp(end, vt[i].expect_rem) != 0) + printf("Observed: %s -> %llu __ %s\n", vt[i].input, q, end); + if (q != vt[i].expect_out || strcmp(end, vt[i].expect_rem) != 0) { + printf("Expected: %s -> %llu __ %s\n", vt[i].input, + vt[i].expect_out, vt[i].expect_rem); return EXIT_FAILURE; + } } return EXIT_SUCCESS; } -static void t_time_units(void) +static int t_time_units(void) { static const struct { unsigned long long input; @@ -432,10 +359,13 @@ static void t_time_units(void) } vt[] = { {31536000, 0, "365d"}, {31622400, 0, "366d"}, - {31622400, HXUNIT_YEARS, "1y18h"}, - {31622400, HXUNIT_MONTHS, "1y"}, - {31622400, HXUNIT_WEEKS, "1y"}, - {31622400, HXUNIT_MONTHS | HXUNIT_WEEKS, "1y"}, + {34819200, HXUNIT_WEEKS, "57weeks4d"}, + {34819200, HXUNIT_MONTHS, "13months7d7h30min"}, + {34819200, HXUNIT_MONTHS | HXUNIT_WEEKS, "13months1week7h30min"}, + {34819200, HXUNIT_YEARS, "1y37d18h"}, + {34819200, HXUNIT_YEARS | HXUNIT_WEEKS, "1y5weeks2d18h"}, + {34819200, HXUNIT_YEARS | HXUNIT_MONTHS, "1y1month7d7h30min"}, + {34819200, HXUNIT_YEARS | HXUNIT_MONTHS | HXUNIT_WEEKS, "1y1month1week7h30min"}, {2678400, HXUNIT_MONTHS, "1month13h30min"}, {2592000, HXUNIT_MONTHS, "30d"}, {608400, HXUNIT_WEEKS, "1week1h"}, @@ -449,41 +379,55 @@ static void t_time_units(void) for (size_t i = 0; i < ARRAY_SIZE(vt); ++i) { char out[60]; char *ret = HX_unit_seconds(out, ARRAY_SIZE(out), vt[i].input, vt[i].flags); - printf("%llus => \"%s\"\n", vt[i].input, ret); - if (strcmp(ret, vt[i].expect_out) != 0) - printf("\tBUG, expected \"%s\"\n", vt[i].expect_out); + printf("Observed: %llus => \"%s\"\n", vt[i].input, ret); + if (strcmp(ret, vt[i].expect_out) != 0) { + printf("Expected: \"%s\"\n", vt[i].expect_out); + return EXIT_FAILURE; + } } + return EXIT_SUCCESS; } -static void t_time_strto(void) +static int t_time_strto(void) { #define NS_PER_S 1000000000ULL static const struct { const char *input; unsigned long long expect_s, expect_ns; - const char expect_rem[8]; + const char expect_rem[16]; } 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), ""}, {" -1d", 0, 0, "-1d"}, - {"1 -", 1, NS_PER_S, "-"}, + {"1 -", 0, 0, "1 -"}, {"12.5 hours .5 hours 240 minutes 25200 seconds", 86400, NS_PER_S * 86400, ""}, {"1s", 1, NS_PER_S, ""}, {"1min", 60, 60 * NS_PER_S, ""}, {"0", 0, 0, ""}, + {"0.0", 0, 0, ""}, + {"1s0", 1, NS_PER_S, ""}, + {"1s0.0", 1, NS_PER_S, ""}, + {"1s1s", 2, 2 * NS_PER_S, ""}, + {"1s1", 1, 1 * NS_PER_S, "1"}, + {"584542046091y", ULLONG_MAX, ULLONG_MAX, "584542046091y"}, }; char *end; printf("===== t_time_strto\n"); for (size_t i = 0; i < ARRAY_SIZE(vt); ++i) { unsigned long long q = HX_strtoull_sec(vt[i].input, &end); unsigned long long qn = HX_strtoull_nsec(vt[i].input, &end); - printf("\"%s\" => %llus [%lluns] + \"%s\"\n", vt[i].input, q, qn, end); - if (q != vt[i].expect_s || qn != vt[i].expect_ns) - printf("\tBUG: expected %llus [%lluns]\n", vt[i].expect_s, vt[i].expect_ns); - if (strcmp(end, vt[i].expect_rem) != 0) - printf("\tBUG: expected remainder \"%s\"\n", vt[i].expect_rem); + 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); + return EXIT_FAILURE; + } + if (strcmp(end, vt[i].expect_rem) != 0) { + printf("Expected: remainder \"%s\"\n", vt[i].expect_rem); + return EXIT_FAILURE; + } } + return EXIT_SUCCESS; } static int t_strmid(void) @@ -515,7 +459,7 @@ static int t_strmid(void) #undef T } -static int runner(int argc, const char **argv) +static int runner(int argc, char **argv) { hxmc_t *tx = NULL; const char *file = (argc >= 2) ? argv[1] : "tx-string.cpp"; @@ -559,19 +503,22 @@ static int runner(int argc, const char **argv) ret = t_units_strto(); if (ret != EXIT_SUCCESS) return EXIT_FAILURE; - t_time_units(); - t_time_strto(); - t_strlcpy(); + ret = t_time_units(); + if (ret != EXIT_SUCCESS) + return EXIT_FAILURE; + ret = t_time_strto(); + if (ret != EXIT_SUCCESS) + return EXIT_FAILURE; t_strlcpy2(); HXmc_free(tx); HX_exit(); return EXIT_SUCCESS; } -int main(int argc, const char **argv) +int main(int argc, char **argv) { int ret = runner(argc, argv); - if (ret != EXIT_FAILURE) + if (ret == EXIT_FAILURE) fprintf(stderr, "FAILED\n"); return ret; } |