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 its 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( "-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));
114 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
116 void NumFormatterTestCase::LongLongToString()
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)));
132 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
134 void NumFormatterTestCase::DoubleToString()
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));
153 void NumFormatterTestCase::NoTrailingZeroes()
155 WX_ASSERT_FAILS_WITH_ASSERT
157 wxNumberFormatter::ToString(123L, wxNumberFormatter::Style_NoTrailingZeroes
)
166 wxNumberFormatter::ToString(123., 3)
172 wxNumberFormatter::ToString(123., 3, wxNumberFormatter::Style_NoTrailingZeroes
)
178 wxNumberFormatter::ToString(123., 9, wxNumberFormatter::Style_NoTrailingZeroes
)
184 wxNumberFormatter::ToString(123.456, 3, wxNumberFormatter::Style_NoTrailingZeroes
)
190 wxNumberFormatter::ToString(123.456, 9)
196 wxNumberFormatter::ToString(123.456, 9, wxNumberFormatter::Style_NoTrailingZeroes
)
200 void NumFormatterTestCase::LongFromString()
205 WX_ASSERT_FAILS_WITH_ASSERT
207 wxNumberFormatter::FromString("123", static_cast<long *>(0))
211 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l
) );
212 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l
) );
213 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l
) );
215 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l
) );
216 CPPUNIT_ASSERT_EQUAL( 123, l
);
218 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l
) );
219 CPPUNIT_ASSERT_EQUAL( 1234, l
);
221 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l
) );
222 CPPUNIT_ASSERT_EQUAL( 1234, l
);
224 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l
) );
225 CPPUNIT_ASSERT_EQUAL( 12345, l
);
227 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l
) );
228 CPPUNIT_ASSERT_EQUAL( 123456, l
);
230 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l
) );
231 CPPUNIT_ASSERT_EQUAL( 1234567, l
);
234 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
236 void NumFormatterTestCase::LongLongFromString()
241 WX_ASSERT_FAILS_WITH_ASSERT
243 wxNumberFormatter::FromString("123", static_cast<wxLongLong_t
*>(0))
247 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l
) );
248 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l
) );
249 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l
) );
251 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l
) );
252 CPPUNIT_ASSERT_EQUAL( 123, l
);
254 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l
) );
255 CPPUNIT_ASSERT_EQUAL( 1234, l
);
257 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l
) );
258 CPPUNIT_ASSERT_EQUAL( 1234, l
);
260 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l
) );
261 CPPUNIT_ASSERT_EQUAL( 12345, l
);
263 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l
) );
264 CPPUNIT_ASSERT_EQUAL( 123456, l
);
266 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l
) );
267 CPPUNIT_ASSERT_EQUAL( 1234567, l
);
270 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
272 void NumFormatterTestCase::DoubleFromString()
277 WX_ASSERT_FAILS_WITH_ASSERT
279 wxNumberFormatter::FromString("123", static_cast<double *>(0))
283 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &d
) );
284 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("bar", &d
) );
286 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &d
) );
287 CPPUNIT_ASSERT_EQUAL( 123., d
);
289 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123.456789012", &d
) );
290 CPPUNIT_ASSERT_EQUAL( 123.456789012, d
);
292 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234.56789012", &d
) );
293 CPPUNIT_ASSERT_EQUAL( 1234.56789012, d
);
295 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345.6789012", &d
) );
296 CPPUNIT_ASSERT_EQUAL( 12345.6789012, d
);
298 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456.789012", &d
) );
299 CPPUNIT_ASSERT_EQUAL( 123456.789012, d
);
301 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567.89012", &d
) );
302 CPPUNIT_ASSERT_EQUAL( 1234567.89012, d
);
304 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345,678.9012", &d
) );
305 CPPUNIT_ASSERT_EQUAL( 12345678.9012, d
);
307 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456,789.012", &d
) );
308 CPPUNIT_ASSERT_EQUAL( 123456789.012, d
);
310 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123456789.012", &d
) );
311 CPPUNIT_ASSERT_EQUAL( 123456789.012, d
);