diff options
author | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2016-11-09 16:36:34 +0100 |
---|---|---|
committer | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2016-11-09 16:36:34 +0100 |
commit | 9b0a0ba9e671d9134b93c33ab73ccccb352acafa (patch) | |
tree | 2a3b007b698c02c72c7bae25d3c7cae6293cd36f /lib/printf_test.c | |
parent | 8860e991f6650e47cfe6c1af595fe4fe92a4edfd (diff) |
Unit Testing for BIRD
- Unit Testing Framework (BirdTest)
- Integration of BirdTest into the BIRD build system
- Tests for several BIRD modules
Based on squashed Pavel Tvrdik's int-test branch, updated for
current int-new branch.
Diffstat (limited to 'lib/printf_test.c')
-rw-r--r-- | lib/printf_test.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/printf_test.c b/lib/printf_test.c new file mode 100644 index 00000000..9763b7b1 --- /dev/null +++ b/lib/printf_test.c @@ -0,0 +1,70 @@ +/* + * BIRD Library -- String Functions Tests + * + * (c) 2015 CZ.NIC z.s.p.o. + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#include "test/birdtest.h" + +#include "lib/string.h" + +#define BSPRINTF(nw, res, buf, fmt, ...) \ + do { \ + int n = bsprintf(buf, fmt, ##__VA_ARGS__); \ + bt_assert_msg(n == nw, "fmt=\"%s\" returns n=%d, want %d", fmt, n, nw); \ + bt_assert_msg(buf[n] == 0, "fmt=\"%s\" buf[%d] should be \'\\0\', found 0x%02x", fmt, n, buf[n]); \ + bt_assert_msg(memcmp(buf, res, nw) == 0, "fmt=\"%s\" writes \"%*s\", want \"%*s\"", fmt, (n < nw ? n : nw), buf, nw, res); \ + } while (0) + +static int +t_simple(void) +{ + char buf[256]; + memset(buf, 0xa5, 256); + + BSPRINTF(0, "", buf, "", NULL); + BSPRINTF(1, "%", buf, "%%", NULL); + BSPRINTF(2, "%%", buf, "%%%%", NULL); + + BSPRINTF(1, "\x00", buf, "%c", 0); + BSPRINTF(1, "@", buf, "@", 64); + BSPRINTF(1, "\xff", buf, "%c", 0xff); + + errno = 5; + BSPRINTF(18, "Input/output error", buf, "%m"); + errno = 0; + + BSPRINTF(18, "Input/output error", buf, "%M", 5); + + BSPRINTF(11, "TeSt%StRiNg", buf, "%s", "TeSt%StRiNg"); + + if (sizeof(void *) == 4) + BSPRINTF(8, "1a15600d", buf, "%p", (void *) 0x1a15600d); + else + BSPRINTF(16, "00000fee1a15600d", buf, "%p", (void *) 0xfee1a15600d); + + long ln = 0; + BSPRINTF(10, "TeStStRiNg", buf, "TeStS%lntRiNg", &ln); + bt_assert_msg(ln == 5, "fmt=\"TeStS%%lntRiNg\", &ln makes ln=%ld, want 5", ln); + + BSPRINTF(2, "%d", buf, "%%d", 1); + BSPRINTF(1, "1", buf, "%d", 1); + BSPRINTF(2, "+1", buf, "%+d", 1); + BSPRINTF(2, " 1", buf, "% d", 1); + BSPRINTF(2, "-1", buf, "%d", -1); + BSPRINTF(11, "-2147483648", buf, "%d", -2147483648); + + return BT_SUCCESS; +} + +int +main(int argc, char *argv[]) +{ + bt_init(argc, argv); + + bt_test_suite(t_simple, "printf without varargs"); + + return bt_exit_value(); +} |