misctests.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/misc/misctests.cpp
  3. // Purpose: test miscellaneous stuff
  4. // Author: Peter Most, Vadim Zeitlin
  5. // Created: 2008-07-10
  6. // Copyright: (c) 2008 Peter Most
  7. // (c) 2009 Vadim Zeitlin
  8. ///////////////////////////////////////////////////////////////////////////////
  9. // ----------------------------------------------------------------------------
  10. // headers
  11. // ----------------------------------------------------------------------------
  12. #include "testprec.h"
  13. #ifdef __BORLANDC__
  14. #pragma hdrstop
  15. #endif
  16. #include "wx/defs.h"
  17. // just some classes using wxRTTI for wxStaticCast() test
  18. #include "wx/tarstrm.h"
  19. #include "wx/zipstrm.h"
  20. // ----------------------------------------------------------------------------
  21. // test class
  22. // ----------------------------------------------------------------------------
  23. class MiscTestCase : public CppUnit::TestCase
  24. {
  25. public:
  26. MiscTestCase() { }
  27. private:
  28. CPPUNIT_TEST_SUITE( MiscTestCase );
  29. CPPUNIT_TEST( Assert );
  30. CPPUNIT_TEST( Delete );
  31. CPPUNIT_TEST( StaticCast );
  32. CPPUNIT_TEST_SUITE_END();
  33. void Assert();
  34. void Delete();
  35. void StaticCast();
  36. DECLARE_NO_COPY_CLASS(MiscTestCase)
  37. };
  38. // register in the unnamed registry so that these tests are run by default
  39. CPPUNIT_TEST_SUITE_REGISTRATION( MiscTestCase );
  40. // also include in its own registry so that these tests can be run alone
  41. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscTestCase, "MiscTestCase" );
  42. namespace
  43. {
  44. bool AssertIfOdd(int n)
  45. {
  46. wxCHECK_MSG( !(n % 2), false, "parameter must be even" );
  47. return true;
  48. }
  49. } // anonymous namespace
  50. void MiscTestCase::Assert()
  51. {
  52. AssertIfOdd(0);
  53. WX_ASSERT_FAILS_WITH_ASSERT(AssertIfOdd(1));
  54. // doesn't fail any more
  55. wxAssertHandler_t oldHandler = wxSetAssertHandler(NULL);
  56. AssertIfOdd(17);
  57. wxSetAssertHandler(oldHandler);
  58. }
  59. void MiscTestCase::Delete()
  60. {
  61. // Allocate some arbitrary memory to get a valid pointer:
  62. long *pointer = new long;
  63. CPPUNIT_ASSERT( pointer != NULL );
  64. // Check that wxDELETE sets the pointer to NULL:
  65. wxDELETE( pointer );
  66. CPPUNIT_ASSERT( pointer == NULL );
  67. // Allocate some arbitrary array to get a valid pointer:
  68. long *array = new long[ 3 ];
  69. CPPUNIT_ASSERT( array != NULL );
  70. // Check that wxDELETEA sets the pointer to NULL:
  71. wxDELETE( array );
  72. CPPUNIT_ASSERT( array == NULL );
  73. // this results in compilation error, as it should
  74. #if 0
  75. struct SomeUnknownStruct *p = NULL;
  76. wxDELETE(p);
  77. #endif
  78. }
  79. namespace
  80. {
  81. // helper function used just to avoid warnings about value computed not being
  82. // used in WX_ASSERT_FAILS_WITH_ASSERT() in StaticCast() below
  83. bool IsNull(void *p)
  84. {
  85. return p == NULL;
  86. }
  87. } // anonymous namespace
  88. void MiscTestCase::StaticCast()
  89. {
  90. #if wxUSE_TARSTREAM
  91. wxTarEntry tarEntry;
  92. CPPUNIT_ASSERT( wxStaticCast(&tarEntry, wxArchiveEntry) );
  93. wxArchiveEntry *entry = &tarEntry;
  94. CPPUNIT_ASSERT( wxStaticCast(entry, wxTarEntry) );
  95. #if wxUSE_ZIPSTREAM
  96. wxZipEntry zipEntry;
  97. entry = &zipEntry;
  98. CPPUNIT_ASSERT( wxStaticCast(entry, wxZipEntry) );
  99. WX_ASSERT_FAILS_WITH_ASSERT( IsNull(wxStaticCast(&zipEntry, wxTarEntry)) );
  100. #endif // wxUSE_ZIPSTREAM
  101. WX_ASSERT_FAILS_WITH_ASSERT( IsNull(wxStaticCast(entry, wxTarEntry)) );
  102. #endif // wxUSE_TARSTREAM
  103. }