test-printfloat.c revision 174205
1330304Srpokala/*- 2330304Srpokala * Copyright (c) 2002, 2005 David Schultz <das@FreeBSD.org> 3330304Srpokala * All rights reserved. 4330304Srpokala * 5330304Srpokala * Redistribution and use in source and binary forms, with or without 6330304Srpokala * modification, are permitted provided that the following conditions 7330304Srpokala * are met: 8330304Srpokala * 1. Redistributions of source code must retain the above copyright 9330304Srpokala * notice, this list of conditions and the following disclaimer. 10330304Srpokala * 2. Redistributions in binary form must reproduce the above copyright 11330304Srpokala * notice, this list of conditions and the following disclaimer in the 12330304Srpokala * documentation and/or other materials provided with the distribution. 13330304Srpokala * 14330304Srpokala * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15330304Srpokala * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16330304Srpokala * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17330304Srpokala * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18330304Srpokala * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19330304Srpokala * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20330304Srpokala * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21330304Srpokala * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22330304Srpokala * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23330304Srpokala * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24330304Srpokala * SUCH DAMAGE. 25330304Srpokala */ 26330304Srpokala 27330304Srpokala/* 28330304Srpokala * Test for printf() floating point formats. 29330304Srpokala */ 30330304Srpokala 31330304Srpokala#include <sys/cdefs.h> 32330304Srpokala__FBSDID("$FreeBSD: head/tools/regression/lib/libc/stdio/test-printfloat.c 174205 2007-12-03 07:18:47Z das $"); 33330304Srpokala 34330304Srpokala#include <assert.h> 35330304Srpokala#include <err.h> 36330304Srpokala#include <fenv.h> 37330304Srpokala#include <float.h> 38330304Srpokala#include <locale.h> 39330304Srpokala#include <math.h> 40330304Srpokala#include <stdio.h> 41330304Srpokala#include <stdarg.h> 42330304Srpokala#include <stdint.h> 43330304Srpokala#include <stdlib.h> 44330304Srpokala#include <string.h> 45330304Srpokala 46330304Srpokala#define testfmt(result, fmt, ...) \ 47330304Srpokala _testfmt((result), __LINE__, #__VA_ARGS__, fmt, __VA_ARGS__) 48330304Srpokalavoid _testfmt(const char *, int, const char *, const char *, ...); 49330304Srpokalavoid smash_stack(void); 50330304Srpokala 51330304Srpokalaint 52330304Srpokalamain(int argc, char *argv[]) 53330304Srpokala{ 54330304Srpokala 55330304Srpokala printf("1..11\n"); 56330304Srpokala assert(setlocale(LC_NUMERIC, "")); 57330304Srpokala 58330304Srpokala /* 59330304Srpokala * Basic tests of decimal output functionality. 60330304Srpokala */ 61330304Srpokala testfmt(" 1.000000E+00", "%13E", 1.0); 62330304Srpokala testfmt(" 1.000000", "%13f", 1.0); 63330304Srpokala testfmt(" 1", "%13G", 1.0); 64330304Srpokala testfmt(" 1.000000E+00", "%13LE", 1.0L); 65330304Srpokala testfmt(" 1.000000", "%13Lf", 1.0L); 66330304Srpokala testfmt(" 1", "%13LG", 1.0L); 67330304Srpokala 68330304Srpokala testfmt("2.718282", "%.*f", -2, 2.7182818); 69330304Srpokala 70330304Srpokala testfmt("1.234568e+06", "%e", 1234567.8); 71330304Srpokala testfmt("1234567.800000", "%f", 1234567.8); 72330304Srpokala testfmt("1.23457E+06", "%G", 1234567.8); 73330304Srpokala testfmt("1.234568e+06", "%Le", 1234567.8L); 74330304Srpokala testfmt("1234567.800000", "%Lf", 1234567.8L); 75330304Srpokala testfmt("1.23457E+06", "%LG", 1234567.8L); 76330304Srpokala 77330304Srpokala#if (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__) 78330304Srpokala testfmt("123456789.864210", "%Lf", 123456789.8642097531L); 79330304Srpokala testfmt("-1.23457E+08", "%LG", -123456789.8642097531L); 80330304Srpokala testfmt("123456789.8642097531", "%.10Lf", 123456789.8642097531L); 81330304Srpokala testfmt(" 3.141592653589793238e-4000", "%L27.18Le", 82330304Srpokala 3.14159265358979323846e-4000L); 83330304Srpokala#endif 84330304Srpokala 85330304Srpokala printf("ok 1 - printfloat\n"); 86330304Srpokala 87330304Srpokala /* 88330304Srpokala * Infinities and NaNs 89330304Srpokala */ 90330304Srpokala testfmt("nan", "%e", NAN); 91330304Srpokala testfmt("NAN", "%F", NAN); 92330304Srpokala testfmt("nan", "%g", NAN); 93330304Srpokala testfmt("NAN", "%LE", (long double)NAN); 94330304Srpokala testfmt(" nan", "%05e", NAN); 95330304Srpokala 96330304Srpokala testfmt("INF", "%E", HUGE_VAL); 97330304Srpokala testfmt("-inf", "%f", -HUGE_VAL); 98330304Srpokala testfmt("+inf", "%+g", HUGE_VAL); 99330304Srpokala testfmt(" inf", "%4.2Le", HUGE_VALL); 100330304Srpokala testfmt("-inf", "%Lf", -HUGE_VALL); 101330304Srpokala testfmt(" inf", "%05e", HUGE_VAL); 102330304Srpokala testfmt(" -inf", "%05e", -HUGE_VAL); 103330304Srpokala 104330304Srpokala printf("ok 2 - printfloat\n"); 105330304Srpokala 106330304Srpokala /* 107330304Srpokala * Padding 108330304Srpokala */ 109330304Srpokala testfmt("0.000000e+00", "%e", 0.0); 110330304Srpokala testfmt("0.000000", "%F", (double)0.0); 111330304Srpokala testfmt("0", "%G", 0.0); 112330304Srpokala testfmt(" 0", "%3.0Lg", 0.0L); 113330304Srpokala testfmt(" 0", "%5.0f", 0.001); 114330304Srpokala printf("ok 3 - printfloat\n"); 115330304Srpokala 116330304Srpokala /* 117330304Srpokala * Precision specifiers 118330304Srpokala */ 119330304Srpokala testfmt("1.0123e+00", "%.4e", 1.0123456789); 120330304Srpokala testfmt("1.0123", "%.4f", 1.0123456789); 121330304Srpokala testfmt("1.012", "%.4g", 1.0123456789); 122330304Srpokala testfmt("1.2346e-02", "%.4e", 0.0123456789); 123330304Srpokala testfmt("0.0123", "%.4f", 0.0123456789); 124330304Srpokala testfmt("0.01235", "%.4g", 0.0123456789); 125330304Srpokala printf("ok 4 - printfloat\n"); 126330304Srpokala 127330304Srpokala /* 128330304Srpokala * Thousands' separators and other locale fun 129330304Srpokala */ 130330304Srpokala testfmt("12345678.0625", "%'.04f", 12345678.0625); 131330304Srpokala testfmt("0012345678.0625", "%'015.4F", 12345678.0625); 132330304Srpokala 133330304Srpokala assert(setlocale(LC_NUMERIC, "hi_IN.ISCII-DEV")); /* grouping == 2;3 */ 134 testfmt("123,456,78.0625", "%'.4f", 12345678.0625); 135 testfmt("00123,456,78.0625", "%'017.4F", 12345678.0625); 136 testfmt(" 90,00", "%'6.0f", 9000.0); 137 testfmt("90,00.0", "%'.1f", 9000.0); 138 139 assert(setlocale(LC_NUMERIC, "ru_RU.ISO8859-5")); /* decimalpoint==, */ 140 testfmt("3,1415", "%g", 3.1415); 141 142 /* thousands=. decimalpoint=, grouping=3;3 */ 143 assert(setlocale(LC_NUMERIC, "el_GR.ISO8859-7")); /* decimalpoint==, */ 144 testfmt("1.234,00", "%'.2f", 1234.00); 145 testfmt("123.456,789", "%'.3f", 123456.789); 146 147 assert(setlocale(LC_NUMERIC, "")); 148 testfmt("12345678.062500", "%'f", 12345678.0625); 149 testfmt("9000.000000", "%'f", 9000.0); 150 151 printf("ok 5 - printfloat\n"); 152 153 /* 154 * Signed conversions 155 */ 156 testfmt("+2.500000e-01", "%+e", 0.25); 157 testfmt("+0.000000", "%+F", 0.0); 158 testfmt("-1", "%+g", -1.0); 159 160 testfmt("-1.000000e+00", "% e", -1.0); 161 testfmt("+1.000000", "% +f", 1.0); 162 testfmt(" 1", "% g", 1.0); 163 testfmt(" 0", "% g", 0.0); 164 165 printf("ok 6 - printfloat\n"); 166 167 /* 168 * ``Alternate form'' 169 */ 170 testfmt("1.250e+00", "%#.3e", 1.25); 171 testfmt("123.000000", "%#f", 123.0); 172 testfmt(" 12345.", "%#7.5g", 12345.0); 173 testfmt(" 1.00000", "%#8g", 1.0); 174 testfmt("0.0", "%#.2g", 0.0); 175 printf("ok 7 - printfloat\n"); 176 177 /* 178 * Padding and decimal point placement 179 */ 180 testfmt("03.2E+00", "%08.1E", 3.25); 181 testfmt("003.25", "%06.2F", 3.25); 182 testfmt("0003.25", "%07.4G", 3.25); 183 184 testfmt("3.14159e-05", "%g", 3.14159e-5); 185 testfmt("0.000314159", "%g", 3.14159e-4); 186 testfmt("3.14159e+06", "%g", 3.14159e6); 187 testfmt("314159", "%g", 3.14159e5); 188 testfmt("314159.", "%#g", 3.14159e5); 189 190 testfmt(" 9.000000e+03", "%13e", 9000.0); 191 testfmt(" 9000.000000", "%12f", 9000.0); 192 testfmt(" 9000", "%5g", 9000.0); 193 testfmt(" 900000.", "%#8g", 900000.0); 194 testfmt(" 9e+06", "%6g", 9000000.0); 195 testfmt(" 9.000000e-04", "%13e", 0.0009); 196 testfmt(" 0.000900", "%9f", 0.0009); 197 testfmt(" 0.0009", "%7g", 0.0009); 198 testfmt(" 9e-05", "%6g", 0.00009); 199 testfmt(" 9.00000e-05", "%#12g", 0.00009); 200 testfmt(" 9.e-05", "%#7.1g", 0.00009); 201 202 testfmt(" 0.0", "%4.1f", 0.0); 203 testfmt("90.0", "%4.1f", 90.0); 204 testfmt(" 100", "%4.0f", 100.0); 205 testfmt("9.0e+01", "%4.1e", 90.0); 206 testfmt("1e+02", "%4.0e", 100.0); 207 208 printf("ok 8 - printfloat\n"); 209 210 /* 211 * Decimal rounding 212 */ 213 fesetround(FE_DOWNWARD); 214 testfmt("4.437", "%.3f", 4.4375); 215 testfmt("-4.438", "%.3f", -4.4375); 216 testfmt("4.437", "%.3Lf", 4.4375L); 217 testfmt("-4.438", "%.3Lf", -4.4375L); 218 219 fesetround(FE_UPWARD); 220 testfmt("4.438", "%.3f", 4.4375); 221 testfmt("-4.437", "%.3f", -4.4375); 222 testfmt("4.438", "%.3Lf", 4.4375L); 223 testfmt("-4.437", "%.3Lf", -4.4375L); 224 225 fesetround(FE_TOWARDZERO); 226 testfmt("4.437", "%.3f", 4.4375); 227 testfmt("-4.437", "%.3f", -4.4375); 228 testfmt("4.437", "%.3Lf", 4.4375L); 229 testfmt("-4.437", "%.3Lf", -4.4375L); 230 231 fesetround(FE_TONEAREST); 232 testfmt("4.438", "%.3f", 4.4375); 233 testfmt("-4.438", "%.3f", -4.4375); 234 testfmt("4.438", "%.3Lf", 4.4375L); 235 testfmt("-4.438", "%.3Lf", -4.4375L); 236 237 printf("ok 9 - printfloat\n"); 238 239 /* 240 * Hexadecimal floating point (%a, %A) tests. Some of these 241 * are only valid if the implementation converts to hex digits 242 * on nibble boundaries. 243 */ 244 testfmt("0x0p+0", "%a", 0x0.0p0); 245 testfmt("0X0.P+0", "%#LA", 0x0.0p0L); 246 testfmt("inf", "%La", (long double)INFINITY); 247 testfmt("+INF", "%+A", INFINITY); 248 testfmt("nan", "%La", (long double)NAN); 249 testfmt("NAN", "%A", NAN); 250 251 testfmt(" 0x1.23p+0", "%10a", 0x1.23p0); 252 testfmt(" 0x1.23p-500", "%12a", 0x1.23p-500); 253 testfmt(" 0x1.2p+40", "%10.1a", 0x1.23p40); 254 testfmt(" 0X1.230000000000000000000000P-4", "%32.24A", 0x1.23p-4); 255 testfmt("0x1p-1074", "%a", 0x1p-1074); 256 testfmt("0x1.2345p-1024", "%a", 0x1.2345p-1024); 257 258#if (LDBL_MANT_DIG == 64) && !defined(__i386__) 259 testfmt("0xc.90fdaa22168c234p-2", "%La", 0x3.243f6a8885a308dp0L); 260 testfmt("0x8p-16448", "%La", 0x1p-16445L); 261 testfmt("0x9.8765p-16384", "%La", 0x9.8765p-16384L); 262#elif (LDBL_MANT_DIG == 113) 263 testfmt("0x1.921fb54442d18469898cc51701b8p+1", "%La", 264 0x3.243f6a8885a308d313198a2e037p0L); 265 testfmt("0x1p-16494", "%La", 0x1p-16494L); 266 testfmt("0x1.2345p-16384", "%La", 0x1.2345p-16384L); 267#else 268 testfmt("0xc.90fdaa22168cp-2", "%La", 0x3.243f6a8885a31p0L); 269 testfmt("0x8p-1077", "%La", 0x1p-1074L); 270 testfmt("0x9.8765p-1024", "%La", 0x9.8765p-1024L); 271#endif 272 273 printf("ok 10 - printfloat\n"); 274 275 /* 276 * Hexadecimal rounding 277 */ 278 fesetround(FE_TOWARDZERO); 279 testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0); 280 testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0); 281 testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0); 282 testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0); 283 testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0); 284 285 fesetround(FE_DOWNWARD); 286 testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0); 287 testfmt("-0x1.23457p+0", "%.5a", -0x1.23456789abcdep0); 288 testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0); 289 testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0); 290 testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0); 291 292 fesetround(FE_UPWARD); 293 testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0); 294 testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0); 295 testfmt("0x1.23457p+0", "%.5a", 0x1.23456789abcdep0); 296 testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0); 297 testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0); 298 299 fesetround(FE_TONEAREST); 300 testfmt("0x1.23456789abcdep+4", "%a", 0x1.23456789abcdep4); 301 testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0); 302 testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0); 303 testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0); 304 testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0); 305 testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0); 306 testfmt("0x2.00p-1030", "%.2a", 0x1.fffp-1030); 307 testfmt("0x2.00p-1027", "%.2a", 0xf.fffp-1030); 308 testfmt("0x1.83p+0", "%.2a", 1.51); 309 310 printf("ok 11 - printfloat\n"); 311 312 return (0); 313} 314 315void 316smash_stack(void) 317{ 318 static uint32_t junk = 0xdeadbeef; 319 uint32_t buf[512]; 320 int i; 321 322 for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) 323 buf[i] = junk; 324} 325 326void 327_testfmt(const char *result, int line, const char *argstr, const char *fmt,...) 328{ 329 char s[100]; 330 va_list ap; 331 332 va_start(ap, fmt); 333 smash_stack(); 334 vsnprintf(s, sizeof(s), fmt, ap); 335 if (strcmp(result, s) != 0) { 336 fprintf(stderr, 337 "%d: printf(\"%s\", %s) ==> [%s], expected [%s]\n", 338 line, fmt, argstr, s, result); 339 abort(); 340 } 341} 342