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