logo

utils-std

Collection of commonly available Unix tools
commit: 1c3aec2905e4be982ea1b54e41c75936e78eaa26
parent db468cfb5af3c7524f8ae3d689e02557d46022cf
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon,  6 May 2024 06:00:17 +0200

test-lib/strtodur: Roll TAP producer

Diffstat:

MMakefile2+-
Mlib/strtodur.c6+-----
Mtest-lib/Kyuafile2+-
Mtest-lib/strtodur.c129+++++++++++++++++++++----------------------------------------------------------
4 files changed, 37 insertions(+), 102 deletions(-)

diff --git a/Makefile b/Makefile @@ -91,7 +91,7 @@ test-lib/mode: test-lib/mode.c lib/mode.c Makefile $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/mode.c lib/mode.c $(LDFLAGS) $(LDSTATIC) test-lib/strtodur: test-lib/strtodur.c lib/strtodur.c Makefile - $(CC) -std=c99 $(CFLAGS) $(ATF_CFLAGS) -o $@ test-lib/strtodur.c lib/strtodur.c $(LDFLAGS) $(ATF_LIBS) + $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/strtodur.c lib/strtodur.c $(LDFLAGS) $(LDSTATIC) test-lib/symbolize_mode: test-lib/symbolize_mode.c lib/symbolize_mode.c Makefile $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/symbolize_mode.c lib/symbolize_mode.c $(LDFLAGS) $(LDSTATIC) diff --git a/lib/strtodur.c b/lib/strtodur.c @@ -12,11 +12,7 @@ int strtodur(char *s, struct timespec *dur) { - if(s == 0 || s[0] == 0) - { - fprintf(stderr, "strtodur: Warning: Got an empty string as duration\n"); - return 0; - } + if(s == 0 || s[0] == 0) return 0; assert(dur); diff --git a/test-lib/Kyuafile b/test-lib/Kyuafile @@ -6,6 +6,6 @@ test_suite("utils-std libs") -- 7,$|LC_ALL=C.UTF-8 sort tap_test_program{name="mode"} -atf_test_program{name="strtodur"} +tap_test_program{name="strtodur"} tap_test_program{name="symbolize_mode"} atf_test_program{name="truncation"} diff --git a/test-lib/strtodur.c b/test-lib/strtodur.c @@ -3,114 +3,53 @@ // SPDX-License-Identifier: MPL-2.0 #define _POSIX_C_SOURCE 200809L -#include <atf-c.h> -#include <assert.h> -#include <signal.h> // SIGABRT - #include "../lib/strtodur.h" -ATF_TC(empty); -ATF_TC_HEAD(empty, tc) -{ - atf_tc_set_md_var(tc, "descr", "Empty string"); -} -ATF_TC_BODY(empty, tc) -{ - struct timespec dur = {.tv_sec = 0, .tv_nsec = 0}; - ATF_CHECK_EQ(0, strtodur("", &dur)); - ATF_CHECK_EQ(0, dur.tv_sec); - ATF_CHECK_EQ(0, dur.tv_nsec); -} +#include <assert.h> +#include <stdio.h> // printf -ATF_TC(null_str); -ATF_TC_HEAD(null_str, tc) -{ - atf_tc_set_md_var(tc, "descr", "NULL string"); -} -ATF_TC_BODY(null_str, tc) -{ - struct timespec dur = {.tv_sec = 0, .tv_nsec = 0}; - ATF_CHECK_EQ(0, strtodur(NULL, &dur)); - ATF_CHECK_EQ(0, dur.tv_sec); - ATF_CHECK_EQ(0, dur.tv_nsec); -} +int counter = 0; +int err = 0; -ATF_TC(one); -ATF_TC_HEAD(one, tc) -{ - atf_tc_set_md_var(tc, "descr", "1"); -} -ATF_TC_BODY(one, tc) +static void +t_strtodur(char *str, time_t ex_sec, long ex_nsec) { - struct timespec dur = {.tv_sec = 0, .tv_nsec = 0}; - ATF_CHECK_EQ(0, strtodur("1", &dur)); - ATF_CHECK_EQ(1, dur.tv_sec); - ATF_CHECK_EQ(0, dur.tv_nsec); -} + int id = ++counter; -ATF_TC(pointone); -ATF_TC_HEAD(pointone, tc) -{ - atf_tc_set_md_var(tc, "descr", ".1"); -} -ATF_TC_BODY(pointone, tc) -{ struct timespec dur = {.tv_sec = 0, .tv_nsec = 0}; - ATF_CHECK_EQ(0, strtodur(".1", &dur)); - ATF_CHECK_EQ(0, dur.tv_sec); + int ret = strtodur(str, &dur); - long expect = 1000000000*0.1; - ATF_CHECK_EQ_MSG(expect, dur.tv_nsec, "dur.tv_nsec(%ld) != %ld", dur.tv_nsec, expect); -} + if(dur.tv_sec == ex_sec && dur.tv_nsec == ex_nsec && ret == 0) + { + printf("ok %d - strtodur(\"%s\", _) -> {.tv_sec = %ld, .tv_nsec = %ld}\n", id, str, dur.tv_sec, dur.tv_nsec); + return; + } -ATF_TC(onepoint); -ATF_TC_HEAD(onepoint, tc) -{ - atf_tc_set_md_var(tc, "descr", "1."); -} -ATF_TC_BODY(onepoint, tc) -{ - struct timespec dur = {.tv_sec = 0, .tv_nsec = 0}; - ATF_CHECK_EQ(0, strtodur("1.", &dur)); - ATF_CHECK_EQ(1, dur.tv_sec); - ATF_CHECK_EQ(0, dur.tv_nsec); + err = 1; + printf("not ok %d - strtodur(\"%s\", _) -> {.tv_sec = %ld, .tv_nsec = %ld}\n", id, str, dur.tv_sec, dur.tv_nsec); + if(dur.tv_sec != ex_sec || dur.tv_nsec != ex_nsec) printf("# Expected: {.tv_sec = %ld, .tv_nsec = %ld}\n", ex_sec, ex_nsec); + if(ret != 0) printf("# Exit status: %d\n", ret); } -ATF_TC(onecomma); -ATF_TC_HEAD(onecomma, tc) -{ - atf_tc_set_md_var(tc, "descr", "1,"); -} -ATF_TC_BODY(onecomma, tc) +int +main() { - struct timespec dur = {.tv_sec = 0, .tv_nsec = 0}; - ATF_CHECK_EQ(0, strtodur("1,", &dur)); - ATF_CHECK_EQ(1, dur.tv_sec); - ATF_CHECK_EQ(0, dur.tv_nsec); -} + int plan = 8; + printf("1..%d\n", plan); -ATF_TC(comma); -ATF_TC_HEAD(comma, tc) -{ - atf_tc_set_md_var(tc, "descr", ","); -} -ATF_TC_BODY(comma, tc) -{ - struct timespec dur = {.tv_sec = 0, .tv_nsec = 0}; - ATF_CHECK_EQ(0, strtodur(",", &dur)); - ATF_CHECK_EQ(0, dur.tv_sec); - ATF_CHECK_EQ(0, dur.tv_nsec); -} + // TODO: Capture errors, say with open_memstream(3) -ATF_TP_ADD_TCS(tp) -{ - ATF_TP_ADD_TC(tp, empty); - ATF_TP_ADD_TC(tp, null_str); - ATF_TP_ADD_TC(tp, one); - ATF_TP_ADD_TC(tp, pointone); - ATF_TP_ADD_TC(tp, onepoint); - ATF_TP_ADD_TC(tp, onecomma); - ATF_TP_ADD_TC(tp, comma); + t_strtodur(NULL, 0, 0); + t_strtodur("", 0, 0); + t_strtodur(",", 0, 0); + + t_strtodur("1", 1, 0); + t_strtodur("1.", 1, 0); + t_strtodur("1,", 1, 0); + + t_strtodur(".1", 0, 1000000000*0.1); + t_strtodur("0.1", 0, 1000000000*0.1); - return atf_no_error(); + assert(counter == plan); + return err; }