Rename t_verbose*() to t_printv*().

Not only is this a slightly more logical name, but it allows us to expose the verbose flag, previously private to cryb_t_main.c, as the equally logically named t_verbose.
This commit is contained in:
Dag-Erling Smørgrav 2017-03-14 14:36:52 +01:00
parent 7b289d622f
commit 52cf1f9d3c
9 changed files with 80 additions and 79 deletions

View file

@ -60,6 +60,7 @@ struct t_test {
};
extern const char *t_progname;
extern int t_verbose;
void t_add_test(t_func *, void *, const char *, ...);
void t_add_tests(struct t_test *, int);
@ -69,8 +70,8 @@ typedef void (*t_cleanup_func)(void);
void t_main(t_prepare_func, t_cleanup_func, int, char **)
CRYB_NORETURN;
void t_verbose_hex(const uint8_t *, size_t);
void t_verbose(const char *, ...)
void t_printv_hex(const uint8_t *, size_t);
void t_printv(const char *, ...)
CRYB_PRINTF(1, 2);
#ifdef _IONBF /* proxy for <stdio.h> */

View file

@ -48,7 +48,7 @@ t_is_null(const void *ptr)
{
if (ptr != NULL) {
t_verbose("expected null pointer, got non-null pointer\n");
t_printv("expected null pointer, got non-null pointer\n");
return (0);
}
return (1);
@ -62,7 +62,7 @@ t_is_not_null(const void *ptr)
{
if (ptr == NULL) {
t_verbose("expected non-null pointer, got null pointer\n");
t_printv("expected non-null pointer, got null pointer\n");
return (0);
}
return (1);
@ -76,16 +76,16 @@ t_compare_ptr(const void *expected, const void *received)
{
if (expected == NULL && received != NULL) {
t_verbose("expected null pointer\n");
t_verbose("received %p\n", received);
t_printv("expected null pointer\n");
t_printv("received %p\n", received);
return (0);
} else if (received == NULL) {
t_verbose("expected %p\n", expected);
t_verbose("received null pointer\n");
t_printv("expected %p\n", expected);
t_printv("received null pointer\n");
return (0);
} else if (expected != received) {
t_verbose("expected %p\n", expected);
t_verbose("received %p\n", received);
t_printv("expected %p\n", expected);
t_printv("received %p\n", received);
return (0);
}
return (1);
@ -99,24 +99,24 @@ t_compare_mem(const void *expected, const void *received, size_t len)
{
if (expected == NULL && received != NULL) {
t_verbose("expected null pointer\n");
t_verbose("received ");
t_verbose_hex(received, len);
t_verbose("\n");
t_printv("expected null pointer\n");
t_printv("received ");
t_printv_hex(received, len);
t_printv("\n");
return (0);
} else if (received == NULL) {
t_verbose("expected ");
t_verbose_hex(expected, len);
t_verbose("\n");
t_verbose("received null pointer\n");
t_printv("expected ");
t_printv_hex(expected, len);
t_printv("\n");
t_printv("received null pointer\n");
return (0);
} else if (memcmp(expected, received, len) != 0) {
t_verbose("expected ");
t_verbose_hex(expected, len);
t_verbose("\n");
t_verbose("received ");
t_verbose_hex(received, len);
t_verbose("\n");
t_printv("expected ");
t_printv_hex(expected, len);
t_printv("\n");
t_printv("received ");
t_printv_hex(received, len);
t_printv("\n");
return (0);
}
return (1);
@ -130,16 +130,16 @@ t_compare_str(const char *expected, const char *received)
{
if (expected == NULL && received != NULL) {
t_verbose("expected null pointer\n");
t_verbose("received \"%s\"\n", received);
t_printv("expected null pointer\n");
t_printv("received \"%s\"\n", received);
return (0);
} else if (expected != NULL && received == NULL) {
t_verbose("expected \"%s\"\n", expected);
t_verbose("received null pointer\n");
t_printv("expected \"%s\"\n", expected);
t_printv("received null pointer\n");
return (0);
} else if (strcmp(expected, received) != 0) {
t_verbose("expected %s\n", expected);
t_verbose("received %s\n", received);
t_printv("expected %s\n", expected);
t_printv("received %s\n", received);
return (0);
}
return (1);
@ -154,16 +154,16 @@ t_compare_strn(const char *expected, const char *received, size_t len)
{
if (expected == NULL && received != NULL) {
t_verbose("expected null pointer\n");
t_verbose("received \"%.*s\"\n", (int)len, received);
t_printv("expected null pointer\n");
t_printv("received \"%.*s\"\n", (int)len, received);
return (0);
} else if (expected != NULL && received == NULL) {
t_verbose("expected \"%.*s\"\n", (int)len, expected);
t_verbose("received null pointer\n");
t_printv("expected \"%.*s\"\n", (int)len, expected);
t_printv("received null pointer\n");
return (0);
} else if (strncmp(expected, received, len) != 0) {
t_verbose("expected %.*s\n", (int)len, expected);
t_verbose("received %.*s\n", (int)len, received);
t_printv("expected %.*s\n", (int)len, expected);
t_printv("received %.*s\n", (int)len, received);
return (0);
}
return (1);
@ -178,8 +178,8 @@ t_compare_##n(t expected, t received) \
{ \
\
if (received != expected) { \
t_verbose("expected " pf "\n", expected); \
t_verbose("received " pf "\n", received); \
t_printv("expected " pf "\n", expected); \
t_printv("received " pf "\n", received); \
return (0); \
} \
return (1); \

View file

@ -46,7 +46,7 @@
const char *t_progname;
/* verbose flag */
static int verbose;
int t_verbose;
/* whether to check for leaks */
static int leaktest;
@ -55,10 +55,10 @@ static int leaktest;
* If verbose flag is set, print an array of bytes in hex
*/
void
t_verbose_hex(const uint8_t *buf, size_t len)
t_printv_hex(const uint8_t *buf, size_t len)
{
if (verbose) {
if (t_verbose) {
while (len--) {
fprintf(stderr, "%02x", *buf++);
if (len > 0 && len % 4 == 0)
@ -71,11 +71,11 @@ t_verbose_hex(const uint8_t *buf, size_t len)
* If verbose flag is set, print a message
*/
void
t_verbose(const char *fmt, ...)
t_printv(const char *fmt, ...)
{
va_list ap;
if (verbose) {
if (t_verbose) {
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
@ -190,7 +190,7 @@ t_run_test(struct t_test *t, int n)
char *desc;
int i, ret, signo;
if (leaktest && verbose)
if (leaktest && t_verbose)
t_malloc_snapshot(snap1, sizeof snap1);
for (i = 0; sigs[i] > 0; ++i)
signal(sigs[i], t_handle_signal);
@ -199,12 +199,12 @@ t_run_test(struct t_test *t, int n)
if ((signo = setjmp(sigjmp)) == 0)
ret = (*t->func)(&desc, t->arg);
if (t_malloc_fail != 0) {
t_verbose("WARNING: test case left t_malloc_fail = %d\n",
t_printv("WARNING: test case left t_malloc_fail = %d\n",
t_malloc_fail);
t_malloc_fail = 0;
}
if (t_malloc_fail_after != 0) {
t_verbose("WARNING: test case left t_malloc_fail_after = %d\n",
t_printv("WARNING: test case left t_malloc_fail_after = %d\n",
t_malloc_fail_after);
t_malloc_fail_after = 0;
}
@ -220,13 +220,13 @@ t_run_test(struct t_test *t, int n)
free(desc);
for (i = 0; sigs[i] > 0; ++i)
signal(sigs[i], SIG_DFL);
if (leaktest && verbose) {
if (leaktest && t_verbose) {
snaplen = t_malloc_snapshot(snap2, sizeof snap2);
if (snaplen > sizeof snap2)
snaplen = sizeof snap2;
t_compare_mem(snap1, snap2, snaplen);
if (memcmp(snap1, snap2, snaplen) != 0)
t_verbose("WARNING: allocator state changed\n");
t_printv("WARNING: allocator state changed\n");
}
return (ret);
}
@ -282,7 +282,7 @@ t_main(t_prepare_func t_prepare, t_cleanup_func t_cleanup,
leaktest = 1;
break;
case 'v':
verbose = 1;
t_verbose = 1;
break;
default:
usage();
@ -314,10 +314,10 @@ t_main(t_prepare_func t_prepare, t_cleanup_func t_cleanup,
free(t_plan);
setvbuf(stdout, NULL, _IONBF, 0);
if (leaktest) {
if (verbose)
if (t_verbose)
t_malloc_printstats(stderr);
t_run_test(&t_memory_leak, nt) ? ++pass : ++fail;
}
t_verbose("%d out of %zd tests passed\n", pass, nt);
t_printv("%d out of %zd tests passed\n", pass, nt);
exit(fail > 0 ? 1 : 0);
}

View file

@ -63,7 +63,7 @@ t_assert_child(void)
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE)
/* prevent core dump */
if (setrlimit(RLIMIT_CORE, &crl) != 0)
t_verbose("failed to disable core dump\n");
t_printv("failed to disable core dump\n");
#endif
/* suppress assertion message */
fclose(stderr);
@ -83,15 +83,15 @@ t_assert_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
}
t_assert(waitpid(pid, &status, 0) == pid);
if (!WIFSIGNALED(status)) {
t_verbose("expected child to raise a signal\n");
t_printv("expected child to raise a signal\n");
return (0);
}
#ifdef WCOREDUMP
if (WCOREDUMP(status))
t_verbose("warning: child dumped core\n");
t_printv("warning: child dumped core\n");
#endif
if ((signo = WTERMSIG(status)) != SIGABRT) {
t_verbose("expected child to raise signal %d (SIGABRT), "
t_printv("expected child to raise signal %d (SIGABRT), "
"got signal %d\n", SIGABRT, signo);
return (0);
}

View file

@ -73,7 +73,7 @@ static const char oc_pfcs[] = OC_PFCS;
crib[(int)oc_##set[i]] = 1; \
for (i = ret = 0; i < sizeof crib; ++i) { \
if (is_##set(i) != crib[i]) { \
t_verbose("is_%s() incorrect " \
t_printv("is_%s() incorrect " \
"for %#02x\n", #set, i); \
++ret; \
} \

View file

@ -92,11 +92,11 @@ t_mpi_grown(cryb_mpi *x)
/* XXX we need inequality predicates */
if (x->words == x->swords) {
t_verbose("value was expected to change");
t_printv("value was expected to change");
ret &= 0;
}
if (x->size == CRYB_MPI_SWORDS) {
t_verbose("value was expected to change");
t_printv("value was expected to change");
ret &= 0;
}
return (ret);
@ -121,13 +121,13 @@ t_mpi_is_zero(const cryb_mpi *x)
* Print an MPI in semi-human-readable form
*/
static void
t_verbose_mpi(const cryb_mpi *x)
t_printv_mpi(const cryb_mpi *x)
{
t_verbose("%c", x->neg ? '-' : '+');
t_verbose("%08x", x->msb == 0 ? 0 : x->words[0]);
t_printv("%c", x->neg ? '-' : '+');
t_printv("%08x", x->msb == 0 ? 0 : x->words[0]);
for (unsigned int i = 1; i < (x->msb + 31) / 32; ++i)
t_verbose(" %08x", x->words[i]);
t_printv(" %08x", x->words[i]);
}
/*
@ -143,17 +143,17 @@ t_compare_mpi(const cryb_mpi *e, const cryb_mpi *x)
if (e == NULL || x == NULL)
return (0);
if (x->words == NULL) {
t_verbose("uninitialized MPI\n");
t_printv("uninitialized MPI\n");
return (0);
}
if (e->neg != x->neg || e->msb != x->msb ||
memcmp(e->words, x->words, (e->msb + 7) / 8) != 0) {
t_verbose("expected ");
t_verbose_mpi(e);
t_verbose("\n");
t_verbose("received ");
t_verbose_mpi(x);
t_verbose("\n");
t_printv("expected ");
t_printv_mpi(e);
t_printv("\n");
t_printv("received ");
t_printv_mpi(x);
t_printv("\n");
ret = 0;
}
return (ret);
@ -254,7 +254,7 @@ t_mpi_grow_twice(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
ret &= t_compare_i(0, mpi_grow(&x, CRYB_MPI_SWORDS * 32 * 2 + 1));
/* XXX we need inequality predicates */
if (x.words == p) {
t_verbose("pointer was expected to change\n");
t_printv("pointer was expected to change\n");
ret &= 0;
}
ret &= t_mpi_grown(&x);
@ -676,12 +676,12 @@ t_mpi_large_load(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
mpi_load(&x, large_v, sizeof large_v);
/* XXX we need inequality predicates */
if (x.words == x.swords) {
t_verbose("reallocation failed to occur\n");
t_printv("reallocation failed to occur\n");
ret &= 0;
}
/* XXX we need inequality predicates */
if (x.size < LARGE_E_SIZE) {
t_verbose("expected at least %zu, received %zu\n",
t_printv("expected at least %zu, received %zu\n",
LARGE_E_SIZE, x.size);
ret &= 0;
}
@ -893,7 +893,7 @@ t_mpi_large_lsh(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
ret &= t_compare_i(0, mpi_lshift(&x, 32));
/* XXX we need inequality predicates */
if (x.words == x.swords) {
t_verbose("reallocation failed to occur\n");
t_printv("reallocation failed to occur\n");
return (0);
}
ret &= t_compare_mem(t_zero, x.words, sizeof x.words[0]);

View file

@ -282,22 +282,22 @@ t_rfc4648(char **desc CRYB_UNUSED, void *arg)
len = t->blen ? t->blen : sizeof buf;
ret = t->func(t->in, t->ilen, buf, &len);
if (ret != t->ret) {
t_verbose("expected return code %d, got %d\n",
t_printv("expected return code %d, got %d\n",
t->ret, ret);
return (0);
}
if (t->out && len != t->olen) {
t_verbose("expected output length %zu, got %zu\n",
t_printv("expected output length %zu, got %zu\n",
t->olen, len);
return (0);
}
if (t->ret != 0 && errno != t->err) {
t_verbose("expected errno %d, got %d\n",
t_printv("expected errno %d, got %d\n",
t->err, errno);
return (0);
}
if (t->ret == 0 && t->out && strncmp(buf, t->out, len) != 0) {
t_verbose("expected '%.*s' got '%.*s'\n",
t_printv("expected '%.*s' got '%.*s'\n",
(int)t->olen, t->out, (int)len, buf);
return (0);
}

View file

@ -137,7 +137,7 @@ t_strlcat(char **desc CRYB_UNUSED, void *arg)
buf[T_BUFSIZE] = T_CANARY;
sz = strlcat(buf, t->in, T_BUFSIZE);
if (buf[T_BUFSIZE] != T_CANARY) {
t_verbose("buffer overflow\n");
t_printv("buffer overflow\n");
return (0);
}
ret = t_compare_sz(t->sz, sz);

View file

@ -95,7 +95,7 @@ t_strlcpy(char **desc CRYB_UNUSED, void *arg)
memset(buf, T_CANARY, sizeof buf);
sz = strlcpy(buf, t->in, T_BUFSIZE);
if (buf[T_BUFSIZE] != T_CANARY) {
t_verbose("buffer overflow\n");
t_printv("buffer overflow\n");
return (0);
}
return (t_compare_sz(t->sz, sz) & t_compare_str(t->out, buf));