rect.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/geometry/rect.cpp
  3. // Purpose: wxRect unit test
  4. // Author: Vadim Zeitlin
  5. // Created: 2004-12-11
  6. // Copyright: (c) 2004 wxWindows
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/gdicmn.h"
  17. #endif // WX_PRECOMP
  18. #include "wx/iosfwrap.h"
  19. // ----------------------------------------------------------------------------
  20. // helper functions
  21. // ----------------------------------------------------------------------------
  22. // this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRects
  23. std::ostream& operator<<(std::ostream& os, const wxRect& r)
  24. {
  25. os << "{"
  26. << r.x << ", " << r.y << ", " << r.width << ", " << r.height
  27. << "}";
  28. return os;
  29. }
  30. // ----------------------------------------------------------------------------
  31. // test class
  32. // ----------------------------------------------------------------------------
  33. class RectTestCase : public CppUnit::TestCase
  34. {
  35. public:
  36. RectTestCase() { }
  37. private:
  38. CPPUNIT_TEST_SUITE( RectTestCase );
  39. CPPUNIT_TEST( CentreIn );
  40. CPPUNIT_TEST( InflateDeflate );
  41. CPPUNIT_TEST( Operators );
  42. CPPUNIT_TEST( Union );
  43. CPPUNIT_TEST_SUITE_END();
  44. void CentreIn();
  45. void InflateDeflate();
  46. void Operators();
  47. void Union();
  48. DECLARE_NO_COPY_CLASS(RectTestCase)
  49. };
  50. // register in the unnamed registry so that these tests are run by default
  51. CPPUNIT_TEST_SUITE_REGISTRATION( RectTestCase );
  52. // also include in its own registry so that these tests can be run alone
  53. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RectTestCase, "RectTestCase" );
  54. void RectTestCase::CentreIn()
  55. {
  56. typedef wxRect R;
  57. CPPUNIT_ASSERT_EQUAL( R(45, 45, 10, 10),
  58. R(0, 0, 10, 10).CentreIn(R(0, 0, 100, 100)));
  59. CPPUNIT_ASSERT_EQUAL( R(-5, -5, 20, 20),
  60. R(0, 0, 20, 20).CentreIn(R(0, 0, 10, 10)));
  61. }
  62. void RectTestCase::InflateDeflate()
  63. {
  64. // This is the rectangle from the example in the documentation of wxRect::Inflate().
  65. const wxRect r1(10, 10, 20, 40);
  66. CPPUNIT_ASSERT(r1.Inflate( 10, 10)==wxRect( 0, 0, 40, 60));
  67. CPPUNIT_ASSERT(r1.Inflate( 20, 30)==wxRect(-10, -20, 60, 100));
  68. CPPUNIT_ASSERT(r1.Inflate(-10, -10)==wxRect( 20, 20, 0, 20));
  69. CPPUNIT_ASSERT(r1.Inflate(-15, -15)==wxRect( 20, 25, 0, 10));
  70. CPPUNIT_ASSERT(r1.Inflate( 10, 10)==r1.Deflate(-10, -10));
  71. CPPUNIT_ASSERT(r1.Inflate( 20, 30)==r1.Deflate(-20, -30));
  72. CPPUNIT_ASSERT(r1.Inflate(-10, -10)==r1.Deflate( 10, 10));
  73. CPPUNIT_ASSERT(r1.Inflate(-15, -15)==r1.Deflate( 15, 15));
  74. }
  75. void RectTestCase::Operators()
  76. {
  77. // test + operator which works like Union but does not ignore empty rectangles
  78. static const struct RectData
  79. {
  80. int x1, y1, w1, h1;
  81. int x2, y2, w2, h2;
  82. int x, y, w, h;
  83. wxRect GetFirst() const { return wxRect(x1, y1, w1, h1); }
  84. wxRect GetSecond() const { return wxRect(x2, y2, w2, h2); }
  85. wxRect GetResult() const { return wxRect(x, y, w, h); }
  86. } s_rects[] =
  87. {
  88. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  89. { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 2, 2 },
  90. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  91. { 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 4, 4 },
  92. { 1, 1, 2, 2, 4, 4, 1, 1, 1, 1, 4, 4 },
  93. { 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 6, 6 },
  94. { 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4 }
  95. };
  96. for ( size_t n = 0; n < WXSIZEOF(s_rects); n++ )
  97. {
  98. const RectData& data = s_rects[n];
  99. CPPUNIT_ASSERT(
  100. ( data.GetFirst() + data.GetSecond() ) == data.GetResult()
  101. );
  102. CPPUNIT_ASSERT(
  103. ( data.GetSecond() + data.GetFirst() ) == data.GetResult()
  104. );
  105. }
  106. // test operator*() which returns the intersection of two rectangles
  107. wxRect r1 = wxRect(0, 2, 3, 4);
  108. wxRect r2 = wxRect(1, 2, 7, 8);
  109. r1 *= r2;
  110. CPPUNIT_ASSERT(wxRect(1, 2, 2, 4) == r1);
  111. CPPUNIT_ASSERT( (r1 * wxRect()).IsEmpty() );
  112. }
  113. void RectTestCase::Union()
  114. {
  115. static const struct RectData
  116. {
  117. int x1, y1, w1, h1;
  118. int x2, y2, w2, h2;
  119. int x, y, w, h;
  120. wxRect GetFirst() const { return wxRect(x1, y1, w1, h1); }
  121. wxRect GetSecond() const { return wxRect(x2, y2, w2, h2); }
  122. wxRect GetResult() const { return wxRect(x, y, w, h); }
  123. } s_rects[] =
  124. {
  125. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  126. { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
  127. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  128. { 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 4, 4 },
  129. { 1, 1, 2, 2, 4, 4, 1, 1, 1, 1, 4, 4 },
  130. { 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 6, 6 },
  131. { 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4 }
  132. };
  133. for ( size_t n = 0; n < WXSIZEOF(s_rects); n++ )
  134. {
  135. const RectData& data = s_rects[n];
  136. CPPUNIT_ASSERT(
  137. data.GetFirst().Union(data.GetSecond()) == data.GetResult()
  138. );
  139. CPPUNIT_ASSERT(
  140. data.GetSecond().Union(data.GetFirst()) == data.GetResult()
  141. );
  142. }
  143. }