6c15fbea19966dfb70a47523d41c3567c4f26ab0
[strongswan.git] / src / libstrongswan / tests / suites / test_printf.c
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "test_suite.h"
17
18 #include <errno.h>
19 #include <math.h>
20
21 static void verify(char *expected, char *format, ...)
22 {
23 FILE *mem;
24 char buf[128];
25 va_list args;
26
27 va_start(args, format);
28 vsnprintf(buf, sizeof(buf), format, args);
29 ck_assert_str_eq(expected, buf);
30 va_end(args);
31
32 mem = fmemopen(buf, sizeof(buf), "w");
33 va_start(args, format);
34 vfprintf(mem, format, args);
35 va_end(args);
36 fclose(mem);
37 ck_assert_str_eq(expected, buf);
38 }
39
40 START_TEST(test_printf_strings)
41 {
42 verify("a bc def", "%s %s %s", "a", "bc", "def");
43 verify("asd", "%.3s", "asdfg");
44 verify("asdf", "%.*s", (int)4, "asdfg");
45 verify(" asdf", "%6s", "asdf");
46 verify(" asdf", "%+6s", "asdf");
47 verify("asdf ", "%-6s", "asdf");
48 }
49 END_TEST
50
51 START_TEST(test_printf_err)
52 {
53 errno = EINVAL;
54 verify((char*)strerror(errno), "%m");
55 }
56 END_TEST
57
58 START_TEST(test_printf_unsigned)
59 {
60 verify("1 23 456", "%u %lu %llu", 1, (u_long)23, (u_int64_t)456);
61 verify("65535 255", "%hu %hhu", 0x1ffff, 0x1ff);
62 verify("123456789", "%zu", (size_t)123456789);
63 verify(" 12", "%5u", 12);
64 verify("12 ", "%-5u", 12);
65 verify("0012", "%04u", 12);
66 verify("0012", "%.4u", 12);
67 }
68 END_TEST
69
70 START_TEST(test_printf_signed)
71 {
72 verify("-1 -23 -456", "%d %ld %lld", -1, (long)-23, (int64_t)-456);
73 verify("-1 -1", "%hd %hhd", 0x1ffff, 0x1ff);
74 verify("123456789", "%zd", (ssize_t)123456789);
75 verify(" -12", "%5d", -12);
76 verify("-12 ", "%-5d", -12);
77 verify("-012", "%04d", -12);
78 verify("-0012", "%.4d", -12);
79 }
80 END_TEST
81
82 START_TEST(test_printf_hex)
83 {
84 verify("1 23 456", "%x %lx %llx", 1, (u_long)0x23, (u_int64_t)0x456);
85 verify("12abcdef 12ABCDEF", "%x %X", 0x12ABCDEF, 0x12ABCDEF);
86 verify("ffff ff", "%hx %hhx", 0x1ffff, 0x1ff);
87 verify("23456789", "%zx", (size_t)0x23456789);
88 verify(" ab", "%5x", 0xab);
89 verify("ab ", "%-5x", 0xab);
90 verify("00ab", "%04x", 0xab);
91 verify("00ab", "%.4x", 0xab);
92 }
93 END_TEST
94
95 START_TEST(test_printf_float)
96 {
97 verify("0.000000", "%f", 0.0);
98 verify("1.000000", "%f", 1.0);
99 verify("12345.1", "%.1f", 12345.123);
100 verify("1", "%.0f", 1.0);
101 verify("1.3", "%.1f", 1.346789);
102 verify("1.23", "%.2f", 1.23456789);
103 verify("1.123", "%.3f", 1.123456789);
104 verify("1.0123", "%.4f", 1.0123456789);
105
106 verify("-1.000000", "%f", -1.0);
107 verify("-12345.1", "%.1f", -12345.123);
108 verify("-1", "%.0f", -1.0);
109 verify("-1.3", "%.1f", -1.3456789);
110 verify("-1.23", "%.2f", -1.23456789);
111 verify("-1.123", "%.3f", -1.123456789);
112 verify("-1.0123", "%.4f", -1.0123456789);
113
114 verify(" 1.2", "%5.1f", 1.234);
115 verify("001.2", "%05.1f", 1.234);
116 verify("1.2 ", "%-5.1f", 1.234);
117
118 verify("12346", "%.0f", 12345.6789);
119 verify("2", "%.0f", 1.5);
120 verify("1", "%.0f", 1.49);
121 verify("1.2", "%.1f", 1.151);
122 verify("1.1", "%.1f", 1.149);
123 verify("1.13", "%.2f", 1.1251);
124 verify("1.12", "%.2f", 1.1249);
125 verify("1.124", "%.3f", 1.12351);
126 verify("1.123", "%.3f", 1.12349);
127
128 verify("-12346", "%.0f", -12345.6789);
129 verify("-2", "%.0f", -1.51);
130 verify("-1", "%.0f", -1.49);
131 verify("-1.2", "%.1f", -1.151);
132 verify("-1.1", "%.1f", -1.149);
133 verify("-1.13", "%.2f", -1.1251);
134 verify("-1.12", "%.2f", -1.1249);
135 verify("-1.124", "%.3f", -1.12351);
136 verify("-1.123", "%.3f", -1.12349);
137
138 #ifdef NAN
139 verify("nan", "%.3f", NAN);
140 verify(" nan", "%5.3f", NAN);
141 verify("NAN", "%.3F", NAN);
142 verify("NAN ", "%-5.3F", NAN);
143 #endif
144 #ifdef INFINITY
145 verify("inf", "%.3f", INFINITY);
146 verify("-inf", "%.4f", -INFINITY);
147 verify("INF", "%.3F", INFINITY);
148 verify("-INF", "%.4F", -INFINITY);
149 #endif
150 }
151 END_TEST
152
153 Suite *printf_suite_create()
154 {
155 Suite *s;
156 TCase *tc;
157
158 s = suite_create("printf");
159
160 tc = tcase_create("strings");
161 tcase_add_test(tc, test_printf_strings);
162 suite_add_tcase(s, tc);
163
164 tc = tcase_create("err");
165 tcase_add_test(tc, test_printf_err);
166 suite_add_tcase(s, tc);
167
168 tc = tcase_create("unsiged");
169 tcase_add_test(tc, test_printf_unsigned);
170 suite_add_tcase(s, tc);
171
172 tc = tcase_create("siged");
173 tcase_add_test(tc, test_printf_signed);
174 suite_add_tcase(s, tc);
175
176 tc = tcase_create("hex");
177 tcase_add_test(tc, test_printf_hex);
178 suite_add_tcase(s, tc);
179
180 tc = tcase_create("float");
181 tcase_add_test(tc, test_printf_float);
182 suite_add_tcase(s, tc);
183
184 return s;
185 }