1 #ifndef DALI_TEST_SUITE_UTILS_H
2 #define DALI_TEST_SUITE_UTILS_H
5 * Copyright (c) 2019 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 whether one unsigned integer value is greater than another.
309 * Test succeeds if value1 > value2
310 * @param[in] value1 The first value
311 * @param[in] value2 The second value
312 * @param[in] location The TEST_LOCATION macro should be used here
314 template< typename T >
315 void DALI_TEST_GREATER( T value1, T value2, const char* location)
317 if (!(value1 > value2))
319 std::cerr << "Test failed in " << location << ", checking " << value1 <<" > " << value2 << "\n";
320 tet_result(TET_FAIL);
325 tet_result(TET_PASS);
330 * Test whether the assertion condition that failed and thus triggered the
331 * exception \b e contained a given substring.
332 * @param[in] e The exception that we expect was fired by a runtime assertion failure.
333 * @param[in] conditionSubString The text that we expect to be present in an
334 * assertion which triggered the exception.
335 * @param[in] location The TEST_LOCATION macro should be used here.
337 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
341 * @param[in] e The exception that we expect was fired by a runtime assertion failure.
343 inline void DALI_TEST_PRINT_ASSERT( DaliException& e )
345 tet_printf("Assertion %s failed at %s\n", e.condition, e.location );
349 * Test that given piece of code triggers the right assertion
350 * Fails the test if the assert didn't occur.
351 * Turns off logging during the execution of the code to avoid excessive false positive log output from the assertions
352 * @param expressions code to execute
353 * @param assertstring the substring expected in the assert
355 #define DALI_TEST_ASSERTION( expressions, assertstring ) \
358 TestApplication::EnableLogging( false ); \
360 TestApplication::EnableLogging( true ); \
361 fprintf(stderr, "Test failed in %s, expected assert: '%s' didn't occur\n", __FILELINE__, assertstring ); \
362 tet_result(TET_FAIL); \
363 throw("TET_FAIL"); } \
364 catch( Dali::DaliException& e ) \
366 DALI_TEST_ASSERT( e, assertstring, TEST_LOCATION ); \
369 // Functor to test whether an Applied signal is emitted
370 struct ConstraintAppliedCheck
372 ConstraintAppliedCheck( bool& signalReceived );
373 void operator()( Constraint& constraint );
375 void CheckSignalReceived();
376 void CheckSignalNotReceived();
377 bool& mSignalReceived; // owned by individual tests
381 * A Helper to test default functions
383 template <typename T>
384 struct DefaultFunctionCoverage
386 DefaultFunctionCoverage()
397 // Helper to Create buffer image
398 BufferImage CreateBufferImage();
399 BufferImage CreateBufferImage(int32_t width, int32_t height, const Vector4& color);
402 // Prepare a resource image to be loaded. Should be called before creating the ResourceImage
403 void PrepareResourceImage( TestApplication& application, uint32_t imageWidth, uint32_t imageHeight, Pixel::Format pixelFormat );
405 // Test namespace to prevent pollution of Dali namespace, add Test helper functions here
411 * Helper to check object destruction occurred
412 * 1) In main part of code create an ObjectDestructionTracker
413 * 2) Within sub section of main create object Actor test and call Start with Actor to test for destruction
414 * 3) Perform code which is expected to destroy Actor
415 * 4) Back in main part of code use IsDestroyed() to test if Actor was destroyed
417 class ObjectDestructionTracker : public ConnectionTracker
422 * @brief Call in main part of code
424 ObjectDestructionTracker();
427 * @brief Call in sub bock of code where the Actor being checked is still alive.
429 * @param[in] actor Actor to be checked for destruction
431 void Start( Actor actor );
434 * @brief Call to check if Actor alive or destroyed.
436 * @return bool true if Actor was destroyed
441 bool mRefObjectDestroyed;
446 #endif // DALI_TEST_SUITE_UTILS_H