]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/bioprinttest.c
Convert more tests to framework
[thirdparty/openssl.git] / test / bioprinttest.c
1 /*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <openssl/bio.h>
13 #include "internal/numbers.h"
14 #include "testutil.h"
15 #include "test_main.h"
16 #include "test_main_custom.h"
17
18 #define nelem(x) (int)(sizeof(x) / sizeof((x)[0]))
19
20 static int justprint = 0;
21
22 static char *fpexpected[][5] = {
23 /* 0 */ { "0.0000e+00", "0.0000", "0", "0.0000E+00", "0" },
24 /* 1 */ { "6.7000e-01", "0.6700", "0.67", "6.7000E-01", "0.67" },
25 /* 2 */ { "6.6667e-01", "0.6667", "0.6667", "6.6667E-01", "0.6667" },
26 /* 3 */ { "6.6667e-04", "0.0007", "0.0006667", "6.6667E-04", "0.0006667" },
27 /* 4 */ { "6.6667e-05", "0.0001", "6.667e-05", "6.6667E-05", "6.667E-05" },
28 /* 5 */ { "6.6667e+00", "6.6667", "6.667", "6.6667E+00", "6.667" },
29 /* 6 */ { "6.6667e+01", "66.6667", "66.67", "6.6667E+01", "66.67" },
30 /* 7 */ { "6.6667e+02", "666.6667", "666.7", "6.6667E+02", "666.7" },
31 /* 8 */ { "6.6667e+03", "6666.6667", "6667", "6.6667E+03", "6667" },
32 /* 9 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" },
33 /* 10 */ { "0.00000e+00", "0.00000", "0", "0.00000E+00", "0" },
34 /* 11 */ { "6.70000e-01", "0.67000", "0.67", "6.70000E-01", "0.67" },
35 /* 12 */ { "6.66667e-01", "0.66667", "0.66667", "6.66667E-01", "0.66667" },
36 /* 13 */ { "6.66667e-04", "0.00067", "0.00066667", "6.66667E-04", "0.00066667" },
37 /* 14 */ { "6.66667e-05", "0.00007", "6.6667e-05", "6.66667E-05", "6.6667E-05" },
38 /* 15 */ { "6.66667e+00", "6.66667", "6.6667", "6.66667E+00", "6.6667" },
39 /* 16 */ { "6.66667e+01", "66.66667", "66.667", "6.66667E+01", "66.667" },
40 /* 17 */ { "6.66667e+02", "666.66667", "666.67", "6.66667E+02", "666.67" },
41 /* 18 */ { "6.66667e+03", "6666.66667", "6666.7", "6.66667E+03", "6666.7" },
42 /* 19 */ { "6.66667e+04", "66666.66667", "66667", "6.66667E+04", "66667" },
43 /* 20 */ { " 0.0000e+00", " 0.0000", " 0", " 0.0000E+00", " 0" },
44 /* 21 */ { " 6.7000e-01", " 0.6700", " 0.67", " 6.7000E-01", " 0.67" },
45 /* 22 */ { " 6.6667e-01", " 0.6667", " 0.6667", " 6.6667E-01", " 0.6667" },
46 /* 23 */ { " 6.6667e-04", " 0.0007", " 0.0006667", " 6.6667E-04", " 0.0006667" },
47 /* 24 */ { " 6.6667e-05", " 0.0001", " 6.667e-05", " 6.6667E-05", " 6.667E-05" },
48 /* 25 */ { " 6.6667e+00", " 6.6667", " 6.667", " 6.6667E+00", " 6.667" },
49 /* 26 */ { " 6.6667e+01", " 66.6667", " 66.67", " 6.6667E+01", " 66.67" },
50 /* 27 */ { " 6.6667e+02", " 666.6667", " 666.7", " 6.6667E+02", " 666.7" },
51 /* 28 */ { " 6.6667e+03", " 6666.6667", " 6667", " 6.6667E+03", " 6667" },
52 /* 29 */ { " 6.6667e+04", " 66666.6667", " 6.667e+04", " 6.6667E+04", " 6.667E+04" },
53 /* 30 */ { " 0.00000e+00", " 0.00000", " 0", " 0.00000E+00", " 0" },
54 /* 31 */ { " 6.70000e-01", " 0.67000", " 0.67", " 6.70000E-01", " 0.67" },
55 /* 32 */ { " 6.66667e-01", " 0.66667", " 0.66667", " 6.66667E-01", " 0.66667" },
56 /* 33 */ { " 6.66667e-04", " 0.00067", " 0.00066667", " 6.66667E-04", " 0.00066667" },
57 /* 34 */ { " 6.66667e-05", " 0.00007", " 6.6667e-05", " 6.66667E-05", " 6.6667E-05" },
58 /* 35 */ { " 6.66667e+00", " 6.66667", " 6.6667", " 6.66667E+00", " 6.6667" },
59 /* 36 */ { " 6.66667e+01", " 66.66667", " 66.667", " 6.66667E+01", " 66.667" },
60 /* 37 */ { " 6.66667e+02", " 666.66667", " 666.67", " 6.66667E+02", " 666.67" },
61 /* 38 */ { " 6.66667e+03", " 6666.66667", " 6666.7", " 6.66667E+03", " 6666.7" },
62 /* 39 */ { " 6.66667e+04", " 66666.66667", " 66667", " 6.66667E+04", " 66667" },
63 /* 40 */ { "0e+00", "0", "0", "0E+00", "0" },
64 /* 41 */ { "7e-01", "1", "0.7", "7E-01", "0.7" },
65 /* 42 */ { "7e-01", "1", "0.7", "7E-01", "0.7" },
66 /* 43 */ { "7e-04", "0", "0.0007", "7E-04", "0.0007" },
67 /* 44 */ { "7e-05", "0", "7e-05", "7E-05", "7E-05" },
68 /* 45 */ { "7e+00", "7", "7", "7E+00", "7" },
69 /* 46 */ { "7e+01", "67", "7e+01", "7E+01", "7E+01" },
70 /* 47 */ { "7e+02", "667", "7e+02", "7E+02", "7E+02" },
71 /* 48 */ { "7e+03", "6667", "7e+03", "7E+03", "7E+03" },
72 /* 49 */ { "7e+04", "66667", "7e+04", "7E+04", "7E+04" },
73 /* 50 */ { "0.000000e+00", "0.000000", "0", "0.000000E+00", "0" },
74 /* 51 */ { "6.700000e-01", "0.670000", "0.67", "6.700000E-01", "0.67" },
75 /* 52 */ { "6.666667e-01", "0.666667", "0.666667", "6.666667E-01", "0.666667" },
76 /* 53 */ { "6.666667e-04", "0.000667", "0.000666667", "6.666667E-04", "0.000666667" },
77 /* 54 */ { "6.666667e-05", "0.000067", "6.66667e-05", "6.666667E-05", "6.66667E-05" },
78 /* 55 */ { "6.666667e+00", "6.666667", "6.66667", "6.666667E+00", "6.66667" },
79 /* 56 */ { "6.666667e+01", "66.666667", "66.6667", "6.666667E+01", "66.6667" },
80 /* 57 */ { "6.666667e+02", "666.666667", "666.667", "6.666667E+02", "666.667" },
81 /* 58 */ { "6.666667e+03", "6666.666667", "6666.67", "6.666667E+03", "6666.67" },
82 /* 59 */ { "6.666667e+04", "66666.666667", "66666.7", "6.666667E+04", "66666.7" },
83 /* 60 */ { "0.0000e+00", "000.0000", "00000000", "0.0000E+00", "00000000" },
84 /* 61 */ { "6.7000e-01", "000.6700", "00000.67", "6.7000E-01", "00000.67" },
85 /* 62 */ { "6.6667e-01", "000.6667", "000.6667", "6.6667E-01", "000.6667" },
86 /* 63 */ { "6.6667e-04", "000.0007", "0.0006667", "6.6667E-04", "0.0006667" },
87 /* 64 */ { "6.6667e-05", "000.0001", "6.667e-05", "6.6667E-05", "6.667E-05" },
88 /* 65 */ { "6.6667e+00", "006.6667", "0006.667", "6.6667E+00", "0006.667" },
89 /* 66 */ { "6.6667e+01", "066.6667", "00066.67", "6.6667E+01", "00066.67" },
90 /* 67 */ { "6.6667e+02", "666.6667", "000666.7", "6.6667E+02", "000666.7" },
91 /* 68 */ { "6.6667e+03", "6666.6667", "00006667", "6.6667E+03", "00006667" },
92 /* 69 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" },
93 };
94
95 typedef struct z_data_st {
96 size_t value;
97 const char *format;
98 const char *expected;
99 } z_data;
100
101 static z_data zu_data[] = {
102 { SIZE_MAX, "%zu", (sizeof(size_t) == 4 ? "4294967295"
103 : sizeof(size_t) == 8 ? "18446744073709551615"
104 : "") },
105 /*
106 * in 2-complement, the unsigned number divided by two plus one becomes the
107 * smallest possible negative signed number of the corresponding type
108 */
109 { SIZE_MAX / 2 + 1, "%zi", (sizeof(size_t) == 4 ? "-2147483648"
110 : sizeof(size_t) == 8 ? "-9223372036854775808"
111 : "") },
112 { 0, "%zu", "0" },
113 { 0, "%zi", "0" },
114 };
115
116 static int test_zu(int i)
117 {
118 char bio_buf[80];
119 const z_data *data = &zu_data[i];
120
121 BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
122 if (!TEST_str_eq(bio_buf, data->expected))
123 return 0;
124 return 1;
125 }
126
127 typedef struct j_data_st {
128 uint64_t value;
129 const char *format;
130 const char *expected;
131 } j_data;
132
133 static j_data jf_data[] = {
134 { 0xffffffffffffffffU, "%ju", "18446744073709551615" },
135 { 0xffffffffffffffffU, "%jx", "ffffffffffffffff" },
136 { 0x8000000000000000U, "%ju", "9223372036854775808" },
137 /*
138 * These tests imply two's-complement, but it's the only binary
139 * representation we support, see test/sanitytest.c...
140 */
141 { 0x8000000000000000U, "%ji", "-9223372036854775808" },
142 };
143
144 static int test_j(int i)
145 {
146 const j_data *data = &jf_data[i];
147 char bio_buf[80];
148
149 BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
150 if (!TEST_str_eq(bio_buf, data->expected))
151 return 0;
152 return 1;
153 }
154
155
156 /* Precision and width. */
157 typedef struct pw_st {
158 int p;
159 const char *w;
160 } pw;
161
162 static pw pw_params[] = {
163 { 4, "" },
164 { 5, "" },
165 { 4, "12" },
166 { 5, "12" },
167 { 0, "" },
168 { -1, "" },
169 { 4, "08" }
170 };
171
172 static int dofptest(int test, double val, const char *width, int prec)
173 {
174 static const char *fspecs[] = {
175 "e", "f", "g", "E", "G"
176 };
177 char format[80], result[80];
178 int ret = 1, i;
179
180 for (i = 0; i < nelem(fspecs); i++) {
181 const char *fspec = fspecs[i];
182
183 if (prec >= 0)
184 BIO_snprintf(format, sizeof(format), "%%%s.%d%s", width, prec,
185 fspec);
186 else
187 BIO_snprintf(format, sizeof(format), "%%%s%s", width, fspec);
188 BIO_snprintf(result, sizeof(result), format, val);
189
190 if (justprint) {
191 if (i == 0)
192 printf(" /* %d */ { \"%s\"", test, result);
193 else
194 printf(", \"%s\"", result);
195 } else if (!TEST_str_eq(fpexpected[test][i], result)) {
196 TEST_info("test %d format=|%s| exp=|%s|, ret=|%s|",
197 test, format, fpexpected[test][i], result);
198 ret = 0;
199 }
200 }
201 if (justprint)
202 printf(" },\n");
203 return ret;
204 }
205
206 static int test_fp(int i)
207 {
208 static int t = 0;
209 const double frac = 2.0 / 3.0;
210 const pw *pwp = &pw_params[i];
211
212 if (!TEST_true(dofptest(t++, 0.0, pwp->w, pwp->p))
213 || !TEST_true(dofptest(t++, 0.67, pwp->w, pwp->p))
214 || !TEST_true(dofptest(t++, frac, pwp->w, pwp->p))
215 || !TEST_true(dofptest(t++, frac / 1000, pwp->w, pwp->p))
216 || !TEST_true(dofptest(t++, frac / 10000, pwp->w, pwp->p))
217 || !TEST_true(dofptest(t++, 6.0 + frac, pwp->w, pwp->p))
218 || !TEST_true(dofptest(t++, 66.0 + frac, pwp->w, pwp->p))
219 || !TEST_true(dofptest(t++, 666.0 + frac, pwp->w, pwp->p))
220 || !TEST_true(dofptest(t++, 6666.0 + frac, pwp->w, pwp->p))
221 || !TEST_true(dofptest(t++, 66666.0 + frac, pwp->w, pwp->p)))
222 return 0;
223 return 1;
224 }
225
226 static int test_big(void)
227 {
228 char buf[80];
229
230 /* Test excessively big number. Should fail */
231 if (!TEST_int_eq(BIO_snprintf(buf, sizeof(buf),
232 "%f\n", 2 * (double)ULONG_MAX), -1))
233 return 0;
234 return 1;
235 }
236
237
238 int test_main(int argc, char **argv)
239 {
240 if (argc == 2 && strcmp(argv[1], "-expected") == 0)
241 justprint = 1;
242
243 ADD_TEST(test_big);
244 ADD_ALL_TESTS(test_fp, nelem(pw_params));
245 ADD_ALL_TESTS(test_zu, nelem(zu_data));
246 ADD_ALL_TESTS(test_j, nelem(jf_data));
247
248 return run_tests(argv[0]);
249 }