PointCoordinates_test.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (c) 2009-2011 C.B. Barber. All rights reserved.
00004 ** $Id: //main/2011/qhull/src/qhulltest/PointCoordinates_test.cpp#4 $$Change: 1382 $
00005 ** $DateTime: 2011/05/14 10:45:42 $$Author: bbarber $
00006 **
00007 ****************************************************************************/
00008 
00009 //pre-compiled headers
00010 #include <iostream>
00011 #include "../road/RoadTest.h" // QT_VERSION
00012 
00013 #include "PointCoordinates.h"
00014 #include "QhullError.h"
00015 #include "RboxPoints.h"
00016 #include "Qhull.h"
00017 
00018 using std::cout;
00019 using std::endl;
00020 using std::ostringstream;
00021 using std::ostream;
00022 using std::string;
00023 using std::stringstream;
00024 
00025 namespace orgQhull {
00026 
00027 class PointCoordinates_test : public RoadTest
00028 {
00029     Q_OBJECT
00030 
00031 #//Test slots
00032 private slots:
00033     void t_construct();
00034     void t_convert();
00035     void t_getset();
00036     void t_element();
00037     void t_foreach();
00038     void t_search();
00039     void t_modify();
00040     void t_append_points();
00041     void t_coord_iterator();
00042     void t_io();
00043 };//PointCoordinates_test
00044 
00045 void
00046 add_PointCoordinates_test()
00047 {
00048     new PointCoordinates_test();
00049 }
00050 
00051 void PointCoordinates_test::
00052 t_construct()
00053 {
00054     PointCoordinates pc;
00055     QCOMPARE(pc.size(), 0U);
00056     QCOMPARE(pc.coordinateCount(), 0);
00057     QCOMPARE(pc.dimension(), 0);
00058     QCOMPARE(pc.coordinates(), (coordT *)0);
00059     QVERIFY(pc.isEmpty());
00060     pc.checkValid();
00061     PointCoordinates pc7(2);
00062     QCOMPARE(pc7.dimension(), 2);
00063     QCOMPARE(pc7.count(), 0);
00064     QVERIFY(pc7.isEmpty());
00065     QVERIFY(pc7.comment().empty());
00066     pc7.checkValid();
00067     PointCoordinates pc2("Test pc2");
00068     QCOMPARE(pc2.count(), 0);
00069     QVERIFY(pc2.isEmpty());
00070     QCOMPARE(pc2.comment(), std::string("Test pc2"));
00071     pc2.checkValid();
00072     PointCoordinates pc3(3, "Test 3-d pc3");
00073     QCOMPARE(pc3.dimension(), 3);
00074     QVERIFY(pc3.isEmpty());
00075     pc3.checkValid();
00076     coordT c[]= { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };
00077     PointCoordinates pc4(2, "Test 2-d pc4", 6, c);
00078     QCOMPARE(pc4.dimension(), 2);
00079     QCOMPARE(pc4.count(), 3);
00080     QCOMPARE(pc4.size(), 3u);
00081     QVERIFY(!pc4.isEmpty());
00082     QVERIFY(!pc4.empty());
00083     pc4.checkValid();
00084     QhullPoint p= pc4[2];
00085     QCOMPARE(p[1], 5.0);
00086     // QhullPoint refers to PointCoordinates
00087     p[1] += 1.0;
00088     QCOMPARE(pc4[2][1], 6.0);
00089     PointCoordinates pc5(4, "Test 4-d pc5 with insufficient coordinates", 6, c);
00090     QCOMPARE(pc5.dimension(), 4);
00091     QCOMPARE(pc5.count(), 1);
00092     QCOMPARE(pc5.extraCoordinatesCount(), 2);
00093     QCOMPARE(pc5.extraCoordinates()[1], 5.0);
00094     QVERIFY(!pc5.isEmpty());;
00095     std::vector<coordT> vc;
00096     vc.push_back(3.0);
00097     vc.push_back(4.0);
00098     vc.push_back(5.0);
00099     vc.push_back(6.0);
00100     vc.push_back(7.0);
00101     vc.push_back(9.0);
00102     pc5.append(2, &vc[3]); // Copy of vc[]
00103     pc5.checkValid();
00104     QhullPoint p5(4, &vc[1]);
00105     QCOMPARE(pc5[1], p5);
00106     PointCoordinates pc6(pc5); // Makes copy of point_coordinates
00107     QCOMPARE(pc6[1], p5);
00108     QVERIFY(pc6==pc5);
00109     QhullPoint p6= pc5[1];  // Refers to pc5.coordinates
00110     pc5[1][0] += 1.0;
00111     QCOMPARE(pc5[1], p6);
00112     QVERIFY(pc5[1]!=p5);
00113     QVERIFY(pc6!=pc5);
00114     pc6= pc5;
00115     QVERIFY(pc6==pc5);
00116     PointCoordinates pc8;
00117     pc6= pc8;
00118     QVERIFY(pc6!=pc5);
00119     QVERIFY(pc6.isEmpty());
00120 }//t_construct
00121 
00122 void PointCoordinates_test::
00123 t_convert()
00124 {
00125     //defineAs tested above
00126     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00127     PointCoordinates ps(3, "two 3-d points", 6, c);
00128     QCOMPARE(ps.dimension(), 3);
00129     QCOMPARE(ps.size(), 2u);
00130     const coordT *c2= ps.constData();
00131     QVERIFY(c!=c2);
00132     QCOMPARE(c[0], c2[0]);
00133     const coordT *c3= ps.data();
00134     QCOMPARE(c3, c2);
00135     coordT *c4= ps.data();
00136     QCOMPARE(c4, c2);
00137     std::vector<coordT> vs= ps.toStdVector();
00138     QCOMPARE(vs.size(), 6u);
00139     QCOMPARE(vs[5], 5.0);
00140     QList<coordT> qs= ps.toQList();
00141     QCOMPARE(qs.size(), 6);
00142     QCOMPARE(qs[5], 5.0);
00143 }//t_convert
00144 
00145 void PointCoordinates_test::
00146 t_getset()
00147 {
00148     // See t_construct() for test of coordinates, coordinateCount, dimension, empty, isEmpty, ==, !=
00149     // See t_construct() for test of checkValid, comment, setDimension
00150     PointCoordinates pc("Coordinates c");
00151     pc.setComment("New comment");
00152     QCOMPARE(pc.comment(), std::string("New comment"));
00153     pc.checkValid();
00154     pc.makeValid();  // A no-op
00155     pc.checkValid();
00156     Coordinates cs= pc.getCoordinates();
00157     QVERIFY(cs.isEmpty());
00158     PointCoordinates pc2(pc);
00159     pc.setDimension(3);
00160     QVERIFY(pc2!=pc);
00161     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00162     pc.append(6, c);
00163     pc.checkValid();
00164     pc.makeValid();  // A no-op
00165     QhullPoint p= pc[0];
00166     QCOMPARE(p[2], 2.0);
00167     try{
00168         pc.setDimension(2);
00169         QFAIL("setDimension(2) did not fail for 3-d.");
00170     }catch (const std::exception &e) {
00171         const char *s= e.what();
00172         cout << "INFO   : Caught " << s;
00173     }
00174 }//t_getset
00175 
00176 void PointCoordinates_test::
00177 t_element()
00178 {
00179     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00180     PointCoordinates pc(2, "2-d points", 6, c);
00181     QhullPoint p= pc.at(0);
00182     QCOMPARE(p, pc[0]);
00183     QCOMPARE(p, pc.first());
00184     QCOMPARE(p, pc.value(0));
00185     p= pc.back();
00186     QCOMPARE(p, pc[2]);
00187     QCOMPARE(p, pc.last());
00188     QCOMPARE(p, pc.value(2));
00189     QhullPoints ps= pc.mid(1, 2);
00190     QCOMPARE(ps[1], p);
00191 }//t_element
00192 
00193 void PointCoordinates_test::
00194 t_foreach()
00195 {
00196     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00197     PointCoordinates pc(2, "2-d points", 6, c);
00198     QhullPoints::Iterator i= pc.begin();
00199     QhullPoint p= pc[0];
00200     QCOMPARE(*i, p);
00201     QCOMPARE((*i)[0], 0.0);
00202     QhullPoint p3= pc[2];
00203     i= pc.end();
00204     QCOMPARE(i[-1], p3);
00205     const PointCoordinates pc2(2, "2-d points", 6, c);
00206     QhullPoints::ConstIterator i2= pc.begin();
00207     const QhullPoint p0= pc2[0];
00208     QCOMPARE(*i2, p0);
00209     QCOMPARE((*i2)[0], 0.0);
00210     QhullPoints::ConstIterator i3= i2;
00211     QCOMPARE(i3, i2);
00212     QCOMPARE((*i3)[0], 0.0);
00213     i3= pc.constEnd();
00214     --i3;
00215     QhullPoint p2= pc2[2];
00216     QCOMPARE(*i3, p2);
00217     i= pc.end();
00218     QVERIFY(i-1==i3);
00219     i2= pc2.end();
00220     QVERIFY(i2-1!=i3);
00221     QCOMPARE(*(i2-1), *i3);
00222     foreach(QhullPoint p3, pc){ //Qt only
00223         QVERIFY(p3[0]>=0.0);
00224         QVERIFY(p3[0]<=5.0);
00225     }
00226     Coordinates::ConstIterator i4= pc.beginCoordinates();
00227     QCOMPARE(*i4, 0.0);
00228     Coordinates::Iterator i5= pc.beginCoordinates();
00229     QCOMPARE(*i5, 0.0);
00230     i4= pc.beginCoordinates(1);
00231     QCOMPARE(*i4, 2.0);
00232     i5= pc.beginCoordinates(1);
00233     QCOMPARE(*i5, 2.0);
00234     i4= pc.endCoordinates();
00235     QCOMPARE(*--i4, 5.0);
00236     i5= pc.endCoordinates();
00237     QCOMPARE(*--i5, 5.0);
00238 }//t_foreach
00239 
00240 void PointCoordinates_test::
00241 t_search()
00242 {
00243     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00244     PointCoordinates pc(2, "2-d points", 6, c);
00245     QhullPoint p0= pc[0];
00246     QhullPoint p2= pc[2];
00247     QVERIFY(pc.contains(p0));
00248     QVERIFY(pc.contains(p2));
00249     QCOMPARE(pc.count(p0), 1);
00250     QCOMPARE(pc.indexOf(p2), 2);
00251     QCOMPARE(pc.lastIndexOf(p0), 0);
00252 }//t_search
00253 
00254 void PointCoordinates_test::
00255 t_modify()
00256 {
00257     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00258     PointCoordinates pc(2, "2-d points", 6, c);
00259     coordT c3[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00260     PointCoordinates pc5(2);
00261     pc5.append(6, c3); // 0-5
00262     QVERIFY(pc5==pc);
00263     PointCoordinates pc2(2, "2-d");
00264     coordT c2[]= {6.0, 7.0, 8.0, 9.0, 10.0, 11.0};
00265     pc2.append(6, c2);
00266     QCOMPARE(pc2.count(), 3);
00267     pc2.append(14.0);
00268     QCOMPARE(pc2.count(), 3);
00269     QCOMPARE(pc2.extraCoordinatesCount(), 1);
00270     pc2.append(15.0); // 6-11, 14,15
00271     QCOMPARE(pc2.count(), 4);
00272     QCOMPARE(pc2.extraCoordinatesCount(), 0);
00273     QhullPoint p(pc[0]);
00274     pc2.append(p); // 6-11, 14,15, 0,1
00275     QCOMPARE(pc2.count(), 5);
00276     QCOMPARE(pc2.extraCoordinatesCount(), 0);
00277     QCOMPARE(pc2.lastIndexOf(p), 4);
00278     pc.append(pc2); // Invalidates p
00279     QCOMPARE(pc.count(), 8); // 0-11, 14,15, 0,1
00280     QCOMPARE(pc.extraCoordinatesCount(), 0);
00281     QCOMPARE(pc.lastIndexOf(pc[0]), 7);
00282     pc.appendComment(" operators");
00283     QCOMPARE(pc.comment(), std::string("2-d points operators"));
00284     pc.checkValid();
00285     // see t_append_points for appendPoints
00286     PointCoordinates pc3= pc+pc2;
00287     pc3.checkValid();
00288     QCOMPARE(pc3.count(), 13);
00289     QCOMPARE(pc3[6][0], 14.0);
00290     QCOMPARE(pc3[8][0], 6.0);
00291     pc3 += pc;
00292     QCOMPARE(pc3.count(), 21);
00293     QCOMPARE(pc3[14][0], 2.0);
00294     pc3 += 12.0;
00295     pc3 += 14.0;
00296     QCOMPARE(pc3.count(), 22);
00297     QCOMPARE(pc3.last()[0], 12.0);
00298     // QhullPoint p3= pc3.first(); // += throws error because append may move the data
00299     QhullPoint p3= pc2.first();
00300     pc3 += p3;
00301     QCOMPARE(pc3.count(), 23);
00302     QCOMPARE(pc3.last()[0], 6.0);
00303     pc3 << pc;
00304     QCOMPARE(pc3.count(), 31);
00305     QCOMPARE(pc3.last()[0], 0.0);
00306     pc3 << 12.0 << 14.0;
00307     QCOMPARE(pc3.count(), 32);
00308     QCOMPARE(pc3.last()[0], 12.0);
00309     PointCoordinates pc4(pc3);
00310     pc4.reserveCoordinates(100);
00311     QVERIFY(pc3==pc4);
00312 }//t_modify
00313 
00314 void PointCoordinates_test::
00315 t_append_points()
00316 {
00317     PointCoordinates pc(2, "stringstream");
00318     stringstream s("2 3 1 2 3 4 5 6");
00319     pc.appendPoints(s);
00320     QCOMPARE(pc.count(), 3);
00321 }//t_append_points
00322 
00323 void PointCoordinates_test::
00324 t_coord_iterator()
00325 {
00326     PointCoordinates c(2);
00327     c << 0.0 << 1.0 << 2.0 << 3.0 << 4.0 << 5.0;
00328     PointCoordinatesIterator i(c);
00329     QhullPoint p0(c[0]);
00330     QhullPoint p1(c[1]);
00331     QhullPoint p2(c[2]);
00332     coordT c2[] = {-1.0, -2.0};
00333     QhullPoint p3(2, c2);
00334     PointCoordinatesIterator i2= c;
00335     QVERIFY(i.findNext(p1));
00336     QVERIFY(!i.findNext(p1));
00337     QVERIFY(!i.findNext(p2));
00338     QVERIFY(!i.findNext(p3));
00339     QVERIFY(i.findPrevious(p2));
00340     QVERIFY(!i.findPrevious(p2));
00341     QVERIFY(!i.findPrevious(p0));
00342     QVERIFY(!i.findPrevious(p3));
00343     QVERIFY(i2.findNext(p2));
00344     QVERIFY(i2.findPrevious(p0));
00345     QVERIFY(i2.findNext(p1));
00346     QVERIFY(i2.findPrevious(p0));
00347     QVERIFY(i2.hasNext());
00348     QVERIFY(!i2.hasPrevious());
00349     QVERIFY(i.hasNext());
00350     QVERIFY(!i.hasPrevious());
00351     i.toBack();
00352     i2.toFront();
00353     QVERIFY(!i.hasNext());
00354     QVERIFY(i.hasPrevious());
00355     QVERIFY(i2.hasNext());
00356     QVERIFY(!i2.hasPrevious());
00357     PointCoordinates c3;
00358     PointCoordinatesIterator i3= c3;
00359     QVERIFY(!i3.hasNext());
00360     QVERIFY(!i3.hasPrevious());
00361     i3.toBack();
00362     QVERIFY(!i3.hasNext());
00363     QVERIFY(!i3.hasPrevious());
00364     QCOMPARE(i.peekPrevious(), p2);
00365     QCOMPARE(i.previous(), p2);
00366     QCOMPARE(i.previous(), p1);
00367     QCOMPARE(i.previous(), p0);
00368     QVERIFY(!i.hasPrevious());
00369     QCOMPARE(i.peekNext(), p0);
00370     // i.peekNext()= 1.0; // compiler error
00371     QCOMPARE(i.next(), p0);
00372     QCOMPARE(i.peekNext(), p1);
00373     QCOMPARE(i.next(), p1);
00374     QCOMPARE(i.next(), p2);
00375     QVERIFY(!i.hasNext());
00376     i.toFront();
00377     QCOMPARE(i.next(), p0);
00378 }//t_coord_iterator
00379 
00380 void PointCoordinates_test::
00381 t_io()
00382 {
00383     PointCoordinates c;
00384     c << 1.0 << 2.0 << 3.0 << 1.0 << 2.0 << 3.0;
00385     ostringstream os;
00386     os << "PointCoordinates 0-d\n" << c;
00387     c.setDimension(2);
00388     os << "PointCoordinates 1-3-2\n" << c;
00389     cout << os.str();
00390     QString s= QString::fromStdString(os.str());
00391     QCOMPARE(s.count("0"), 3);
00392     QCOMPARE(s.count("2"), 4);
00393 }//t_io
00394 
00395 }//orgQhull
00396 
00397 #include "moc/PointCoordinates_test.moc"
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


libqhull
Author(s): Robert Krug
autogenerated on Tue Jun 18 2013 12:38:49