numformatter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #include "wx/numformatter.h"
  16. #include "wx/intl.h"
  17. // ----------------------------------------------------------------------------
  18. // test class
  19. // ----------------------------------------------------------------------------
  20. class NumFormatterTestCase : public CppUnit::TestCase
  21. {
  22. public:
  23. NumFormatterTestCase() { m_locale = NULL; }
  24. virtual void setUp()
  25. {
  26. // We need to use a locale with known decimal point and which uses the
  27. // thousands separator for the tests to make sense.
  28. m_locale = new wxLocale(wxLANGUAGE_ENGLISH_UK,
  29. wxLOCALE_DONT_LOAD_DEFAULT);
  30. if ( !m_locale->IsOk() )
  31. tearDown();
  32. }
  33. virtual void tearDown()
  34. {
  35. delete m_locale;
  36. m_locale = NULL;
  37. }
  38. private:
  39. CPPUNIT_TEST_SUITE( NumFormatterTestCase );
  40. CPPUNIT_TEST( LongToString );
  41. #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  42. CPPUNIT_TEST( LongLongToString );
  43. #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  44. CPPUNIT_TEST( DoubleToString );
  45. CPPUNIT_TEST( NoTrailingZeroes );
  46. CPPUNIT_TEST( LongFromString );
  47. #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  48. CPPUNIT_TEST( LongLongFromString );
  49. #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  50. CPPUNIT_TEST( DoubleFromString );
  51. CPPUNIT_TEST_SUITE_END();
  52. void LongToString();
  53. #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  54. void LongLongToString();
  55. #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  56. void DoubleToString();
  57. void NoTrailingZeroes();
  58. void LongFromString();
  59. #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  60. void LongLongFromString();
  61. #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  62. void DoubleFromString();
  63. wxLocale *m_locale;
  64. wxDECLARE_NO_COPY_CLASS(NumFormatterTestCase);
  65. };
  66. // register in the unnamed registry so that these tests are run by default
  67. CPPUNIT_TEST_SUITE_REGISTRATION( NumFormatterTestCase );
  68. // also include in its own registry so that these tests can be run alone
  69. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NumFormatterTestCase, "NumFormatterTestCase" );
  70. // ----------------------------------------------------------------------------
  71. // tests themselves
  72. // ----------------------------------------------------------------------------
  73. void NumFormatterTestCase::LongToString()
  74. {
  75. if ( !m_locale )
  76. return;
  77. CPPUNIT_ASSERT_EQUAL( "1", wxNumberFormatter::ToString( 1L));
  78. CPPUNIT_ASSERT_EQUAL( "-1", wxNumberFormatter::ToString( -1L));
  79. CPPUNIT_ASSERT_EQUAL( "12", wxNumberFormatter::ToString( 12L));
  80. CPPUNIT_ASSERT_EQUAL( "-12", wxNumberFormatter::ToString( -12L));
  81. CPPUNIT_ASSERT_EQUAL( "123", wxNumberFormatter::ToString( 123L));
  82. CPPUNIT_ASSERT_EQUAL( "-123", wxNumberFormatter::ToString( -123L));
  83. CPPUNIT_ASSERT_EQUAL( "1,234", wxNumberFormatter::ToString( 1234L));
  84. CPPUNIT_ASSERT_EQUAL( "-1,234", wxNumberFormatter::ToString( -1234L));
  85. CPPUNIT_ASSERT_EQUAL( "12,345", wxNumberFormatter::ToString( 12345L));
  86. CPPUNIT_ASSERT_EQUAL( "-12,345", wxNumberFormatter::ToString( -12345L));
  87. CPPUNIT_ASSERT_EQUAL( "123,456", wxNumberFormatter::ToString( 123456L));
  88. CPPUNIT_ASSERT_EQUAL( "-123,456", wxNumberFormatter::ToString( -123456L));
  89. CPPUNIT_ASSERT_EQUAL( "1,234,567", wxNumberFormatter::ToString( 1234567L));
  90. CPPUNIT_ASSERT_EQUAL( "-1,234,567", wxNumberFormatter::ToString( -1234567L));
  91. CPPUNIT_ASSERT_EQUAL( "12,345,678", wxNumberFormatter::ToString( 12345678L));
  92. CPPUNIT_ASSERT_EQUAL("-12,345,678", wxNumberFormatter::ToString( -12345678L));
  93. CPPUNIT_ASSERT_EQUAL("123,456,789", wxNumberFormatter::ToString( 123456789L));
  94. }
  95. #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  96. void NumFormatterTestCase::LongLongToString()
  97. {
  98. if ( !m_locale )
  99. return;
  100. CPPUNIT_ASSERT_EQUAL( "1", wxNumberFormatter::ToString(wxLL( 1)));
  101. CPPUNIT_ASSERT_EQUAL( "12", wxNumberFormatter::ToString(wxLL( 12)));
  102. CPPUNIT_ASSERT_EQUAL( "123", wxNumberFormatter::ToString(wxLL( 123)));
  103. CPPUNIT_ASSERT_EQUAL( "1,234", wxNumberFormatter::ToString(wxLL( 1234)));
  104. CPPUNIT_ASSERT_EQUAL( "12,345", wxNumberFormatter::ToString(wxLL( 12345)));
  105. CPPUNIT_ASSERT_EQUAL( "123,456", wxNumberFormatter::ToString(wxLL( 123456)));
  106. CPPUNIT_ASSERT_EQUAL( "1,234,567", wxNumberFormatter::ToString(wxLL( 1234567)));
  107. CPPUNIT_ASSERT_EQUAL( "12,345,678", wxNumberFormatter::ToString(wxLL( 12345678)));
  108. CPPUNIT_ASSERT_EQUAL("123,456,789", wxNumberFormatter::ToString(wxLL( 123456789)));
  109. }
  110. #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  111. void NumFormatterTestCase::DoubleToString()
  112. {
  113. if ( !m_locale )
  114. return;
  115. CPPUNIT_ASSERT_EQUAL("1.0", wxNumberFormatter::ToString(1., 1));
  116. CPPUNIT_ASSERT_EQUAL("0.123456", wxNumberFormatter::ToString(0.123456, 6));
  117. CPPUNIT_ASSERT_EQUAL("1.234567", wxNumberFormatter::ToString(1.234567, 6));
  118. CPPUNIT_ASSERT_EQUAL("12.34567", wxNumberFormatter::ToString(12.34567, 5));
  119. CPPUNIT_ASSERT_EQUAL("123.4567", wxNumberFormatter::ToString(123.4567, 4));
  120. CPPUNIT_ASSERT_EQUAL("1,234.56", wxNumberFormatter::ToString(1234.56, 2));
  121. CPPUNIT_ASSERT_EQUAL("12,345.6", wxNumberFormatter::ToString(12345.6, 1));
  122. CPPUNIT_ASSERT_EQUAL("12,345.6", wxNumberFormatter::ToString(12345.6, 1));
  123. CPPUNIT_ASSERT_EQUAL("123,456,789.0",
  124. wxNumberFormatter::ToString(123456789., 1));
  125. CPPUNIT_ASSERT_EQUAL("123,456,789.012",
  126. wxNumberFormatter::ToString(123456789.012, 3));
  127. }
  128. void NumFormatterTestCase::NoTrailingZeroes()
  129. {
  130. WX_ASSERT_FAILS_WITH_ASSERT
  131. (
  132. wxNumberFormatter::ToString(123L, wxNumberFormatter::Style_NoTrailingZeroes)
  133. );
  134. if ( !m_locale )
  135. return;
  136. CPPUNIT_ASSERT_EQUAL
  137. (
  138. "123.000",
  139. wxNumberFormatter::ToString(123., 3)
  140. );
  141. CPPUNIT_ASSERT_EQUAL
  142. (
  143. "123",
  144. wxNumberFormatter::ToString(123., 3, wxNumberFormatter::Style_NoTrailingZeroes)
  145. );
  146. CPPUNIT_ASSERT_EQUAL
  147. (
  148. "123",
  149. wxNumberFormatter::ToString(123., 9, wxNumberFormatter::Style_NoTrailingZeroes)
  150. );
  151. CPPUNIT_ASSERT_EQUAL
  152. (
  153. "123.456",
  154. wxNumberFormatter::ToString(123.456, 3, wxNumberFormatter::Style_NoTrailingZeroes)
  155. );
  156. CPPUNIT_ASSERT_EQUAL
  157. (
  158. "123.456000000",
  159. wxNumberFormatter::ToString(123.456, 9)
  160. );
  161. CPPUNIT_ASSERT_EQUAL
  162. (
  163. "123.456",
  164. wxNumberFormatter::ToString(123.456, 9, wxNumberFormatter::Style_NoTrailingZeroes)
  165. );
  166. }
  167. void NumFormatterTestCase::LongFromString()
  168. {
  169. if ( !m_locale )
  170. return;
  171. WX_ASSERT_FAILS_WITH_ASSERT
  172. (
  173. wxNumberFormatter::FromString("123", static_cast<long *>(0))
  174. );
  175. long l;
  176. CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l) );
  177. CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l) );
  178. CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l) );
  179. CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l) );
  180. CPPUNIT_ASSERT_EQUAL( 123, l );
  181. CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l) );
  182. CPPUNIT_ASSERT_EQUAL( 1234, l );
  183. CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l) );
  184. CPPUNIT_ASSERT_EQUAL( 1234, l );
  185. CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l) );
  186. CPPUNIT_ASSERT_EQUAL( 12345, l );
  187. CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l) );
  188. CPPUNIT_ASSERT_EQUAL( 123456, l );
  189. CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l) );
  190. CPPUNIT_ASSERT_EQUAL( 1234567, l );
  191. }
  192. #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  193. void NumFormatterTestCase::LongLongFromString()
  194. {
  195. if ( !m_locale )
  196. return;
  197. WX_ASSERT_FAILS_WITH_ASSERT
  198. (
  199. wxNumberFormatter::FromString("123", static_cast<wxLongLong_t *>(0))
  200. );
  201. wxLongLong_t l;
  202. CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l) );
  203. CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l) );
  204. CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l) );
  205. CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l) );
  206. CPPUNIT_ASSERT_EQUAL( 123, l );
  207. CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l) );
  208. CPPUNIT_ASSERT_EQUAL( 1234, l );
  209. CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l) );
  210. CPPUNIT_ASSERT_EQUAL( 1234, l );
  211. CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l) );
  212. CPPUNIT_ASSERT_EQUAL( 12345, l );
  213. CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l) );
  214. CPPUNIT_ASSERT_EQUAL( 123456, l );
  215. CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l) );
  216. CPPUNIT_ASSERT_EQUAL( 1234567, l );
  217. }
  218. #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  219. void NumFormatterTestCase::DoubleFromString()
  220. {
  221. if ( !m_locale )
  222. return;
  223. WX_ASSERT_FAILS_WITH_ASSERT
  224. (
  225. wxNumberFormatter::FromString("123", static_cast<double *>(0))
  226. );
  227. double d;
  228. CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &d) );
  229. CPPUNIT_ASSERT( !wxNumberFormatter::FromString("bar", &d) );
  230. CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &d) );
  231. CPPUNIT_ASSERT_EQUAL( 123., d );
  232. CPPUNIT_ASSERT( wxNumberFormatter::FromString("123.456789012", &d) );
  233. CPPUNIT_ASSERT_EQUAL( 123.456789012, d );
  234. CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234.56789012", &d) );
  235. CPPUNIT_ASSERT_EQUAL( 1234.56789012, d );
  236. CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345.6789012", &d) );
  237. CPPUNIT_ASSERT_EQUAL( 12345.6789012, d );
  238. CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456.789012", &d) );
  239. CPPUNIT_ASSERT_EQUAL( 123456.789012, d );
  240. CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567.89012", &d) );
  241. CPPUNIT_ASSERT_EQUAL( 1234567.89012, d );
  242. CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345,678.9012", &d) );
  243. CPPUNIT_ASSERT_EQUAL( 12345678.9012, d );
  244. CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456,789.012", &d) );
  245. CPPUNIT_ASSERT_EQUAL( 123456789.012, d );
  246. CPPUNIT_ASSERT( wxNumberFormatter::FromString("123456789.012", &d) );
  247. CPPUNIT_ASSERT_EQUAL( 123456789.012, d );
  248. }