| 1 | // 1999-11-15 Kevin Ediger <[email protected]> |
| 2 | // test the floating point inserters (facet num_put) |
| 3 | |
| 4 | // Copyright (C) 1999-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | #include <iostream> |
| 22 | #include <iomanip> |
| 23 | #include <sstream> |
| 24 | #include <limits> |
| 25 | #include <testsuite_hooks.h> |
| 26 | |
| 27 | using namespace std; |
| 28 | |
| 29 | struct _TestCase |
| 30 | { |
| 31 | double val; |
| 32 | |
| 33 | int precision; |
| 34 | int width; |
| 35 | char decimal; |
| 36 | char fill; |
| 37 | |
| 38 | bool fixed; |
| 39 | bool scientific; |
| 40 | bool showpos; |
| 41 | bool showpoint; |
| 42 | bool uppercase; |
| 43 | bool internal; |
| 44 | bool left; |
| 45 | bool right; |
| 46 | |
| 47 | const char* result; |
| 48 | }; |
| 49 | |
| 50 | static bool T=true; |
| 51 | static bool F=false; |
| 52 | |
| 53 | static _TestCase testcases[] = |
| 54 | { |
| 55 | // standard output (no formatting applied) |
| 56 | { 1.2, 6,0,'.',' ', F,F,F,F,F,F,F,F, "1.2" }, |
| 57 | { 54, 6,0,'.',' ', F,F,F,F,F,F,F,F, "54" }, |
| 58 | { -.012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-0.012" }, |
| 59 | { -.00000012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-1.2e-07" }, |
| 60 | |
| 61 | // fixed formatting |
| 62 | { 10.2345, 0,0,'.',' ', T,F,F,F,F,F,F,F, "10" }, |
| 63 | { 10.2345, 0,0,'.',' ', T,F,F,T,F,F,F,F, "10." }, |
| 64 | { 10.2345, 1,0,'.',' ', T,F,F,F,F,F,F,F, "10.2" }, |
| 65 | { 10.2345, 4,0,'.',' ', T,F,F,F,F,F,F,F, "10.2345" }, |
| 66 | { 10.2345, 6,0,'.',' ', T,F,T,F,F,F,F,F, "+10.234500" }, |
| 67 | { -10.2345, 6,0,'.',' ', T,F,F,F,F,F,F,F, "-10.234500" }, |
| 68 | { -10.2345, 6,0,',',' ', T,F,F,F,F,F,F,F, "-10,234500" }, |
| 69 | |
| 70 | // fixed formatting with width |
| 71 | { 10.2345, 4,5,'.',' ', T,F,F,F,F,F,F,F, "10.2345" }, |
| 72 | { 10.2345, 4,6,'.',' ', T,F,F,F,F,F,F,F, "10.2345" }, |
| 73 | { 10.2345, 4,7,'.',' ', T,F,F,F,F,F,F,F, "10.2345" }, |
| 74 | { 10.2345, 4,8,'.',' ', T,F,F,F,F,F,F,F, " 10.2345" }, |
| 75 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,F, " 10.2345" }, |
| 76 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,T,F, "10.2345 " }, |
| 77 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,T, " 10.2345" }, |
| 78 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, " 10.2345" }, |
| 79 | { -10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, "- 10.2345" }, |
| 80 | { -10.2345, 4,10,'.','A', T,F,F,F,F,T,F,F, "-AA10.2345" }, |
| 81 | { 10.2345, 4,10,'.','#', T,F,T,F,F,T,F,F, "+##10.2345" }, |
| 82 | |
| 83 | // scientific formatting |
| 84 | { 1.23e+12, 1,0,'.',' ', F,T,F,F,F,F,F,F, "1.2e+12" }, |
| 85 | { 1.23e+12, 1,0,'.',' ', F,T,F,F,T,F,F,F, "1.2E+12" }, |
| 86 | { 1.23e+12, 2,0,'.',' ', F,T,F,F,F,F,F,F, "1.23e+12" }, |
| 87 | { 1.23e+12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "1.230e+12" }, |
| 88 | { 1.23e+12, 3,0,'.',' ', F,T,T,F,F,F,F,F, "+1.230e+12" }, |
| 89 | { -1.23e-12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "-1.230e-12" }, |
| 90 | { 1.23e+12, 3,0,',',' ', F,T,F,F,F,F,F,F, "1,230e+12" }, |
| 91 | }; |
| 92 | |
| 93 | template<typename _CharT> |
| 94 | class testpunct : public numpunct<_CharT> |
| 95 | { |
| 96 | public: |
| 97 | typedef _CharT char_type; |
| 98 | const char_type dchar; |
| 99 | |
| 100 | explicit |
| 101 | testpunct(char_type decimal_char) : numpunct<_CharT>(), dchar(decimal_char) |
| 102 | { } |
| 103 | |
| 104 | protected: |
| 105 | char_type |
| 106 | do_decimal_point() const |
| 107 | { return dchar; } |
| 108 | |
| 109 | char_type |
| 110 | do_thousands_sep() const |
| 111 | { return ','; } |
| 112 | |
| 113 | string |
| 114 | do_grouping() const |
| 115 | { return string(); } |
| 116 | }; |
| 117 | |
| 118 | template<typename _CharT> |
| 119 | void apply_formatting(const _TestCase & tc, basic_ostream<_CharT> & os) |
| 120 | { |
| 121 | os.precision(tc.precision); |
| 122 | os.width(tc.width); |
| 123 | os.fill(static_cast<_CharT>(tc.fill)); |
| 124 | if (tc.fixed) |
| 125 | os.setf(ios::fixed); |
| 126 | if (tc.scientific) |
| 127 | os.setf(ios::scientific); |
| 128 | if (tc.showpos) |
| 129 | os.setf(ios::showpos); |
| 130 | if (tc.showpoint) |
| 131 | os.setf(ios::showpoint); |
| 132 | if (tc.uppercase) |
| 133 | os.setf(ios::uppercase); |
| 134 | if (tc.internal) |
| 135 | os.setf(ios::internal); |
| 136 | if (tc.left) |
| 137 | os.setf(ios::left); |
| 138 | if (tc.right) |
| 139 | os.setf(ios::right); |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | test01() |
| 144 | { |
| 145 | for (std::size_t j = 0; j<sizeof(testcases)/sizeof(testcases[0]); j++) |
| 146 | { |
| 147 | _TestCase & tc = testcases[j]; |
| 148 | #ifdef TEST_NUMPUT_VERBOSE |
| 149 | cout << "expect: " << tc.result << endl; |
| 150 | #endif |
| 151 | // test double with char type |
| 152 | { |
| 153 | testpunct<char>* __tp = new testpunct<char>(tc.decimal); |
| 154 | ostringstream os; |
| 155 | locale __loc(os.getloc(), __tp); |
| 156 | os.imbue(__loc); |
| 157 | apply_formatting(tc, os); |
| 158 | os << tc.val; |
| 159 | #ifdef TEST_NUMPUT_VERBOSE |
| 160 | cout << j << "result 1: " << os.str() << endl; |
| 161 | #endif |
| 162 | VERIFY( os && os.str() == tc.result ); |
| 163 | } |
| 164 | // test long double with char type |
| 165 | { |
| 166 | testpunct<char>* __tp = new testpunct<char>(tc.decimal); |
| 167 | ostringstream os; |
| 168 | locale __loc(os.getloc(), __tp); |
| 169 | os.imbue(__loc); |
| 170 | apply_formatting(tc, os); |
| 171 | os << (long double)tc.val; |
| 172 | #ifdef TEST_NUMPUT_VERBOSE |
| 173 | cout << j << "result 2: " << os.str() << endl; |
| 174 | #endif |
| 175 | VERIFY( os && os.str() == tc.result ); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | int |
| 181 | main() |
| 182 | { |
| 183 | test01(); |
| 184 | return 0; |
| 185 | } |