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 UtcDaliVector2ConstructorUint16PairP(void)
88 TestApplication application;
89 Uint16Pair pair( 20, 30 );
91 DALI_TEST_EQUALS(vec2.x, 20.0f, 0.001, TEST_LOCATION);
92 DALI_TEST_EQUALS(vec2.y, 30.0f, 0.001, TEST_LOCATION);
96 int UtcDaliVector2CopyConstructor01P(void)
98 TestApplication application;
99 float array[] = {1.f,1.f};
102 DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
103 DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
107 int UtcDaliVector2CopyConstructor02P(void)
109 TestApplication application;
110 Vector3 vec3(1.f,1.f,1.f);
113 DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
114 DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
118 int UtcDaliVector2CopyConstructor03P(void)
120 TestApplication application;
121 Vector4 vec4(1.f,1.f,1.f,1.f);
124 DALI_TEST_EQUALS(vec2.x, 1.0f, 0.001, TEST_LOCATION);
125 DALI_TEST_EQUALS(vec2.y, 1.0f, 0.001, TEST_LOCATION);
129 int UtcDaliVector2AssignP(void)
131 TestApplication application;
132 Vector2 v1(10.0f, 20.0f);
133 Vector2 r0(11.0f, 22.0f);
136 DALI_TEST_EQUALS(v1, r0, TEST_LOCATION);
140 int UtcDaliVector2AssignP02(void)
142 TestApplication application;
143 Vector2 v1(10.0f, 20.0f);
144 Vector4 r0(11.0f, 22.0f, 33.f, 44.f);
147 DALI_TEST_EQUALS(v1, Vector2(r0.x, r0.y), TEST_LOCATION);
151 int UtcDaliVector2AssignP03(void)
153 TestApplication application;
154 Vector2 v1(10.0f, 20.0f);
155 Vector3 r0(11.0f, 22.0f, 33.f);
158 DALI_TEST_EQUALS(v1, Vector2(r0.x, r0.y), TEST_LOCATION);
162 int UtcDaliVector2AddP(void)
164 TestApplication application;
165 Vector2 v0(1.0f, 2.0f);
166 Vector2 v1(10.0f, 20.0f);
167 Vector2 r0(11.0f, 22.0f);
170 DALI_TEST_EQUALS(v2, r0, TEST_LOCATION);
174 int UtcDaliVector2AddAssignP(void)
176 TestApplication application;
177 Vector2 v0(1.0f, 2.0f);
178 Vector2 v1(10.0f, 20.0f);
179 Vector2 r0(11.0f, 22.0f);
182 DALI_TEST_EQUALS(v0, r0, TEST_LOCATION);
186 int UtcDaliVector2SubtractP(void)
188 TestApplication application;
189 Vector2 v0(11.0f, 22.0f);
190 Vector2 v1(10.0f, 20.0f);
191 Vector2 r0(1.0f, 2.0f);
194 DALI_TEST_EQUALS(v2, r0, TEST_LOCATION);
199 int UtcDaliVector2SubtractAssignP(void)
201 TestApplication application;
202 Vector2 v0(11.0f, 22.0f);
203 Vector2 v1(10.0f, 20.0f);
204 Vector2 r0(1.0f, 2.0f);
207 DALI_TEST_EQUALS(v0, r0, TEST_LOCATION);
211 int UtcDaliVector2NegateP(void)
213 TestApplication application;
214 Vector2 v1(10.0f, 20.0f);
215 Vector2 r0(-10.0f, -20.0f);
218 DALI_TEST_EQUALS(v2, r0, TEST_LOCATION);
222 int UtcDaliVector2Multiply01P(void)
224 TestApplication application;
225 Vector2 v0(2.0f, 3.0f);
226 Vector2 v1(10.0f, 20.0f);
227 Vector2 r0(20.0f, 60.0f);
229 Vector2 v2 = v0 * v1;
230 DALI_TEST_EQUALS(v2, r0, TEST_LOCATION);
235 int UtcDaliVector2Multiply02P(void)
237 TestApplication application;
238 Vector2 v0(2.0f, 3.0f);
239 Vector2 r0(20.0f, 30.0f);
241 Vector2 v2 = v0 * 10.f;
242 DALI_TEST_EQUALS(v2, r0, TEST_LOCATION);
247 int UtcDaliVector2MultiplyAssign01P(void)
249 TestApplication application;
250 Vector2 v0(2.0f, 3.0f);
251 Vector2 v1(10.0f, 20.0f);
252 Vector2 r0(20.0f, 60.0f);
255 DALI_TEST_EQUALS(v0, r0, TEST_LOCATION);
259 int UtcDaliVector2MultiplyAssign02P(void)
261 TestApplication application;
262 Vector2 v0(2.0f, 3.0f);
263 Vector2 r0(20.0f, 30.0f);
266 DALI_TEST_EQUALS(v0, r0, TEST_LOCATION);
270 int UtcDaliVector2Divide01P(void)
272 TestApplication application;
273 Vector2 v0(1.0f, 1.0f);
274 Vector2 v1(2.0f, 3.0f);
275 Vector2 v2(4.0f, 9.0f);
277 DALI_TEST_EQUALS( v0/v0, v0, TEST_LOCATION);
278 DALI_TEST_EQUALS( v1/v0, v1, TEST_LOCATION);
279 DALI_TEST_EQUALS( v1/v1, v0, TEST_LOCATION);
280 DALI_TEST_EQUALS( v2/v1, v1, TEST_LOCATION);
285 int UtcDaliVector2Divide02P(void)
287 TestApplication application;
288 Vector2 v0(1.0f, 1.0f);
289 Vector2 v1(3.0f, 3.0f);
290 Vector2 v2(9.0f, 9.0f);
292 DALI_TEST_EQUALS( v0/1.f, v0, TEST_LOCATION);
293 DALI_TEST_EQUALS( v1/1.f, v1, TEST_LOCATION);
294 DALI_TEST_EQUALS( v1/3.f, v0, TEST_LOCATION);
299 int UtcDaliVector2DivideAssign01P(void)
301 TestApplication application;
302 Vector2 v0(1.0f, 1.0f);
303 Vector2 v1(2.0f, 3.0f);
304 Vector2 v2(4.0f, 9.0f);
308 DALI_TEST_EQUALS(v4, v0, TEST_LOCATION);
312 DALI_TEST_EQUALS(v5, v1, TEST_LOCATION);
316 DALI_TEST_EQUALS(v6, v0, TEST_LOCATION);
319 DALI_TEST_EQUALS(v2, v1, TEST_LOCATION);
324 int UtcDaliVector2DivideAssign02P(void)
326 TestApplication application;
327 Vector2 v0(1.0f, 1.0f);
328 Vector2 v1(3.0f, 3.0f);
332 DALI_TEST_EQUALS(v4, v0, TEST_LOCATION);
336 DALI_TEST_EQUALS(v5, v1, TEST_LOCATION);
340 DALI_TEST_EQUALS(v6, v0, TEST_LOCATION);
345 int UtcDaliVector2EqualsP(void)
347 TestApplication application;
348 Vector2 v0(1.0f, 2.0f);
349 Vector2 v1(1.0f, 2.0f);
351 DALI_TEST_CHECK(v0 == v1);
356 int UtcDaliVector2NotEqualsP(void)
358 TestApplication application;
359 Vector2 v0(1.0f, 2.0f);
360 Vector2 v1(1.0f, 2.0f);
362 Vector2 v2 = Vector2(0.0f, 2.0f);
363 DALI_TEST_CHECK(v0 != v2);
365 v2 = Vector2(1.0f, 0.0f);
366 DALI_TEST_CHECK(v0 != v2);
368 v2 = Vector2(1.0f, 77.0f);
369 DALI_TEST_CHECK(v0 != v2);
371 v2 = Vector2(33.0f, 44.0f);
372 DALI_TEST_CHECK(v0 != v2);
376 int UtcDaliVector2OperatorSubscriptP(void)
378 TestApplication application;
379 Vector2 testVector(1.0f, 2.0f);
381 // read array subscripts
382 DALI_TEST_EQUALS( testVector[0], 1.0f, TEST_LOCATION );
383 DALI_TEST_EQUALS( testVector[1], 2.0f, TEST_LOCATION );
385 // write array subscripts/read struct memebers
386 testVector[0] = 3.0f;
387 testVector[1] = 4.0f;
389 DALI_TEST_EQUALS( testVector.x, 3.0f, TEST_LOCATION );
390 DALI_TEST_EQUALS( testVector.y, 4.0f, TEST_LOCATION );
395 int UtcDaliVector2OperatorSubscriptN(void)
397 TestApplication application;
398 Vector2 testVector(1.0f, 2.0f);
402 float& w = testVector[4];
403 (void)w; // Suppress unused variable warning
404 tet_result(TET_FAIL);
406 catch (Dali::DaliException& e)
408 DALI_TEST_PRINT_ASSERT( e );
409 DALI_TEST_ASSERT( e, "Vector element index out of bounds", TEST_LOCATION);
415 int UtcDaliVector2OperatorConstSubscriptP(void)
417 TestApplication application;
418 const Vector2 testVector2(3.0f, 4.0f);
419 const float& x = testVector2[0];
420 const float& y = testVector2[1];
421 DALI_TEST_EQUALS( x, 3.0f, TEST_LOCATION );
422 DALI_TEST_EQUALS( y, 4.0f, TEST_LOCATION );
426 int UtcDaliVector2OperatorConstSubscriptN(void)
428 TestApplication application;
429 const Vector2 testVector2(3.0f, 4.0f);
433 const float& w = testVector2[4];
434 (void)w; // Suppress unused variable warning
435 tet_result(TET_FAIL);
437 catch (Dali::DaliException& e)
439 DALI_TEST_PRINT_ASSERT( e );
440 DALI_TEST_ASSERT( e, "Vector element index out of bounds" , TEST_LOCATION);
448 int UtcDaliVector2LengthP(void)
450 TestApplication application;
451 Vector2 v(1.0f, 2.0f);
452 DALI_TEST_EQUALS(v.Length(), sqrtf(v.x*v.x + v.y*v.y), 0.001f, TEST_LOCATION);
454 Vector2 v1(0.0f, 0.0f);
455 DALI_TEST_EQUALS(v1.Length(), 0.0f, TEST_LOCATION);
459 int UtcDaliVector2LengthSquaredP(void)
461 TestApplication application;
462 Vector2 v(1.0f, 2.0f);
463 DALI_TEST_EQUALS(v.LengthSquared(), v.x*v.x + v.y*v.y, 0.001f, TEST_LOCATION);
465 Vector2 v1(0.0f, 0.0f);
466 DALI_TEST_EQUALS(v1.LengthSquared(), 0.0f, TEST_LOCATION);
470 int UtcDaliVector2NormalizeP(void)
472 TestApplication application;
473 for (float f=0.0f; f<6.0f; f+=1.0f)
475 Vector2 v(cosf(f)*10.0f, cosf(f+1.0f)*10.0f);
477 DALI_TEST_EQUALS(v.LengthSquared(), 1.0f, 0.001f, TEST_LOCATION);
482 int UtcDaliVector2NormalizeN(void)
484 TestApplication application;
485 Vector2 v(0.0f, 0.0f);
487 DALI_TEST_EQUALS(v.LengthSquared(), 0.0f, 0.00001f, TEST_LOCATION);
491 int UtcDaliVector2ClampVector2P(void)
493 TestApplication application;
494 tet_infoline("Testing Dali::Vector2::Clamp( const Vector2& v, const Vector2& min, const Vector2& max )");
496 Vector2 v0(2.0f, 0.8f);
497 Vector2 v1(-1.0f, 2.0f);
498 Vector2 v2(10.0f, 5.0f);
499 Vector2 v3(8.0f, 10.0f);
500 Vector2 v4(4.9f, 5.1f);
501 Vector2 min(1.0f, 4.0f);
502 Vector2 max(9.0f, 6.0f);
504 v0.Clamp( min, max );
505 v1.Clamp( min, max );
506 v2.Clamp( min, max );
507 v3.Clamp( min, max );
508 v4.Clamp( min, max );
510 DALI_TEST_EQUALS( v0, Vector2(2.0f, 4.0f), 0.01f, TEST_LOCATION );
511 DALI_TEST_EQUALS( v1, Vector2(1.0f, 4.0f), 0.01f, TEST_LOCATION );
512 DALI_TEST_EQUALS( v2, Vector2(9.0f, 5.0f), 0.01f, TEST_LOCATION );
513 DALI_TEST_EQUALS( v3, Vector2(8.0f, 6.0f), 0.01f, TEST_LOCATION );
514 DALI_TEST_EQUALS( v4, Vector2(4.9f, 5.1f), 0.01f, TEST_LOCATION );
518 int UtcDaliVector2AsFloatP(void)
520 TestApplication application;
521 Vector2 vec2(1.f,1.f);
522 float* p = vec2.AsFloat();
524 DALI_TEST_EQUALS( p[0], 1.f, 0.01f, TEST_LOCATION );
525 DALI_TEST_EQUALS( p[1], 1.f, 0.01f, TEST_LOCATION );
530 DALI_TEST_EQUALS( p[0], 2.f, 0.01f, TEST_LOCATION );
531 DALI_TEST_EQUALS( p[1], 2.f, 0.01f, TEST_LOCATION );
536 int UtcDaliVector2ConstAsFloatP(void)
538 TestApplication application;
539 Vector2 vec2(1.f,1.f);
540 const float* p = vec2.AsFloat();
542 DALI_TEST_EQUALS( p[0], 1.f, 0.01f, TEST_LOCATION );
543 DALI_TEST_EQUALS( p[1], 1.f, 0.01f, TEST_LOCATION );
548 int UtcDaliVector2XWidthP(void)
550 TestApplication application;
551 Vector2 vec2(1.f,1.f);
553 DALI_TEST_EQUALS( vec2.x, 1.f, 0.01f, TEST_LOCATION );
554 DALI_TEST_EQUALS( vec2.width, 1.f, 0.01f, TEST_LOCATION );
559 int UtcDaliVector2YHeightP(void)
561 TestApplication application;
562 Vector2 vec2(1.f,1.f);
564 DALI_TEST_EQUALS( vec2.y, 1.f, 0.01f, TEST_LOCATION );
565 DALI_TEST_EQUALS( vec2.height, 1.f, 0.01f, TEST_LOCATION );
570 int UtcDaliVector2OStreamOperatorP(void)
572 TestApplication application;
573 std::ostringstream oss;
575 Vector2 vector(1, 2);
579 std::string expectedOutput = "[1, 2]";
581 DALI_TEST_EQUALS( oss.str(), expectedOutput, TEST_LOCATION);
586 int UtcDaliVector2MaxP(void)
588 TestApplication application;
589 Vector2 v0(2.0f, 1.0f);
590 Vector2 v1(1.0f, 2.0f);
592 DALI_TEST_EQUALS(Max(v0, v1), Vector2(2.0f, 2.0f), 0.01f, TEST_LOCATION);
596 int UtcDaliVector2MinP(void)
598 TestApplication application;
599 Vector2 v0(2.0f, 1.0f);
600 Vector2 v1(1.0f, 2.0f);
602 DALI_TEST_EQUALS(Min(v0, v1), Vector2(1.0f, 1.0f), 0.01f, TEST_LOCATION);
606 int UtcDaliVector2ClampP(void)
608 TestApplication application;
609 tet_infoline("Testing Dali::Vector2::Clamp( const Vector2& v, const float& min, const float& max )");
611 Vector2 v0(2.0f, 0.8f);
612 Vector2 v1(-1.0f, 2.0f);
614 DALI_TEST_EQUALS( Clamp( v0, 0.9f, 1.1f ), Vector2(1.1f, 0.9f), 0.01f, TEST_LOCATION );
615 DALI_TEST_EQUALS( Clamp( v1, 1.0f, 1.0f ), Vector2(1.0f, 1.0f), 0.01f, TEST_LOCATION );
616 DALI_TEST_EQUALS( Clamp( v1, 0.0f, 3.0f ), Vector2(0.0f, 2.0f), 0.01f, TEST_LOCATION );