From 9d38bc79ae203a18b43480ecb43e540847d472a3 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 9 Jun 2020 10:13:23 -0400 Subject: tests: New acceptance test for xtimegm() --- .gitignore | 1 + Makefile | 12 ++++++-- tests/xtimegm.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 tests/xtimegm.c diff --git a/.gitignore b/.gitignore index 99776af..c18ddc3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.d /bfs /tests/mksock +/tests/xtimegm diff --git a/Makefile b/Makefile index 7a31d6b..4f4c1e3 100644 --- a/Makefile +++ b/Makefile @@ -89,7 +89,7 @@ ALL_LDLIBS = $(LOCAL_LDLIBS) $(LDLIBS) default: bfs -all: bfs tests/mksock +all: bfs tests/mksock tests/xtimegm bfs: \ bftw.o \ @@ -126,10 +126,16 @@ release: bfs tests/mksock: tests/mksock.o $(CC) $(ALL_LDFLAGS) $^ -o $@ +tests/xtimegm: time.o tests/xtimegm.o + $(CC) $(ALL_LDFLAGS) $^ -o $@ + %.o: %.c $(CC) $(ALL_CFLAGS) -c $< -o $@ -check: check-bfs check-dfs check-ids +check: check-xtimegm check-bfs check-dfs check-ids + +check-xtimegm: tests/xtimegm + $< check-%: all ./tests.sh --bfs="$(CURDIR)/bfs -S $*" $(TEST_FLAGS) @@ -144,7 +150,7 @@ endif +$(MAKE) -B check $(DISTCHECK_FLAGS) clean: - $(RM) bfs *.[od] tests/mksock tests/*.[od] + $(RM) bfs *.[od] tests/mksock tests/xtimegm tests/*.[od] install: $(MKDIR) $(DESTDIR)$(PREFIX)/bin diff --git a/tests/xtimegm.c b/tests/xtimegm.c new file mode 100644 index 0000000..2426815 --- /dev/null +++ b/tests/xtimegm.c @@ -0,0 +1,91 @@ +#include "../time.h" +#include +#include +#include +#include +#include + +static bool tm_equal(const struct tm *tma, const struct tm *tmb) { + if (tma->tm_year != tmb->tm_year) { + return false; + } + if (tma->tm_mon != tmb->tm_mon) { + return false; + } + if (tma->tm_mday != tmb->tm_mday) { + return false; + } + if (tma->tm_hour != tmb->tm_hour) { + return false; + } + if (tma->tm_min != tmb->tm_min) { + return false; + } + if (tma->tm_sec != tmb->tm_sec) { + return false; + } + if (tma->tm_wday != tmb->tm_wday) { + return false; + } + if (tma->tm_yday != tmb->tm_yday) { + return false; + } + if (tma->tm_isdst != tmb->tm_isdst) { + return false; + } + + return true; +} + +static void tm_print(FILE *file, const struct tm *tm) { + fprintf(file, "Y%d M%d D%d h%d m%d s%d wd%d yd%d%s\n", + tm->tm_year, tm->tm_mon, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec, + tm->tm_wday, tm->tm_yday, + tm->tm_isdst ? (tm->tm_isdst < 0 ? " (DST?)" : " (DST)") : ""); +} + +int main(void) { + if (setenv("TZ", "UTC0", true) != 0) { + perror("setenv()"); + return EXIT_FAILURE; + } + + struct tm tm = { + .tm_isdst = -1, + }; + + for (tm.tm_year = 0; tm.tm_year <= 200; tm.tm_year += 10) + for (tm.tm_mon = -3; tm.tm_mon <= 15; tm.tm_mon += 3) + for (tm.tm_mday = -31; tm.tm_mday <= 61; tm.tm_mday += 4) + for (tm.tm_hour = -1; tm.tm_hour <= 24; tm.tm_hour += 5) + for (tm.tm_min = -1; tm.tm_min <= 60; tm.tm_min += 31) + for (tm.tm_sec = -60; tm.tm_sec <= 120; tm.tm_sec += 5) { + struct tm tma = tm, tmb = tm; + time_t ta, tb; + ta = mktime(&tma); + if (xtimegm(&tmb, &tb) != 0) { + tb = -1; + } + + bool fail = false; + if (ta != tb) { + printf("Mismatch: %jd != %jd\n", (intmax_t)ta, (intmax_t)tb); + fail = true; + } + if (ta != -1 && !tm_equal(&tma, &tmb)) { + printf("mktime(): "); + tm_print(stdout, &tma); + printf("xtimegm(): "); + tm_print(stdout, &tmb); + fail = true; + } + if (fail) { + printf("Input: "); + tm_print(stdout, &tm); + return EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; +} -- cgit v1.2.3