Dali 3D User Interface Engine
utc-Dali-Vector2.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include <iostream>
19 #include <sstream>
20 #include <cmath> // isfinite
21 
22 #include <stdlib.h>
24 #include <dali-test-suite-utils.h>
25 
26 using namespace Dali;
27 
29 {
31 }
32 
34 {
36 }
37 
39 {
40  TestApplication application;
41  Vector2 vec2;
42  DALI_TEST_EQUALS(vec2.x, 0.0f, 0.001, TEST_LOCATION);
43  DALI_TEST_EQUALS(vec2.y, 0.0f, 0.001, TEST_LOCATION);
44  END_TEST;
45 }
46 
48 {
49  TestApplication application;
50  Vector2 vec2(1.f,1.f);
51  DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
52  DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
53  END_TEST;
54 }
55 
57 {
58  TestApplication application;
59  float array[] = {1.f,1.f};
60  Vector2 vec2(array);
61  DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
62  DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
63  END_TEST;
64 }
65 
67 {
68  TestApplication application;
69  Vector3 vec3(1.f,1.f,1.f);
70  Vector3 vec2(vec3);
71  DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
72  DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
73  END_TEST;
74 }
75 
77 {
78  TestApplication application;
79  Vector4 vec4(1.f,1.f,1.f,1.f);
80  Vector2 vec2(vec4);
81  DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
82  DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
83  END_TEST;
84 }
85 
87 {
88  TestApplication application;
89  float array[] = {1.f,1.f};
90  Vector2 vec2;
91  vec2 = array;
92  DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
93  DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
94  END_TEST;
95 }
96 
98 {
99  TestApplication application;
100  Vector3 vec3(1.f,1.f,1.f);
101  Vector3 vec2;
102  vec2 = vec3;
103  DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
104  DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
105  END_TEST;
106 }
107 
109 {
110  TestApplication application;
111  Vector4 vec4(1.f,1.f,1.f,1.f);
112  Vector3 vec2;
113  vec2 = vec4;
114  DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
115  DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
116  END_TEST;
117 }
118 
120 {
121  TestApplication application;
122  Vector2 v1(10.0f, 20.0f);
123  Vector2 r0(11.0f, 22.0f);
124 
125  v1 = r0;
127  END_TEST;
128 }
129 
131 {
132  TestApplication application;
133  Vector2 v1(10.0f, 20.0f);
134  Vector4 r0(11.0f, 22.0f, 33.f, 44.f);
135 
136  v1 = r0;
137  DALI_TEST_EQUALS(v1, Vector2(r0.x, r0.y), TEST_LOCATION);
138  END_TEST;
139 }
140 
142 {
143  TestApplication application;
144  Vector2 v1(10.0f, 20.0f);
145  Vector3 r0(11.0f, 22.0f, 33.f);
146 
147  v1 = r0;
148  DALI_TEST_EQUALS(v1, Vector2(r0.x, r0.y), TEST_LOCATION);
149  END_TEST;
150 }
151 
153 {
154  TestApplication application;
155  Vector2 v0(1.0f, 2.0f);
156  Vector2 v1(10.0f, 20.0f);
157  Vector2 r0(11.0f, 22.0f);
158 
159  Vector2 v2 = v0+v1;
161  END_TEST;
162 }
163 
165 {
166  TestApplication application;
167  Vector2 v0(1.0f, 2.0f);
168  Vector2 v1(10.0f, 20.0f);
169  Vector2 r0(11.0f, 22.0f);
170 
171  v0 += v1;
173  END_TEST;
174 }
175 
177 {
178  TestApplication application;
179  Vector2 v0(11.0f, 22.0f);
180  Vector2 v1(10.0f, 20.0f);
181  Vector2 r0(1.0f, 2.0f);
182 
183  Vector2 v2 = v0-v1;
185 
186  END_TEST;
187 }
188 
190 {
191  TestApplication application;
192  Vector2 v0(11.0f, 22.0f);
193  Vector2 v1(10.0f, 20.0f);
194  Vector2 r0(1.0f, 2.0f);
195 
196  v0 -= v1;
198  END_TEST;
199 }
200 
202 {
203  TestApplication application;
204  Vector2 v1(10.0f, 20.0f);
205  Vector2 r0(-10.0f, -20.0f);
206 
207  Vector2 v2 = -v1;
209  END_TEST;
210 }
211 
213 {
214  TestApplication application;
215  Vector2 v0(2.0f, 3.0f);
216  Vector2 v1(10.0f, 20.0f);
217  Vector2 r0(20.0f, 60.0f);
218 
219  Vector2 v2 = v0 * v1;
221 
222  END_TEST;
223 }
224 
226 {
227  TestApplication application;
228  Vector2 v0(2.0f, 3.0f);
229  Vector2 r0(20.0f, 30.0f);
230 
231  Vector2 v2 = v0 * 10.f;
233 
234  END_TEST;
235 }
236 
238 {
239  TestApplication application;
240  Vector2 v0(2.0f, 3.0f);
241  Vector2 v1(10.0f, 20.0f);
242  Vector2 r0(20.0f, 60.0f);
243 
244  v0 *= v1;
246  END_TEST;
247 }
248 
250 {
251  TestApplication application;
252  Vector2 v0(2.0f, 3.0f);
253  Vector2 r0(20.0f, 30.0f);
254 
255  v0 *= 10.f;
257  END_TEST;
258 }
259 
261 {
262  TestApplication application;
263  Vector2 v0(1.0f, 1.0f);
264  Vector2 v1(2.0f, 3.0f);
265  Vector2 v2(4.0f, 9.0f);
266 
267  DALI_TEST_EQUALS( v0/v0, v0, TEST_LOCATION);
268  DALI_TEST_EQUALS( v1/v0, v1, TEST_LOCATION);
269  DALI_TEST_EQUALS( v1/v1, v0, TEST_LOCATION);
270  DALI_TEST_EQUALS( v2/v1, v1, TEST_LOCATION);
271 
272  END_TEST;
273 }
274 
276 {
277  TestApplication application;
278  Vector2 v0(1.0f, 1.0f);
279  Vector2 v1(3.0f, 3.0f);
280  Vector2 v2(9.0f, 9.0f);
281 
282  DALI_TEST_EQUALS( v0/1.f, v0, TEST_LOCATION);
283  DALI_TEST_EQUALS( v1/1.f, v1, TEST_LOCATION);
284  DALI_TEST_EQUALS( v1/3.f, v0, TEST_LOCATION);
285 
286  END_TEST;
287 }
288 
290 {
291  TestApplication application;
292  Vector2 v0(1.0f, 1.0f);
293  Vector2 v1(2.0f, 3.0f);
294  Vector2 v2(4.0f, 9.0f);
295 
296  Vector2 v4(v0);
297  v4 /= v0;
299 
300  Vector2 v5(v1);
301  v5 /= v0;
303 
304  Vector2 v6(v1);
305  v6 /= v6;
307 
308  v2 /= v1;
310 
311  END_TEST;
312 }
313 
315 {
316  TestApplication application;
317  Vector2 v0(1.0f, 1.0f);
318  Vector2 v1(3.0f, 3.0f);
319 
320  Vector2 v4(v0);
321  v4 /= 1.f;
323 
324  Vector2 v5(v1);
325  v5 /= 1.f;
327 
328  Vector2 v6(v1);
329  v6 /= 3.f;
331 
332  END_TEST;
333 }
334 
336 {
337  TestApplication application;
338  Vector2 v0(1.0f, 2.0f);
339  Vector2 v1(1.0f, 2.0f);
340 
341  DALI_TEST_CHECK(v0 == v1);
342 
343  END_TEST;
344 }
345 
347 {
348  TestApplication application;
349  Vector2 v0(1.0f, 2.0f);
350  Vector2 v1(1.0f, 2.0f);
351 
352  Vector2 v2 = Vector2(0.0f, 2.0f);
353  DALI_TEST_CHECK(v0 != v2);
354 
355  v2 = Vector2(1.0f, 0.0f);
356  DALI_TEST_CHECK(v0 != v2);
357 
358  v2 = Vector2(1.0f, 77.0f);
359  DALI_TEST_CHECK(v0 != v2);
360 
361  v2 = Vector2(33.0f, 44.0f);
362  DALI_TEST_CHECK(v0 != v2);
363  END_TEST;
364 }
365 
367 {
368  TestApplication application;
369  Vector2 testVector(1.0f, 2.0f);
370 
371  // read array subscripts
372  DALI_TEST_EQUALS( testVector[0], 1.0f, TEST_LOCATION );
373  DALI_TEST_EQUALS( testVector[1], 2.0f, TEST_LOCATION );
374 
375  // write array subscripts/read struct memebers
376  testVector[0] = 3.0f;
377  testVector[1] = 4.0f;
378 
379  DALI_TEST_EQUALS( testVector.x, 3.0f, TEST_LOCATION );
380  DALI_TEST_EQUALS( testVector.y, 4.0f, TEST_LOCATION );
381 
382  END_TEST;
383 }
384 
386 {
387  TestApplication application;
388  Vector2 testVector(1.0f, 2.0f);
389 
390  try
391  {
392  float& w = testVector[4];
393  (void)w; // Suppress unused variable warning
395  }
396  catch (Dali::DaliException& e)
397  {
399  DALI_TEST_ASSERT( e, "Vector element index out of bounds", TEST_LOCATION);
400  }
401 
402  END_TEST;
403 }
404 
406 {
407  TestApplication application;
408  const Vector2 testVector2(3.0f, 4.0f);
409  const float& x = testVector2[0];
410  const float& y = testVector2[1];
411  DALI_TEST_EQUALS( x, 3.0f, TEST_LOCATION );
412  DALI_TEST_EQUALS( y, 4.0f, TEST_LOCATION );
413  END_TEST;
414 }
415 
417 {
418  TestApplication application;
419  const Vector2 testVector2(3.0f, 4.0f);
420 
421  try
422  {
423  const float& w = testVector2[4];
424  (void)w; // Suppress unused variable warning
426  }
427  catch (Dali::DaliException& e)
428  {
430  DALI_TEST_ASSERT( e, "Vector element index out of bounds" , TEST_LOCATION);
431  }
432 
433 
434  END_TEST;
435 }
436 
437 
439 {
440  TestApplication application;
441  Vector2 v(1.0f, 2.0f);
442  DALI_TEST_EQUALS(v.Length(), sqrtf(v.x*v.x + v.y*v.y), 0.001f, TEST_LOCATION);
443 
444  Vector2 v1(0.0f, 0.0f);
445  DALI_TEST_EQUALS(v1.Length(), 0.0f, TEST_LOCATION);
446  END_TEST;
447 }
448 
450 {
451  TestApplication application;
452  Vector2 v(1.0f, 2.0f);
453  DALI_TEST_EQUALS(v.LengthSquared(), v.x*v.x + v.y*v.y, 0.001f, TEST_LOCATION);
454 
455  Vector2 v1(0.0f, 0.0f);
456  DALI_TEST_EQUALS(v1.LengthSquared(), 0.0f, TEST_LOCATION);
457  END_TEST;
458 }
459 
461 {
462  TestApplication application;
463  for (float f=0.0f; f<6.0f; f+=1.0f)
464  {
465  Vector2 v(cosf(f)*10.0f, cosf(f+1.0f)*10.0f);
466  v.Normalize();
467  DALI_TEST_EQUALS(v.LengthSquared(), 1.0f, 0.001f, TEST_LOCATION);
468  }
469  END_TEST;
470 }
471 
473 {
474  TestApplication application;
475  Vector2 v(0.0f, 0.0f);
476  v.Normalize();
477  DALI_TEST_EQUALS(v.LengthSquared(), 0.0f, 0.00001f, TEST_LOCATION);
478  END_TEST;
479 }
480 
482 {
483  TestApplication application;
484  tet_infoline("Testing Dali::Vector2::Clamp( const Vector2& v, const Vector2& min, const Vector2& max )");
485 
486  Vector2 v0(2.0f, 0.8f);
487  Vector2 v1(-1.0f, 2.0f);
488  Vector2 v2(10.0f, 5.0f);
489  Vector2 v3(8.0f, 10.0f);
490  Vector2 v4(4.9f, 5.1f);
491  Vector2 min(1.0f, 4.0f);
492  Vector2 max(9.0f, 6.0f);
493 
494  v0.Clamp( min, max );
495  v1.Clamp( min, max );
496  v2.Clamp( min, max );
497  v3.Clamp( min, max );
498  v4.Clamp( min, max );
499 
500  DALI_TEST_EQUALS( v0, Vector2(2.0f, 4.0f), 0.01f, TEST_LOCATION );
501  DALI_TEST_EQUALS( v1, Vector2(1.0f, 4.0f), 0.01f, TEST_LOCATION );
502  DALI_TEST_EQUALS( v2, Vector2(9.0f, 5.0f), 0.01f, TEST_LOCATION );
503  DALI_TEST_EQUALS( v3, Vector2(8.0f, 6.0f), 0.01f, TEST_LOCATION );
504  DALI_TEST_EQUALS( v4, Vector2(4.9f, 5.1f), 0.01f, TEST_LOCATION );
505  END_TEST;
506 }
507 
509 {
510  TestApplication application;
511  Vector2 vec2(1.f,1.f);
512  float* p = vec2.AsFloat();
513 
514  DALI_TEST_EQUALS( p[0], 1.f, 0.01f, TEST_LOCATION );
515  DALI_TEST_EQUALS( p[1], 1.f, 0.01f, TEST_LOCATION );
516 
517  p[0] = 2.f;
518  p[1] = 2.f;
519 
520  DALI_TEST_EQUALS( p[0], 2.f, 0.01f, TEST_LOCATION );
521  DALI_TEST_EQUALS( p[1], 2.f, 0.01f, TEST_LOCATION );
522 
523  END_TEST;
524 }
525 
527 {
528  TestApplication application;
529  Vector2 vec2(1.f,1.f);
530  const float* p = vec2.AsFloat();
531 
532  DALI_TEST_EQUALS( p[0], 1.f, 0.01f, TEST_LOCATION );
533  DALI_TEST_EQUALS( p[1], 1.f, 0.01f, TEST_LOCATION );
534 
535  END_TEST;
536 }
537 
539 {
540  TestApplication application;
541  Vector2 vec2(1.f,1.f);
542 
543  DALI_TEST_EQUALS( vec2.x, 1.f, 0.01f, TEST_LOCATION );
544  DALI_TEST_EQUALS( vec2.width, 1.f, 0.01f, TEST_LOCATION );
545 
546  END_TEST;
547 }
548 
550 {
551  TestApplication application;
552  Vector2 vec2(1.f,1.f);
553 
554  DALI_TEST_EQUALS( vec2.y, 1.f, 0.01f, TEST_LOCATION );
555  DALI_TEST_EQUALS( vec2.height, 1.f, 0.01f, TEST_LOCATION );
556 
557  END_TEST;
558 }
559 
561 {
562  TestApplication application;
563  std::ostringstream oss;
564 
565  Vector2 vector(1, 2);
566 
567  oss << vector;
568 
569  std::string expectedOutput = "[1, 2]";
570 
571  DALI_TEST_EQUALS( oss.str(), expectedOutput, TEST_LOCATION);
572  END_TEST;
573 }
574 
575 
577 {
578  TestApplication application;
579  Vector2 v0(2.0f, 1.0f);
580  Vector2 v1(1.0f, 2.0f);
581 
582  DALI_TEST_EQUALS(Max(v0, v1), Vector2(2.0f, 2.0f), 0.01f, TEST_LOCATION);
583  END_TEST;
584 }
585 
587 {
588  TestApplication application;
589  Vector2 v0(2.0f, 1.0f);
590  Vector2 v1(1.0f, 2.0f);
591 
592  DALI_TEST_EQUALS(Min(v0, v1), Vector2(1.0f, 1.0f), 0.01f, TEST_LOCATION);
593  END_TEST;
594 }
595 
597 {
598  TestApplication application;
599  tet_infoline("Testing Dali::Vector2::Clamp( const Vector2& v, const float& min, const float& max )");
600 
601  Vector2 v0(2.0f, 0.8f);
602  Vector2 v1(-1.0f, 2.0f);
603 
604  DALI_TEST_EQUALS( Clamp( v0, 0.9f, 1.1f ), Vector2(1.1f, 0.9f), 0.01f, TEST_LOCATION );
605  DALI_TEST_EQUALS( Clamp( v1, 1.0f, 1.0f ), Vector2(1.0f, 1.0f), 0.01f, TEST_LOCATION );
606  DALI_TEST_EQUALS( Clamp( v1, 0.0f, 3.0f ), Vector2(0.0f, 2.0f), 0.01f, TEST_LOCATION );
607  END_TEST;
608 }
609 
Dali Docs Home
Read more about Dali