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 /tests | |
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
Diffstat (limited to 'tests')
-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 |
16 files changed, 558 insertions, 386 deletions
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; } |