From c68b349396adda8fca29fee180567f72dc3aed9d Mon Sep 17 00:00:00 2001 From: Graham Percival Date: Sat, 27 Jan 2024 14:36:24 -0800 Subject: [PATCH 1/3] tests/humansize: comments and clarifications --- tests/humansize/main.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/humansize/main.c b/tests/humansize/main.c index 7b6a57d3..bcfa2ec6 100644 --- a/tests/humansize/main.c +++ b/tests/humansize/main.c @@ -9,6 +9,7 @@ static struct testcase { int bad; uint64_t val; } tests[] = { + /* good strings */ { "1", 0, 1ULL }, { "2B", 0, 2ULL }, { "1234G", 0, 1234000000000ULL }, @@ -16,6 +17,7 @@ static struct testcase { { "0 E", 0, 0ULL }, { "321 k", 0, 321000ULL }, { "10 EB", 0, 10000000000000000000ULL }, + /* bad strings */ { "", 1, 0ULL }, { "1 BB", 1, 0ULL }, { "1 EB", 1, 0ULL }, @@ -33,7 +35,15 @@ main(int argc, char * argv[]) (void)argc; /* UNUSED */ for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { - printf("Parsing \"%s\" as a number of bytes", tests[i].s); + /* Notify about what we're trying to do. */ + if (!tests[i].bad) + printf("Parsing \"%s\" as a number of bytes:", + tests[i].s); + else + printf("Should fail to parse \"%s\" as a number" + " of bytes:", tests[i].s); + + /* Try to parse the test string. */ if (humansize_parse(tests[i].s, &size)) { if (!tests[i].bad) { printf(" FAILED!\n"); From a92f46bf34e02cfe1118791e99488de0f3217437 Mon Sep 17 00:00:00 2001 From: Graham Percival Date: Sat, 27 Jan 2024 14:36:25 -0800 Subject: [PATCH 2/3] tests/humansize: check generated strings --- tests/humansize/main.c | 46 ++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/tests/humansize/main.c b/tests/humansize/main.c index bcfa2ec6..8551d297 100644 --- a/tests/humansize/main.c +++ b/tests/humansize/main.c @@ -1,5 +1,8 @@ +#include #include #include +#include +#include #include "humansize.h" #include "warnp.h" @@ -8,20 +11,21 @@ static struct testcase { const char * s; int bad; uint64_t val; + const char * canonical; } tests[] = { /* good strings */ - { "1", 0, 1ULL }, - { "2B", 0, 2ULL }, - { "1234G", 0, 1234000000000ULL }, - { "9 B", 0, 9ULL }, - { "0 E", 0, 0ULL }, - { "321 k", 0, 321000ULL }, - { "10 EB", 0, 10000000000000000000ULL }, + { "1", 0, 1ULL, "1 B" }, + { "2B", 0, 2ULL, "2 B" }, + { "1234G", 0, 1234000000000ULL, "1.2 TB" }, + { "9 B", 0, 9ULL, "9 B" }, + { "0 E", 0, 0ULL, "0 B" }, + { "321 k", 0, 321000ULL, "321 kB" }, + { "10 EB", 0, 10000000000000000000ULL, "10 EB" }, /* bad strings */ - { "", 1, 0ULL }, - { "1 BB", 1, 0ULL }, - { "1 EB", 1, 0ULL }, - { "100 EB", 1, 0ULL } + { "", 1, 0ULL, "" }, + { "1 BB", 1, 0ULL, "" }, + { "1 EB", 1, 0ULL, "" }, + { "100 EB", 1, 0ULL, "" } }; int @@ -29,6 +33,7 @@ main(int argc, char * argv[]) { size_t i; uint64_t size; + char * s; int failures = 0; WARNP_INIT; @@ -61,6 +66,25 @@ main(int argc, char * argv[]) } } + for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { + /* Don't try to do anything with the "bad values". */ + if (tests[i].bad) + continue; + + /* Notify about what we're trying to do. */ + printf("Generating a string for %" PRIu64 ":", tests[i].val); + + /* Generate (and check) a string. */ + s = humansize(tests[i].val); + if (strcmp(tests[i].canonical, s) == 0) + printf(" PASSED!\n"); + else { + printf(" FAILED!\n"); + failures++; + } + free(s); + } + if (failures) return (1); else From 08df7ca63c5999b011f0e4193dd82ec03300e4e6 Mon Sep 17 00:00:00 2001 From: Graham Percival Date: Sat, 27 Jan 2024 14:36:27 -0800 Subject: [PATCH 3/3] tests/mpool: allocate 128 bytes instead of 4 This is needed for FreeBSD 14.0 with clang; their malloc is allocating a slab of pointers at a time, which benefits small allocations. Our mpool test is not intended as a serious benchmark of memory allocation speeds; rather, it's a quick check that nothing unusual is happening with the platform's kernel and libc. For a serious comparison, we would need to check the actual scenarios: for example, are we allocating 4096 records of 16 bytes, or 16 records of 52 bytes? --- tests/mpool/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/mpool/main.c b/tests/mpool/main.c index 6a7605c2..52e79dc4 100644 --- a/tests/mpool/main.c +++ b/tests/mpool/main.c @@ -12,7 +12,7 @@ #define INITIAL_POOL 100 struct stuff { - int i; + uint8_t buf[128]; }; MPOOL(stuff, struct stuff, INITIAL_POOL); @@ -28,8 +28,8 @@ check_mem(volatile struct stuff * vol) { /* This is `volatile`, so it shouldn't be optimized out. */ - vol->i = 0; - if (vol->i != 0) + vol->buf[0] = 0; + if (vol->buf[0] != 0) goto err0; /* Success! */