summaryrefslogtreecommitdiff
path: root/tests/trace.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/trace.c')
-rw-r--r--tests/trace.c146
1 files changed, 43 insertions, 103 deletions
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;
}