]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/printf_test.c
Merge master into int-new
[thirdparty/bird.git] / lib / printf_test.c
1 /*
2 * BIRD Library -- String Functions Tests
3 *
4 * (c) 2015 CZ.NIC z.s.p.o.
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #include "test/birdtest.h"
10
11 #include "lib/string.h"
12
13 #define BSPRINTF(nw, res, buf, fmt, ...) \
14 do { \
15 int n = bsprintf(buf, fmt, ##__VA_ARGS__); \
16 bt_assert_msg(n == nw, "fmt=\"%s\" returns n=%d, want %d", fmt, n, nw); \
17 bt_assert_msg(buf[n] == 0, "fmt=\"%s\" buf[%d] should be \'\\0\', found 0x%02x", fmt, n, buf[n]); \
18 bt_assert_msg(memcmp(buf, res, nw) == 0, "fmt=\"%s\" writes \"%*s\", want \"%*s\"", fmt, (n < nw ? n : nw), buf, nw, res); \
19 } while (0)
20
21 static int
22 t_simple(void)
23 {
24 char buf[256];
25 memset(buf, 0xa5, 256);
26
27 BSPRINTF(0, "", buf, "", NULL);
28 BSPRINTF(1, "%", buf, "%%", NULL);
29 BSPRINTF(2, "%%", buf, "%%%%", NULL);
30
31 BSPRINTF(1, "\x00", buf, "%c", 0);
32 BSPRINTF(1, "@", buf, "@", 64);
33 BSPRINTF(1, "\xff", buf, "%c", 0xff);
34
35 errno = 5;
36 BSPRINTF(18, "Input/output error", buf, "%m");
37 errno = 0;
38
39 BSPRINTF(18, "Input/output error", buf, "%M", 5);
40
41 BSPRINTF(11, "TeSt%StRiNg", buf, "%s", "TeSt%StRiNg");
42
43 if (sizeof(void *) == 4)
44 BSPRINTF(8, "1a15600d", buf, "%p", (void *) 0x1a15600d);
45 else
46 BSPRINTF(16, "00000fee1a15600d", buf, "%p", (void *) 0xfee1a15600d);
47
48 long ln = 0;
49 BSPRINTF(10, "TeStStRiNg", buf, "TeStS%lntRiNg", &ln);
50 bt_assert_msg(ln == 5, "fmt=\"TeStS%%lntRiNg\", &ln makes ln=%ld, want 5", ln);
51
52 BSPRINTF(2, "%d", buf, "%%d", 1);
53 BSPRINTF(1, "1", buf, "%d", 1);
54 BSPRINTF(2, "+1", buf, "%+d", 1);
55 BSPRINTF(2, " 1", buf, "% d", 1);
56 BSPRINTF(2, "-1", buf, "%d", -1);
57 BSPRINTF(11, "-2147483648", buf, "%d", -2147483648);
58
59 return 1;
60 }
61
62 int
63 main(int argc, char *argv[])
64 {
65 bt_init(argc, argv);
66
67 bt_test_suite(t_simple, "printf without varargs");
68
69 return bt_exit_value();
70 }