| 1 | #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 | zero[0] = 0.0; | 
| 9 | zero[1] = 0.0; | 
| 10 | zero[2] = 0.0; | 
| 11 | zero[3] = 0.0; | 
| 12 |  | 
| 13 | 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 | } | 
| 43 |  | 
| 44 | void VectorTestCase::testConstructors(){ | 
| 45 | 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 | } | 
| 75 |  | 
| 76 | void VectorTestCase::testArithmetic(){ | 
| 77 | //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 | } | 
| 135 |  | 
| 136 | void VectorTestCase::testOperators(){ | 
| 137 | //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 | a6 *= 2.0; | 
| 171 | CPPUNIT_ASSERT( a6 == two); | 
| 172 |  | 
| 173 | Vec4 a7; | 
| 174 | a7 =  one * 2.0; | 
| 175 | CPPUNIT_ASSERT( a7 == two); | 
| 176 | a7 =  2.0 * one; | 
| 177 | CPPUNIT_ASSERT( a7 == two); | 
| 178 |  | 
| 179 | //test div | 
| 180 | 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 | } | 
| 187 |  | 
| 188 | void VectorTestCase::testAccessEntries(){ | 
| 189 | //test [] operator | 
| 190 |  | 
| 191 | 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 | //test () operator | 
| 195 | CPPUNIT_ASSERT_DOUBLES_EQUAL(v3(0) , 4.0, oopse::epsilon); | 
| 196 |  | 
| 197 | } | 
| 198 |  | 
| 199 | void VectorTestCase::testOtherMemberFunctions(){ | 
| 200 | //test length() | 
| 201 | CPPUNIT_ASSERT_DOUBLES_EQUAL(zero.length(), 0.0, oopse::epsilon); | 
| 202 | CPPUNIT_ASSERT_DOUBLES_EQUAL(one.length(), 2.0, oopse::epsilon); | 
| 203 | CPPUNIT_ASSERT_DOUBLES_EQUAL(v2.length(), sqrt(14.0), oopse::epsilon); | 
| 204 |  | 
| 205 | //test lengthSquare() | 
| 206 | CPPUNIT_ASSERT_DOUBLES_EQUAL(zero.lengthSquare(), 0.0, oopse::epsilon); | 
| 207 | CPPUNIT_ASSERT_DOUBLES_EQUAL(one.lengthSquare(), 4.0, oopse::epsilon); | 
| 208 | CPPUNIT_ASSERT_DOUBLES_EQUAL(v2.lengthSquare(), 14.0, oopse::epsilon); | 
| 209 |  | 
| 210 | //test normalize() | 
| 211 | Vec4 a1 = one; | 
| 212 | Vec4 a2 = two; | 
| 213 |  | 
| 214 | a1.normalize(); | 
| 215 | a2.normalize(); | 
| 216 | CPPUNIT_ASSERT(a1 == a2); | 
| 217 |  | 
| 218 | //test isNormalized(); | 
| 219 | CPPUNIT_ASSERT(a1.isNormalized()); | 
| 220 | CPPUNIT_ASSERT(!one.isNormalized()); | 
| 221 |  | 
| 222 |  | 
| 223 | } | 
| 224 | void VectorTestCase::testOtherTemplateFunctions(){ | 
| 225 | //test dot | 
| 226 | CPPUNIT_ASSERT_DOUBLES_EQUAL(dot(one, two), 8.0, oopse::epsilon); | 
| 227 | CPPUNIT_ASSERT_DOUBLES_EQUAL(dot(v1, v3), 20.0, oopse::epsilon); | 
| 228 |  | 
| 229 | //test distance | 
| 230 | CPPUNIT_ASSERT_DOUBLES_EQUAL(distance(one, two), 2.0, oopse::epsilon); | 
| 231 | CPPUNIT_ASSERT_DOUBLES_EQUAL(distance(v1, v2), sqrt(56.0), oopse::epsilon); | 
| 232 |  | 
| 233 | //test distanceSquare | 
| 234 | CPPUNIT_ASSERT_DOUBLES_EQUAL(distanceSquare(one, two), 4.0, oopse::epsilon); | 
| 235 | CPPUNIT_ASSERT_DOUBLES_EQUAL(distanceSquare(v1, v2), 56, oopse::epsilon); | 
| 236 |  | 
| 237 | } |