Code

784ccee8938b98bc345c3997977011498f219a91
[inkscape.git] / src / display / curve-test.h
1 #include <cxxtest/TestSuite.h>
3 #include "display/curve.h"
4 #include <2geom/curves.h>
5 #include <2geom/path.h>
6 #include <2geom/pathvector.h>
8 class CurveTest : public CxxTest::TestSuite {
9 private:
10     Geom::Path path1;
11     Geom::Path path2;
12     Geom::Path path3;
13     Geom::Path path4;
15 public:
16     CurveTest() : path4(Geom::Point(3,5)) // Just a moveto
17     {
18         // Closed path
19         path1.append(Geom::HLineSegment(Geom::Point(0,0),1));
20         path1.append(Geom::VLineSegment(Geom::Point(1,0),1));
21         path1.close();
22         // Closed path (ClosingSegment is zero length)
23         path2.append(Geom::LineSegment(Geom::Point(2,0),Geom::Point(3,0)));
24         path2.append(Geom::BezierCurve<3>(Geom::Point(3,0),Geom::Point(2,1),Geom::Point(1,1),Geom::Point(2,0)));
25         path2.close();
26         // Open path
27         path3.append(Geom::SVGEllipticalArc(Geom::Point(4,0),1,2,M_PI,false,false,Geom::Point(5,1)));
28         path3.append(Geom::VLineSegment(Geom::Point(5,1),2), Geom::Path::STITCH_DISCONTINUOUS);
29         path3.append(Geom::HLineSegment(Geom::Point(6,4),2), Geom::Path::STITCH_DISCONTINUOUS);
30     }
31     virtual ~CurveTest() {}
33 // createSuite and destroySuite get us per-suite setup and teardown
34 // without us having to worry about static initialization order, etc.
35     static CurveTest *createSuite() { return new CurveTest(); }
36     static void destroySuite( CurveTest *suite ) { delete suite; }
38     void testGetSegmentCount()
39     {
40         { // Zero segments
41             Geom::PathVector pv;
42             SPCurve curve(pv);
43             TS_ASSERT_EQUALS(curve.get_segment_count() , 0u);
44         }
45         { // Zero segments
46             Geom::PathVector pv;
47             pv.push_back(Geom::Path());
48             SPCurve curve(pv);
49             TS_ASSERT_EQUALS(curve.get_segment_count() , 0u);
50         }
51         { // Individual paths
52             Geom::PathVector pv(1, Geom::Path());
53             pv[0] = path1;
54             TS_ASSERT_EQUALS(SPCurve(pv).get_segment_count() , 3u);
55             pv[0] = path2;
56             TS_ASSERT_EQUALS(SPCurve(pv).get_segment_count() , 3u);
57             pv[0] = path3;
58             TS_ASSERT_EQUALS(SPCurve(pv).get_segment_count() , 4u);
59             pv[0] = path4;
60             TS_ASSERT_EQUALS(SPCurve(pv).get_segment_count() , 0u);
61             pv[0].close();
62             TS_ASSERT_EQUALS(SPCurve(pv).get_segment_count() , 1u);
63         }
64         { // Combination
65             Geom::PathVector pv;
66             pv.push_back(path1);
67             pv.push_back(path2);
68             pv.push_back(path3);
69             pv.push_back(path4);
70             SPCurve curve(pv);
71             TS_ASSERT_EQUALS(curve.get_segment_count() , 10u);
72         }
73     }
75     void testNodesInPath()
76     {
77         { // Zero segments
78             Geom::PathVector pv;
79             SPCurve curve(pv);
80             TS_ASSERT_EQUALS(curve.nodes_in_path() , 0u);
81         }
82         { // Zero segments
83             Geom::PathVector pv;
84             pv.push_back(Geom::Path());
85             SPCurve curve(pv);
86             TS_ASSERT_EQUALS(curve.nodes_in_path() , 1u);
87         }
88         { // Individual paths
89             Geom::PathVector pv(1, Geom::Path());
90             pv[0] = path1;
91             TS_ASSERT_EQUALS(SPCurve(pv).nodes_in_path() , 3u);
92             pv[0] = path2;
93             TS_ASSERT_EQUALS(SPCurve(pv).nodes_in_path() , 2u); // zero length closing segments do not increase the nodecount.
94             pv[0] = path3;
95             TS_ASSERT_EQUALS(SPCurve(pv).nodes_in_path() , 5u);
96             pv[0] = path4;
97             TS_ASSERT_EQUALS(SPCurve(pv).nodes_in_path() , 1u);
98             pv[0].close();
99             TS_ASSERT_EQUALS(SPCurve(pv).nodes_in_path() , 1u);
100         }
101         { // Combination
102             Geom::PathVector pv;
103             pv.push_back(path1);
104             pv.push_back(path2);
105             pv.push_back(path3);
106             pv.push_back(path4);
107             SPCurve curve(pv);
108             TS_ASSERT_EQUALS(curve.nodes_in_path() , 12u);
109         }
110     }
112     void testIsEmpty()
113     {
114         TS_ASSERT(SPCurve(Geom::PathVector()).is_empty());
115         TS_ASSERT(!SPCurve(Geom::PathVector(1, path1)).is_empty());
116         TS_ASSERT(!SPCurve(Geom::PathVector(1, path2)).is_empty());
117         TS_ASSERT(!SPCurve(Geom::PathVector(1, path3)).is_empty());
118         TS_ASSERT(!SPCurve(Geom::PathVector(1, path4)).is_empty());
119     }
121     void testIsClosed()
122     {
123         TS_ASSERT(!SPCurve(Geom::PathVector()).is_closed());
124         Geom::PathVector pv(1, Geom::Path());
125         TS_ASSERT(!SPCurve(pv).is_closed());
126         pv[0].close();
127         TS_ASSERT(SPCurve(pv).is_closed());
128         TS_ASSERT(SPCurve(Geom::PathVector(1, path1)).is_closed());
129         TS_ASSERT(SPCurve(Geom::PathVector(1, path2)).is_closed());
130         TS_ASSERT(!SPCurve(Geom::PathVector(1, path3)).is_closed());
131         TS_ASSERT(!SPCurve(Geom::PathVector(1, path4)).is_closed());
132     }
134     void testLastFirstSegment()
135     {
136         Geom::PathVector pv(1, path4);
137         TS_ASSERT_EQUALS(SPCurve(pv).first_segment() , (void*)0);
138         TS_ASSERT_EQUALS(SPCurve(pv).last_segment()  , (void*)0);
139         pv[0].close();
140         TS_ASSERT_DIFFERS(SPCurve(pv).first_segment() , (void*)0);
141         TS_ASSERT_DIFFERS(SPCurve(pv).last_segment()  , (void*)0);
142         /* Geom::Curve can't be compared very easily (?)
143         Geom::PathVector pv(1, path4);
144         pv[0].close();
145         TS_ASSERT_EQUALS(*SPCurve(pv).first_segment() , pv[0][0]);
146         TS_ASSERT_EQUALS(*SPCurve(pv).last_segment()  , pv[0][0]);
147         pv[0] = path1;
148         TS_ASSERT_EQUALS(*SPCurve(pv).first_segment() , pv[0][0]);
149         TS_ASSERT_EQUALS(*SPCurve(pv).last_segment()  , pv[0][2]);
150         pv[0] = path2;
151         TS_ASSERT_EQUALS(*SPCurve(pv).first_segment() , pv[0][0]);
152         TS_ASSERT_EQUALS(*SPCurve(pv).last_segment()  , pv[0][1]);
153         pv[0] = path3;
154         TS_ASSERT_EQUALS(*SPCurve(pv).first_segment() , pv[0][0]);
155         TS_ASSERT_EQUALS(*SPCurve(pv).last_segment()  , pv[0][3]);
156         pv[0] = path4;
157         TS_ASSERT_EQUALS(SPCurve(pv).first_segment() , (void*)0);
158         TS_ASSERT_EQUALS(SPCurve(pv).last_segment()  , (void*)0);
159         pv.clear();
160         pv.push_back(path1);
161         pv.push_back(path2);
162         pv.push_back(path3);
163         TS_ASSERT_EQUALS(*SPCurve(pv).first_segment() , pv[0][0]);
164         TS_ASSERT_EQUALS(*SPCurve(pv).last_segment()  , pv[2][3]);*/
165     }
167     void testLastFirstPath()
168     {
169         Geom::PathVector pv;
170         TS_ASSERT_EQUALS(SPCurve(pv).first_path() , (void*)0);
171         TS_ASSERT_EQUALS(SPCurve(pv).last_path()  , (void*)0);
172         pv.push_back(path1);
173         TS_ASSERT_EQUALS(*SPCurve(pv).first_path() , pv[0]);
174         TS_ASSERT_EQUALS(*SPCurve(pv).last_path()  , pv[0]);
175         pv.push_back(path2);
176         TS_ASSERT_EQUALS(*SPCurve(pv).first_path() , pv[0]);
177         TS_ASSERT_EQUALS(*SPCurve(pv).last_path()  , pv[1]);
178         pv.push_back(path3);
179         TS_ASSERT_EQUALS(*SPCurve(pv).first_path() , pv[0]);
180         TS_ASSERT_EQUALS(*SPCurve(pv).last_path()  , pv[2]);
181         pv.push_back(path4);
182         TS_ASSERT_EQUALS(*SPCurve(pv).first_path() , pv[0]);
183         TS_ASSERT_EQUALS(*SPCurve(pv).last_path()  , pv[3]);
184     }
186     void testFirstPoint()
187     {
188         TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path1)).first_point()) , Geom::Point(0,0));
189         TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path2)).first_point()) , Geom::Point(2,0));
190         TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path3)).first_point()) , Geom::Point(4,0));
191         TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path4)).first_point()) , Geom::Point(3,5));
192         Geom::PathVector pv;
193         TS_ASSERT(!SPCurve(pv).first_point());
194         pv.push_back(path1);
195         pv.push_back(path2);
196         pv.push_back(path3);
197         TS_ASSERT_EQUALS(*(SPCurve(pv).first_point()) , Geom::Point(0,0));
198         pv.insert(pv.begin(), path4);
199         TS_ASSERT_EQUALS(*(SPCurve(pv).first_point()) , Geom::Point(3,5));
200     }
202     void testLastPoint()
203     {
204         TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path1)).last_point()) , Geom::Point(0,0));
205         TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path2)).last_point()) , Geom::Point(2,0));
206         TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path3)).last_point()) , Geom::Point(8,4));
207         TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path4)).last_point()) , Geom::Point(3,5));
208         Geom::PathVector pv;
209         TS_ASSERT(!SPCurve(pv).last_point());
210         pv.push_back(path1);
211         pv.push_back(path2);
212         pv.push_back(path3);
213         TS_ASSERT_EQUALS(*(SPCurve(pv).last_point()) , Geom::Point(8,4));
214         pv.push_back(path4);
215         TS_ASSERT_EQUALS(*(SPCurve(pv).last_point()) , Geom::Point(3,5));
216     }
218     void testSecondPoint()
219     {
220         TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path1)).second_point()) , Geom::Point(1,0));
221         TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path2)).second_point()) , Geom::Point(3,0));
222         TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path3)).second_point()) , Geom::Point(5,1));
223         TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path4)).second_point()) , Geom::Point(3,5));
224         Geom::PathVector pv;
225         pv.push_back(path1);
226         pv.push_back(path2);
227         pv.push_back(path3);
228         TS_ASSERT_EQUALS( *(SPCurve(pv).second_point()) , Geom::Point(1,0));
229         pv.insert(pv.begin(), path4);
230         TS_ASSERT_EQUALS( *SPCurve(pv).second_point(), Geom::Point(0,0) );
231     }
233     void testPenultimatePoint()
234     {
235         TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path1)).penultimate_point()) , Geom::Point(1,1));
236         TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path2)).penultimate_point()) , Geom::Point(3,0));
237         TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path3)).penultimate_point()) , Geom::Point(6,4));
238         TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path4)).penultimate_point()) , Geom::Point(3,5));
239         Geom::PathVector pv;
240         pv.push_back(path1);
241         pv.push_back(path2);
242         pv.push_back(path3);
243         TS_ASSERT_EQUALS( *(SPCurve(pv).penultimate_point()) , Geom::Point(6,4));
244         pv.push_back(path4);
245         TS_ASSERT_EQUALS( *(SPCurve(pv).penultimate_point()) , Geom::Point(8,4));
246     }
248     // TODO: Rest of the methods
249 };
251 /*
252   Local Variables:
253   mode:c++
254   c-file-style:"stroustrup"
255   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
256   indent-tabs-mode:nil
257   fill-column:99
258   End:
259 */
260 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :