diff options
author | EuAndreh <eu@euandre.org> | 2024-04-05 16:07:39 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-04-05 17:08:08 -0300 |
commit | ea60b1b08015060a08b1ead38e4e81c44a88017f (patch) | |
tree | 9be302cafc856410383e1b1ab315f8b1a371153d /src | |
parent | git mv meta.capim meta.weave (diff) | |
download | pindaiba-ea60b1b08015060a08b1ead38e4e81c44a88017f.tar.gz pindaiba-ea60b1b08015060a08b1ead38e4e81c44a88017f.tar.xz |
Move unit tests out of src/*.c into tests/
Diffstat (limited to 'src')
-rw-r--r-- | src/catalog.c | 330 | ||||
-rw-r--r-- | src/i18n.c | 26 | ||||
-rw-r--r-- | src/lib.c | 9 | ||||
-rw-r--r-- | src/logerr.c | 284 | ||||
-rw-r--r-- | src/random.c | 75 | ||||
-rw-r--r-- | src/testing.c | 43 |
6 files changed, 0 insertions, 767 deletions
diff --git a/src/catalog.c b/src/catalog.c index d543864..68ec3c3 100644 --- a/src/catalog.c +++ b/src/catalog.c @@ -157,333 +157,3 @@ dump_translatable_strings(const char *const MSGS[]) { out: return rc; } - - - -#ifdef TEST -#include "testing.h" -#include "../tests/slurp.h" - - -static const char *const -FNAME = __FILE__ ".txt"; - -enum TEST_MSGCATALOG_ID { - MSG_X_FIRST = 1, - MSG_X_1, - MSG_X_2, - MSG_X_LAST, - MSG_STANDALONE, -}; - -static const char *const -TEST_MSGS[] = { - "", - [MSG_X_FIRST]="First line\n", - [MSG_X_1]="a second\n", - [MSG_X_2]="a third\n", - [MSG_X_LAST]="and the last one\n", - [MSG_STANDALONE]="single line message\n", - NULL -}; - -static int -test_i18n_init(void) { - int rc = 0; - - test_start("i18n_init()"); - - { - testing("simple call without touching the environment"); - - const int should_overwrite = 1; - if (setenv(NLSPATH_KEY, "src/%N.en.cat", should_overwrite)) { - logerr("setenv(\"%s\", \"src/%%N.en.cat\", 1): %s\n", - NLSPATH_KEY, strerror(errno)); - rc = -1; - goto out; - } - - if (i18n_init()) { - logerr("i18n_init()\n"); - rc = -1; - goto out; - } - - test_ok(); - } - -out: - if (i18n_destroy()) { - logerr("i18n_destroy()\n"); - rc = -1; - } - return rc; -} - -static int -test_s_print_msgs(void) { - int rc = 0; - - test_start("s_print_msgs()"); - FILE *file = NULL; - char *str = NULL; - - { - testing("message in range"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)) { - logerr("print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)\n"); - rc = -1; - goto out; - } - - const int ret = fclose(file); - file = NULL; - if (ret) { - logerr("fclose(file): %s\n", strerror(errno)); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - logerr("slurp_for_tests(FNAME, &str)\n"); - rc = -1; - goto out; - } - - const char *const expected = - "First line\n" - "a second\n" - "a third\n" - "and the last one\n" - ; - - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - { - testing("range begin and end is the same"); - - file = fopen(FNAME, "w"); - if (!file) { - logerr("fopen(FNAME, \"w\"): %s\n", strerror(errno)); - rc = -1; - goto out; - } - - if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)) { - logerr("s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)\n"); - rc = -1; - goto out; - } - - const int ret = fclose(file); - file = NULL; - if (ret) { - logerr("fclose(file): %s\n", strerror(errno)); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - logerr("slurp_for_tests(FNAME, &str)\n"); - rc = -1; - goto out; - } - - const char *const expected = - "First line\n"; - - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - -out: - if (str) { - free(str); - } - if (file) { - if (fclose(file)) { - logerr("fclose(file): %s\n", strerror(errno)); - rc = -1; - } - } - return rc; -} - -static int -test_s(void) { - int rc = 0; - - test_start("_()"); - FILE *file = NULL; - char *str = NULL; - - { - testing("empty string"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - // FIXME: implement correct test - - - - test_ok(); - } - -out: - if (str) { - free(str); - } - if (file) { - if (fclose(file)) { - logerr("fclose(file): %s\n", strerror(errno)); - rc = -1; - } - } - return rc; -} - -static int -test_i18n_destroy(void) { - int rc = 0; - - test_start("i18n_destroy()"); - - { - testing("simple call without init first"); - - if (i18n_destroy()) { - logerr("i18n_destroy()\n"); - rc = -1; - goto out; - } - - test_ok(); - } - -out: - return rc; -} - -static int -test_s_print_msg(void) { - int rc = 0; - - test_start("s_print_msg()"); - FILE *file = NULL; - char *str = NULL; - - { - testing("simple individual message"); - - file = fopen(FNAME, "w"); - if (!file) { - logerr("fopen(FNAME, \"w\"): %s\n"); - rc = -1; - goto out; - } - - if (s_print_msg(TEST_MSGS, file, MSG_STANDALONE)) { - logerr("s_print_msg(TEST_MSGS, file, MSG_STANDALONE)\n"); - rc = -1; - goto out; - } - - const int ret = fclose(file); - file = NULL; - if (ret) { - logerr("fopen(file): %s\n", strerror(errno)); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - logerr("slurp_for_tests(FNAME, &str)\n"); - rc = -1; - goto out; - } - - const char *const expected = - "single line message\n"; - - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - -out: - if (str) { - free(str); - } - if (file) { - if (fclose(file)) { - logerr("fclose(file): %s\n", strerror(errno)); - rc = -1; - } - } - return rc; -} - -int -main(void) { - int rc = 0; - - if (test_i18n_init()) { - logerr("test_i18n_init()\n"); - rc = -1; - goto out; - } - - if (test_i18n_destroy()) { - logerr("test_i18n_destroy()\n"); - rc = -1; - goto out; - } - - if (test_s()) { - logerr("test_s()\n"); - rc = -1; - goto out; - } - - if (test_s_print_msgs()) { - logerr("test_s_print_msgs()\n"); - rc = -1; - goto out; - } - - if (test_s_print_msg()) { - logerr("test_s_print_msg()\n"); - rc = -1; - goto out; - } - -out: - return !!rc; -} -#endif @@ -47,29 +47,3 @@ MSGS[] = { [MSG_ERR_VECTOR_OUT_OF_BOUNDS]= "idx (%ld) is beyond bounds (%ld)\n", NULL }; - - - -#ifdef TEST -#include <stdlib.h> - -#include "logerr.h" -#include "catalog.h" - - -int -main(void) { - int rc = 0; - - if (getenv("DUMP_TRANSLATABLE_STRINGS")) { - if (dump_translatable_strings(MSGS)) { - logerr("dump_translatable_strings(MSGS)\n"); - rc = -1; - goto out; - } - } - -out: - return !!rc; -} -#endif @@ -9,12 +9,3 @@ re_main(int argc, char **argv) { } return rc; } - - -#ifdef TEST -int -main(void) { - int rc = 0; - return rc; -} -#endif diff --git a/src/logerr.c b/src/logerr.c index ab13e78..c00a4c1 100644 --- a/src/logerr.c +++ b/src/logerr.c @@ -28,287 +28,3 @@ vlogerr( return; } - - -#ifdef TEST -#include <assert.h> -#include <errno.h> -#include <string.h> - -#include "testing.h" -#include "../tests/slurp.h" - - -static const char *const -FNAME = __FILE__ ".txt"; - -static int -test_vlogerr(void) { - int rc = 0; - - test_start("vlogerr()"); - FILE *file = NULL; - char *str = NULL; - - { - testing("empty varargs"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - #line 100 - vlogerr(__FILE__, __func__, __LINE__, file, - ""); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - rc = -1; - goto out; - } - - const char *const expected = - "src/logerr.c:test_vlogerr:100: "; - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - { - testing("a newline only"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - #line 200 - vlogerr(__FILE__, __func__, __LINE__, file, - "\n"); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - rc = -1; - goto out; - } - - const char *const expected = - "src/logerr.c:test_vlogerr:200: \n"; - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - { - testing("static format string"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - #line 300 - vlogerr(__FILE__, __func__, __LINE__, file, - "some static string\n"); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - rc = -1; - goto out; - } - - const char *const expected = - "src/logerr.c:test_vlogerr:300: some static string\n"; - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - { - testing("single arg format string"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - #line 400 - vlogerr(__FILE__, __func__, __LINE__, file, - "fn(%s)\n", "an-arg"); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - rc = -1; - goto out; - } - - const char *const expected = - "src/logerr.c:test_vlogerr:400: fn(an-arg)\n"; - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - { - testing("multiple format strings"); - - file = fopen(FNAME, "w"); - if (!file) { - perror("fopen(FNAME, \"w\")"); - rc = -1; - goto out; - } - - #line 500 - vlogerr(__FILE__, __func__, __LINE__, file, - "int (%d), string (%s) and char (%c)\n", - 123, - "another-str", - 'z'); - - const int ret = fclose(file); - file = NULL; - if (ret) { - perror("fclose(file)"); - rc = -1; - goto out; - } - - if (slurp_for_tests(FNAME, &str)) { - perror("slurp_for_tests(FNAME, &str)"); - rc = -1; - goto out; - } - - const char *const expected = - "src/logerr.c:test_vlogerr:500: " - "int (123), string (another-str) and char (z)\n"; - assert(strcmp(expected, str) == 0); - - free(str); - str = NULL; - - test_ok(); - } - -out: - if (str) { - free(str); - } - if (file) { - if (fclose(file)) { - perror("fclose(file)"); - rc = -1; - } - } - return rc; -} - -static int -test_logerr(void) { - int rc = 0; - - test_start("logerr()"); - - { - testing("can be called with an empty string"); - - logerr(""); - - test_ok(); - } - { - testing("can be called with a static string"); - - logerr("some err\n"); - - test_ok(); - } - { - testing("can be called with a formatted string"); - - logerr("some err: %s\n", strerror(errno)); - - test_ok(); - } - { - testing("can be called with formatting arguments"); - - logerr("int: %d\nstr: %s\n", 123, "an example string"); - - test_ok(); - } - - return rc; -} - - -int -main(void) { - int rc = 0; - - if (test_vlogerr()) { - perror("test_vlogerr()"); - rc = -1; - goto out; - } - - if (test_logerr()) { - perror("test_logerr()"); - rc = -1; - goto out; - } - -out: - return !!rc; -} -#endif diff --git a/src/random.c b/src/random.c index c6da688..a35eafd 100644 --- a/src/random.c +++ b/src/random.c @@ -41,78 +41,3 @@ out: } return rc; } - - -#ifdef TEST -#include "testing.h" - -static int -test_urandom_bytes(void) { - int rc = 0; - - test_start("urandom_bytes()"); - - { - testing("we get to pick the size that comes out"); - - const size_t LEN = 256; - uint8_t arr[256 /* LEN */] = { 0 }; - - for (size_t n = 0; n < LEN; n++) { - if (urandom_bytes(n, &arr)) { - logerr("urandom_bytes(n, &arr);\n"); - rc = -1; - goto out; - } - for (size_t i = n; i < LEN; i++) { - assert(arr[i] == 0); - } - } - - test_ok(); - } - - { - testing("we always get a new value as a result"); - - const size_t LEN = 64; - uint8_t arr1[64 /* LEN */] = { 0 }; - uint8_t arr2[64 /* LEN */] = { 0 }; - - if (urandom_bytes(LEN, &arr1)) { - logerr("urandom_bytes(LEN, &arr1);\n"); - rc = -1; - goto out; - } - - const size_t attempts = 10; - for (size_t n = 0; n < attempts; n++) { - if (urandom_bytes(LEN, &arr2)) { - logerr("urandom_bytes(LEN, &arr2);\n"); - rc = -1; - goto out; - } - assert(memcmp(arr1, arr2, LEN) != 0); - } - - test_ok(); - } - -out: - return rc; -} - -int -main(void) { - int rc = 0; - - if (test_urandom_bytes()) { - logerr("test_urandom_bytes();\n"); - rc = -1; - goto out; - } - -out: - return !!rc; -} -#endif diff --git a/src/testing.c b/src/testing.c index 028e48f..e8c0fa6 100644 --- a/src/testing.c +++ b/src/testing.c @@ -69,46 +69,3 @@ test_ok(void) { return; } - -#ifdef TEST -int -main(void) { - int rc = 0; - - test_start("testing.c"); - const int should_overwrite = 1; - - if (unsetenv(ENVVAR_NAME)) { - perror("unsetenv(\"NO_COLOR\")"); - rc = -1; - goto out; - } - { - testing("unset NO_COLOR"); - test_ok(); - } - - if (setenv(ENVVAR_NAME, "", should_overwrite)) { - perror("setenv(\"NO_COLOR\", \"\", 1)"); - rc = -1; - goto out; - } - { - testing("empty NO_COLOR"); - test_ok(); - } - - if (setenv(ENVVAR_NAME, "something", should_overwrite)) { - perror("setenv(\"NO_COLOR\", \"something\", 1)"); - rc = -1; - goto out; - } - { - testing("defined NO_COLOR"); - test_ok(); - } - -out: - return !!rc; -} -#endif |