metatest.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/misc/metatest.cpp
  3. // Purpose: Test template meta-programming constructs
  4. // Author: Jaakko Salli
  5. // Copyright: (c) the wxWidgets team
  6. // Licence: wxWindows licence
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #ifdef __BORLANDC__
  10. # pragma hdrstop
  11. #endif
  12. #include "wx/object.h"
  13. #include "wx/utils.h"
  14. #include "wx/meta/pod.h"
  15. #include "wx/meta/movable.h"
  16. #ifndef wxNO_RTTI
  17. #include <typeinfo>
  18. #endif
  19. // ----------------------------------------------------------------------------
  20. // test class
  21. // ----------------------------------------------------------------------------
  22. class MetaProgrammingTestCase : public CppUnit::TestCase
  23. {
  24. public:
  25. MetaProgrammingTestCase() { }
  26. private:
  27. CPPUNIT_TEST_SUITE( MetaProgrammingTestCase );
  28. CPPUNIT_TEST( IsPod );
  29. CPPUNIT_TEST( IsMovable );
  30. CPPUNIT_TEST( ImplicitConversion );
  31. CPPUNIT_TEST( MinMax );
  32. CPPUNIT_TEST_SUITE_END();
  33. void IsPod();
  34. void IsMovable();
  35. void ImplicitConversion();
  36. void MinMax();
  37. DECLARE_NO_COPY_CLASS(MetaProgrammingTestCase)
  38. };
  39. // register in the unnamed registry so that these tests are run by default
  40. CPPUNIT_TEST_SUITE_REGISTRATION( MetaProgrammingTestCase );
  41. // also include in its own registry so that these tests can be run alone
  42. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MetaProgrammingTestCase,
  43. "MetaProgrammingTestCase" );
  44. void MetaProgrammingTestCase::IsPod()
  45. {
  46. CPPUNIT_ASSERT(wxIsPod<bool>::value);
  47. CPPUNIT_ASSERT(wxIsPod<signed int>::value);
  48. CPPUNIT_ASSERT(wxIsPod<double>::value);
  49. #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)
  50. CPPUNIT_ASSERT(wxIsPod<wxObject*>::value);
  51. #endif
  52. CPPUNIT_ASSERT(!wxIsPod<wxObject>::value);
  53. }
  54. void MetaProgrammingTestCase::IsMovable()
  55. {
  56. CPPUNIT_ASSERT(wxIsMovable<bool>::value);
  57. CPPUNIT_ASSERT(wxIsMovable<signed int>::value);
  58. CPPUNIT_ASSERT(wxIsMovable<double>::value);
  59. #if !defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)
  60. CPPUNIT_ASSERT(wxIsMovable<wxObject*>::value);
  61. #endif
  62. CPPUNIT_ASSERT(!wxIsMovable<wxObject>::value);
  63. }
  64. void MetaProgrammingTestCase::ImplicitConversion()
  65. {
  66. #ifndef wxNO_RTTI
  67. CPPUNIT_ASSERT(typeid(wxImplicitConversionType<char,int>::value) == typeid(int));
  68. CPPUNIT_ASSERT(typeid(wxImplicitConversionType<int,unsigned>::value) == typeid(unsigned));
  69. #ifdef wxLongLong_t
  70. CPPUNIT_ASSERT(typeid(wxImplicitConversionType<wxLongLong_t,float>::value) == typeid(float));
  71. #endif
  72. #endif // !wxNO_RTTI
  73. }
  74. void MetaProgrammingTestCase::MinMax()
  75. {
  76. // test that wxMax(1.1,1) returns float, not long int
  77. float f = wxMax(1.1f, 1l);
  78. CPPUNIT_ASSERT_EQUAL( 1.1f, f);
  79. // test that comparing signed and unsigned correctly returns unsigned: this
  80. // may seem counterintuitive in this case but this is consistent with the
  81. // standard C conversions
  82. CPPUNIT_ASSERT_EQUAL( 1, wxMin(-1, 1u) );
  83. CPPUNIT_ASSERT_EQUAL( -1., wxClip(-1.5, -1, 1) );
  84. CPPUNIT_ASSERT_EQUAL( 0, wxClip(0, -1, 1) );
  85. CPPUNIT_ASSERT_EQUAL( 1, wxClip(2l, -1, 1) );
  86. }