00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <iostream>
00011 #include "../road/RoadTest.h"
00012
00013 #include "Coordinates.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
00024 namespace orgQhull {
00025
00026 class Coordinates_test : public RoadTest
00027 {
00028 Q_OBJECT
00029
00030 #//Test slots
00031 private slots:
00032 void t_construct();
00033 void t_convert();
00034 void t_element();
00035 void t_readonly();
00036 void t_operator();
00037 void t_const_iterator();
00038 void t_iterator();
00039 void t_coord_iterator();
00040 void t_mutable_coord_iterator();
00041 void t_readwrite();
00042 void t_search();
00043 void t_io();
00044 };
00045
00046 void
00047 add_Coordinates_test()
00048 {
00049 new Coordinates_test();
00050 }
00051
00052 void Coordinates_test::
00053 t_construct()
00054 {
00055 Coordinates c;
00056 QCOMPARE(c.size(), 0U);
00057 QVERIFY(c.isEmpty());
00058 c << 1.0;
00059 QCOMPARE(c.count(), 1);
00060 Coordinates c2(c);
00061 c2 << 2.0;
00062 QCOMPARE(c2.count(), 2);
00063 Coordinates c3;
00064 c3 = c2;
00065 QCOMPARE(c3.count(), 2);
00066 QCOMPARE(c3[0]+c3[1], 3.0);
00067 QVERIFY(c2==c3);
00068 std::vector<coordT> vc;
00069 vc.push_back(3.0);
00070 vc.push_back(4.0);
00071 Coordinates c4(vc);
00072 QCOMPARE(c4[0]+c4[1], 7.0);
00073 Coordinates c5(c3);
00074 QVERIFY(c5==c3);
00075 c5= vc;
00076 QVERIFY(c5!=c3);
00077 QVERIFY(c5==c4);
00078 }
00079
00080 void Coordinates_test::
00081 t_convert()
00082 {
00083 Coordinates c;
00084 c << 1.0 << 3.0;
00085 QCOMPARE(c.data()[1], 3.0);
00086 coordT *c2= c.data();
00087 const coordT *c3= c.data();
00088 QCOMPARE(c2, c3);
00089 std::vector<coordT> vc= c.toStdVector();
00090 QCOMPARE(vc.size(), c.size());
00091 for(size_t k= vc.size(); k--; ){
00092 QCOMPARE(vc[k], c[k]);
00093 }
00094 QList<coordT> qc= c.toQList();
00095 QCOMPARE(qc.count(), c.count());
00096 for(int k= qc.count(); k--; ){
00097 QCOMPARE(qc[k], c[k]);
00098 }
00099 Coordinates c4;
00100 c4= std::vector<double>(2, 0.0);
00101 QCOMPARE(c4.back(), 0.0);
00102 Coordinates c5(std::vector<double>(2, 0.0));
00103 QCOMPARE(c4.size(), c5.size());
00104 QVERIFY(c4==c5);
00105 }
00106
00107 void Coordinates_test::
00108 t_element()
00109 {
00110 Coordinates c;
00111 c << 1.0 << -2.0;
00112 c.at(1)= -3;
00113 QCOMPARE(c.at(1), -3.0);
00114 QCOMPARE(c.back(), -3.0);
00115 QCOMPARE(c.front(), 1.0);
00116 c[1]= -2.0;
00117 QCOMPARE(c[1],-2.0);
00118 QCOMPARE(c.first(), 1.0);
00119 c.first()= 2.0;
00120 QCOMPARE(c.first(), 2.0);
00121 QCOMPARE(c.last(), -2.0);
00122 c.last()= 0.0;
00123 QCOMPARE(c.first()+c.last(), 2.0);
00124 coordT *c4= &c.first();
00125 const coordT *c5= &c.first();
00126 QCOMPARE(c4, c5);
00127 coordT *c6= &c.last();
00128 const coordT *c7= &c.last();
00129 QCOMPARE(c6, c7);
00130 Coordinates c2= c.mid(1);
00131 QCOMPARE(c2.count(), 1);
00132 c << 3.0;
00133 Coordinates c3= c.mid(1,1);
00134 QCOMPARE(c2, c3);
00135 QCOMPARE(c3.value(-1, -1.0), -1.0);
00136 QCOMPARE(c3.value(3, 4.0), 4.0);
00137 QCOMPARE(c.value(2, 4.0), 3.0);
00138 }
00139
00140 void Coordinates_test::
00141 t_readonly()
00142 {
00143 Coordinates c;
00144 QCOMPARE(c.size(), 0u);
00145 QCOMPARE(c.count(), 0);
00146 QVERIFY(c.empty());
00147 QVERIFY(c.isEmpty());
00148 c << 1.0 << -2.0;
00149 QCOMPARE(c.size(), 2u);
00150 QCOMPARE(c.count(), 2);
00151 QVERIFY(!c.empty());
00152 QVERIFY(!c.isEmpty());
00153 }
00154
00155 void Coordinates_test::
00156 t_operator()
00157 {
00158 Coordinates c;
00159 Coordinates c2(c);
00160 QVERIFY(c==c2);
00161 QVERIFY(!(c!=c2));
00162 c << 1.0;
00163 QVERIFY(!(c==c2));
00164 QVERIFY(c!=c2);
00165 c2 << 1.0;
00166 QVERIFY(c==c2);
00167 QVERIFY(!(c!=c2));
00168 c[0]= 0.0;
00169 QVERIFY(c!=c2);
00170 Coordinates c3= c+c2;
00171 QCOMPARE(c3.count(), 2);
00172 QCOMPARE(c3[0], 0.0);
00173 QCOMPARE(c3[1], 1.0);
00174 c3 += c3;
00175 QCOMPARE(c3.count(), 4);
00176 QCOMPARE(c3[2], 0.0);
00177 QCOMPARE(c3[3], 1.0);
00178 c3 += c2;
00179 QCOMPARE(c3[4], 1.0);
00180 c3 += 5.0;
00181 QCOMPARE(c3.count(), 6);
00182 QCOMPARE(c3[5], 5.0);
00183
00184 }
00185
00186 void Coordinates_test::
00187 t_const_iterator()
00188 {
00189 Coordinates c;
00190 QCOMPARE(c.begin(), c.end());
00191
00192 c << 1.0 << 3.0;
00193 Coordinates::const_iterator i= c.begin();
00194 QCOMPARE(*i, 1.0);
00195 QCOMPARE(i[1], 3.0);
00196
00197
00198 QCOMPARE(*i++, 1.0);
00199 QCOMPARE(*i, 3.0);
00200 QCOMPARE(*i--, 3.0);
00201 QCOMPARE(*i, 1.0);
00202 QCOMPARE(*(i+1), 3.0);
00203 QCOMPARE(*++i, 3.0);
00204 QCOMPARE(*(i-1), 1.0);
00205 QCOMPARE(*--i, 1.0);
00206 QVERIFY(i==c.begin());
00207 QVERIFY(i==c.constBegin());
00208 QVERIFY(i!=c.end());
00209 QVERIFY(i!=c.constEnd());
00210 QVERIFY(i<c.end());
00211 QVERIFY(i>=c.begin());
00212 QVERIFY(i+1<=c.end());
00213 QVERIFY(i+1>c.begin());
00214 Coordinates::iterator i2= c.begin();
00215 Coordinates::const_iterator i3(i2);
00216 QCOMPARE(*i3, 1.0);
00217 QCOMPARE(i3[1], 3.0);
00218 }
00219
00220 void Coordinates_test::
00221 t_iterator()
00222 {
00223 Coordinates c;
00224 QCOMPARE(c.begin(), c.end());
00225
00226 c << 1.0 << 3.0;
00227 Coordinates::iterator i= c.begin();
00228 QCOMPARE(*i, 1.0);
00229 QCOMPARE(i[1], 3.0);
00230 *i= -1.0;
00231 QCOMPARE(*i, -1.0);
00232 i[1]= -3.0;
00233 QCOMPARE(i[1], -3.0);
00234 *i= 1.0;
00235
00236 QCOMPARE(*i++, 1.0);
00237 QCOMPARE(*i, -3.0);
00238 *i= 3.0;
00239 QCOMPARE(*i--, 3.0);
00240 QCOMPARE(*i, 1.0);
00241 QCOMPARE(*(i+1), 3.0);
00242 QCOMPARE(*++i, 3.0);
00243 QCOMPARE(*(i-1), 1.0);
00244 QCOMPARE(*--i, 1.0);
00245 QVERIFY(i==c.begin());
00246 QVERIFY(i==c.constBegin());
00247 QVERIFY(i!=c.end());
00248 QVERIFY(i!=c.constEnd());
00249 QVERIFY(i<c.end());
00250 QVERIFY(i>=c.begin());
00251 QVERIFY(i+1<=c.end());
00252 QVERIFY(i+1>c.begin());
00253 }
00254
00255 void Coordinates_test::
00256 t_coord_iterator()
00257 {
00258 Coordinates c;
00259 c << 1.0 << 3.0;
00260 CoordinatesIterator i(c);
00261 CoordinatesIterator i2= c;
00262 QVERIFY(i.findNext(1.0));
00263 QVERIFY(!i.findNext(2.0));
00264 QVERIFY(!i.findNext(3.0));
00265 QVERIFY(i.findPrevious(3.0));
00266 QVERIFY(!i.findPrevious(2.0));
00267 QVERIFY(!i.findPrevious(1.0));
00268 QVERIFY(i2.findNext(3.0));
00269 QVERIFY(i2.findPrevious(3.0));
00270 QVERIFY(i2.findNext(3.0));
00271 QVERIFY(i2.findPrevious(1.0));
00272 QVERIFY(i2.hasNext());
00273 QVERIFY(!i2.hasPrevious());
00274 QVERIFY(i.hasNext());
00275 QVERIFY(!i.hasPrevious());
00276 i.toBack();
00277 i2.toFront();
00278 QVERIFY(!i.hasNext());
00279 QVERIFY(i.hasPrevious());
00280 QVERIFY(i2.hasNext());
00281 QVERIFY(!i2.hasPrevious());
00282 Coordinates c2;
00283 i2= c2;
00284 QVERIFY(!i2.hasNext());
00285 QVERIFY(!i2.hasPrevious());
00286 i2.toBack();
00287 QVERIFY(!i2.hasNext());
00288 QVERIFY(!i2.hasPrevious());
00289 QCOMPARE(i.peekPrevious(), 3.0);
00290 QCOMPARE(i.previous(), 3.0);
00291 QCOMPARE(i.previous(), 1.0);
00292 QVERIFY(!i.hasPrevious());
00293 QCOMPARE(i.peekNext(), 1.0);
00294
00295 QCOMPARE(i.next(), 1.0);
00296 QCOMPARE(i.peekNext(), 3.0);
00297 QCOMPARE(i.next(), 3.0);
00298 QVERIFY(!i.hasNext());
00299 i.toFront();
00300 QCOMPARE(i.next(), 1.0);
00301 }
00302
00303 void Coordinates_test::
00304 t_mutable_coord_iterator()
00305 {
00306
00307 Coordinates c;
00308 c << 1.0 << 3.0;
00309 MutableCoordinatesIterator i(c);
00310 MutableCoordinatesIterator i2= c;
00311 QVERIFY(i.findNext(1.0));
00312 QVERIFY(!i.findNext(2.0));
00313 QVERIFY(!i.findNext(3.0));
00314 QVERIFY(i.findPrevious(3.0));
00315 QVERIFY(!i.findPrevious(2.0));
00316 QVERIFY(!i.findPrevious(1.0));
00317 QVERIFY(i2.findNext(3.0));
00318 QVERIFY(i2.findPrevious(3.0));
00319 QVERIFY(i2.findNext(3.0));
00320 QVERIFY(i2.findPrevious(1.0));
00321 QVERIFY(i2.hasNext());
00322 QVERIFY(!i2.hasPrevious());
00323 QVERIFY(i.hasNext());
00324 QVERIFY(!i.hasPrevious());
00325 i.toBack();
00326 i2.toFront();
00327 QVERIFY(!i.hasNext());
00328 QVERIFY(i.hasPrevious());
00329 QVERIFY(i2.hasNext());
00330 QVERIFY(!i2.hasPrevious());
00331 Coordinates c2;
00332 i2= c2;
00333 QVERIFY(!i2.hasNext());
00334 QVERIFY(!i2.hasPrevious());
00335 i2.toBack();
00336 QVERIFY(!i2.hasNext());
00337 QVERIFY(!i2.hasPrevious());
00338 QCOMPARE(i.peekPrevious(), 3.0);
00339 QCOMPARE(i.peekPrevious(), 3.0);
00340 QCOMPARE(i.previous(), 3.0);
00341 QCOMPARE(i.previous(), 1.0);
00342 QVERIFY(!i.hasPrevious());
00343 QCOMPARE(i.peekNext(), 1.0);
00344 QCOMPARE(i.next(), 1.0);
00345 QCOMPARE(i.peekNext(), 3.0);
00346 QCOMPARE(i.next(), 3.0);
00347 QVERIFY(!i.hasNext());
00348 i.toFront();
00349 QCOMPARE(i.next(), 1.0);
00350
00351
00352 i.toFront();
00353 i.peekNext()= -1.0;
00354 QCOMPARE(i.peekNext(), -1.0);
00355 QCOMPARE((i.next()= 1.0), 1.0);
00356 QCOMPARE(i.peekPrevious(), 1.0);
00357 i.remove();
00358 QCOMPARE(c.count(), 1);
00359 i.remove();
00360 QCOMPARE(c.count(), 1);
00361 QCOMPARE(i.peekNext(), 3.0);
00362 i.insert(1.0);
00363 i.insert(2.0);
00364 QCOMPARE(c.count(), 3);
00365 QCOMPARE(i.peekNext(), 3.0);
00366 QCOMPARE(i.peekPrevious(), 2.0);
00367 i.peekPrevious()= -2.0;
00368 QCOMPARE(i.peekPrevious(), -2.0);
00369 QCOMPARE((i.previous()= 2.0), 2.0);
00370 QCOMPARE(i.peekNext(), 2.0);
00371 i.toBack();
00372 i.remove();
00373 QCOMPARE(c.count(), 3);
00374 i.toFront();
00375 i.remove();
00376 QCOMPARE(c.count(), 3);
00377 QCOMPARE(i.peekNext(), 1.0);
00378 i.remove();
00379 QCOMPARE(c.count(), 3);
00380 i.insert(0.0);
00381 QCOMPARE(c.count(), 4);
00382 QCOMPARE(i.value(), 0.0);
00383 QCOMPARE(i.peekPrevious(), 0.0);
00384 i.setValue(-10.0);
00385 QCOMPARE(c.count(), 4);
00386 QCOMPARE(i.peekNext(), 1.0);
00387 QCOMPARE(i.peekPrevious(), -10.0);
00388 i.findNext(1.0);
00389 i.setValue(-1.0);
00390 QCOMPARE(i.peekPrevious(), -1.0);
00391 i.setValue(1.0);
00392 QCOMPARE(i.peekPrevious(), 1.0);
00393 QCOMPARE(i.value(), 1.0);
00394 i.findPrevious(1.0);
00395 i.setValue(-1.0);
00396 QCOMPARE(i.peekNext(), -1.0);
00397 i.toBack();
00398 QCOMPARE(i.previous(), 3.0);
00399 i.setValue(-3.0);
00400 QCOMPARE(i.peekNext(), -3.0);
00401 double d= i.value();
00402 QCOMPARE(d, -3.0);
00403 QCOMPARE(i.previous(), 2.0);
00404 }
00405
00406 void Coordinates_test::
00407 t_readwrite()
00408 {
00409 Coordinates c;
00410 c.clear();
00411 QCOMPARE(c.count(), 0);
00412 c << 1.0 << 3.0;
00413 c.clear();
00414 QCOMPARE(c.count(), 0);
00415 c << 1.0 << 3.0;
00416 c.erase(c.begin(), c.end());
00417 QCOMPARE(c.count(), 0);
00418 c << 1.0 << 0.0;
00419 Coordinates::iterator i= c.erase(c.begin());
00420 QCOMPARE(*i, 0.0);
00421 i= c.insert(c.end(), 1.0);
00422 QCOMPARE(*i, 1.0);
00423 QCOMPARE(c.count(), 2);
00424 c.pop_back();
00425 QCOMPARE(c.count(), 1);
00426 QCOMPARE(c[0], 0.0);
00427 c.push_back(2.0);
00428 QCOMPARE(c.count(), 2);
00429 c.append(3.0);
00430 QCOMPARE(c.count(), 3);
00431 QCOMPARE(c[2], 3.0);
00432 c.insert(0, 4.0);
00433 QCOMPARE(c[0], 4.0);
00434 QCOMPARE(c[3], 3.0);
00435 c.insert(c.count(), 5.0);
00436 QCOMPARE(c.count(), 5);
00437 QCOMPARE(c[4], 5.0);
00438 c.move(4, 0);
00439 QCOMPARE(c.count(), 5);
00440 QCOMPARE(c[0], 5.0);
00441 c.pop_front();
00442 QCOMPARE(c.count(), 4);
00443 QCOMPARE(c[0], 4.0);
00444 c.prepend(6.0);
00445 QCOMPARE(c.count(), 5);
00446 QCOMPARE(c[0], 6.0);
00447 c.push_front(7.0);
00448 QCOMPARE(c.count(), 6);
00449 QCOMPARE(c[0], 7.0);
00450 c.removeAt(1);
00451 QCOMPARE(c.count(), 5);
00452 QCOMPARE(c[1], 4.0);
00453 c.removeFirst();
00454 QCOMPARE(c.count(), 4);
00455 QCOMPARE(c[0], 4.0);
00456 c.removeLast();
00457 QCOMPARE(c.count(), 3);
00458 QCOMPARE(c.last(), 2.0);
00459 c.replace(2, 8.0);
00460 QCOMPARE(c.count(), 3);
00461 QCOMPARE(c[2], 8.0);
00462 c.swap(0, 2);
00463 QCOMPARE(c[2], 4.0);
00464 double d= c.takeAt(2);
00465 QCOMPARE(c.count(), 2);
00466 QCOMPARE(d, 4.0);
00467 double d2= c.takeFirst();
00468 QCOMPARE(c.count(), 1);
00469 QCOMPARE(d2, 8.0);
00470 double d3= c.takeLast();
00471 QVERIFY(c.isEmpty()); \
00472 QCOMPARE(d3, 0.0);
00473 }
00474
00475 void Coordinates_test::
00476 t_search()
00477 {
00478 Coordinates c;
00479 c << 1.0 << 3.0 << 1.0;
00480 QVERIFY(c.contains(1.0));
00481 QVERIFY(c.contains(3.0));
00482 QVERIFY(!c.contains(0.0));
00483 QCOMPARE(c.count(1.0), 2);
00484 QCOMPARE(c.count(3.0), 1);
00485 QCOMPARE(c.count(0.0), 0);
00486 QCOMPARE(c.indexOf(1.0), 0);
00487 QCOMPARE(c.indexOf(3.0), 1);
00488 QCOMPARE(c.indexOf(1.0, -1), 2);
00489 QCOMPARE(c.indexOf(3.0, -1), -1);
00490 QCOMPARE(c.indexOf(3.0, -2), 1);
00491 QCOMPARE(c.indexOf(1.0, -3), 0);
00492 QCOMPARE(c.indexOf(1.0, -4), 0);
00493 QCOMPARE(c.indexOf(1.0, 1), 2);
00494 QCOMPARE(c.indexOf(3.0, 2), -1);
00495 QCOMPARE(c.indexOf(1.0, 2), 2);
00496 QCOMPARE(c.indexOf(1.0, 3), -1);
00497 QCOMPARE(c.indexOf(1.0, 4), -1);
00498 QCOMPARE(c.lastIndexOf(1.0), 2);
00499 QCOMPARE(c.lastIndexOf(3.0), 1);
00500 QCOMPARE(c.lastIndexOf(1.0, -1), 2);
00501 QCOMPARE(c.lastIndexOf(3.0, -1), 1);
00502 QCOMPARE(c.lastIndexOf(3.0, -2), 1);
00503 QCOMPARE(c.lastIndexOf(1.0, -3), 0);
00504 QCOMPARE(c.lastIndexOf(1.0, -4), -1);
00505 QCOMPARE(c.lastIndexOf(1.0, 1), 0);
00506 QCOMPARE(c.lastIndexOf(3.0, 2), 1);
00507 QCOMPARE(c.lastIndexOf(1.0, 2), 2);
00508 QCOMPARE(c.lastIndexOf(1.0, 3), 2);
00509 QCOMPARE(c.lastIndexOf(1.0, 4), 2);
00510 c.removeAll(3.0);
00511 QCOMPARE(c.count(), 2);
00512 c.removeAll(4.0);
00513 QCOMPARE(c.count(), 2);
00514 c.removeAll(1.0);
00515 QCOMPARE(c.count(), 0);
00516 c.removeAll(4.0);
00517 QCOMPARE(c.count(), 0);
00518 }
00519
00520 void Coordinates_test::
00521 t_io()
00522 {
00523 Coordinates c;
00524 c << 1.0 << 2.0 << 3.0;
00525 ostringstream os;
00526 os << "Coordinates 1-2-3\n" << c;
00527 cout << os.str();
00528 QString s= QString::fromStdString(os.str());
00529 QCOMPARE(s.count("2"), 2);
00530 }
00531
00532 }
00533
00534 #include "moc/Coordinates_test.moc"