diff options
author | EuAndreh <eu@euandre.org> | 2024-04-05 16:07:39 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-04-05 17:08:08 -0300 |
commit | ea60b1b08015060a08b1ead38e4e81c44a88017f (patch) | |
tree | 9be302cafc856410383e1b1ab315f8b1a371153d | |
parent | git mv meta.capim meta.weave (diff) | |
download | pindaiba-ea60b1b08015060a08b1ead38e4e81c44a88017f.tar.gz pindaiba-ea60b1b08015060a08b1ead38e4e81c44a88017f.tar.xz |
Move unit tests out of src/*.c into tests/
-rw-r--r-- | .gitignore | 12 | ||||
-rw-r--r-- | Makefile | 92 | ||||
-rw-r--r-- | deps.mk | 92 | ||||
-rwxr-xr-x | mkdeps.sh | 36 | ||||
-rw-r--r-- | src/catalog.c | 330 | ||||
-rw-r--r-- | src/i18n.c | 26 | ||||
-rw-r--r-- | src/lib.c | 9 | ||||
-rw-r--r-- | src/logerr.c | 284 | ||||
-rw-r--r-- | src/random.c | 75 | ||||
-rw-r--r-- | src/testing.c | 43 | ||||
-rw-r--r-- | tests/catalog.c | 327 | ||||
-rw-r--r-- | tests/i18n.c | 23 | ||||
-rw-r--r-- | tests/lib.c | 8 | ||||
-rw-r--r-- | tests/logerr.c | 283 | ||||
-rw-r--r-- | tests/random.c | 73 | ||||
-rw-r--r-- | tests/testing.c | 43 |
16 files changed, 888 insertions, 868 deletions
@@ -1,14 +1,14 @@ /*.a +/*.bin /src/config.h /src/*.o -/src/*.to -/src/*.a -/src/*.bin -/src/*.c.txt /src/*.cat -/doc/*.[0-9] +/tests/*.o +/tests/*.a +/tests/*.bin +/tests/*.c.txt /tests/fuzz/*.o /tests/fuzz/*.a /tests/fuzz/*.bin /tests/fuzz/corpus/ -/tests/slurp.o +/doc/*.[0-9] @@ -22,7 +22,7 @@ LDLIBS = -lsiphash .SUFFIXES: -.SUFFIXES: .in .c .o .to .a .bin .msg .cat .bin-check +.SUFFIXES: .in .c .o .a .bin .msg .cat .bin-check .in: sed \ @@ -33,10 +33,7 @@ LDLIBS = -lsiphash if [ -x $< ]; then chmod +x $@; fi .c.o: - $(CC) $(CFLAGS) -o $@ -c $< - -.c.to: - $(CC) $(CFLAGS) -DTEST -o $@ -c $< + $(CC) $(CFLAGS) -o $@ -c $< .a.bin: $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) @@ -51,49 +48,49 @@ catalogs.cat = $(catalogs.msg:.msg=.cat) sources.h = $(sources.c:.c=.h) sources.o = $(sources.c:.c=.o) -sources.to = $(sources.c:.c=.to) -sources.a = $(sources.c:.c=.a) -sources.bin = $(sources.c:.c=.bin) +tests.o = $(tests.c:.c=.o) +tests.a = $(tests.c:.c=.a) +tests.bin = $(tests.c:.c=.bin) fuzz.o = $(fuzz.c:.c=.o) fuzz.a = $(fuzz.c:.c=.a) fuzz.bin = $(fuzz.c:.c=.bin) archives = \ - lib$(NAME).a \ - $(sources.a) \ - src/main.a \ - $(fuzz.a) \ + lib$(NAME).a \ + $(tests.a) \ + $(NAME).a \ + $(fuzz.a) \ sources = \ - $(sources.c) \ - $(sources.h) \ - src/config.h.in \ - src/config.h \ - src/main.c \ - src/$(NAME).h \ - $(catalogs.msg) \ + $(sources.c) \ + $(sources.h) \ + src/config.h.in \ + src/config.h \ + src/main.c \ + src/$(NAME).h \ + $(catalogs.msg) \ derived-assets = \ - lib$(NAME).a \ - src/config.h \ - $(manpages) \ - $(catalogs.cat) \ - $(sources.o) \ - $(sources.to) \ - $(sources.a) \ - $(sources.bin) \ - tests/slurp.o \ - src/main.o \ - src/main.a \ - src/main.bin \ + lib$(NAME).a \ + src/config.h \ + $(manpages) \ + $(catalogs.cat) \ + $(sources.o) \ + $(tests.o) \ + $(tests.a) \ + $(tests.bin) \ + tests/slurp.o \ + src/main.o \ + $(NAME).a \ + $(NAME).bin \ side-assets = \ - $(fuzz.o) \ - $(fuzz.a) \ - $(fuzz.bin) \ - src/logerr.c.txt \ - src/catalog.c.txt \ + $(fuzz.o) \ + $(fuzz.a) \ + $(fuzz.bin) \ + tests/logerr.c.txt \ + tests/catalog.c.txt \ @@ -103,31 +100,30 @@ all: $(derived-assets) lib$(NAME).a: $(sources.o) -src/main.a: $(sources.o) src/main.o +$(NAME).a: $(sources.o) src/main.o $(fuzz.bin): lib$(NAME).a -$(manpages) src/config.h: Makefile deps.mk -$(sources.o) $(sources.to): Makefile deps.mk src/config.h -tests/slurp.o: Makefile deps.mk src/config.h +$(manpages) src/config.h: Makefile deps.mk +$(sources.o) $(tests.o): Makefile deps.mk src/config.h +src/main.o tests/slurp.o: Makefile deps.mk src/config.h +tests/slurp.o: tests/slurp.h $(archives): $(AR) $(ARFLAGS) $@ $? -src/catalog.bin-check: src/$(NAME).en.cat -src/$(NAME).en.cat: src/i18n.bin - env DUMP_TRANSLATABLE_STRINGS=1 $(EXEC)src/i18n.bin > $*.msg.new - cmp -s $*.msg.new $*.msg || mv $*.msg.new $*.msg - rm -f $*.msg.new +tests/catalog.bin-check: src/$(NAME).en.cat +src/$(NAME).en.cat: tests/i18n.bin + env DUMP_TRANSLATABLE_STRINGS=1 $(EXEC)tests/i18n.bin | ifnew $*.msg gencat $@ $*.msg touch $@ -sources.bin-check = $(sources.c:.c=.bin-check) -$(sources.bin-check): +tests.bin-check = $(tests.c:.c=.bin-check) +$(tests.bin-check): $(EXEC)$*.bin -check-unit: $(sources.bin-check) +check-unit: $(tests.bin-check) ## Run all tests. Each test suite is isolated, so that a parallel @@ -1,15 +1,15 @@ manpages.en.in = \ - doc/pindaiba.CHANGELOG.en.7.in \ - doc/pindaiba.README.en.7.in \ - doc/pindaiba.TODOs.en.7.in \ doc/pindaiba.en.1.in \ doc/pindaiba.recipes.en.7.in \ doc/pindaiba.tutorial.en.7.in \ doc/pindaiba.why.en.7.in \ -manpages.in = $(manpages.en.in) catalogs.en.msg = src/pindaiba.en.msg -catalogs.msg = $(catalogs.en.msg) +manpages.in = \ + $(manpages.en.in) \ + +catalogs.msg = \ + $(catalogs.en.msg) \ fuzz.c = \ tests/fuzz/another.c \ @@ -29,38 +29,60 @@ sources.c = \ src/random.c \ src/testing.c \ -src/catalog.o src/catalog.to: src/catalog.h -src/i18n.o src/i18n.to: src/i18n.h -src/lib.o src/lib.to: src/lib.h -src/logerr.o src/logerr.to: src/logerr.h -src/random.o src/random.to: src/random.h -src/testing.o src/testing.to: src/testing.h +tests.c = \ + tests/catalog.c \ + tests/i18n.c \ + tests/lib.c \ + tests/logerr.c \ + tests/random.c \ + tests/testing.c \ + +src/catalog.o: src/catalog.h +src/i18n.o: src/i18n.h +src/lib.o: src/lib.h +src/logerr.o: src/logerr.h +src/random.o: src/random.h +src/testing.o: src/testing.h + +tests/catalog.o: src/catalog.c src/catalog.h +tests/i18n.o: src/i18n.c src/i18n.h +tests/lib.o: src/lib.c src/lib.h +tests/logerr.o: src/logerr.c src/logerr.h +tests/random.o: src/random.c src/random.h +tests/testing.o: src/testing.c src/testing.h + +tests/catalog.a: tests/catalog.o +tests/i18n.a: tests/i18n.o +tests/lib.a: tests/lib.o +tests/logerr.a: tests/logerr.o +tests/random.a: tests/random.o +tests/testing.a: tests/testing.o -src/catalog.a: src/catalog.to -src/i18n.a: src/i18n.to -src/lib.a: src/lib.to -src/logerr.a: src/logerr.to -src/random.a: src/random.to -src/testing.a: src/testing.to +tests/catalog.bin-check: tests/catalog.bin +tests/i18n.bin-check: tests/i18n.bin +tests/lib.bin-check: tests/lib.bin +tests/logerr.bin-check: tests/logerr.bin +tests/random.bin-check: tests/random.bin +tests/testing.bin-check: tests/testing.bin -src/catalog.bin-check: src/catalog.bin -src/i18n.bin-check: src/i18n.bin -src/lib.bin-check: src/lib.bin -src/logerr.bin-check: src/logerr.bin -src/random.bin-check: src/random.bin -src/testing.bin-check: src/testing.bin +src/catalog.o: src/logerr.h +src/i18n.o: +src/lib.o: +src/logerr.o: +src/random.o: src/logerr.h +src/testing.o: -src/catalog.o src/catalog.to: src/logerr.h src/testing.h src/../tests/slurp.h -src/i18n.o src/i18n.to: src/logerr.h src/catalog.h -src/lib.o src/lib.to: -src/logerr.o src/logerr.to: src/testing.h src/../tests/slurp.h -src/random.o src/random.to: src/logerr.h src/testing.h -src/testing.o src/testing.to: +tests/catalog.o: src/logerr.h src/testing.h tests/slurp.h +tests/i18n.o: src/logerr.h src/catalog.h +tests/lib.o: +tests/logerr.o: src/testing.h tests/slurp.h +tests/random.o: src/logerr.h src/testing.h +tests/testing.o: -src/catalog.a: src/logerr.o src/testing.o src/../tests/slurp.o -src/i18n.a: src/logerr.o src/catalog.o -src/lib.a: -src/logerr.a: src/testing.o src/../tests/slurp.o -src/random.a: src/logerr.o src/testing.o -src/testing.a: +tests/catalog.a: src/logerr.o src/testing.o tests/slurp.o +tests/i18n.a: src/logerr.o src/catalog.o +tests/lib.a: +tests/logerr.a: src/testing.o tests/slurp.o +tests/random.a: src/logerr.o src/testing.o +tests/testing.a: @@ -1,28 +1,40 @@ #!/bin/sh set -eu +export LANG=POSIX.UTF-8 + varlist() { printf '%s = \\\n' "$1" - sed 's|^\(.*\)$|\t\1 \\|' + sort | sed 's|^\(.*\)$|\t\1 \\|' printf '\n' } cfiles() { - git ls-files src/ | grep '\.c$' | grep -v '^src/main\.c$' | sort + find src/*.c -not -name 'main.c' } -export LANG=POSIX.UTF-8 +langs() { + awk '/^LANGUAGES = / { + for (i = 3; i < NF + 1; i++) { + print $(i) + } + exit + }' Makefile +} + +NAME="$(awk '/^NAME = / { print $3 }' Makefile)" -find doc/*.en.*.in 2>/dev/null | sort | varlist 'manpages.en.in' ||: -echo 'manpages.in = $(manpages.en.in)' +find doc/*.en.*.in | varlist 'manpages.en.in' +# langs printf 'catalogs.en.msg = %s\n' "$(find src/ -name '*.msg')" -echo 'catalogs.msg = $(catalogs.en.msg)' -printf '\n' +langs | sed 's|^\(.*\)$|$(manpages.\1.in)|' | varlist 'manpages.in' +langs | sed 's|^\(.*\)$|$(catalogs.\1.msg)|' | varlist 'catalogs.msg' -find tests/fuzz/*.c | sort | varlist 'fuzz.c' -find tests/fuzz/*.c | sort | awk -F. '{ printf "%s.a: %s.o\n", $1, $1 }' -find tests/fuzz/*.c | sort | awk -F. '{ printf "%s.bin-check: %s.bin\n", $1, $1 }' +find tests/fuzz/*.c | varlist 'fuzz.c' +find tests/fuzz/*.c | awk -F. '{ printf "%s.a: %s.o\n", $1, $1 }' +find tests/fuzz/*.c | awk -F. '{ printf "%s.bin-check: %s.bin\n", $1, $1 }' printf '\n\n' -cfiles | varlist 'sources.c' -ldev deps $(cfiles) +cfiles | varlist 'sources.c' +cfiles | sed 's|^src/|tests/|' | varlist 'tests.c' +ldev deps $(cfiles | sort) diff --git a/src/catalog.c b/src/catalog.c index d543864..68ec3c3 100644 --- a/src/catalog.c +++ b/src/catalog.c @@ -157,333 +157,3 @@ dump_translatable_strings(const char *const MSGS[]) { out: return rc; } - - - -#ifdef TEST -#include "testing.h" -#include "../tests/slurp.h" - - -static const char *const -FNAME = __FILE__ ".txt"; - -enum TEST_MSGCATALOG_ID { - MSG_X_FIRST = 1, - MSG_X_1, - MSG_X_2, - MSG_X_LAST, - MSG_STANDALONE, -}; - -static const char *const -TEST_MSGS[] = { - "", - [MSG_X_FIRST]="First line\n", - [MSG_X_1]="a second\n", - [MSG_X_2]="a third\n", - [MSG_X_LAST]="and the last one\n", - [MSG_STANDALONE]="single line message\n", - NULL -}; - -static int -test_i18n_init(void) { - int rc = 0; - - test_start("i18n_init()"); - - { - testing("simple call without touching the environment"); - - const int should_overwrite = 1; - if (setenv(NLSPATH_KEY, "src/%N.en.cat", should_overwrite)) { - logerr("setenv(\"%s\", \"src/%%N.en.cat\", 1): %s\n", - NLSPATH_KEY, strerror(errno)); - rc = -1; - goto out; - } - - if (i18n_init()) { - logerr("i18n_init()\n"); - rc = -1; - goto out; - } - - test_ok(); - } - -out: - if (i18n_destroy()) { - logerr("i18n_destroy()\n"); - rc = -1; - } - return rc; -} - -static int -test_s_print_msgs(void) { - int rc = 0; - - test_start("s_print_msgs()"); - FILE *file = NULL; - char *str = NULL; - - { - testing("message in range"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)) { - logerr("print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)\n"); - rc = -1; - goto out; - } - - const int ret = fclose(file); - file = NULL; - if (ret) { - logerr("fclose(file): %s\n", strerror(errno)); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - logerr("slurp_for_tests(FNAME, &str)\n"); - rc = -1; - goto out; - } - - const char *const expected = - "First line\n" - "a second\n" - "a third\n" - "and the last one\n" - ; - - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - { - testing("range begin and end is the same"); - - file = fopen(FNAME, "w"); - if (!file) { - logerr("fopen(FNAME, \"w\"): %s\n", strerror(errno)); - rc = -1; - goto out; - } - - if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)) { - logerr("s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)\n"); - rc = -1; - goto out; - } - - const int ret = fclose(file); - file = NULL; - if (ret) { - logerr("fclose(file): %s\n", strerror(errno)); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - logerr("slurp_for_tests(FNAME, &str)\n"); - rc = -1; - goto out; - } - - const char *const expected = - "First line\n"; - - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - -out: - if (str) { - free(str); - } - if (file) { - if (fclose(file)) { - logerr("fclose(file): %s\n", strerror(errno)); - rc = -1; - } - } - return rc; -} - -static int -test_s(void) { - int rc = 0; - - test_start("_()"); - FILE *file = NULL; - char *str = NULL; - - { - testing("empty string"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - // FIXME: implement correct test - - - - test_ok(); - } - -out: - if (str) { - free(str); - } - if (file) { - if (fclose(file)) { - logerr("fclose(file): %s\n", strerror(errno)); - rc = -1; - } - } - return rc; -} - -static int -test_i18n_destroy(void) { - int rc = 0; - - test_start("i18n_destroy()"); - - { - testing("simple call without init first"); - - if (i18n_destroy()) { - logerr("i18n_destroy()\n"); - rc = -1; - goto out; - } - - test_ok(); - } - -out: - return rc; -} - -static int -test_s_print_msg(void) { - int rc = 0; - - test_start("s_print_msg()"); - FILE *file = NULL; - char *str = NULL; - - { - testing("simple individual message"); - - file = fopen(FNAME, "w"); - if (!file) { - logerr("fopen(FNAME, \"w\"): %s\n"); - rc = -1; - goto out; - } - - if (s_print_msg(TEST_MSGS, file, MSG_STANDALONE)) { - logerr("s_print_msg(TEST_MSGS, file, MSG_STANDALONE)\n"); - rc = -1; - goto out; - } - - const int ret = fclose(file); - file = NULL; - if (ret) { - logerr("fopen(file): %s\n", strerror(errno)); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - logerr("slurp_for_tests(FNAME, &str)\n"); - rc = -1; - goto out; - } - - const char *const expected = - "single line message\n"; - - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - -out: - if (str) { - free(str); - } - if (file) { - if (fclose(file)) { - logerr("fclose(file): %s\n", strerror(errno)); - rc = -1; - } - } - return rc; -} - -int -main(void) { - int rc = 0; - - if (test_i18n_init()) { - logerr("test_i18n_init()\n"); - rc = -1; - goto out; - } - - if (test_i18n_destroy()) { - logerr("test_i18n_destroy()\n"); - rc = -1; - goto out; - } - - if (test_s()) { - logerr("test_s()\n"); - rc = -1; - goto out; - } - - if (test_s_print_msgs()) { - logerr("test_s_print_msgs()\n"); - rc = -1; - goto out; - } - - if (test_s_print_msg()) { - logerr("test_s_print_msg()\n"); - rc = -1; - goto out; - } - -out: - return !!rc; -} -#endif @@ -47,29 +47,3 @@ MSGS[] = { [MSG_ERR_VECTOR_OUT_OF_BOUNDS]= "idx (%ld) is beyond bounds (%ld)\n", NULL }; - - - -#ifdef TEST -#include <stdlib.h> - -#include "logerr.h" -#include "catalog.h" - - -int -main(void) { - int rc = 0; - - if (getenv("DUMP_TRANSLATABLE_STRINGS")) { - if (dump_translatable_strings(MSGS)) { - logerr("dump_translatable_strings(MSGS)\n"); - rc = -1; - goto out; - } - } - -out: - return !!rc; -} -#endif @@ -9,12 +9,3 @@ re_main(int argc, char **argv) { } return rc; } - - -#ifdef TEST -int -main(void) { - int rc = 0; - return rc; -} -#endif diff --git a/src/logerr.c b/src/logerr.c index ab13e78..c00a4c1 100644 --- a/src/logerr.c +++ b/src/logerr.c @@ -28,287 +28,3 @@ vlogerr( return; } - - -#ifdef TEST -#include <assert.h> -#include <errno.h> -#include <string.h> - -#include "testing.h" -#include "../tests/slurp.h" - - -static const char *const -FNAME = __FILE__ ".txt"; - -static int -test_vlogerr(void) { - int rc = 0; - - test_start("vlogerr()"); - FILE *file = NULL; - char *str = NULL; - - { - testing("empty varargs"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - #line 100 - vlogerr(__FILE__, __func__, __LINE__, file, - ""); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - rc = -1; - goto out; - } - - const char *const expected = - "src/logerr.c:test_vlogerr:100: "; - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - { - testing("a newline only"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - #line 200 - vlogerr(__FILE__, __func__, __LINE__, file, - "\n"); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - rc = -1; - goto out; - } - - const char *const expected = - "src/logerr.c:test_vlogerr:200: \n"; - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - { - testing("static format string"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - #line 300 - vlogerr(__FILE__, __func__, __LINE__, file, - "some static string\n"); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - rc = -1; - goto out; - } - - const char *const expected = - "src/logerr.c:test_vlogerr:300: some static string\n"; - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - { - testing("single arg format string"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - #line 400 - vlogerr(__FILE__, __func__, __LINE__, file, - "fn(%s)\n", "an-arg"); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - rc = -1; - goto out; - } - - const char *const expected = - "src/logerr.c:test_vlogerr:400: fn(an-arg)\n"; - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - { - testing("multiple format strings"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - #line 500 - vlogerr(__FILE__, __func__, __LINE__, file, - "int (%d), string (%s) and char (%c)\n", - 123, - "another-str", - 'z'); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - rc = -1; - goto out; - } - - const char *const expected = - "src/logerr.c:test_vlogerr:500: " - "int (123), string (another-str) and char (z)\n"; - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - -out: - if (str) { - free(str); - } - if (file) { - if (fclose(file)) { - perror("fclose(file)"); - rc = -1; - } - } - return rc; -} - -static int -test_logerr(void) { - int rc = 0; - - test_start("logerr()"); - - { - testing("can be called with an empty string"); - - logerr(""); - - test_ok(); - } - { - testing("can be called with a static string"); - - logerr("some err\n"); - - test_ok(); - } - { - testing("can be called with a formatted string"); - - logerr("some err: %s\n", strerror(errno)); - - test_ok(); - } - { - testing("can be called with formatting arguments"); - - logerr("int: %d\nstr: %s\n", 123, "an example string"); - - test_ok(); - } - - return rc; -} - - -int -main(void) { - int rc = 0; - - if (test_vlogerr()) { - perror("test_vlogerr()"); - rc = -1; - goto out; - } - - if (test_logerr()) { - perror("test_logerr()"); - rc = -1; - goto out; - } - -out: - return !!rc; -} -#endif diff --git a/src/random.c b/src/random.c index c6da688..a35eafd 100644 --- a/src/random.c +++ b/src/random.c @@ -41,78 +41,3 @@ out: } return rc; } - - -#ifdef TEST -#include "testing.h" - -static int -test_urandom_bytes(void) { - int rc = 0; - - test_start("urandom_bytes()"); - - { - testing("we get to pick the size that comes out"); - - const size_t LEN = 256; - uint8_t arr[256 /* LEN */] = { 0 }; - - for (size_t n = 0; n < LEN; n++) { - if (urandom_bytes(n, &arr)) { - logerr("urandom_bytes(n, &arr);\n"); - rc = -1; - goto out; - } - for (size_t i = n; i < LEN; i++) { - assert(arr[i] == 0); - } - } - - test_ok(); - } - - { - testing("we always get a new value as a result"); - - const size_t LEN = 64; - uint8_t arr1[64 /* LEN */] = { 0 }; - uint8_t arr2[64 /* LEN */] = { 0 }; - - if (urandom_bytes(LEN, &arr1)) { - logerr("urandom_bytes(LEN, &arr1);\n"); - rc = -1; - goto out; - } - - const size_t attempts = 10; - for (size_t n = 0; n < attempts; n++) { - if (urandom_bytes(LEN, &arr2)) { - logerr("urandom_bytes(LEN, &arr2);\n"); - rc = -1; - goto out; - } - assert(memcmp(arr1, arr2, LEN) != 0); - } - - test_ok(); - } - -out: - return rc; -} - -int -main(void) { - int rc = 0; - - if (test_urandom_bytes()) { - logerr("test_urandom_bytes();\n"); - rc = -1; - goto out; - } - -out: - return !!rc; -} -#endif diff --git a/src/testing.c b/src/testing.c index 028e48f..e8c0fa6 100644 --- a/src/testing.c +++ b/src/testing.c @@ -69,46 +69,3 @@ test_ok(void) { return; } - -#ifdef TEST -int -main(void) { - int rc = 0; - - test_start("testing.c"); - const int should_overwrite = 1; - - if (unsetenv(ENVVAR_NAME)) { - perror("unsetenv(\"NO_COLOR\")"); - rc = -1; - goto out; - } - { - testing("unset NO_COLOR"); - test_ok(); - } - - if (setenv(ENVVAR_NAME, "", should_overwrite)) { - perror("setenv(\"NO_COLOR\", \"\", 1)"); - rc = -1; - goto out; - } - { - testing("empty NO_COLOR"); - test_ok(); - } - - if (setenv(ENVVAR_NAME, "something", should_overwrite)) { - perror("setenv(\"NO_COLOR\", \"something\", 1)"); - rc = -1; - goto out; - } - { - testing("defined NO_COLOR"); - test_ok(); - } - -out: - return !!rc; -} -#endif diff --git a/tests/catalog.c b/tests/catalog.c new file mode 100644 index 0000000..d357175 --- /dev/null +++ b/tests/catalog.c @@ -0,0 +1,327 @@ +#include "../src/catalog.c" + +#include "../src/testing.h" +#include "slurp.h" + + +static const char *const +FNAME = __FILE__ ".txt"; + +enum TEST_MSGCATALOG_ID { + MSG_X_FIRST = 1, + MSG_X_1, + MSG_X_2, + MSG_X_LAST, + MSG_STANDALONE, +}; + +static const char *const +TEST_MSGS[] = { + "", + [MSG_X_FIRST]="First line\n", + [MSG_X_1]="a second\n", + [MSG_X_2]="a third\n", + [MSG_X_LAST]="and the last one\n", + [MSG_STANDALONE]="single line message\n", + NULL +}; + +static int +test_i18n_init(void) { + int rc = 0; + + test_start("i18n_init()"); + + { + testing("simple call without touching the environment"); + + const int should_overwrite = 1; + if (setenv(NLSPATH_KEY, "src/%N.en.cat", should_overwrite)) { + logerr("setenv(\"%s\", \"src/%%N.en.cat\", 1): %s\n", + NLSPATH_KEY, strerror(errno)); + rc = -1; + goto out; + } + + if (i18n_init()) { + logerr("i18n_init()\n"); + rc = -1; + goto out; + } + + test_ok(); + } + +out: + if (i18n_destroy()) { + logerr("i18n_destroy()\n"); + rc = -1; + } + return rc; +} + +static int +test_s_print_msgs(void) { + int rc = 0; + + test_start("s_print_msgs()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("message in range"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)) { + logerr("print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)\n"); + rc = -1; + goto out; + } + + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests(FNAME, &str)\n"); + rc = -1; + goto out; + } + + const char *const expected = + "First line\n" + "a second\n" + "a third\n" + "and the last one\n" + ; + + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("range begin and end is the same"); + + file = fopen(FNAME, "w"); + if (!file) { + logerr("fopen(FNAME, \"w\"): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)) { + logerr("s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)\n"); + rc = -1; + goto out; + } + + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests(FNAME, &str)\n"); + rc = -1; + goto out; + } + + const char *const expected = + "First line\n"; + + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} + +static int +test_s(void) { + int rc = 0; + + test_start("_()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("empty string"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + // FIXME: implement correct test + + + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} + +static int +test_i18n_destroy(void) { + int rc = 0; + + test_start("i18n_destroy()"); + + { + testing("simple call without init first"); + + if (i18n_destroy()) { + logerr("i18n_destroy()\n"); + rc = -1; + goto out; + } + + test_ok(); + } + +out: + return rc; +} + +static int +test_s_print_msg(void) { + int rc = 0; + + test_start("s_print_msg()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("simple individual message"); + + file = fopen(FNAME, "w"); + if (!file) { + logerr("fopen(FNAME, \"w\"): %s\n"); + rc = -1; + goto out; + } + + if (s_print_msg(TEST_MSGS, file, MSG_STANDALONE)) { + logerr("s_print_msg(TEST_MSGS, file, MSG_STANDALONE)\n"); + rc = -1; + goto out; + } + + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fopen(file): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests(FNAME, &str)\n"); + rc = -1; + goto out; + } + + const char *const expected = + "single line message\n"; + + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} + +int +main(void) { + int rc = 0; + + if (test_i18n_init()) { + logerr("test_i18n_init()\n"); + rc = -1; + goto out; + } + + if (test_i18n_destroy()) { + logerr("test_i18n_destroy()\n"); + rc = -1; + goto out; + } + + if (test_s()) { + logerr("test_s()\n"); + rc = -1; + goto out; + } + + if (test_s_print_msgs()) { + logerr("test_s_print_msgs()\n"); + rc = -1; + goto out; + } + + if (test_s_print_msg()) { + logerr("test_s_print_msg()\n"); + rc = -1; + goto out; + } + +out: + return !!rc; +} diff --git a/tests/i18n.c b/tests/i18n.c new file mode 100644 index 0000000..47a3247 --- /dev/null +++ b/tests/i18n.c @@ -0,0 +1,23 @@ +#include "../src/i18n.c" + +#include <stdlib.h> + +#include "../src/logerr.h" +#include "../src/catalog.h" + + +int +main(void) { + int rc = 0; + + if (getenv("DUMP_TRANSLATABLE_STRINGS")) { + if (dump_translatable_strings(MSGS)) { + logerr("dump_translatable_strings(MSGS)\n"); + rc = -1; + goto out; + } + } + +out: + return !!rc; +} diff --git a/tests/lib.c b/tests/lib.c new file mode 100644 index 0000000..14fdd4f --- /dev/null +++ b/tests/lib.c @@ -0,0 +1,8 @@ +#include "../src/lib.c" + + +int +main(void) { + int rc = 0; + return rc; +} diff --git a/tests/logerr.c b/tests/logerr.c new file mode 100644 index 0000000..78cdf10 --- /dev/null +++ b/tests/logerr.c @@ -0,0 +1,283 @@ +#include "../src/logerr.c" + + +#include <assert.h> +#include <errno.h> +#include <string.h> + +#include "../src/testing.h" +#include "slurp.h" + + +static const char *const +FNAME = __FILE__ ".txt"; + +static int +test_vlogerr(void) { + int rc = 0; + + test_start("vlogerr()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("empty varargs"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 100 + vlogerr(__FILE__, __func__, __LINE__, file, + ""); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "tests/logerr.c:test_vlogerr:100: "; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("a newline only"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 200 + vlogerr(__FILE__, __func__, __LINE__, file, + "\n"); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "tests/logerr.c:test_vlogerr:200: \n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("static format string"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 300 + vlogerr(__FILE__, __func__, __LINE__, file, + "some static string\n"); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "tests/logerr.c:test_vlogerr:300: some static string\n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("single arg format string"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 400 + vlogerr(__FILE__, __func__, __LINE__, file, + "fn(%s)\n", "an-arg"); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "tests/logerr.c:test_vlogerr:400: fn(an-arg)\n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("multiple format strings"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 500 + vlogerr(__FILE__, __func__, __LINE__, file, + "int (%d), string (%s) and char (%c)\n", + 123, + "another-str", + 'z'); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "tests/logerr.c:test_vlogerr:500: " + "int (123), string (another-str) and char (z)\n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + perror("fclose(file)"); + rc = -1; + } + } + return rc; +} + +static int +test_logerr(void) { + int rc = 0; + + test_start("logerr()"); + + { + testing("can be called with an empty string"); + + logerr(""); + + test_ok(); + } + { + testing("can be called with a static string"); + + logerr("some err\n"); + + test_ok(); + } + { + testing("can be called with a formatted string"); + + logerr("some err: %s\n", strerror(errno)); + + test_ok(); + } + { + testing("can be called with formatting arguments"); + + logerr("int: %d\nstr: %s\n", 123, "an example string"); + + test_ok(); + } + + return rc; +} + + +int +main(void) { + int rc = 0; + + if (test_vlogerr()) { + perror("test_vlogerr()"); + rc = -1; + goto out; + } + + if (test_logerr()) { + perror("test_logerr()"); + rc = -1; + goto out; + } + +out: + return !!rc; +} diff --git a/tests/random.c b/tests/random.c new file mode 100644 index 0000000..eec83c5 --- /dev/null +++ b/tests/random.c @@ -0,0 +1,73 @@ +#include "../src/random.c" +#include "../src/testing.h" + + +static int +test_urandom_bytes(void) { + int rc = 0; + + test_start("urandom_bytes()"); + + { + testing("we get to pick the size that comes out"); + + const size_t LEN = 256; + uint8_t arr[256 /* LEN */] = { 0 }; + + for (size_t n = 0; n < LEN; n++) { + if (urandom_bytes(n, &arr)) { + logerr("urandom_bytes(n, &arr);\n"); + rc = -1; + goto out; + } + for (size_t i = n; i < LEN; i++) { + assert(arr[i] == 0); + } + } + + test_ok(); + } + + { + testing("we always get a new value as a result"); + + const size_t LEN = 64; + uint8_t arr1[64 /* LEN */] = { 0 }; + uint8_t arr2[64 /* LEN */] = { 0 }; + + if (urandom_bytes(LEN, &arr1)) { + logerr("urandom_bytes(LEN, &arr1);\n"); + rc = -1; + goto out; + } + + const size_t attempts = 10; + for (size_t n = 0; n < attempts; n++) { + if (urandom_bytes(LEN, &arr2)) { + logerr("urandom_bytes(LEN, &arr2);\n"); + rc = -1; + goto out; + } + assert(memcmp(arr1, arr2, LEN) != 0); + } + + test_ok(); + } + +out: + return rc; +} + +int +main(void) { + int rc = 0; + + if (test_urandom_bytes()) { + logerr("test_urandom_bytes();\n"); + rc = -1; + goto out; + } + +out: + return !!rc; +} diff --git a/tests/testing.c b/tests/testing.c new file mode 100644 index 0000000..65012f9 --- /dev/null +++ b/tests/testing.c @@ -0,0 +1,43 @@ +#include "../src/testing.c" + + +int +main(void) { + int rc = 0; + + test_start("testing.c"); + const int should_overwrite = 1; + + if (unsetenv(ENVVAR_NAME)) { + perror("unsetenv(\"NO_COLOR\")"); + rc = -1; + goto out; + } + { + testing("unset NO_COLOR"); + test_ok(); + } + + if (setenv(ENVVAR_NAME, "", should_overwrite)) { + perror("setenv(\"NO_COLOR\", \"\", 1)"); + rc = -1; + goto out; + } + { + testing("empty NO_COLOR"); + test_ok(); + } + + if (setenv(ENVVAR_NAME, "something", should_overwrite)) { + perror("setenv(\"NO_COLOR\", \"something\", 1)"); + rc = -1; + goto out; + } + { + testing("defined NO_COLOR"); + test_ok(); + } + +out: + return !!rc; +} |