unit-tests: Skip fmemopen() based printf() tests if not available
[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 char buf[128];
24 va_list args;
25
26 va_start(args, format);
27 vsnprintf(buf, sizeof(buf), format, args);
28 ck_assert_str_eq(expected, buf);
29 va_end(args);
30
31 #ifdef HAVE_FMEMOPEN
32 {
33 FILE *mem;
34
35 mem = fmemopen(buf, sizeof(buf), "w");
36 va_start(args, format);
37 vfprintf(mem, format, args);
38 va_end(args);
39 fclose(mem);
40 ck_assert_str_eq(expected, buf);
41 }
42 #endif /* HAVE_FMEMOPEN */
43 }
44
45 START_TEST(test_printf_strings)
46 {
47 verify("a bc def", "%s %s %s", "a", "bc", "def");
48 verify("asd", "%.3s", "asdfg");
49 verify("asdf", "%.*s", (int)4, "asdfg");
50 verify(" asdf", "%6s", "asdf");
51 verify(" asdf", "%+6s", "asdf");
52 verify("asdf ", "%-6s", "asdf");
53 }
54 END_TEST
55
56 START_TEST(test_printf_err)
57 {
58 errno = EINVAL;
59 verify((char*)strerror(errno), "%m");
60 }
61 END_TEST
62
63 START_TEST(test_printf_unsigned)
64 {
65 verify("1 23 456", "%u %lu %llu", 1, (u_long)23, (u_int64_t)456);
66 verify("65535 255", "%hu %hhu", 0x1ffff, 0x1ff);
67 verify("123456789", "%zu", (size_t)123456789);
68 verify(" 12", "%5u", 12);
69 verify("12 ", "%-5u", 12);
70 verify("0012", "%04u", 12);
71 verify("0012", "%.4u", 12);
72 }
73 END_TEST
74
75 START_TEST(test_printf_signed)
76 {
77 verify("-1 -23 -456", "%d %ld %lld", -1, (long)-23, (int64_t)-456);
78 verify("-1 -1", "%hd %hhd", 0x1ffff, 0x1ff);
79 verify("123456789", "%zd", (ssize_t)123456789);
80 verify(" -12", "%5d", -12);
81 verify("-12 ", "%-5d", -12);
82 verify("-012", "%04d", -12);
83 verify("-0012", "%.4d", -12);
84 }
85 END_TEST
86
87 START_TEST(test_printf_hex)
88 {
89 verify("1 23 456", "%x %lx %llx", 1, (u_long)0x23, (u_int64_t)0x456);
90 verify("12abcdef 12ABCDEF", "%x %X", 0x12ABCDEF, 0x12ABCDEF);
91 verify("ffff ff", "%hx %hhx", 0x1ffff, 0x1ff);
92 verify("23456789", "%zx", (size_t)0x23456789);
93 verify(" ab", "%5x", 0xab);
94 verify("ab ", "%-5x", 0xab);
95 verify("00ab", "%04x", 0xab);
96 verify("00ab", "%.4x", 0xab);
97 }
98 END_TEST
99
100 START_TEST(test_printf_float)
101 {
102 verify("0.000000", "%f", 0.0);
103 verify("1.000000", "%f", 1.0);
104 verify("12345.1", "%.1f", 12345.123);
105 verify("1", "%.0f", 1.0);
106 verify("1.3", "%.1f", 1.346789);
107 verify("1.23", "%.2f", 1.23456789);
108 verify("1.123", "%.3f", 1.123456789);
109 verify("1.0123", "%.4f", 1.0123456789);
110
111 verify("-1.000000", "%f", -1.0);
112 verify("-12345.1", "%.1f", -12345.123);
113 verify("-1", "%.0f", -1.0);
114 verify("-1.3", "%.1f", -1.3456789);
115 verify("-1.23", "%.2f", -1.23456789);
116 verify("-1.123", "%.3f", -1.123456789);
117 verify("-1.0123", "%.4f", -1.0123456789);
118
119 verify(" 1.2", "%5.1f", 1.234);
120 verify("001.2", "%05.1f", 1.234);
121 verify("1.2 ", "%-5.1f", 1.234);
122
123 verify("12346", "%.0f", 12345.6789);
124 verify("2", "%.0f", 1.5);
125 verify("1", "%.0f", 1.49);
126 verify("1.2", "%.1f", 1.151);
127 verify("1.1", "%.1f", 1.149);
128 verify("1.13", "%.2f", 1.1251);
129 verify("1.12", "%.2f", 1.1249);
130 verify("1.124", "%.3f", 1.12351);
131 verify("1.123", "%.3f", 1.12349);
132
133 verify("-12346", "%.0f", -12345.6789);
134 verify("-2", "%.0f", -1.51);
135 verify("-1", "%.0f", -1.49);
136 verify("-1.2", "%.1f", -1.151);
137 verify("-1.1", "%.1f", -1.149);
138 verify("-1.13", "%.2f", -1.1251);
139 verify("-1.12", "%.2f", -1.1249);
140 verify("-1.124", "%.3f", -1.12351);
141 verify("-1.123", "%.3f", -1.12349);
142
143 #ifdef NAN
144 verify("nan", "%.3f", NAN);
145 verify(" nan", "%5.3f", NAN);
146 verify("NAN", "%.3F", NAN);
147 verify("NAN ", "%-5.3F", NAN);
148 #endif
149 #ifdef INFINITY
150 verify("inf", "%.3f", INFINITY);
151 verify("-inf", "%.4f", -INFINITY);
152 verify("INF", "%.3F", INFINITY);
153 verify("-INF", "%.4F", -INFINITY);
154 #endif
155 }
156 END_TEST
157
158 Suite *printf_suite_create()
159 {
160 Suite *s;
161 TCase *tc;
162
163 s = suite_create("printf");
164
165 tc = tcase_create("strings");
166 tcase_add_test(tc, test_printf_strings);
167 suite_add_tcase(s, tc);
168
169 tc = tcase_create("err");
170 tcase_add_test(tc, test_printf_err);
171 suite_add_tcase(s, tc);
172
173 tc = tcase_create("unsiged");
174 tcase_add_test(tc, test_printf_unsigned);
175 suite_add_tcase(s, tc);
176
177 tc = tcase_create("siged");
178 tcase_add_test(tc, test_printf_signed);
179 suite_add_tcase(s, tc);
180
181 tc = tcase_create("hex");
182 tcase_add_test(tc, test_printf_hex);
183 suite_add_tcase(s, tc);
184
185 tc = tcase_create("float");
186 tcase_add_test(tc, test_printf_float);
187 suite_add_tcase(s, tc);
188
189 return s;
190 }