]>
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( "-1", wxNumberFormatter::ToString( -1L)); | |
97 | CPPUNIT_ASSERT_EQUAL( "12", wxNumberFormatter::ToString( 12L)); | |
98 | CPPUNIT_ASSERT_EQUAL( "-12", wxNumberFormatter::ToString( -12L)); | |
99 | CPPUNIT_ASSERT_EQUAL( "123", wxNumberFormatter::ToString( 123L)); | |
100 | CPPUNIT_ASSERT_EQUAL( "-123", wxNumberFormatter::ToString( -123L)); | |
101 | CPPUNIT_ASSERT_EQUAL( "1,234", wxNumberFormatter::ToString( 1234L)); | |
102 | CPPUNIT_ASSERT_EQUAL( "-1,234", wxNumberFormatter::ToString( -1234L)); | |
103 | CPPUNIT_ASSERT_EQUAL( "12,345", wxNumberFormatter::ToString( 12345L)); | |
104 | CPPUNIT_ASSERT_EQUAL( "-12,345", wxNumberFormatter::ToString( -12345L)); | |
105 | CPPUNIT_ASSERT_EQUAL( "123,456", wxNumberFormatter::ToString( 123456L)); | |
106 | CPPUNIT_ASSERT_EQUAL( "-123,456", wxNumberFormatter::ToString( -123456L)); | |
107 | CPPUNIT_ASSERT_EQUAL( "1,234,567", wxNumberFormatter::ToString( 1234567L)); | |
108 | CPPUNIT_ASSERT_EQUAL( "-1,234,567", wxNumberFormatter::ToString( -1234567L)); | |
109 | CPPUNIT_ASSERT_EQUAL( "12,345,678", wxNumberFormatter::ToString( 12345678L)); | |
110 | CPPUNIT_ASSERT_EQUAL("-12,345,678", wxNumberFormatter::ToString( -12345678L)); | |
111 | CPPUNIT_ASSERT_EQUAL("123,456,789", wxNumberFormatter::ToString( 123456789L)); | |
112 | } | |
113 | ||
114 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
115 | ||
116 | void NumFormatterTestCase::LongLongToString() | |
117 | { | |
118 | if ( !m_locale ) | |
119 | return; | |
120 | ||
121 | CPPUNIT_ASSERT_EQUAL( "1", wxNumberFormatter::ToString(wxLL( 1))); | |
122 | CPPUNIT_ASSERT_EQUAL( "12", wxNumberFormatter::ToString(wxLL( 12))); | |
123 | CPPUNIT_ASSERT_EQUAL( "123", wxNumberFormatter::ToString(wxLL( 123))); | |
124 | CPPUNIT_ASSERT_EQUAL( "1,234", wxNumberFormatter::ToString(wxLL( 1234))); | |
125 | CPPUNIT_ASSERT_EQUAL( "12,345", wxNumberFormatter::ToString(wxLL( 12345))); | |
126 | CPPUNIT_ASSERT_EQUAL( "123,456", wxNumberFormatter::ToString(wxLL( 123456))); | |
127 | CPPUNIT_ASSERT_EQUAL( "1,234,567", wxNumberFormatter::ToString(wxLL( 1234567))); | |
128 | CPPUNIT_ASSERT_EQUAL( "12,345,678", wxNumberFormatter::ToString(wxLL( 12345678))); | |
129 | CPPUNIT_ASSERT_EQUAL("123,456,789", wxNumberFormatter::ToString(wxLL( 123456789))); | |
130 | } | |
131 | ||
132 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
133 | ||
134 | void NumFormatterTestCase::DoubleToString() | |
135 | { | |
136 | if ( !m_locale ) | |
137 | return; | |
138 | ||
139 | CPPUNIT_ASSERT_EQUAL("1.0", wxNumberFormatter::ToString(1., 1)); | |
140 | CPPUNIT_ASSERT_EQUAL("0.123456", wxNumberFormatter::ToString(0.123456, 6)); | |
141 | CPPUNIT_ASSERT_EQUAL("1.234567", wxNumberFormatter::ToString(1.234567, 6)); | |
142 | CPPUNIT_ASSERT_EQUAL("12.34567", wxNumberFormatter::ToString(12.34567, 5)); | |
143 | CPPUNIT_ASSERT_EQUAL("123.4567", wxNumberFormatter::ToString(123.4567, 4)); | |
144 | CPPUNIT_ASSERT_EQUAL("1,234.56", wxNumberFormatter::ToString(1234.56, 2)); | |
145 | CPPUNIT_ASSERT_EQUAL("12,345.6", wxNumberFormatter::ToString(12345.6, 1)); | |
146 | CPPUNIT_ASSERT_EQUAL("12,345.6", wxNumberFormatter::ToString(12345.6, 1)); | |
147 | CPPUNIT_ASSERT_EQUAL("123,456,789.0", | |
148 | wxNumberFormatter::ToString(123456789., 1)); | |
149 | CPPUNIT_ASSERT_EQUAL("123,456,789.012", | |
150 | wxNumberFormatter::ToString(123456789.012, 3)); | |
151 | } | |
152 | ||
153 | void NumFormatterTestCase::NoTrailingZeroes() | |
154 | { | |
155 | WX_ASSERT_FAILS_WITH_ASSERT | |
156 | ( | |
157 | wxNumberFormatter::ToString(123L, wxNumberFormatter::Style_NoTrailingZeroes) | |
158 | ); | |
159 | ||
160 | if ( !m_locale ) | |
161 | return; | |
162 | ||
163 | CPPUNIT_ASSERT_EQUAL | |
164 | ( | |
165 | "123.000", | |
166 | wxNumberFormatter::ToString(123., 3) | |
167 | ); | |
168 | ||
169 | CPPUNIT_ASSERT_EQUAL | |
170 | ( | |
171 | "123", | |
172 | wxNumberFormatter::ToString(123., 3, wxNumberFormatter::Style_NoTrailingZeroes) | |
173 | ); | |
174 | ||
175 | CPPUNIT_ASSERT_EQUAL | |
176 | ( | |
177 | "123", | |
178 | wxNumberFormatter::ToString(123., 9, wxNumberFormatter::Style_NoTrailingZeroes) | |
179 | ); | |
180 | ||
181 | CPPUNIT_ASSERT_EQUAL | |
182 | ( | |
183 | "123.456", | |
184 | wxNumberFormatter::ToString(123.456, 3, wxNumberFormatter::Style_NoTrailingZeroes) | |
185 | ); | |
186 | ||
187 | CPPUNIT_ASSERT_EQUAL | |
188 | ( | |
189 | "123.456000000", | |
190 | wxNumberFormatter::ToString(123.456, 9) | |
191 | ); | |
192 | ||
193 | CPPUNIT_ASSERT_EQUAL | |
194 | ( | |
195 | "123.456", | |
196 | wxNumberFormatter::ToString(123.456, 9, wxNumberFormatter::Style_NoTrailingZeroes) | |
197 | ); | |
198 | } | |
199 | ||
200 | void NumFormatterTestCase::LongFromString() | |
201 | { | |
202 | if ( !m_locale ) | |
203 | return; | |
204 | ||
205 | WX_ASSERT_FAILS_WITH_ASSERT | |
206 | ( | |
207 | wxNumberFormatter::FromString("123", static_cast<long *>(0)) | |
208 | ); | |
209 | ||
210 | long l; | |
211 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l) ); | |
212 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l) ); | |
213 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l) ); | |
214 | ||
215 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l) ); | |
216 | CPPUNIT_ASSERT_EQUAL( 123, l ); | |
217 | ||
218 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l) ); | |
219 | CPPUNIT_ASSERT_EQUAL( 1234, l ); | |
220 | ||
221 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l) ); | |
222 | CPPUNIT_ASSERT_EQUAL( 1234, l ); | |
223 | ||
224 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l) ); | |
225 | CPPUNIT_ASSERT_EQUAL( 12345, l ); | |
226 | ||
227 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l) ); | |
228 | CPPUNIT_ASSERT_EQUAL( 123456, l ); | |
229 | ||
230 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l) ); | |
231 | CPPUNIT_ASSERT_EQUAL( 1234567, l ); | |
232 | } | |
233 | ||
234 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
235 | ||
236 | void NumFormatterTestCase::LongLongFromString() | |
237 | { | |
238 | if ( !m_locale ) | |
239 | return; | |
240 | ||
241 | WX_ASSERT_FAILS_WITH_ASSERT | |
242 | ( | |
243 | wxNumberFormatter::FromString("123", static_cast<wxLongLong_t *>(0)) | |
244 | ); | |
245 | ||
246 | wxLongLong_t l; | |
247 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l) ); | |
248 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l) ); | |
249 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l) ); | |
250 | ||
251 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l) ); | |
252 | CPPUNIT_ASSERT_EQUAL( 123, l ); | |
253 | ||
254 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l) ); | |
255 | CPPUNIT_ASSERT_EQUAL( 1234, l ); | |
256 | ||
257 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l) ); | |
258 | CPPUNIT_ASSERT_EQUAL( 1234, l ); | |
259 | ||
260 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l) ); | |
261 | CPPUNIT_ASSERT_EQUAL( 12345, l ); | |
262 | ||
263 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l) ); | |
264 | CPPUNIT_ASSERT_EQUAL( 123456, l ); | |
265 | ||
266 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l) ); | |
267 | CPPUNIT_ASSERT_EQUAL( 1234567, l ); | |
268 | } | |
269 | ||
270 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG | |
271 | ||
272 | void NumFormatterTestCase::DoubleFromString() | |
273 | { | |
274 | if ( !m_locale ) | |
275 | return; | |
276 | ||
277 | WX_ASSERT_FAILS_WITH_ASSERT | |
278 | ( | |
279 | wxNumberFormatter::FromString("123", static_cast<double *>(0)) | |
280 | ); | |
281 | ||
282 | double d; | |
283 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &d) ); | |
284 | CPPUNIT_ASSERT( !wxNumberFormatter::FromString("bar", &d) ); | |
285 | ||
286 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &d) ); | |
287 | CPPUNIT_ASSERT_EQUAL( 123., d ); | |
288 | ||
289 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123.456789012", &d) ); | |
290 | CPPUNIT_ASSERT_EQUAL( 123.456789012, d ); | |
291 | ||
292 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234.56789012", &d) ); | |
293 | CPPUNIT_ASSERT_EQUAL( 1234.56789012, d ); | |
294 | ||
295 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345.6789012", &d) ); | |
296 | CPPUNIT_ASSERT_EQUAL( 12345.6789012, d ); | |
297 | ||
298 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456.789012", &d) ); | |
299 | CPPUNIT_ASSERT_EQUAL( 123456.789012, d ); | |
300 | ||
301 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567.89012", &d) ); | |
302 | CPPUNIT_ASSERT_EQUAL( 1234567.89012, d ); | |
303 | ||
304 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345,678.9012", &d) ); | |
305 | CPPUNIT_ASSERT_EQUAL( 12345678.9012, d ); | |
306 | ||
307 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456,789.012", &d) ); | |
308 | CPPUNIT_ASSERT_EQUAL( 123456789.012, d ); | |
309 | ||
310 | CPPUNIT_ASSERT( wxNumberFormatter::FromString("123456789.012", &d) ); | |
311 | CPPUNIT_ASSERT_EQUAL( 123456789.012, d ); | |
312 | } |