| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-string-conversions "" } |
| 3 | // { dg-xfail-run-if "broken long double IO" { newlib_broken_long_double_io } } |
| 4 | |
| 5 | // 2014-03-27 RĂ¼diger Sonderfeld |
| 6 | // test the hexadecimal floating point inserters (facet num_put) |
| 7 | |
| 8 | // Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 9 | // |
| 10 | // This file is part of the GNU ISO C++ Library. This library is free |
| 11 | // software; you can redistribute it and/or modify it under the |
| 12 | // terms of the GNU General Public License as published by the |
| 13 | // Free Software Foundation; either version 3, or (at your option) |
| 14 | // any later version. |
| 15 | |
| 16 | // This library is distributed in the hope that it will be useful, |
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | // GNU General Public License for more details. |
| 20 | |
| 21 | // You should have received a copy of the GNU General Public License along |
| 22 | // with this library; see the file COPYING3. If not see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | #include <iomanip> |
| 26 | #include <sstream> |
| 27 | #include <string> |
| 28 | #include <testsuite_hooks.h> |
| 29 | |
| 30 | #ifdef TEST_NUMPUT_VERBOSE |
| 31 | # include <iostream> |
| 32 | #endif |
| 33 | |
| 34 | using namespace std; |
| 35 | |
| 36 | void |
| 37 | test01() |
| 38 | { |
| 39 | ostringstream os; |
| 40 | double d = 272.; // 0x1.1p+8; |
| 41 | #ifdef TEST_NUMPUT_VERBOSE |
| 42 | cout << os.precision() << endl; |
| 43 | #endif |
| 44 | os << hexfloat << setprecision(1); |
| 45 | os << d; |
| 46 | #ifdef TEST_NUMPUT_VERBOSE |
| 47 | cout << "got: " << os.str() << endl; |
| 48 | #endif |
| 49 | VERIFY( os && std::stod(os.str()) == d ); |
| 50 | VERIFY( os.str().substr(0, 2) == "0x" ); |
| 51 | VERIFY( os.str().find('p') != std::string::npos ); |
| 52 | |
| 53 | os.str(""); |
| 54 | os << uppercase << d; |
| 55 | #ifdef TEST_NUMPUT_VERBOSE |
| 56 | cout << "got: " << os.str() << endl; |
| 57 | #endif |
| 58 | VERIFY( os && std::stod(os.str()) == d ); |
| 59 | VERIFY( os.str().substr(0, 2) == "0X" ); |
| 60 | VERIFY( os.str().find('P') != std::string::npos ); |
| 61 | |
| 62 | os << nouppercase; |
| 63 | os.str(""); |
| 64 | os << defaultfloat << setprecision(6); |
| 65 | os << d; |
| 66 | #ifdef TEST_NUMPUT_VERBOSE |
| 67 | cout << "got: " << os.str() << endl; |
| 68 | #endif |
| 69 | VERIFY( os && os.str() == "272" ); |
| 70 | |
| 71 | os.str(""); |
| 72 | d = 15.; //0x1.ep+3; |
| 73 | os << hexfloat << setprecision(1); |
| 74 | os << d; |
| 75 | #ifdef TEST_NUMPUT_VERBOSE |
| 76 | cout << "got: " << os.str() << endl; |
| 77 | #endif |
| 78 | VERIFY( os && std::stod(os.str()) == d ); |
| 79 | os.str(""); |
| 80 | os << uppercase << d; |
| 81 | #ifdef TEST_NUMPUT_VERBOSE |
| 82 | cout << "got: " << os.str() << endl; |
| 83 | #endif |
| 84 | VERIFY( os && std::stod(os.str()) == d ); |
| 85 | os << nouppercase; |
| 86 | os.str(""); |
| 87 | os << defaultfloat << setprecision(6); |
| 88 | os << d; |
| 89 | #ifdef TEST_NUMPUT_VERBOSE |
| 90 | cout << "got: " << os.str() << endl; |
| 91 | #endif |
| 92 | VERIFY( os && os.str() == "15" ); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | test02() |
| 97 | { |
| 98 | ostringstream os; |
| 99 | long double d = 272.L; // 0x1.1p+8L; |
| 100 | os << hexfloat << setprecision(1); |
| 101 | os << d; |
| 102 | #ifdef TEST_NUMPUT_VERBOSE |
| 103 | cout << "got: " << os.str() << endl; |
| 104 | #endif |
| 105 | VERIFY( os && std::stold(os.str()) == d ); |
| 106 | os.str(""); |
| 107 | os << uppercase << d; |
| 108 | #ifdef TEST_NUMPUT_VERBOSE |
| 109 | cout << "got: " << os.str() << endl; |
| 110 | #endif |
| 111 | VERIFY( os && std::stold(os.str()) == d ); |
| 112 | os << nouppercase; |
| 113 | os.str(""); |
| 114 | os << defaultfloat << setprecision(6); |
| 115 | os << d; |
| 116 | #ifdef TEST_NUMPUT_VERBOSE |
| 117 | cout << "got: " << os.str() << endl; |
| 118 | #endif |
| 119 | VERIFY( os && os.str() == "272" ); |
| 120 | |
| 121 | os.str(""); |
| 122 | os << hexfloat << setprecision(1); |
| 123 | d = 15.; //0x1.ep+3; |
| 124 | os << d; |
| 125 | #ifdef TEST_NUMPUT_VERBOSE |
| 126 | cout << "got: " << os.str() << endl; |
| 127 | #endif |
| 128 | VERIFY( os && std::stold(os.str()) == d ); |
| 129 | os.str(""); |
| 130 | os << uppercase << d; |
| 131 | #ifdef TEST_NUMPUT_VERBOSE |
| 132 | cout << "got: " << os.str() << endl; |
| 133 | #endif |
| 134 | VERIFY( os && std::stold(os.str()) == d ); |
| 135 | os << nouppercase; |
| 136 | os.str(""); |
| 137 | os << defaultfloat << setprecision(6); |
| 138 | os << d; |
| 139 | #ifdef TEST_NUMPUT_VERBOSE |
| 140 | cout << "got: " << os.str() << endl; |
| 141 | #endif |
| 142 | VERIFY( os && os.str() == "15" ); |
| 143 | } |
| 144 | |
| 145 | int |
| 146 | main() |
| 147 | { |
| 148 | test01(); |
| 149 | test02(); |
| 150 | } |