Code

cca77e06391dadfe41369f594877edb07521dd5a
[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 which needs a closing segment
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 that doesn't need a closing segment
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() , 2u);
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         }
62         { // Combination
63             Geom::PathVector pv;
64             pv.push_back(path1);
65             pv.push_back(path2);
66             pv.push_back(path3);
67             pv.push_back(path4);
68             SPCurve curve(pv);
69             TS_ASSERT_EQUALS(curve.get_segment_count() , 9u);
70         }
71     }
73     void testNodesInPath()
74     {
75         { // Zero segments
76             Geom::PathVector pv;
77             SPCurve curve(pv);
78             TS_ASSERT_EQUALS(curve.nodes_in_path() , 0u);
79         }
80         { // Zero segments
81             Geom::PathVector pv;
82             pv.push_back(Geom::Path());
83             SPCurve curve(pv);
84             TS_ASSERT_EQUALS(curve.nodes_in_path() , 1u);
85         }
86         { // Individual paths
87             Geom::PathVector pv(1, Geom::Path());
88             pv[0] = path1;
89             TS_ASSERT_EQUALS(SPCurve(pv).nodes_in_path() , 3u);
90             pv[0] = path2;
91             TS_ASSERT_EQUALS(SPCurve(pv).nodes_in_path() , 2u);
92             pv[0] = path3;
93             TS_ASSERT_EQUALS(SPCurve(pv).nodes_in_path() , 5u);
94             pv[0] = path4;
95             TS_ASSERT_EQUALS(SPCurve(pv).nodes_in_path() , 1u);
96         }
97         { // Combination
98             Geom::PathVector pv;
99             pv.push_back(path1);
100             pv.push_back(path2);
101             pv.push_back(path3);
102             pv.push_back(path4);
103             SPCurve curve(pv);
104             TS_ASSERT_EQUALS(curve.nodes_in_path() , 11u);
105         }
106     }
108     void testIsEmpty()
109     {
110         TS_ASSERT(SPCurve(Geom::PathVector()).is_empty());
111         TS_ASSERT(!SPCurve(Geom::PathVector(1, path1)).is_empty());
112         TS_ASSERT(!SPCurve(Geom::PathVector(1, path2)).is_empty());
113         TS_ASSERT(!SPCurve(Geom::PathVector(1, path3)).is_empty());
114         TS_ASSERT(!SPCurve(Geom::PathVector(1, path4)).is_empty());
115     }
117     void testIsClosed()
118     {
119         TS_ASSERT(SPCurve(Geom::PathVector()).is_closed()); // An empty conjuction is true
120         Geom::PathVector pv(1, Geom::Path());
121         TS_ASSERT(!SPCurve(pv).is_closed());
122         pv[0].close();
123         TS_ASSERT(SPCurve(pv).is_closed());
124         TS_ASSERT(SPCurve(Geom::PathVector(1, path1)).is_closed());
125         TS_ASSERT(SPCurve(Geom::PathVector(1, path2)).is_closed());
126         TS_ASSERT(!SPCurve(Geom::PathVector(1, path3)).is_closed());
127         TS_ASSERT(!SPCurve(Geom::PathVector(1, path4)).is_closed());
128     }
130     void testLastFirstSegment()
131     {
132         Geom::PathVector pv(1, path4);
133         TS_ASSERT_EQUALS(SPCurve(pv).first_segment() , (void*)0);
134         TS_ASSERT_EQUALS(SPCurve(pv).last_segment()  , (void*)0);
135         pv[0].close();
136         TS_ASSERT_DIFFERS(SPCurve(pv).first_segment() , (void*)0);
137         TS_ASSERT_DIFFERS(SPCurve(pv).last_segment()  , (void*)0);
138         /* Geom::Curve can't be compared very easily (?)
139         Geom::PathVector pv(1, path4);
140         pv[0].close();
141         TS_ASSERT_EQUALS(*SPCurve(pv).first_segment() , pv[0][0]);
142         TS_ASSERT_EQUALS(*SPCurve(pv).last_segment()  , pv[0][0]);
143         pv[0] = path1;
144         TS_ASSERT_EQUALS(*SPCurve(pv).first_segment() , pv[0][0]);
145         TS_ASSERT_EQUALS(*SPCurve(pv).last_segment()  , pv[0][2]);
146         pv[0] = path2;
147         TS_ASSERT_EQUALS(*SPCurve(pv).first_segment() , pv[0][0]);
148         TS_ASSERT_EQUALS(*SPCurve(pv).last_segment()  , pv[0][1]);
149         pv[0] = path3;
150         TS_ASSERT_EQUALS(*SPCurve(pv).first_segment() , pv[0][0]);
151         TS_ASSERT_EQUALS(*SPCurve(pv).last_segment()  , pv[0][3]);
152         pv[0] = path4;
153         TS_ASSERT_EQUALS(SPCurve(pv).first_segment() , (void*)0);
154         TS_ASSERT_EQUALS(SPCurve(pv).last_segment()  , (void*)0);
155         pv.clear();
156         pv.push_back(path1);
157         pv.push_back(path2);
158         pv.push_back(path3);
159         TS_ASSERT_EQUALS(*SPCurve(pv).first_segment() , pv[0][0]);
160         TS_ASSERT_EQUALS(*SPCurve(pv).last_segment()  , pv[2][3]);*/
161     }
163     void testLastFirstPath()
164     {
165         Geom::PathVector pv;
166         TS_ASSERT_EQUALS(SPCurve(pv).first_path() , (void*)0);
167         TS_ASSERT_EQUALS(SPCurve(pv).last_path()  , (void*)0);
168         pv.push_back(path1);
169         TS_ASSERT_EQUALS(*SPCurve(pv).first_path() , pv[0]);
170         TS_ASSERT_EQUALS(*SPCurve(pv).last_path()  , pv[0]);
171         pv.push_back(path2);
172         TS_ASSERT_EQUALS(*SPCurve(pv).first_path() , pv[0]);
173         TS_ASSERT_EQUALS(*SPCurve(pv).last_path()  , pv[1]);
174         pv.push_back(path3);
175         TS_ASSERT_EQUALS(*SPCurve(pv).first_path() , pv[0]);
176         TS_ASSERT_EQUALS(*SPCurve(pv).last_path()  , pv[2]);
177         pv.push_back(path4);
178         TS_ASSERT_EQUALS(*SPCurve(pv).first_path() , pv[0]);
179         TS_ASSERT_EQUALS(*SPCurve(pv).last_path()  , pv[3]);
180     }
182     void testFirstPoint()
183     {
184         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path1)).first_point() , Geom::Point(0,0));
185         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path2)).first_point() , Geom::Point(2,0));
186         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path3)).first_point() , Geom::Point(4,0));
187         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path4)).first_point() , Geom::Point(3,5));
188         Geom::PathVector pv;
189         pv.push_back(path1);
190         pv.push_back(path2);
191         pv.push_back(path3);
192         TS_ASSERT_EQUALS(SPCurve(pv).first_point() , Geom::Point(0,0));
193         pv.insert(pv.begin(), path4);
194         TS_ASSERT_EQUALS(SPCurve(pv).first_point() , Geom::Point(3,5));
195     }
197     void testLastPoint()
198     {
199         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path1)).last_point() , Geom::Point(0,0));
200         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path2)).last_point() , Geom::Point(2,0));
201         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path3)).last_point() , Geom::Point(8,4));
202         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path4)).last_point() , Geom::Point(3,5));
203         Geom::PathVector pv;
204         pv.push_back(path1);
205         pv.push_back(path2);
206         pv.push_back(path3);
207         TS_ASSERT_EQUALS(SPCurve(pv).last_point() , Geom::Point(8,4));
208         pv.push_back(path4);
209         TS_ASSERT_EQUALS(SPCurve(pv).last_point() , Geom::Point(3,5));
210     }
212     void testSecondPoint()
213     {
214         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path1)).second_point() , Geom::Point(1,0));
215         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path2)).second_point() , Geom::Point(3,0));
216         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path3)).second_point() , Geom::Point(5,1));
217         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path4)).second_point() , Geom::Point(3,5));
218         Geom::PathVector pv;
219         pv.push_back(path1);
220         pv.push_back(path2);
221         pv.push_back(path3);
222         TS_ASSERT_EQUALS(SPCurve(pv).second_point() , Geom::Point(1,0));
223         pv.insert(pv.begin(), path4);
224         TS_ASSERT_EQUALS(SPCurve(pv).second_point() , Geom::Point(0,0));
225     }
227     void testPenultimatePoint()
228     {
229         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path1)).penultimate_point() , Geom::Point(1,1));
230         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path2)).penultimate_point() , Geom::Point(3,0));
231         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path3)).penultimate_point() , Geom::Point(6,4));
232         TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path4)).penultimate_point() , Geom::Point(3,5));
233         Geom::PathVector pv;
234         pv.push_back(path1);
235         pv.push_back(path2);
236         pv.push_back(path3);
237         TS_ASSERT_EQUALS(SPCurve(pv).penultimate_point() , Geom::Point(6,4));
238         pv.push_back(path4);
239         TS_ASSERT_EQUALS(SPCurve(pv).penultimate_point() , Geom::Point(8,4));
240     }
242     // TODO: Rest of the methods
243 };
245 /*
246   Local Variables:
247   mode:c++
248   c-file-style:"stroustrup"
249   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
250   indent-tabs-mode:nil
251   fill-column:99
252   End:
253 */
254 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :