1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/numformat.cpp
3 // Purpose: wxNumberFormatter unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
20 #include "wx/numformatter.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 class NumFormatterTestCase
: public CppUnit::TestCase
30 NumFormatterTestCase() { m_locale
= NULL
; }
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() )
42 virtual void tearDown()
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();
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();
77 wxDECLARE_NO_COPY_CLASS(NumFormatterTestCase
);
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION( NumFormatterTestCase
);
83 // also include in it's own registry so that these tests can be run alone
84 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NumFormatterTestCase
, "NumFormatterTestCase" );
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 void NumFormatterTestCase::LongToString()
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));
106 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
108 void NumFormatterTestCase::LongLongToString()
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)));
124 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
126 void NumFormatterTestCase::DoubleToString()
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));
145 void NumFormatterTestCase::NoTrailingZeroes()
147 WX_ASSERT_FAILS_WITH_ASSERT
149 wxNumberFormatter::ToString(123L, wxNumberFormatter::Style_NoTrailingZeroes
)
158 wxNumberFormatter::ToString(123., 3)
164 wxNumberFormatter::ToString(123., 3, wxNumberFormatter::Style_NoTrailingZeroes
)
170 wxNumberFormatter::ToString(123., 9, wxNumberFormatter::Style_NoTrailingZeroes
)
176 wxNumberFormatter::ToString(123.456, 3, wxNumberFormatter::Style_NoTrailingZeroes
)
182 wxNumberFormatter::ToString(123.456, 9)
188 wxNumberFormatter::ToString(123.456, 9, wxNumberFormatter::Style_NoTrailingZeroes
)
192 void NumFormatterTestCase::LongFromString()
197 WX_ASSERT_FAILS_WITH_ASSERT
199 wxNumberFormatter::FromString("123", static_cast<long *>(0))
203 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l
) );
204 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l
) );
205 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l
) );
207 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l
) );
208 CPPUNIT_ASSERT_EQUAL( 123, l
);
210 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l
) );
211 CPPUNIT_ASSERT_EQUAL( 1234, l
);
213 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l
) );
214 CPPUNIT_ASSERT_EQUAL( 1234, l
);
216 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l
) );
217 CPPUNIT_ASSERT_EQUAL( 12345, l
);
219 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l
) );
220 CPPUNIT_ASSERT_EQUAL( 123456, l
);
222 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l
) );
223 CPPUNIT_ASSERT_EQUAL( 1234567, l
);
226 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
228 void NumFormatterTestCase::LongLongFromString()
233 WX_ASSERT_FAILS_WITH_ASSERT
235 wxNumberFormatter::FromString("123", static_cast<wxLongLong_t
*>(0))
239 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l
) );
240 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l
) );
241 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l
) );
243 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l
) );
244 CPPUNIT_ASSERT_EQUAL( 123, l
);
246 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l
) );
247 CPPUNIT_ASSERT_EQUAL( 1234, l
);
249 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l
) );
250 CPPUNIT_ASSERT_EQUAL( 1234, l
);
252 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l
) );
253 CPPUNIT_ASSERT_EQUAL( 12345, l
);
255 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l
) );
256 CPPUNIT_ASSERT_EQUAL( 123456, l
);
258 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l
) );
259 CPPUNIT_ASSERT_EQUAL( 1234567, l
);
262 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
264 void NumFormatterTestCase::DoubleFromString()
269 WX_ASSERT_FAILS_WITH_ASSERT
271 wxNumberFormatter::FromString("123", static_cast<double *>(0))
275 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &d
) );
276 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("bar", &d
) );
278 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &d
) );
279 CPPUNIT_ASSERT_EQUAL( 123., d
);
281 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123.456789012", &d
) );
282 CPPUNIT_ASSERT_EQUAL( 123.456789012, d
);
284 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234.56789012", &d
) );
285 CPPUNIT_ASSERT_EQUAL( 1234.56789012, d
);
287 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345.6789012", &d
) );
288 CPPUNIT_ASSERT_EQUAL( 12345.6789012, d
);
290 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456.789012", &d
) );
291 CPPUNIT_ASSERT_EQUAL( 123456.789012, d
);
293 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567.89012", &d
) );
294 CPPUNIT_ASSERT_EQUAL( 1234567.89012, d
);
296 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345,678.9012", &d
) );
297 CPPUNIT_ASSERT_EQUAL( 12345678.9012, d
);
299 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456,789.012", &d
) );
300 CPPUNIT_ASSERT_EQUAL( 123456789.012, d
);
302 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123456789.012", &d
) );
303 CPPUNIT_ASSERT_EQUAL( 123456789.012, d
);