1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/strings/numformat.cpp
3 // Purpose: wxNumberFormatter unit test
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
19 #include "wx/numformatter.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 class NumFormatterTestCase
: public CppUnit::TestCase
29 NumFormatterTestCase() { m_locale
= NULL
; }
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() )
41 virtual void tearDown()
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();
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();
76 wxDECLARE_NO_COPY_CLASS(NumFormatterTestCase
);
79 // register in the unnamed registry so that these tests are run by default
80 CPPUNIT_TEST_SUITE_REGISTRATION( NumFormatterTestCase
);
82 // also include in its own registry so that these tests can be run alone
83 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NumFormatterTestCase
, "NumFormatterTestCase" );
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 void NumFormatterTestCase::LongToString()
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));
113 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
115 void NumFormatterTestCase::LongLongToString()
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)));
131 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
133 void NumFormatterTestCase::DoubleToString()
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));
152 void NumFormatterTestCase::NoTrailingZeroes()
154 WX_ASSERT_FAILS_WITH_ASSERT
156 wxNumberFormatter::ToString(123L, wxNumberFormatter::Style_NoTrailingZeroes
)
165 wxNumberFormatter::ToString(123., 3)
171 wxNumberFormatter::ToString(123., 3, wxNumberFormatter::Style_NoTrailingZeroes
)
177 wxNumberFormatter::ToString(123., 9, wxNumberFormatter::Style_NoTrailingZeroes
)
183 wxNumberFormatter::ToString(123.456, 3, wxNumberFormatter::Style_NoTrailingZeroes
)
189 wxNumberFormatter::ToString(123.456, 9)
195 wxNumberFormatter::ToString(123.456, 9, wxNumberFormatter::Style_NoTrailingZeroes
)
199 void NumFormatterTestCase::LongFromString()
204 WX_ASSERT_FAILS_WITH_ASSERT
206 wxNumberFormatter::FromString("123", static_cast<long *>(0))
210 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l
) );
211 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l
) );
212 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l
) );
214 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l
) );
215 CPPUNIT_ASSERT_EQUAL( 123, l
);
217 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l
) );
218 CPPUNIT_ASSERT_EQUAL( 1234, l
);
220 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l
) );
221 CPPUNIT_ASSERT_EQUAL( 1234, l
);
223 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l
) );
224 CPPUNIT_ASSERT_EQUAL( 12345, l
);
226 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l
) );
227 CPPUNIT_ASSERT_EQUAL( 123456, l
);
229 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l
) );
230 CPPUNIT_ASSERT_EQUAL( 1234567, l
);
233 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
235 void NumFormatterTestCase::LongLongFromString()
240 WX_ASSERT_FAILS_WITH_ASSERT
242 wxNumberFormatter::FromString("123", static_cast<wxLongLong_t
*>(0))
246 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l
) );
247 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l
) );
248 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l
) );
250 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l
) );
251 CPPUNIT_ASSERT_EQUAL( 123, l
);
253 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l
) );
254 CPPUNIT_ASSERT_EQUAL( 1234, l
);
256 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l
) );
257 CPPUNIT_ASSERT_EQUAL( 1234, l
);
259 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l
) );
260 CPPUNIT_ASSERT_EQUAL( 12345, l
);
262 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l
) );
263 CPPUNIT_ASSERT_EQUAL( 123456, l
);
265 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l
) );
266 CPPUNIT_ASSERT_EQUAL( 1234567, l
);
269 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
271 void NumFormatterTestCase::DoubleFromString()
276 WX_ASSERT_FAILS_WITH_ASSERT
278 wxNumberFormatter::FromString("123", static_cast<double *>(0))
282 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &d
) );
283 CPPUNIT_ASSERT( !wxNumberFormatter::FromString("bar", &d
) );
285 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &d
) );
286 CPPUNIT_ASSERT_EQUAL( 123., d
);
288 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123.456789012", &d
) );
289 CPPUNIT_ASSERT_EQUAL( 123.456789012, d
);
291 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234.56789012", &d
) );
292 CPPUNIT_ASSERT_EQUAL( 1234.56789012, d
);
294 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345.6789012", &d
) );
295 CPPUNIT_ASSERT_EQUAL( 12345.6789012, d
);
297 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456.789012", &d
) );
298 CPPUNIT_ASSERT_EQUAL( 123456.789012, d
);
300 CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567.89012", &d
) );
301 CPPUNIT_ASSERT_EQUAL( 1234567.89012, d
);
303 CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345,678.9012", &d
) );
304 CPPUNIT_ASSERT_EQUAL( 12345678.9012, d
);
306 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456,789.012", &d
) );
307 CPPUNIT_ASSERT_EQUAL( 123456789.012, d
);
309 CPPUNIT_ASSERT( wxNumberFormatter::FromString("123456789.012", &d
) );
310 CPPUNIT_ASSERT_EQUAL( 123456789.012, d
);