logo

utils-std

Collection of commonly available Unix tools
commit: 85d881017a03965fa9901975a56591ec2a780f18
parent 1c3aec2905e4be982ea1b54e41c75936e78eaa26
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon,  6 May 2024 16:56:46 +0200

test-lib/truncation: Roll TAP producer

Diffstat:

MMakefile2+-
Mtest-lib/Kyuafile2+-
Mtest-lib/truncation.c115++++++++++++++++++++++++++++++++++++-------------------------------------------
3 files changed, 55 insertions(+), 64 deletions(-)

diff --git a/Makefile b/Makefile @@ -97,7 +97,7 @@ 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) test-lib/truncation: test-lib/truncation.c lib/truncation.c Makefile - $(CC) -std=c99 $(CFLAGS) $(ATF_CFLAGS) -o $@ test-lib/truncation.c lib/truncation.c $(LDFLAGS) $(ATF_LIBS) + $(CC) -std=c99 $(CFLAGS) -o $@ test-lib/truncation.c lib/truncation.c $(LDFLAGS) $(LDSTATIC) cmd/df: cmd/df.c lib/humanize.c Makefile rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} diff --git a/test-lib/Kyuafile b/test-lib/Kyuafile @@ -8,4 +8,4 @@ test_suite("utils-std libs") tap_test_program{name="mode"} tap_test_program{name="strtodur"} tap_test_program{name="symbolize_mode"} -atf_test_program{name="truncation"} +tap_test_program{name="truncation"} diff --git a/test-lib/truncation.c b/test-lib/truncation.c @@ -3,87 +3,78 @@ // SPDX-License-Identifier: MPL-2.0 #define _POSIX_C_SOURCE 200809L -#include <atf-c.h> - #include "../lib/truncation.h" -#define CHECK_EQ_SIZE(expect, got) ATF_CHECK_EQ_MSG(expect, got, "Expected %ld; Got %ld\n", expect, got) - -#define TEST_SIZE_OP(str, expect_size, expect_op) \ - ATF_CHECK(parse_size(str, &tr) == 0); \ - CHECK_EQ_SIZE(expect_size, tr.size); \ - ATF_CHECK_EQ(expect_op, tr.op); +#include <assert.h> +#include <stdio.h> // printf char *argv0 = "test-lib/truncation"; -ATF_TC(empty); -ATF_TC_HEAD(empty, tc) -{ - atf_tc_set_md_var(tc, "descr", "Empty string"); -} -ATF_TC_BODY(empty, tc) -{ - struct truncation tr; - - ATF_CHECK(parse_size("", &tr) == -1); - CHECK_EQ_SIZE(0L, tr.size); -} +int counter = 0; +int err = 0; -ATF_TC(set); -ATF_TC_HEAD(set, tc) -{ - atf_tc_set_md_var(tc, "descr", "Set"); -} -ATF_TC_BODY(set, tc) +static void +t_parse_size(char *str, off_t size, enum operation_e op) { - struct truncation tr; + int id = ++counter; - TEST_SIZE_OP("0", 0L, OP_SET); - TEST_SIZE_OP("666", 666L, OP_SET); - TEST_SIZE_OP("666M", 666*1024*1024L, OP_SET); - TEST_SIZE_OP("666MB", 666*1000*1000L, OP_SET); - TEST_SIZE_OP("666MiB", 666*1024*1024L, OP_SET); + struct truncation tr; + int ret = parse_size(str, &tr); + if(ret == 0 && tr.size == size && tr.op == op) + { + printf("ok %d - parse_size(\"%s\", _) -> {.size = %ld, .op = %d}\n", id, str, tr.size, tr.op); + return; + } + + err = 1; + printf("not ok %d - parse_size(\"%s\", _) -> {.size = %ld, .op = %d}\n", id, str, tr.size, tr.op); + if(tr.size != size || tr.op != op) printf("# Expected: {.size = %ld, .op = %d}\n", size, op); + if(ret != 0) printf("# Exit status: %d\n", ret); } -ATF_TC(inc); -ATF_TC_HEAD(inc, tc) +static void +set() { - atf_tc_set_md_var(tc, "descr", "Increase"); -} -ATF_TC_BODY(inc, tc) -{ - struct truncation tr; - - TEST_SIZE_OP("+0", 0L, OP_INC); - TEST_SIZE_OP("+666", 666L, OP_INC); - TEST_SIZE_OP("+666M", 666*1024*1024L, OP_INC); - TEST_SIZE_OP("+666MB", 666*1000*1000L, OP_INC); - TEST_SIZE_OP("+666MiB", 666*1024*1024L, OP_INC); + t_parse_size("0", 0L, OP_SET); + t_parse_size("666", 666L, OP_SET); + t_parse_size("666M", 666*1024*1024L, OP_SET); + t_parse_size("666MB", 666*1000*1000L, OP_SET); + t_parse_size("666MiB", 666*1024*1024L, OP_SET); } -ATF_TC(dec); -ATF_TC_HEAD(dec, tc) +static void +inc() { - atf_tc_set_md_var(tc, "descr", "Decrease"); + t_parse_size("+0", 0L, OP_INC); + t_parse_size("+666", 666L, OP_INC); + t_parse_size("+666M", 666*1024*1024L, OP_INC); + t_parse_size("+666MB", 666*1000*1000L, OP_INC); + t_parse_size("+666MiB", 666*1024*1024L, OP_INC); } -ATF_TC_BODY(dec, tc) -{ - struct truncation tr; - TEST_SIZE_OP("-0", 0L, OP_DEC); - TEST_SIZE_OP("-666", 666L, OP_DEC); - TEST_SIZE_OP("-666M", 666*1024*1024L, OP_DEC); - TEST_SIZE_OP("-666MB", 666*1000*1000L, OP_DEC); - TEST_SIZE_OP("-666MiB", 666*1024*1024L, OP_DEC); +static void +dec() +{ + t_parse_size("-0", 0L, OP_DEC); + t_parse_size("-666", 666L, OP_DEC); + t_parse_size("-666M", 666*1024*1024L, OP_DEC); + t_parse_size("-666MB", 666*1000*1000L, OP_DEC); + t_parse_size("-666MiB", 666*1024*1024L, OP_DEC); } -ATF_TP_ADD_TCS(tp) +int +main() { - ATF_TP_ADD_TC(tp, empty); + int plan = 15; + printf("1..%d\n", plan); + + // needs standard error capture + //t_parse_size("", 0, OP_SET); - ATF_TP_ADD_TC(tp, set); - ATF_TP_ADD_TC(tp, inc); - ATF_TP_ADD_TC(tp, dec); + set(); + inc(); + dec(); - return atf_no_error(); + assert(counter == plan); + return err; }