Skip to content

Commit

Permalink
Merge pull request #511 from Tarsnap/tests-update
Browse files Browse the repository at this point in the history
Update mpool and humansize tests
  • Loading branch information
cperciva authored Jan 28, 2024
2 parents ca04c8b + 08df7ca commit f70da0e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
58 changes: 46 additions & 12 deletions tests/humansize/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "humansize.h"
#include "warnp.h"
Expand All @@ -8,32 +11,44 @@ static struct testcase {
const char * s;
int bad;
uint64_t val;
const char * canonical;
} tests[] = {
{ "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, 0ULL },
{ "1 BB", 1, 0ULL },
{ "1 EB", 1, 0ULL },
{ "100 EB", 1, 0ULL }
/* good strings */
{ "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, "" }
};

int
main(int argc, char * argv[])
{
size_t i;
uint64_t size;
char * s;
int failures = 0;

WARNP_INIT;
(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");
Expand All @@ -51,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
Expand Down
6 changes: 3 additions & 3 deletions tests/mpool/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define INITIAL_POOL 100

struct stuff {
int i;
uint8_t buf[128];
};

MPOOL(stuff, struct stuff, INITIAL_POOL);
Expand All @@ -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! */
Expand Down

0 comments on commit f70da0e

Please sign in to comment.