1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/* libmisc/tests/test_log.c - Tests for <libmisc/log.h>
*
* Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <stdarg.h> /* for va_list */
#include <stdio.h> /* for vsnprintf() */
#include <stdlib.h> /* for realloc(), free() */
#include <string.h> /* for strlen(), strcmp() */
#define LOG_NAME FROBNICATE
#include <libmisc/log.h>
#include <libmisc/_intercept.h>
#include "test.h"
/* Intercept output ***********************************************************/
static struct fmt_buf log_output = {};
size_t __lm_printf(const char *format, ...) {
restart:
va_list va;
va_start(va, format);
size_t ret = (size_t) vsnprintf(log_output.dat, log_output.cap, format, va);
va_end(va);
if (ret > log_output.cap) {
log_output.cap = ret+8;
log_output.dat = realloc(log_output.dat, log_output.cap);
memset(log_output.dat, 0, log_output.cap);
goto restart;
}
log_output.len = ret;
return ret;
}
void __lm_putchar(unsigned char c) {
if (log_output.len+1 >= log_output.cap) {
log_output.cap += 16;
log_output.dat = realloc(log_output.dat, log_output.cap);
memset(log_output.dat + log_output.len, 0, log_output.cap - log_output.len);
}
((uint8_t *)log_output.dat)[log_output.len++] = (uint8_t)c;
}
static void log_output_clear(void) {
if (log_output.dat)
memset(log_output.dat, 0, log_output.cap);
log_output.len = 0;
}
/* Actual tests ***************************************************************/
#define should_print(_exp, cmd) do { \
char *exp = _exp; \
test_assert(log_output.len == 0); \
cmd; \
if (!exp) \
test_assert(log_output.len == 0); \
else { \
test_assert(log_output.dat); \
test_assert(strlen(log_output.dat) == log_output.len); \
if (strcmp(log_output.dat, exp)) { \
printf("exp = \"%s\"\n" \
"act = \"%s\"\n", \
exp, (char *)log_output.dat); \
test_assert(0); \
} \
} \
log_output_clear(); \
} while (0)
int main() {
/* printf */
should_print("error: FROBNICATE: val=42\n",
log_errorf("val=%d", 42));
should_print("info : FROBNICATE: val=0\n",
log_infof("val=%d", 0));
#ifndef NDEBUG
#define CONFIG_FROBNICATE_DEBUG 1
should_print("debug: FROBNICATE: val=-2\n",
log_debugf("val=%d", -2));
#undef CONFIG_FROBNICATE_DEBUG
#define CONFIG_FROBNICATE_DEBUG 0
should_print(NULL,
log_debugf("val=%d", -2));
#undef CONFIG_FROBNICATE_DEBUG
#endif
/* libmisc/fmt.h */
should_print("error: FROBNICATE: val=42\n",
log_errorln("val=", 42));
should_print("info : FROBNICATE: val=0\n",
log_infoln("val=", 0));
#ifndef NDEBUG
#define CONFIG_FROBNICATE_DEBUG 1
should_print("debug: FROBNICATE: val=-2\n",
log_debugln("val=", -2));
#undef CONFIG_FROBNICATE_DEBUG
#define CONFIG_FROBNICATE_DEBUG 0
should_print(NULL,
log_debugln("val=", -2));
#undef CONFIG_FROBNICATE_DEBUG
#endif
if (log_output.dat) {
free(log_output.dat);
log_output.dat = NULL;
log_output.cap = 0;
}
return 0;
}
|