Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / lib / printf_test.c @ 6b3f1a54

History | View | Annotate | Download (2.25 KB)

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
  BSPRINTF(7, "123.456", buf, "%t", (btime) 123456789);
60
  BSPRINTF(7, "123.456", buf, "%2t", (btime) 123456789);
61
  BSPRINTF(8, " 123.456", buf, "%8t", (btime) 123456789);
62
  BSPRINTF(4, " 123", buf, "%4.0t", (btime) 123456789);
63
  BSPRINTF(8, "123.4567", buf, "%8.4t", (btime) 123456789);
64
  BSPRINTF(9, "0123.4567", buf, "%09.4t", (btime) 123456789);
65
  BSPRINTF(12, "  123.456789", buf, "%12.10t", (btime) 123456789);
66
  BSPRINTF(8, " 123.004", buf, "%8t", (btime) 123004 MS);
67

    
68
  return 1;
69
}
70

    
71
int
72
main(int argc, char *argv[])
73
{
74
  bt_init(argc, argv);
75

    
76
  bt_test_suite(t_simple, "printf without varargs");
77

    
78
  return bt_exit_value();
79
}