Coordinates_test.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (c) 2009-2015 C.B. Barber. All rights reserved.
4 ** $Id: //main/2015/qhull/src/qhulltest/Coordinates_test.cpp#2 $$Change: 2062 $
5 ** $DateTime: 2016/01/17 13:13:18 $$Author: bbarber $
6 **
7 ****************************************************************************/
8 
9 //pre-compiled headers
10 #include <iostream>
11 #include "qhulltest/RoadTest.h" // QT_VERSION
12 
14 #include "libqhullcpp/QhullError.h"
15 #include "libqhullcpp/RboxPoints.h"
16 #include "libqhullcpp/Qhull.h"
17 
18 using std::cout;
19 using std::endl;
20 using std::ostringstream;
21 using std::ostream;
22 using std::string;
23 
24 namespace orgQhull {
25 
26 class Coordinates_test : public RoadTest
27 {
28  Q_OBJECT
29 
30 #
31 private slots:
32  void t_construct();
33  void t_convert();
34  void t_element();
35  void t_readonly();
36  void t_operator();
37  void t_const_iterator();
38  void t_iterator();
39  void t_coord_iterator();
41  void t_readwrite();
42  void t_search();
43  void t_io();
44 };//Coordinates_test
45 
46 void
48 {
49  new Coordinates_test(); // RoadTest::s_testcases
50 }
51 
54 {
55  Coordinates c;
56  QCOMPARE(c.size(), 0U);
57  QVERIFY(c.isEmpty());
58  c << 1.0;
59  QCOMPARE(c.count(), 1);
60  Coordinates c2(c);
61  c2 << 2.0;
62  QCOMPARE(c2.count(), 2);
63  Coordinates c3;
64  c3 = c2;
65  QCOMPARE(c3.count(), 2);
66  QCOMPARE(c3[0]+c3[1], 3.0);
67  QVERIFY(c2==c3);
68  std::vector<coordT> vc;
69  vc.push_back(3.0);
70  vc.push_back(4.0);
71  Coordinates c4(vc);
72  QCOMPARE(c4[0]+c4[1], 7.0);
73  Coordinates c5(c3);
74  QVERIFY(c5==c3);
75  c5= vc;
76  QVERIFY(c5!=c3);
77  QVERIFY(c5==c4);
78 }//t_construct
79 
82 {
83  Coordinates c;
84  c << 1.0 << 3.0;
85  QCOMPARE(c.data()[1], 3.0);
86  coordT *c2= c.data();
87  const coordT *c3= c.data();
88  QCOMPARE(c2, c3);
89  std::vector<coordT> vc= c.toStdVector();
90  QCOMPARE((size_t)vc.size(), c.size());
91  for(int k= (int)vc.size(); k--; ){
92  QCOMPARE(vc[k], c[k]);
93  }
94  QList<coordT> qc= c.toQList();
95  QCOMPARE(qc.count(), c.count());
96  for(int k= qc.count(); k--; ){
97  QCOMPARE(qc[k], c[k]);
98  }
99  Coordinates c4;
100  c4= std::vector<double>(2, 0.0);
101  QCOMPARE(c4.back(), 0.0);
102  Coordinates c5(std::vector<double>(2, 0.0));
103  QCOMPARE(c4.size(), c5.size());
104  QVERIFY(c4==c5);
105 }//t_convert
106 
109 {
110  Coordinates c;
111  c << 1.0 << -2.0;
112  c.at(1)= -3;
113  QCOMPARE(c.at(1), -3.0);
114  QCOMPARE(c.back(), -3.0);
115  QCOMPARE(c.front(), 1.0);
116  c[1]= -2.0;
117  QCOMPARE(c[1],-2.0);
118  QCOMPARE(c.first(), 1.0);
119  c.first()= 2.0;
120  QCOMPARE(c.first(), 2.0);
121  QCOMPARE(c.last(), -2.0);
122  c.last()= 0.0;
123  QCOMPARE(c.first()+c.last(), 2.0);
124  coordT *c4= &c.first();
125  const coordT *c5= &c.first();
126  QCOMPARE(c4, c5);
127  coordT *c6= &c.last();
128  const coordT *c7= &c.last();
129  QCOMPARE(c6, c7);
130  Coordinates c2= c.mid(1);
131  QCOMPARE(c2.count(), 1);
132  c << 3.0;
133  Coordinates c3= c.mid(1,1);
134  QCOMPARE(c2, c3);
135  QCOMPARE(c3.value(-1, -1.0), -1.0);
136  QCOMPARE(c3.value(3, 4.0), 4.0);
137  QCOMPARE(c.value(2, 4.0), 3.0);
138 }//t_element
139 
142 {
143  Coordinates c;
144  QCOMPARE(c.size(), 0u);
145  QCOMPARE(c.count(), 0);
146  QVERIFY(c.isEmpty());
147  c << 1.0 << -2.0;
148  QCOMPARE(c.size(), 2u);
149  QCOMPARE(c.count(), 2);
150  QVERIFY(!c.isEmpty());
151 }//t_readonly
152 
155 {
156  Coordinates c;
157  Coordinates c2(c);
158  QVERIFY(c==c2);
159  QVERIFY(!(c!=c2));
160  c << 1.0;
161  QVERIFY(!(c==c2));
162  QVERIFY(c!=c2);
163  c2 << 1.0;
164  QVERIFY(c==c2);
165  QVERIFY(!(c!=c2));
166  c[0]= 0.0;
167  QVERIFY(c!=c2);
168  Coordinates c3= c+c2;
169  QCOMPARE(c3.count(), 2);
170  QCOMPARE(c3[0], 0.0);
171  QCOMPARE(c3[1], 1.0);
172  c3 += c3;
173  QCOMPARE(c3.count(), 4);
174  QCOMPARE(c3[2], 0.0);
175  QCOMPARE(c3[3], 1.0);
176  c3 += c2;
177  QCOMPARE(c3[4], 1.0);
178  c3 += 5.0;
179  QCOMPARE(c3.count(), 6);
180  QCOMPARE(c3[5], 5.0);
181  // << checked above
182 }//t_operator
183 
186 {
187  Coordinates c;
188  QCOMPARE(c.begin(), c.end());
189  // begin and end checked elsewhere
190  c << 1.0 << 3.0;
192  QCOMPARE(*i, 1.0);
193  QCOMPARE(i[1], 3.0);
194  // i[1]= -3.0; // compiler error
195  // operator-> is not applicable to double
196  QCOMPARE(*i++, 1.0);
197  QCOMPARE(*i, 3.0);
198  QCOMPARE(*i--, 3.0);
199  QCOMPARE(*i, 1.0);
200  QCOMPARE(*(i+1), 3.0);
201  QCOMPARE(*++i, 3.0);
202  QCOMPARE(*(i-1), 1.0);
203  QCOMPARE(*--i, 1.0);
204  QVERIFY(i==c.begin());
205  QVERIFY(i==c.constBegin());
206  QVERIFY(i!=c.end());
207  QVERIFY(i!=c.constEnd());
208  QVERIFY(i<c.end());
209  QVERIFY(i>=c.begin());
210  QVERIFY(i+1<=c.end());
211  QVERIFY(i+1>c.begin());
214  QCOMPARE(*i3, 1.0);
215  QCOMPARE(i3[1], 3.0);
216 }//t_const_iterator
217 
220 {
221  Coordinates c;
222  QCOMPARE(c.begin(), c.end());
223  // begin and end checked elsewhere
224  c << 1.0 << 3.0;
226  QCOMPARE(*i, 1.0);
227  QCOMPARE(i[1], 3.0);
228  *i= -1.0;
229  QCOMPARE(*i, -1.0);
230  i[1]= -3.0;
231  QCOMPARE(i[1], -3.0);
232  *i= 1.0;
233  // operator-> is not applicable to double
234  QCOMPARE(*i++, 1.0);
235  QCOMPARE(*i, -3.0);
236  *i= 3.0;
237  QCOMPARE(*i--, 3.0);
238  QCOMPARE(*i, 1.0);
239  QCOMPARE(*(i+1), 3.0);
240  QCOMPARE(*++i, 3.0);
241  QCOMPARE(*(i-1), 1.0);
242  QCOMPARE(*--i, 1.0);
243  QVERIFY(i==c.begin());
244  QVERIFY(i==c.constBegin());
245  QVERIFY(i!=c.end());
246  QVERIFY(i!=c.constEnd());
247  QVERIFY(i<c.end());
248  QVERIFY(i>=c.begin());
249  QVERIFY(i+1<=c.end());
250  QVERIFY(i+1>c.begin());
251 }//t_iterator
252 
255 {
256  Coordinates c;
257  c << 1.0 << 3.0;
258  CoordinatesIterator i(c);
259  CoordinatesIterator i2= c;
260  QVERIFY(i.findNext(1.0));
261  QVERIFY(!i.findNext(2.0));
262  QVERIFY(!i.findNext(3.0));
263  QVERIFY(i.findPrevious(3.0));
264  QVERIFY(!i.findPrevious(2.0));
265  QVERIFY(!i.findPrevious(1.0));
266  QVERIFY(i2.findNext(3.0));
267  QVERIFY(i2.findPrevious(3.0));
268  QVERIFY(i2.findNext(3.0));
269  QVERIFY(i2.findPrevious(1.0));
270  QVERIFY(i2.hasNext());
271  QVERIFY(!i2.hasPrevious());
272  QVERIFY(i.hasNext());
273  QVERIFY(!i.hasPrevious());
274  i.toBack();
275  i2.toFront();
276  QVERIFY(!i.hasNext());
277  QVERIFY(i.hasPrevious());
278  QVERIFY(i2.hasNext());
279  QVERIFY(!i2.hasPrevious());
280  Coordinates c2;
281  i2= c2;
282  QVERIFY(!i2.hasNext());
283  QVERIFY(!i2.hasPrevious());
284  i2.toBack();
285  QVERIFY(!i2.hasNext());
286  QVERIFY(!i2.hasPrevious());
287  QCOMPARE(i.peekPrevious(), 3.0);
288  QCOMPARE(i.previous(), 3.0);
289  QCOMPARE(i.previous(), 1.0);
290  QVERIFY(!i.hasPrevious());
291  QCOMPARE(i.peekNext(), 1.0);
292  // i.peekNext()= 1.0; // compiler error
293  QCOMPARE(i.next(), 1.0);
294  QCOMPARE(i.peekNext(), 3.0);
295  QCOMPARE(i.next(), 3.0);
296  QVERIFY(!i.hasNext());
297  i.toFront();
298  QCOMPARE(i.next(), 1.0);
299 }//t_coord_iterator
300 
303 {
304  // Same tests as CoordinatesIterator
305  Coordinates c;
306  c << 1.0 << 3.0;
309  QVERIFY(i.findNext(1.0));
310  QVERIFY(!i.findNext(2.0));
311  QVERIFY(!i.findNext(3.0));
312  QVERIFY(i.findPrevious(3.0));
313  QVERIFY(!i.findPrevious(2.0));
314  QVERIFY(!i.findPrevious(1.0));
315  QVERIFY(i2.findNext(3.0));
316  QVERIFY(i2.findPrevious(3.0));
317  QVERIFY(i2.findNext(3.0));
318  QVERIFY(i2.findPrevious(1.0));
319  QVERIFY(i2.hasNext());
320  QVERIFY(!i2.hasPrevious());
321  QVERIFY(i.hasNext());
322  QVERIFY(!i.hasPrevious());
323  i.toBack();
324  i2.toFront();
325  QVERIFY(!i.hasNext());
326  QVERIFY(i.hasPrevious());
327  QVERIFY(i2.hasNext());
328  QVERIFY(!i2.hasPrevious());
329  Coordinates c2;
330  i2= c2;
331  QVERIFY(!i2.hasNext());
332  QVERIFY(!i2.hasPrevious());
333  i2.toBack();
334  QVERIFY(!i2.hasNext());
335  QVERIFY(!i2.hasPrevious());
336  QCOMPARE(i.peekPrevious(), 3.0);
337  QCOMPARE(i.peekPrevious(), 3.0);
338  QCOMPARE(i.previous(), 3.0);
339  QCOMPARE(i.previous(), 1.0);
340  QVERIFY(!i.hasPrevious());
341  QCOMPARE(i.peekNext(), 1.0);
342  QCOMPARE(i.next(), 1.0);
343  QCOMPARE(i.peekNext(), 3.0);
344  QCOMPARE(i.next(), 3.0);
345  QVERIFY(!i.hasNext());
346  i.toFront();
347  QCOMPARE(i.next(), 1.0);
348 
349  // Mutable tests
350  i.toFront();
351  i.peekNext()= -1.0;
352  QCOMPARE(i.peekNext(), -1.0);
353  QCOMPARE((i.next()= 1.0), 1.0);
354  QCOMPARE(i.peekPrevious(), 1.0);
355  i.remove();
356  QCOMPARE(c.count(), 1);
357  i.remove();
358  QCOMPARE(c.count(), 1);
359  QCOMPARE(i.peekNext(), 3.0);
360  i.insert(1.0);
361  i.insert(2.0);
362  QCOMPARE(c.count(), 3);
363  QCOMPARE(i.peekNext(), 3.0);
364  QCOMPARE(i.peekPrevious(), 2.0);
365  i.peekPrevious()= -2.0;
366  QCOMPARE(i.peekPrevious(), -2.0);
367  QCOMPARE((i.previous()= 2.0), 2.0);
368  QCOMPARE(i.peekNext(), 2.0);
369  i.toBack();
370  i.remove();
371  QCOMPARE(c.count(), 3); // unchanged
372  i.toFront();
373  i.remove();
374  QCOMPARE(c.count(), 3); // unchanged
375  QCOMPARE(i.peekNext(), 1.0);
376  i.remove();
377  QCOMPARE(c.count(), 3); // unchanged
378  i.insert(0.0);
379  QCOMPARE(c.count(), 4);
380  QCOMPARE(i.value(), 0.0);
381  QCOMPARE(i.peekPrevious(), 0.0);
382  i.setValue(-10.0);
383  QCOMPARE(c.count(), 4); // unchanged
384  QCOMPARE(i.peekNext(), 1.0);
385  QCOMPARE(i.peekPrevious(), -10.0);
386  i.findNext(1.0);
387  i.setValue(-1.0);
388  QCOMPARE(i.peekPrevious(), -1.0);
389  i.setValue(1.0);
390  QCOMPARE(i.peekPrevious(), 1.0);
391  QCOMPARE(i.value(), 1.0);
392  i.findPrevious(1.0);
393  i.setValue(-1.0);
394  QCOMPARE(i.peekNext(), -1.0);
395  i.toBack();
396  QCOMPARE(i.previous(), 3.0);
397  i.setValue(-3.0);
398  QCOMPARE(i.peekNext(), -3.0);
399  double d= i.value();
400  QCOMPARE(d, -3.0);
401  QCOMPARE(i.previous(), 2.0);
402 }//t_mutable_coord_iterator
403 
406 {
407  Coordinates c;
408  c.clear();
409  QCOMPARE(c.count(), 0);
410  c << 1.0 << 3.0;
411  c.clear();
412  QCOMPARE(c.count(), 0);
413  coordT c2[4]= { 0.0, 1.0, 2.0, 3.0};
414  c.append(4, c2);
415  QCOMPARE(c.count(), 4);
416  QCOMPARE(c[0], 0.0);
417  QCOMPARE(c[1], 1.0);
418  QCOMPARE(c[3], 3.0);
419  c.clear();
420  c << 1.0 << 3.0;
421  c.erase(c.begin(), c.end());
422  QCOMPARE(c.count(), 0);
423  c << 1.0 << 0.0;
425  QCOMPARE(*i, 0.0);
426  i= c.insert(c.end(), 1.0);
427  QCOMPARE(*i, 1.0);
428  QCOMPARE(c.count(), 2);
429  c.pop_back();
430  QCOMPARE(c.count(), 1); // 0
431  QCOMPARE(c[0], 0.0);
432  c.push_back(2.0);
433  QCOMPARE(c.count(), 2);
434  c.append(3.0);
435  QCOMPARE(c.count(), 3); // 0, 2, 3
436  QCOMPARE(c[2], 3.0);
437  c.insert(0, 4.0);
438  QCOMPARE(c[0], 4.0);
439  QCOMPARE(c[3], 3.0);
440  c.insert(c.count(), 5.0);
441  QCOMPARE(c.count(), 5); // 4, 0, 2, 3, 5
442  QCOMPARE(c[4], 5.0);
443  c.move(4, 0);
444  QCOMPARE(c.count(), 5); // 5, 4, 0, 2, 3
445  QCOMPARE(c[0], 5.0);
446  c.pop_front();
447  QCOMPARE(c.count(), 4);
448  QCOMPARE(c[0], 4.0);
449  c.prepend(6.0);
450  QCOMPARE(c.count(), 5); // 6, 4, 0, 2, 3
451  QCOMPARE(c[0], 6.0);
452  c.push_front(7.0);
453  QCOMPARE(c.count(), 6);
454  QCOMPARE(c[0], 7.0);
455  c.removeAt(1);
456  QCOMPARE(c.count(), 5); // 7, 4, 0, 2, 3
457  QCOMPARE(c[1], 4.0);
458  c.removeFirst();
459  QCOMPARE(c.count(), 4); // 4, 0, 2, 3
460  QCOMPARE(c[0], 4.0);
461  c.removeLast();
462  QCOMPARE(c.count(), 3);
463  QCOMPARE(c.last(), 2.0);
464  c.replace(2, 8.0);
465  QCOMPARE(c.count(), 3); // 4, 0, 8
466  QCOMPARE(c[2], 8.0);
467  c.swap(0, 2);
468  QCOMPARE(c[2], 4.0);
469  double d= c.takeAt(2);
470  QCOMPARE(c.count(), 2); // 8, 0
471  QCOMPARE(d, 4.0);
472  double d2= c.takeFirst();
473  QCOMPARE(c.count(), 1); // 0
474  QCOMPARE(d2, 8.0);
475  double d3= c.takeLast();
476  QVERIFY(c.isEmpty()); \
477  QCOMPARE(d3, 0.0);
478 }//t_readwrite
479 
482 {
483  Coordinates c;
484  c << 1.0 << 3.0 << 1.0;
485  QVERIFY(c.contains(1.0));
486  QVERIFY(c.contains(3.0));
487  QVERIFY(!c.contains(0.0));
488  QCOMPARE(c.count(1.0), 2);
489  QCOMPARE(c.count(3.0), 1);
490  QCOMPARE(c.count(0.0), 0);
491  QCOMPARE(c.indexOf(1.0), 0);
492  QCOMPARE(c.indexOf(3.0), 1);
493  QCOMPARE(c.indexOf(1.0, -1), 2);
494  QCOMPARE(c.indexOf(3.0, -1), -1);
495  QCOMPARE(c.indexOf(3.0, -2), 1);
496  QCOMPARE(c.indexOf(1.0, -3), 0);
497  QCOMPARE(c.indexOf(1.0, -4), 0);
498  QCOMPARE(c.indexOf(1.0, 1), 2);
499  QCOMPARE(c.indexOf(3.0, 2), -1);
500  QCOMPARE(c.indexOf(1.0, 2), 2);
501  QCOMPARE(c.indexOf(1.0, 3), -1);
502  QCOMPARE(c.indexOf(1.0, 4), -1);
503  QCOMPARE(c.lastIndexOf(1.0), 2);
504  QCOMPARE(c.lastIndexOf(3.0), 1);
505  QCOMPARE(c.lastIndexOf(1.0, -1), 2);
506  QCOMPARE(c.lastIndexOf(3.0, -1), 1);
507  QCOMPARE(c.lastIndexOf(3.0, -2), 1);
508  QCOMPARE(c.lastIndexOf(1.0, -3), 0);
509  QCOMPARE(c.lastIndexOf(1.0, -4), -1);
510  QCOMPARE(c.lastIndexOf(1.0, 1), 0);
511  QCOMPARE(c.lastIndexOf(3.0, 2), 1);
512  QCOMPARE(c.lastIndexOf(1.0, 2), 2);
513  QCOMPARE(c.lastIndexOf(1.0, 3), 2);
514  QCOMPARE(c.lastIndexOf(1.0, 4), 2);
515  c.removeAll(3.0);
516  QCOMPARE(c.count(), 2);
517  c.removeAll(4.0);
518  QCOMPARE(c.count(), 2);
519  c.removeAll(1.0);
520  QCOMPARE(c.count(), 0);
521  c.removeAll(4.0);
522  QCOMPARE(c.count(), 0);
523 }//t_search
524 
527 {
528  Coordinates c;
529  c << 1.0 << 2.0 << 3.0;
530  ostringstream os;
531  os << "Coordinates 1-2-3\n" << c;
532  cout << os.str();
533  QString s= QString::fromStdString(os.str());
534  QCOMPARE(s.count("2"), 2);
535 }//t_io
536 
537 }//orgQhull
538 
539 #include "moc/Coordinates_test.moc"
iterator erase(iterator idx)
Definition: Coordinates.h:111
QhullRidge – Qhull&#39;s ridge structure, ridgeT, as a C++ class.
Definition: Coordinates.cpp:21
bool isEmpty() const
Definition: Coordinates.h:75
void push_front(const coordT &c)
Definition: Coordinates.h:124
bool findNext(const coordT &t)
Definition: Coordinates.h:281
void replace(countT idx, const coordT &c)
Definition: Coordinates.h:129
coordT & at(countT idx)
Definition: Coordinates.h:81
void setValue(const coordT &t) const
Definition: Coordinates.h:291
const coordT & peekNext() const
Definition: Coordinates.h:257
countT count() const
Definition: Coordinates.h:72
countT lastIndexOf(const coordT &t, countT from=-1) const
c
const_iterator constBegin() const
Definition: Coordinates.h:99
#define coordT
Definition: libqhull.h:80
bool findNext(const coordT &t)
Definition: Coordinates.h:251
void removeAt(countT idx)
Definition: Coordinates.h:126
void insert(countT before, const coordT &c)
Definition: Coordinates.h:113
coordT value(countT idx, const coordT &defaultValue) const
Definition: Coordinates.cpp:45
void swap(countT idx, countT other)
size_t size() const
Definition: Coordinates.h:78
bool findPrevious(const coordT &t)
Definition: Coordinates.h:282
void prepend(const coordT &c)
Definition: Coordinates.h:122
void removeAll(const coordT &t)
const_iterator constEnd() const
Definition: Coordinates.h:100
void push_back(const coordT &c)
Definition: Coordinates.h:123
bool findPrevious(const coordT &t)
Definition: Coordinates.h:252
bool contains(const coordT &t) const
void add_Coordinates_test()
countT indexOf(const coordT &t, countT from=0) const
c2
coordT takeAt(countT idx)
Definition: Coordinates.cpp:86
void move(countT from, countT to)
Definition: Coordinates.h:115
std::vector< coordT > toStdVector() const
Definition: Coordinates.h:65
const coordT & peekPrevious() const
Definition: Coordinates.h:258
void append(int pointDimension, coordT *c)
Definition: Coordinates.cpp:75
Coordinates mid(countT idx, countT length=-1) const
Definition: Coordinates.cpp:31


hpp-fcl
Author(s):
autogenerated on Fri Jun 2 2023 02:39:00