]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: tests/strings/numformat.cpp | |
3 | // Purpose: wxNumberFormatter unit test | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2011-01-15 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org> | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | #include "testprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #include "wx/numformatter.h" | |
21 | #include "wx/intl.h" | |
22 | ||
23 | // ---------------------------------------------------------------------------- | |
24 | // test class | |
25 | // ---------------------------------------------------------------------------- | |
26 | ||
27 | class NumFormatterTestCase : public CppUnit::TestCase | |
28 | { | |
29 | public: | |
30 | NumFormatterTestCase() { m_locale = NULL; } | |
31 | ||
32 | virtual void setUp() | |
33 | { | |
34 | // We need to use a locale with known decimal point and which uses the | |
35 | // thousands separator for the tests to make sense. | |
36 | m_locale = new wxLocale(wxLANGUAGE_ENGLISH_UK, | |
37 | wxLOCALE_DONT_LOAD_DEFAULT); | |
38 | if ( !m_locale->IsOk() ) | |
39 | tearDown(); | |
40 | } | |
41 | ||
42 | virtual void tearDown() | |
43 | { | |
44 | delete m_locale; | |
45 | m_locale = NULL; | |
46 | } | |
47 | ||
48 | private: | |
49 | CPPUNIT_TEST_SUITE( NumFormatterTestCase ); | |
50 | CPPUNIT_TEST( LongToString ); | |
51 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
52 | CPPUNIT_TEST( LongLongToString ); | |
53 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
54 | CPPUNIT_TEST( DoubleToString ); | |
55 | CPPUNIT_TEST( NoTrailingZeroes ); | |
56 | CPPUNIT_TEST( LongFromString ); | |
57 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
58 | CPPUNIT_TEST( LongLongFromString ); | |
59 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
60 | CPPUNIT_TEST( DoubleFromString ); | |
61 | CPPUNIT_TEST_SUITE_END(); | |
62 | ||
63 | void LongToString(); | |
64 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
65 | void LongLongToString(); | |
66 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
67 | void DoubleToString(); | |
68 | void NoTrailingZeroes(); | |
69 | void LongFromString(); | |
70 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
71 | void LongLongFromString(); | |
72 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
73 | void DoubleFromString(); | |
74 | ||
75 | wxLocale *m_locale; | |
76 | ||
77 | wxDECLARE_NO_COPY_CLASS(NumFormatterTestCase); | |
78 | }; | |
79 | ||
80 | // register in the unnamed registry so that these tests are run by default | |
81 | CPPUNIT_TEST_SUITE_REGISTRATION( NumFormatterTestCase ); | |
82 | ||
83 | // also include in its own registry so that these tests can be run alone | |
84 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NumFormatterTestCase, "NumFormatterTestCase" ); | |
85 | ||
86 | // ---------------------------------------------------------------------------- | |
87 | // tests themselves | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
90 | void NumFormatterTestCase::LongToString() | |
91 | { | |
92 | if ( !m_locale ) | |
93 | return; | |
94 | ||
95 | CPPUNIT_ASSERT_EQUAL( "1", wxNumberFormatter::ToString( 1L)); | |
96 | CPPUNIT_ASSERT_EQUAL( "12", wxNumberFormatter::ToString( 12L)); | |
97 | CPPUNIT_ASSERT_EQUAL( "123", wxNumberFormatter::ToString( 123L)); | |
98 | CPPUNIT_ASSERT_EQUAL( "1,234", wxNumberFormatter::ToString( 1234L)); | |
99 | CPPUNIT_ASSERT_EQUAL( "12,345", wxNumberFormatter::ToString( 12345L)); | |
100 | CPPUNIT_ASSERT_EQUAL( "123,456", wxNumberFormatter::ToString( 123456L)); | |
101 | CPPUNIT_ASSERT_EQUAL( "1,234,567", wxNumberFormatter::ToString( 1234567L)); | |
102 | CPPUNIT_ASSERT_EQUAL( "12,345,678", wxNumberFormatter::ToString( 12345678L)); | |
103 | CPPUNIT_ASSERT_EQUAL("123,456,789", wxNumberFormatter::ToString( 123456789L)); | |
104 | } | |
105 | ||
106 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
107 | ||
108 | void NumFormatterTestCase::LongLongToString() | |
109 | { | |
110 | if ( !m_locale ) | |
111 | return; | |
112 | ||
113 | CPPUNIT_ASSERT_EQUAL( "1", wxNumberFormatter::ToString(wxLL( 1))); | |
114 | CPPUNIT_ASSERT_EQUAL( "12", wxNumberFormatter::ToString(wxLL( 12))); | |
115 | CPPUNIT_ASSERT_EQUAL( "123", wxNumberFormatter::ToString(wxLL( 123))); | |
116 | CPPUNIT_ASSERT_EQUAL( "1,234", wxNumberFormatter::ToString(wxLL( 1234))); | |
117 | CPPUNIT_ASSERT_EQUAL( "12,345", wxNumberFormatter::ToString(wxLL( 12345))); | |
118 | CPPUNIT_ASSERT_EQUAL( "123,456", wxNumberFormatter::ToString(wxLL( 123456))); | |
119 | CPPUNIT_ASSERT_EQUAL( "1,234,567", wxNumberFormatter::ToString(wxLL( 1234567))); | |
120 | CPPUNIT_ASSERT_EQUAL( "12,345,678", wxNumberFormatter::ToString(wxLL( 12345678))); | |
121 | CPPUNIT_ASSERT_EQUAL("123,456,789", wxNumberFormatter::ToString(wxLL( 123456789))); | |
122 | } | |
123 | ||
124 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
125 | ||
126 | void NumFormatterTestCase::DoubleToString() | |
127 | { | |
128 | if ( !m_locale ) | |
129 | return; | |
130 | ||
131 | CPPUNIT_ASSERT_EQUAL("1.0", wxNumberFormatter::ToString(1., 1)); | |
132 | CPPUNIT_ASSERT_EQUAL("0.123456", wxNumberFormatter::ToString(0.123456, 6)); | |
133 | CPPUNIT_ASSERT_EQUAL("1.234567", wxNumberFormatter::ToString(1.234567, 6)); | |
134 | CPPUNIT_ASSERT_EQUAL("12.34567", wxNumberFormatter::ToString(12.34567, 5)); | |
135 | CPPUNIT_ASSERT_EQUAL("123.4567", wxNumberFormatter::ToString(123.4567, 4)); | |
136 | CPPUNIT_ASSERT_EQUAL("1,234.56", wxNumberFormatter::ToString(1234.56, 2)); | |
137 | CPPUNIT_ASSERT_EQUAL("12,345.6", wxNumberFormatter::ToString(12345.6, 1)); | |
138 | CPPUNIT_ASSERT_EQUAL("12,345.6", wxNumberFormatter::ToString(12345.6, 1)); | |
139 | CPPUNIT_ASSERT_EQUAL("123,456,789.0", | |
140 | wxNumberFormatter::ToString(123456789., 1)); | |
141 | CPPUNIT_ASSERT_EQUAL("123,456,789.012", | |
142 | wxNumberFormatter::ToString(123456789.012, 3)); | |
143 | } | |
144 | ||
145 | void NumFormatterTestCase::NoTrailingZeroes() | |
146 | { | |
147 | WX_ASSERT_FAILS_WITH_ASSERT | |
148 | ( | |
149 | wxNumberFormatter::ToString(123L, wxNumberFormatter::Style_NoTrailingZeroes) | |
150 | ); | |
151 | ||
152 | if ( !m_locale ) | |
153 | return; | |
154 | ||
155 | CPPUNIT_ASSERT_EQUAL | |
156 | ( | |
157 | "123.000", | |
158 | wxNumberFormatter::ToString(123., 3) | |
159 | ); | |
160 | ||
161 | CPPUNIT_ASSERT_EQUAL | |
162 | ( | |
163 | "123", | |
164 | wxNumberFormatter::ToString(123., 3, wxNumberFormatter::Style_NoTrailingZeroes) | |
165 | ); | |
166 | ||
167 | CPPUNIT_ASSERT_EQUAL | |
168 | ( | |
169 | "123", | |
170 | wxNumberFormatter::ToString(123., 9, wxNumberFormatter::Style_NoTrailingZeroes) | |
171 | ); | |
172 | ||
173 | CPPUNIT_ASSERT_EQUAL | |
174 | ( | |
175 | "123.456", | |
176 | wxNumberFormatter::ToString(123.456, 3, wxNumberFormatter::Style_NoTrailingZeroes) | |
177 | ); | |
178 | ||
179 | CPPUNIT_ASSERT_EQUAL | |
180 | ( | |
181 | "123.456000000", | |
182 | wxNumberFormatter::ToString(123.456, 9) | |
183 | ); | |
184 | ||
185 | CPPUNIT_ASSERT_EQUAL | |
186 | ( | |
187 | "123.456", | |
188 | wxNumberFormatter::ToString(123.456, 9, wxNumberFormatter::Style_NoTrailingZeroes) | |
189 | ); | |
190 | } | |
191 | ||
192 | void NumFormatterTestCase::LongFromString() | |
193 | { | |
194 | if ( !m_locale ) | |
195 | return; | |
196 | ||
197 | WX_ASSERT_FAILS_WITH_ASSERT | |
198 | ( | |
199 | wxNumberFormatter::FromString("123", static_cast<long *>(0)) | |
200 | ); | |
201 | ||
202 | long l; | |
203 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l) ); | |
204 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l) ); | |
205 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l) ); | |
206 | ||
207 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l) ); | |
208 | CPPUNIT_ASSERT_EQUAL( 123, l ); | |
209 | ||
210 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l) ); | |
211 | CPPUNIT_ASSERT_EQUAL( 1234, l ); | |
212 | ||
213 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l) ); | |
214 | CPPUNIT_ASSERT_EQUAL( 1234, l ); | |
215 | ||
216 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l) ); | |
217 | CPPUNIT_ASSERT_EQUAL( 12345, l ); | |
218 | ||
219 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l) ); | |
220 | CPPUNIT_ASSERT_EQUAL( 123456, l ); | |
221 | ||
222 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l) ); | |
223 | CPPUNIT_ASSERT_EQUAL( 1234567, l ); | |
224 | } | |
225 | ||
226 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
227 | ||
228 | void NumFormatterTestCase::LongLongFromString() | |
229 | { | |
230 | if ( !m_locale ) | |
231 | return; | |
232 | ||
233 | WX_ASSERT_FAILS_WITH_ASSERT | |
234 | ( | |
235 | wxNumberFormatter::FromString("123", static_cast<wxLongLong_t *>(0)) | |
236 | ); | |
237 | ||
238 | wxLongLong_t l; | |
239 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l) ); | |
240 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l) ); | |
241 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l) ); | |
242 | ||
243 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l) ); | |
244 | CPPUNIT_ASSERT_EQUAL( 123, l ); | |
245 | ||
246 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l) ); | |
247 | CPPUNIT_ASSERT_EQUAL( 1234, l ); | |
248 | ||
249 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l) ); | |
250 | CPPUNIT_ASSERT_EQUAL( 1234, l ); | |
251 | ||
252 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l) ); | |
253 | CPPUNIT_ASSERT_EQUAL( 12345, l ); | |
254 | ||
255 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l) ); | |
256 | CPPUNIT_ASSERT_EQUAL( 123456, l ); | |
257 | ||
258 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l) ); | |
259 | CPPUNIT_ASSERT_EQUAL( 1234567, l ); | |
260 | } | |
261 | ||
262 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
263 | ||
264 | void NumFormatterTestCase::DoubleFromString() | |
265 | { | |
266 | if ( !m_locale ) | |
267 | return; | |
268 | ||
269 | WX_ASSERT_FAILS_WITH_ASSERT | |
270 | ( | |
271 | wxNumberFormatter::FromString("123", static_cast<double *>(0)) | |
272 | ); | |
273 | ||
274 | double d; | |
275 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &d) ); | |
276 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("bar", &d) ); | |
277 | ||
278 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &d) ); | |
279 | CPPUNIT_ASSERT_EQUAL( 123., d ); | |
280 | ||
281 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123.456789012", &d) ); | |
282 | CPPUNIT_ASSERT_EQUAL( 123.456789012, d ); | |
283 | ||
284 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234.56789012", &d) ); | |
285 | CPPUNIT_ASSERT_EQUAL( 1234.56789012, d ); | |
286 | ||
287 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345.6789012", &d) ); | |
288 | CPPUNIT_ASSERT_EQUAL( 12345.6789012, d ); | |
289 | ||
290 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456.789012", &d) ); | |
291 | CPPUNIT_ASSERT_EQUAL( 123456.789012, d ); | |
292 | ||
293 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567.89012", &d) ); | |
294 | CPPUNIT_ASSERT_EQUAL( 1234567.89012, d ); | |
295 | ||
296 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345,678.9012", &d) ); | |
297 | CPPUNIT_ASSERT_EQUAL( 12345678.9012, d ); | |
298 | ||
299 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456,789.012", &d) ); | |
300 | CPPUNIT_ASSERT_EQUAL( 123456789.012, d ); | |
301 | ||
302 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123456789.012", &d) ); | |
303 | CPPUNIT_ASSERT_EQUAL( 123456789.012, d ); | |
304 | } |