summaryrefslogtreecommitdiff
path: root/tests/trace.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/trace.c')
-rw-r--r--tests/trace.c269
1 files changed, 269 insertions, 0 deletions
diff --git a/tests/trace.c b/tests/trace.c
new file mode 100644
index 0000000..bffa6c0
--- /dev/null
+++ b/tests/trace.c
@@ -0,0 +1,269 @@
+#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 int
+test_vtrace(void) {
+ int rc = -1;
+
+ test_start("vtrace()");
+ FILE *file = NULL;
+ char *str = NULL;
+
+ {
+ testing("empty varargs");
+
+ file = fopen(FNAME, "w");
+ if (file == NULL) {
+ perror("fopen(FNAME, \"w\")");
+ goto out;
+ }
+ set_output(file);
+
+ #line 100
+ vtrace(__FILE__, __func__, __LINE__,
+ 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;
+ }
+
+ const char *const expected =
+ "tests/trace.c:test_vtrace:100: \n";
+ printf("\n");
+ printf("expected: >>>%s<<<\n", expected);
+ printf("str: >>>%s<<<\n", str);
+ // FIXME
+ assert(strcmp(expected, str) == 0);
+
+ freeit((void *)&str);
+
+ test_ok();
+ }
+ {
+ testing("multiple format strings");
+
+ file = fopen(FNAME, "w");
+ if (file == NULL) {
+ perror("fopen(FNAME, \"w\")");
+ goto out;
+ }
+ set_output(file);
+
+ #line 200
+ vtrace(__FILE__, __func__, __LINE__,
+ TraceLevel_INFO,
+ "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 *const expected =
+ "tests/trace.c:test_vtrace: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);
+
+ test_ok();
+ }
+ {
+ testing("insufficient trace level");
+
+ file = fopen(FNAME, "w");
+ if (file == NULL) {
+ perror("fopen(FNAME, \"w\")");
+ goto out;
+ }
+ set_output(file);
+
+ vtrace(__FILE__, __func__, __LINE__,
+ TraceLevel_INFO, "ignored");
+
+ 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
+ // assert(strcmp("", str) == 0);
+
+ freeit((void *)&str);
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (str != NULL) {
+ freeit((void *)&str);
+ }
+ if (file != NULL) {
+ if (fclose(file)) {
+ perror("fclose(file)");
+ rc = -1;
+ }
+ }
+ return rc;
+}
+
+static int
+test_trace(void) {
+ int rc = -1;
+
+ test_start("trace()");
+ FILE *file = NULL;
+ char *str = NULL;
+
+ {
+ 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);
+
+ test_ok();
+ }
+
+ rc = 0;
+out:
+ if (str != NULL) {
+ freeit((void *)&str);
+ }
+ if (file != NULL) {
+ if (fclose(file)) {
+ perror("fclose(file)");
+ rc = -1;
+ }
+ }
+ return rc;
+}
+
+
+int
+main(void) {
+ int rc = EXIT_FAILURE;
+
+ TRACE_LEVEL = TraceLevel_INFO;
+ TRACE_LEVEL = TraceLevel_DEBUG;
+
+ if (test_vtrace()) {
+ perror("test_vtrace()");
+ goto out;
+ }
+
+ if (test_trace()) {
+ perror("test_trace()");
+ goto out;
+ }
+
+ rc = EXIT_SUCCESS;
+out:
+ return rc;
+}