2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <cmath> // isfinite
23 #include <dali/public-api/dali-core.h>
24 #include <dali-test-suite-utils.h>
28 void utc_dali_vector2_startup(void)
30 test_return_value = TET_UNDEF;
33 void utc_dali_vector2_cleanup(void)
35 test_return_value = TET_PASS;
38 int UtcDaliVector2Constructor01P(void)
40 TestApplication application;
42 DALI_TEST_EQUALS(vec2.x, 0.0f, 0.001, TEST_LOCATION);
43 DALI_TEST_EQUALS(vec2.y, 0.0f, 0.001, TEST_LOCATION);
47 int UtcDaliVector2Constructor02P(void)
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);
56 int UtcDaliVector2Constructor03P(void)
58 TestApplication application;
59 float array[] = {1.f,1.f};
61 DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
62 DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
66 int UtcDaliVector2Constructor04P(void)
68 TestApplication application;
69 Vector3 vec3(1.f,1.f,1.f);
71 DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
72 DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
76 int UtcDaliVector2Constructor05P(void)
78 TestApplication application;
79 Vector4 vec4(1.f,1.f,1.f,1.f);
81 DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
82 DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
86 int UtcDaliVector2CopyConstructor01P(void)
88 TestApplication application;
89 float array[] = {1.f,1.f};
92 DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
93 DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
97 int UtcDaliVector2CopyConstructor02P(void)
99 TestApplication application;
100 Vector3 vec3(1.f,1.f,1.f);
103 DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
104 DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
108 int UtcDaliVector2CopyConstructor03P(void)
110 TestApplication application;
111 Vector4 vec4(1.f,1.f,1.f,1.f);
114 DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
115 DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
119 int UtcDaliVector2AssignP(void)
121 TestApplication application;
122 Vector2 v1(10.0f, 20.0f);
123 Vector2 r0(11.0f, 22.0f);
126 DALI_TEST_EQUALS(v1, r0, TEST_LOCATION);
130 int UtcDaliVector2AssignP02(void)
132 TestApplication application;
133 Vector2 v1(10.0f, 20.0f);
134 Vector4 r0(11.0f, 22.0f, 33.f, 44.f);
137 DALI_TEST_EQUALS(v1, Vector2(r0.x, r0.y), TEST_LOCATION);
141 int UtcDaliVector2AssignP03(void)
143 TestApplication application;
144 Vector2 v1(10.0f, 20.0f);
145 Vector3 r0(11.0f, 22.0f, 33.f);
148 DALI_TEST_EQUALS(v1, Vector2(r0.x, r0.y), TEST_LOCATION);
152 int UtcDaliVector2AddP(void)
154 TestApplication application;
155 Vector2 v0(1.0f, 2.0f);
156 Vector2 v1(10.0f, 20.0f);
157 Vector2 r0(11.0f, 22.0f);
160 DALI_TEST_EQUALS(v2, r0, TEST_LOCATION);
164 int UtcDaliVector2AddAssignP(void)
166 TestApplication application;
167 Vector2 v0(1.0f, 2.0f);
168 Vector2 v1(10.0f, 20.0f);
169 Vector2 r0(11.0f, 22.0f);
172 DALI_TEST_EQUALS(v0, r0, TEST_LOCATION);
176 int UtcDaliVector2SubtractP(void)
178 TestApplication application;
179 Vector2 v0(11.0f, 22.0f);
180 Vector2 v1(10.0f, 20.0f);
181 Vector2 r0(1.0f, 2.0f);
184 DALI_TEST_EQUALS(v2, r0, TEST_LOCATION);
189 int UtcDaliVector2SubtractAssignP(void)
191 TestApplication application;
192 Vector2 v0(11.0f, 22.0f);
193 Vector2 v1(10.0f, 20.0f);
194 Vector2 r0(1.0f, 2.0f);
197 DALI_TEST_EQUALS(v0, r0, TEST_LOCATION);
201 int UtcDaliVector2NegateP(void)
203 TestApplication application;
204 Vector2 v1(10.0f, 20.0f);
205 Vector2 r0(-10.0f, -20.0f);
208 DALI_TEST_EQUALS(v2, r0, TEST_LOCATION);
212 int UtcDaliVector2Multiply01P(void)
214 TestApplication application;
215 Vector2 v0(2.0f, 3.0f);
216 Vector2 v1(10.0f, 20.0f);
217 Vector2 r0(20.0f, 60.0f);
219 Vector2 v2 = v0 * v1;
220 DALI_TEST_EQUALS(v2, r0, TEST_LOCATION);
225 int UtcDaliVector2Multiply02P(void)
227 TestApplication application;
228 Vector2 v0(2.0f, 3.0f);
229 Vector2 r0(20.0f, 30.0f);
231 Vector2 v2 = v0 * 10.f;
232 DALI_TEST_EQUALS(v2, r0, TEST_LOCATION);
237 int UtcDaliVector2MultiplyAssign01P(void)
239 TestApplication application;
240 Vector2 v0(2.0f, 3.0f);
241 Vector2 v1(10.0f, 20.0f);
242 Vector2 r0(20.0f, 60.0f);
245 DALI_TEST_EQUALS(v0, r0, TEST_LOCATION);
249 int UtcDaliVector2MultiplyAssign02P(void)
251 TestApplication application;
252 Vector2 v0(2.0f, 3.0f);
253 Vector2 r0(20.0f, 30.0f);
256 DALI_TEST_EQUALS(v0, r0, TEST_LOCATION);
260 int UtcDaliVector2Divide01P(void)
262 TestApplication application;
263 Vector2 v0(1.0f, 1.0f);
264 Vector2 v1(2.0f, 3.0f);
265 Vector2 v2(4.0f, 9.0f);
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);
275 int UtcDaliVector2Divide02P(void)
277 TestApplication application;
278 Vector2 v0(1.0f, 1.0f);
279 Vector2 v1(3.0f, 3.0f);
280 Vector2 v2(9.0f, 9.0f);
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);
289 int UtcDaliVector2DivideAssign01P(void)
291 TestApplication application;
292 Vector2 v0(1.0f, 1.0f);
293 Vector2 v1(2.0f, 3.0f);
294 Vector2 v2(4.0f, 9.0f);
298 DALI_TEST_EQUALS(v4, v0, TEST_LOCATION);
302 DALI_TEST_EQUALS(v5, v1, TEST_LOCATION);
306 DALI_TEST_EQUALS(v6, v0, TEST_LOCATION);
309 DALI_TEST_EQUALS(v2, v1, TEST_LOCATION);
314 int UtcDaliVector2DivideAssign02P(void)
316 TestApplication application;
317 Vector2 v0(1.0f, 1.0f);
318 Vector2 v1(3.0f, 3.0f);
322 DALI_TEST_EQUALS(v4, v0, TEST_LOCATION);
326 DALI_TEST_EQUALS(v5, v1, TEST_LOCATION);
330 DALI_TEST_EQUALS(v6, v0, TEST_LOCATION);
335 int UtcDaliVector2EqualsP(void)
337 TestApplication application;
338 Vector2 v0(1.0f, 2.0f);
339 Vector2 v1(1.0f, 2.0f);
341 DALI_TEST_CHECK(v0 == v1);
346 int UtcDaliVector2NotEqualsP(void)
348 TestApplication application;
349 Vector2 v0(1.0f, 2.0f);
350 Vector2 v1(1.0f, 2.0f);
352 Vector2 v2 = Vector2(0.0f, 2.0f);
353 DALI_TEST_CHECK(v0 != v2);
355 v2 = Vector2(1.0f, 0.0f);
356 DALI_TEST_CHECK(v0 != v2);
358 v2 = Vector2(1.0f, 77.0f);
359 DALI_TEST_CHECK(v0 != v2);
361 v2 = Vector2(33.0f, 44.0f);
362 DALI_TEST_CHECK(v0 != v2);
366 int UtcDaliVector2OperatorSubscriptP(void)
368 TestApplication application;
369 Vector2 testVector(1.0f, 2.0f);
371 // read array subscripts
372 DALI_TEST_EQUALS( testVector[0], 1.0f, TEST_LOCATION );
373 DALI_TEST_EQUALS( testVector[1], 2.0f, TEST_LOCATION );
375 // write array subscripts/read struct memebers
376 testVector[0] = 3.0f;
377 testVector[1] = 4.0f;
379 DALI_TEST_EQUALS( testVector.x, 3.0f, TEST_LOCATION );
380 DALI_TEST_EQUALS( testVector.y, 4.0f, TEST_LOCATION );
385 int UtcDaliVector2OperatorSubscriptN(void)
387 TestApplication application;
388 Vector2 testVector(1.0f, 2.0f);
392 float& w = testVector[4];
393 tet_result(TET_FAIL);
395 catch (Dali::DaliException& e)
397 DALI_TEST_PRINT_ASSERT( e );
398 DALI_TEST_ASSERT( e, "Vector element index out of bounds", TEST_LOCATION);
404 int UtcDaliVector2OperatorConstSubscriptP(void)
406 TestApplication application;
407 const Vector2 testVector2(3.0f, 4.0f);
408 const float& x = testVector2[0];
409 const float& y = testVector2[1];
410 DALI_TEST_EQUALS( x, 3.0f, TEST_LOCATION );
411 DALI_TEST_EQUALS( y, 4.0f, TEST_LOCATION );
415 int UtcDaliVector2OperatorConstSubscriptN(void)
417 TestApplication application;
418 const Vector2 testVector2(3.0f, 4.0f);
422 const float& w = testVector2[4];
423 tet_result(TET_FAIL);
425 catch (Dali::DaliException& e)
427 DALI_TEST_PRINT_ASSERT( e );
428 DALI_TEST_ASSERT( e, "Vector element index out of bounds" , TEST_LOCATION);
436 int UtcDaliVector2LengthP(void)
438 TestApplication application;
439 Vector2 v(1.0f, 2.0f);
440 DALI_TEST_EQUALS(v.Length(), sqrtf(v.x*v.x + v.y*v.y), 0.001f, TEST_LOCATION);
442 Vector2 v1(0.0f, 0.0f);
443 DALI_TEST_EQUALS(v1.Length(), 0.0f, TEST_LOCATION);
447 int UtcDaliVector2LengthSquaredP(void)
449 TestApplication application;
450 Vector2 v(1.0f, 2.0f);
451 DALI_TEST_EQUALS(v.LengthSquared(), v.x*v.x + v.y*v.y, 0.001f, TEST_LOCATION);
453 Vector2 v1(0.0f, 0.0f);
454 DALI_TEST_EQUALS(v1.LengthSquared(), 0.0f, TEST_LOCATION);
458 int UtcDaliVector2NormalizeP(void)
460 TestApplication application;
461 for (float f=0.0f; f<6.0f; f+=1.0f)
463 Vector2 v(cosf(f)*10.0f, cosf(f+1.0f)*10.0f);
465 DALI_TEST_EQUALS(v.LengthSquared(), 1.0f, 0.001f, TEST_LOCATION);
470 int UtcDaliVector2NormalizeN(void)
472 TestApplication application;
473 Vector2 v(0.0f, 0.0f);
475 DALI_TEST_EQUALS(v.LengthSquared(), 0.0f, 0.00001f, TEST_LOCATION);
479 int UtcDaliVector2ClampVector2P(void)
481 TestApplication application;
482 tet_infoline("Testing Dali::Vector2::Clamp( const Vector2& v, const Vector2& min, const Vector2& max )");
484 Vector2 v0(2.0f, 0.8f);
485 Vector2 v1(-1.0f, 2.0f);
486 Vector2 v2(10.0f, 5.0f);
487 Vector2 v3(8.0f, 10.0f);
488 Vector2 v4(4.9f, 5.1f);
489 Vector2 min(1.0f, 4.0f);
490 Vector2 max(9.0f, 6.0f);
492 v0.Clamp( min, max );
493 v1.Clamp( min, max );
494 v2.Clamp( min, max );
495 v3.Clamp( min, max );
496 v4.Clamp( min, max );
498 DALI_TEST_EQUALS( v0, Vector2(2.0f, 4.0f), 0.01f, TEST_LOCATION );
499 DALI_TEST_EQUALS( v1, Vector2(1.0f, 4.0f), 0.01f, TEST_LOCATION );
500 DALI_TEST_EQUALS( v2, Vector2(9.0f, 5.0f), 0.01f, TEST_LOCATION );
501 DALI_TEST_EQUALS( v3, Vector2(8.0f, 6.0f), 0.01f, TEST_LOCATION );
502 DALI_TEST_EQUALS( v4, Vector2(4.9f, 5.1f), 0.01f, TEST_LOCATION );
506 int UtcDaliVector2AsFloatP(void)
508 TestApplication application;
509 Vector2 vec2(1.f,1.f);
510 float* p = vec2.AsFloat();
512 DALI_TEST_EQUALS( p[0], 1.f, 0.01f, TEST_LOCATION );
513 DALI_TEST_EQUALS( p[1], 1.f, 0.01f, TEST_LOCATION );
518 DALI_TEST_EQUALS( p[0], 2.f, 0.01f, TEST_LOCATION );
519 DALI_TEST_EQUALS( p[1], 2.f, 0.01f, TEST_LOCATION );
524 int UtcDaliVector2ConstAsFloatP(void)
526 TestApplication application;
527 Vector2 vec2(1.f,1.f);
528 const float* p = vec2.AsFloat();
530 DALI_TEST_EQUALS( p[0], 1.f, 0.01f, TEST_LOCATION );
531 DALI_TEST_EQUALS( p[1], 1.f, 0.01f, TEST_LOCATION );
536 int UtcDaliVector2XWidthP(void)
538 TestApplication application;
539 Vector2 vec2(1.f,1.f);
541 DALI_TEST_EQUALS( vec2.x, 1.f, 0.01f, TEST_LOCATION );
542 DALI_TEST_EQUALS( vec2.width, 1.f, 0.01f, TEST_LOCATION );
547 int UtcDaliVector2YHeightP(void)
549 TestApplication application;
550 Vector2 vec2(1.f,1.f);
552 DALI_TEST_EQUALS( vec2.y, 1.f, 0.01f, TEST_LOCATION );
553 DALI_TEST_EQUALS( vec2.height, 1.f, 0.01f, TEST_LOCATION );
558 int UtcDaliVector2OStreamOperatorP(void)
560 TestApplication application;
561 std::ostringstream oss;
563 Vector2 vector(1, 2);
567 std::string expectedOutput = "[1, 2]";
569 DALI_TEST_EQUALS( oss.str(), expectedOutput, TEST_LOCATION);
574 int UtcDaliVector2MaxP(void)
576 TestApplication application;
577 Vector2 v0(2.0f, 1.0f);
578 Vector2 v1(1.0f, 2.0f);
580 DALI_TEST_EQUALS(Max(v0, v1), Vector2(2.0f, 2.0f), 0.01f, TEST_LOCATION);
584 int UtcDaliVector2MinP(void)
586 TestApplication application;
587 Vector2 v0(2.0f, 1.0f);
588 Vector2 v1(1.0f, 2.0f);
590 DALI_TEST_EQUALS(Min(v0, v1), Vector2(1.0f, 1.0f), 0.01f, TEST_LOCATION);
594 int UtcDaliVector2ClampP(void)
596 TestApplication application;
597 tet_infoline("Testing Dali::Vector2::Clamp( const Vector2& v, const float& min, const float& max )");
599 Vector2 v0(2.0f, 0.8f);
600 Vector2 v1(-1.0f, 2.0f);
602 DALI_TEST_EQUALS( Clamp( v0, 0.9f, 1.1f ), Vector2(1.1f, 0.9f), 0.01f, TEST_LOCATION );
603 DALI_TEST_EQUALS( Clamp( v1, 1.0f, 1.0f ), Vector2(1.0f, 1.0f), 0.01f, TEST_LOCATION );
604 DALI_TEST_EQUALS( Clamp( v1, 0.0f, 3.0f ), Vector2(0.0f, 2.0f), 0.01f, TEST_LOCATION );