printfloat_test.c revision 113192
1/*- 2 * Copyright (c) 2002 David Schultz <das@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27/* 28 * Test for printf() floating point formats. 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: head/tools/regression/lib/libc/stdio/test-printfloat.c 113192 2003-04-07 00:42:49Z das $"); 33 34#include <assert.h> 35#include <err.h> 36#include <float.h> 37#include <locale.h> 38#include <math.h> 39#include <stdio.h> 40#include <stdarg.h> 41#include <stdint.h> 42#include <stdlib.h> 43#include <string.h> 44 45#define testfmt(result, fmt, ...) \ 46 _testfmt((result), __LINE__, #__VA_ARGS__, fmt, __VA_ARGS__) 47void _testfmt(const char *, int, const char *, const char *, ...); 48void smash_stack(void); 49 50int 51main(int argc, char *argv[]) 52{ 53 54 assert(setlocale(LC_NUMERIC, "")); 55 56 testfmt(" 1.000000E+00", "%13E", 1.0); 57 testfmt(" 1.000000", "%13f", 1.0); 58 testfmt(" 1", "%13G", 1.0); 59 testfmt(" 1.000000E+00", "%13LE", 1.0L); 60 testfmt(" 1.000000", "%13Lf", 1.0L); 61 testfmt(" 1", "%13LG", 1.0L); 62 63 testfmt("2.718282", "%.*f", -2, 2.7182818); 64 65 testfmt("1.234568e+06", "%e", 1234567.8); 66 testfmt("1234567.800000", "%f", 1234567.8); 67 testfmt("1.23457E+06", "%G", 1234567.8); 68 testfmt("1.234568e+06", "%Le", 1234567.8L); 69 testfmt("1234567.800000", "%Lf", 1234567.8L); 70 testfmt("1.23457E+06", "%LG", 1234567.8L); 71 72#if LDBL_MANT_DIG > DBL_MANT_DIG 73 testfmt("123456789.864210", "%Lf", 123456789.8642097531L); 74 testfmt("-1.23457E+08", "%LG", -123456789.8642097531L); 75 testfmt("123456789.8642097531", "%.10Lf", 123456789.8642097531L); 76 testfmt(" 3.141592653589793238e-4000", "%L27.18Le", 77 3.14159265358979323846e-4000L); 78#endif 79 80 testfmt("nan", "%e", NAN); 81 testfmt("NAN", "%F", NAN); 82 testfmt("nan", "%g", NAN); 83 testfmt("NAN", "%LE", (long double)NAN); 84 85 testfmt("INF", "%E", HUGE_VAL); 86 testfmt("-inf", "%f", -HUGE_VAL); 87 testfmt("+inf", "%+g", HUGE_VAL); 88 testfmt(" inf", "%4.2Le", HUGE_VALL); 89 testfmt("-inf", "%Lf", -HUGE_VALL); 90 91 testfmt("0.000000e+00", "%e", 0.0); 92 testfmt("0.000000", "%F", (double)0.0); 93 testfmt("0", "%G", 0.0); 94 testfmt(" 0", "%3.0Lg", 0.0L); 95 96 testfmt("1.0123e+00", "%.4e", 1.0123456789); 97 testfmt("1.0123", "%.4f", 1.0123456789); 98 testfmt("1.012", "%.4g", 1.0123456789); 99 testfmt("1.2346e-02", "%.4e", 0.0123456789); 100 testfmt("0.0123", "%.4f", 0.0123456789); 101 testfmt("0.01235", "%.4g", 0.0123456789); 102 103 testfmt("12345678.0625", "%'.04f", 12345678.0625); 104 testfmt("0012345678.0625", "%'015.4F", 12345678.0625); 105 106 assert(setlocale(LC_NUMERIC, "hi_IN.ISCII-DEV")); /* grouping == 2;3 */ 107 testfmt("123,456,78.0625", "%'.4f", 12345678.0625); 108 testfmt("00123,456,78.0625", "%'017.4F", 12345678.0625); 109 testfmt(" 90,00", "%'6.0f", 9000.0); 110 testfmt("90,00.0", "%'.1f", 9000.0); 111 assert(setlocale(LC_NUMERIC, "ru_RU.ISO8859-5")); /* decimalpoint==, */ 112 testfmt("3,1415", "%g", 3.1415); 113 assert(setlocale(LC_NUMERIC, "")); 114 testfmt("12345678.062500", "%'f", 12345678.0625); 115 testfmt("9000.000000", "%'f", 9000.0); 116 117 testfmt("+2.500000e-01", "%+e", 0.25); 118 testfmt("+0.000000", "%+F", 0.0); 119 testfmt("-1", "%+g", -1.0); 120 121 testfmt("-1.000000e+00", "% e", -1.0); 122 testfmt("+1.000000", "% +f", 1.0); 123 testfmt(" 1", "% g", 1.0); 124 testfmt(" 0", "% g", 0.0); 125 126 testfmt("1.250e+00", "%#.3e", 1.25); 127 testfmt("123.000000", "%#f", 123.0); 128 testfmt(" 12345.", "%#7.5g", 12345.0); 129 testfmt(" 1.00000", "%#8g", 1.0); 130 testfmt("0.0", "%#.2g", 0.0); 131 132 testfmt("03.2E+00", "%08.1E", 3.25); 133 testfmt("003.25", "%06.2F", 3.25); 134 testfmt("0003.25", "%07.4G", 3.25); 135 136 testfmt("3.14159e-05", "%g", 3.14159e-5); 137 testfmt("0.000314159", "%g", 3.14159e-4); 138 testfmt("3.14159e+06", "%g", 3.14159e6); 139 testfmt("314159", "%g", 3.14159e5); 140 testfmt("314159.", "%#g", 3.14159e5); 141 142 testfmt(" 0.0", "%4.1f", 0.0); 143 testfmt("90.0", "%4.1f", 90.0); 144 testfmt(" 100", "%4.0f", 100.0); 145 testfmt("9.0e+01", "%4.1e", 90.0); 146 testfmt("1e+02", "%4.0e", 100.0); 147 148 printf("PASS printfloat\n"); 149 150 return (0); 151} 152 153void 154smash_stack(void) 155{ 156 static uint32_t junk = 0xdeadbeef; 157 uint32_t buf[512]; 158 int i; 159 160 for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) 161 buf[i] = junk; 162} 163 164void 165_testfmt(const char *result, int line, const char *argstr, const char *fmt,...) 166{ 167 char s[100]; 168 va_list ap; 169 170 va_start(ap, fmt); 171 smash_stack(); 172 vsnprintf(s, sizeof(s), fmt, ap); 173 if (strcmp(result, s) != 0) { 174 fprintf(stderr, 175 "%d: printf(\"%s\", %s) ==> [%s], expected [%s]\n", 176 line, fmt, argstr, s, result); 177 abort(); 178 } 179} 180