1 /* $OpenBSD: fp.c,v 1.1 2008/09/07 20:36:10 martynas Exp $ */ 2 /*- 3 * Copyright (c) 2002, 2005 David Schultz <das@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * Test for printf() floating point formats. 30 */ 31 32 #include <assert.h> 33 #include <err.h> 34 #include <float.h> 35 #include <math.h> 36 #include <stdio.h> 37 #include <stdarg.h> 38 #include <stdint.h> 39 #include <stdlib.h> 40 #include <string.h> 41 42 #define testfmt(result, fmt, ...) \ 43 _testfmt((result), __LINE__, #__VA_ARGS__, fmt, __VA_ARGS__) 44 void _testfmt(const char *, int, const char *, const char *, ...); 45 void smash_stack(void); 46 47 int 48 main(int argc, char *argv[]) 49 { 50 /* 51 * Basic tests of decimal output functionality. 52 */ 53 testfmt(" 1.000000E+00", "%13E", 1.0); 54 testfmt(" 1.000000", "%13f", 1.0); 55 testfmt(" 1", "%13G", 1.0); 56 testfmt(" 1.000000E+00", "%13LE", 1.0L); 57 testfmt(" 1.000000", "%13Lf", 1.0L); 58 testfmt(" 1", "%13LG", 1.0L); 59 60 testfmt("2.718282", "%.*f", -2, 2.7182818); 61 62 testfmt("1.234568e+06", "%e", 1234567.8); 63 testfmt("1234567.800000", "%f", 1234567.8); 64 testfmt("1.23457E+06", "%G", 1234567.8); 65 testfmt("1.234568e+06", "%Le", 1234567.8L); 66 testfmt("1234567.800000", "%Lf", 1234567.8L); 67 testfmt("1.23457E+06", "%LG", 1234567.8L); 68 69 #if (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__) 70 testfmt("123456789.864210", "%Lf", 123456789.8642097531L); 71 testfmt("-1.23457E+08", "%LG", -123456789.8642097531L); 72 testfmt("123456789.8642097531", "%.10Lf", 123456789.8642097531L); 73 testfmt(" 3.141592653589793238e-4000", "%L27.18Le", 74 3.14159265358979323846e-4000L); 75 #endif /* (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__) */ 76 77 /* 78 * Infinities and NaNs 79 */ 80 #ifdef NAN 81 testfmt("nan", "%e", NAN); 82 testfmt("NAN", "%F", NAN); 83 testfmt("nan", "%g", NAN); 84 testfmt("NAN", "%LE", (long double)NAN); 85 testfmt(" nan", "%05e", NAN); 86 #endif /* NAN */ 87 88 testfmt("INF", "%E", HUGE_VAL); 89 testfmt("-inf", "%f", -HUGE_VAL); 90 testfmt("+inf", "%+g", HUGE_VAL); 91 testfmt(" inf", "%4.2Le", HUGE_VALL); 92 testfmt("-inf", "%Lf", -HUGE_VALL); 93 testfmt(" inf", "%05e", HUGE_VAL); 94 testfmt(" -inf", "%05e", -HUGE_VAL); 95 96 /* 97 * Padding 98 */ 99 testfmt("0.000000e+00", "%e", 0.0); 100 testfmt("0.000000", "%F", (double)0.0); 101 testfmt("0", "%G", 0.0); 102 testfmt(" 0", "%3.0Lg", 0.0L); 103 testfmt(" 0", "%5.0f", 0.001); 104 105 /* 106 * Precision specifiers 107 */ 108 testfmt("1.0123e+00", "%.4e", 1.0123456789); 109 testfmt("1.0123", "%.4f", 1.0123456789); 110 testfmt("1.012", "%.4g", 1.0123456789); 111 testfmt("1.2346e-02", "%.4e", 0.0123456789); 112 testfmt("0.0123", "%.4f", 0.0123456789); 113 testfmt("0.01235", "%.4g", 0.0123456789); 114 115 /* 116 * Signed conversions 117 */ 118 testfmt("+2.500000e-01", "%+e", 0.25); 119 testfmt("+0.000000", "%+F", 0.0); 120 testfmt("-1", "%+g", -1.0); 121 122 testfmt("-1.000000e+00", "% e", -1.0); 123 testfmt("+1.000000", "% +f", 1.0); 124 testfmt(" 1", "% g", 1.0); 125 testfmt(" 0", "% g", 0.0); 126 127 /* 128 * ``Alternate form'' 129 */ 130 testfmt("1.250e+00", "%#.3e", 1.25); 131 testfmt("123.000000", "%#f", 123.0); 132 testfmt(" 12345.", "%#7.5g", 12345.0); 133 testfmt(" 1.00000", "%#8g", 1.0); 134 testfmt("0.0", "%#.2g", 0.0); 135 136 /* 137 * Padding and decimal point placement 138 */ 139 testfmt("03.2E+00", "%08.1E", 3.25); 140 testfmt("003.25", "%06.2F", 3.25); 141 testfmt("0003.25", "%07.4G", 3.25); 142 143 testfmt("3.14159e-05", "%g", 3.14159e-5); 144 testfmt("0.000314159", "%g", 3.14159e-4); 145 testfmt("3.14159e+06", "%g", 3.14159e6); 146 testfmt("314159", "%g", 3.14159e5); 147 testfmt("314159.", "%#g", 3.14159e5); 148 149 testfmt(" 9.000000e+03", "%13e", 9000.0); 150 testfmt(" 9000.000000", "%12f", 9000.0); 151 testfmt(" 9000", "%5g", 9000.0); 152 testfmt(" 900000.", "%#8g", 900000.0); 153 testfmt(" 9e+06", "%6g", 9000000.0); 154 testfmt(" 9.000000e-04", "%13e", 0.0009); 155 testfmt(" 0.000900", "%9f", 0.0009); 156 testfmt(" 0.0009", "%7g", 0.0009); 157 testfmt(" 9e-05", "%6g", 0.00009); 158 testfmt(" 9.00000e-05", "%#12g", 0.00009); 159 testfmt(" 9.e-05", "%#7.1g", 0.00009); 160 161 testfmt(" 0.0", "%4.1f", 0.0); 162 testfmt("90.0", "%4.1f", 90.0); 163 testfmt(" 100", "%4.0f", 100.0); 164 testfmt("9.0e+01", "%4.1e", 90.0); 165 testfmt("1e+02", "%4.0e", 100.0); 166 167 /* 168 * Hexadecimal floating point (%a, %A) tests. Some of these 169 * are only valid if the implementation converts to hex digits 170 * on nibble boundaries. 171 */ 172 testfmt("0x0p+0", "%a", 0x0.0p0); 173 testfmt("0X0.P+0", "%#LA", 0x0.0p0L); 174 #ifdef NAN 175 testfmt("inf", "%La", (long double)INFINITY); 176 testfmt("+INF", "%+A", INFINITY); 177 testfmt("nan", "%La", (long double)NAN); 178 testfmt("NAN", "%A", NAN); 179 #endif /* NAN */ 180 181 testfmt(" 0x1.23p+0", "%10a", 0x1.23p0); 182 testfmt(" 0x1.23p-500", "%12a", 0x1.23p-500); 183 testfmt(" 0x1.2p+40", "%10.1a", 0x1.23p40); 184 testfmt(" 0X1.230000000000000000000000P-4", "%32.24A", 0x1.23p-4); 185 testfmt("0x1p-1074", "%a", 0x1p-1074); 186 testfmt("0x1.2345p-1024", "%a", 0x1.2345p-1024); 187 188 return (0); 189 } 190 191 void 192 smash_stack(void) 193 { 194 static uint32_t junk = 0xdeadbeef; 195 uint32_t buf[512]; 196 int i; 197 198 for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) 199 buf[i] = junk; 200 } 201 202 void 203 _testfmt(const char *result, int line, const char *argstr, const char *fmt,...) 204 { 205 char s[100]; 206 va_list ap; 207 208 va_start(ap, fmt); 209 smash_stack(); 210 vsnprintf(s, sizeof(s), fmt, ap); 211 if (strcmp(result, s) != 0) { 212 fprintf(stderr, 213 "%d: printf(\"%s\", %s) ==> [%s], expected [%s]\n", 214 line, fmt, argstr, s, result); 215 abort(); 216 } 217 } 218