diff options
author | EuAndreh <eu@euandre.org> | 2025-01-12 00:14:03 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-01-12 14:27:57 -0300 |
commit | 44d56f5311f98a8955c67638e7520963dbd4d845 (patch) | |
tree | fbb2c58c79f1730ff62c83cef116fb5c0e035dfe | |
parent | Replace src/config.h with <s.h>; incorporate changes from other projects (diff) | |
download | pindaiba-44d56f5311f98a8955c67638e7520963dbd4d845.tar.gz pindaiba-44d56f5311f98a8955c67638e7520963dbd4d845.tar.xz |
Revamp lib, simplify it a bit and address some FIXMEs
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | deps.mk | 100 | ||||
-rw-r--r-- | src/catalog.h | 22 | ||||
-rw-r--r-- | src/cmp.c | 2 | ||||
-rw-r--r-- | src/hash.c | 83 | ||||
-rw-r--r-- | src/hash.h | 6 | ||||
-rw-r--r-- | src/i18n.c | 18 | ||||
-rw-r--r-- | src/lib.c | 4 | ||||
-rw-r--r-- | src/lib.h | 3 | ||||
-rw-r--r-- | src/logerr.c | 52 | ||||
-rw-r--r-- | src/logerr.h | 25 | ||||
-rw-r--r-- | src/main.c | 6 | ||||
-rw-r--r-- | src/math.c | 9 | ||||
-rw-r--r-- | src/math.h | 6 | ||||
-rw-r--r-- | src/meta.h.in | 2 | ||||
-rw-r--r-- | src/msgs.c (renamed from src/catalog.c) | 35 | ||||
-rw-r--r-- | src/msgs.h | 26 | ||||
-rw-r--r-- | src/pindaiba.en.msg | 22 | ||||
-rw-r--r-- | src/random.c | 109 | ||||
-rw-r--r-- | src/random.h | 16 | ||||
-rw-r--r-- | src/set.c | 14 | ||||
-rw-r--r-- | src/string.c | 58 | ||||
-rw-r--r-- | src/string.h | 2 | ||||
-rw-r--r-- | src/testing.c | 31 | ||||
-rw-r--r-- | src/testing.h | 3 | ||||
-rw-r--r-- | src/trace.c | 94 | ||||
-rw-r--r-- | src/trace.h | 49 | ||||
-rw-r--r-- | src/tree.c | 47 | ||||
-rw-r--r-- | src/tree.h | 10 | ||||
-rw-r--r-- | src/util.c | 26 | ||||
-rw-r--r-- | src/util.h | 13 | ||||
-rw-r--r-- | src/vector.c | 39 | ||||
-rwxr-xr-x | tests/cli-opts.sh | 11 | ||||
-rw-r--r-- | tests/cmp.c | 1 | ||||
-rw-r--r-- | tests/hash.c | 33 | ||||
-rw-r--r-- | tests/i18n.c | 2 | ||||
-rw-r--r-- | tests/lib.c | 5 | ||||
-rw-r--r-- | tests/logerr.c | 90 | ||||
-rw-r--r-- | tests/math.c | 2 | ||||
-rw-r--r-- | tests/msgs.c (renamed from tests/catalog.c) | 132 | ||||
-rw-r--r-- | tests/random.c | 75 | ||||
-rw-r--r-- | tests/set.c | 8 | ||||
-rw-r--r-- | tests/slurp.c | 7 | ||||
-rw-r--r-- | tests/string.c | 12 | ||||
-rw-r--r-- | tests/testing.c | 82 | ||||
-rw-r--r-- | tests/trace.c | 146 | ||||
-rw-r--r-- | tests/util.c | 101 | ||||
-rw-r--r-- | tests/vector.c | 237 |
48 files changed, 1052 insertions, 831 deletions
@@ -81,9 +81,12 @@ derived-assets = \ $(NAME).bin \ side-assets = \ - tests/catalog.c.txt \ tests/logerr.c.txt \ + tests/msgs.c.txt \ + tests/testing.c.txt \ tests/trace.c.txt \ + tests/util.c.txt \ + tests/vector.c.txt \ @@ -101,7 +104,7 @@ src/main.o: src/$(NAME).h $(archives): $(AR) $(ARFLAGS) $@ $? -tests/catalog.bin-check: src/$(NAME).en.cat +tests/msgs.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 @@ -3,13 +3,13 @@ catalogs.msg = $(catalogs.en.msg) sources.c = \ - src/catalog.c \ src/cmp.c \ src/hash.c \ src/i18n.c \ src/lib.c \ src/logerr.c \ src/math.c \ + src/msgs.c \ src/random.c \ src/set.c \ src/sorted-vector.c \ @@ -21,13 +21,13 @@ sources.c = \ src/vector.c \ tests.c = \ - tests/catalog.c \ tests/cmp.c \ tests/hash.c \ tests/i18n.c \ tests/lib.c \ tests/logerr.c \ tests/math.c \ + tests/msgs.c \ tests/random.c \ tests/set.c \ tests/sorted-vector.c \ @@ -38,13 +38,13 @@ tests.c = \ tests/util.c \ tests/vector.c \ -src/catalog.o: src/catalog.h src/cmp.o: src/cmp.h src/hash.o: src/hash.h src/i18n.o: src/i18n.h src/lib.o: src/lib.h src/logerr.o: src/logerr.h src/math.o: src/math.h +src/msgs.o: src/msgs.h src/random.o: src/random.h src/set.o: src/set.h src/sorted-vector.o: src/sorted-vector.h @@ -55,13 +55,13 @@ src/tree.o: src/tree.h src/util.o: src/util.h src/vector.o: src/vector.h -tests/catalog.o: src/catalog.c src/catalog.h tests/cmp.o: src/cmp.c src/cmp.h tests/hash.o: src/hash.c src/hash.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/math.o: src/math.c src/math.h +tests/msgs.o: src/msgs.c src/msgs.h tests/random.o: src/random.c src/random.h tests/set.o: src/set.c src/set.h tests/sorted-vector.o: src/sorted-vector.c src/sorted-vector.h @@ -72,13 +72,13 @@ tests/tree.o: src/tree.c src/tree.h tests/util.o: src/util.c src/util.h tests/vector.o: src/vector.c src/vector.h -tests/catalog.a: tests/catalog.o tests/cmp.a: tests/cmp.o tests/hash.a: tests/hash.o tests/i18n.a: tests/i18n.o tests/lib.a: tests/lib.o tests/logerr.a: tests/logerr.o tests/math.a: tests/math.o +tests/msgs.a: tests/msgs.o tests/random.a: tests/random.o tests/set.a: tests/set.o tests/sorted-vector.a: tests/sorted-vector.o @@ -89,13 +89,13 @@ tests/tree.a: tests/tree.o tests/util.a: tests/util.o tests/vector.a: tests/vector.o -tests/catalog.bin-check: tests/catalog.bin tests/cmp.bin-check: tests/cmp.bin tests/hash.bin-check: tests/hash.bin tests/i18n.bin-check: tests/i18n.bin tests/lib.bin-check: tests/lib.bin tests/logerr.bin-check: tests/logerr.bin tests/math.bin-check: tests/math.bin +tests/msgs.bin-check: tests/msgs.bin tests/random.bin-check: tests/random.bin tests/set.bin-check: tests/set.bin tests/sorted-vector.bin-check: tests/sorted-vector.bin @@ -107,53 +107,53 @@ tests/util.bin-check: tests/util.bin tests/vector.bin-check: tests/vector.bin -src/catalog.o: src/logerr.h src/cmp.o: -src/hash.o: src/logerr.h src/random.h -src/i18n.o: src/catalog.h -src/lib.o: src/logerr.h +src/hash.o: src/logerr.h src/random.h +src/i18n.o: src/meta.h src/msgs.h +src/lib.o: src/logerr.h src/meta.h src/logerr.o: src/math.o: -src/random.o: src/logerr.h src/util.h -src/set.o: src/hash.h src/logerr.h src/tree.h src/util.h src/vector.h -src/sorted-vector.o: src/logerr.h src/util.h src/vector.h -src/string.o: src/logerr.h src/math.h src/util.h +src/msgs.o: src/logerr.h src/meta.h +src/random.o: src/logerr.h +src/set.o: src/hash.h src/logerr.h src/tree.h src/util.h src/vector.h +src/sorted-vector.o: src/logerr.h src/util.h src/vector.h +src/string.o: src/logerr.h src/math.h src/util.h src/testing.o: -src/trace.o: src/logerr.h -src/tree.o: src/logerr.h src/util.h -src/util.o: src/logerr.h -src/vector.o: src/catalog.h src/i18n.h src/logerr.h src/math.h src/util.h +src/trace.o: src/logerr.h +src/tree.o: src/logerr.h src/util.h +src/util.o: src/logerr.h +src/vector.o: src/msgs.h src/i18n.h src/logerr.h src/math.h src/util.h tests/slurp.h -tests/catalog.o: src/logerr.h src/testing.h src/util.h tests/slurp.h -tests/cmp.o: src/testing.h -tests/hash.o: src/logerr.h src/random.h src/testing.h -tests/i18n.o: src/catalog.h src/logerr.h -tests/lib.o: src/logerr.h -tests/logerr.o: src/testing.h src/util.h tests/slurp.h -tests/math.o: src/testing.h -tests/random.o: src/logerr.h src/util.h src/testing.h -tests/set.o: src/hash.h src/logerr.h src/tree.h src/util.h src/vector.h src/testing.h -tests/sorted-vector.o: src/logerr.h src/util.h src/testing.h -tests/string.o: src/logerr.h src/math.h src/util.h src/testing.h -tests/testing.o: -tests/trace.o: src/logerr.h tests/slurp.h src/testing.h src/util.h -tests/tree.o: src/logerr.h src/util.h src/testing.h -tests/util.o: src/logerr.h src/testing.h tests/slurp.h -tests/vector.o: src/catalog.h src/i18n.h src/logerr.h src/math.h src/util.h src/testing.h +tests/cmp.o: src/testing.h +tests/hash.o: src/logerr.h src/random.h src/testing.h +tests/i18n.o: src/logerr.h src/meta.h src/msgs.h +tests/lib.o: src/logerr.h src/meta.h +tests/logerr.o: src/testing.h tests/slurp.h +tests/math.o: src/testing.h +tests/msgs.o: src/logerr.h src/meta.h tests/slurp.h src/testing.h +tests/random.o: src/logerr.h src/testing.h +tests/set.o: src/hash.h src/logerr.h src/tree.h src/util.h src/vector.h src/testing.h +tests/sorted-vector.o: src/logerr.h src/util.h src/testing.h +tests/string.o: src/logerr.h src/math.h src/util.h src/testing.h +tests/testing.o: src/logerr.h tests/slurp.h +tests/trace.o: src/logerr.h tests/slurp.h src/testing.h src/util.h +tests/tree.o: src/logerr.h src/util.h src/testing.h +tests/util.o: tests/slurp.h src/logerr.h src/testing.h +tests/vector.o: src/msgs.h src/i18n.h src/logerr.h src/math.h src/util.h src/testing.h tests/slurp.h -tests/catalog.a: src/logerr.o src/testing.o src/util.o tests/slurp.o -tests/cmp.a: src/testing.o -tests/hash.a: src/logerr.o src/random.o src/testing.o src/util.o -tests/i18n.a: src/catalog.o src/logerr.o -tests/lib.a: src/logerr.o -tests/logerr.a: src/testing.o src/util.o tests/slurp.o -tests/math.a: src/testing.o -tests/random.a: src/logerr.o src/testing.o src/util.o -tests/set.a: src/catalog.o src/hash.o src/i18n.o src/logerr.o src/math.o src/random.o src/testing.o src/tree.o src/util.o src/vector.o -tests/sorted-vector.a: src/logerr.o src/testing.o src/util.o src/vector.o -tests/string.a: src/logerr.o src/math.o src/testing.o src/util.o -tests/testing.a: -tests/trace.a: src/logerr.o tests/slurp.o src/testing.o src/util.o -tests/tree.a: src/logerr.o src/testing.o src/util.o -tests/util.a: src/logerr.o src/testing.o tests/slurp.o -tests/vector.a: src/catalog.o src/i18n.o src/logerr.o src/math.o src/testing.o src/util.o +tests/cmp.a: src/testing.o +tests/hash.a: src/logerr.o src/random.o src/testing.o +tests/i18n.a: src/logerr.o src/msgs.o +tests/lib.a: src/logerr.o +tests/logerr.a: tests/slurp.o src/testing.o +tests/math.a: src/testing.o +tests/msgs.a: src/logerr.o src/testing.o src/util.o tests/slurp.o +tests/random.a: src/logerr.o src/testing.o +tests/set.a: src/msgs.o src/hash.o src/i18n.o src/logerr.o src/math.o src/random.o src/testing.o src/tree.o src/util.o src/vector.o +tests/sorted-vector.a: src/logerr.o src/testing.o src/util.o src/vector.o +tests/string.a: src/logerr.o src/math.o src/testing.o src/util.o +tests/testing.a: src/logerr.o tests/slurp.o +tests/trace.a: src/logerr.o tests/slurp.o src/testing.o src/util.o +tests/tree.a: src/logerr.o src/testing.o src/util.o +tests/util.a: tests/slurp.o src/logerr.o src/testing.o +tests/vector.a: src/msgs.o src/i18n.o src/logerr.o src/math.o src/testing.o src/util.o tests/slurp.o diff --git a/src/catalog.h b/src/catalog.h deleted file mode 100644 index 3acb24d..0000000 --- a/src/catalog.h +++ /dev/null @@ -1,22 +0,0 @@ -int -i18n_init_msgs(const char *const MSGS[]); - -int -i18n_destroy(void); - -const char * -s(const char *const MSGS[], const int msg_id); - -int -s_print_msgs( - const char *const MSGS[], - FILE *restrict stream, - const int msg_begin, - const int msg_end -); - -int -s_print_msg(const char *const MSGS[], FILE *restrict stream, const int msg_id); - -int -dump_translatable_strings(const char *const MSGS[]); @@ -1,4 +1,4 @@ -#include "config.h" +#include <s.h> #include <stddef.h> @@ -1,104 +1,47 @@ -#include "config.h" +#include <s.h> #include <assert.h> -#include <pthread.h> #include <stdbool.h> -#include <stddef.h> -#include <stdint.h> #include <stdio.h> -#include <string.h> #include <siphash.h> -#include "hash.h" #include "logerr.h" #include "random.h" +#include "hash.h" -static volatile bool -INITIALIZED = false; -static uint8_t -HASH_KEY[SIPHASHBS_KEY_LENGTH]; +static bool +SEED_INITIALIZED = false; -static pthread_mutex_t -SEED_MUTEX = PTHREAD_MUTEX_INITIALIZER; +static u8 +HASH_KEY[SIPHASH_KEY_LENGTH]; -static int -unsafe_init(void) { +int +hash_setup(void) { int rc = -1; - if (urandom_bytes(SIPHASHBS_KEY_LENGTH, &HASH_KEY)) { + if (random_bytes(SIPHASH_KEY_LENGTH, &HASH_KEY)) { logerr("urandom_bytes()"); goto out; } + SEED_INITIALIZED = true; rc = 0; out: return rc; } -static int -safe_init(void) { - int rc = -1; - - bool lock_acquired = false; - - if (INITIALIZED == true) { - rc = 0; - goto out; - } - - const int ret1 = pthread_mutex_lock(&SEED_MUTEX); - if (ret1) { - logerr("pthread_mutex_lock(): %s", strerror(ret1)); - goto out; - } - lock_acquired = true; - - if (INITIALIZED == true) { - rc = 0; - goto out; - } - - if (unsafe_init()) { - logerr("unsafe_init()"); - goto out; - } - - INITIALIZED = true; - - rc = 0; -out: - if (lock_acquired) { - const int ret2 = pthread_mutex_unlock(&SEED_MUTEX); - if (ret2) { - logerr("pthread_mutex_unlock(): %s", strerror(ret2)); - rc = -1; - } - } - return rc; -} - -static void -ensure_initialized(void) { - assert((safe_init() == 0) && "Failed to initialized the hash seed"); -} - -int -hash_init(void) { - return safe_init(); -} - void hash( const size_t inlen, const void *const restrict in, - uint8_t out[HASH_OUTPUT_LENGTH] + u8 out[HASH_OUTPUT_LENGTH] ) { - ensure_initialized(); - siphashbs(HASH_KEY, inlen, in, out); + assert(SEED_INITIALIZED == true); + siphash(HASH_KEY, inlen, in, out); } @@ -1,8 +1,10 @@ -#define HASH_OUTPUT_LENGTH 16U +enum { + HASH_OUTPUT_LENGTH = 16U, +}; void hash( const size_t inlen, const void *const restrict in, - uint8_t out[HASH_OUTPUT_LENGTH] + u8 out[HASH_OUTPUT_LENGTH] ); @@ -1,12 +1,14 @@ -#include "config.h" +#include <s.h> #include <stdio.h> -#include "catalog.h" +#include "meta.h" +#include "msgs.h" #include "i18n.h" + const char *const MSGS[] = { [MSG_USAGE_FIRST]="Usage:\n", @@ -26,11 +28,11 @@ MSGS[] = { [MSG_HELP_10]="\n", [MSG_HELP_11]="Run the " NAME "(1) parser program.\n", [MSG_HELP_12]="\n", - [MSG_HELP_13]="Here is the explanation for what it does, and the synopsis\n", - [MSG_HELP_14]="of its usage.\n", + [MSG_HELP_13]="Here is the explanation for what it does, and the\n", + [MSG_HELP_14]="synopsis of its usage.\n", [MSG_HELP_15]="\n", [MSG_HELP_16]="See \"man " NAME "\" for usage information and\n", - [MSG_HELP_17]="\"man " NAME ".tutorial\" for a beginner introduction.\n", + [MSG_HELP_17]="\"man " NAME ".tutorial\" for a beginner intro.\n", [MSG_HELP_18]="\n", [MSG_HELP_19]="\n", [MSG_HELP_20]="Examples:\n", @@ -50,12 +52,14 @@ MSGS[] = { NULL }; + + const char * _(const int msg_id) { - return s(MSGS, msg_id); + return msgs_string(MSGS, msg_id); } int i18n_init(void) { - return i18n_init_msgs(MSGS); + return msgs_init(MSGS); } @@ -1,4 +1,4 @@ -#include "config.h" +#include <s.h> #include <errno.h> #include <stdio.h> @@ -6,10 +6,12 @@ #include <string.h> #include "logerr.h" +#include "meta.h" #include "lib.h" + int pindaiba_main(int argc, char *argv[]) { int rc = EXIT_FAILURE; @@ -1,5 +1,2 @@ -extern const size_t -NULL_TERMINATOR; - int pindaiba_main(int argc, char *argv[]); diff --git a/src/logerr.c b/src/logerr.c index fac87f0..2d5c340 100644 --- a/src/logerr.c +++ b/src/logerr.c @@ -1,34 +1,72 @@ -#include "config.h" +#include <s.h> #include <stdarg.h> #include <stdio.h> -#include <stdlib.h> #include "logerr.h" +static FILE * +STREAM = NULL; + + + void -vlogerr( +vflogerrf( const char *const file, const char *const function, const int lineno, FILE *restrict stream, const char *restrict format, - ... + va_list args ) { if (fprintf(stream, "%s:%s:%d: ", file, function, lineno) < 0) { perror(__FILE__ ":vlogerr(): fprintf() < 0"); } - va_list args; - va_start(args, format); if (vfprintf(stream, format, args) < 0) { perror(__FILE__ ":vlogerr(): vfprintf() < 0"); } - va_end(args); if (fprintf(stream, "\n") < 0) { perror(__FILE__ ":vlogerr(): fprintf() < 0"); } } + +void +flogerrf( + const char *const file, + const char *const function, + const int lineno, + FILE *restrict stream, + const char *restrict format, + ... +) { + va_list args; + va_start(args, format); + vflogerrf(file, function, lineno, stream, format, args); + va_end(args); +} + +void +logerrf( + const char *const file, + const char *const function, + const int lineno, + const char *restrict format, + ... +) { + if (STREAM == NULL) { + STREAM = stderr; + } + va_list args; + va_start(args, format); + vflogerrf(file, function, lineno, STREAM, format, args); + va_end(args); +} + +void +logerr_set_stream(FILE *stream) { + STREAM = stream; +} diff --git a/src/logerr.h b/src/logerr.h index 736da7a..346989d 100644 --- a/src/logerr.h +++ b/src/logerr.h @@ -1,10 +1,19 @@ -#define logerr(...) vlogerr(__FILE__, __func__, __LINE__, stderr, __VA_ARGS__) +#define logerr(...) logerrf(__FILE__, __func__, __LINE__, __VA_ARGS__) void -vlogerr( +vflogerrf( + const char *const file, + const char *const function, + const int lineno, + FILE *restrict stream, + const char *restrict format, + va_list args +); +void +flogerrf( const char *const file, const char *const function, const int lineno, @@ -12,3 +21,15 @@ vlogerr( const char *restrict format, ... ); + +void +logerrf( + const char *const file, + const char *const function, + const int lineno, + const char *restrict format, + ... +); + +void +logerr_set_stream(FILE *stream); @@ -1,8 +1,8 @@ -#include "config.h" +#include <s.h> + +#include "pindaiba.h" -#include <stddef.h> -#include "pindaibabs.h" int main(int argc, char *argv[]) { @@ -1,4 +1,4 @@ -#include "config.h" +#include <s.h> #include <assert.h> #include <errno.h> @@ -9,6 +9,7 @@ #include "math.h" + int add_size(const size_t x, const size_t y, size_t *const out) { int rc = EOVERFLOW; @@ -42,5 +43,7 @@ out: return rc; } -extern inline size_t -max_size(const size_t x, const size_t y); +size_t +max_size(const size_t x, const size_t y) { + return x > y ? x : y; +} @@ -4,7 +4,5 @@ add_size(const size_t x, const size_t y, size_t *const out); int mul_size(const size_t x, const size_t y, size_t *const out); -inline size_t -max_size(size_t x, size_t y) { - return x > y ? x : y; -} +size_t +max_size(size_t x, size_t y); diff --git a/src/meta.h.in b/src/meta.h.in index 9bd8c08..52f3437 100644 --- a/src/meta.h.in +++ b/src/meta.h.in @@ -1,3 +1,5 @@ #define VERSION "@VERSION@" #define DATE "@DATE@" #define NAME "@NAME@" + +#define LOCALEDIR "@LOCALEDIR@" diff --git a/src/catalog.c b/src/msgs.c index 9550631..eaa6276 100644 --- a/src/catalog.c +++ b/src/msgs.c @@ -1,4 +1,4 @@ -#include "config.h" +#include <s.h> #include <assert.h> #include <errno.h> @@ -8,12 +8,14 @@ #include <string.h> #include "logerr.h" +#include "meta.h" -#include "catalog.h" +#include "msgs.h" -static const char *const -CATALOG_NAME = NAME; + +static const char +CATALOG_NAME[] = NAME; static nl_catd catalog_descriptor = NULL; @@ -21,16 +23,17 @@ catalog_descriptor = NULL; static size_t msgs_length = 0U; -static const char *const -NLSPATH = LOCALEDIR "/%l_%t/LC_MESSAGES/%N.cat" ":" +static const char +NLSPATH[] = LOCALEDIR "/%l_%t/LC_MESSAGES/%N.cat" ":" LOCALEDIR "/%l/LC_MESSAGES/%N.cat"; -static const char *const -NLSPATH_KEY = "NLSPATH"; +static const char +NLSPATH_KEY[] = "NLSPATH"; + int -i18n_init_msgs(const char *const MSGS[]) { +msgs_init(const char *const MSGS[]) { int rc = -1; static const int should_overwrite = 0; @@ -57,7 +60,7 @@ out: } int -i18n_destroy(void) { +msgs_end(void) { int rc = -1; if (catalog_descriptor != NULL) { @@ -76,7 +79,7 @@ out: } const char * -s(const char* const MSGS[], const int msg_id) { +msgs_string(const char* const MSGS[], const int msg_id) { assert(msg_id >= 0); if (msgs_length != 0U) { assert((size_t)msg_id < msgs_length); @@ -96,7 +99,7 @@ s(const char* const MSGS[], const int msg_id) { } int -s_print_msgs( +msgs_print_ids( const char *const MSGS[], FILE *restrict stream, const int msg_begin, @@ -105,7 +108,7 @@ s_print_msgs( int rc = -1; for (int i = msg_begin; i <= msg_end; i++) { - if (fprintf(stream, "%s", s(MSGS, i)) < 0) { + if (fprintf(stream, "%s", msgs_string(MSGS, i)) < 0) { logerr("fprintf(): %s", strerror(errno)); goto out; } @@ -117,12 +120,12 @@ out: } int -s_print_msg(const char *const MSGS[], FILE *const fd, const int msg_id) { - return s_print_msgs(MSGS, fd, msg_id, msg_id); +msgs_print_id(const char *const MSGS[], FILE *const stream, const int msg_id) { + return msgs_print_ids(MSGS, stream, msg_id, msg_id); } int -dump_translatable_strings(const char *const MSGS[]) { +msgs_dump_translatable_strings(const char *const MSGS[]) { int rc = -1; for (size_t i = 0U; MSGS[i] != NULL; i++) { diff --git a/src/msgs.h b/src/msgs.h new file mode 100644 index 0000000..27a7f26 --- /dev/null +++ b/src/msgs.h @@ -0,0 +1,26 @@ +int +msgs_init(const char *const MSGS[]); + +int +msgs_end(void); + +const char * +msgs_string(const char *const MSGS[], const int msg_id); + +int +msgs_print_ids( + const char *const MSGS[], + FILE *restrict stream, + const int msg_begin, + const int msg_end +); + +int +msgs_print_id( + const char *const MSGS[], + FILE *restrict stream, + const int msg_id +); + +int +msgs_dump_translatable_strings(const char *const MSGS[]); diff --git a/src/pindaiba.en.msg b/src/pindaiba.en.msg index 72c5daa..8fc9fa9 100644 --- a/src/pindaiba.en.msg +++ b/src/pindaiba.en.msg @@ -1,10 +1,10 @@ 0 Usage:\n -1 pindaibabs -p FILE [-o DIRECTORY]\n +1 pindaiba -p FILE [-o DIRECTORY]\n -2 pindaibabs -l FILE [-o DIRECTORY]\n +2 pindaiba -l FILE [-o DIRECTORY]\n -3 pindaibabs [-hV]\n +3 pindaiba [-hV]\n 4 \n @@ -28,19 +28,19 @@ 14 \n -15 Run the pindaibabs(1) parser program.\n +15 Run the pindaiba(1) parser program.\n 16 \n -17 Here is the explanation for what it does, and the synopsis\n +17 Here is the explanation for what it does, and the\n -18 of its usage.\n +18 synopsis of its usage.\n 19 \n -20 See "man pindaibabs" for usage information and\n +20 See "man pindaiba" for usage information and\n -21 "man pindaibabs.tutorial" for a beginner introduction.\n +21 "man pindaiba.tutorial" for a beginner intro.\n 22 \n @@ -54,7 +54,7 @@ 27 \n -28 $ pindaibabs run md.grammar < README.md\n +28 $ pindaiba run md.grammar < README.md\n 29 \n @@ -64,9 +64,9 @@ 32 \n -33 $ pindaibabs build csv.grammar > dunno.alsodunno\n +33 $ pindaiba build csv.grammar > dunno.alsodunno\n -34 pindaibabs 0.1.0 1970-01-01\n +34 pindaiba 0.1.0 1970-01-01\n 35 Already at max capacity (%ld): %s\n diff --git a/src/random.c b/src/random.c index b409e6a..0382b3e 100644 --- a/src/random.c +++ b/src/random.c @@ -1,141 +1,64 @@ -#include "config.h" +#include <s.h> #include <assert.h> #include <errno.h> -#include <stdint.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #include "logerr.h" -#include "util.h" #include "random.h" -struct Random { - FILE *const file_handle; -}; +static FILE * +FRANDOM = NULL; int -urandom_bytes(const size_t n, uint8_t (*const addr)[]) { +random_init(void) { int rc = -1; - uint8_t *ret = NULL; - FILE *f = NULL; - - ret = malloc(n); - if (ret == NULL) { - logerr("malloc(): %s", strerror(errno)); - goto out; - } - - f = fopen("/dev/urandom", "r"); - if (f == NULL) { + FRANDOM = fopen("/dev/urandom", "r"); + if (FRANDOM == NULL) { logerr("fopen(): %s", strerror(errno)); goto out; } - const size_t read_count = fread(ret, 1U, n, f); - if (ferror(f)) { - logerr("fread(): %s", strerror(errno)); - goto out; - } - assert(read_count == n); - - if (fclose(f)) { - logerr("fclose(): %s", strerror(errno)); - goto out; - } - f = NULL; - - memcpy(addr, ret, n); rc = 0; out: - if (f != NULL) { - if (fclose(f)) { - logerr("fclose(): %s", strerror(errno)); - rc = -1; - } - } - if (ret != NULL) { - freeit((void *)&ret); - } return rc; } int -random_generate( - const struct Random *const r, - const size_t length, - uint8_t (*const out)[] -) { +random_end(void) { int rc = -1; - const size_t read_count = fread(out, 1U, length, r->file_handle); - if (ferror(r->file_handle)) { - logerr("fread(): %s", strerror(errno)); + if (fclose(FRANDOM)) { + logerr("fclose(): %s", strerror(errno)); goto out; } - assert(read_count == length); rc = 0; out: + FRANDOM = NULL; return rc; } int -random_new(const struct Random **const out) { +random_bytes(const size_t length, u8 (*const out)[]) { int rc = -1; - const struct Random *ret = NULL; - FILE *f = NULL; - - ret = malloc(sizeof(*ret)); - if (ret == NULL) { - logerr("malloc(): %s", strerror(errno)); - goto out; - } - - f = fopen("/dev/urandom", "r"); - if (f == NULL) { - logerr("fopen(): %s", strerror(errno)); - goto out; - } - - memcpy((void *)ret, &(struct Random) { - .file_handle = f, - }, sizeof(*ret)); - *out = ret; - rc = 0; -out: - if (rc) { - random_free(&ret); - } - return rc; -} - -int -random_free(const struct Random **const r) { - if (r == NULL) { - return 0; - } - - if (*r == NULL) { - return 0; - } - - int rc = -1; - if (fclose((*r)->file_handle)) { - logerr("fclose(): %s", strerror(errno)); + assert(FRANDOM != NULL); + const size_t read_count = fread(out, 1U, length, FRANDOM); + if (ferror(FRANDOM)) { + logerr("fread(): %s", strerror(errno)); goto out; } + assert(read_count == length); rc = 0; out: - freeit((void *)r); return rc; } diff --git a/src/random.h b/src/random.h index 4b31abb..ef876fe 100644 --- a/src/random.h +++ b/src/random.h @@ -1,18 +1,8 @@ -struct Random; - - -int -urandom_bytes(const size_t n, uint8_t (*const addr)[]); - int -random_new(const struct Random **out); +random_init(void); int -random_free(const struct Random **r); +random_end(void); int -random_generate( - const struct Random *r, - const size_t length, - uint8_t (*const out)[] -); +random_bytes(const size_t length, u8 (*const out)[]); @@ -1,4 +1,4 @@ -#include "config.h" +#include <s.h> #include <assert.h> #include <errno.h> @@ -8,13 +8,11 @@ #include <stdint.h> #include <string.h> -#include <endiannessbs.h> -#include <siphashbs.h> +#include <endianness.h> #include "hash.h" #include "logerr.h" #include "tree.h" -#include "util.h" #include "vector.h" #include "set.h" @@ -73,7 +71,8 @@ out: vector_free(&table); } if (ret != NULL) { - freeit((void *)&ret); + free((struct Set *)ret); + ret = NULL; } } return rc; @@ -84,7 +83,8 @@ set_free(const struct Set **const s) { assert((*s) != NULL); const struct Vector *table = (*s)->table; vector_free(&table); - freeit((void *)s); + free((struct Set *)*s); + *s = NULL; } int @@ -94,7 +94,7 @@ set_add(const struct Set *const s, const void *const value) { uint8_t hash_bytes[HASH_OUTPUT_LENGTH]; hash(s->value_size, value, hash_bytes); - const uint64_t hash_value = endiannessbs_from_le64(hash_bytes); + const uint64_t hash_value = endianness_from_le64(hash_bytes); const size_t idx = hash_value % vector_capacity(s->table); const struct Tree *slot; diff --git a/src/string.c b/src/string.c index 74fe2bd..e8c7dbc 100644 --- a/src/string.c +++ b/src/string.c @@ -1,17 +1,14 @@ -#include "config.h" +#include <s.h> #include <assert.h> #include <errno.h> #include <stdbool.h> -#include <stddef.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "logerr.h" #include "math.h" -#include "util.h" #include "string.h" @@ -19,7 +16,7 @@ struct String { const size_t length; - const uint8_t *const bytes; + const u8 *const bytes; }; @@ -33,7 +30,7 @@ string_new_with( int rc = -1; const struct String *ret = NULL; - const uint8_t *bytes = NULL; + const u8 *bytes = NULL; ret = malloc(sizeof(*ret)); if (ret == NULL) { @@ -47,9 +44,9 @@ string_new_with( goto out; } - memcpy((void *)bytes, string, length); - memcpy((void *)(bytes + length), "", NULL_TERMINATOR); - memcpy((void *)ret, &(struct String) { + memcpy((u8 *)bytes, string, length); + memcpy((u8 *)(bytes + length), "", NULL_TERMINATOR); + memcpy((u8 *)ret, &(struct String) { .length = length, .bytes = bytes, }, sizeof(*ret)); @@ -58,10 +55,12 @@ string_new_with( out: if (rc) { if (bytes != NULL) { - freeit((void *)&bytes); + free((u8 *)bytes); + bytes = NULL; } if (ret != NULL) { - freeit((void *)&ret); + free((struct String *)ret); + ret = NULL; } } return rc; @@ -72,11 +71,21 @@ string_new(const char *const string, const struct String **const out) { return string_new_with(string, strlen(string), out); } +struct String +S(const char *const string) { + return (struct String) { + .length = strlen(string) + NULL_TERMINATOR, + .bytes = (const u8 *)string, + }; +} + void string_free(const struct String **const s) { - const uint8_t *bytes = (*s)->bytes; - freeit((void *)&bytes); - freeit((void *)s); + const u8 *bytes = (*s)->bytes; + free((u8 *)bytes); + bytes = NULL; + free((struct String *)*s); + *s = NULL; } const char * @@ -91,7 +100,10 @@ string_compare(const struct String *const s1, const struct String *const s2) { } else if (s1->length > s2->length) { return Comparison_GT; } else { - const int ret = strcmp((const char *)s1->bytes, (const char *)s2->bytes); + const int ret = strcmp( + (const char *)s1->bytes, + (const char *)s2->bytes + ); if (ret < 0) { return Comparison_LT; } else if (ret > 0) { @@ -116,7 +128,7 @@ string_append( int rc = -1; const struct String *ret = NULL; - const uint8_t *bytes = NULL; + const u8 *bytes = NULL; size_t length; if (add_size(s1->length, s2->length, &length)) { @@ -136,10 +148,10 @@ string_append( goto out; } - memcpy((void *)bytes, s1->bytes, s1->length); - memcpy((void *)(bytes + s1->length), s2->bytes, s2->length); - memcpy((void *)(bytes + length), "", NULL_TERMINATOR); - memcpy((void *)ret, &(struct String) { + memcpy((u8 *)bytes, s1->bytes, s1->length); + memcpy((u8 *)(bytes + s1->length), s2->bytes, s2->length); + memcpy((u8 *)(bytes + length), "", NULL_TERMINATOR); + memcpy((u8 *)ret, &(struct String) { .length = length, .bytes = bytes, }, sizeof(*ret)); @@ -148,10 +160,12 @@ string_append( out: if (rc) { if (bytes != NULL) { - freeit((void *)&bytes); + free((u8 *)bytes); + bytes = NULL; } if (ret != NULL) { - freeit((void *)&ret); + free((struct String *)ret); + ret = NULL; } } return rc; diff --git a/src/string.h b/src/string.h index 2ae5a98..7b1e27b 100644 --- a/src/string.h +++ b/src/string.h @@ -6,6 +6,8 @@ enum Comparison { Comparison_GT, }; + + int string_new(const char *const string, const struct String **out); diff --git a/src/testing.c b/src/testing.c index f3239e0..d0b65db 100644 --- a/src/testing.c +++ b/src/testing.c @@ -1,4 +1,4 @@ -#include "config.h" +#include <s.h> #include <stdbool.h> #include <stdio.h> @@ -7,6 +7,7 @@ #include "testing.h" + #define COLOUR_RESET "\033[0m" #define COLOUR_GREEN "\033[0;32m" #define COLOUR_YELLOW "\033[0;33m" @@ -14,6 +15,10 @@ static const char ENVVAR_NAME[] = "NO_COLOUR"; +static FILE * +STREAM = NULL; + + static bool show_colour(void) { @@ -23,20 +28,26 @@ show_colour(void) { void test_start(const char *const name) { - (void)fprintf(stderr, "%s:\n", name); + if (STREAM == NULL) { + STREAM = stderr; + } + (void)fprintf(STREAM, "%s:\n", name); } void testing(const char *const message) { + if (STREAM == NULL) { + STREAM = stderr; + } if (show_colour()) { (void)fprintf( - stderr, + STREAM, COLOUR_YELLOW "testing" COLOUR_RESET ": %s... ", message ); } else { (void)fprintf( - stderr, + STREAM, "testing: %s...", message ); @@ -45,9 +56,17 @@ testing(const char *const message) { void test_ok(void) { + if (STREAM == NULL) { + STREAM = stderr; + } if (show_colour()) { - (void)fprintf(stderr, COLOUR_GREEN "OK" COLOUR_RESET ".\n"); + (void)fprintf(STREAM, COLOUR_GREEN "OK" COLOUR_RESET ".\n"); } else { - (void)fprintf(stderr, " OK.\n"); + (void)fprintf(STREAM, " OK.\n"); } } + +void +test_set_stream(FILE *stream) { + STREAM = stream; +} diff --git a/src/testing.h b/src/testing.h index 9a1bb2e..0bf6b4d 100644 --- a/src/testing.h +++ b/src/testing.h @@ -6,3 +6,6 @@ testing(const char *const message); void test_ok(void); + +void +test_set_stream(FILE *new_stream); diff --git a/src/trace.c b/src/trace.c index b37da2f..335ee43 100644 --- a/src/trace.c +++ b/src/trace.c @@ -1,73 +1,95 @@ -#include "config.h" +#include <s.h> #include <assert.h> +#include <errno.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <threads.h> +#include "logerr.h" #include "trace.h" -thread_local enum TraceLevel -TRACE_LEVEL = TraceLevel_INFO; +static thread_local enum TraceLevel +LEVEL = TraceLevel_INFO; -thread_local FILE * -TRACE_OUTPUT = NULL; +static thread_local FILE * +STREAM = NULL; void -set_level(const enum TraceLevel level) { - assert(level >= TraceLevel_NONE); - assert(level <= TraceLevel_ERROR); - TRACE_LEVEL = level; -} - -void -set_output(FILE *stream) { - TRACE_OUTPUT = stream; -} - -int -vtrace( +vftracef( const char *const file, const char *const function, const int lineno, + FILE *restrict stream, const enum TraceLevel level, const char *restrict format, - ... + va_list args ) { - int rc = 0; + if (level > LEVEL) { + return; + } - // FIXME: test - if (level < TRACE_LEVEL) { - return rc; + if (fprintf(stream, "%s:%s:%d: ", file, function, lineno) < 0) { + logerr("fprintf() < 0: %s", strerror(errno)); } - if (TRACE_OUTPUT == NULL) { - TRACE_OUTPUT = stderr; + if (vfprintf(stream, format, args) < 0) { + perror(__FILE__ ":vtrace(): vfprintf() < 0"); } - // FIXME: weavify! - if (fprintf(TRACE_OUTPUT, "%s:%s:%d: ", file, function, lineno) < 0) { + if (fprintf(stream, "\n") < 0) { perror(__FILE__ ":vtrace(): fprintf() < 0"); - rc = -1; } +} +void +ftracef( + const char *const file, + const char *const function, + const int lineno, + FILE *restrict stream, + const enum TraceLevel level, + const char *restrict format, + ... +) { va_list args; va_start(args, format); - if (vfprintf(TRACE_OUTPUT, format, args) < 0) { - perror(__FILE__ ":vtrace(): vfprintf() < 0"); - rc = -1; - } + vftracef(file, function, lineno, stream, level, format, args); va_end(args); +} - if (fprintf(TRACE_OUTPUT, "\n") < 0) { - perror(__FILE__ ":vtrace(): fprintf() < 0"); - rc = -1; +void +tracef( + const char *const file, + const char *const function, + const int lineno, + const enum TraceLevel level, + const char *restrict format, + ... +) { + if (STREAM == NULL) { + STREAM = stderr; } + va_list args; + va_start(args, format); + vftracef(file, function, lineno, STREAM, level, format, args); + va_end(args); +} - return rc; +void +trace_set_stream(FILE *stream) { + STREAM = stream; +} + +void +trace_set_level(const enum TraceLevel level) { + assert(level >= TraceLevel_NONE); + assert(level <= TraceLevel_DEBUG); + LEVEL = level; } diff --git a/src/trace.h b/src/trace.h index 220e820..b81e616 100644 --- a/src/trace.h +++ b/src/trace.h @@ -1,26 +1,45 @@ -#define trace(level, ...) vtrace(__FILE__, __func__, __LINE__, level, __VA_ARGS__) -#define debug(...) vtrace(__FILE__, __func__, __LINE__, TraceLevel_DEBUG, __VA_ARGS__) -#define info(...) vtrace(__FILE__, __func__, __LINE__, TraceLevel_INFO, __VA_ARGS__) -#define warning(...) vtrace(__FILE__, __func__, __LINE__, TraceLevel_WARNING, __VA_ARGS__) -#define error(...) vtrace(__FILE__, __func__, __LINE__, TraceLevel_ERROR, __VA_ARGS__) +#define trace(level, ...) tracef(__FILE__, __func__, __LINE__, level, __VA_ARGS__) +#define debug(...) tracef(__FILE__, __func__, __LINE__, TraceLevel_DEBUG, __VA_ARGS__) +#define info(...) tracef(__FILE__, __func__, __LINE__, TraceLevel_INFO, __VA_ARGS__) +#define warning(...) tracef(__FILE__, __func__, __LINE__, TraceLevel_WARNING, __VA_ARGS__) +#define error(...) tracef(__FILE__, __func__, __LINE__, TraceLevel_ERROR, __VA_ARGS__) enum TraceLevel { - TraceLevel_DEBUG = 1, - TraceLevel_INFO = 2, + TraceLevel_NONE = 1, + TraceLevel_ERROR = 2, TraceLevel_WARNING = 3, - TraceLevel_ERROR = 4, - TraceLevel_NONE = 5, + TraceLevel_INFO = 4, + TraceLevel_DEBUG = 5, }; void -set_level(const enum TraceLevel level); +vftracef( + const char *const file, + const char *const function, + const int lineno, + FILE *restrict stream, + const enum TraceLevel level, + const char *restrict format, + va_list args +); + +void +ftracef( + const char *const file, + const char *const function, + const int lineno, + FILE *restrict stream, + const enum TraceLevel level, + const char *restrict format, + ... +); -int -vtrace( +void +tracef( const char *const file, const char *const function, const int lineno, @@ -28,3 +47,9 @@ vtrace( const char *restrict format, ... ); + +void +trace_set_stream(FILE *stream); + +void +trace_set_level(const enum TraceLevel level); @@ -1,4 +1,4 @@ -#include "config.h" +#include <s.h> #include <assert.h> #include <errno.h> @@ -14,7 +14,22 @@ +// https://www.sanfoundry.com/c-program-implement-avl-tree/ + +/* + Implement: + + - AVL tree + - B-tree + - Red-black tree + - Hash array-mapped trie + + Also, persistent variations of all of them. +*/ + + struct Tree { + // struct Data data; const void *const value; const size_t value_size; const struct Tree *left; @@ -24,6 +39,30 @@ struct Tree { +/* +static enum Comparison +cmp(struct Data lhs, struct Data rhs) { + if (lhs.length < rhs.length) { + return Comparison_LESSER; + } + + if (lhs.length > rhs.length) { + return Comparison_GREATER; + } + + const int c = memcmp(lhs.data, rhs.data, lhs.length); + if (c < 0) { + return Comparison_LESSER; + } + + if (c > 0) { + return Comparison_GREATER; + } + + return Comparison_EQUAL; +} +*/ + int tree_new(const size_t value_size, const struct Tree **const out) { int rc = -1; @@ -48,7 +87,8 @@ tree_new(const size_t value_size, const struct Tree **const out) { out: if (rc) { if (ret != NULL) { - freeit((void *)&ret); + free((struct Tree *)ret); + ret = NULL; } } return rc; @@ -70,7 +110,8 @@ tree_free(const struct Tree **const t) { tree_free(&right); } - freeit((void *)t); + free((struct Tree *)(*t)); + *t = NULL; } static void @@ -1,5 +1,12 @@ struct Tree; +/* +struct Data { + size_t length; + void *data; +}; +*/ + int @@ -16,3 +23,6 @@ tree_remove(const struct Tree *const t, const void *const value); bool tree_contains(const struct Tree *const, const void *const value); + +// void * +// tree_search(); @@ -1,4 +1,4 @@ -#include "config.h" +#include <s.h> #include <errno.h> #include <stdio.h> @@ -11,26 +11,8 @@ -const size_t -NULL_TERMINATOR = sizeof((char)'\0'); - -const int -EXIT_USAGE = 2; - - - -void -freeit(const void **const ptr) { - if (ptr == NULL) { - return; - } - - free((void *)*ptr); - *ptr = NULL; -} - int -slurp(const char *const filename, char **out) { +slurp(const char *const filename, size_t *const outlen, char **out) { int rc = -1; FILE *file = NULL; @@ -73,6 +55,7 @@ slurp(const char *const filename, char **out) { } str[size] = '\0'; *out = str; + *outlen = size; rc = 0; out: @@ -84,7 +67,8 @@ out: } if (rc) { if (str != NULL) { - freeit((void *)&str); + free(str); + str = NULL; } } return rc; @@ -1,13 +1,2 @@ -extern const size_t -NULL_TERMINATOR; - -extern const int -EXIT_USAGE; - - - -void -freeit(const void **const ptr); - int -slurp(const char *const filename, char **out); +slurp(const char *const filename, size_t *const outlen, char **out); diff --git a/src/vector.c b/src/vector.c index 385e600..2736820 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1,19 +1,16 @@ -#include "config.h" +#include <s.h> #include <assert.h> #include <errno.h> -#include <locale.h> #include <stdbool.h> #include <stdint.h> #include <stdlib.h> #include <stdio.h> #include <string.h> -#include "catalog.h" #include "i18n.h" #include "logerr.h" #include "math.h" -#include "util.h" #include "vector.h" @@ -55,11 +52,8 @@ vector_new_with( assert(capacity > 0); if (capacity > max_capacity) { - logerr( - _(MSG_ERR_VECTOR_MAX_CAPACITY), - max_capacity, - strerror(EOVERFLOW) - ); + const char *const s = _(MSG_ERR_VECTOR_MAX_CAPACITY); + logerr(s, max_capacity, strerror(EOVERFLOW)); rc = EOVERFLOW; goto out; } @@ -90,10 +84,12 @@ vector_new_with( out: if (rc) { if (ret != NULL) { - freeit((void *)&ret); + free((struct Vector *)ret); + ret = NULL; } if (values != NULL) { - freeit((void *)&values); + free((struct Vector *)values); + values = NULL; } } return rc; @@ -121,8 +117,10 @@ vector_free(const struct Vector **const v) { } const void **values = (*v)->values; - freeit((void *)&values); - freeit((void *)v); + free(values); + values = NULL; + free((struct Vector *)(*v)); + *v = NULL; } size_t @@ -146,12 +144,20 @@ next_capacity(const struct Vector *const v) { } static int -next_size(const struct Vector *const v, const size_t capacity, size_t *const out) { +next_size( + const struct Vector *const v, + const size_t capacity, + size_t *const out +) { return mul_size(v->value_size, capacity, out); } int -vector_nth(const struct Vector *const v, const size_t idx, const void **const out) { +vector_nth( + const struct Vector *const v, + const size_t idx, + const void **const out +) { int rc = -1; const size_t count = vector_count(v); @@ -232,7 +238,8 @@ vector_push_back(const struct Vector *const v, const void *const value) { out: if (rc) { if (new_values != NULL) { - freeit((void *)&new_values); + free(new_values); + new_values = NULL; } } return rc; diff --git a/tests/cli-opts.sh b/tests/cli-opts.sh index f8b9f0a..92b70ea 100755 --- a/tests/cli-opts.sh +++ b/tests/cli-opts.sh @@ -1,13 +1,2 @@ #!/bin/sh set -eu - -VERSION="$(awk '/^VERSION / { print $3 }' Makefile)" -DATE="$(awk '/^DATE / { print $3 }' Makefile)" -NAME="$(awk '/^NAME / { print $3 }' Makefile)" - -EXPECTED="$NAME $VERSION $DATE" -GIVEN="$("$@")" - -if [ "$GIVEN" != "$EXPECTED" ]; then - exit 1 -fi diff --git a/tests/cmp.c b/tests/cmp.c index 943402f..df116d0 100644 --- a/tests/cmp.c +++ b/tests/cmp.c @@ -2,6 +2,7 @@ #include <assert.h> #include <stdint.h> +#include <stdio.h> #include "../src/testing.h" diff --git a/tests/hash.c b/tests/hash.c index ebb39a0..b3c2aad 100644 --- a/tests/hash.c +++ b/tests/hash.c @@ -13,30 +13,28 @@ test_HASH_OUTPUT_LENGTH(void) { { testing("Enforce we're always consistent with SipHash"); - assert(HASH_OUTPUT_LENGTH == SIPHASHBS_OUTPUT_LENGTH); + assert((int)HASH_OUTPUT_LENGTH == (int)SIPHASH_OUTPUT_LENGTH); test_ok(); } } static int -test_hash_init(void) { +test_hash_setup(void) { int rc = -1; - test_start("hash_init()"); + test_start("hash_setup()"); { - testing("INITIALIZED changes after calling hash_init()"); + testing("SEED_INITIALIZED changes after calling hash_setup()"); - assert(INITIALIZED == false); + assert(SEED_INITIALIZED == false); - if (hash_init()) { - logerr("hash_init()"); + if (hash_setup()) { + logerr("hash_setup()"); goto out; } - rc = 0; goto out; - - assert(INITIALIZED == true); + assert(SEED_INITIALIZED == true); test_ok(); } @@ -50,14 +48,25 @@ int main(void) { int rc = EXIT_FAILURE; + if (random_init()) { + logerr("random_init()"); + goto out; + } + test_HASH_OUTPUT_LENGTH(); - if (test_hash_init()) { - logerr("test_hash_init()"); + if (test_hash_setup()) { + logerr("test_hash_setup()"); goto out; } rc = EXIT_SUCCESS; out: + if (random_end()) { + if (rc == EXIT_SUCCESS) { + rc = EXIT_FAILURE; + } + } + return rc; } diff --git a/tests/i18n.c b/tests/i18n.c index c9d3453..f26d2ac 100644 --- a/tests/i18n.c +++ b/tests/i18n.c @@ -10,7 +10,7 @@ main(void) { int rc = EXIT_FAILURE; if (getenv("DUMP_TRANSLATABLE_STRINGS")) { - if (dump_translatable_strings(MSGS)) { + if (msgs_dump_translatable_strings(MSGS)) { logerr("dump_translatable_strings()"); goto out; } diff --git a/tests/lib.c b/tests/lib.c index d4e8472..462ae42 100644 --- a/tests/lib.c +++ b/tests/lib.c @@ -1,9 +1,8 @@ #include "../src/lib.c" + int main(void) { - int rc = EXIT_FAILURE; - rc = EXIT_SUCCESS; - return rc; + return EXIT_SUCCESS; } diff --git a/tests/logerr.c b/tests/logerr.c index 8ae1140..9f9c081 100644 --- a/tests/logerr.c +++ b/tests/logerr.c @@ -2,24 +2,24 @@ #include <assert.h> #include <errno.h> +#include <stdlib.h> #include <string.h> #include "../src/testing.h" -#include "../src/util.h" #include "slurp.h" -static const char *const -FNAME = __FILE__ ".txt"; +static const char +FNAME[] = __FILE__ ".txt"; static int -test_vlogerr(void) { +test_flogerrf(void) { int rc = -1; - test_start("vlogerr()"); + test_start("flogerrf()"); FILE *file = NULL; char *str = NULL; @@ -33,7 +33,7 @@ test_vlogerr(void) { } #line 100 - vlogerr(__FILE__, __func__, __LINE__, file, + flogerrf(__FILE__, __func__, __LINE__, file, ""); const int ret = fclose(file); @@ -48,11 +48,12 @@ test_vlogerr(void) { goto out; } - const char *const expected = - "tests/logerr.c:test_vlogerr:100: \n"; + const char expected[] = + "tests/logerr.c:test_flogerrf:100: \n"; assert(strcmp(expected, str) == 0); - freeit((void *)&str); + free(str); + str = NULL; test_ok(); } @@ -66,7 +67,7 @@ test_vlogerr(void) { } #line 200 - vlogerr(__FILE__, __func__, __LINE__, file, + flogerrf(__FILE__, __func__, __LINE__, file, "\n"); const int ret = fclose(file); @@ -81,11 +82,12 @@ test_vlogerr(void) { goto out; } - const char *const expected = - "tests/logerr.c:test_vlogerr:200: \n\n"; + const char expected[] = + "tests/logerr.c:test_flogerrf:200: \n\n"; assert(strcmp(expected, str) == 0); - freeit((void *)&str); + free(str); + str = NULL; test_ok(); } @@ -99,7 +101,7 @@ test_vlogerr(void) { } #line 300 - vlogerr(__FILE__, __func__, __LINE__, file, + flogerrf(__FILE__, __func__, __LINE__, file, "some static string"); const int ret = fclose(file); @@ -114,11 +116,13 @@ test_vlogerr(void) { goto out; } - const char *const expected = - "tests/logerr.c:test_vlogerr:300: some static string\n"; + const char expected[] = + "tests/logerr.c:test_flogerrf:300: " + "some static string\n"; assert(strcmp(expected, str) == 0); - freeit((void *)&str); + free(str); + str = NULL; test_ok(); } @@ -132,7 +136,7 @@ test_vlogerr(void) { } #line 400 - vlogerr(__FILE__, __func__, __LINE__, file, + flogerrf(__FILE__, __func__, __LINE__, file, "fn(%s)", "an-arg"); const int ret = fclose(file); @@ -147,11 +151,12 @@ test_vlogerr(void) { goto out; } - const char *const expected = - "tests/logerr.c:test_vlogerr:400: fn(an-arg)\n"; + const char expected[] = + "tests/logerr.c:test_flogerrf:400: fn(an-arg)\n"; assert(strcmp(expected, str) == 0); - freeit((void *)&str); + free(str); + str = NULL; test_ok(); } @@ -165,7 +170,7 @@ test_vlogerr(void) { } #line 500 - vlogerr(__FILE__, __func__, __LINE__, file, + flogerrf(__FILE__, __func__, __LINE__, file, "int (%d), string (%s) and char (%c)", 123, "another-str", @@ -183,12 +188,13 @@ test_vlogerr(void) { goto out; } - const char *const expected = - "tests/logerr.c:test_vlogerr:500: " + const char expected[] = + "tests/logerr.c:test_flogerrf:500: " "int (123), string (another-str) and char (z)\n"; assert(strcmp(expected, str) == 0); - freeit((void *)&str); + free(str); + str = NULL; test_ok(); } @@ -196,7 +202,8 @@ test_vlogerr(void) { rc = 0; out: if (str != NULL) { - freeit((void *)&str); + free(str); + str = NULL; } if (file != NULL) { if (fclose(file)) { @@ -207,10 +214,19 @@ out: return rc; } -static void +static int test_logerr(void) { + int rc = -1; + test_start("logerr()"); + FILE *file = fopen("/dev/null", "w"); + if (file == NULL) { + perror("fopen(\"/dev/null\", \"w\")"); + goto out; + } + logerr_set_stream(file); + { testing("can be called with an empty string"); @@ -239,6 +255,17 @@ test_logerr(void) { test_ok(); } + + rc = 0; +out: + logerr_set_stream(NULL); + if (file != NULL) { + if (fclose(file)) { + perror("fclose(file)"); + rc = -1; + } + } + return rc; } @@ -246,12 +273,15 @@ int main(void) { int rc = EXIT_FAILURE; - if (test_vlogerr()) { - perror("test_vlogerr()"); + if (test_flogerrf()) { + perror("test_flogerrf()"); goto out; } - test_logerr(); + if (test_logerr()) { + perror("test_logerr()"); + goto out; + } rc = EXIT_SUCCESS; out: diff --git a/tests/math.c b/tests/math.c index 5dcebda..35d57e5 100644 --- a/tests/math.c +++ b/tests/math.c @@ -1,10 +1,12 @@ #include "../src/math.c" #include <stdlib.h> +#include <stdio.h> #include "../src/testing.h" + static void test_add_size(void) { test_start("add_size()"); diff --git a/tests/catalog.c b/tests/msgs.c index 4464fbe..37c8890 100644 --- a/tests/catalog.c +++ b/tests/msgs.c @@ -1,12 +1,9 @@ -#include "../src/catalog.c" +#include "../src/msgs.c" #include "../src/testing.h" -#include "../src/util.h" #include "slurp.h" -static const char *const -FNAME = __FILE__ ".txt"; enum TEST_MSGCATALOG_ID { MSG_X_FIRST, @@ -16,10 +13,8 @@ enum TEST_MSGCATALOG_ID { MSG_STANDALONE, }; -#define TEST_MSGS_LEN 6U - static const char *const -TEST_MSGS[TEST_MSGS_LEN] = { +TEST_MSGS[] = { [MSG_X_FIRST]="First line\n", [MSG_X_1]="a second\n", [MSG_X_2]="a third\n", @@ -28,11 +23,19 @@ TEST_MSGS[TEST_MSGS_LEN] = { NULL }; +static size_t +TEST_MSGS_LEN = nelem(TEST_MSGS); + +static const char +FNAME[] = __FILE__ ".txt"; + + + static int -test_i18n_init_msgs(void) { +test_msgs_init(void) { int rc = -1; - test_start("i18n_init_msgs()"); + test_start("msgs_init()"); { testing("simple call without touching the environment"); @@ -43,8 +46,8 @@ test_i18n_init_msgs(void) { goto out; } - if (i18n_init_msgs(TEST_MSGS)) { - logerr("i18n_init_msgs()"); + if (msgs_init(TEST_MSGS)) { + logerr("msgs_init()"); goto out; } assert(msgs_length == TEST_MSGS_LEN - 1U); @@ -54,18 +57,18 @@ test_i18n_init_msgs(void) { rc = 0; out: - if (i18n_destroy()) { - logerr("i18n_destroy()"); + if (msgs_end()) { + logerr("msgs_end()"); rc = -1; } return rc; } static int -test_s_print_msgs(void) { +test_msgs_print_ids(void) { int rc = -1; - test_start("s_print_msgs()"); + test_start("msgs_print_ids()"); FILE *file = NULL; char *str = NULL; @@ -73,13 +76,13 @@ test_s_print_msgs(void) { testing("message in range"); file = fopen(FNAME, "w"); - if (!file) { + if (file == NULL) { perror("fopen(FNAME, \"w\")"); goto out; } - if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)) { - logerr("s_print_msgs()"); + if (msgs_print_ids(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)) { + logerr("msgs_print_ids()"); goto out; } @@ -95,7 +98,7 @@ test_s_print_msgs(void) { goto out; } - const char *const expected = + const char expected[] = "First line\n" "a second\n" "a third\n" @@ -104,7 +107,9 @@ test_s_print_msgs(void) { assert(strcmp(expected, str) == 0); - freeit((void *)&str); + free(str); + str = NULL; + test_ok(); } { @@ -116,8 +121,8 @@ test_s_print_msgs(void) { goto out; } - if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)) { - logerr("s_print_msgs()"); + if (msgs_print_ids(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)) { + logerr("msgs_print_ids()"); goto out; } @@ -133,50 +138,13 @@ test_s_print_msgs(void) { goto out; } - const char *const expected = + const char expected[] = "First line\n"; assert(strcmp(expected, str) == 0); - freeit((void *)&str); - - test_ok(); - } - - rc = 0; -out: - if (str != NULL) { - freeit((void *)&str); - } - if (file != NULL) { - if (fclose(file)) { - logerr("fclose(): %s", strerror(errno)); - rc = -1; - } - } - return rc; -} - -static int -test_s(void) { - int rc = -1; - - test_start("_()"); - FILE *file = NULL; - char *str = NULL; - - { - testing("empty string"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - goto out; - } - - // FIXME: implement correct test - - + free(str); + str = NULL; test_ok(); } @@ -184,7 +152,8 @@ test_s(void) { rc = 0; out: if (str != NULL) { - freeit((void *)&str); + free(str); + str = NULL; } if (file != NULL) { if (fclose(file)) { @@ -204,8 +173,8 @@ test_i18n_destroy(void) { { testing("simple call without init first"); - if (i18n_destroy()) { - logerr("i18n_destroy()"); + if (msgs_end()) { + logerr("msgs_end()"); goto out; } @@ -218,10 +187,10 @@ out: } static int -test_s_print_msg(void) { +test_msgs_print_id(void) { int rc = -1; - test_start("s_print_msg()"); + test_start("msgs_print_id()"); FILE *file = NULL; char *str = NULL; @@ -234,8 +203,8 @@ test_s_print_msg(void) { goto out; } - if (s_print_msg(TEST_MSGS, file, MSG_STANDALONE)) { - logerr("s_print_msg()"); + if (msgs_print_id(TEST_MSGS, file, MSG_STANDALONE)) { + logerr("msgs_print_id()"); goto out; } @@ -251,12 +220,13 @@ test_s_print_msg(void) { goto out; } - const char *const expected = + const char expected[] = "single line message\n"; assert(strcmp(expected, str) == 0); - freeit((void *)&str); + free(str); + str = NULL; test_ok(); } @@ -264,7 +234,8 @@ test_s_print_msg(void) { rc = 0; out: if (str != NULL) { - freeit((void *)&str); + free(str); + str = NULL; } if (file != NULL) { if (fclose(file)) { @@ -279,8 +250,8 @@ int main(void) { int rc = EXIT_FAILURE; - if (test_i18n_init_msgs()) { - logerr("test_i18n_init_msgs()"); + if (test_msgs_init()) { + logerr("test_msgs_init()"); goto out; } @@ -289,18 +260,13 @@ main(void) { goto out; } - if (test_s()) { - logerr("test_s()"); - goto out; - } - - if (test_s_print_msgs()) { - logerr("test_s_print_msgs()"); + if (test_msgs_print_ids()) { + logerr("test_msgs_print_ids()"); goto out; } - if (test_s_print_msg()) { - logerr("test_s_print_msg()"); + if (test_msgs_print_id()) { + logerr("test_msgs_print_id()"); goto out; } diff --git a/tests/random.c b/tests/random.c index 09ad1a7..19c23b0 100644 --- a/tests/random.c +++ b/tests/random.c @@ -1,22 +1,28 @@ #include "../src/random.c" + +#include <stdlib.h> + #include "../src/testing.h" + static int -test_urandom_bytes(void) { +test_random_bytes(void) { int rc = -1; - test_start("urandom_bytes()"); + test_start("random_bytes()"); { testing("we get to pick the size that comes out"); - const size_t LEN = 256; - uint8_t arr[256 /* LEN */] = { 0 }; + enum { + LEN = 256, + }; + u8 arr[LEN] = { 0 }; for (size_t n = 0; n < LEN; n++) { - if (urandom_bytes(n, &arr)) { - logerr("urandom_bytes()"); + if (random_bytes(n, &arr)) { + logerr("random_bytes()"); goto out; } for (size_t i = n; i < LEN; i++) { @@ -30,19 +36,21 @@ test_urandom_bytes(void) { { 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 }; + enum { + LEN = 64, + }; + u8 arr1[LEN] = { 0 }; + u8 arr2[LEN] = { 0 }; - if (urandom_bytes(LEN, &arr1)) { - logerr("urandom_bytes()"); + if (random_bytes(LEN, &arr1)) { + logerr("random_bytes()"); goto out; } const size_t attempts = 10; for (size_t n = 0; n < attempts; n++) { - if (urandom_bytes(LEN, &arr2)) { - logerr("urandom_bytes()"); + if (random_bytes(LEN, &arr2)) { + logerr("random_bytes()"); goto out; } assert(memcmp(arr1, arr2, LEN) != 0); @@ -56,50 +64,27 @@ out: return rc; } -static int -test_random_new(void) { - // FIXME: implement these - return 0; -} - -static int -test_random_free(void) { - // FIXME: implement these - return 0; -} - -static int -test_random_generate(void) { - // FIXME: implement these - return 0; -} - int main(void) { int rc = EXIT_FAILURE; - if (test_urandom_bytes()) { - logerr("test_urandom_bytes()"); - goto out; - } - - if (test_random_new()) { - logerr("test_random_new()"); + if (random_init()) { + logerr("random_init()"); goto out; } - if (test_random_free()) { - logerr("test_random_free()"); - goto out; - } - - if (test_random_generate()) { - logerr("test_random_generate()"); + if (test_random_bytes()) { + logerr("test_random_bytes()"); goto out; } rc = EXIT_SUCCESS; out: + if (random_end()) { + if (rc == EXIT_SUCCESS) { + rc = EXIT_FAILURE; + } + } return rc; } diff --git a/tests/set.c b/tests/set.c index eda31f0..2f0ffcc 100644 --- a/tests/set.c +++ b/tests/set.c @@ -9,13 +9,13 @@ test_HASH_OUTPUT_LENGTH(void) { test_start("HASH_OUTPUT_LENGTH"); { - testing("Enforce that SipHash's output matches endiannessbs's input"); + testing("SipHash's output matches endianness's input"); return; // FIXME printf("\n"); - printf("HASH_OUTPUT_LENGTH: %u\n", HASH_OUTPUT_LENGTH); - printf("ENDIANNESSBS_64BIT_LENGTH: %u\n", ENDIANNESSBS_64BIT_LENGTH); - assert(HASH_OUTPUT_LENGTH == ENDIANNESSBS_64BIT_LENGTH); + printf("HASH_OUTPUT_LENGTH: %u\n", HASH_OUTPUT_LENGTH); + printf("ENDIANNESS_64BIT_LENGTH: %u\n",ENDIANNESS_64BIT_LENGTH); + assert((int)HASH_OUTPUT_LENGTH == (int)ENDIANNESS_64BIT_LENGTH); test_ok(); } } diff --git a/tests/slurp.c b/tests/slurp.c index 6b6a02e..1ae2280 100644 --- a/tests/slurp.c +++ b/tests/slurp.c @@ -1,4 +1,4 @@ -#include "../src/config.h" +#include <s.h> #include <errno.h> #include <stdio.h> @@ -7,8 +7,6 @@ #include "slurp.h" -static const size_t -NULL_TERMINATOR = sizeof((char)'\0'); int slurp_for_tests(const char *const filename, char **out) { @@ -19,7 +17,8 @@ slurp_for_tests(const char *const filename, char **out) { file = fopen(filename, "r"); if (file == NULL) { - perror("fopen(filename, \"r\")"); + // Comment to prevent clobbering in test output + // perror("fopen(filename, \"r\")"); goto out; } diff --git a/tests/string.c b/tests/string.c index 923d086..7740b4e 100644 --- a/tests/string.c +++ b/tests/string.c @@ -14,7 +14,7 @@ test_string_new_with(void) { { testing("length value isn't checked"); - const char *const string = "abc"; + const char string[] = "abc"; if (string_new_with(string, 123U, &s)) { logerr("string_new_with()"); goto out; @@ -94,7 +94,7 @@ test_cstr(void) { test_start("cstr()"); - const char *const string = "some string"; + const char string[] = "some string"; const size_t length = strlen(string); if (string_new(string, &s)) { logerr("string_new()"); @@ -226,17 +226,17 @@ test_string_equals(void) { const struct String *const s1 = &(struct String) { .length = 1U, - .bytes = (const uint8_t *)"", + .bytes = (const u8 *)"", }; const struct String *const s2 = &(struct String) { .length = 1U, - .bytes = (const uint8_t *)"", + .bytes = (const u8 *)"", }; const struct String *const s3 = &(struct String) { .length = 2U, - .bytes = (const uint8_t *)" ", + .bytes = (const u8 *)" ", }; { @@ -325,7 +325,7 @@ test_string_append(void) { assert(string_equals(s, &(struct String) { .length = 6U, - .bytes = (const uint8_t *)"abcabc", + .bytes = (const u8 *)"abcabc", })); string_free(&s); diff --git a/tests/testing.c b/tests/testing.c index 14e545d..21e0264 100644 --- a/tests/testing.c +++ b/tests/testing.c @@ -1,6 +1,16 @@ #include "../src/testing.c" #include <assert.h> +#include <string.h> + +#include "../src/logerr.h" +#include "slurp.h" + + + +static const char +FNAME[] = __FILE__ ".txt"; + int @@ -8,10 +18,25 @@ main(void) { int rc = EXIT_FAILURE; test_start("testing.c"); + testing("output varying on $NO_COLOUR value"); + + FILE *file = NULL; + char *str = NULL; + + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + test_set_stream(file); + + test_start("testing.c"); const bool should_overwrite = true; + const char *const orig = getenv(ENVVAR_NAME); + if (unsetenv(ENVVAR_NAME)) { - perror("unsetenv(\"NO_COLOUR\")"); + logerr("unsetenv(\"NO_COLOUR\")"); goto out; } { @@ -21,7 +46,7 @@ main(void) { } if (setenv(ENVVAR_NAME, "", should_overwrite)) { - perror("setenv(\"NO_COLOUR\", \"\", 1)"); + logerr("setenv(\"NO_COLOUR\", \"\", 1)"); goto out; } { @@ -31,7 +56,7 @@ main(void) { } if (setenv(ENVVAR_NAME, "something", should_overwrite)) { - perror("setenv(\"NO_COLOUR\", \"something\", 1)"); + logerr("setenv(\"NO_COLOUR\", \"something\", 1)"); goto out; } { @@ -40,7 +65,58 @@ main(void) { test_ok(); } + test_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (orig == NULL) { + if (unsetenv(ENVVAR_NAME)) { + logerr("unsetenv(\"NO_COLOUR\")"); + goto out; + } + } else { + if (setenv(ENVVAR_NAME, orig, true)) { + logerr("setenv(\"NO_COLOUR\", orig, true)"); + goto out; + } + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "testing.c:\n" + "\033[0;33mtesting\033[0m: unset NO_COLOUR... " + "\033[0;32mOK\033[0m.\n" + "\033[0;33mtesting\033[0m: empty NO_COLOUR... " + "\033[0;32mOK\033[0m.\n" + "testing: defined NO_COLOUR... OK.\n" + ; + + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + rc = EXIT_SUCCESS; out: + if (str != NULL) { + free(str); + str = NULL; + } + if (file != NULL) { + if (fclose(file)) { + logerr("flose()"); + rc = EXIT_FAILURE; + } + } return rc; } diff --git a/tests/trace.c b/tests/trace.c index bffa6c0..aa10c81 100644 --- a/tests/trace.c +++ b/tests/trace.c @@ -1,27 +1,26 @@ #include "../src/trace.c" -#include <assert.h> -#include <errno.h> -#include <string.h> - #include "../src/testing.h" -#include "../src/util.h" #include "slurp.h" -static const char *const -FNAME = __FILE__ ".txt"; +static const char +FNAME[] = __FILE__ ".txt"; static int -test_vtrace(void) { +test_ftracef(void) { int rc = -1; - test_start("vtrace()"); + test_start("ftracef()"); + FILE *file = NULL; - char *str = NULL; + char *str = NULL; + + const enum TraceLevel orig = LEVEL; + trace_set_level(TraceLevel_INFO); { testing("empty varargs"); @@ -31,10 +30,9 @@ test_vtrace(void) { perror("fopen(FNAME, \"w\")"); goto out; } - set_output(file); #line 100 - vtrace(__FILE__, __func__, __LINE__, + ftracef(__FILE__, __func__, __LINE__, file, TraceLevel_INFO, ""); const int ret = fclose(file); @@ -49,15 +47,13 @@ test_vtrace(void) { goto out; } - const char *const expected = - "tests/trace.c:test_vtrace:100: \n"; - printf("\n"); - printf("expected: >>>%s<<<\n", expected); - printf("str: >>>%s<<<\n", str); - // FIXME + const char expected[] = + "tests/trace.c:test_ftracef:100: \n"; + assert(strcmp(expected, str) == 0); - freeit((void *)&str); + free(str); + str = NULL; test_ok(); } @@ -69,10 +65,10 @@ test_vtrace(void) { perror("fopen(FNAME, \"w\")"); goto out; } - set_output(file); + trace_set_stream(file); #line 200 - vtrace(__FILE__, __func__, __LINE__, + ftracef(__FILE__, __func__, __LINE__, file, TraceLevel_INFO, "int (%d), string (%s) and char (%c)", 123, @@ -91,16 +87,14 @@ test_vtrace(void) { goto out; } - const char *const expected = - "tests/trace.c:test_vtrace:200: " + const char expected[] = + "tests/trace.c:test_ftracef:200: " "int (123), string (another-str) and char (z)\n"; - printf("\n"); - printf("expected: >>>%s<<<\n", expected); - printf("str: >>>%s<<<\n", str); - // FIXME - // assert(strcmp(expected, str) == 0); - freeit((void *)&str); + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; test_ok(); } @@ -112,10 +106,10 @@ test_vtrace(void) { perror("fopen(FNAME, \"w\")"); goto out; } - set_output(file); + trace_set_stream(file); - vtrace(__FILE__, __func__, __LINE__, - TraceLevel_INFO, "ignored"); + ftracef(__FILE__, __func__, __LINE__, file, + TraceLevel_DEBUG, "ignored"); const int ret = fclose(file); file = NULL; @@ -129,18 +123,20 @@ test_vtrace(void) { goto out; } - // FIXME - // assert(strcmp("", str) == 0); + assert(strcmp("", str) == 0); - freeit((void *)&str); + free(str); + str = NULL; test_ok(); } rc = 0; out: + trace_set_level(orig); if (str != NULL) { - freeit((void *)&str); + free(str); + str = NULL; } if (file != NULL) { if (fclose(file)) { @@ -156,86 +152,31 @@ test_trace(void) { int rc = -1; test_start("trace()"); - FILE *file = NULL; - char *str = NULL; + + FILE *file = fopen("/dev/null", "w"); + if (file == NULL) { + logerr("fopen(\"/dev/null\", \"w\")"); + goto out; + } + trace_set_stream(file); { testing("can be called with an empty string"); - file = fopen(FNAME, "w"); - if (file == NULL) { - perror("fopen(FNAME, \"w\")"); - goto out; - } - set_output(file); - trace(TraceLevel_INFO, ""); - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - goto out; - } - - // FIXME - /* - const char *const expected = - "tests/trace.c:test_vtrace:200: " - "int (123), string (another-str) and char (z)\n"; - */ - // assert(strcmp(expected, str) == 0); - - freeit((void *)&str); - test_ok(); } { testing("can be called with formatting arguments"); - file = fopen(FNAME, "w"); - if (file == NULL) { - perror("fopen(FNAME, \"w\")"); - goto out; - } - set_output(file); - - // FIXME - // trace(TraceLevel_INFO, "int: %d\tstr: %s", 123, "an example string"); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - goto out; - } - - /* - const char *const expected = - ""; - assert(strcmp(expected, str) == 0); - */ - - freeit((void *)&str); + trace(TraceLevel_INFO, "int: %d\tstr: %s", 123, "example"); test_ok(); } rc = 0; out: - if (str != NULL) { - freeit((void *)&str); - } if (file != NULL) { if (fclose(file)) { perror("fclose(file)"); @@ -250,11 +191,10 @@ int main(void) { int rc = EXIT_FAILURE; - TRACE_LEVEL = TraceLevel_INFO; - TRACE_LEVEL = TraceLevel_DEBUG; + LEVEL = TraceLevel_DEBUG; - if (test_vtrace()) { - perror("test_vtrace()"); + if (test_ftracef()) { + perror("test_ftracef()"); goto out; } diff --git a/tests/util.c b/tests/util.c index a720123..260922a 100644 --- a/tests/util.c +++ b/tests/util.c @@ -6,76 +6,34 @@ #include "slurp.h" -static void -test_EXIT_USAGE(void) { - test_start("EXIT_USAGE"); - - { - testing("it is different that EXIT_SUCCESS and EXIT_FAILURE"); - assert(EXIT_USAGE != EXIT_SUCCESS); - assert(EXIT_USAGE != EXIT_FAILURE); - test_ok(); - } -} static int -test_freeit(void) { +test_slurp(void) { int rc = -1; - test_start("freeit()"); - - const char *ptr = NULL; - - { - testing("ptr is NULL afterwards"); - - ptr = malloc(1234U); - if (ptr == NULL) { - logerr("malloc(): %s", strerror(errno)); - goto out; - } - - assert(ptr != NULL); - freeit((void *)&ptr); - assert(ptr == NULL); - - test_ok(); - } - { - testing("tolerate be given a NULL pointer"); - - void *p = NULL; - freeit(NULL); - freeit(p); - freeit((void *)&p); + test_start("slurp()"); - test_ok(); - } + char *expected = NULL; + char *given = NULL; + size_t givenlen = 0U; - rc = 0; -out: - if (ptr != NULL) { - freeit((void *)&ptr); + FILE *file = fopen("/dev/null", "w"); + if (file == NULL) { + logerr("fopen(\"/dev/null\", \"w\")"); + goto out; } - return rc; -} - -static int -test_slurp(void) { - int rc = -1; - - char *given = NULL; - char *expected = NULL; + logerr_set_stream(file); { testing("non-existent file"); - const char *const filename = __FILE__ ".non-existant"; - const int ret_given = slurp(filename, &given); + const char filename[] = __FILE__ ".non-existant"; const int ret_expected = slurp_for_tests(filename, &expected); + const int ret_given = slurp(filename, &givenlen, &given); - assert(given == NULL); assert(expected == NULL); + assert(given == NULL); + assert(givenlen == 0U); assert(!!ret_given == !!ret_expected); test_ok(); @@ -83,7 +41,7 @@ test_slurp(void) { { testing("slurp() == slurp_for_tests()"); - if (slurp(__FILE__, &given)) { + if (slurp(__FILE__, &givenlen, &given)) { logerr("slurp()"); goto out; } @@ -93,23 +51,35 @@ test_slurp(void) { goto out; } - assert(given != NULL); assert(expected != NULL); + assert(given != NULL); + assert(givenlen == strlen(given)); assert(strcmp(given, expected) == 0); - freeit((void *)&given); - freeit((void *)&expected); + free(expected); + free(given); + expected = NULL; + given = NULL; test_ok(); } rc = 0; out: + logerr_set_stream(NULL); if (expected != NULL) { - freeit((void *)expected); + free(expected); + expected = NULL; } if (given != NULL) { - freeit((void *)&given); + free(given); + given = NULL; + } + if (file != NULL) { + if (fclose(file)) { + logerr("fclose(file)"); + rc = -1; + } } return rc; } @@ -119,18 +89,11 @@ int main(void) { int rc = EXIT_FAILURE; - test_EXIT_USAGE(); - if (test_slurp()) { logerr("test_slurp()"); goto out; } - if (test_freeit()) { - logerr("test_freeit()"); - goto out; - } - rc = EXIT_SUCCESS; out: return rc; diff --git a/tests/vector.c b/tests/vector.c index 1ff270a..443dc81 100644 --- a/tests/vector.c +++ b/tests/vector.c @@ -9,6 +9,12 @@ #include <string.h> #include "../src/testing.h" +#include "slurp.h" + + + +static const char +FNAME[] = __FILE__ ".txt"; @@ -16,9 +22,12 @@ static int test_vector_new_with(void) { int rc = -1; + test_start("vector_new_with()"); + const struct Vector *v = NULL; + FILE *file = NULL; + char *str = NULL; - test_start("vector_new_with()"); { testing("allocating struct Vector with small capacity"); @@ -53,6 +62,13 @@ test_vector_new_with(void) { const size_t max_capacity = 2U; + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + logerr_set_stream(file); + assert(v == NULL); assert(vector_new_with( max_capacity + 1, @@ -63,16 +79,50 @@ test_vector_new_with(void) { ) == EOVERFLOW); assert(v == NULL); + logerr_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "tests/../src/vector.c:vector_new_with:56: " + "Already at max capacity (2): " + "Value too large for defined data type\n\n" + ; + + assert(strcmp(str, expected) == 0); + + free(str); + str = NULL; + test_ok(); } rc = 0; out: + logerr_set_stream(NULL); + if (str != NULL) { + free(str); + str = NULL; + } + if (file != NULL) { + if (fclose(file)) { + logerr("fclose(file)"); + rc = -1; + } + } vector_free(&v); return rc; } - static int test_vector_new(void) { int rc = -1; @@ -187,7 +237,6 @@ test_vector_capacity(void) { assert(vector_capacity(v) == VECTOR_DEFAULT_CAPACITY); assert(v->capacity == VECTOR_DEFAULT_CAPACITY); - printf("\n"); const long value = 123; for (size_t i = 0U; i < VECTOR_DEFAULT_CAPACITY; i++) { assert(vector_push_back(v, &value) == 0); @@ -344,14 +393,18 @@ static int test_vector_nth(void) { int rc = -1; + test_start("vector_nth()"); + const struct Vector *v = NULL; + FILE *file = NULL; + char *str = NULL; + const int first = 123; const int second = 321; const int third = 555; const int values[] = { first, second, third }; - const size_t values_len = sizeof(values) / sizeof(values[0]); + const size_t values_len = nelem(values); - test_start("vector_nth()"); { testing("nth with growth"); @@ -390,10 +443,40 @@ test_vector_nth(void) { assert(vector_push_back(v, &values[i]) == 0); } + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + logerr_set_stream(file); + int nth = 222; assert(vector_nth(v, 4U, (void *)&nth) != 0); assert(nth == 222); + logerr_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "tests/../src/vector.c:vector_nth:165: " + "idx (4) is beyond bounds (3)\n\n" + ; + + assert(strcmp(str, expected) == 0); + + free(str); + str = NULL; + vector_free(&v); test_ok(); @@ -401,6 +484,17 @@ test_vector_nth(void) { rc = 0; out: + logerr_set_stream(NULL); + if (str != NULL) { + free(str); + str = NULL; + } + if (file != NULL) { + if (fclose(file)) { + logerr("fclose(file)"); + rc = -1; + } + } vector_free(&v); return rc; } @@ -411,6 +505,9 @@ test_vector_assign(void) { const struct Vector *v = NULL; + FILE *file = NULL; + char *str = NULL; + test_start("vector_assign()"); { testing("replace existing value"); @@ -420,7 +517,7 @@ test_vector_assign(void) { const int third = 555; const int fourth = 999; const int values[] = { first, second, third }; - const size_t values_len = sizeof(values) / sizeof(values[0]); + const size_t values_len = nelem(values); if (vector_new(sizeof(int), &v)) { logerr("vector_new()"); @@ -432,9 +529,8 @@ test_vector_assign(void) { assert(vector_push_back(v, &values[i]) == 0); } - int value; + int value = 0; assert(vector_assign(v, 1U, &fourth) == 0); - rc = 0; goto out; // FIXME assert(vector_nth(v, 1U, (void *)&value) == 0); assert(value == fourth); @@ -445,11 +541,12 @@ test_vector_assign(void) { { testing("assign new value"); + const int zero = 0; const int first = 121; const int second = 122; const int third = 123; const int values[] = { first, second, third }; - const size_t values_len = sizeof(values) / sizeof(values[0]); + const size_t values_len = nelem(values); if (vector_new(sizeof(int), &v)) { logerr("vector_new()"); @@ -458,10 +555,14 @@ test_vector_assign(void) { assert(v->capacity >= values_len); for (unsigned int i = 0; i < values_len; i++) { + assert(vector_push_back(v, &zero) == 0); + } + + for (unsigned int i = 0; i < values_len; i++) { assert(vector_assign(v, i, &values[i]) == 0); } - int value; + int value = 0; assert(vector_nth(v, 0U, (void *)&value) == 0); assert(value == 121); assert(vector_nth(v, 1U, (void *)&value) == 0); @@ -476,13 +577,41 @@ test_vector_assign(void) { { testing("assign out of bounds errors"); + const int n = 222; + if (vector_new(sizeof(short), &v)) { logerr("vector_new()"); goto out; } - const int new_value = 222; - assert(vector_assign(v, VECTOR_DEFAULT_CAPACITY, &new_value) != 0); + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + logerr_set_stream(file); + + assert(vector_assign(v, VECTOR_DEFAULT_CAPACITY, &n) != 0); + + logerr_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "tests/../src/vector.c:vector_assign:185: " + "idx (8) is beyond bounds (0)\n\n" + ; + + assert(strcmp(str, expected) == 0); vector_free(&v); @@ -491,6 +620,17 @@ test_vector_assign(void) { rc = 0; out: + logerr_set_stream(NULL); + if (str != NULL) { + free(str); + str = NULL; + } + if (file != NULL) { + if (fclose(file)) { + logerr("fclose(file)"); + rc = -1; + } + } vector_free(&v); return rc; } @@ -568,6 +708,8 @@ test_vector_pop_back(void) { int rc = -1; const struct Vector *v = NULL; + FILE *file = NULL; + char *str = NULL; test_start("vector_pop_back()"); { @@ -578,10 +720,40 @@ test_vector_pop_back(void) { goto out; } + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + logerr_set_stream(file); + int val = 222; assert(vector_pop_back(v, (void *)&val) != 0); assert(val == 222); + logerr_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "tests/../src/vector.c:vector_pop_back:256: " + "pop on an empty vector\n\n" + ; + + assert(strcmp(str, expected) == 0); + + free(str); + str = NULL; + vector_free(&v); test_ok(); @@ -620,8 +792,38 @@ test_vector_pop_back(void) { assert(val == 1234U); } + file = fopen(FNAME, "w"); + if (file == NULL) { + logerr("fopen(FNAME, \"w\")"); + goto out; + } + logerr_set_stream(file); + assert(vector_pop_back(v, (void *)&val) != 0); + logerr_set_stream(NULL); + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file)"); + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests()"); + goto out; + } + + const char expected[] = + "tests/../src/vector.c:vector_pop_back:256: " + "pop on an empty vector\n\n" + ; + + assert(strcmp(str, expected) == 0); + + free(str); + str = NULL; + vector_free(&v); test_ok(); @@ -629,6 +831,17 @@ test_vector_pop_back(void) { rc = 0; out: + logerr_set_stream(NULL); + if (str != NULL) { + free(str); + str = NULL; + } + if (file != NULL) { + if (fclose(file)) { + logerr("fclose(file)"); + rc = -1; + } + } vector_free(&v); return rc; } |