File: region.cpp

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (139 lines) | stat: -rw-r--r-- 3,644 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/geometry/region.cpp
// Purpose:     wxRegion unit test
// Author:      Vadim Zeitlin
// Created:     2011-10-12
// Copyright:   (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#include "testprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/region.h"
#endif // WX_PRECOMP

#include "wx/iosfwrap.h"

// ----------------------------------------------------------------------------
// helper functions
// ----------------------------------------------------------------------------

namespace
{

// This function could be easily added to wxRegionIterator itself, where it
// could be implemented far more efficiently as all major platforms store the
// number of rectangles anyhow, but as we only use it for debugging purposes,
// just keep it here for now.
unsigned GetRectsCount(const wxRegion& rgn)
{
    unsigned count = 0;
    for ( wxRegionIterator iter(rgn); iter.HaveRects(); ++iter )
        count++;
    return count;
}

} // anonymous namespace

// this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRegions
std::ostream& operator<<(std::ostream& os, const wxRegion& rgn)
{
    wxRect r = rgn.GetBox();
    os << "# rects = " << GetRectsCount(rgn)
       << "; bounding box {"
       << r.x << ", " << r.y << ", " << r.width << ", " << r.height
       << "}";
    return os;
}

// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------

class RegionTestCase : public CppUnit::TestCase
{
public:
    RegionTestCase() { }

private:
    CPPUNIT_TEST_SUITE( RegionTestCase );
        CPPUNIT_TEST( Validity );
        CPPUNIT_TEST( Intersect );
    CPPUNIT_TEST_SUITE_END();

    void Validity();
    void Intersect();

    wxDECLARE_NO_COPY_CLASS(RegionTestCase);
};

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( RegionTestCase );

// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegionTestCase, "RegionTestCase" );

void RegionTestCase::Validity()
{
    wxRegion r;

    CPPUNIT_ASSERT_MESSAGE
    (
        "Default constructed region must be invalid",
        !r.IsOk()
    );

    CPPUNIT_ASSERT_MESSAGE
    (
        "Invalid region should be empty",
        r.IsEmpty()
    );

    // Offsetting an invalid region doesn't make sense.
    WX_ASSERT_FAILS_WITH_ASSERT( r.Offset(1, 1) );

    CPPUNIT_ASSERT_MESSAGE
    (
        "Combining with a valid region should create a valid region",
        r.Union(0, 0, 10, 10)
    );

    CPPUNIT_ASSERT_EQUAL_MESSAGE
    (
        "Union() with invalid region should give the same region",
        wxRegion(0, 0, 10, 10),
        r
    );
}

void RegionTestCase::Intersect()
{
    const wxPoint points1[] = {
        wxPoint(310, 392),
        wxPoint(270, 392),
        wxPoint(270, 421),
        wxPoint(310, 421)
    };

    wxRegion region1(WXSIZEOF(points1), points1);

    const wxPoint points2[] = {
        wxPoint(54, 104),
        wxPoint(85,  82),
        wxPoint(68,  58),
        wxPoint(37,  80)
    };

    wxRegion region2(4,points2);

    CPPUNIT_ASSERT( region1.Intersect(region2) );
    CPPUNIT_ASSERT( region1.IsEmpty() );
}