#include "../src/logerr.c" #include #include #include #include #include "../src/testing.h" #include "slurp.h" static const char FNAME[] = __FILE__ ".txt"; static int test_flogerrf(void) { int rc = -1; test_start("flogerrf()"); FILE *file = NULL; char *str = NULL; { testing("empty varargs"); file = fopen(FNAME, "w"); if (file == NULL) { perror("fopen(FNAME, \"w\")"); goto out; } #line 100 flogerrf(__FILE__, __func__, __LINE__, file, ""); 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 expected[] = "tests/logerr.c:test_flogerrf:100: \n"; assert(strcmp(expected, str) == 0); free(str); str = NULL; test_ok(); } { testing("a newline only"); file = fopen(FNAME, "w"); if (file == NULL) { perror("fopen(FNAME, \"w\")"); goto out; } #line 200 flogerrf(__FILE__, __func__, __LINE__, file, "\n"); 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 expected[] = "tests/logerr.c:test_flogerrf:200: \n\n"; assert(strcmp(expected, str) == 0); free(str); str = NULL; test_ok(); } { testing("static format string"); file = fopen(FNAME, "w"); if (file == NULL) { perror("fopen(FNAME, \"w\")"); goto out; } #line 300 flogerrf(__FILE__, __func__, __LINE__, file, "some static 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 expected[] = "tests/logerr.c:test_flogerrf: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 == NULL) { perror("fopen(FNAME, \"w\")"); goto out; } #line 400 flogerrf(__FILE__, __func__, __LINE__, file, "fn(%s)", "an-arg"); 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 expected[] = "tests/logerr.c:test_flogerrf: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 == NULL) { perror("fopen(FNAME, \"w\")"); goto out; } #line 500 flogerrf(__FILE__, __func__, __LINE__, file, "int (%d), string (%s) and char (%c)", 123, "another-str", 'z'); 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 expected[] = "tests/logerr.c:test_flogerrf:500: " "int (123), string (another-str) and char (z)\n"; assert(strcmp(expected, str) == 0); free(str); str = NULL; test_ok(); } rc = 0; out: if (str != NULL) { free(str); str = NULL; } if (file != NULL) { if (fclose(file)) { perror("fclose(file)"); rc = -1; } } return rc; } 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"); logerr(""); test_ok(); } { testing("can be called with a static string"); logerr("some err"); test_ok(); } { testing("can be called with a formatted string"); logerr("some err: %s", strerror(errno)); test_ok(); } { testing("can be called with formatting arguments"); logerr("int: %d\tstr: %s", 123, "an example string"); test_ok(); } rc = 0; out: logerr_set_stream(NULL); if (file != NULL) { if (fclose(file)) { perror("fclose(file)"); rc = -1; } } return rc; } int main(void) { int rc = EXIT_FAILURE; if (test_flogerrf()) { perror("test_flogerrf()"); goto out; } if (test_logerr()) { perror("test_logerr()"); goto out; } rc = EXIT_SUCCESS; out: return rc; }