diff --git a/Makefile.am b/Makefile.am index c159420..448d770 100644 --- a/Makefile.am +++ b/Makefile.am @@ -19,7 +19,7 @@ doc_DATA = README.md LICENSE EXTRA_DIST = README.md LICENSE AUTHORS ChangeLog.md DIST_SUBDIRS = examples -SUBDIRS = . +SUBDIRS = tests if ENABLE_EXAMPLES SUBDIRS += examples endif diff --git a/configure.ac b/configure.ac index 5ddfda1..a34d8c8 100644 --- a/configure.ac +++ b/configure.ac @@ -4,7 +4,7 @@ AM_SILENT_RULES([yes]) AC_CONFIG_SRCDIR(uev.c) AM_CONFIG_HEADER(config.h) -AC_CONFIG_FILES([Makefile libuev.pc examples/Makefile]) +AC_CONFIG_FILES([Makefile libuev.pc examples/Makefile tests/Makefile]) AC_CONFIG_MACRO_DIR([m4]) AC_PROG_CC diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..4d9da9b --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,11 @@ +EXTRA_DIST = check.h +CLEANFILES = *~ *.trs *.log + +TESTS = + +check_PROGRAMS = $(TESTS) + +# Ignore warnings about unused result, in e.g. write() +CFLAGS = -W -Wall -Wextra -Wno-unused-result +CPPFLAGS = -D_GNU_SOURCE +LDADD = -L../ ../libuev.la diff --git a/tests/check.h b/tests/check.h new file mode 100644 index 0000000..330aac5 --- /dev/null +++ b/tests/check.h @@ -0,0 +1,81 @@ +#ifndef UEV_TESTS_CHECK_H_ +#define UEV_TESTS_CHECK_H_ + +#include +#include +#include +#include +#include + +#include "../uev.h" + +#define UNUSED(arg) arg __attribute__ ((unused)) + +#define fail_unless(test) \ + do { \ + if (!(test)) { \ + fprintf(stderr, \ + "----------------------------------------------\n" \ + "%s:%d: test FAILED:\nFailed test: %s\n" \ + "----------------------------------------------\n", \ + __FILE__, __LINE__, #test); \ + exit(1); \ + } \ + } while (0) + +static inline int test(int result, const char *fmt, ...) +{ + char buf[80]; + size_t len; + va_list ap; + const char success[] = " \033[1m[ OK ]\033[0m\n"; + const char failure[] = " \033[7m[FAIL]\033[0m\n"; + const char dots[] = " ....................................................................."; + + va_start(ap, fmt); + len = vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + + write(STDERR_FILENO, buf, len); + write(STDERR_FILENO, dots, 60 - len); /* pad with dots. */ + + if (!result) + write(STDERR_FILENO, success, strlen(success)); + else + write(STDERR_FILENO, failure, strlen(failure)); + + return result; +} + +static inline int timespec_newer(const struct timespec *a, const struct timespec *b) +{ + if (a->tv_sec != b->tv_sec) + return a->tv_sec > b->tv_sec; + + return a->tv_nsec > b->tv_nsec; +} + +static inline char *timespec2str(struct timespec *ts, char *buf, size_t len) +{ + size_t ret, pos; + struct tm t; + + memset(buf, 0, len); + + tzset(); + if (localtime_r(&(ts->tv_sec), &t) == NULL) + return buf; + + ret = strftime(buf, len, "%F %T", &t); + if (ret == 0) + return buf; + len -= ret - 1; + + pos = strlen(buf); + len -= pos; + snprintf(&buf[pos], len, ".%09ld", ts->tv_nsec); + + return buf; +} + +#endif /* UEV_TESTS_CHECK_H_ */