usata-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Usata-commits] Changes to usata2/src/test/vector.cpp


From: Chong Kai Xiong
Subject: [Usata-commits] Changes to usata2/src/test/vector.cpp
Date: Tue, 04 Jan 2005 10:21:25 -0500

Index: usata2/src/test/vector.cpp
diff -u usata2/src/test/vector.cpp:1.1 usata2/src/test/vector.cpp:1.2
--- usata2/src/test/vector.cpp:1.1      Mon Jan  3 09:47:14 2005
+++ usata2/src/test/vector.cpp  Tue Jan  4 15:04:37 2005
@@ -9,7 +9,7 @@
 // included in the software distribution, or visit
 // http://www.fsf.org/licenses/gpl.html.
 //
-// $Id: vector.cpp,v 1.1 2005/01/03 09:47:14 Descender Exp $
+// $Id: vector.cpp,v 1.2 2005/01/04 15:04:37 Descender Exp $
 
 #include <iostream>
 #include "test.hpp"
@@ -23,6 +23,12 @@
 {
        using math::Vector4i;
 
+       // NOTE: the order of these tests are important. Each test depend
+       // on the truth result of previous ones - descender
+
+       // NOTE: These tests will become more useful when we switch to an
+       // expression template implementation - descender
+
        // default constructor
        {
                Vector4i x;
@@ -41,13 +47,149 @@
                                                  y.v[2] == 2 && y.v[3] == 0);
        }
 
+       // index operator
+       {
+               Vector4i x(0, 1, 2, 3);
+               // rvalue
+               USATA_TEST_ASSERT(x[0] == x.v[0] && x[1] == x.v[1] &&
+                                                 x[2] == x.v[2] && x[3] == 
x.v[3]);
+
+               // lvalue
+               x[0] = 4;
+               x[1] = 5;
+               x[2] = 6;
+               x[3] = 7;
+               USATA_TEST_ASSERT(x[0] == 4 && x[1] == 5 &&
+                                                 x[2] == 6 && x[3] == 7);
+       }
+
+       // equality operators
        {
                Vector4i x(0, 1, 2, 3);
                Vector4i y(x);
 
                USATA_TEST_ASSERT(x == y);
                USATA_TEST_ASSERT(!(x != y));
-       }   
+       }
+
+       // assignment, copy constructor
+       {
+               Vector4i x(0, 1, 2, 3);
+               Vector4i y;
+               
+               y = x;
+               USATA_TEST_ASSERT(x == y);
+
+               Vector4i z(y);
+               USATA_TEST_ASSERT(y == z);
+       }
+
+       // addition, subtraction, negation
+       {
+               Vector4i x(0, 1, 2, 3);
+               Vector4i y(4, 5, 6, 7);
+               Vector4i z;
+
+               z = x + y;
+               USATA_TEST_ASSERT(z[0] == x[0]+y[0] && 
+                                                 z[1] == x[1]+y[1] &&
+                                                 z[2] == x[2]+y[2] &&
+                                                 z[3] == x[3]+y[3]);
+
+               USATA_TEST_ASSERT(z == Vector4i(4, 6, 8, 10));
+
+               z = x - y;
+               USATA_TEST_ASSERT(z == Vector4i(-4, -4, -4, -4));
+
+               z = -x;
+               USATA_TEST_ASSERT(z == Vector4i(0, -1, -2, -3));
+
+               USATA_TEST_ASSERT(x - y == -(y - x));
+       }
+
+       // scalar multiplication/division operator
+       {
+               Vector4i x(0, 1, 2, 3);
+               Vector4i y = x * 2;
+
+               USATA_TEST_ASSERT(y == Vector4i(0, 2, 4, 6));
+               USATA_TEST_ASSERT(y == 2 * x);
+               USATA_TEST_ASSERT(y/2 == x);
+       }
+
+       // assignment-x operators
+       {
+               Vector4i x(0, 1, 2, 3);
+               Vector4i y(4, 5, 6, 7);
+               Vector4i z;
+
+               z = x; z += y;
+               USATA_TEST_ASSERT(z == x + y);
+
+               z = x; z -= y;
+               USATA_TEST_ASSERT(z == x - y);
+
+               z = x; z *= 2;
+               USATA_TEST_ASSERT(z == x * 2);
+
+               z = x; z /= 2;
+               USATA_TEST_ASSERT(z == x / 2);
+       }
+
+       // dot product
+       {
+               Vector4i x(0, 1, 2, 3);
+               Vector4i y(4, 5, 6, 7);
+
+               USATA_TEST_ASSERT(x.dot(x) == 14);
+               USATA_TEST_ASSERT(x.dot(y) == 38);
+               USATA_TEST_ASSERT(x.dot(y) == y.dot(x));
+       }
+
+       // cross product
+       {
+               Vector4i x(1, 0, 0);
+               Vector4i y(0, 1, 0);
+               Vector4i z = x.cross(y);
+
+               USATA_TEST_ASSERT(z == Vector4i(0, 0, 1));
+               USATA_TEST_ASSERT(z == -y.cross(x));
+               USATA_TEST_ASSERT(z.dot(x) == 0 && z.dot(y) == 0);
+       }
+
+       // length and normalization
+       {
+               Vector4i x(100, 200, 300);
+               Vector4i y(10, 0, 0);
+
+               USATA_TEST_ASSERT(x.length<int>() == 374);
+               USATA_TEST_ASSERT(std::abs(x.length<double>() - 
374.16573867739413) <= 0.001);
+
+               // NOTE: Normalizing an integer vector doesn't work very for 
most vectors
+               // - descender
+
+               USATA_TEST_ASSERT(x.unit<int>() == Vector4i());
+
+               x.normalize();
+               USATA_TEST_ASSERT(x.length<int>() == 0);
+               USATA_TEST_ASSERT(x.length<double>() == 0.0);
+
+               USATA_TEST_ASSERT(y.unit<int>() == Vector4i(1, 0, 0));
+
+               y.normalize();
+               USATA_TEST_ASSERT(y.length<int>() == 1);
+               USATA_TEST_ASSERT(y.length<double>() == 1.0);
+       }
+
+       // conversion 
+       {
+               using math::Vector4f;
+
+               Vector4f x(1.2, 2.3, 3.4, 4.5);
+               Vector4i y = x.to<int>();
+
+               USATA_TEST_ASSERT(y == Vector4i(1, 2, 3, 4));
+       }
 }
 
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]