1 #ifndef DALI_TEST_SUITE_UTILS_H
2 #define DALI_TEST_SUITE_UTILS_H
5 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
29 #include <dali/public-api/dali-core.h>
30 #include <test-compare-types.h>
32 void tet_infoline(const char*str);
33 void tet_printf(const char *format, ...);
35 #include "test-application.h"
36 #include "test-actor-utils.h"
37 #include "test-gesture-generator.h"
41 #define STRINGIZE_I(text) #text
42 #define STRINGIZE(text) STRINGIZE_I(text)
45 * Inspired by https://stackoverflow.com/questions/1706346/file-macro-manipulation-handling-at-compile-time
46 * answer by Chetan Reddy
48 constexpr int32_t basenameIndex( const char * const path, const int32_t index = 0, const int32_t slashIndex = -1 )
51 ? ( path[ index ] == '/'
52 ? basenameIndex( path, index + 1, index )
53 : basenameIndex( path, index + 1, slashIndex ) )
57 #define __FILELINE__ ( { static const int32_t basenameIdx = basenameIndex( __FILE__ ); \
58 static_assert (basenameIdx >= 0, "compile-time basename" ); \
59 __FILE__ ":" STRINGIZE(__LINE__) + basenameIdx ; } )
61 #define TEST_LOCATION __FILELINE__
62 #define TEST_INNER_LOCATION(x) ( std::string(x) + " (" + STRINGIZE(__LINE__) + ")" ).c_str()
68 extern int32_t test_return_value;
70 void tet_result(int32_t value);
73 return ((test_return_value>0)?1:0)
75 void tet_infoline(const char* str);
76 void tet_printf(const char *format, ...);
79 * DALI_TEST_CHECK is a wrapper for tet_result.
80 * If the condition evaluates to false, the test is stopped.
81 * @param[in] The boolean expression to check
83 #define DALI_TEST_CHECK(condition) \
86 tet_result(TET_PASS); \
90 fprintf(stderr, "Test failed in %s, condition: %s\n", __FILELINE__, #condition ); \
91 tet_result(TET_FAIL); \
96 bool operator==(TimePeriod a, TimePeriod b);
97 std::ostream& operator<<( std::ostream& ostream, TimePeriod value );
98 std::ostream& operator<<( std::ostream& ostream, Radian angle );
99 std::ostream& operator<<( std::ostream& ostream, Degree angle );
102 * Test whether two values are equal.
103 * @param[in] value1 The first value
104 * @param[in] value2 The second value
105 * @param[in] location The TEST_LOCATION macro should be used here
107 template<typename Type>
108 inline void DALI_TEST_EQUALS(Type value1, Type value2, const char* location)
110 if( !CompareType<Type>(value1, value2, 0.01f) )
112 std::ostringstream o;
113 o << value1 << " == " << value2 << std::endl;
114 fprintf(stderr, "Test failed in %s, checking %s", location, o.str().c_str());
115 tet_result(TET_FAIL);
120 tet_result(TET_PASS);
125 * Test whether two values are equal.
126 * @param[in] value1 The first value
127 * @param[in] value2 The second value
129 #define DALI_TEST_EQUAL( v1, v2 ) DALI_TEST_EQUALS( v1, v2, __FILELINE__ )
131 template<typename Type>
132 inline void DALI_TEST_EQUALS(Type value1, Type value2, float epsilon, const char* location)
134 if( !CompareType<Type>(value1, value2, epsilon) )
136 std::ostringstream o;
137 o << value1 << " == " << value2 << std::endl;
138 fprintf(stderr, "Test failed in %s, checking %s", location, o.str().c_str());
139 tet_result(TET_FAIL);
144 tet_result(TET_PASS);
148 template<typename Type>
149 inline void DALI_TEST_NOT_EQUALS(Type value1, Type value2, float epsilon, const char* location)
151 if( CompareType<Type>(value1, value2, epsilon) )
153 std::ostringstream o;
154 o << value1 << " != " << value2 << std::endl;
155 fprintf(stderr, "Test failed in %s, checking %s", location, o.str().c_str());
156 tet_result(TET_FAIL);
161 tet_result(TET_PASS);
167 * Test whether two TimePeriods are within a certain distance of each other.
168 * @param[in] value1 The first value
169 * @param[in] value2 The second value
170 * @param[in] epsilon The values must be within this distance of each other
171 * @param[in] location The TEST_LOCATION macro should be used here
174 inline void DALI_TEST_EQUALS<TimePeriod>( TimePeriod value1, TimePeriod value2, float epsilon, const char* location)
176 if ((fabs(value1.durationSeconds - value2.durationSeconds) > epsilon))
178 fprintf(stderr, "Test failed in %s, checking durations %f == %f, epsilon %f\n", location, value1.durationSeconds, value2.durationSeconds, epsilon);
179 tet_result(TET_FAIL);
182 else if ((fabs(value1.delaySeconds - value2.delaySeconds) > epsilon))
184 fprintf(stderr, "Test failed in %s, checking delays %f == %f, epsilon %f\n", location, value1.delaySeconds, value2.delaySeconds, epsilon);
185 tet_result(TET_FAIL);
190 tet_result(TET_PASS);
195 * Test whether two base handles are equal.
196 * @param[in] baseHandle1 The first value
197 * @param[in] baseHandle2 The second value
198 * @param[in] location The TEST_LOCATION macro should be used here
200 void DALI_TEST_EQUALS( const BaseHandle& baseHandle1, const BaseHandle& baseHandle2, const char* location );
203 * Test whether a size_t value and an uint32_t are equal.
204 * @param[in] value1 The first value
205 * @param[in] value2 The second value
206 * @param[in] location The TEST_LOCATION macro should be used here
208 void DALI_TEST_EQUALS( const size_t value1, const uint32_t value2, const char* location );
211 * Test whether an uint32_t and a size_t value and are equal.
212 * @param[in] value1 The first value
213 * @param[in] value2 The second value
214 * @param[in] location The TEST_LOCATION macro should be used here
216 void DALI_TEST_EQUALS( const uint32_t value1, const size_t value2, const char* location );
219 * Test whether two Matrix3 objects are equal.
220 * @param[in] matrix1 The first object
221 * @param[in] matrix2 The second object
222 * @param[in] location The TEST_LOCATION macro should be used here
224 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location);
226 /** Test whether two Matrix3 objects are equal (fuzzy compare).
227 * @param[in] matrix1 The first object
228 * @param[in] matrix2 The second object
229 * @param[in] epsilon The epsilon to use for comparison
230 * @param[in] location The TEST_LOCATION macro should be used here
232 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location);
235 * Test whether two Matrix objects are equal.
236 * @param[in] matrix1 The first object
237 * @param[in] matrix2 The second object
238 * @param[in] location The TEST_LOCATION macro should be used here
240 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location);
243 * Test whether two Matrix objects are equal (fuzzy-compare).
244 * @param[in] matrix1 The first object
245 * @param[in] matrix2 The second object
246 * @param[in] location The TEST_LOCATION macro should be used here
248 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location);
251 * Test whether two strings are equal.
252 * @param[in] str1 The first string
253 * @param[in] str2 The second string
254 * @param[in] location The TEST_LOCATION macro should be used here
257 inline void DALI_TEST_EQUALS<const char*>( const char* str1, const char* str2, const char* location)
259 if (strcmp(str1, str2))
261 fprintf(stderr, "Test failed in %s, checking '%s' == '%s'\n", location, str1, str2);
262 tet_result(TET_FAIL);
267 tet_result(TET_PASS);
272 * Test whether two strings are equal.
273 * @param[in] str1 The first string
274 * @param[in] str2 The second string
275 * @param[in] location The TEST_LOCATION macro should be used here
278 inline void DALI_TEST_EQUALS<const std::string&>( const std::string &str1, const std::string &str2, const char* location)
280 DALI_TEST_EQUALS(str1.c_str(), str2.c_str(), location);
284 * Test whether two strings are equal.
285 * @param[in] str1 The first string
286 * @param[in] str2 The second string
287 * @param[in] location The TEST_LOCATION macro should be used here
289 void DALI_TEST_EQUALS( Property::Value& str1, const char* str2, const char* location);
292 * Test whether two strings are equal.
293 * @param[in] str1 The first string
294 * @param[in] str2 The second string
295 * @param[in] location The TEST_LOCATION macro should be used here
297 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location);
300 * Test whether two strings are equal.
301 * @param[in] str1 The first string
302 * @param[in] str2 The second string
303 * @param[in] location The TEST_LOCATION macro should be used here
305 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location);
308 * Test if a property value type is equal to a trivial type.
310 template<typename Type>
311 inline void DALI_TEST_VALUE_EQUALS( Property::Value&& value1, Type value2, float epsilon, const char* location)
313 Property::Value value2b(value2);
314 DALI_TEST_EQUALS(value1, value2b, epsilon, location);
319 * Test whether one unsigned integer value is greater than another.
320 * Test succeeds if value1 > value2
321 * @param[in] value1 The first value
322 * @param[in] value2 The second value
323 * @param[in] location The TEST_LOCATION macro should be used here
325 template< typename T >
326 void DALI_TEST_GREATER( T value1, T value2, const char* location)
328 if (!(value1 > value2))
330 std::cerr << "Test failed in " << location << ", checking " << value1 <<" > " << value2 << "\n";
331 tet_result(TET_FAIL);
336 tet_result(TET_PASS);
341 * Test whether the assertion condition that failed and thus triggered the
342 * exception \b e contained a given substring.
343 * @param[in] e The exception that we expect was fired by a runtime assertion failure.
344 * @param[in] conditionSubString The text that we expect to be present in an
345 * assertion which triggered the exception.
346 * @param[in] location The TEST_LOCATION macro should be used here.
348 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
352 * @param[in] e The exception that we expect was fired by a runtime assertion failure.
354 inline void DALI_TEST_PRINT_ASSERT( DaliException& e )
356 tet_printf("Assertion %s failed at %s\n", e.condition, e.location );
360 * Test that given piece of code triggers the right assertion
361 * Fails the test if the assert didn't occur.
362 * Turns off logging during the execution of the code to avoid excessive false positive log output from the assertions
363 * @param expressions code to execute
364 * @param assertstring the substring expected in the assert
366 #define DALI_TEST_ASSERTION( expressions, assertstring ) \
369 TestApplication::EnableLogging( false ); \
371 TestApplication::EnableLogging( true ); \
372 fprintf(stderr, "Test failed in %s, expected assert: '%s' didn't occur\n", __FILELINE__, assertstring ); \
373 tet_result(TET_FAIL); \
374 throw("TET_FAIL"); } \
375 catch( Dali::DaliException& e ) \
377 DALI_TEST_ASSERT( e, assertstring, TEST_LOCATION ); \
380 // Functor to test whether an Applied signal is emitted
381 struct ConstraintAppliedCheck
383 ConstraintAppliedCheck( bool& signalReceived );
384 void operator()( Constraint& constraint );
386 void CheckSignalReceived();
387 void CheckSignalNotReceived();
388 bool& mSignalReceived; // owned by individual tests
392 * A Helper to test default functions
394 template <typename T>
395 struct DefaultFunctionCoverage
397 DefaultFunctionCoverage()
407 // Test namespace to prevent pollution of Dali namespace, add Test helper functions here
413 * Helper to check object destruction occurred
414 * 1) In main part of code create an ObjectDestructionTracker
415 * 2) Within sub section of main create object Actor test and call Start with Actor to test for destruction
416 * 3) Perform code which is expected to destroy Actor
417 * 4) Back in main part of code use IsDestroyed() to test if Actor was destroyed
419 class ObjectDestructionTracker : public ConnectionTracker
424 * @brief Call in main part of code
425 * @param[in] objectRegistry The object Registry being used
427 ObjectDestructionTracker( ObjectRegistry objectRegistry );
430 * @brief Call in sub bock of code where the Actor being checked is still alive.
432 * @param[in] actor Actor to be checked for destruction
434 void Start( Actor actor );
437 * @brief Call to check if Actor alive or destroyed.
439 * @return bool true if Actor was destroyed
444 ObjectRegistry mObjectRegistry;
445 bool mRefObjectDestroyed;
450 #endif // DALI_TEST_SUITE_UTILS_H