]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/printf_test.c
2 * BIRD Library -- String Functions Tests
4 * (c) 2015 CZ.NIC z.s.p.o.
6 * Can be freely distributed and used under the terms of the GNU GPL.
9 #include "test/birdtest.h"
11 #include "lib/string.h"
13 #define BSPRINTF(nw, res, buf, fmt, ...) \
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); \
25 memset(buf
, 0xa5, 256);
27 BSPRINTF(0, "", buf
, "", NULL
);
28 BSPRINTF(1, "%", buf
, "%%", NULL
);
29 BSPRINTF(2, "%%", buf
, "%%%%", NULL
);
31 BSPRINTF(1, "\x00", buf
, "%c", 0);
32 BSPRINTF(1, "@", buf
, "@", 64);
33 BSPRINTF(1, "\xff", buf
, "%c", 0xff);
36 BSPRINTF(18, "Input/output error", buf
, "%m");
39 BSPRINTF(18, "Input/output error", buf
, "%M", 5);
41 BSPRINTF(11, "TeSt%StRiNg", buf
, "%s", "TeSt%StRiNg");
43 if (sizeof(void *) == 4)
44 BSPRINTF(8, "1a15600d", buf
, "%p", (void *) 0x1a15600d);
46 BSPRINTF(16, "00000fee1a15600d", buf
, "%p", (void *) 0xfee1a15600d);
49 BSPRINTF(10, "TeStStRiNg", buf
, "TeStS%lntRiNg", &ln
);
50 bt_assert_msg(ln
== 5, "fmt=\"TeStS%%lntRiNg\", &ln makes ln=%ld, want 5", ln
);
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);
63 main(int argc
, char *argv
[])
67 bt_test_suite(t_simple
, "printf without varargs");
69 return bt_exit_value();