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