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