]>
Commit | Line | Data |
---|---|---|
23cac885 BK |
1 | // 1999-11-15 Kevin Ediger <kediger@licor.com> |
2 | // test the floating point inserters (facet num_put) | |
3 | ||
4 | // Copyright (C) 1999, 2002, 2003 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 2, 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 COPYING. If not, write to the Free | |
83f51799 | 19 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
23cac885 BK |
20 | // USA. |
21 | ||
23cac885 BK |
22 | #include <iostream> |
23 | #include <iomanip> | |
24 | #include <sstream> | |
25 | #include <limits> | |
26 | #include <testsuite_hooks.h> | |
27 | ||
28 | using namespace std; | |
29 | ||
cb584bcf | 30 | #ifndef _GLIBCXX_ASSERT |
23cac885 BK |
31 | # define TEST_NUMPUT_VERBOSE 1 |
32 | #endif | |
33 | ||
34 | struct _TestCase | |
35 | { | |
36 | double val; | |
37 | ||
38 | int precision; | |
39 | int width; | |
40 | char decimal; | |
41 | char fill; | |
42 | ||
43 | bool fixed; | |
44 | bool scientific; | |
45 | bool showpos; | |
46 | bool showpoint; | |
47 | bool uppercase; | |
48 | bool internal; | |
49 | bool left; | |
50 | bool right; | |
51 | ||
52 | const char* result; | |
53 | }; | |
54 | ||
55 | static bool T=true; | |
56 | static bool F=false; | |
57 | ||
58 | static _TestCase testcases[] = | |
59 | { | |
60 | // standard output (no formatting applied) | |
61 | { 1.2, 6,0,'.',' ', F,F,F,F,F,F,F,F, "1.2" }, | |
62 | { 54, 6,0,'.',' ', F,F,F,F,F,F,F,F, "54" }, | |
63 | { -.012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-0.012" }, | |
64 | { -.00000012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-1.2e-07" }, | |
65 | ||
66 | // fixed formatting | |
67 | { 10.2345, 0,0,'.',' ', T,F,F,F,F,F,F,F, "10" }, | |
68 | { 10.2345, 0,0,'.',' ', T,F,F,T,F,F,F,F, "10." }, | |
69 | { 10.2345, 1,0,'.',' ', T,F,F,F,F,F,F,F, "10.2" }, | |
70 | { 10.2345, 4,0,'.',' ', T,F,F,F,F,F,F,F, "10.2345" }, | |
71 | { 10.2345, 6,0,'.',' ', T,F,T,F,F,F,F,F, "+10.234500" }, | |
72 | { -10.2345, 6,0,'.',' ', T,F,F,F,F,F,F,F, "-10.234500" }, | |
73 | { -10.2345, 6,0,',',' ', T,F,F,F,F,F,F,F, "-10,234500" }, | |
74 | ||
75 | // fixed formatting with width | |
76 | { 10.2345, 4,5,'.',' ', T,F,F,F,F,F,F,F, "10.2345" }, | |
77 | { 10.2345, 4,6,'.',' ', T,F,F,F,F,F,F,F, "10.2345" }, | |
78 | { 10.2345, 4,7,'.',' ', T,F,F,F,F,F,F,F, "10.2345" }, | |
79 | { 10.2345, 4,8,'.',' ', T,F,F,F,F,F,F,F, " 10.2345" }, | |
80 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,F, " 10.2345" }, | |
81 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,T,F, "10.2345 " }, | |
82 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,T, " 10.2345" }, | |
83 | { 10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, " 10.2345" }, | |
84 | { -10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, "- 10.2345" }, | |
85 | { -10.2345, 4,10,'.','A', T,F,F,F,F,T,F,F, "-AA10.2345" }, | |
86 | { 10.2345, 4,10,'.','#', T,F,T,F,F,T,F,F, "+##10.2345" }, | |
87 | ||
88 | // scientific formatting | |
89 | { 1.23e+12, 1,0,'.',' ', F,T,F,F,F,F,F,F, "1.2e+12" }, | |
90 | { 1.23e+12, 1,0,'.',' ', F,T,F,F,T,F,F,F, "1.2E+12" }, | |
91 | { 1.23e+12, 2,0,'.',' ', F,T,F,F,F,F,F,F, "1.23e+12" }, | |
92 | { 1.23e+12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "1.230e+12" }, | |
93 | { 1.23e+12, 3,0,'.',' ', F,T,T,F,F,F,F,F, "+1.230e+12" }, | |
94 | { -1.23e-12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "-1.230e-12" }, | |
95 | { 1.23e+12, 3,0,',',' ', F,T,F,F,F,F,F,F, "1,230e+12" }, | |
96 | }; | |
97 | ||
98 | template<typename _CharT> | |
99 | class testpunct : public numpunct<_CharT> | |
100 | { | |
101 | public: | |
102 | typedef _CharT char_type; | |
103 | const char_type dchar; | |
104 | ||
105 | explicit | |
106 | testpunct(char_type decimal_char) : numpunct<_CharT>(), dchar(decimal_char) | |
107 | { } | |
108 | ||
109 | protected: | |
110 | char_type | |
111 | do_decimal_point() const | |
112 | { return dchar; } | |
113 | ||
114 | char_type | |
115 | do_thousands_sep() const | |
116 | { return ','; } | |
117 | ||
118 | string | |
119 | do_grouping() const | |
120 | { return string(); } | |
121 | }; | |
122 | ||
123 | template<typename _CharT> | |
124 | void apply_formatting(const _TestCase & tc, basic_ostream<_CharT> & os) | |
125 | { | |
126 | os.precision(tc.precision); | |
127 | os.width(tc.width); | |
128 | os.fill(static_cast<_CharT>(tc.fill)); | |
129 | if (tc.fixed) | |
130 | os.setf(ios::fixed); | |
131 | if (tc.scientific) | |
132 | os.setf(ios::scientific); | |
133 | if (tc.showpos) | |
134 | os.setf(ios::showpos); | |
135 | if (tc.showpoint) | |
136 | os.setf(ios::showpoint); | |
137 | if (tc.uppercase) | |
138 | os.setf(ios::uppercase); | |
139 | if (tc.internal) | |
140 | os.setf(ios::internal); | |
141 | if (tc.left) | |
142 | os.setf(ios::left); | |
143 | if (tc.right) | |
144 | os.setf(ios::right); | |
145 | } | |
146 | ||
147 | void | |
148 | test01() | |
149 | { | |
11f10e6b BK |
150 | bool test __attribute__((unused)) = true; |
151 | for (std::size_t j = 0; j<sizeof(testcases)/sizeof(testcases[0]); j++) | |
23cac885 BK |
152 | { |
153 | _TestCase & tc = testcases[j]; | |
154 | #ifdef TEST_NUMPUT_VERBOSE | |
155 | cout << "expect: " << tc.result << endl; | |
156 | #endif | |
157 | // test double with char type | |
158 | { | |
159 | testpunct<char>* __tp = new testpunct<char>(tc.decimal); | |
160 | ostringstream os; | |
161 | locale __loc(os.getloc(), __tp); | |
162 | os.imbue(__loc); | |
163 | apply_formatting(tc, os); | |
164 | os << tc.val; | |
165 | #ifdef TEST_NUMPUT_VERBOSE | |
166 | cout << j << "result 1: " << os.str() << endl; | |
167 | #endif | |
168 | VERIFY( os && os.str() == tc.result ); | |
169 | } | |
170 | // test long double with char type | |
171 | { | |
172 | testpunct<char>* __tp = new testpunct<char>(tc.decimal); | |
173 | ostringstream os; | |
174 | locale __loc(os.getloc(), __tp); | |
175 | os.imbue(__loc); | |
176 | apply_formatting(tc, os); | |
177 | os << (long double)tc.val; | |
178 | #ifdef TEST_NUMPUT_VERBOSE | |
179 | cout << j << "result 2: " << os.str() << endl; | |
180 | #endif | |
181 | VERIFY( os && os.str() == tc.result ); | |
182 | } | |
183 | } | |
184 | } | |
185 | ||
186 | int | |
187 | main() | |
188 | { | |
189 | test01(); | |
190 | return 0; | |
191 | } |