| [45ef76] | 1 | /*
|
|---|
| 2 | * LineUnittest.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: May 27, 2010
|
|---|
| 5 | * Author: crueger
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include "LineUnittest.hpp"
|
|---|
| 9 |
|
|---|
| 10 | #include "vector.hpp"
|
|---|
| 11 | #include "Exceptions/LinearDependenceException.hpp"
|
|---|
| 12 | #include "Exceptions/SkewException.hpp"
|
|---|
| 13 |
|
|---|
| 14 | #include <cppunit/CompilerOutputter.h>
|
|---|
| 15 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
|---|
| 16 | #include <cppunit/ui/text/TestRunner.h>
|
|---|
| 17 |
|
|---|
| 18 | #include <iostream>
|
|---|
| 19 |
|
|---|
| 20 | using namespace std;
|
|---|
| 21 |
|
|---|
| 22 | #ifdef HAVE_TESTRUNNER
|
|---|
| 23 | #include "UnitTestMain.hpp"
|
|---|
| 24 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 25 |
|
|---|
| 26 | CPPUNIT_TEST_SUITE_REGISTRATION( LineUnittest );
|
|---|
| 27 |
|
|---|
| 28 | void LineUnittest::setUp(){
|
|---|
| 29 | // three lines along the axes
|
|---|
| 30 | la1 = new Line(zeroVec,e1);
|
|---|
| 31 | la2 = new Line(zeroVec,e2);
|
|---|
| 32 | la3 = new Line(zeroVec,e3);
|
|---|
| 33 |
|
|---|
| 34 | // the lines along the planes defined by two coordinate axes
|
|---|
| 35 | lp1 = new Line(e1,e1-e2);
|
|---|
| 36 | lp2 = new Line(e2,e2-e3);
|
|---|
| 37 | lp3 = new Line(e3,e3-e1);
|
|---|
| 38 | }
|
|---|
| 39 | void LineUnittest::tearDown(){
|
|---|
| 40 | delete la1;
|
|---|
| 41 | delete la2;
|
|---|
| 42 | delete la3;
|
|---|
| 43 |
|
|---|
| 44 | delete lp1;
|
|---|
| 45 | delete lp2;
|
|---|
| 46 | delete lp3;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | void LineUnittest::constructionErrorTest(){
|
|---|
| 50 | // test some constructions
|
|---|
| 51 |
|
|---|
| 52 | // direction+origin should never fail
|
|---|
| 53 | CPPUNIT_ASSERT_NO_THROW(Line(zeroVec,e1));
|
|---|
| 54 | CPPUNIT_ASSERT_NO_THROW(Line(zeroVec,e2));
|
|---|
| 55 | CPPUNIT_ASSERT_NO_THROW(Line(zeroVec,e3));
|
|---|
| 56 |
|
|---|
| 57 | // two points fails if both points are the same
|
|---|
| 58 | CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e1,e2));
|
|---|
| 59 | CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e2,e3));
|
|---|
| 60 | CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e3,e1));
|
|---|
| 61 | // for zerovectors
|
|---|
| 62 | CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e1,zeroVec));
|
|---|
| 63 | CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e2,zeroVec));
|
|---|
| 64 | CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e3,zeroVec));
|
|---|
| 65 | // now we pass two times the same point
|
|---|
| 66 | CPPUNIT_ASSERT_THROW(makeLineThrough(zeroVec,zeroVec),LinearDependenceException);
|
|---|
| 67 | CPPUNIT_ASSERT_THROW(makeLineThrough(e1,e1),LinearDependenceException);
|
|---|
| 68 | CPPUNIT_ASSERT_THROW(makeLineThrough(e2,e2),LinearDependenceException);
|
|---|
| 69 | CPPUNIT_ASSERT_THROW(makeLineThrough(e3,e3),LinearDependenceException);
|
|---|
| 70 |
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | bool testDirection(const Vector &dir1,const Vector &dir2){
|
|---|
| 74 | return (dir1==dir2) || (dir1==-1*dir2);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void LineUnittest::constructionResultTest(){
|
|---|
| 78 | // test all directions
|
|---|
| 79 | CPPUNIT_ASSERT(testDirection(la1->getDirection(),e1));
|
|---|
| 80 | CPPUNIT_ASSERT(testDirection(la2->getDirection(),e2));
|
|---|
| 81 | CPPUNIT_ASSERT(testDirection(la3->getDirection(),e3));
|
|---|
| 82 |
|
|---|
| 83 | // test origins
|
|---|
| 84 | CPPUNIT_ASSERT_EQUAL(la1->getOrigin(),zeroVec);
|
|---|
| 85 | CPPUNIT_ASSERT_EQUAL(la2->getOrigin(),zeroVec);
|
|---|
| 86 | CPPUNIT_ASSERT_EQUAL(la2->getOrigin(),zeroVec);
|
|---|
| 87 |
|
|---|
| 88 | // test if desired points are on the lines
|
|---|
| 89 | CPPUNIT_ASSERT(la1->isContained(zeroVec));
|
|---|
| 90 | CPPUNIT_ASSERT(la2->isContained(zeroVec));
|
|---|
| 91 | CPPUNIT_ASSERT(la3->isContained(zeroVec));
|
|---|
| 92 |
|
|---|
| 93 | CPPUNIT_ASSERT(la1->isContained(e1));
|
|---|
| 94 | CPPUNIT_ASSERT(la2->isContained(e2));
|
|---|
| 95 | CPPUNIT_ASSERT(la3->isContained(e3));
|
|---|
| 96 |
|
|---|
| 97 | CPPUNIT_ASSERT(lp1->isContained(e1));
|
|---|
| 98 | CPPUNIT_ASSERT(lp2->isContained(e2));
|
|---|
| 99 | CPPUNIT_ASSERT(lp3->isContained(e3));
|
|---|
| 100 |
|
|---|
| 101 | CPPUNIT_ASSERT(lp1->isContained(e2));
|
|---|
| 102 | CPPUNIT_ASSERT(lp2->isContained(e3));
|
|---|
| 103 | CPPUNIT_ASSERT(lp3->isContained(e1));
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void LineUnittest::isContainedTest(){
|
|---|
| 107 | // Zerovector on the axes lines
|
|---|
| 108 | CPPUNIT_ASSERT(la1->isContained(zeroVec));
|
|---|
| 109 | CPPUNIT_ASSERT(la2->isContained(zeroVec));
|
|---|
| 110 | CPPUNIT_ASSERT(la3->isContained(zeroVec));
|
|---|
| 111 |
|
|---|
| 112 | // multiples of the second support vector
|
|---|
| 113 | CPPUNIT_ASSERT(la1->isContained(e1));
|
|---|
| 114 | CPPUNIT_ASSERT(la2->isContained(e2));
|
|---|
| 115 | CPPUNIT_ASSERT(la3->isContained(e3));
|
|---|
| 116 |
|
|---|
| 117 | CPPUNIT_ASSERT(la1->isContained(2*e1));
|
|---|
| 118 | CPPUNIT_ASSERT(la2->isContained(2*e2));
|
|---|
| 119 | CPPUNIT_ASSERT(la3->isContained(2*e3));
|
|---|
| 120 |
|
|---|
| 121 | CPPUNIT_ASSERT(la1->isContained(3*e1));
|
|---|
| 122 | CPPUNIT_ASSERT(la2->isContained(3*e2));
|
|---|
| 123 | CPPUNIT_ASSERT(la3->isContained(3*e3));
|
|---|
| 124 |
|
|---|
| 125 | // negative multiples
|
|---|
| 126 | CPPUNIT_ASSERT(la1->isContained(-1*e1));
|
|---|
| 127 | CPPUNIT_ASSERT(la2->isContained(-1*e2));
|
|---|
| 128 | CPPUNIT_ASSERT(la3->isContained(-1*e3));
|
|---|
| 129 |
|
|---|
| 130 | CPPUNIT_ASSERT(la1->isContained(-2*e1));
|
|---|
| 131 | CPPUNIT_ASSERT(la2->isContained(-2*e2));
|
|---|
| 132 | CPPUNIT_ASSERT(la3->isContained(-2*e3));
|
|---|
| 133 |
|
|---|
| 134 | // points that should not be on the lines
|
|---|
| 135 | CPPUNIT_ASSERT(!la1->isContained(e2));
|
|---|
| 136 | CPPUNIT_ASSERT(!la2->isContained(e3));
|
|---|
| 137 | CPPUNIT_ASSERT(!la3->isContained(e1));
|
|---|
| 138 |
|
|---|
| 139 | CPPUNIT_ASSERT(!la1->isContained(2*e2));
|
|---|
| 140 | CPPUNIT_ASSERT(!la2->isContained(2*e3));
|
|---|
| 141 | CPPUNIT_ASSERT(!la3->isContained(2*e1));
|
|---|
| 142 |
|
|---|
| 143 | CPPUNIT_ASSERT(!la1->isContained(-1*e2));
|
|---|
| 144 | CPPUNIT_ASSERT(!la2->isContained(-1*e3));
|
|---|
| 145 | CPPUNIT_ASSERT(!la3->isContained(-1*e1));
|
|---|
| 146 |
|
|---|
| 147 | // For the plane lines
|
|---|
| 148 | CPPUNIT_ASSERT(lp1->isContained(e1));
|
|---|
| 149 | CPPUNIT_ASSERT(lp2->isContained(e2));
|
|---|
| 150 | CPPUNIT_ASSERT(lp3->isContained(e3));
|
|---|
| 151 |
|
|---|
| 152 | CPPUNIT_ASSERT(lp1->isContained(e2));
|
|---|
| 153 | CPPUNIT_ASSERT(lp2->isContained(e3));
|
|---|
| 154 | CPPUNIT_ASSERT(lp3->isContained(e1));
|
|---|
| 155 |
|
|---|
| 156 | CPPUNIT_ASSERT(lp1->isContained(e1+2*(e1-e2)));
|
|---|
| 157 | CPPUNIT_ASSERT(lp2->isContained(e2+2*(e2-e3)));
|
|---|
| 158 | CPPUNIT_ASSERT(lp3->isContained(e3+2*(e3-e1)));
|
|---|
| 159 |
|
|---|
| 160 | CPPUNIT_ASSERT(lp1->isContained(e1-2*(e1-e2)));
|
|---|
| 161 | CPPUNIT_ASSERT(lp2->isContained(e2-2*(e2-e3)));
|
|---|
| 162 | CPPUNIT_ASSERT(lp3->isContained(e3-2*(e3-e1)));
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | void LineUnittest::intersectionTest(){
|
|---|
| 166 | Vector fixture;
|
|---|
| 167 |
|
|---|
| 168 | // intersection of the axis lines
|
|---|
| 169 | fixture = la1->getIntersection(*la2);
|
|---|
| 170 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
|
|---|
| 171 | fixture = la2->getIntersection(*la3);
|
|---|
| 172 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
|
|---|
| 173 | fixture = la3->getIntersection(*la1);
|
|---|
| 174 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
|
|---|
| 175 |
|
|---|
| 176 | // axes and plane lines
|
|---|
| 177 | fixture = la1->getIntersection(*lp1);
|
|---|
| 178 | CPPUNIT_ASSERT_EQUAL(fixture,e1);
|
|---|
| 179 | fixture = la2->getIntersection(*lp2);
|
|---|
| 180 | CPPUNIT_ASSERT_EQUAL(fixture,e2);
|
|---|
| 181 | fixture = la3->getIntersection(*lp3);
|
|---|
| 182 | CPPUNIT_ASSERT_EQUAL(fixture,e3);
|
|---|
| 183 |
|
|---|
| 184 | fixture = la1->getIntersection(*lp3);
|
|---|
| 185 | CPPUNIT_ASSERT_EQUAL(fixture,e1);
|
|---|
| 186 | fixture = la2->getIntersection(*lp1);
|
|---|
| 187 | CPPUNIT_ASSERT_EQUAL(fixture,e2);
|
|---|
| 188 | fixture = la3->getIntersection(*lp2);
|
|---|
| 189 | CPPUNIT_ASSERT_EQUAL(fixture,e3);
|
|---|
| 190 |
|
|---|
| 191 | // two plane lines
|
|---|
| 192 | fixture = lp1->getIntersection(*lp2);
|
|---|
| 193 | CPPUNIT_ASSERT_EQUAL(fixture,e2);
|
|---|
| 194 | fixture = lp2->getIntersection(*lp3);
|
|---|
| 195 | CPPUNIT_ASSERT_EQUAL(fixture,e3);
|
|---|
| 196 | fixture = lp3->getIntersection(*lp1);
|
|---|
| 197 | CPPUNIT_ASSERT_EQUAL(fixture,e1);
|
|---|
| 198 |
|
|---|
| 199 | // When we have two times the same line, we check if the point is on the line
|
|---|
| 200 | fixture = la1->getIntersection(*la1);
|
|---|
| 201 | CPPUNIT_ASSERT(la1->isContained(fixture));
|
|---|
| 202 | fixture = la2->getIntersection(*la2);
|
|---|
| 203 | CPPUNIT_ASSERT(la2->isContained(fixture));
|
|---|
| 204 | fixture = la3->getIntersection(*la3);
|
|---|
| 205 | CPPUNIT_ASSERT(la3->isContained(fixture));
|
|---|
| 206 |
|
|---|
| 207 | fixture = lp1->getIntersection(*lp1);
|
|---|
| 208 | CPPUNIT_ASSERT(lp1->isContained(fixture));
|
|---|
| 209 | fixture = lp2->getIntersection(*lp2);
|
|---|
| 210 | CPPUNIT_ASSERT(lp2->isContained(fixture));
|
|---|
| 211 | fixture = lp3->getIntersection(*lp3);
|
|---|
| 212 | CPPUNIT_ASSERT(lp3->isContained(fixture));
|
|---|
| 213 |
|
|---|
| 214 | // lines that are askew should produce an Error
|
|---|
| 215 | CPPUNIT_ASSERT_THROW(lp1->getIntersection(*la3),SkewException);
|
|---|
| 216 | CPPUNIT_ASSERT_THROW(lp2->getIntersection(*la1),SkewException);
|
|---|
| 217 | CPPUNIT_ASSERT_THROW(lp3->getIntersection(*la2),SkewException);
|
|---|
| 218 |
|
|---|
| 219 | CPPUNIT_ASSERT_THROW(la1->getIntersection(*lp2),SkewException);
|
|---|
| 220 | CPPUNIT_ASSERT_THROW(la2->getIntersection(*lp3),SkewException);
|
|---|
| 221 | CPPUNIT_ASSERT_THROW(la3->getIntersection(*lp1),SkewException);
|
|---|
| 222 | }
|
|---|