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