/[usata]/usata2/src/test/vector.cpp
ViewVC logotype

Diff of /usata2/src/test/vector.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by Descender, Mon Jan 3 09:47:14 2005 UTC revision 1.2 by Descender, Tue Jan 4 15:04:37 2005 UTC
# Line 23  test_vector4i() Line 23  test_vector4i()
23  {  {
24          using math::Vector4i;          using math::Vector4i;
25    
26            // NOTE: the order of these tests are important. Each test depend
27            // on the truth result of previous ones - descender
28    
29            // NOTE: These tests will become more useful when we switch to an
30            // expression template implementation - descender
31    
32          // default constructor          // default constructor
33          {          {
34                  Vector4i x;                  Vector4i x;
# Line 41  test_vector4i() Line 47  test_vector4i()
47                                                    y.v[2] == 2 && y.v[3] == 0);                                                    y.v[2] == 2 && y.v[3] == 0);
48          }          }
49    
50            // index operator
51            {
52                    Vector4i x(0, 1, 2, 3);
53                    // rvalue
54                    USATA_TEST_ASSERT(x[0] == x.v[0] && x[1] == x.v[1] &&
55                                                      x[2] == x.v[2] && x[3] == x.v[3]);
56    
57                    // lvalue
58                    x[0] = 4;
59                    x[1] = 5;
60                    x[2] = 6;
61                    x[3] = 7;
62                    USATA_TEST_ASSERT(x[0] == 4 && x[1] == 5 &&
63                                                      x[2] == 6 && x[3] == 7);
64            }
65    
66            // equality operators
67          {          {
68                  Vector4i x(0, 1, 2, 3);                  Vector4i x(0, 1, 2, 3);
69                  Vector4i y(x);                  Vector4i y(x);
70    
71                  USATA_TEST_ASSERT(x == y);                  USATA_TEST_ASSERT(x == y);
72                  USATA_TEST_ASSERT(!(x != y));                  USATA_TEST_ASSERT(!(x != y));
73          }            }
74    
75            // assignment, copy constructor
76            {
77                    Vector4i x(0, 1, 2, 3);
78                    Vector4i y;
79                    
80                    y = x;
81                    USATA_TEST_ASSERT(x == y);
82    
83                    Vector4i z(y);
84                    USATA_TEST_ASSERT(y == z);
85            }
86    
87            // addition, subtraction, negation
88            {
89                    Vector4i x(0, 1, 2, 3);
90                    Vector4i y(4, 5, 6, 7);
91                    Vector4i z;
92    
93                    z = x + y;
94                    USATA_TEST_ASSERT(z[0] == x[0]+y[0] &&
95                                                      z[1] == x[1]+y[1] &&
96                                                      z[2] == x[2]+y[2] &&
97                                                      z[3] == x[3]+y[3]);
98    
99                    USATA_TEST_ASSERT(z == Vector4i(4, 6, 8, 10));
100    
101                    z = x - y;
102                    USATA_TEST_ASSERT(z == Vector4i(-4, -4, -4, -4));
103    
104                    z = -x;
105                    USATA_TEST_ASSERT(z == Vector4i(0, -1, -2, -3));
106    
107                    USATA_TEST_ASSERT(x - y == -(y - x));
108            }
109    
110            // scalar multiplication/division operator
111            {
112                    Vector4i x(0, 1, 2, 3);
113                    Vector4i y = x * 2;
114    
115                    USATA_TEST_ASSERT(y == Vector4i(0, 2, 4, 6));
116                    USATA_TEST_ASSERT(y == 2 * x);
117                    USATA_TEST_ASSERT(y/2 == x);
118            }
119    
120            // assignment-x operators
121            {
122                    Vector4i x(0, 1, 2, 3);
123                    Vector4i y(4, 5, 6, 7);
124                    Vector4i z;
125    
126                    z = x; z += y;
127                    USATA_TEST_ASSERT(z == x + y);
128    
129                    z = x; z -= y;
130                    USATA_TEST_ASSERT(z == x - y);
131    
132                    z = x; z *= 2;
133                    USATA_TEST_ASSERT(z == x * 2);
134    
135                    z = x; z /= 2;
136                    USATA_TEST_ASSERT(z == x / 2);
137            }
138    
139            // dot product
140            {
141                    Vector4i x(0, 1, 2, 3);
142                    Vector4i y(4, 5, 6, 7);
143    
144                    USATA_TEST_ASSERT(x.dot(x) == 14);
145                    USATA_TEST_ASSERT(x.dot(y) == 38);
146                    USATA_TEST_ASSERT(x.dot(y) == y.dot(x));
147            }
148    
149            // cross product
150            {
151                    Vector4i x(1, 0, 0);
152                    Vector4i y(0, 1, 0);
153                    Vector4i z = x.cross(y);
154    
155                    USATA_TEST_ASSERT(z == Vector4i(0, 0, 1));
156                    USATA_TEST_ASSERT(z == -y.cross(x));
157                    USATA_TEST_ASSERT(z.dot(x) == 0 && z.dot(y) == 0);
158            }
159    
160            // length and normalization
161            {
162                    Vector4i x(100, 200, 300);
163                    Vector4i y(10, 0, 0);
164    
165                    USATA_TEST_ASSERT(x.length<int>() == 374);
166                    USATA_TEST_ASSERT(std::abs(x.length<double>() - 374.16573867739413) <= 0.001);
167    
168                    // NOTE: Normalizing an integer vector doesn't work very for most vectors
169                    // - descender
170    
171                    USATA_TEST_ASSERT(x.unit<int>() == Vector4i());
172    
173                    x.normalize();
174                    USATA_TEST_ASSERT(x.length<int>() == 0);
175                    USATA_TEST_ASSERT(x.length<double>() == 0.0);
176    
177                    USATA_TEST_ASSERT(y.unit<int>() == Vector4i(1, 0, 0));
178    
179                    y.normalize();
180                    USATA_TEST_ASSERT(y.length<int>() == 1);
181                    USATA_TEST_ASSERT(y.length<double>() == 1.0);
182            }
183    
184            // conversion
185            {
186                    using math::Vector4f;
187    
188                    Vector4f x(1.2, 2.3, 3.4, 4.5);
189                    Vector4i y = x.to<int>();
190    
191                    USATA_TEST_ASSERT(y == Vector4i(1, 2, 3, 4));
192            }
193  }  }
194    
195    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26