summaryrefslogtreecommitdiff
path: root/libmisc/tests/test_fmt.c
blob: 6a6eb7c7f9d55ebe016aa73f3df0c8fb501e2c9a (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* libmisc/tests/test_fmt.c - Tests for <libmisc/fmt.h>
 *
 * Copyright (C) 2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <string.h> /* for strcmp(), memcmp(), memset() */

#include <libmisc/fmt.h>

#include "test.h"

int main() {
	char str[128] = {};
#define do_print(...) fmt_snprint(str, sizeof(str), __VA_ARGS__)

	do_print("hello ", 9, " world!\n");
	test_assert(strcmp(str, "hello 9 world!\n") == 0);
	memset(str, 0, sizeof(str));

	do_print("hello ", (base8, 9), " world!\n");
	test_assert(strcmp(str, "hello 11 world!\n") == 0);
	memset(str, 0, sizeof(str));

	do_print("hello ", (base2, 9), (qstr, " world!\n"));
	test_assert(strcmp(str, "hello 1001\" world!\\n\"") == 0);
	memset(str, 0, sizeof(str));

	do_print("hello ", (base16, 17), " world!\n");
	test_assert(strcmp(str, "hello 11 world!\n") == 0);
	memset(str, 0, sizeof(str));

	do_print((strn, "hello ", 4));
	test_assert(strcmp(str, "hell") == 0);
	memset(str, 0, sizeof(str));

	do_print((strn, "h\0ello ", 4));
	test_assert(memcmp(str, "h\0\0", 3) == 0);
	memset(str, 0, sizeof(str));

	do_print((mem, "hello ", 4));
	test_assert(strcmp(str, "hell") == 0);
	memset(str, 0, sizeof(str));

	do_print((mem, "hello\0world", strlen("hello world")+1));
	test_assert(memcmp(str, "hello\0world", strlen("hello world")+1) == 0);
	memset(str, 0, sizeof(str));

	do_print((qmem, "hello\0world", strlen("hello world")+1));
	test_assert(strcmp(str, "\"hello\\0world\\0\"") == 0);
	memset(str, 0, sizeof(str));

	do_print((qstr, "hello\0world"));
	test_assert(strcmp(str, "\"hello\"") == 0);
	memset(str, 0, sizeof(str));

	do_print((qstrn, "hello\0world", strlen("hello world")+1));
	test_assert(strcmp(str, "\"hello\"") == 0);
	memset(str, 0, sizeof(str));

	do_print((qstrn, "hello\0world", 4));
	test_assert(strcmp(str, "\"hell\"") == 0);
	memset(str, 0, sizeof(str));

	do_print((byte, 'h'), (byte, 'w'));
	test_assert(strcmp(str, "hw") == 0);
	memset(str, 0, sizeof(str));

	do_print((qbyte, 'h'), (qbyte, 'w'));
	test_assert(strcmp(str, "'h''w'") == 0);
	memset(str, 0, sizeof(str));

	do_print("zero ", 0);
	test_assert(strcmp(str, "zero 0") == 0);
	memset(str, 0, sizeof(str));

	const char *const_str = "hello";
	do_print(const_str);
	test_assert(strcmp(str, "hello") == 0);
	memset(str, 0, sizeof(str));

	bool t = true;
	do_print(t);
	test_assert(strcmp(str, "true") == 0);
	memset(str, 0, sizeof(str));

	bool f = false;
	do_print(f);
	test_assert(strcmp(str, "false") == 0);
	memset(str, 0, sizeof(str));

	/* Check that it accepts all primitive types of integer, not
	 * just all sizes of integer (e.g., on x86-64,
	 * sizeof(long)==sizeof(int), but they're different primitive
	 * types).  */
	{
		signed char x = 42;
		do_print("schar ", x);
		test_assert(strcmp(str, "schar 42") == 0);
		memset(str, 0, sizeof(str));
	}
	{
		unsigned char x = 43;
		do_print("uchar ", x);
		test_assert(strcmp(str, "uchar 43") == 0);
		memset(str, 0, sizeof(str));
	}

	{
		short x = 44;
		do_print("short ", x);
		test_assert(strcmp(str, "short 44") == 0);
		memset(str, 0, sizeof(str));
	}
	{
		unsigned short x = 45;
		do_print("ushort ", x);
		test_assert(strcmp(str, "ushort 45") == 0);
		memset(str, 0, sizeof(str));
	}

	{
		int x = 46;
		do_print("int ", x);
		test_assert(strcmp(str, "int 46") == 0);
		memset(str, 0, sizeof(str));
	}
	{
		unsigned int x = 47;
		do_print("uint ", x);
		test_assert(strcmp(str, "uint 47") == 0);
		memset(str, 0, sizeof(str));
	}

	{
		long x = 48;
		do_print("long ", x);
		test_assert(strcmp(str, "long 48") == 0);
		memset(str, 0, sizeof(str));
	}
	{
		unsigned long x = 49;
		do_print("ulong ", x);
		test_assert(strcmp(str, "ulong 49") == 0);
		memset(str, 0, sizeof(str));
	}

	{
		long long x = 50;
		do_print("long long ", x);
		test_assert(strcmp(str, "long long 50") == 0);
		memset(str, 0, sizeof(str));
	}
	{
		unsigned long long x = 51;
		do_print("ulong long ", x);
		test_assert(strcmp(str, "ulong long 51") == 0);
		memset(str, 0, sizeof(str));
	}

	do_print((ljust, 10, ' ', (base10, 1), "x"));
	test_assert(strcmp(str, "1x        ") == 0);
	memset(str, 0, sizeof(str));

	do_print((rjust, 10, ' ', (base10, 1), "x"));
	test_assert(strcmp(str, "        1x") == 0);
	memset(str, 0, sizeof(str));

	return 0;
}