aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libsimple.c61
-rw-r--r--test.c63
-rw-r--r--test.h18
3 files changed, 101 insertions, 41 deletions
diff --git a/libsimple.c b/libsimple.c
index b27f3bb..731396d 100644
--- a/libsimple.c
+++ b/libsimple.c
@@ -17,36 +17,25 @@ test_timespec(double d, time_t sec, long int nsec, double rd, const char *s, con
double dt;
libsimple_doubletotimespec(&t, d);
- if (t.tv_sec != sec)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
- if (t.tv_nsec != nsec)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
+ subassert(t.tv_sec == sec);
+ subassert(t.tv_nsec == nsec);
dt = libsimple_timespectodouble(&t);
- if (dt < rd - 0.0000000001)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
- if (dt > rd + 0.0000000001)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
+ subassert(dt >= rd - 0.0000000001);
+ subassert(dt <= rd + 0.0000000001);
str = libsimple_timespectostr(buf, &t);
- if (str != buf)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
- if (strcmp(str, s))
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
+ subassert(str == buf);
+ subassert(!strcmp(str, s));
- str = libsimple_timespectostr(NULL, &t);
- if (!str)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
- if (strcmp(str, s))
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
+ subassert((str = libsimple_timespectostr(NULL, &t)));
+ subassert(!strcmp(str, s));
strcpy(buf, str);
free(str);
str = libsimple_minimise_number_string(buf);
- if (str != buf)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
- if (strcmp(str, ss))
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
+ subassert(str == buf);
+ subassert(!strcmp(str, ss));
return 1;
}
@@ -59,36 +48,26 @@ test_timeval(double d, time_t sec, long int usec, double rd, const char *s, cons
double dt;
libsimple_doubletotimeval(&t, d);
- if (t.tv_sec != sec)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
- if (t.tv_usec != usec)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
+ subassert(t.tv_sec == sec);
+ subassert(t.tv_usec == usec);
dt = libsimple_timevaltodouble(&t);
- if (dt < rd - 0.0000001)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
- if (dt > rd + 0.0000001)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
+ subassert(dt >= rd - 0.0000001);
+ subassert(dt <= rd + 0.0000001);
str = libsimple_timevaltostr(buf, &t);
- if (str != buf)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
- if (strcmp(str, s))
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
+ subassert(str == buf);
+ subassert(!strcmp(str, s));
str = libsimple_timevaltostr(NULL, &t);
- if (!str)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
- if (strcmp(str, s))
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
+ subassert(str);
+ subassert(!strcmp(str, s));
strcpy(buf, str);
free(str);
str = libsimple_minimise_number_string(buf);
- if (str != buf)
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
- if (strcmp(str, ss))
- return fprintf(stderr, "Failed at %s:%i\n", __FILE__, __LINE__ - 1), 0;
+ subassert(str == buf);
+ subassert(!strcmp(str, ss));
return 1;
}
diff --git a/test.c b/test.c
index ad5a4bc..edc3d15 100644
--- a/test.c
+++ b/test.c
@@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */
#include "libsimple.h"
#include "test.h"
+#include <sys/syscall.h>
#include <malloc.h>
#undef strndup
@@ -8,6 +9,13 @@
size_t alloc_fail_in = 0;
+int exit_real = 0;
+int exit_ok = 0;
+int exit_status;
+jmp_buf exit_jmp;
+char stderr_buf[8 << 10];
+size_t stderr_n = 0;
+int stderr_real = 0;
static int custom_malloc = 0;
@@ -222,3 +230,58 @@ free(void *ptr)
return;
assert(!munmap(info->real_beginning, info->real_size));
}
+
+
+void
+exit(int status)
+{
+ exit_status = status;
+ if (exit_real) {
+#ifdef SYS_exit_group
+ syscall(SYS_exit_group, status);
+#else
+ syscall(SYS_exit, status);
+#endif
+ }
+ assert(exit_ok);
+ longjmp(exit_jmp, 1);
+}
+
+
+int
+fprintf(FILE *restrict stream, const char *restrict format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ return vfprintf(stream, format, ap);
+ va_end(ap);
+}
+
+
+int
+vfprintf(FILE *restrict stream, const char *restrict format, va_list ap)
+{
+ va_list ap2;
+ int r;
+ char *buf;
+ size_t n;
+
+ va_copy(ap2, ap);
+ r = vsnprintf(NULL, 0, format, ap2);
+ va_end(ap2);
+
+ if (r >= 0) {
+ n = (size_t)r;
+ buf = alloca(n + 1);
+ n = vsnprintf(buf, n + 1, format, ap);
+ if (fileno(stream) != STDERR_FILENO) {
+ fwrite(buf, 1, n, stream);
+ } else {
+ assert(stderr_n + n <= sizeof(stderr_buf));
+ memcpy(&stderr_buf[stderr_n], buf, n);
+ stderr_n += n;
+ }
+ }
+
+ return r;
+}
diff --git a/test.h b/test.h
index 5281cbc..e68c9c1 100644
--- a/test.h
+++ b/test.h
@@ -5,10 +5,21 @@
do {\
if (EXPR)\
break;\
+ stderr_real = 1;\
fprintf(stderr, "Failed at %s:%i: %s\n", __FILE__, __LINE__, #EXPR);\
+ exit_real = 1;\
exit(1);\
} while (0)
+#define subassert(EXPR)\
+ do {\
+ if (EXPR)\
+ break;\
+ stderr_real = 1;\
+ fprintf(stderr, "Failed at %s:%i: %s\n", __FILE__, __LINE__, #EXPR);\
+ return 0;\
+ } while (0)
+
struct allocinfo {
void *real_beginning;
@@ -22,6 +33,13 @@ struct allocinfo {
extern size_t alloc_fail_in;
+extern int exit_real;
+extern int exit_ok;
+extern int exit_status;
+extern jmp_buf exit_jmp;
+extern char stderr_buf[8 << 10];
+extern size_t stderr_n;
+extern int stderr_real;
size_t get_pagesize(void);