diff options
author | kartofen <mladenovnasko0@gmail.com> | 2023-04-30 13:21:44 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2023-04-30 13:21:44 +0300 |
commit | d42853496fc976ef3d067af421a1a3811660033d (patch) | |
tree | 272ece830ae755e92f715b77f632e24b1adec2d8 /src/include | |
parent | a78c52265d755a2294a743e186ad5a6b5456d9f1 (diff) |
i am getting tired of the makefile
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/test-util.h | 71 | ||||
-rwxr-xr-x | src/include/test-util.sh | 53 |
2 files changed, 124 insertions, 0 deletions
diff --git a/src/include/test-util.h b/src/include/test-util.h new file mode 100644 index 0000000..4ec36fc --- /dev/null +++ b/src/include/test-util.h @@ -0,0 +1,71 @@ +#ifndef TEST_UTIL_H +#define TEST_UTIL_H + +#include <stdio.h> +#include <string.h> + +unsigned int tests = 0, failed = 0, cur_test = 0; + +void description(char *str) +{ + printf("# DESCRIPTION: %s\n", str); +} + +void plan(int n) +{ + tests = n; failed = 0; cur_test = 0; + printf("1..%d\n", tests); +} + +int conclude() +{ + float perc = 100.0f * (1.0f - ((float)failed / tests)); + printf("# CONCLUSION: (%.2f%%) %d out of %d tests failed\n", perc, failed, tests); + + if(tests != cur_test) { + printf("# expected %d, but got %d tests\n", tests, cur_test); + } + + if(failed != 0) { + return 1; + } + + return 0; +} + +void ok(int val, char *desc) +{ + cur_test++; + if(val == 0) { + printf("ok %d - %s\n", cur_test, desc); + } else { + printf("not ok %d - %s\n", cur_test, desc); + printf(" returned \'%d\'\n", val); + failed++; + } +} +void is_i(int val, int exp, char *desc) +{ + cur_test++; + if(val == exp) { + printf("ok %d - %s\n", cur_test, desc); + } else { + printf("not ok %d - %s\n", cur_test, desc); + printf(" expected \'%d\', but got \'%d\'\n", exp, val); + failed++; + } +} + +void is_s(char *val, char *exp, char *desc) +{ + cur_test++; + if(strcmp(val, exp) == 0) { + printf("ok %d - %s\n", cur_test, desc); + } else { + printf("not ok %d - %s\n", cur_test, desc); + printf(" expected \'%s\', but got \'%s\'\n", exp, val); + failed++; + } +} + +#endif diff --git a/src/include/test-util.sh b/src/include/test-util.sh new file mode 100755 index 0000000..8af522f --- /dev/null +++ b/src/include/test-util.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +function description { + echo "# DESCRIPTION: $1" +} + +# $1 is the number of total tests +function plan { + TESTS=$1; FAILED=0; CUR_TEST=0 + echo "1..$TESTS" +} + +function conclude { + PERC=$(echo "scale=2;100 * (1 - ($FAILED / $TESTS))" | bc) + echo "# CONCLUSION: ($PERC%) $FAILED out of $TESTS tests failed" + + if [ $TESTS -ne $CUR_TEST ]; then + echo -e "# expected $TESTS, but got $CUR_TEST tests" + fi + + if [ $FAILED -ne 0 ]; then + exit 1 + fi +} + +# $1 is the value to be tested +# $2 description +function ok { + CUR_TEST=$((CUR_TEST + 1)) + + if [ $1 -eq 0 ]; then + echo "ok $CUR_TEST - $2" + else + echo "not ok $CUR_TEST - $2" + echo -e " returned '$1'" + FAILED=$((FAILED + 1)) + fi +} + +# $1 is the expected value +# $2 is the actual value +# $3 description +function is { + CUR_TEST=$((CUR_TEST + 1)) + + if [ "$1" = "$2" ]; then + echo "ok $CUR_TEST - $3" + else + echo "not ok $CUR_TEST - $3" + echo -e " exptected '$2', but got '$1'" + FAILED=$((FAILED + 1)) + fi +} |