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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/** @file
* @brief Unit tests for Rect, OptRect, IntRect, and OptIntRect.
* Uses the Google Testing Framework
*//*
* Authors:
* Krzysztof KosiĆski <tweenk.pl@gmail.com>
*
* Copyright 2010 Authors
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*/
#include <gtest/gtest.h>
#include <2geom/coord.h>
#include <2geom/rect.h>
namespace Geom {
typedef ::testing::Types<Coord, IntCoord> CoordTypes;
TEST(RectTest, Upconversion) {
IntRect ir(0, -27, 10, 202);
Rect r_a(ir);
Rect r_b = ir;
OptIntRect oir_a(ir);
OptIntRect oir_b = ir;
OptRect or_a(oir_a);
OptRect or_b = oir_b;
EXPECT_EQ(r_a, ir);
EXPECT_EQ(r_a, r_b);
EXPECT_EQ(r_a, *oir_a);
EXPECT_EQ(r_a, *oir_b);
EXPECT_EQ(r_a, *or_a);
EXPECT_EQ(r_a, *or_b);
EXPECT_EQ(oir_a, oir_b);
EXPECT_EQ(or_a, or_b);
}
TEST(RectTest, Rounding) {
Rect r(-0.5, -0.5, 5.5, 5.5);
Rect r_small(0.3, 0.0, 0.6, 10.0);
Rect r_int(0,0,10,10);
IntRect out(-1, -1, 6, 6);
IntRect out_small(0, 0, 1, 10);
IntRect out_int(0,0,10,10);
OptIntRect in = IntRect(0, 0, 5, 5);
EXPECT_EQ(r.roundOutwards(), out);
EXPECT_EQ(r_small.roundOutwards(), out_small);
EXPECT_EQ(r_int.roundOutwards(), out_int);
EXPECT_EQ(r.roundInwards(), in);
EXPECT_EQ(r_small.roundInwards(), OptIntRect());
}
template <typename C>
class GenericRectTest : public ::testing::Test {
public:
typedef typename CoordTraits<C>::PointType CPoint;
typedef typename CoordTraits<C>::RectType CRect;
typedef typename CoordTraits<C>::OptRectType OptCRect;
CRect a, a2, b, c, d;
CRect int_ab, int_bc, uni_ab, uni_bc;
GenericRectTest()
: a(0, 0, 10, 10)
, a2(0, 0, 10, 10)
, b(-5, -5, 5, 5)
, c(-10, -10, -1, -1)
, d(1, 1, 9, 9)
, int_ab(0, 0, 5, 5)
, int_bc(-5, -5, -1, -1)
, uni_ab(-5, -5, 10, 10)
, uni_bc(-10, -10, 5, 5)
{}
};
TYPED_TEST_CASE(GenericRectTest, CoordTypes);
TYPED_TEST(GenericRectTest, EqualityTest) {
typename TestFixture::CRect a(0, 0, 10, 10), a2(a), b(-5, -5, 5, 5);
typename TestFixture::OptCRect empty, oa = a;
EXPECT_TRUE (a == a);
EXPECT_FALSE(a != a);
EXPECT_TRUE (a == a2);
EXPECT_FALSE(a != a2);
EXPECT_TRUE (empty == empty);
EXPECT_FALSE(empty != empty);
EXPECT_FALSE(a == empty);
EXPECT_TRUE (a != empty);
EXPECT_FALSE(empty == a);
EXPECT_TRUE (empty != a);
EXPECT_FALSE(a == b);
EXPECT_TRUE (a != b);
EXPECT_TRUE (a == oa);
EXPECT_FALSE(a != oa);
}
TYPED_TEST(GenericRectTest, Intersects) {
typename TestFixture::CRect a(0, 0, 10, 10), b(-5, -5, 5, 5), c(-10, -10, -1, -1), d(1, 1, 9, 9);
typename TestFixture::OptCRect empty, oa(a), oc(c), od(d);
EXPECT_TRUE(a.intersects(a));
EXPECT_TRUE(a.intersects(b));
EXPECT_TRUE(b.intersects(a));
EXPECT_TRUE(b.intersects(c));
EXPECT_TRUE(c.intersects(b));
EXPECT_TRUE(a.intersects(d));
EXPECT_TRUE(d.intersects(a));
EXPECT_FALSE(a.intersects(c));
EXPECT_FALSE(c.intersects(a));
EXPECT_FALSE(c.intersects(d));
EXPECT_FALSE(empty.intersects(empty));
EXPECT_FALSE(empty.intersects(oa));
EXPECT_FALSE(oa.intersects(empty));
EXPECT_TRUE(oa.intersects(od));
EXPECT_FALSE(oa.intersects(oc));
}
/**
JonCruz failure: (10, 20)-(55,30) and (45,20)-(100,30) should intersect.
*/
TYPED_TEST(GenericRectTest, JonCruzRect) {
typename TestFixture::CRect a(10, 20, 55, 30), b(45, 20, 100,30);
typename TestFixture::OptCRect empty, oa(a), ob(b);
EXPECT_TRUE(a.intersects(a));
EXPECT_TRUE(a.intersects(b));
EXPECT_TRUE(b.intersects(a));
EXPECT_TRUE(oa.intersects(oa));
EXPECT_TRUE(oa.intersects(ob));
EXPECT_TRUE(ob.intersects(oa));
}
TYPED_TEST(GenericRectTest, Intersection) {
typename TestFixture::CRect a(0, 0, 10, 10), b(-5, -5, 5, 5), c(-10, -10, -1, -1), d(1, 1, 9, 9);
typename TestFixture::CRect int_ab(0, 0, 5, 5), int_bc(-5, -5, -1, -1);
typename TestFixture::OptCRect empty, oa(a), ob(b);
EXPECT_EQ(a & a, a);
EXPECT_EQ(a & b, int_ab);
EXPECT_EQ(b & c, int_bc);
EXPECT_EQ(intersect(b, c), int_bc);
EXPECT_EQ(intersect(a, a), a);
EXPECT_EQ(a & c, empty);
EXPECT_EQ(a & d, d);
EXPECT_EQ(a & empty, empty);
EXPECT_EQ(empty & empty, empty);
oa &= ob;
EXPECT_EQ(oa, int_ab);
oa = a;
oa &= b;
EXPECT_EQ(oa, int_ab);
oa = a;
oa &= empty;
EXPECT_EQ(oa, empty);
}
TYPED_TEST(GenericRectTest, Contains) {
typename TestFixture::CRect a(0, 0, 10, 10), b(-5, -5, 5, 5), c(-10, -10, -1, -1), d(1, 1, 9, 9);
typename TestFixture::CRect int_ab(0, 0, 5, 5), int_bc(-5, -5, -1, -1);
typename TestFixture::OptCRect empty, oa(a), od(d);
EXPECT_TRUE(a.contains(a));
EXPECT_FALSE(a.contains(b));
EXPECT_FALSE(b.contains(a));
EXPECT_FALSE(a.contains(c));
EXPECT_FALSE(c.contains(a));
EXPECT_TRUE(a.contains(d));
EXPECT_FALSE(d.contains(a));
EXPECT_TRUE(a.contains(int_ab));
EXPECT_TRUE(b.contains(int_ab));
EXPECT_TRUE(b.contains(int_bc));
EXPECT_TRUE(c.contains(int_bc));
EXPECT_FALSE(int_ab.contains(a));
EXPECT_FALSE(int_ab.contains(b));
EXPECT_FALSE(int_bc.contains(b));
EXPECT_FALSE(int_bc.contains(c));
EXPECT_FALSE(empty.contains(empty));
EXPECT_FALSE(empty.contains(od));
EXPECT_TRUE(oa.contains(empty));
EXPECT_TRUE(oa.contains(od));
EXPECT_FALSE(od.contains(oa));
}
TYPED_TEST(GenericRectTest, Union) {
typename TestFixture::CRect a(0, 0, 10, 10), old_a(a), b(-5, -5, 5, 5), c(-10, -10, -1, -1), d(1, 1, 9, 9);
typename TestFixture::CRect int_ab(0, 0, 5, 5), int_bc(-5, -5, -1, -1);
typename TestFixture::CRect uni_ab(-5, -5, 10, 10), uni_bc(-10, -10, 5, 5);
typename TestFixture::OptCRect empty, oa(a), ob(b);
EXPECT_EQ(a | b, uni_ab);
EXPECT_EQ(b | c, uni_bc);
EXPECT_EQ(a | a, a);
EXPECT_EQ(a | d, a);
EXPECT_EQ(a | int_ab, a);
EXPECT_EQ(b | int_ab, b);
EXPECT_EQ(uni_ab | a, uni_ab);
EXPECT_EQ(uni_bc | c, uni_bc);
EXPECT_EQ(a | empty, a);
EXPECT_EQ(empty | empty, empty);
a |= b;
EXPECT_EQ(a, uni_ab);
a = old_a;
a |= ob;
EXPECT_EQ(a, uni_ab);
a = old_a;
a |= empty;
EXPECT_EQ(a, old_a);
oa |= ob;
EXPECT_EQ(oa, uni_ab);
oa = old_a;
oa |= b;
EXPECT_EQ(oa, uni_ab);
}
TYPED_TEST(GenericRectTest, Area) {
typename TestFixture::CRect a(0, 0, 10, 10), b(-5, -5, 5, 5), c(-10, -10, -1, -1), d(1, 1, 9, 9);
typename TestFixture::CRect zero(0,0,0,0);
EXPECT_EQ(a.area(), 100);
EXPECT_EQ(a.area(), a.width() * a.height());
EXPECT_EQ(b.area(), 100);
EXPECT_EQ(c.area(), 81);
EXPECT_EQ(d.area(), 64);
EXPECT_FALSE(a.hasZeroArea());
EXPECT_TRUE(zero.hasZeroArea());
}
TYPED_TEST(GenericRectTest, Emptiness) {
typename TestFixture::OptCRect empty, oa(0, 0, 10, 10);
EXPECT_TRUE(empty.empty());
EXPECT_FALSE(empty);
EXPECT_TRUE(!empty);
EXPECT_FALSE(oa.empty());
EXPECT_TRUE(oa);
EXPECT_FALSE(!oa);
}
TYPED_TEST(GenericRectTest, Dimensions) {
typedef typename TestFixture::CPoint CPoint;
typename TestFixture::CRect a(-10, -20, 10, 20), b(-15, 30, 45, 90);
EXPECT_EQ(a.width(), 20);
EXPECT_EQ(a.height(), 40);
EXPECT_EQ(a.left(), -10);
EXPECT_EQ(a.top(), -20);
EXPECT_EQ(a.right(), 10);
EXPECT_EQ(a.bottom(), 20);
EXPECT_EQ(a.min(), CPoint(-10, -20));
EXPECT_EQ(a.max(), CPoint(10, 20));
EXPECT_EQ(a.minExtent(), a.width());
EXPECT_EQ(a.maxExtent(), a.height());
EXPECT_EQ(a.dimensions(), CPoint(20, 40));
EXPECT_EQ(a.midpoint(), CPoint(0, 0));
EXPECT_EQ(b.width(), 60);
EXPECT_EQ(b.height(), 60);
EXPECT_EQ(b.left(), -15);
EXPECT_EQ(b.top(), 30);
EXPECT_EQ(b.right(), 45);
EXPECT_EQ(b.bottom(), 90);
EXPECT_EQ(b.min(), CPoint(-15, 30));
EXPECT_EQ(b.max(), CPoint(45, 90));
EXPECT_EQ(b.minExtent(), b.maxExtent());
EXPECT_EQ(b.dimensions(), CPoint(60, 60));
EXPECT_EQ(b.midpoint(), CPoint(15, 60));
}
TYPED_TEST(GenericRectTest, Modification) {
typedef typename TestFixture::CRect CRect;
typedef typename TestFixture::OptCRect OptCRect;
typedef typename TestFixture::CPoint CPoint;
CRect a(-1, -1, 1, 1);
a.expandBy(9);
EXPECT_EQ(a, CRect(-10, -10, 10, 10));
a.setMin(CPoint(0, 0));
EXPECT_EQ(a, CRect(0, 0, 10, 10));
a.setMax(CPoint(20, 30));
EXPECT_EQ(a, CRect(0, 0, 20, 30));
a.setMax(CPoint(-5, -5));
EXPECT_EQ(a, CRect(-5, -5, -5, -5));
a.expandTo(CPoint(5, 5));
EXPECT_EQ(a, CRect(-5, -5, 5, 5));
a.expandTo(CPoint(0, 0));
EXPECT_EQ(a, CRect(-5, -5, 5, 5));
a.expandTo(CPoint(0, 15));
EXPECT_EQ(a, CRect(-5, -5, 5, 15));
a.expandBy(-10);
EXPECT_EQ(a, CRect(0, 5, 0, 5));
EXPECT_EQ(a.midpoint(), CPoint(0, 5));
a.unionWith(CRect(-20, 0, -10, 20));
EXPECT_EQ(a, CRect(-20, 0, 0, 20));
OptCRect oa(a);
oa.intersectWith(CRect(-10, -5, 5, 15));
EXPECT_EQ(oa, OptCRect(-10, 0, 0, 15));
}
TYPED_TEST(GenericRectTest, OptRectDereference) {
typename TestFixture::CRect a(0, 0, 5, 5);
typename TestFixture::OptCRect oa(0, 0, 10, 10);
EXPECT_NE(a, oa);
a = *oa;
EXPECT_EQ(a, oa);
}
TYPED_TEST(GenericRectTest, Offset) {
typename TestFixture::CRect a(0, 0, 5, 5), old_a(a), app1(-5, 0, 0, 5), amp1(5, 0, 10, 5),
app2(5, -10, 10, -5), amp2(-5, 10, 0, 15);
typename TestFixture::CPoint p1(-5, 0), p2(5, -10);
EXPECT_EQ(a + p1, app1);
EXPECT_EQ(a + p2, app2);
EXPECT_EQ(a - p1, amp1);
EXPECT_EQ(a - p2, amp2);
a += p1;
EXPECT_EQ(a, app1);
a = old_a;
a += p2;
EXPECT_EQ(a, app2);
a = old_a;
a -= p1;
EXPECT_EQ(a, amp1);
a = old_a;
a -= p2;
EXPECT_EQ(a, amp2);
}
TYPED_TEST(GenericRectTest, NearestEdgePoint) {
typename TestFixture::CRect a(0, 0, 10, 10);
typename TestFixture::CPoint p1(-5, 5), p2(15, 17), p3(6, 5), p4(3, 9);
typename TestFixture::CPoint r1(0, 5), r2(10, 10), r3(10, 5), r4(3, 10);
EXPECT_EQ(a.nearestEdgePoint(p1), r1);
EXPECT_EQ(a.nearestEdgePoint(p2), r2);
EXPECT_EQ(a.nearestEdgePoint(p3), r3);
EXPECT_EQ(a.nearestEdgePoint(p4), r4);
}
} // end namespace Geom
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|