00001
00034 #include <tut/tut.h>
00035
00036 #include "linden_common.h"
00037 #include "lltut.h"
00038 #include "v2math.h"
00039
00040
00041 namespace tut
00042 {
00043 struct v2math_data
00044 {
00045 };
00046 typedef test_group<v2math_data> v2math_test;
00047 typedef v2math_test::object v2math_object;
00048 tut::v2math_test v2math_testcase("v2math");
00049
00050 template<> template<>
00051 void v2math_object::test<1>()
00052 {
00053 LLVector2 vec2;
00054 ensure("LLVector2:Fail to initialize ", (0.f == vec2.mV[VX] && 0.f == vec2.mV[VY]));
00055
00056 F32 x =2.0f, y = 3.2f ;
00057 LLVector2 vec3(x,y);
00058 ensure("LLVector2(F32 x, F32 y):Fail to initialize ", (x == vec3.mV[VX]) && (y == vec3.mV[VY]));
00059
00060 const F32 vec[2] = {3.2f, 4.5f};
00061 LLVector2 vec4(vec);
00062 ensure("LLVector2(const F32 *vec):Fail to initialize ", (vec[0] == vec4.mV[VX]) && (vec[1] == vec4.mV[VY]));
00063
00064 vec4.clearVec();
00065 ensure("clearVec():Fail to clean the values ", (0.f == vec4.mV[VX] && 0.f == vec4.mV[VY]));
00066
00067 vec3.zeroVec();
00068 ensure("zeroVec():Fail to fill the zero ", (0.f == vec3.mV[VX] && 0.f == vec3.mV[VY]));
00069 }
00070
00071 template<> template<>
00072 void v2math_object::test<2>()
00073 {
00074 F32 x = 123.356f, y = 2387.453f;
00075 LLVector2 vec2,vec3;
00076 vec2.setVec(x, y);
00077 ensure("1:setVec: Fail ", (x == vec2.mV[VX]) && (y == vec2.mV[VY]));
00078
00079 vec3.setVec(vec2);
00080 ensure("2:setVec: Fail " ,(vec2 == vec3));
00081
00082 vec3.zeroVec();
00083 const F32 vec[2] = {3.24653f, 457653.4f};
00084 vec3.setVec(vec);
00085 ensure("3:setVec: Fail ", (vec[0] == vec3.mV[VX]) && (vec[1] == vec3.mV[VY]));
00086 }
00087
00088 template<> template<>
00089 void v2math_object::test<3>()
00090 {
00091 F32 x = 2.2345f, y = 3.5678f ;
00092 LLVector2 vec2(x,y);
00093 ensure("magVecSquared:Fail ", is_approx_equal(vec2.magVecSquared(), (x*x + y*y)));
00094 ensure("magVec:Fail ", is_approx_equal(vec2.magVec(), fsqrtf(x*x + y*y)));
00095 }
00096
00097 template<> template<>
00098 void v2math_object::test<4>()
00099 {
00100 F32 x =-2.0f, y = -3.0f ;
00101 LLVector2 vec2(x,y);
00102 ensure_equals("abs():Fail", vec2.abs(), TRUE);
00103 ensure("abs() x", is_approx_equal(vec2.mV[VX], 2.f));
00104 ensure("abs() y", is_approx_equal(vec2.mV[VY], 3.f));
00105
00106 ensure("isNull():Fail ", FALSE == vec2.isNull());
00107
00108 x =.00000001f, y = .000001001f;
00109 vec2.setVec(x, y);
00110 ensure("isNull(): Fail ", TRUE == vec2.isNull());
00111 }
00112
00113 template<> template<>
00114 void v2math_object::test<5>()
00115 {
00116 F32 x =1.f, y = 2.f;
00117 LLVector2 vec2(x, y), vec3;
00118 vec3 = vec3.scaleVec(vec2);
00119 ensure("scaleVec: Fail ", vec3.mV[VX] == 0. && vec3.mV[VY] == 0.);
00120 ensure("isExactlyZero(): Fail", TRUE == vec3.isExactlyZero());
00121
00122 vec3.setVec(2.f, 1.f);
00123 vec3 = vec3.scaleVec(vec2);
00124 ensure("scaleVec: Fail ", (2.f == vec3.mV[VX]) && (2.f == vec3.mV[VY]));
00125 ensure("isExactlyZero():Fail", FALSE == vec3.isExactlyZero());
00126 }
00127
00128 template<> template<>
00129 void v2math_object::test<6>()
00130 {
00131 F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
00132 F32 val1, val2;
00133 LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
00134 vec4 = vec2 + vec3 ;
00135 val1 = x1+x2;
00136 val2 = y1+y2;
00137 ensure("1:operator+ failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
00138
00139 vec2.clearVec();
00140 vec3.clearVec();
00141 x1 = -.235f, y1 = -24.32f, x2 = -2.3f, y2 = 1.f;
00142 vec2.setVec(x1, y1);
00143 vec3.setVec(x2, y2);
00144 vec4 = vec2 + vec3;
00145 val1 = x1+x2;
00146 val2 = y1+y2;
00147 ensure("2:operator+ failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
00148 }
00149
00150 template<> template<>
00151 void v2math_object::test<7>()
00152 {
00153 F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
00154 F32 val1, val2;
00155 LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
00156 vec4 = vec2 - vec3 ;
00157 val1 = x1-x2;
00158 val2 = y1-y2;
00159 ensure("1:operator- failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
00160
00161 vec2.clearVec();
00162 vec3.clearVec();
00163 vec4.clearVec();
00164 x1 = -.235f, y1 = -24.32f, x2 = -2.3f, y2 = 1.f;
00165 vec2.setVec(x1, y1);
00166 vec3.setVec(x2, y2);
00167 vec4 = vec2 - vec3;
00168 val1 = x1-x2;
00169 val2 = y1-y2;
00170 ensure("2:operator- failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
00171 }
00172
00173 template<> template<>
00174 void v2math_object::test<8>()
00175 {
00176 F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
00177 F32 val1, val2;
00178 LLVector2 vec2(x1, y1), vec3(x2, y2);
00179 val1 = vec2 * vec3;
00180 val2 = x1*x2 + y1*y2;
00181 ensure("1:operator* failed",(val1 == val2));
00182
00183 vec3.clearVec();
00184 F32 mulVal = 4.332f;
00185 vec3 = vec2 * mulVal;
00186 val1 = x1*mulVal;
00187 val2 = y1*mulVal;
00188 ensure("2:operator* failed",(val1 == vec3.mV[VX]) && (val2 == vec3.mV[VY]));
00189
00190 vec3.clearVec();
00191 vec3 = mulVal * vec2;
00192 ensure("3:operator* failed",(val1 == vec3.mV[VX]) && (val2 == vec3.mV[VY]));
00193 }
00194
00195 template<> template<>
00196 void v2math_object::test<9>()
00197 {
00198 F32 x1 =1.f, y1 = 2.f, div = 3.2f;
00199 F32 val1, val2;
00200 LLVector2 vec2(x1, y1), vec3;
00201 vec3 = vec2 / div;
00202 val1 = x1 / div;
00203 val2 = y1 / div;
00204 ensure("1:operator/ failed", is_approx_equal(val1, vec3.mV[VX]) && is_approx_equal(val2, vec3.mV[VY]));
00205
00206 vec3.clearVec();
00207 x1 = -.235f, y1 = -24.32f, div = -2.2f;
00208 vec2.setVec(x1, y1);
00209 vec3 = vec2 / div;
00210 val1 = x1 / div;
00211 val2 = y1 / div;
00212 ensure("2:operator/ failed", is_approx_equal(val1, vec3.mV[VX]) && is_approx_equal(val2, vec3.mV[VY]));
00213 }
00214
00215 template<> template<>
00216 void v2math_object::test<10>()
00217 {
00218 F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
00219 F32 val1, val2;
00220 LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
00221 vec4 = vec2 % vec3;
00222 val1 = x1*y2 - x2*y1;
00223 val2 = y1*x2 - y2*x1;
00224 ensure("1:operator% failed",(val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY]));
00225
00226 vec2.clearVec();
00227 vec3.clearVec();
00228 vec4.clearVec();
00229 x1 = -.235f, y1 = -24.32f, x2 = -2.3f, y2 = 1.f;
00230 vec2.setVec(x1, y1);
00231 vec3.setVec(x2, y2);
00232 vec4 = vec2 % vec3;
00233 val1 = x1*y2 - x2*y1;
00234 val2 = y1*x2 - y2*x1;
00235 ensure("2:operator% failed",(val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY]));
00236 }
00237 template<> template<>
00238 void v2math_object::test<11>()
00239 {
00240 F32 x1 =1.f, y1 = 2.f;
00241 LLVector2 vec2(x1, y1), vec3(x1, y1);
00242 ensure("1:operator== failed",(vec2 == vec3));
00243
00244 vec2.clearVec();
00245 vec3.clearVec();
00246 x1 = -.235f, y1 = -24.32f;
00247 vec2.setVec(x1, y1);
00248 vec3.setVec(vec2);
00249 ensure("2:operator== failed",(vec2 == vec3));
00250 }
00251
00252 template<> template<>
00253 void v2math_object::test<12>()
00254 {
00255 F32 x1 = 1.f, y1 = 2.f,x2 = 2.332f, y2 = -1.23f;
00256 LLVector2 vec2(x1, y1), vec3(x2, y2);
00257 ensure("1:operator!= failed",(vec2 != vec3));
00258
00259 vec2.clearVec();
00260 vec3.clearVec();
00261 vec2.setVec(x1, y1);
00262 vec3.setVec(vec2);
00263 ensure("2:operator!= failed", (FALSE == (vec2 != vec3)));
00264 }
00265 template<> template<>
00266 void v2math_object::test<13>()
00267 {
00268 F32 x1 = 1.f, y1 = 2.f,x2 = 2.332f, y2 = -1.23f;
00269 F32 val1, val2;
00270 LLVector2 vec2(x1, y1), vec3(x2, y2);
00271 vec2 +=vec3;
00272 val1 = x1+x2;
00273 val2 = y1+y2;
00274 ensure("1:operator+= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
00275
00276 vec2.setVec(x1, y1);
00277 vec2 -=vec3;
00278 val1 = x1-x2;
00279 val2 = y1-y2;
00280 ensure("2:operator-= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
00281
00282 vec2.clearVec();
00283 vec3.clearVec();
00284 x1 = -21.000466f, y1 = 2.98382f,x2 = 0.332f, y2 = -01.23f;
00285 vec2.setVec(x1, y1);
00286 vec3.setVec(x2, y2);
00287 vec2 +=vec3;
00288 val1 = x1+x2;
00289 val2 = y1+y2;
00290 ensure("3:operator+= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
00291
00292 vec2.setVec(x1, y1);
00293 vec2 -=vec3;
00294 val1 = x1-x2;
00295 val2 = y1-y2;
00296 ensure("4:operator-= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
00297 }
00298
00299 template<> template<>
00300 void v2math_object::test<14>()
00301 {
00302 F32 x1 =1.f, y1 = 2.f;
00303 F32 val1, val2, mulVal = 4.332f;
00304 LLVector2 vec2(x1, y1);
00305 vec2 /=mulVal;
00306 val1 = x1 / mulVal;
00307 val2 = y1 / mulVal;
00308 ensure("1:operator/= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
00309
00310 vec2.clearVec();
00311 x1 = .213f, y1 = -2.34f, mulVal = -.23f;
00312 vec2.setVec(x1, y1);
00313 vec2 /=mulVal;
00314 val1 = x1 / mulVal;
00315 val2 = y1 / mulVal;
00316 ensure("2:operator/= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
00317 }
00318
00319 template<> template<>
00320 void v2math_object::test<15>()
00321 {
00322 F32 x1 =1.f, y1 = 2.f;
00323 F32 val1, val2, mulVal = 4.332f;
00324 LLVector2 vec2(x1, y1);
00325 vec2 *=mulVal;
00326 val1 = x1*mulVal;
00327 val2 = y1*mulVal;
00328 ensure("1:operator*= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
00329
00330 vec2.clearVec();
00331 x1 = .213f, y1 = -2.34f, mulVal = -.23f;
00332 vec2.setVec(x1, y1);
00333 vec2 *=mulVal;
00334 val1 = x1*mulVal;
00335 val2 = y1*mulVal;
00336 ensure("2:operator*= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
00337 }
00338
00339 template<> template<>
00340 void v2math_object::test<16>()
00341 {
00342 F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
00343 F32 val1, val2;
00344 LLVector2 vec2(x1, y1), vec3(x2, y2);
00345 vec2 %= vec3;
00346 val1 = x1*y2 - x2*y1;
00347 val2 = y1*x2 - y2*x1;
00348 ensure("1:operator%= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
00349 }
00350
00351 template<> template<>
00352 void v2math_object::test<17>()
00353 {
00354 F32 x1 =1.f, y1 = 2.f;
00355 LLVector2 vec2(x1, y1),vec3;
00356 vec3 = -vec2;
00357 ensure("1:operator- failed",(-vec3 == vec2));
00358 }
00359
00360 template<> template<>
00361 void v2math_object::test<18>()
00362 {
00363 F32 x1 =1.f, y1 = 2.f;
00364 std::ostringstream stream1, stream2;
00365 LLVector2 vec2(x1, y1),vec3;
00366 stream1 << vec2;
00367 vec3.setVec(x1, y1);
00368 stream2 << vec3;
00369 ensure("1:operator << failed",(stream1.str() == stream2.str()));
00370 }
00371
00372 template<> template<>
00373 void v2math_object::test<19>()
00374 {
00375 F32 x1 =1.0f, y1 = 2.0f, x2 = -.32f, y2 = .2234f;
00376 LLVector2 vec2(x1, y1),vec3(x2, y2);
00377 ensure("1:operator < failed",(vec3 < vec2));
00378
00379 x1 = 1.0f, y1 = 2.0f, x2 = 1.0f, y2 = 3.2234f;
00380 vec2.setVec(x1, y1);
00381 vec3.setVec(x2, y2);
00382 ensure("2:operator < failed", (FALSE == vec3 < vec2));
00383 }
00384
00385 template<> template<>
00386 void v2math_object::test<20>()
00387 {
00388 F32 x1 =1.0f, y1 = 2.0f;
00389 LLVector2 vec2(x1, y1);
00390 ensure("1:operator [] failed",( x1 == vec2[0]));
00391 ensure("2:operator [] failed",( y1 == vec2[1]));
00392
00393 vec2.clearVec();
00394 x1 = 23.0f, y1 = -.2361f;
00395 vec2.setVec(x1, y1);
00396 F32 ref1 = vec2[0];
00397 ensure("3:operator [] failed", ( ref1 == x1));
00398 F32 ref2 = vec2[1];
00399 ensure("4:operator [] failed", ( ref2 == y1));
00400 }
00401
00402 template<> template<>
00403 void v2math_object::test<21>()
00404 {
00405 F32 x1 =1.f, y1 = 2.f, x2 = -.32f, y2 = .2234f;
00406 F32 val1, val2;
00407 LLVector2 vec2(x1, y1),vec3(x2, y2);
00408 val1 = dist_vec_squared2D(vec2, vec3);
00409 val2 = (x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2);
00410 ensure_equals("dist_vec_squared2D values are not equal",val2, val1);
00411
00412 val1 = dist_vec_squared(vec2, vec3);
00413 ensure_equals("dist_vec_squared values are not equal",val2, val1);
00414
00415 val1 = dist_vec(vec2, vec3);
00416 val2 = fsqrtf((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2));
00417 ensure_equals("dist_vec values are not equal",val2, val1);
00418 }
00419
00420 template<> template<>
00421 void v2math_object::test<22>()
00422 {
00423 F32 x1 =1.f, y1 = 2.f, x2 = -.32f, y2 = .2234f,fVal = .0121f;
00424 F32 val1, val2;
00425 LLVector2 vec2(x1, y1),vec3(x2, y2);
00426 LLVector2 vec4 = lerp(vec2, vec3, fVal);
00427 val1 = x1 + (x2 - x1) * fVal;
00428 val2 = y1 + (y2 - y1) * fVal;
00429 ensure("lerp values are not equal", ((val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY])));
00430 }
00431
00432 template<> template<>
00433 void v2math_object::test<23>()
00434 {
00435 F32 x1 =1.f, y1 = 2.f;
00436 F32 val1, val2;
00437 LLVector2 vec2(x1, y1);
00438
00439 F32 vecMag = vec2.normVec();
00440 F32 mag = fsqrtf(x1*x1 + y1*y1);
00441
00442 F32 oomag = 1.f / mag;
00443 val1 = x1 * oomag;
00444 val2 = y1 * oomag;
00445
00446 ensure("normVec failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]) && is_approx_equal(vecMag, mag));
00447
00448 x1 =.00000001f, y1 = 0.f;
00449
00450 vec2.setVec(x1, y1);
00451 vecMag = vec2.normVec();
00452 ensure("normVec failed should be 0.", 0. == vec2.mV[VX] && 0. == vec2.mV[VY] && vecMag == 0.);
00453 }
00454 }