]>
Commit | Line | Data |
---|---|---|
23cac885 BK |
1 | // 1999-11-15 Kevin Ediger <kediger@licor.com> |
2 | // test the floating point inserters (facet num_put) | |
3 | ||
8d9254fc | 4 | // Copyright (C) 1999-2020 Free Software Foundation, Inc. |
23cac885 BK |
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 | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
23cac885 BK |
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 | |
748086b7 JJ |
18 | // with this library; see the file COPYING3. If not see |
19 | // <http://www.gnu.org/licenses/>. | |
23cac885 | 20 | |
23cac885 BK |
21 | #include <iostream> |
22 | #include <iomanip> | |
23 | #include <sstream> | |
24 | #include <limits> | |
25 | #include <testsuite_hooks.h> | |
26 | ||
27 | using namespace std; | |
28 | ||
23cac885 BK |
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 | const wchar_t* wresult; | |
49 | }; | |
50 | ||
51 | static bool T=true; | |
52 | static bool F=false; | |
53 | ||
54 | static _TestCase testcases[] = | |
55 | { | |
56 | // standard output (no formatting applied) 1-4 | |
57 | { 1.2, 6,0,'.',' ', F,F,F,F,F,F,F,F, "1.2",L"1.2" }, | |
58 | { 54, 6,0,'.',' ', F,F,F,F,F,F,F,F, "54",L"54" }, | |
59 | { -.012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-0.012",L"-0.012" }, | |
60 | { -.00000012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-1.2e-07",L"-1.2e-07" }, | |
61 | ||
62 | // fixed formatting 5-11 | |
63 | { 10.2345, 0,0,'.',' ', T,F,F,F,F,F,F,F, "10",L"10" }, | |
64 | { 10.2345, 0,0,'.',' ', T,F,F,T,F,F,F,F, "10.",L"10." }, | |
65 | { 10.2345, 1,0,'.',' ', T,F,F,F,F,F,F,F, "10.2",L"10.2" }, | |
66 | { 10.2345, 4,0,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" }, | |
67 | { 10.2345, 6,0,'.',' ', T,F,T,F,F,F,F,F, "+10.234500",L"+10.234500" }, | |
68 | { -10.2345, 6,0,'.',' ', T,F,F,F,F,F,F,F, "-10.234500",L"-10.234500" }, | |
69 | { -10.2345, 6,0,',',' ', T,F,F,F,F,F,F,F, "-10,234500",L"-10,234500" }, | |
70 | ||
71 | // fixed formatting with width 12-22 | |
72 | { 10.2345, 4,5,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" }, | |
73 | { 10.2345, 4,6,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" }, | |
74 | { 10.2345, 4,7,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" }, | |
75 | { 10.2345, 4,8,'.',' ', T,F,F,F,F,F,F,F, " 10.2345",L" 10.2345" }, | |
76 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,F, " 10.2345",L" 10.2345" }, | |
77 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,T,F, "10.2345 ",L"10.2345 " }, | |
78 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,T, " 10.2345",L" 10.2345" }, | |
79 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, " 10.2345",L" 10.2345" }, | |
80 | { -10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, "- 10.2345",L"- 10.2345" }, | |
81 | { -10.2345, 4,10,'.','A', T,F,F,F,F,T,F,F, "-AA10.2345",L"-AA10.2345" }, | |
82 | { 10.2345, 4,10,'.','#', T,F,T,F,F,T,F,F, "+##10.2345",L"+##10.2345" }, | |
83 | ||
84 | // scientific formatting 23-29 | |
85 | { 1.23e+12, 1,0,'.',' ', F,T,F,F,F,F,F,F, "1.2e+12",L"1.2e+12" }, | |
86 | { 1.23e+12, 1,0,'.',' ', F,T,F,F,T,F,F,F, "1.2E+12",L"1.2E+12" }, | |
87 | { 1.23e+12, 2,0,'.',' ', F,T,F,F,F,F,F,F, "1.23e+12",L"1.23e+12" }, | |
88 | { 1.23e+12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "1.230e+12",L"1.230e+12" }, | |
89 | { 1.23e+12, 3,0,'.',' ', F,T,T,F,F,F,F,F, "+1.230e+12",L"+1.230e+12" }, | |
90 | { -1.23e-12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "-1.230e-12",L"-1.230e-12" }, | |
91 | { 1.23e+12, 3,0,',',' ', F,T,F,F,F,F,F,F, "1,230e+12",L"1,230e+12" }, | |
92 | }; | |
93 | ||
94 | template<typename _CharT> | |
95 | class testpunct : public numpunct<_CharT> | |
96 | { | |
97 | public: | |
98 | typedef _CharT char_type; | |
99 | const char_type dchar; | |
100 | ||
101 | explicit | |
102 | testpunct(char_type decimal_char) : numpunct<_CharT>(), dchar(decimal_char) | |
103 | { } | |
104 | ||
105 | protected: | |
106 | char_type | |
107 | do_decimal_point() const | |
108 | { return dchar; } | |
109 | ||
110 | char_type | |
111 | do_thousands_sep() const | |
112 | { return ','; } | |
113 | ||
114 | string | |
115 | do_grouping() const | |
116 | { return string(); } | |
117 | }; | |
118 | ||
119 | template<typename _CharT> | |
120 | void apply_formatting(const _TestCase & tc, basic_ostream<_CharT> & os) | |
121 | { | |
122 | os.precision(tc.precision); | |
123 | os.width(tc.width); | |
124 | os.fill(static_cast<_CharT>(tc.fill)); | |
125 | if (tc.fixed) | |
126 | os.setf(ios::fixed); | |
127 | if (tc.scientific) | |
128 | os.setf(ios::scientific); | |
129 | if (tc.showpos) | |
130 | os.setf(ios::showpos); | |
131 | if (tc.showpoint) | |
132 | os.setf(ios::showpoint); | |
133 | if (tc.uppercase) | |
134 | os.setf(ios::uppercase); | |
135 | if (tc.internal) | |
136 | os.setf(ios::internal); | |
137 | if (tc.left) | |
138 | os.setf(ios::left); | |
139 | if (tc.right) | |
140 | os.setf(ios::right); | |
141 | } | |
142 | ||
143 | void | |
144 | test01() | |
145 | { | |
11f10e6b | 146 | for (std::size_t j = 0; j<sizeof(testcases)/sizeof(testcases[0]); j++) |
23cac885 BK |
147 | { |
148 | _TestCase & tc = testcases[j]; | |
149 | #ifdef TEST_NUMPUT_VERBOSE | |
150 | cout << "expect: " << tc.result << endl; | |
151 | #endif | |
152 | // test double with wchar_t type | |
153 | { | |
154 | testpunct<wchar_t>* __tp = new testpunct<wchar_t>(tc.decimal); | |
155 | wostringstream os; | |
156 | locale __loc(os.getloc(), __tp); | |
157 | os.imbue(__loc); | |
158 | apply_formatting(tc, os); | |
159 | os << tc.val; | |
160 | VERIFY( os && os.str() == tc.wresult ); | |
161 | } | |
162 | // test long double with wchar_t type | |
163 | { | |
164 | testpunct<wchar_t>* __tp = new testpunct<wchar_t>(tc.decimal); | |
165 | wostringstream os; | |
166 | locale __loc(os.getloc(), __tp); | |
167 | os.imbue(__loc); | |
168 | apply_formatting(tc, os); | |
169 | os << (long double)tc.val; | |
170 | VERIFY( os && os.str() == tc.wresult ); | |
171 | } | |
172 | } | |
173 | } | |
174 | ||
175 | int | |
176 | main() | |
177 | { | |
178 | test01(); | |
179 | return 0; | |
180 | } |