1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/**
* @file v2math_tut.cpp
* @author Adroit
* @date 2007-02
* @brief v2math test cases.
*
* $LicenseInfo:firstyear=2007&license=viewergpl$
*
* Copyright (c) 2007-2008, Linden Research, Inc.
*
* Second Life Viewer Source Code
* The source code in this file ("Source Code") is provided by Linden Lab
* to you under the terms of the GNU General Public License, version 2.0
* ("GPL"), unless you have obtained a separate licensing agreement
* ("Other License"), formally executed by you and Linden Lab. Terms of
* the GPL can be found in doc/GPL-license.txt in this distribution, or
* online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution, or
* online at http://secondlifegrid.net/programs/open_source/licensing/flossexception
*
* By copying, modifying or distributing this software, you acknowledge
* that you have read and understood your obligations described above,
* and agree to abide by those obligations.
*
* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
* COMPLETENESS OR PERFORMANCE.
* $/LicenseInfo$
*/
#include <tut/tut.h>
#include "linden_common.h"
#include "lltut.h"
#include "v2math.h"
namespace tut
{
struct v2math_data
{
};
typedef test_group<v2math_data> v2math_test;
typedef v2math_test::object v2math_object;
tut::v2math_test v2math_testcase("v2math");
template<> template<>
void v2math_object::test<1>()
{
LLVector2 vec2;
ensure("LLVector2:Fail to initialize ", (0.f == vec2.mV[VX] && 0.f == vec2.mV[VY]));
F32 x =2.0f, y = 3.2f ;
LLVector2 vec3(x,y);
ensure("LLVector2(F32 x, F32 y):Fail to initialize ", (x == vec3.mV[VX]) && (y == vec3.mV[VY]));
const F32 vec[2] = {3.2f, 4.5f};
LLVector2 vec4(vec);
ensure("LLVector2(const F32 *vec):Fail to initialize ", (vec[0] == vec4.mV[VX]) && (vec[1] == vec4.mV[VY]));
vec4.clearVec();
ensure("clearVec():Fail to clean the values ", (0.f == vec4.mV[VX] && 0.f == vec4.mV[VY]));
vec3.zeroVec();
ensure("zeroVec():Fail to fill the zero ", (0.f == vec3.mV[VX] && 0.f == vec3.mV[VY]));
}
template<> template<>
void v2math_object::test<2>()
{
F32 x = 123.356f, y = 2387.453f;
LLVector2 vec2,vec3;
vec2.setVec(x, y);
ensure("1:setVec: Fail ", (x == vec2.mV[VX]) && (y == vec2.mV[VY]));
vec3.setVec(vec2);
ensure("2:setVec: Fail " ,(vec2 == vec3));
vec3.zeroVec();
const F32 vec[2] = {3.24653f, 457653.4f};
vec3.setVec(vec);
ensure("3:setVec: Fail ", (vec[0] == vec3.mV[VX]) && (vec[1] == vec3.mV[VY]));
}
template<> template<>
void v2math_object::test<3>()
{
F32 x = 2.2345f, y = 3.5678f ;
LLVector2 vec2(x,y);
ensure("magVecSquared:Fail ", is_approx_equal(vec2.magVecSquared(), (x*x + y*y)));
ensure("magVec:Fail ", is_approx_equal(vec2.magVec(), fsqrtf(x*x + y*y)));
}
template<> template<>
void v2math_object::test<4>()
{
F32 x =-2.0f, y = -3.0f ;
LLVector2 vec2(x,y);
ensure_equals("abs():Fail", vec2.abs(), TRUE);
ensure("abs() x", is_approx_equal(vec2.mV[VX], 2.f));
ensure("abs() y", is_approx_equal(vec2.mV[VY], 3.f));
ensure("isNull():Fail ", FALSE == vec2.isNull()); //Returns TRUE if vector has a _very_small_ length
x =.00000001f, y = .000001001f;
vec2.setVec(x, y);
ensure("isNull(): Fail ", TRUE == vec2.isNull());
}
template<> template<>
void v2math_object::test<5>()
{
F32 x =1.f, y = 2.f;
LLVector2 vec2(x, y), vec3;
vec3 = vec3.scaleVec(vec2);
ensure("scaleVec: Fail ", vec3.mV[VX] == 0. && vec3.mV[VY] == 0.);
ensure("isExactlyZero(): Fail", TRUE == vec3.isExactlyZero());
vec3.setVec(2.f, 1.f);
vec3 = vec3.scaleVec(vec2);
ensure("scaleVec: Fail ", (2.f == vec3.mV[VX]) && (2.f == vec3.mV[VY]));
ensure("isExactlyZero():Fail", FALSE == vec3.isExactlyZero());
}
template<> template<>
void v2math_object::test<6>()
{
F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
F32 val1, val2;
LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
vec4 = vec2 + vec3 ;
val1 = x1+x2;
val2 = y1+y2;
ensure("1:operator+ failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
vec2.clearVec();
vec3.clearVec();
x1 = -.235f, y1 = -24.32f, x2 = -2.3f, y2 = 1.f;
vec2.setVec(x1, y1);
vec3.setVec(x2, y2);
vec4 = vec2 + vec3;
val1 = x1+x2;
val2 = y1+y2;
ensure("2:operator+ failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
}
template<> template<>
void v2math_object::test<7>()
{
F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
F32 val1, val2;
LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
vec4 = vec2 - vec3 ;
val1 = x1-x2;
val2 = y1-y2;
ensure("1:operator- failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
vec2.clearVec();
vec3.clearVec();
vec4.clearVec();
x1 = -.235f, y1 = -24.32f, x2 = -2.3f, y2 = 1.f;
vec2.setVec(x1, y1);
vec3.setVec(x2, y2);
vec4 = vec2 - vec3;
val1 = x1-x2;
val2 = y1-y2;
ensure("2:operator- failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
}
template<> template<>
void v2math_object::test<8>()
{
F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
F32 val1, val2;
LLVector2 vec2(x1, y1), vec3(x2, y2);
val1 = vec2 * vec3;
val2 = x1*x2 + y1*y2;
ensure("1:operator* failed",(val1 == val2));
vec3.clearVec();
F32 mulVal = 4.332f;
vec3 = vec2 * mulVal;
val1 = x1*mulVal;
val2 = y1*mulVal;
ensure("2:operator* failed",(val1 == vec3.mV[VX]) && (val2 == vec3.mV[VY]));
vec3.clearVec();
vec3 = mulVal * vec2;
ensure("3:operator* failed",(val1 == vec3.mV[VX]) && (val2 == vec3.mV[VY]));
}
template<> template<>
void v2math_object::test<9>()
{
F32 x1 =1.f, y1 = 2.f, div = 3.2f;
F32 val1, val2;
LLVector2 vec2(x1, y1), vec3;
vec3 = vec2 / div;
val1 = x1 / div;
val2 = y1 / div;
ensure("1:operator/ failed", is_approx_equal(val1, vec3.mV[VX]) && is_approx_equal(val2, vec3.mV[VY]));
vec3.clearVec();
x1 = -.235f, y1 = -24.32f, div = -2.2f;
vec2.setVec(x1, y1);
vec3 = vec2 / div;
val1 = x1 / div;
val2 = y1 / div;
ensure("2:operator/ failed", is_approx_equal(val1, vec3.mV[VX]) && is_approx_equal(val2, vec3.mV[VY]));
}
template<> template<>
void v2math_object::test<10>()
{
F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
F32 val1, val2;
LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
vec4 = vec2 % vec3;
val1 = x1*y2 - x2*y1;
val2 = y1*x2 - y2*x1;
ensure("1:operator% failed",(val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY]));
vec2.clearVec();
vec3.clearVec();
vec4.clearVec();
x1 = -.235f, y1 = -24.32f, x2 = -2.3f, y2 = 1.f;
vec2.setVec(x1, y1);
vec3.setVec(x2, y2);
vec4 = vec2 % vec3;
val1 = x1*y2 - x2*y1;
val2 = y1*x2 - y2*x1;
ensure("2:operator% failed",(val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY]));
}
template<> template<>
void v2math_object::test<11>()
{
F32 x1 =1.f, y1 = 2.f;
LLVector2 vec2(x1, y1), vec3(x1, y1);
ensure("1:operator== failed",(vec2 == vec3));
vec2.clearVec();
vec3.clearVec();
x1 = -.235f, y1 = -24.32f;
vec2.setVec(x1, y1);
vec3.setVec(vec2);
ensure("2:operator== failed",(vec2 == vec3));
}
template<> template<>
void v2math_object::test<12>()
{
F32 x1 = 1.f, y1 = 2.f,x2 = 2.332f, y2 = -1.23f;
LLVector2 vec2(x1, y1), vec3(x2, y2);
ensure("1:operator!= failed",(vec2 != vec3));
vec2.clearVec();
vec3.clearVec();
vec2.setVec(x1, y1);
vec3.setVec(vec2);
ensure("2:operator!= failed", (FALSE == (vec2 != vec3)));
}
template<> template<>
void v2math_object::test<13>()
{
F32 x1 = 1.f, y1 = 2.f,x2 = 2.332f, y2 = -1.23f;
F32 val1, val2;
LLVector2 vec2(x1, y1), vec3(x2, y2);
vec2 +=vec3;
val1 = x1+x2;
val2 = y1+y2;
ensure("1:operator+= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
vec2.setVec(x1, y1);
vec2 -=vec3;
val1 = x1-x2;
val2 = y1-y2;
ensure("2:operator-= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
vec2.clearVec();
vec3.clearVec();
x1 = -21.000466f, y1 = 2.98382f,x2 = 0.332f, y2 = -01.23f;
vec2.setVec(x1, y1);
vec3.setVec(x2, y2);
vec2 +=vec3;
val1 = x1+x2;
val2 = y1+y2;
ensure("3:operator+= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
vec2.setVec(x1, y1);
vec2 -=vec3;
val1 = x1-x2;
val2 = y1-y2;
ensure("4:operator-= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
}
template<> template<>
void v2math_object::test<14>()
{
F32 x1 =1.f, y1 = 2.f;
F32 val1, val2, mulVal = 4.332f;
LLVector2 vec2(x1, y1);
vec2 /=mulVal;
val1 = x1 / mulVal;
val2 = y1 / mulVal;
ensure("1:operator/= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
vec2.clearVec();
x1 = .213f, y1 = -2.34f, mulVal = -.23f;
vec2.setVec(x1, y1);
vec2 /=mulVal;
val1 = x1 / mulVal;
val2 = y1 / mulVal;
ensure("2:operator/= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
}
template<> template<>
void v2math_object::test<15>()
{
F32 x1 =1.f, y1 = 2.f;
F32 val1, val2, mulVal = 4.332f;
LLVector2 vec2(x1, y1);
vec2 *=mulVal;
val1 = x1*mulVal;
val2 = y1*mulVal;
ensure("1:operator*= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
vec2.clearVec();
x1 = .213f, y1 = -2.34f, mulVal = -.23f;
vec2.setVec(x1, y1);
vec2 *=mulVal;
val1 = x1*mulVal;
val2 = y1*mulVal;
ensure("2:operator*= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
}
template<> template<>
void v2math_object::test<16>()
{
F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
F32 val1, val2;
LLVector2 vec2(x1, y1), vec3(x2, y2);
vec2 %= vec3;
val1 = x1*y2 - x2*y1;
val2 = y1*x2 - y2*x1;
ensure("1:operator%= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
}
template<> template<>
void v2math_object::test<17>()
{
F32 x1 =1.f, y1 = 2.f;
LLVector2 vec2(x1, y1),vec3;
vec3 = -vec2;
ensure("1:operator- failed",(-vec3 == vec2));
}
template<> template<>
void v2math_object::test<18>()
{
F32 x1 =1.f, y1 = 2.f;
std::ostringstream stream1, stream2;
LLVector2 vec2(x1, y1),vec3;
stream1 << vec2;
vec3.setVec(x1, y1);
stream2 << vec3;
ensure("1:operator << failed",(stream1.str() == stream2.str()));
}
template<> template<>
void v2math_object::test<19>()
{
F32 x1 =1.0f, y1 = 2.0f, x2 = -.32f, y2 = .2234f;
LLVector2 vec2(x1, y1),vec3(x2, y2);
ensure("1:operator < failed",(vec3 < vec2));
x1 = 1.0f, y1 = 2.0f, x2 = 1.0f, y2 = 3.2234f;
vec2.setVec(x1, y1);
vec3.setVec(x2, y2);
ensure("2:operator < failed", (FALSE == vec3 < vec2));
}
template<> template<>
void v2math_object::test<20>()
{
F32 x1 =1.0f, y1 = 2.0f;
LLVector2 vec2(x1, y1);
ensure("1:operator [] failed",( x1 == vec2[0]));
ensure("2:operator [] failed",( y1 == vec2[1]));
vec2.clearVec();
x1 = 23.0f, y1 = -.2361f;
vec2.setVec(x1, y1);
F32 ref1 = vec2[0];
ensure("3:operator [] failed", ( ref1 == x1));
F32 ref2 = vec2[1];
ensure("4:operator [] failed", ( ref2 == y1));
}
template<> template<>
void v2math_object::test<21>()
{
F32 x1 =1.f, y1 = 2.f, x2 = -.32f, y2 = .2234f;
F32 val1, val2;
LLVector2 vec2(x1, y1),vec3(x2, y2);
val1 = dist_vec_squared2D(vec2, vec3);
val2 = (x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2);
ensure_equals("dist_vec_squared2D values are not equal",val2, val1);
val1 = dist_vec_squared(vec2, vec3);
ensure_equals("dist_vec_squared values are not equal",val2, val1);
val1 = dist_vec(vec2, vec3);
val2 = fsqrtf((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2));
ensure_equals("dist_vec values are not equal",val2, val1);
}
template<> template<>
void v2math_object::test<22>()
{
F32 x1 =1.f, y1 = 2.f, x2 = -.32f, y2 = .2234f,fVal = .0121f;
F32 val1, val2;
LLVector2 vec2(x1, y1),vec3(x2, y2);
LLVector2 vec4 = lerp(vec2, vec3, fVal);
val1 = x1 + (x2 - x1) * fVal;
val2 = y1 + (y2 - y1) * fVal;
ensure("lerp values are not equal", ((val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY])));
}
template<> template<>
void v2math_object::test<23>()
{
F32 x1 =1.f, y1 = 2.f;
F32 val1, val2;
LLVector2 vec2(x1, y1);
F32 vecMag = vec2.normVec();
F32 mag = fsqrtf(x1*x1 + y1*y1);
F32 oomag = 1.f / mag;
val1 = x1 * oomag;
val2 = y1 * oomag;
ensure("normVec failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]) && is_approx_equal(vecMag, mag));
x1 =.00000001f, y1 = 0.f;
vec2.setVec(x1, y1);
vecMag = vec2.normVec();
ensure("normVec failed should be 0.", 0. == vec2.mV[VX] && 0. == vec2.mV[VY] && vecMag == 0.);
}
}
|