region.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/geometry/region.cpp
  3. // Purpose: wxRegion unit test
  4. // Author: Vadim Zeitlin
  5. // Created: 2011-10-12
  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. #ifndef WX_PRECOMP
  16. #include "wx/region.h"
  17. #endif // WX_PRECOMP
  18. #include "wx/iosfwrap.h"
  19. // ----------------------------------------------------------------------------
  20. // helper functions
  21. // ----------------------------------------------------------------------------
  22. namespace
  23. {
  24. // This function could be easily added to wxRegionIterator itself, where it
  25. // could be implemented far more efficiently as all major platforms store the
  26. // number of rectangles anyhow, but as we only use it for debugging purposes,
  27. // just keep it here for now.
  28. unsigned GetRectsCount(const wxRegion& rgn)
  29. {
  30. unsigned count = 0;
  31. for ( wxRegionIterator iter(rgn); iter.HaveRects(); ++iter )
  32. count++;
  33. return count;
  34. }
  35. } // anonymous namespace
  36. // this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRegions
  37. std::ostream& operator<<(std::ostream& os, const wxRegion& rgn)
  38. {
  39. wxRect r = rgn.GetBox();
  40. os << "# rects = " << GetRectsCount(rgn)
  41. << "; bounding box {"
  42. << r.x << ", " << r.y << ", " << r.width << ", " << r.height
  43. << "}";
  44. return os;
  45. }
  46. // ----------------------------------------------------------------------------
  47. // test class
  48. // ----------------------------------------------------------------------------
  49. class RegionTestCase : public CppUnit::TestCase
  50. {
  51. public:
  52. RegionTestCase() { }
  53. private:
  54. CPPUNIT_TEST_SUITE( RegionTestCase );
  55. CPPUNIT_TEST( Validity );
  56. CPPUNIT_TEST( Intersect );
  57. CPPUNIT_TEST_SUITE_END();
  58. void Validity();
  59. void Intersect();
  60. wxDECLARE_NO_COPY_CLASS(RegionTestCase);
  61. };
  62. // register in the unnamed registry so that these tests are run by default
  63. CPPUNIT_TEST_SUITE_REGISTRATION( RegionTestCase );
  64. // also include in its own registry so that these tests can be run alone
  65. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegionTestCase, "RegionTestCase" );
  66. void RegionTestCase::Validity()
  67. {
  68. wxRegion r;
  69. CPPUNIT_ASSERT_MESSAGE
  70. (
  71. "Default constructed region must be invalid",
  72. !r.IsOk()
  73. );
  74. CPPUNIT_ASSERT_MESSAGE
  75. (
  76. "Invalid region should be empty",
  77. r.IsEmpty()
  78. );
  79. // Offsetting an invalid region doesn't make sense.
  80. WX_ASSERT_FAILS_WITH_ASSERT( r.Offset(1, 1) );
  81. CPPUNIT_ASSERT_MESSAGE
  82. (
  83. "Combining with a valid region should create a valid region",
  84. r.Union(0, 0, 10, 10)
  85. );
  86. CPPUNIT_ASSERT_EQUAL_MESSAGE
  87. (
  88. "Union() with invalid region should give the same region",
  89. wxRegion(0, 0, 10, 10),
  90. r
  91. );
  92. }
  93. void RegionTestCase::Intersect()
  94. {
  95. const wxPoint points1[] = {
  96. wxPoint(310, 392),
  97. wxPoint(270, 392),
  98. wxPoint(270, 421),
  99. wxPoint(310, 421)
  100. };
  101. wxRegion region1(WXSIZEOF(points1), points1);
  102. const wxPoint points2[] = {
  103. wxPoint(54, 104),
  104. wxPoint(85, 82),
  105. wxPoint(68, 58),
  106. wxPoint(37, 80)
  107. };
  108. wxRegion region2(4,points2);
  109. CPPUNIT_ASSERT( region1.Intersect(region2) );
  110. CPPUNIT_ASSERT( region1.IsEmpty() );
  111. }