ConditionalWait to replace boost conditional variable usage
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / dali-test-suite-utils / dali-test-suite-utils.h
1 #ifndef __DALI_TEST_SUITE_UTILS_H__
2 #define __DALI_TEST_SUITE_UTILS_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <cstdarg>
23 #include <iostream>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/dali-core.h>
27
28 void tet_infoline(const char*str);
29 void tet_printf(const char *format, ...);
30
31 #include "test-application.h"
32
33 using namespace Dali;
34
35 #define STRINGIZE_I(text) #text
36 #define STRINGIZE(text) STRINGIZE_I(text)
37
38 // the following is the other compilers way of token pasting, gcc seems to just concatenate strings automatically
39 //#define TOKENPASTE(x,y) x ## y
40 #define TOKENPASTE(x,y) x y
41 #define TOKENPASTE2(x,y) TOKENPASTE( x, y )
42 #define TEST_LOCATION TOKENPASTE2( "Test failed in ", TOKENPASTE2( __FILE__, TOKENPASTE2( ", line ", STRINGIZE(__LINE__) ) ) )
43
44 #define TET_UNDEF 2
45 #define TET_FAIL 1
46 #define TET_PASS 0
47
48 extern int test_return_value;
49
50 void tet_result(int value);
51
52 #define END_TEST \
53   return ((test_return_value>0)?1:0)
54
55 void tet_infoline(const char* str);
56 void tet_printf(const char *format, ...);
57
58 /**
59  * DALI_TEST_CHECK is a wrapper for tet_result.
60  * If the condition evaluates to false, then the function & line number is printed.
61  * @param[in] The boolean expression to check
62  */
63 #define DALI_TEST_CHECK(condition)                                                        \
64 if ( (condition) )                                                                        \
65 {                                                                                         \
66   tet_result(TET_PASS);                                                                   \
67 }                                                                                         \
68 else                                                                                      \
69 {                                                                                         \
70   fprintf(stderr, "%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);    \
71   tet_result(TET_FAIL);                                                                   \
72 }
73
74 template <typename Type>
75 inline bool CompareType(Type value1, Type value2, float epsilon);
76
77 /**
78  * A helper for fuzzy-comparing Vector2 objects
79  * @param[in] vector1 the first object
80  * @param[in] vector2 the second object
81  * @param[in] epsilon difference threshold
82  * @returns true if difference is smaller than epsilon threshold, false otherwise
83  */
84 template <>
85 inline bool CompareType<float>(float value1, float value2, float epsilon)
86 {
87   return fabsf(value1 - value2) < epsilon;
88 }
89
90 /**
91  * A helper for fuzzy-comparing Vector2 objects
92  * @param[in] vector1 the first object
93  * @param[in] vector2 the second object
94  * @param[in] epsilon difference threshold
95  * @returns true if difference is smaller than epsilon threshold, false otherwise
96  */
97 template <>
98 inline bool CompareType<Vector2>(Vector2 vector1, Vector2 vector2, float epsilon)
99 {
100   return fabsf(vector1.x - vector2.x)<epsilon && fabsf(vector1.y - vector2.y)<epsilon;
101 }
102
103 /**
104  * A helper for fuzzy-comparing Vector3 objects
105  * @param[in] vector1 the first object
106  * @param[in] vector2 the second object
107  * @param[in] epsilon difference threshold
108  * @returns true if difference is smaller than epsilon threshold, false otherwise
109  */
110 template <>
111 inline bool CompareType<Vector3>(Vector3 vector1, Vector3 vector2, float epsilon)
112 {
113   return fabsf(vector1.x - vector2.x)<epsilon &&
114          fabsf(vector1.y - vector2.y)<epsilon &&
115          fabsf(vector1.z - vector2.z)<epsilon;
116 }
117
118
119 /**
120  * A helper for fuzzy-comparing Vector4 objects
121  * @param[in] vector1 the first object
122  * @param[in] vector2 the second object
123  * @param[in] epsilon difference threshold
124  * @returns true if difference is smaller than epsilon threshold, false otherwise
125  */
126 template <>
127 inline bool CompareType<Vector4>(Vector4 vector1, Vector4 vector2, float epsilon)
128 {
129   return fabsf(vector1.x - vector2.x)<epsilon &&
130          fabsf(vector1.y - vector2.y)<epsilon &&
131          fabsf(vector1.z - vector2.z)<epsilon &&
132          fabsf(vector1.w - vector2.w)<epsilon;
133 }
134
135 template <>
136 inline bool CompareType<Quaternion>(Quaternion q1, Quaternion q2, float epsilon)
137 {
138   Quaternion q2N = -q2; // These quaternions represent the same rotation
139   return CompareType<Vector4>(q1.mVector, q2.mVector, epsilon) || CompareType<Vector4>(q1.mVector, q2N.mVector, epsilon);
140 }
141
142 template <>
143 inline bool CompareType<Radian>(Radian q1, Radian q2, float epsilon)
144 {
145   return CompareType<float>(q1.radian, q2.radian, epsilon);
146 }
147
148 template <>
149 inline bool CompareType<Degree>(Degree q1, Degree q2, float epsilon)
150 {
151   return CompareType<float>(q1.degree, q2.degree, epsilon);
152 }
153
154 bool operator==(TimePeriod a, TimePeriod b);
155 std::ostream& operator<<( std::ostream& ostream, TimePeriod value );
156 std::ostream& operator<<( std::ostream& ostream, Radian angle );
157 std::ostream& operator<<( std::ostream& ostream, Degree angle );
158
159 /**
160  * Test whether two values are equal.
161  * @param[in] value1 The first value
162  * @param[in] value2 The second value
163  * @param[in] location The TEST_LOCATION macro should be used here
164  */
165 template<typename TypeA, typename TypeB>
166 inline void DALI_TEST_EQUALS(TypeA value1, TypeB value2, const char* location)
167 {
168   if (!(value1 == value2))
169   {
170     std::ostringstream o;
171     o << value1 << " == " << value2 << std::endl;
172     fprintf(stderr, "%s, checking %s", location, o.str().c_str());
173     tet_result(TET_FAIL);
174   }
175   else
176   {
177     tet_result(TET_PASS);
178   }
179 }
180
181 template<typename Type>
182 inline void DALI_TEST_EQUALS(Type value1, Type value2, float epsilon, const char* location)
183 {
184   if( !CompareType<Type>(value1, value2, epsilon) )
185   {
186     std::ostringstream o;
187     o << value1 << " == " << value2 << std::endl;
188     fprintf(stderr, "%s, checking %s", location, o.str().c_str());
189     tet_result(TET_FAIL);
190   }
191   else
192   {
193     tet_result(TET_PASS);
194   }
195 }
196
197 /**
198  * Test whether two TimePeriods are within a certain distance of each other.
199  * @param[in] value1 The first value
200  * @param[in] value2 The second value
201  * @param[in] epsilon The values must be within this distance of each other
202  * @param[in] location The TEST_LOCATION macro should be used here
203  */
204 template<>
205 inline void DALI_TEST_EQUALS<TimePeriod>( TimePeriod value1, TimePeriod value2, float epsilon, const char* location)
206 {
207   if ((fabs(value1.durationSeconds - value2.durationSeconds) > epsilon))
208   {
209     fprintf(stderr, "%s, checking durations %f == %f, epsilon %f\n", location, value1.durationSeconds, value2.durationSeconds, epsilon);
210     tet_result(TET_FAIL);
211   }
212   else if ((fabs(value1.delaySeconds - value2.delaySeconds) > epsilon))
213   {
214     fprintf(stderr, "%s, checking delays %f == %f, epsilon %f\n", location, value1.delaySeconds, value2.delaySeconds, epsilon);
215     tet_result(TET_FAIL);
216   }
217   else
218   {
219     tet_result(TET_PASS);
220   }
221 }
222
223 /**
224  * Test whether two Matrix3 objects are equal.
225  * @param[in] matrix1 The first object
226  * @param[in] matrix2 The second object
227  * @param[in] location The TEST_LOCATION macro should be used here
228  */
229 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location);
230
231 /** Test whether two Matrix3 objects are equal (fuzzy compare).
232  * @param[in] matrix1 The first object
233  * @param[in] matrix2 The second object
234  * @param[in] epsilon The epsilon to use for comparison
235  * @param[in] location The TEST_LOCATION macro should be used here
236  */
237 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location);
238
239 /**
240  * Test whether two Matrix objects are equal.
241  * @param[in] matrix1 The first object
242  * @param[in] matrix2 The second object
243  * @param[in] location The TEST_LOCATION macro should be used here
244  */
245 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location);
246
247 /**
248  * Test whether two Matrix objects are equal (fuzzy-compare).
249  * @param[in] matrix1 The first object
250  * @param[in] matrix2 The second object
251  * @param[in] location The TEST_LOCATION macro should be used here
252  */
253 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location);
254
255 /**
256  * Test whether two strings are equal.
257  * @param[in] str1 The first string
258  * @param[in] str2 The second string
259  * @param[in] location The TEST_LOCATION macro should be used here
260  */
261 template<>
262 inline void DALI_TEST_EQUALS<const char*>( const char* str1, const char* str2, const char* location)
263 {
264   if (strcmp(str1, str2))
265   {
266     fprintf(stderr, "%s, checking '%s' == '%s'\n", location, str1, str2);
267     tet_result(TET_FAIL);
268   }
269   else
270   {
271     tet_result(TET_PASS);
272   }
273 }
274
275 /**
276  * Test whether two strings are equal.
277  * @param[in] str1 The first string
278  * @param[in] str2 The second string
279  * @param[in] location The TEST_LOCATION macro should be used here
280  */
281 template<>
282 inline void DALI_TEST_EQUALS<const std::string&>( const std::string &str1, const std::string &str2, const char* location)
283 {
284   DALI_TEST_EQUALS(str1.c_str(), str2.c_str(), location);
285 }
286
287 /**
288  * Test whether two strings are equal.
289  * @param[in] str1 The first string
290  * @param[in] str2 The second string
291  * @param[in] location The TEST_LOCATION macro should be used here
292  */
293 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location);
294
295 /**
296  * Test whether two strings are equal.
297  * @param[in] str1 The first string
298  * @param[in] str2 The second string
299  * @param[in] location The TEST_LOCATION macro should be used here
300  */
301 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location);
302
303 /**
304  * Test whether one unsigned integer value is greater than another.
305  * Test succeeds if value1 > value2
306  * @param[in] value1 The first value
307  * @param[in] value2 The second value
308  * @param[in] location The TEST_LOCATION macro should be used here
309  */
310 template< typename T >
311 void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location)
312 {
313   if (!(value1 > value2))
314   {
315     std::cerr << location << ", checking " << value1 <<" > " << value2 << "\n";
316     tet_result(TET_FAIL);
317   }
318   else
319   {
320     tet_result(TET_PASS);
321   }
322 }
323
324 /**
325  * Test whether the assertion condition that failed and thus triggered the
326  * exception \b e contained a given substring.
327  * @param[in] e The exception that we expect was fired by a runtime assertion failure.
328  * @param[in] conditionSubString The text that we expect to be present in an
329  *                               assertion which triggered the exception.
330  * @param[in] location The TEST_LOCATION macro should be used here.
331  */
332 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
333
334 /**
335  * Print the assert
336  * @param[in] e The exception that we expect was fired by a runtime assertion failure.
337  */
338 inline void DALI_TEST_PRINT_ASSERT( DaliException& e )
339 {
340   tet_printf("Assertion %s failed at %s\n", e.condition, e.location );
341 }
342
343 // Functor to test whether an Applied signal is emitted
344 struct ConstraintAppliedCheck
345 {
346   ConstraintAppliedCheck( bool& signalReceived );
347   void operator()( Constraint& constraint );
348   void Reset();
349   void CheckSignalReceived();
350   void CheckSignalNotReceived();
351   bool& mSignalReceived; // owned by individual tests
352 };
353
354 /**
355  * A Helper to test default functions
356  */
357 template <typename T>
358 struct DefaultFunctionCoverage
359 {
360   DefaultFunctionCoverage()
361   {
362     T a;
363     T *b = new T(a);
364     DALI_TEST_CHECK(b);
365     a = *b;
366     delete b;
367   }
368 };
369
370
371 // Helper to Create bitmap image
372 BufferImage CreateBufferImage();
373
374 #endif // __DALI_TEST_SUITE_UTILS_H__