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 14 static int justprint = 0; 15 16 static char *fpexpected[][5] = { 17 /* 0 */ { "0.0000e+00", "0.0000", "0", "0.0000E+00", "0" }, 18 /* 1 */ { "6.7000e-01", "0.6700", "0.67", "6.7000E-01", "0.67" }, 19 /* 2 */ { "6.6667e-01", "0.6667", "0.6667", "6.6667E-01", "0.6667" }, 20 /* 3 */ { "6.6667e-04", "0.0007", "0.0006667", "6.6667E-04", "0.0006667" }, 21 /* 4 */ { "6.6667e-05", "0.0001", "6.667e-05", "6.6667E-05", "6.667E-05" }, 22 /* 5 */ { "6.6667e+00", "6.6667", "6.667", "6.6667E+00", "6.667" }, 23 /* 6 */ { "6.6667e+01", "66.6667", "66.67", "6.6667E+01", "66.67" }, 24 /* 7 */ { "6.6667e+02", "666.6667", "666.7", "6.6667E+02", "666.7" }, 25 /* 8 */ { "6.6667e+03", "6666.6667", "6667", "6.6667E+03", "6667" }, 26 /* 9 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" }, 27 /* 10 */ { "0.00000e+00", "0.00000", "0", "0.00000E+00", "0" }, 28 /* 11 */ { "6.70000e-01", "0.67000", "0.67", "6.70000E-01", "0.67" }, 29 /* 12 */ { "6.66667e-01", "0.66667", "0.66667", "6.66667E-01", "0.66667" }, 30 /* 13 */ { "6.66667e-04", "0.00067", "0.00066667", "6.66667E-04", "0.00066667" }, 31 /* 14 */ { "6.66667e-05", "0.00007", "6.6667e-05", "6.66667E-05", "6.6667E-05" }, 32 /* 15 */ { "6.66667e+00", "6.66667", "6.6667", "6.66667E+00", "6.6667" }, 33 /* 16 */ { "6.66667e+01", "66.66667", "66.667", "6.66667E+01", "66.667" }, 34 /* 17 */ { "6.66667e+02", "666.66667", "666.67", "6.66667E+02", "666.67" }, 35 /* 18 */ { "6.66667e+03", "6666.66667", "6666.7", "6.66667E+03", "6666.7" }, 36 /* 19 */ { "6.66667e+04", "66666.66667", "66667", "6.66667E+04", "66667" }, 37 /* 20 */ { " 0.0000e+00", " 0.0000", " 0", " 0.0000E+00", " 0" }, 38 /* 21 */ { " 6.7000e-01", " 0.6700", " 0.67", " 6.7000E-01", " 0.67" }, 39 /* 22 */ { " 6.6667e-01", " 0.6667", " 0.6667", " 6.6667E-01", " 0.6667" }, 40 /* 23 */ { " 6.6667e-04", " 0.0007", " 0.0006667", " 6.6667E-04", " 0.0006667" }, 41 /* 24 */ { " 6.6667e-05", " 0.0001", " 6.667e-05", " 6.6667E-05", " 6.667E-05" }, 42 /* 25 */ { " 6.6667e+00", " 6.6667", " 6.667", " 6.6667E+00", " 6.667" }, 43 /* 26 */ { " 6.6667e+01", " 66.6667", " 66.67", " 6.6667E+01", " 66.67" }, 44 /* 27 */ { " 6.6667e+02", " 666.6667", " 666.7", " 6.6667E+02", " 666.7" }, 45 /* 28 */ { " 6.6667e+03", " 6666.6667", " 6667", " 6.6667E+03", " 6667" }, 46 /* 29 */ { " 6.6667e+04", " 66666.6667", " 6.667e+04", " 6.6667E+04", " 6.667E+04" }, 47 /* 30 */ { " 0.00000e+00", " 0.00000", " 0", " 0.00000E+00", " 0" }, 48 /* 31 */ { " 6.70000e-01", " 0.67000", " 0.67", " 6.70000E-01", " 0.67" }, 49 /* 32 */ { " 6.66667e-01", " 0.66667", " 0.66667", " 6.66667E-01", " 0.66667" }, 50 /* 33 */ { " 6.66667e-04", " 0.00067", " 0.00066667", " 6.66667E-04", " 0.00066667" }, 51 /* 34 */ { " 6.66667e-05", " 0.00007", " 6.6667e-05", " 6.66667E-05", " 6.6667E-05" }, 52 /* 35 */ { " 6.66667e+00", " 6.66667", " 6.6667", " 6.66667E+00", " 6.6667" }, 53 /* 36 */ { " 6.66667e+01", " 66.66667", " 66.667", " 6.66667E+01", " 66.667" }, 54 /* 37 */ { " 6.66667e+02", " 666.66667", " 666.67", " 6.66667E+02", " 666.67" }, 55 /* 38 */ { " 6.66667e+03", " 6666.66667", " 6666.7", " 6.66667E+03", " 6666.7" }, 56 /* 39 */ { " 6.66667e+04", " 66666.66667", " 66667", " 6.66667E+04", " 66667" }, 57 /* 40 */ { "0e+00", "0", "0", "0E+00", "0" }, 58 /* 41 */ { "7e-01", "1", "0.7", "7E-01", "0.7" }, 59 /* 42 */ { "7e-01", "1", "0.7", "7E-01", "0.7" }, 60 /* 43 */ { "7e-04", "0", "0.0007", "7E-04", "0.0007" }, 61 /* 44 */ { "7e-05", "0", "7e-05", "7E-05", "7E-05" }, 62 /* 45 */ { "7e+00", "7", "7", "7E+00", "7" }, 63 /* 46 */ { "7e+01", "67", "7e+01", "7E+01", "7E+01" }, 64 /* 47 */ { "7e+02", "667", "7e+02", "7E+02", "7E+02" }, 65 /* 48 */ { "7e+03", "6667", "7e+03", "7E+03", "7E+03" }, 66 /* 49 */ { "7e+04", "66667", "7e+04", "7E+04", "7E+04" }, 67 /* 50 */ { "0.000000e+00", "0.000000", "0", "0.000000E+00", "0" }, 68 /* 51 */ { "6.700000e-01", "0.670000", "0.67", "6.700000E-01", "0.67" }, 69 /* 52 */ { "6.666667e-01", "0.666667", "0.666667", "6.666667E-01", "0.666667" }, 70 /* 53 */ { "6.666667e-04", "0.000667", "0.000666667", "6.666667E-04", "0.000666667" }, 71 /* 54 */ { "6.666667e-05", "0.000067", "6.66667e-05", "6.666667E-05", "6.66667E-05" }, 72 /* 55 */ { "6.666667e+00", "6.666667", "6.66667", "6.666667E+00", "6.66667" }, 73 /* 56 */ { "6.666667e+01", "66.666667", "66.6667", "6.666667E+01", "66.6667" }, 74 /* 57 */ { "6.666667e+02", "666.666667", "666.667", "6.666667E+02", "666.667" }, 75 /* 58 */ { "6.666667e+03", "6666.666667", "6666.67", "6.666667E+03", "6666.67" }, 76 /* 59 */ { "6.666667e+04", "66666.666667", "66666.7", "6.666667E+04", "66666.7" }, 77 /* 60 */ { "0.0000e+00", "000.0000", "00000000", "0.0000E+00", "00000000" }, 78 /* 61 */ { "6.7000e-01", "000.6700", "00000.67", "6.7000E-01", "00000.67" }, 79 /* 62 */ { "6.6667e-01", "000.6667", "000.6667", "6.6667E-01", "000.6667" }, 80 /* 63 */ { "6.6667e-04", "000.0007", "0.0006667", "6.6667E-04", "0.0006667" }, 81 /* 64 */ { "6.6667e-05", "000.0001", "6.667e-05", "6.6667E-05", "6.667E-05" }, 82 /* 65 */ { "6.6667e+00", "006.6667", "0006.667", "6.6667E+00", "0006.667" }, 83 /* 66 */ { "6.6667e+01", "066.6667", "00066.67", "6.6667E+01", "00066.67" }, 84 /* 67 */ { "6.6667e+02", "666.6667", "000666.7", "6.6667E+02", "000666.7" }, 85 /* 68 */ { "6.6667e+03", "6666.6667", "00006667", "6.6667E+03", "00006667" }, 86 /* 69 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" }, 87 }; 88 89 static void dofptest(int test, double val, char *width, int prec, int *fail) 90 { 91 char format[80], result[80]; 92 int i; 93 94 for (i = 0; i < 5; i++) { 95 char *fspec = NULL; 96 switch (i) { 97 case 0: 98 fspec = "e"; 99 break; 100 case 1: 101 fspec = "f"; 102 break; 103 case 2: 104 fspec = "g"; 105 break; 106 case 3: 107 fspec = "E"; 108 break; 109 case 4: 110 fspec = "G"; 111 break; 112 } 113 114 if (prec >= 0) 115 BIO_snprintf(format, sizeof(format), "%%%s.%d%s", width, prec, 116 fspec); 117 else 118 BIO_snprintf(format, sizeof(format), "%%%s%s", width, fspec); 119 BIO_snprintf(result, sizeof(result), format, val); 120 121 if (justprint) { 122 if (i == 0) { 123 printf(" /* %3d */ { \"%s\"", test, result); 124 } else { 125 printf(", \"%s\"", result); 126 } 127 } else { 128 if (strcmp(fpexpected[test][i], result) != 0) { 129 printf("Test %d(%d) failed. Expected \"%s\". Got \"%s\". " 130 "Format \"%s\"\n", test, i, fpexpected[test][i], result, 131 format); 132 *fail = 1; 133 } 134 } 135 } 136 if (justprint) { 137 printf(" },\n"); 138 } 139 } 140 141 int main(int argc, char **argv) 142 { 143 int test = 0; 144 int i; 145 int fail = 0; 146 int prec = -1; 147 char *width = ""; 148 const double frac = 2.0/3.0; 149 char buf[80]; 150 151 if (argc == 2 && strcmp(argv[1], "-expected") == 0) { 152 justprint = 1; 153 } 154 155 CRYPTO_set_mem_debug(1); 156 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); 157 158 /* Tests for floating point format specifiers */ 159 for (i = 0; i < 7; i++) { 160 switch (i) { 161 case 0: 162 prec = 4; 163 width = ""; 164 break; 165 case 1: 166 prec = 5; 167 width = ""; 168 break; 169 case 2: 170 prec = 4; 171 width = "12"; 172 break; 173 case 3: 174 prec = 5; 175 width = "12"; 176 break; 177 case 4: 178 prec = 0; 179 width = ""; 180 break; 181 case 5: 182 prec = -1; 183 width = ""; 184 break; 185 case 6: 186 prec = 4; 187 width = "08"; 188 break; 189 } 190 191 dofptest(test++, 0.0, width, prec, &fail); 192 dofptest(test++, 0.67, width, prec, &fail); 193 dofptest(test++, frac, width, prec, &fail); 194 dofptest(test++, frac / 1000, width, prec, &fail); 195 dofptest(test++, frac / 10000, width, prec, &fail); 196 dofptest(test++, 6.0 + frac, width, prec, &fail); 197 dofptest(test++, 66.0 + frac, width, prec, &fail); 198 dofptest(test++, 666.0 + frac, width, prec, &fail); 199 dofptest(test++, 6666.0 + frac, width, prec, &fail); 200 dofptest(test++, 66666.0 + frac, width, prec, &fail); 201 } 202 203 /* Test excessively big number. Should fail */ 204 if (BIO_snprintf(buf, sizeof(buf), "%f\n", 2 * (double)ULONG_MAX) != -1) { 205 printf("Test %d failed. Unexpected success return from " 206 "BIO_snprintf()\n", test); 207 fail = 1; 208 } 209 210 #ifndef OPENSSL_NO_CRYPTO_MDEBUG 211 if (CRYPTO_mem_leaks_fp(stderr) <= 0) 212 return 1; 213 # endif 214 215 if (!justprint) { 216 if (fail) { 217 printf("FAIL\n"); 218 return 1; 219 } 220 printf ("PASS\n"); 221 } 222 return 0; 223 } 224 225 226