mirror of
https://github.com/cryb-to/cryb-to.git
synced 2024-11-28 00:25:43 +00:00
Add and use err(3)-like functions.
This commit is contained in:
parent
5768034d36
commit
c72cbdbc29
5 changed files with 131 additions and 20 deletions
|
@ -1,5 +1,5 @@
|
|||
/*-
|
||||
* Copyright (c) 2012-2016 Dag-Erling Smørgrav
|
||||
* Copyright (c) 2012-2017 Dag-Erling Smørgrav
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -75,6 +75,15 @@ void t_printv_hex(const uint8_t *, size_t);
|
|||
void t_printv(const char *, ...)
|
||||
CRYB_PRINTF(1, 2);
|
||||
|
||||
void t_warn(const char *, ...)
|
||||
CRYB_PRINTF(1, 2);
|
||||
void t_syswarn(const char *, ...)
|
||||
CRYB_PRINTF(1, 2);
|
||||
void t_err(int, const char *, ...)
|
||||
CRYB_PRINTF(2, 3) CRYB_NORETURN;
|
||||
void t_syserr(int, const char *, ...)
|
||||
CRYB_PRINTF(2, 3) CRYB_NORETURN;
|
||||
|
||||
#if CRYB_TEST_HAVE_STDIO
|
||||
/*
|
||||
* Convenience functions for temp files
|
||||
|
|
|
@ -4,6 +4,7 @@ lib_LTLIBRARIES = libcryb-test.la
|
|||
libcryb_test_la_SOURCES = \
|
||||
cryb_t_compare.c \
|
||||
cryb_t_const.c \
|
||||
cryb_t_err.c \
|
||||
cryb_t_file.c \
|
||||
cryb_t_main.c \
|
||||
cryb_t_malloc.c \
|
||||
|
|
103
lib/test/cryb_t_err.c
Normal file
103
lib/test/cryb_t_err.c
Normal file
|
@ -0,0 +1,103 @@
|
|||
/*-
|
||||
* Copyright (c) 2017 Dag-Erling Smørgrav
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote
|
||||
* products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "cryb/impl.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <cryb/test.h>
|
||||
|
||||
/*
|
||||
* Print a warning message.
|
||||
*/
|
||||
void
|
||||
t_warn(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
fprintf(stderr, "%s: ", t_progname);
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* Print a warning message followed by the appropriate text for the
|
||||
* current value of errno.
|
||||
*/
|
||||
void
|
||||
t_syswarn(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
fprintf(stderr, "%s: ", t_progname);
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
fprintf(stderr, ": %s\n", strerror(errno));
|
||||
}
|
||||
|
||||
/*
|
||||
* Print an error message and exit.
|
||||
*/
|
||||
void
|
||||
t_err(int status, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
fprintf(stderr, "%s: ", t_progname);
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
fprintf(stderr, "\n");
|
||||
exit(status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Print an error message followed by the appropriate text for the current
|
||||
* value of errno and exit.
|
||||
*/
|
||||
void
|
||||
t_syserr(int status, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
fprintf(stderr, "%s: ", t_progname);
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
fprintf(stderr, ": %s\n", strerror(errno));
|
||||
exit(status);
|
||||
}
|
|
@ -29,7 +29,6 @@
|
|||
|
||||
#include "cryb/impl.h"
|
||||
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
|
@ -53,20 +52,20 @@ t_fopen(const char *filename)
|
|||
int fd, ret;
|
||||
|
||||
if ((tf = calloc(sizeof *tf, 1)) == NULL)
|
||||
err(1, "%s(): calloc()", __func__);
|
||||
t_syserr(1, "%s(): calloc()", __func__);
|
||||
if (filename) {
|
||||
if ((tf->name = strdup(filename)) == NULL)
|
||||
err(1, "%s(): strdup()", __func__);
|
||||
t_syserr(1, "%s(): strdup()", __func__);
|
||||
} else {
|
||||
ret = asprintf(&tf->name, "%s.%lu.%p.tmp",
|
||||
t_progname, (unsigned long)getpid(), (void *)tf);
|
||||
if (ret < 0 || tf->name == NULL)
|
||||
err(1, "%s(): asprintf()", __func__);
|
||||
t_syserr(1, "%s(): asprintf()", __func__);
|
||||
}
|
||||
if ((fd = open(tf->name, O_RDWR|O_CREAT|O_TRUNC, 0600)) < 0)
|
||||
err(1, "%s(): %s", __func__, tf->name);
|
||||
t_syserr(1, "%s(): %s", __func__, tf->name);
|
||||
if ((tf->file = fdopen(fd, "r+")) == NULL)
|
||||
err(1, "%s(): fdopen()", __func__);
|
||||
t_syserr(1, "%s(): fdopen()", __func__);
|
||||
if ((tf->next = tflist) != NULL)
|
||||
tf->next->prev = tf;
|
||||
tflist = tf;
|
||||
|
@ -86,7 +85,7 @@ t_fprintf(struct t_file *tf, const char *fmt, ...)
|
|||
len = vfprintf(tf->file, fmt, ap);
|
||||
va_end(ap);
|
||||
if (ferror(tf->file))
|
||||
err(1, "%s(): vfprintf()", __func__);
|
||||
t_syserr(1, "%s(): vfprintf()", __func__);
|
||||
return (len);
|
||||
}
|
||||
|
||||
|
@ -100,7 +99,7 @@ t_frewind(struct t_file *tf)
|
|||
errno = 0;
|
||||
rewind(tf->file);
|
||||
if (errno != 0)
|
||||
err(1, "%s(): rewind()", __func__);
|
||||
t_syserr(1, "%s(): rewind()", __func__);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -138,7 +137,7 @@ t_fclose(struct t_file *tf)
|
|||
tf->next->prev = tf->prev;
|
||||
fclose(tf->file);
|
||||
if (unlink(tf->name) < 0)
|
||||
warn("%s(): unlink()", __func__);
|
||||
t_syswarn("%s(): unlink()", __func__);
|
||||
free(tf->name);
|
||||
free(tf);
|
||||
}
|
||||
|
|
|
@ -29,7 +29,6 @@
|
|||
|
||||
#include "cryb/impl.h"
|
||||
|
||||
#include <err.h>
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
|
@ -69,11 +68,11 @@ t_printv_hex(const uint8_t *buf, size_t len)
|
|||
|
||||
/*
|
||||
* If verbose flag is set, print a message
|
||||
*/
|
||||
*/
|
||||
void
|
||||
t_printv(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_list ap;
|
||||
|
||||
if (t_verbose) {
|
||||
va_start(ap, fmt);
|
||||
|
@ -122,7 +121,7 @@ t_grow(int n)
|
|||
while (t_plan_len + n >= t_plan_size)
|
||||
t_plan_size *= 2;
|
||||
if ((p = realloc(t_plan, t_plan_size * sizeof *p)) == NULL)
|
||||
err(1, "realloc()");
|
||||
t_syserr(1, "realloc()");
|
||||
t_plan = p;
|
||||
}
|
||||
|
||||
|
@ -136,11 +135,11 @@ t_add_test(t_func *func, void *arg, const char *fmt, ...)
|
|||
va_list ap;
|
||||
|
||||
if ((t = malloc(sizeof *t)) == NULL)
|
||||
err(1, "malloc()");
|
||||
t_syserr(1, "malloc()");
|
||||
t->func = func;
|
||||
va_start(ap, fmt);
|
||||
if (vasprintf(&t->desc, fmt, ap) < 0)
|
||||
err(1, "vasprintf()");
|
||||
t_err(1, "vasprintf()");
|
||||
va_end(ap);
|
||||
t->arg = arg;
|
||||
t_grow(1);
|
||||
|
@ -200,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_printv("WARNING: test case left t_malloc_fail = %d\n",
|
||||
t_warn("test case left t_malloc_fail = %d\n",
|
||||
t_malloc_fail);
|
||||
t_malloc_fail = 0;
|
||||
}
|
||||
if (t_malloc_fail_after != 0) {
|
||||
t_printv("WARNING: test case left t_malloc_fail_after = %d\n",
|
||||
t_warn("test case left t_malloc_fail_after = %d\n",
|
||||
t_malloc_fail_after);
|
||||
t_malloc_fail_after = 0;
|
||||
}
|
||||
|
@ -227,7 +226,7 @@ t_run_test(struct t_test *t, int n)
|
|||
snaplen = sizeof snap2;
|
||||
t_compare_mem(snap1, snap2, snaplen);
|
||||
if (memcmp(snap1, snap2, snaplen) != 0)
|
||||
t_printv("WARNING: allocator state changed\n");
|
||||
t_warn("allocator state changed\n");
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
@ -299,7 +298,7 @@ t_main(t_prepare_func t_prepare, t_cleanup_func t_cleanup,
|
|||
if (t_prepare != NULL)
|
||||
t_prepare(argc, argv);
|
||||
if (t_plan_len == 0)
|
||||
errx(1, "no plan");
|
||||
t_err(1, "no plan");
|
||||
|
||||
/* run the tests */
|
||||
nt = leaktest ? t_plan_len + 1 : t_plan_len;
|
||||
|
|
Loading…
Reference in a new issue