| 1 | tim | 1593 | #include "math/VectorTestCase.hpp" | 
| 2 |  |  |  | 
| 3 |  |  | // Registers the fixture into the 'registry' | 
| 4 |  |  | CPPUNIT_TEST_SUITE_REGISTRATION( VectorTestCase ); | 
| 5 |  |  |  | 
| 6 |  |  |  | 
| 7 |  |  | void VectorTestCase::setUp(){ | 
| 8 | tim | 1594 | zero[0] = 0.0; | 
| 9 |  |  | zero[1] = 0.0; | 
| 10 |  |  | zero[2] = 0.0; | 
| 11 |  |  | zero[3] = 0.0; | 
| 12 | tim | 1593 |  | 
| 13 | tim | 1594 | one[0] = 1.0; | 
| 14 |  |  | one[1] = 1.0; | 
| 15 |  |  | one[2] = 1.0; | 
| 16 |  |  | one[3] = 1.0; | 
| 17 |  |  |  | 
| 18 |  |  | two[0] = 2.0; | 
| 19 |  |  | two[1] = 2.0; | 
| 20 |  |  | two[2] = 2.0; | 
| 21 |  |  | two[3] = 2.0; | 
| 22 |  |  |  | 
| 23 |  |  |  | 
| 24 |  |  | v1[0] = 3.0; | 
| 25 |  |  | v1[1] = 0.0; | 
| 26 |  |  | v1[2] = 2.0; | 
| 27 |  |  | v1[3] = 1.0; | 
| 28 |  |  |  | 
| 29 |  |  | v2[0] = -3.0; | 
| 30 |  |  | v2[1] = 0.0; | 
| 31 |  |  | v2[2] = -2.0; | 
| 32 |  |  | v2[3] = -1.0; | 
| 33 |  |  |  | 
| 34 |  |  | v3[0] = 4.0; | 
| 35 |  |  | v3[1] = 1.0; | 
| 36 |  |  | v3[2] = 3.0; | 
| 37 |  |  | v3[3] = 2.0; | 
| 38 |  |  |  | 
| 39 |  |  | s1 = 1.0; | 
| 40 |  |  | s2 = 2.0; | 
| 41 |  |  |  | 
| 42 | tim | 1593 | } | 
| 43 |  |  |  | 
| 44 |  |  | void VectorTestCase::testConstructors(){ | 
| 45 | tim | 1594 | Vec4 a0; | 
| 46 |  |  | Vec4 a1(1); | 
| 47 |  |  | Vec4 a2(2); | 
| 48 |  |  |  | 
| 49 |  |  | CPPUNIT_ASSERT( a0 == zero); | 
| 50 |  |  | CPPUNIT_ASSERT( a1 == one); | 
| 51 |  |  | CPPUNIT_ASSERT( a2 == two); | 
| 52 |  |  |  | 
| 53 |  |  | CPPUNIT_ASSERT( a1 != two); | 
| 54 |  |  |  | 
| 55 |  |  | //test copy constructor | 
| 56 |  |  | Vec4 b1(v1); | 
| 57 |  |  | CPPUNIT_ASSERT( b1 == v1); | 
| 58 |  |  |  | 
| 59 |  |  | //test operator = | 
| 60 |  |  | b1 = v2; | 
| 61 |  |  | CPPUNIT_ASSERT( b1 == v2); | 
| 62 |  |  |  | 
| 63 |  |  | //test constructor from an array | 
| 64 |  |  | double tempArray[] = {1.0, 2.0, 5.0, 8.0}; | 
| 65 |  |  | Vec4 tempV; | 
| 66 |  |  | tempV[0] = 1.0; | 
| 67 |  |  | tempV[1] = 2.0; | 
| 68 |  |  | tempV[2] = 5.0; | 
| 69 |  |  | tempV[3] = 8.0; | 
| 70 |  |  |  | 
| 71 |  |  | Vec4 b2(tempV); | 
| 72 |  |  | CPPUNIT_ASSERT( b2 == tempArray); | 
| 73 |  |  |  | 
| 74 | tim | 1593 | } | 
| 75 |  |  |  | 
| 76 |  |  | void VectorTestCase::testArithmetic(){ | 
| 77 | tim | 1594 | //test negate | 
| 78 |  |  | Vec4 a0 = v2; | 
| 79 |  |  | a0.negate(); | 
| 80 |  |  | CPPUNIT_ASSERT (a0 == v1); | 
| 81 |  |  |  | 
| 82 |  |  | Vec4 a1; | 
| 83 |  |  | a1.negate(v2); | 
| 84 |  |  | CPPUNIT_ASSERT(a1 == v1); | 
| 85 |  |  |  | 
| 86 |  |  | //test add | 
| 87 |  |  | Vec4 a2; | 
| 88 |  |  | a2 = v1; | 
| 89 |  |  | a2.add(v2); | 
| 90 |  |  | CPPUNIT_ASSERT( a2 == zero); | 
| 91 |  |  |  | 
| 92 |  |  | Vec4 a3; | 
| 93 |  |  | a3.add(v2, v3); | 
| 94 |  |  | CPPUNIT_ASSERT( a3 == one); | 
| 95 |  |  |  | 
| 96 |  |  |  | 
| 97 |  |  | //test sub | 
| 98 |  |  | Vec4 a4; | 
| 99 |  |  | a4 = two; | 
| 100 |  |  | a4.sub(one); | 
| 101 |  |  | CPPUNIT_ASSERT( a4 == one); | 
| 102 |  |  |  | 
| 103 |  |  | Vec4 a5; | 
| 104 |  |  | a5.sub(two, one); | 
| 105 |  |  | CPPUNIT_ASSERT( a5 == one); | 
| 106 |  |  |  | 
| 107 |  |  | //test mul | 
| 108 |  |  | Vec4 a6; | 
| 109 |  |  | a6 = one; | 
| 110 |  |  | a6.mul(2.0); | 
| 111 |  |  | CPPUNIT_ASSERT( a6 == two); | 
| 112 |  |  |  | 
| 113 |  |  | Vec4 a7; | 
| 114 |  |  | a7.mul(one, 2.0); | 
| 115 |  |  | CPPUNIT_ASSERT( a7 == two); | 
| 116 |  |  | Vec4 a8; | 
| 117 |  |  | a7.mul(zero, 2.0); | 
| 118 |  |  | CPPUNIT_ASSERT( a7 == zero); | 
| 119 |  |  |  | 
| 120 |  |  | //test div | 
| 121 |  |  | Vec4 a9; | 
| 122 |  |  | a9 = two; | 
| 123 |  |  | a9.div(2.0); | 
| 124 |  |  | CPPUNIT_ASSERT( a9 == one); | 
| 125 |  |  |  | 
| 126 |  |  | Vec4 a10; | 
| 127 |  |  | a10.div(two, 2.0); | 
| 128 |  |  | CPPUNIT_ASSERT( a10 == one); | 
| 129 |  |  | Vec4 a11; | 
| 130 |  |  | a11.mul(zero, 2.0); | 
| 131 |  |  | CPPUNIT_ASSERT( a11 == zero); | 
| 132 |  |  |  | 
| 133 |  |  |  | 
| 134 | tim | 1593 | } | 
| 135 |  |  |  | 
| 136 |  |  | void VectorTestCase::testOperators(){ | 
| 137 | tim | 1594 | //test unary minus | 
| 138 |  |  | Vec4 a0 = v2; | 
| 139 |  |  | a0 = - a0; | 
| 140 |  |  | CPPUNIT_ASSERT (a0 == v1); | 
| 141 |  |  |  | 
| 142 |  |  |  | 
| 143 |  |  | //test add | 
| 144 |  |  | Vec4 a1; | 
| 145 |  |  | a1 = v1; | 
| 146 |  |  | a1 += v2; | 
| 147 |  |  | CPPUNIT_ASSERT( a1 == zero); | 
| 148 |  |  |  | 
| 149 |  |  | Vec4 a2; | 
| 150 |  |  | a2 = v2 + v3; | 
| 151 |  |  | CPPUNIT_ASSERT( a2 == one); | 
| 152 |  |  |  | 
| 153 |  |  | //test sub | 
| 154 |  |  | Vec4 a3; | 
| 155 |  |  | a3 = two; | 
| 156 |  |  | a3 -= one; | 
| 157 |  |  | CPPUNIT_ASSERT( a3 == one); | 
| 158 |  |  |  | 
| 159 |  |  | Vec4 a4; | 
| 160 |  |  | a4 = two - one; | 
| 161 |  |  | CPPUNIT_ASSERT( a4 == one); | 
| 162 |  |  |  | 
| 163 |  |  | Vec4 a5; | 
| 164 |  |  | a5.sub(two, one); | 
| 165 |  |  | CPPUNIT_ASSERT( a5 == one); | 
| 166 |  |  |  | 
| 167 |  |  | //test mul | 
| 168 |  |  | Vec4 a6; | 
| 169 |  |  | a6 = one; | 
| 170 | tim | 1595 | a6 *= 2.0; | 
| 171 | tim | 1594 | CPPUNIT_ASSERT( a6 == two); | 
| 172 |  |  |  | 
| 173 |  |  | Vec4 a7; | 
| 174 | tim | 1595 | a7 =  one * 2.0; | 
| 175 | tim | 1594 | CPPUNIT_ASSERT( a7 == two); | 
| 176 | tim | 1595 | a7 =  2.0 * one; | 
| 177 |  |  | CPPUNIT_ASSERT( a7 == two); | 
| 178 | tim | 1594 |  | 
| 179 |  |  | //test div | 
| 180 | tim | 1595 | Vec4 a8; | 
| 181 |  |  | a8 = two; | 
| 182 |  |  | a8 /= 2.0; | 
| 183 |  |  | CPPUNIT_ASSERT( a8 == one); | 
| 184 |  |  | a8 = two /2.0; | 
| 185 |  |  | CPPUNIT_ASSERT( a8 == one); | 
| 186 | tim | 1593 | } | 
| 187 |  |  |  | 
| 188 |  |  | void VectorTestCase::testAccessEntries(){ | 
| 189 | tim | 1595 | //test [] operator | 
| 190 | tim | 1594 |  | 
| 191 | tim | 1603 | CPPUNIT_ASSERT_DOUBLES_EQUAL(zero[0], 0.0, oopse::epsilon); | 
| 192 |  |  | CPPUNIT_ASSERT_DOUBLES_EQUAL(one[0] , 1.0, oopse::epsilon); | 
| 193 |  |  | CPPUNIT_ASSERT_DOUBLES_EQUAL(v3[0] , 4.0, oopse::epsilon); | 
| 194 | tim | 1595 | //test () operator | 
| 195 | tim | 1603 | CPPUNIT_ASSERT_DOUBLES_EQUAL(v3(0) , 4.0, oopse::epsilon); | 
| 196 | tim | 1594 |  | 
| 197 | tim | 1630 | Vec4 a1; | 
| 198 |  |  | double *pa1 = a1.getArrayPointer(); | 
| 199 |  |  |  | 
| 200 |  |  | pa1[0] = 4.0; | 
| 201 |  |  | pa1[1] = 1.0; | 
| 202 |  |  | pa1[2] = 3.0; | 
| 203 |  |  | pa1[3] = 2.0; | 
| 204 |  |  |  | 
| 205 |  |  | CPPUNIT_ASSERT(a1 == v3); | 
| 206 | tim | 1593 | } | 
| 207 |  |  |  | 
| 208 | tim | 1595 | void VectorTestCase::testOtherMemberFunctions(){ | 
| 209 |  |  | //test length() | 
| 210 | tim | 1603 | CPPUNIT_ASSERT_DOUBLES_EQUAL(zero.length(), 0.0, oopse::epsilon); | 
| 211 |  |  | CPPUNIT_ASSERT_DOUBLES_EQUAL(one.length(), 2.0, oopse::epsilon); | 
| 212 |  |  | CPPUNIT_ASSERT_DOUBLES_EQUAL(v2.length(), sqrt(14.0), oopse::epsilon); | 
| 213 | tim | 1595 |  | 
| 214 |  |  | //test lengthSquare() | 
| 215 | tim | 1603 | CPPUNIT_ASSERT_DOUBLES_EQUAL(zero.lengthSquare(), 0.0, oopse::epsilon); | 
| 216 |  |  | CPPUNIT_ASSERT_DOUBLES_EQUAL(one.lengthSquare(), 4.0, oopse::epsilon); | 
| 217 |  |  | CPPUNIT_ASSERT_DOUBLES_EQUAL(v2.lengthSquare(), 14.0, oopse::epsilon); | 
| 218 | tim | 1595 |  | 
| 219 |  |  | //test normalize() | 
| 220 |  |  | Vec4 a1 = one; | 
| 221 |  |  | Vec4 a2 = two; | 
| 222 |  |  |  | 
| 223 |  |  | a1.normalize(); | 
| 224 |  |  | a2.normalize(); | 
| 225 |  |  | CPPUNIT_ASSERT(a1 == a2); | 
| 226 |  |  |  | 
| 227 |  |  | //test isNormalized(); | 
| 228 |  |  | CPPUNIT_ASSERT(a1.isNormalized()); | 
| 229 |  |  | CPPUNIT_ASSERT(!one.isNormalized()); | 
| 230 |  |  |  | 
| 231 |  |  |  | 
| 232 |  |  | } | 
| 233 | tim | 1593 | void VectorTestCase::testOtherTemplateFunctions(){ | 
| 234 | tim | 1595 | //test dot | 
| 235 | tim | 1603 | CPPUNIT_ASSERT_DOUBLES_EQUAL(dot(one, two), 8.0, oopse::epsilon); | 
| 236 |  |  | CPPUNIT_ASSERT_DOUBLES_EQUAL(dot(v1, v3), 20.0, oopse::epsilon); | 
| 237 | tim | 1593 |  | 
| 238 | tim | 1595 | //test distance | 
| 239 | tim | 1603 | CPPUNIT_ASSERT_DOUBLES_EQUAL(distance(one, two), 2.0, oopse::epsilon); | 
| 240 |  |  | CPPUNIT_ASSERT_DOUBLES_EQUAL(distance(v1, v2), sqrt(56.0), oopse::epsilon); | 
| 241 | tim | 1595 |  | 
| 242 |  |  | //test distanceSquare | 
| 243 | tim | 1603 | CPPUNIT_ASSERT_DOUBLES_EQUAL(distanceSquare(one, two), 4.0, oopse::epsilon); | 
| 244 |  |  | CPPUNIT_ASSERT_DOUBLES_EQUAL(distanceSquare(v1, v2), 56, oopse::epsilon); | 
| 245 | tim | 1595 |  | 
| 246 | tim | 1594 | } |