Path: blob/main/lib/libc/tests/stdio/printfloat_test.c
39530 views
/*-1* Copyright (c) 2002-2009 David Schultz <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Test for printf() floating point formats.28*/2930#include <err.h>31#include <fenv.h>32#include <float.h>33#include <locale.h>34#include <math.h>35#include <stdio.h>36#include <stdarg.h>37#include <stdint.h>38#include <stdlib.h>39#include <string.h>40#include <wchar.h>4142#include <atf-c.h>4344static void45smash_stack(void)46{47static uint32_t junk = 0xdeadbeef;48uint32_t buf[512];49size_t i;5051for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)52buf[i] = junk;53}5455#define testfmt(result, fmt, ...) \56_testfmt((result), #__VA_ARGS__, fmt, __VA_ARGS__)57static void58_testfmt(const char *result, const char *argstr, const char *fmt,...)59{60#define BUF 10061wchar_t ws[BUF], wfmt[BUF], wresult[BUF];62char s[BUF];63va_list ap, ap2;6465va_start(ap, fmt);66va_copy(ap2, ap);67smash_stack();68vsnprintf(s, sizeof(s), fmt, ap);69ATF_CHECK_MSG(strcmp(result, s) == 0,70"printf(\"%s\", %s) ==> [%s], expected [%s]",71fmt, argstr, s, result);7273smash_stack();74mbstowcs(ws, s, BUF - 1);75mbstowcs(wfmt, fmt, BUF - 1);76mbstowcs(wresult, result, BUF - 1);77vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2);78ATF_CHECK_MSG(wcscmp(wresult, ws) == 0,79"wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]",80wfmt, argstr, ws, wresult);8182va_end(ap);83va_end(ap2);84}8586ATF_TC_WITHOUT_HEAD(float_within_limits);87ATF_TC_BODY(float_within_limits, tc)88{8990ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));9192/* Basic tests of decimal output functionality. */93testfmt(" 1.000000E+00", "%13E", 1.0);94testfmt(" 1.000000", "%13f", 1.0);95testfmt(" 1", "%13G", 1.0);96testfmt(" 1.000000E+00", "%13LE", 1.0L);97testfmt(" 1.000000", "%13Lf", 1.0L);98testfmt(" 1", "%13LG", 1.0L);99100testfmt("2.718282", "%.*f", -2, 2.7182818);101102testfmt("1.234568e+06", "%e", 1234567.8);103testfmt("1234567.800000", "%f", 1234567.8);104testfmt("1.23457E+06", "%G", 1234567.8);105testfmt("1.234568e+06", "%Le", 1234567.8L);106testfmt("1234567.800000", "%Lf", 1234567.8L);107testfmt("1.23457E+06", "%LG", 1234567.8L);108109#if (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__)110testfmt("123456789.864210", "%Lf", 123456789.8642097531L);111testfmt("-1.23457E+08", "%LG", -123456789.8642097531L);112testfmt("123456789.8642097531", "%.10Lf", 123456789.8642097531L);113testfmt(" 3.141592653589793238e-4000", "%L27.18Le",1143.14159265358979323846e-4000L);115#endif116}117118ATF_TC_WITHOUT_HEAD(infinities_and_nans);119ATF_TC_BODY(infinities_and_nans, tc)120{121122ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));123124testfmt("nan", "%e", NAN);125testfmt("NAN", "%F", NAN);126testfmt("nan", "%g", NAN);127testfmt("NAN", "%LE", (long double)NAN);128testfmt(" nan", "%05e", NAN);129130testfmt("INF", "%E", HUGE_VAL);131testfmt("-inf", "%f", -HUGE_VAL);132testfmt("+inf", "%+g", HUGE_VAL);133testfmt(" inf", "%4.2Le", HUGE_VALL);134testfmt("-inf", "%Lf", -HUGE_VALL);135testfmt(" inf", "%05e", HUGE_VAL);136testfmt(" -inf", "%05e", -HUGE_VAL);137}138139ATF_TC_WITHOUT_HEAD(padding);140ATF_TC_BODY(padding, tc)141{142143ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));144145testfmt("0.000000e+00", "%e", 0.0);146testfmt("0.000000", "%F", (double)0.0);147testfmt("0", "%G", 0.0);148testfmt(" 0", "%3.0Lg", 0.0L);149testfmt(" 0", "%5.0f", 0.001);150}151152ATF_TC_WITHOUT_HEAD(precision_specifiers);153ATF_TC_BODY(precision_specifiers, tc)154{155156ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));157158testfmt("1.0123e+00", "%.4e", 1.0123456789);159testfmt("1.0123", "%.4f", 1.0123456789);160testfmt("1.012", "%.4g", 1.0123456789);161testfmt("1.2346e-02", "%.4e", 0.0123456789);162testfmt("0.0123", "%.4f", 0.0123456789);163testfmt("0.01235", "%.4g", 0.0123456789);164}165166ATF_TC_WITHOUT_HEAD(thousands_separator_and_other_locale_tests);167ATF_TC_BODY(thousands_separator_and_other_locale_tests, tc)168{169170ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));171172testfmt("12345678.0625", "%'.04f", 12345678.0625);173testfmt("0012345678.0625", "%'015.4F", 12345678.0625);174175ATF_REQUIRE(setlocale(LC_NUMERIC, "hi_IN.ISCII-DEV")); /* grouping == 2;3 */176testfmt("1,23,45,678.0625", "%'.4f", 12345678.0625);177testfmt("01,23,45,678.0625", "%'017.4F", 12345678.0625);178testfmt(" 9,000", "%'6.0f", 9000.0);179testfmt("9,000.0", "%'.1f", 9000.0);180181ATF_REQUIRE(setlocale(LC_NUMERIC, "ru_RU.ISO8859-5")); /* decimalpoint==, */182testfmt("3,1415", "%g", 3.1415);183184/* thousands=. decimalpoint=, grouping=3;3 */185ATF_REQUIRE(setlocale(LC_NUMERIC, "el_GR.ISO8859-7")); /* decimalpoint==, */186testfmt("1.234,00", "%'.2f", 1234.00);187testfmt("123.456,789", "%'.3f", 123456.789);188189ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));190testfmt("12345678.062500", "%'f", 12345678.0625);191testfmt("9000.000000", "%'f", 9000.0);192}193194ATF_TC_WITHOUT_HEAD(signed_conversions);195ATF_TC_BODY(signed_conversions, tc)196{197198ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));199200testfmt("+2.500000e-01", "%+e", 0.25);201testfmt("+0.000000", "%+F", 0.0);202testfmt("-1", "%+g", -1.0);203204testfmt("-1.000000e+00", "% e", -1.0);205testfmt("+1.000000", "% +f", 1.0);206testfmt(" 1", "% g", 1.0);207testfmt(" 0", "% g", 0.0);208}209210ATF_TC_WITHOUT_HEAD(alternate_form);211ATF_TC_BODY(alternate_form, tc)212{213214ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));215216testfmt("1.250e+00", "%#.3e", 1.25);217testfmt("123.000000", "%#f", 123.0);218testfmt(" 12345.", "%#7.5g", 12345.0);219testfmt(" 1.00000", "%#8g", 1.0);220testfmt("0.0", "%#.2g", 0.0);221}222223ATF_TC_WITHOUT_HEAD(padding_and_decimal_point_placement);224ATF_TC_BODY(padding_and_decimal_point_placement, tc)225{226227ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));228229testfmt("03.2E+00", "%08.1E", 3.25);230testfmt("003.25", "%06.2F", 3.25);231testfmt("0003.25", "%07.4G", 3.25);232233testfmt("3.14159e-05", "%g", 3.14159e-5);234testfmt("0.000314159", "%g", 3.14159e-4);235testfmt("3.14159e+06", "%g", 3.14159e6);236testfmt("314159", "%g", 3.14159e5);237testfmt("314159.", "%#g", 3.14159e5);238239testfmt(" 9.000000e+03", "%13e", 9000.0);240testfmt(" 9000.000000", "%12f", 9000.0);241testfmt(" 9000", "%5g", 9000.0);242testfmt(" 900000.", "%#8g", 900000.0);243testfmt(" 9e+06", "%6g", 9000000.0);244testfmt(" 9.000000e-04", "%13e", 0.0009);245testfmt(" 0.000900", "%9f", 0.0009);246testfmt(" 0.0009", "%7g", 0.0009);247testfmt(" 9e-05", "%6g", 0.00009);248testfmt(" 9.00000e-05", "%#12g", 0.00009);249testfmt(" 9.e-05", "%#7.1g", 0.00009);250251testfmt(" 0.0", "%4.1f", 0.0);252testfmt("90.0", "%4.1f", 90.0);253testfmt(" 100", "%4.0f", 100.0);254testfmt("9.0e+01", "%4.1e", 90.0);255testfmt("1e+02", "%4.0e", 100.0);256}257258ATF_TC_WITHOUT_HEAD(decimal_rounding);259ATF_TC_BODY(decimal_rounding, tc)260{261262ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));263264fesetround(FE_DOWNWARD);265testfmt("4.437", "%.3f", 4.4375);266testfmt("-4.438", "%.3f", -4.4375);267testfmt("4.437", "%.3Lf", 4.4375L);268testfmt("-4.438", "%.3Lf", -4.4375L);269270fesetround(FE_UPWARD);271testfmt("4.438", "%.3f", 4.4375);272testfmt("-4.437", "%.3f", -4.4375);273testfmt("4.438", "%.3Lf", 4.4375L);274testfmt("-4.437", "%.3Lf", -4.4375L);275276fesetround(FE_TOWARDZERO);277testfmt("4.437", "%.3f", 4.4375);278testfmt("-4.437", "%.3f", -4.4375);279testfmt("4.437", "%.3Lf", 4.4375L);280testfmt("-4.437", "%.3Lf", -4.4375L);281282fesetround(FE_TONEAREST);283testfmt("4.438", "%.3f", 4.4375);284testfmt("-4.438", "%.3f", -4.4375);285testfmt("4.438", "%.3Lf", 4.4375L);286testfmt("-4.438", "%.3Lf", -4.4375L);287}288289ATF_TC_WITHOUT_HEAD(hexadecimal_floating_point);290ATF_TC_BODY(hexadecimal_floating_point, tc)291{292293ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));294295/*296* Hexadecimal floating point (%a, %A) tests. Some of these297* are only valid if the implementation converts to hex digits298* on nibble boundaries.299*/300testfmt("0x0p+0", "%a", 0x0.0p0);301testfmt("0X0.P+0", "%#LA", 0x0.0p0L);302testfmt("inf", "%La", (long double)INFINITY);303testfmt("+INF", "%+A", INFINITY);304testfmt("nan", "%La", (long double)NAN);305testfmt("NAN", "%A", NAN);306307testfmt(" 0x1.23p+0", "%10a", 0x1.23p0);308testfmt(" 0x1.23p-500", "%12a", 0x1.23p-500);309testfmt(" 0x1.2p+40", "%10.1a", 0x1.23p40);310testfmt(" 0X1.230000000000000000000000P-4", "%32.24A", 0x1.23p-4);311testfmt("0x1p-1074", "%a", 0x1p-1074);312testfmt("0x1.2345p-1024", "%a", 0x1.2345p-1024);313314#if (LDBL_MANT_DIG == 64)315testfmt("0x1.921fb54442d18468p+1", "%La", 0x3.243f6a8885a308dp0L);316testfmt("0x1p-16445", "%La", 0x1p-16445L);317testfmt("0x1.30ecap-16381", "%La", 0x9.8765p-16384L);318#elif (LDBL_MANT_DIG == 113)319testfmt("0x1.921fb54442d18469898cc51701b8p+1", "%La",3200x3.243f6a8885a308d313198a2e037p0L);321testfmt("0x1p-16494", "%La", 0x1p-16494L);322testfmt("0x1.2345p-16384", "%La", 0x1.2345p-16384L);323#else324testfmt("0x1.921fb54442d18p+1", "%La", 0x3.243f6a8885a31p0L);325testfmt("0x1p-1074", "%La", 0x1p-1074L);326testfmt("0x1.30ecap-1021", "%La", 0x9.8765p-1024L);327#endif328}329330ATF_TC_WITHOUT_HEAD(hexadecimal_rounding);331ATF_TC_BODY(hexadecimal_rounding, tc)332{333334ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));335336fesetround(FE_TOWARDZERO);337testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0);338testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);339testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);340testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0);341testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0);342343fesetround(FE_DOWNWARD);344testfmt("0X1.23456789ABCP+0", "%.11A", 0x1.23456789abcdep0);345testfmt("-0x1.23457p+0", "%.5a", -0x1.23456789abcdep0);346testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);347testfmt("0x1.234567p+0", "%.6a", 0x1.23456789abcdep0);348testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0);349350fesetround(FE_UPWARD);351testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0);352testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);353testfmt("0x1.23457p+0", "%.5a", 0x1.23456789abcdep0);354testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0);355testfmt("-0x1.234566p+0", "%.6a", -0x1.23456689abcdep0);356357fesetround(FE_TONEAREST);358testfmt("0x1.23456789abcdep+4", "%a", 0x1.23456789abcdep4);359testfmt("0X1.23456789ABDP+0", "%.11A", 0x1.23456789abcdep0);360testfmt("-0x1.23456p+0", "%.5a", -0x1.23456789abcdep0);361testfmt("0x1.23456p+0", "%.5a", 0x1.23456789abcdep0);362testfmt("0x1.234568p+0", "%.6a", 0x1.23456789abcdep0);363testfmt("-0x1.234567p+0", "%.6a", -0x1.23456689abcdep0);364testfmt("0x1.00p-1029", "%.2a", 0x1.fffp-1030);365testfmt("0x1.00p-1026", "%.2a", 0xf.fffp-1030);366testfmt("0x1.83p+0", "%.2a", 1.51);367}368369ATF_TC_WITHOUT_HEAD(subnormal_double);370ATF_TC_BODY(subnormal_double, tc)371{372/* Regression test for https://bugs.freebsd.org/253847 */373double positive = __DBL_DENORM_MIN__;374testfmt("4.9406564584124654418e-324", "%20.20g", positive);375testfmt("4.9406564584124654418E-324", "%20.20G", positive);376testfmt("0x1p-1074", "%a", positive);377testfmt("0X1P-1074", "%A", positive);378double negative = -__DBL_DENORM_MIN__;379testfmt("-4.9406564584124654418e-324", "%20.20g", negative);380testfmt("-4.9406564584124654418E-324", "%20.20G", negative);381testfmt("-0x1p-1074", "%a", negative);382testfmt("-0X1P-1074", "%A", negative);383}384385ATF_TC_WITHOUT_HEAD(subnormal_float);386ATF_TC_BODY(subnormal_float, tc)387{388float positive = __FLT_DENORM_MIN__;389testfmt("1.4012984643248170709e-45", "%20.20g", positive);390testfmt("1.4012984643248170709E-45", "%20.20G", positive);391testfmt("0x1p-149", "%a", positive);392testfmt("0X1P-149", "%A", positive);393float negative = -__FLT_DENORM_MIN__;394testfmt("-1.4012984643248170709e-45", "%20.20g", negative);395testfmt("-1.4012984643248170709E-45", "%20.20G", negative);396testfmt("-0x1p-149", "%a", negative);397testfmt("-0X1P-149", "%A", negative);398}399400ATF_TC_WITHOUT_HEAD(hexadecimal_rounding_fullprec);401ATF_TC_BODY(hexadecimal_rounding_fullprec, tc)402{403/* Double: %.13a with binary64 mantissa=53 */404testfmt("0x1.1234567890bbbp+0", "%.13a", 0x1.1234567890bbbp+0);405406#if defined(__aarch64__)407/* On arm64, long double is IEEE binary128 (mantissa=113) */408testfmt("0x1.3c0ca428c59fbbbbbbbbbbbbbbbbp+0", "%.28La", 0x1.3c0ca428c59fbbbbbbbbbbbbbbbbp+0L);409#endif410}411412ATF_TP_ADD_TCS(tp)413{414415ATF_TP_ADD_TC(tp, float_within_limits);416ATF_TP_ADD_TC(tp, infinities_and_nans);417ATF_TP_ADD_TC(tp, padding);418ATF_TP_ADD_TC(tp, precision_specifiers);419ATF_TP_ADD_TC(tp, thousands_separator_and_other_locale_tests);420ATF_TP_ADD_TC(tp, signed_conversions);421ATF_TP_ADD_TC(tp, alternate_form);422ATF_TP_ADD_TC(tp, padding_and_decimal_point_placement);423ATF_TP_ADD_TC(tp, decimal_rounding);424ATF_TP_ADD_TC(tp, hexadecimal_floating_point);425ATF_TP_ADD_TC(tp, hexadecimal_rounding);426ATF_TP_ADD_TC(tp, subnormal_double);427ATF_TP_ADD_TC(tp, subnormal_float);428ATF_TP_ADD_TC(tp, hexadecimal_rounding_fullprec);429430return (atf_no_error());431}432433434