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