1 #ifndef __DALI_TEST_SUITE_UTILS_H__
2 #define __DALI_TEST_SUITE_UTILS_H__
5 * Copyright (c) 2017 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"
40 #define STRINGIZE_I(text) #text
41 #define STRINGIZE(text) STRINGIZE_I(text)
44 * Inspired by https://stackoverflow.com/questions/1706346/file-macro-manipulation-handling-at-compile-time
45 * answer by Chetan Reddy
47 constexpr int32_t basenameIndex( const char * const path, const int32_t index = 0, const int32_t slashIndex = -1 )
50 ? ( path[ index ] == '/'
51 ? basenameIndex( path, index + 1, index )
52 : basenameIndex( path, index + 1, slashIndex ) )
56 #define __FILELINE__ ( { static const int32_t basenameIdx = basenameIndex( __FILE__ ); \
57 static_assert (basenameIdx >= 0, "compile-time basename" ); \
58 __FILE__ ":" STRINGIZE(__LINE__) + basenameIdx ; } )
60 #define TEST_LOCATION __FILELINE__
61 #define TEST_INNER_LOCATION(x) ( std::string(x) + " (" + STRINGIZE(__LINE__) + ")" ).c_str()
67 extern int test_return_value;
69 void tet_result(int value);
72 return ((test_return_value>0)?1:0)
74 void tet_infoline(const char* str);
75 void tet_printf(const char *format, ...);
78 * DALI_TEST_CHECK is a wrapper for tet_result.
79 * If the condition evaluates to false, the test is stopped.
80 * @param[in] The boolean expression to check
82 #define DALI_TEST_CHECK(condition) \
85 tet_result(TET_PASS); \
89 fprintf(stderr, "Test failed in %s, condition: %s\n", __FILELINE__, #condition ); \
90 tet_result(TET_FAIL); \
95 bool operator==(TimePeriod a, TimePeriod b);
96 std::ostream& operator<<( std::ostream& ostream, TimePeriod value );
97 std::ostream& operator<<( std::ostream& ostream, Radian angle );
98 std::ostream& operator<<( std::ostream& ostream, Degree angle );
101 * Test whether two values are equal.
102 * @param[in] value1 The first value
103 * @param[in] value2 The second value
104 * @param[in] location The TEST_LOCATION macro should be used here
106 template<typename Type>
107 inline void DALI_TEST_EQUALS(Type value1, Type value2, const char* location)
109 if( !CompareType<Type>(value1, value2, 0.01f) )
111 std::ostringstream o;
112 o << value1 << " == " << value2 << std::endl;
113 fprintf(stderr, "Test failed in %s, checking %s", location, o.str().c_str());
114 tet_result(TET_FAIL);
119 tet_result(TET_PASS);
124 * Test whether two values are equal.
125 * @param[in] value1 The first value
126 * @param[in] value2 The second value
128 #define DALI_TEST_EQUAL( v1, v2 ) DALI_TEST_EQUALS( v1, v2, __FILELINE__ )
130 template<typename Type>
131 inline void DALI_TEST_EQUALS(Type value1, Type value2, float epsilon, const char* location)
133 if( !CompareType<Type>(value1, value2, epsilon) )
135 std::ostringstream o;
136 o << value1 << " == " << value2 << std::endl;
137 fprintf(stderr, "Test failed in %s, checking %s", location, o.str().c_str());
138 tet_result(TET_FAIL);
143 tet_result(TET_PASS);
147 template<typename Type>
148 inline void DALI_TEST_NOT_EQUALS(Type value1, Type value2, float epsilon, const char* location)
150 if( CompareType<Type>(value1, value2, epsilon) )
152 std::ostringstream o;
153 o << value1 << " != " << value2 << std::endl;
154 fprintf(stderr, "Test failed in %s, checking %s", location, o.str().c_str());
155 tet_result(TET_FAIL);
160 tet_result(TET_PASS);
166 * Test whether two TimePeriods are within a certain distance of each other.
167 * @param[in] value1 The first value
168 * @param[in] value2 The second value
169 * @param[in] epsilon The values must be within this distance of each other
170 * @param[in] location The TEST_LOCATION macro should be used here
173 inline void DALI_TEST_EQUALS<TimePeriod>( TimePeriod value1, TimePeriod value2, float epsilon, const char* location)
175 if ((fabs(value1.durationSeconds - value2.durationSeconds) > epsilon))
177 fprintf(stderr, "Test failed in %s, checking durations %f == %f, epsilon %f\n", location, value1.durationSeconds, value2.durationSeconds, epsilon);
178 tet_result(TET_FAIL);
181 else if ((fabs(value1.delaySeconds - value2.delaySeconds) > epsilon))
183 fprintf(stderr, "Test failed in %s, checking delays %f == %f, epsilon %f\n", location, value1.delaySeconds, value2.delaySeconds, epsilon);
184 tet_result(TET_FAIL);
189 tet_result(TET_PASS);
194 * Test whether two base handles are equal.
195 * @param[in] baseHandle1 The first value
196 * @param[in] baseHandle2 The second value
197 * @param[in] location The TEST_LOCATION macro should be used here
199 void DALI_TEST_EQUALS( const BaseHandle& baseHandle1, const BaseHandle& baseHandle2, const char* location );
202 * Test whether a size_t value and an unsigned int are equal.
203 * @param[in] value1 The first value
204 * @param[in] value2 The second value
205 * @param[in] location The TEST_LOCATION macro should be used here
207 void DALI_TEST_EQUALS( const size_t value1, const unsigned int value2, const char* location );
210 * Test whether an unsigned int and a size_t value and are equal.
211 * @param[in] value1 The first value
212 * @param[in] value2 The second value
213 * @param[in] location The TEST_LOCATION macro should be used here
215 void DALI_TEST_EQUALS( const unsigned int value1, const size_t value2, const char* location );
218 * Test whether two Matrix3 objects are equal.
219 * @param[in] matrix1 The first object
220 * @param[in] matrix2 The second object
221 * @param[in] location The TEST_LOCATION macro should be used here
223 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location);
225 /** Test whether two Matrix3 objects are equal (fuzzy compare).
226 * @param[in] matrix1 The first object
227 * @param[in] matrix2 The second object
228 * @param[in] epsilon The epsilon to use for comparison
229 * @param[in] location The TEST_LOCATION macro should be used here
231 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location);
234 * Test whether two Matrix objects are equal.
235 * @param[in] matrix1 The first object
236 * @param[in] matrix2 The second object
237 * @param[in] location The TEST_LOCATION macro should be used here
239 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location);
242 * Test whether two Matrix objects are equal (fuzzy-compare).
243 * @param[in] matrix1 The first object
244 * @param[in] matrix2 The second object
245 * @param[in] location The TEST_LOCATION macro should be used here
247 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location);
250 * Test whether two strings are equal.
251 * @param[in] str1 The first string
252 * @param[in] str2 The second string
253 * @param[in] location The TEST_LOCATION macro should be used here
256 inline void DALI_TEST_EQUALS<const char*>( const char* str1, const char* str2, const char* location)
258 if (strcmp(str1, str2))
260 fprintf(stderr, "Test failed in %s, checking '%s' == '%s'\n", location, str1, str2);
261 tet_result(TET_FAIL);
266 tet_result(TET_PASS);
271 * Test whether two strings are equal.
272 * @param[in] str1 The first string
273 * @param[in] str2 The second string
274 * @param[in] location The TEST_LOCATION macro should be used here
277 inline void DALI_TEST_EQUALS<const std::string&>( const std::string &str1, const std::string &str2, const char* location)
279 DALI_TEST_EQUALS(str1.c_str(), str2.c_str(), location);
283 * Test whether two strings are equal.
284 * @param[in] str1 The first string
285 * @param[in] str2 The second string
286 * @param[in] location The TEST_LOCATION macro should be used here
288 void DALI_TEST_EQUALS( Property::Value& str1, const char* str2, const char* location);
291 * Test whether two strings are equal.
292 * @param[in] str1 The first string
293 * @param[in] str2 The second string
294 * @param[in] location The TEST_LOCATION macro should be used here
296 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location);
299 * Test whether two strings are equal.
300 * @param[in] str1 The first string
301 * @param[in] str2 The second string
302 * @param[in] location The TEST_LOCATION macro should be used here
304 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location);
307 * Test whether one unsigned integer value is greater than another.
308 * Test succeeds if value1 > value2
309 * @param[in] value1 The first value
310 * @param[in] value2 The second value
311 * @param[in] location The TEST_LOCATION macro should be used here
313 template< typename T >
314 void DALI_TEST_GREATER( T value1, T value2, const char* location)
316 if (!(value1 > value2))
318 std::cerr << "Test failed in " << location << ", checking " << value1 <<" > " << value2 << "\n";
319 tet_result(TET_FAIL);
324 tet_result(TET_PASS);
329 * Test whether the assertion condition that failed and thus triggered the
330 * exception \b e contained a given substring.
331 * @param[in] e The exception that we expect was fired by a runtime assertion failure.
332 * @param[in] conditionSubString The text that we expect to be present in an
333 * assertion which triggered the exception.
334 * @param[in] location The TEST_LOCATION macro should be used here.
336 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
340 * @param[in] e The exception that we expect was fired by a runtime assertion failure.
342 inline void DALI_TEST_PRINT_ASSERT( DaliException& e )
344 tet_printf("Assertion %s failed at %s\n", e.condition, e.location );
347 // Functor to test whether an Applied signal is emitted
348 struct ConstraintAppliedCheck
350 ConstraintAppliedCheck( bool& signalReceived );
351 void operator()( Constraint& constraint );
353 void CheckSignalReceived();
354 void CheckSignalNotReceived();
355 bool& mSignalReceived; // owned by individual tests
359 * A Helper to test default functions
361 template <typename T>
362 struct DefaultFunctionCoverage
364 DefaultFunctionCoverage()
375 // Helper to Create buffer image
376 BufferImage CreateBufferImage();
377 BufferImage CreateBufferImage(int width, int height, const Vector4& color);
380 // Prepare a resource image to be loaded. Should be called before creating the ResourceImage
381 void PrepareResourceImage( TestApplication& application, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat );
383 // Test namespace to prevent pollution of Dali namespace, add Test helper functions here
389 * Helper to check object destruction occurred
390 * 1) In main part of code create an ObjectDestructionTracker
391 * 2) Within sub section of main create object Actor test and call Start with Actor to test for destruction
392 * 3) Perform code which is expected to destroy Actor
393 * 4) Back in main part of code use IsDestroyed() to test if Actor was destroyed
395 class ObjectDestructionTracker : public ConnectionTracker
400 * @brief Call in main part of code
402 ObjectDestructionTracker();
405 * @brief Call in sub bock of code where the Actor being checked is still alive.
407 * @param[in] actor Actor to be checked for destruction
409 void Start( Actor actor );
412 * @brief Call to check if Actor alive or destroyed.
414 * @return bool true if Actor was destroyed
419 bool mRefObjectDestroyed;
424 #endif // __DALI_TEST_SUITE_UTILS_H__