Add automated tests on function-type FSCommand parameter passing.
[gnash.git] / testsuite / libcore.all / EdgeTest.cpp
bloba44ae6af59b058151dad044813f472e1c65367dc
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 #include "gnashconfig.h"
21 #endif
23 #include <ostream>
24 #include <sstream>
25 #include <cassert>
26 #include <cmath>
27 #include <algorithm>
29 #include "Geometry.h" // for edge
30 #include "check.h"
32 using gnash::Edge;
33 using gnash::point;
35 // for double comparison
36 struct D {
37 double _d;
38 double _t; // tolerance
40 D(double d) : _d(d), _t(1e-4) {}
42 // Set tolerance
43 D(double d, double t) : _d(d), _t(t) {}
45 // Return true if the difference between the two
46 // doubles is below the minimum tolerance defined for the two
47 bool operator==(const D& d)
49 double tol = std::min(_t, d._t);
50 double delta = std::abs(_d - d._d);
51 bool ret = delta < tol;
52 //cout << "D " << _d << "operator==(const D " << d._d <<") returning " << ret << " (delta is " << delta << ") " << endl;
53 return ret;
56 std::ostream& operator<<(std::ostream& os, const D& d)
58 return os << d._d << " [tol: " << d._t << "]";
61 int
62 main(int /*argc*/, char** /*argv*/)
66 // Test distance
69 check_equals(Edge::distancePtSeg(point(0,0), point(9, 0), point(9, 0)), 9);
70 check_equals(Edge::distancePtSeg(point(0,0), point(0, 0), point(3, 0)), 0);
71 check_equals(Edge::distancePtSeg(point(-5,0), point(0, 0), point(3, 0)), 5);
72 check_equals(Edge::distancePtSeg(point(5,0), point(0, 0), point(3, 0)), 2);
73 check_equals(D(Edge::distancePtSeg(point(0,0), point(-10, 0), point(3, 0))), 0);
74 check_equals(Edge::distancePtSeg(point(0,0), point(-10, 0), point(-10, 30)), 10);
75 check_equals(Edge::distancePtSeg(point(5,5), point(-10, 0), point(10, 0)), 5);
78 // Test pointOnCurve
82 // A-----C
83 // |
84 // B
86 point A(10, 10);
87 point C(20, 10);
88 point B(20, 20);
89 check_equals(Edge::pointOnCurve(A, C, B, 0), A);
90 check_equals(Edge::pointOnCurve(A, C, B, 1), B);
91 check_equals(Edge::pointOnCurve(A, C, B, 0.5), point(17.5, 12.5));
92 check_equals(std::sqrt((float)Edge::squareDistancePtCurve(A, C, B, B, 1)), 0);
93 check_equals(std::sqrt((float)Edge::squareDistancePtCurve(A, C, B, A, 0)), 0);
96 // A----B---C
98 A.setTo(10, 10);
99 C.setTo(40, 10);
100 B.setTo(20, 10);
101 check_equals(Edge::pointOnCurve(A, C, B, 0), A);
102 check_equals(Edge::pointOnCurve(A, C, B, 1), B);
103 check_equals(Edge::pointOnCurve(A, C, B, 0.5), point(27.5, 10));
104 check_equals(std::sqrt((float)Edge::squareDistancePtCurve(A, C, B, B, 1)), 0);
105 check_equals(std::sqrt((float)Edge::squareDistancePtCurve(A, C, B, A, 0)), 0);
106 return 0;