QhullPoints_test.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (p) 2009-2011 C.B. Barber. All rights reserved.
00004 ** $Id: //main/2011/qhull/src/qhulltest/QhullPoints_test.cpp#4 $$Change: 1382 $
00005 ** $DateTime: 2011/05/14 10:45:42 $$Author: bbarber $
00006 **
00007 ****************************************************************************/
00008 
00009 //pre-compiled header
00010 #include <iostream>
00011 #include "../road/RoadTest.h" // QT_VERSION
00012 
00013 #include "QhullPoints.h"
00014 #include "RboxPoints.h"
00015 #include "Qhull.h"
00016 
00017 using std::cout;
00018 using std::endl;
00019 using std::ostringstream;
00020 
00021 namespace orgQhull {
00022 
00023 class QhullPoints_test : public RoadTest
00024 {
00025     Q_OBJECT
00026 
00027 #//Test slots
00028 private slots:
00029     void cleanup();
00030     void t_construct();
00031     void t_convert();
00032     void t_getset();
00033     void t_element();
00034     void t_iterator();
00035     void t_const_iterator();
00036     void t_search();
00037     void t_points_iterator();
00038     void t_io();
00039 };//QhullPoints_test
00040 
00041 void
00042 add_QhullPoints_test()
00043 {
00044     new QhullPoints_test();
00045 }
00046 
00047 //Executed after each testcase
00048 void QhullPoints_test::
00049 cleanup()
00050 {
00051     UsingLibQhull::checkQhullMemoryEmpty();
00052     RoadTest::cleanup();
00053 }
00054 
00055 void QhullPoints_test::
00056 t_construct()
00057 {
00058     QhullPoints ps;
00059     QCOMPARE(ps.dimension(), 0);
00060     QVERIFY(ps.isEmpty());
00061     QVERIFY(ps.empty());
00062     QCOMPARE(ps.count(), 0);
00063     QCOMPARE(ps.size(), 0u);
00064     QCOMPARE(ps.coordinateCount(), 0);
00065     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00066     QhullPoints ps2;
00067     ps2.defineAs(2, 6, c);
00068     QCOMPARE(ps2.dimension(), 2);
00069     QVERIFY(!ps2.isEmpty());
00070     QVERIFY(!ps2.empty());
00071     QCOMPARE(ps2.count(), 3);
00072     QCOMPARE(ps2.size(), 3u);
00073     QCOMPARE(ps2.coordinates(), c);
00074     QhullPoints ps7(3);
00075     QCOMPARE(ps7.dimension(), 3);
00076     QVERIFY(ps7.isEmpty());
00077     QhullPoints ps3(2, 6, c);
00078     QCOMPARE(ps3.dimension(), 2);
00079     QVERIFY(!ps3.isEmpty());
00080     QCOMPARE(ps3.coordinates(), ps2.coordinates());
00081     QVERIFY(ps3==ps2);
00082     QVERIFY(ps3!=ps);
00083     QhullPoints ps4= ps3;
00084     QVERIFY(ps4==ps3);
00085     // ps4= ps3; //compiler error
00086     QhullPoints ps5(ps4);
00087     QVERIFY(ps5==ps4);
00088     QVERIFY(!(ps5!=ps4));
00089     coordT c2[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00090     QhullPoints ps6(2, 6, c2);
00091     QVERIFY(ps6==ps2);
00092 }//t_construct
00093 
00094 void QhullPoints_test::
00095 t_convert()
00096 {
00097     //defineAs tested above
00098     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00099     QhullPoints ps(3, 6, c);
00100     QCOMPARE(ps.dimension(), 3);
00101     QCOMPARE(ps.size(), 2u);
00102     const coordT *c2= ps.constData();
00103     QCOMPARE(c, c2);
00104     const coordT *c3= ps.data();
00105     QCOMPARE(c, c3);
00106     coordT *c4= ps.data();
00107     QCOMPARE(c, c4);
00108     std::vector<QhullPoint> vs= ps.toStdVector();
00109     QCOMPARE(vs.size(), 2u);
00110     QhullPoint p= vs[1];
00111     QCOMPARE(p[2], 5.0);
00112     QList<QhullPoint> qs= ps.toQList();
00113     QCOMPARE(qs.size(), 2);
00114     QhullPoint p2= qs[1];
00115     QCOMPARE(p2[2], 5.0);
00116 }//t_convert
00117 
00118 void QhullPoints_test::
00119 t_getset()
00120 {
00121     //See t_construct for coordinates, count, defineAs, dimension, empty, isempty, ==, !=, size
00122     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00123     QhullPoints ps(3, 6, c);
00124     QhullPoints ps2(3, 6, c);
00125     QCOMPARE(ps2.dimension(), 3);
00126     QCOMPARE(ps2.coordinates(), c);
00127     QCOMPARE(ps2.count(), 2);
00128     QCOMPARE(ps2.coordinateCount(), 6);
00129     coordT c2[]= {-1.0, -2.0, -3.0, -4.0, -5.0, -6.0};
00130     ps2.defineAs(6, c2);
00131     QCOMPARE(ps2.coordinates(), c2);
00132     QCOMPARE(ps2.count(), 2);
00133     QCOMPARE(ps2.size(), 2u);
00134     QCOMPARE(ps2.dimension(), 3);
00135     QVERIFY(!ps2.isEmpty());
00136     QVERIFY(ps!=ps2);
00137     // ps2= ps; // assignment not available, compiler error
00138     ps2.defineAs(ps);
00139     QVERIFY(ps==ps2);
00140     ps2.setDimension(2);
00141     QCOMPARE(ps2.dimension(), 2);
00142     QCOMPARE(ps2.coordinates(), c);
00143     QVERIFY(!ps2.isEmpty());
00144     QCOMPARE(ps2.count(), 3);
00145     QCOMPARE(ps2.size(), 3u);
00146     QVERIFY(ps!=ps2);
00147     QhullPoints ps3(3);
00148     ps3.defineAs(5, c2);
00149     QCOMPARE(ps3.count(), 1);
00150     QCOMPARE(ps3.extraCoordinatesCount(), 2);
00151     QCOMPARE(ps3.extraCoordinates()[0], -4.0);
00152     QVERIFY(ps3.includesCoordinates(ps3.data()));
00153     QVERIFY(ps3.includesCoordinates(ps3.data()+ps3.count()-1));
00154     QVERIFY(!ps3.includesCoordinates(ps3.data()-1));
00155     QVERIFY(!ps3.includesCoordinates(ps3.data()+ps3.coordinateCount()));
00156 }//t_getset
00157 
00158 
00159 void QhullPoints_test::
00160 t_element()
00161 {
00162     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00163     QhullPoints ps(2, 6, c);
00164     QhullPoint p(2, c);
00165     QCOMPARE(ps[0], p);
00166     QCOMPARE(ps.at(1), ps[1]);
00167     QCOMPARE(ps.first(), p);
00168     QCOMPARE(ps.front(), ps.first());
00169     QCOMPARE(ps.last(), ps.at(2));
00170     QCOMPARE(ps.back(), ps.last());
00171     QhullPoints ps2= ps.mid(2);
00172     QCOMPARE(ps2.count(), 1);
00173     QhullPoints ps3= ps.mid(3);
00174     QVERIFY(ps3.isEmpty());
00175     QVERIFY(ps3.empty());
00176     QhullPoints ps4= ps.mid(10);
00177     QVERIFY(ps4.isEmpty());
00178     QhullPoints ps5= ps.mid(-1);
00179     QVERIFY(ps5.isEmpty());
00180     QhullPoints ps6= ps.mid(1, 1);
00181     QCOMPARE(ps6.count(), 1);
00182     QCOMPARE(ps6[0], ps[1]);
00183     QhullPoints ps7= ps.mid(1, 10);
00184     QCOMPARE(ps7.count(), 2);
00185     QCOMPARE(ps7[1], ps[2]);
00186     QhullPoint p8;
00187     QCOMPARE(ps.value(2), ps[2]);
00188     QCOMPARE(ps.value(-1), p8);
00189     QCOMPARE(ps.value(3), p8);
00190     QCOMPARE(ps.value(3, p), p);
00191     QVERIFY(ps.value(1, p)!=p);
00192     foreach(QhullPoint p9, ps){  // Qt only
00193         QCOMPARE(p9.dimension(), 2);
00194         QVERIFY(p9[0]==0.0 || p9[0]==2.0 || p9[0]==4.0);
00195     }
00196 }//t_element
00197 
00198 void QhullPoints_test::
00199 t_iterator()
00200 {
00201     coordT c[]= {0.0, 1.0, 2.0};
00202     QhullPoints ps(1, 3, c);
00203     QhullPoints::Iterator i(ps);
00204     QhullPoints::iterator i2= ps.begin();
00205     QVERIFY(i==i2);
00206     QVERIFY(i>=i2);
00207     QVERIFY(i<=i2);
00208     i= ps.begin();
00209     QVERIFY(i==i2);
00210     i2= ps.end();
00211     QVERIFY(i!=i2);
00212     QhullPoint p(i); // QhullPoint is the base class for QhullPoints::iterator
00213     QCOMPARE(p.dimension(), ps.dimension());
00214     QCOMPARE(p.coordinates(), ps.coordinates());
00215     i2--;
00216     QhullPoint p2= *i2;
00217     QCOMPARE(p[0], 0.0);
00218     QCOMPARE(p2[0], 2.0);
00219     QhullPoints::Iterator i5(i2);
00220     QCOMPARE(*i2, *i5);
00221     coordT c3[]= {0.0, -1.0, -2.0};
00222     QhullPoints::Iterator i3(1, c3);
00223     QVERIFY(i!=i3);
00224     QCOMPARE(*i, *i3);
00225 
00226     (i3= i)++;
00227     QCOMPARE((*i3)[0], 1.0);
00228     QCOMPARE(i3->dimension(), 1);
00229     QCOMPARE(i3[0][0], 1.0);
00230     QCOMPARE(i3[0], ps[1]);
00231 
00232     QVERIFY(i==i);
00233     QVERIFY(i!=i2);
00234     QVERIFY(i<i2);
00235     QVERIFY(i<=i2);
00236     QVERIFY(i2>i);
00237     QVERIFY(i2>=i);
00238 
00239     QhullPoints::ConstIterator i4(1, c);
00240     QVERIFY(i==i4); // iterator COMP const_iterator
00241     QVERIFY(i<=i4);
00242     QVERIFY(i>=i4);
00243     QVERIFY(i4==i); // const_iterator COMP iterator
00244     QVERIFY(i4<=i);
00245     QVERIFY(i4>=i);
00246     QVERIFY(i>=i4);
00247     QVERIFY(i4<=i);
00248     QVERIFY(i2!=i4);
00249     QVERIFY(i2>i4);
00250     QVERIFY(i2>=i4);
00251     QVERIFY(i4!=i2);
00252     QVERIFY(i4<i2);
00253     QVERIFY(i4<=i2);
00254     ++i4;
00255     QVERIFY(i<i4);
00256     QVERIFY(i<=i4);
00257     QVERIFY(i4>i);
00258     QVERIFY(i4>=i);
00259 
00260     i= ps.begin();
00261     i2= ps.begin();
00262     QCOMPARE(i, i2++);
00263     QCOMPARE(*i2, ps[1]);
00264     QCOMPARE(++i, i2);
00265     QCOMPARE(i, i2--);
00266     QCOMPARE(i2, ps.begin());
00267     QCOMPARE(--i, i2);
00268     QCOMPARE(i2+=3, ps.end());
00269     QCOMPARE(i2-=3, ps.begin());
00270     QCOMPARE(i2+0, ps.begin());
00271     QCOMPARE(i2+3, ps.end());
00272     i2 += 3;
00273     i= i2-0;
00274     QCOMPARE(i, i2);
00275     i= i2-3;
00276     QCOMPARE(i, ps.begin());
00277     QCOMPARE(i2-i, 3);
00278 
00279     //ps.begin end tested above
00280 
00281     // QhullPoints is const-only
00282 }//t_iterator
00283 
00284 void QhullPoints_test::
00285 t_const_iterator()
00286 {
00287     coordT c[]= {0.0, 1.0, 2.0};
00288     const QhullPoints ps(1, 3, c);
00289     QhullPoints::ConstIterator i(ps);
00290     QhullPoints::const_iterator i2= ps.begin();
00291     QVERIFY(i==i2);
00292     QVERIFY(i>=i2);
00293     QVERIFY(i<=i2);
00294     i= ps.begin();
00295     QVERIFY(i==i2);
00296     i2= ps.end();
00297     QVERIFY(i!=i2);
00298     QhullPoint p(i);
00299     QCOMPARE(p.dimension(), ps.dimension());
00300     QCOMPARE(p.coordinates(), ps.coordinates());
00301     i2--;
00302     QhullPoint p2= *i2;
00303     QCOMPARE(p[0], 0.0);
00304     QCOMPARE(p2[0], 2.0);
00305     QhullPoints::ConstIterator i5(i2);
00306     QCOMPARE(*i2, *i5);
00307     coordT c3[]= {0.0, -1.0, -2.0};
00308     QhullPoints::ConstIterator i3(1, c3);
00309     QVERIFY(i!=i3);
00310     QCOMPARE(*i, *i3);
00311 
00312     (i3= i)++;
00313     QCOMPARE((*i3)[0], 1.0);
00314     QCOMPARE(i3->dimension(), 1);
00315     QCOMPARE(i3[0][0], 1.0);
00316     QCOMPARE(i3[0][0], 1.0);
00317     QCOMPARE(i3[0], ps[1]);
00318 
00319     QVERIFY(i==i);
00320     QVERIFY(i!=i2);
00321     QVERIFY(i<i2);
00322     QVERIFY(i<=i2);
00323     QVERIFY(i2>i);
00324     QVERIFY(i2>=i);
00325 
00326     // See t_iterator for const_iterator COMP iterator
00327 
00328     i= ps.begin();
00329     i2= ps.constBegin();
00330     QCOMPARE(i, i2++);
00331     QCOMPARE(*i2, ps[1]);
00332     QCOMPARE(++i, i2);
00333     QCOMPARE(i, i2--);
00334     QCOMPARE(i2, ps.constBegin());
00335     QCOMPARE(--i, i2);
00336     QCOMPARE(i2+=3, ps.constEnd());
00337     QCOMPARE(i2-=3, ps.constBegin());
00338     QCOMPARE(i2+0, ps.constBegin());
00339     QCOMPARE(i2+3, ps.constEnd());
00340     i2 += 3;
00341     i= i2-0;
00342     QCOMPARE(i, i2);
00343     i= i2-3;
00344     QCOMPARE(i, ps.constBegin());
00345     QCOMPARE(i2-i, 3);
00346 
00347     // QhullPoints is const-only
00348 }//t_const_iterator
00349 
00350 
00351 void QhullPoints_test::
00352 t_search()
00353 {
00354     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 0, 1};
00355     QhullPoints ps(2, 8, c); //2-d array of 4 points
00356     QhullPoint p= ps.first();
00357     QhullPoint p2= ps.last();
00358     QVERIFY(ps.contains(p));
00359     QVERIFY(ps.contains(p2));
00360     QVERIFY(p==p2);
00361     QhullPoint p5= ps[2];
00362     QVERIFY(p!=p5);
00363     QVERIFY(ps.contains(p5));
00364     coordT c2[]= {0.0, 1.0, 2.0, 3.0};
00365     QhullPoint p3(2, c2); //2-d point
00366     QVERIFY(ps.contains(p3));
00367     QhullPoint p4(3, c2); //3-d point
00368     QVERIFY(!ps.contains(p4));
00369     p4.defineAs(2, c); //2-d point
00370     QVERIFY(ps.contains(p4));
00371     p4.defineAs(2, c+1); //2-d point
00372     QVERIFY(!ps.contains(p4));
00373     QhullPoint p6(2, c2+2); //2-d point
00374     QCOMPARE(ps.count(p), 2);
00375     QCOMPARE(ps.count(p2), 2);
00376     QCOMPARE(ps.count(p3), 2);
00377     QCOMPARE(ps.count(p4), 0);
00378     QCOMPARE(ps.count(p6), 1);
00379     QCOMPARE(ps.indexOf(&ps[0][0]), 0);
00380     //QCOMPARE(ps.indexOf(ps.end()), -1); //ps.end() is a QhullPoint which may match
00381     QCOMPARE(ps.indexOf(0), -1);
00382     QCOMPARE(ps.indexOf(&ps[3][0]), 3);
00383     QCOMPARE(ps.indexOf(&ps[3][1], QhullError::NOthrow), 3);
00384     QCOMPARE(ps.indexOf(ps.data()+ps.coordinateCount(), QhullError::NOthrow), -1);
00385     QCOMPARE(ps.indexOf(p), 0);
00386     QCOMPARE(ps.indexOf(p2), 0);
00387     QCOMPARE(ps.indexOf(p3), 0);
00388     QCOMPARE(ps.indexOf(p4), -1);
00389     QCOMPARE(ps.indexOf(p5), 2);
00390     QCOMPARE(ps.indexOf(p6), 1);
00391     QCOMPARE(ps.lastIndexOf(p), 3);
00392     QCOMPARE(ps.lastIndexOf(p4), -1);
00393     QCOMPARE(ps.lastIndexOf(p6), 1);
00394     QhullPoints ps2(3);
00395     QCOMPARE(ps2.indexOf(ps2.data()), -1);
00396     QCOMPARE(ps2.indexOf(ps2.data()+1, QhullError::NOthrow), -1);
00397     QCOMPARE(ps2.indexOf(p), -1);
00398     QCOMPARE(ps2.lastIndexOf(p), -1);
00399     QhullPoints ps3;
00400     QCOMPARE(ps3.indexOf(ps3.data()), -1);
00401     QCOMPARE(ps3.indexOf(ps3.data()+1, QhullError::NOthrow), -1);
00402     QCOMPARE(ps3.indexOf(p), -1);
00403     QCOMPARE(ps3.lastIndexOf(p), -1);
00404     QhullPoints ps4(2, 0, c);
00405     QCOMPARE(ps4.indexOf(p), -1);
00406     QCOMPARE(ps4.lastIndexOf(p), -1);
00407 }//t_search
00408 
00409 void QhullPoints_test::
00410 t_points_iterator()
00411 {
00412     coordT c2[]= {0.0};
00413     QhullPoints ps2(0, 0, c2); // 0-dimensional
00414     QhullPointsIterator i2= ps2;
00415     QVERIFY(!i2.hasNext());
00416     QVERIFY(!i2.hasPrevious());
00417     i2.toBack();
00418     QVERIFY(!i2.hasNext());
00419     QVERIFY(!i2.hasPrevious());
00420 
00421     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00422     QhullPoints ps(3, 6, c); // 3-dimensional
00423     QhullPointsIterator i(ps);
00424     i2= ps;
00425     QVERIFY(i2.hasNext());
00426     QVERIFY(!i2.hasPrevious());
00427     QVERIFY(i.hasNext());
00428     QVERIFY(!i.hasPrevious());
00429     i2.toBack();
00430     i.toFront();
00431     QVERIFY(!i2.hasNext());
00432     QVERIFY(i2.hasPrevious());
00433     QVERIFY(i.hasNext());
00434     QVERIFY(!i.hasPrevious());
00435 
00436     QhullPoint p= ps[0];
00437     QhullPoint p2(ps[0]);
00438     QCOMPARE(p, p2);
00439     QVERIFY(p==p2);
00440     QhullPoint p3(ps[1]);
00441  // p2[0]= 0.0;
00442     QVERIFY(p==p2);
00443     QCOMPARE(i2.peekPrevious(), p3);
00444     QCOMPARE(i2.previous(), p3);
00445     QCOMPARE(i2.previous(), p);
00446     QVERIFY(!i2.hasPrevious());
00447     QCOMPARE(i.peekNext(), p);
00448     // i.peekNext()= 1.0; // compiler error
00449     QCOMPARE(i.next(), p);
00450     QCOMPARE(i.peekNext(), p3);
00451     QCOMPARE(i.next(), p3);
00452     QVERIFY(!i.hasNext());
00453     i.toFront();
00454     QCOMPARE(i.next(), p);
00455 }//t_points_iterator
00456 
00457 void QhullPoints_test::
00458 t_io()
00459 {
00460     QhullPoints ps;
00461     ostringstream os;
00462     os << "Empty QhullPoints\n" << ps << endl;
00463     coordT c[]= {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
00464     QhullPoints ps2(3, 6, c); // 3-dimensional explicit
00465     os << "QhullPoints from c[]\n" << ps2 << endl;
00466     RboxPoints rcube("c");
00467     Qhull q(rcube,"Qt QR0");  // triangulation of rotated unit cube
00468     QhullPoints ps3= q.points();
00469     os << "QhullPoints\n" << ps3;
00470     os << "RunId\n" << ps3.print(q.runId());
00471     os << ps3.print(q.runId(), "RunId w/ message\n");
00472     os << ps3.printWithIdentifier(q.runId(), "RunId w/ identifiers\n");
00473     cout << os.str();
00474     QString s= QString::fromStdString(os.str());
00475     QCOMPARE(s.count("p"), 3*8+3);
00476     // QCOMPARE(s.count(QRegExp("f\\d")), 3*7 + 13*3*2);
00477 }//t_io
00478 
00479 }//orgQhull
00480 
00481 #include "moc/QhullPoints_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:50