1 #ifndef __DALI_TEST_SUITE_UTILS_H__
2 #define __DALI_TEST_SUITE_UTILS_H__
5 * Copyright (c) 2014 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.
22 #include <dali/public-api/dali-core.h>
25 void tet_infoline(const char*str);
26 void tet_printf(const char *format, ...);
28 #include "test-application.h"
32 #define STRINGIZE_I(text) #text
33 #define STRINGIZE(text) STRINGIZE_I(text)
35 // the following is the other compilers way of token pasting, gcc seems to just concatenate strings automatically
36 //#define TOKENPASTE(x,y) x ## y
37 #define TOKENPASTE(x,y) x y
38 #define TOKENPASTE2(x,y) TOKENPASTE( x, y )
39 #define TEST_LOCATION TOKENPASTE2( "Test failed in ", TOKENPASTE2( __FILE__, TOKENPASTE2( ", line ", STRINGIZE(__LINE__) ) ) )
45 extern int test_return_value;
47 void tet_result(int value);
50 return ((test_return_value>0)?1:0)
52 void tet_infoline(const char* str);
53 void tet_printf(const char *format, ...);
56 * DALI_TEST_CHECK is a wrapper for tet_result.
57 * If the condition evaluates to false, the test is stopped.
58 * @param[in] The boolean expression to check
60 #define DALI_TEST_CHECK(condition) \
63 tet_result(TET_PASS); \
67 fprintf(stderr, "%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__); \
68 tet_result(TET_FAIL); \
72 template <typename Type>
73 inline bool CompareType(Type value1, Type value2, float epsilon);
76 * A helper for fuzzy-comparing Vector2 objects
77 * @param[in] vector1 the first object
78 * @param[in] vector2 the second object
79 * @param[in] epsilon difference threshold
80 * @returns true if difference is smaller than epsilon threshold, false otherwise
83 inline bool CompareType<float>(float value1, float value2, float epsilon)
85 return fabsf(value1 - value2) < epsilon;
89 * A helper for fuzzy-comparing Vector2 objects
90 * @param[in] vector1 the first object
91 * @param[in] vector2 the second object
92 * @param[in] epsilon difference threshold
93 * @returns true if difference is smaller than epsilon threshold, false otherwise
96 inline bool CompareType<Vector2>(Vector2 vector1, Vector2 vector2, float epsilon)
98 return fabsf(vector1.x - vector2.x)<epsilon && fabsf(vector1.y - vector2.y)<epsilon;
102 * A helper for fuzzy-comparing Vector3 objects
103 * @param[in] vector1 the first object
104 * @param[in] vector2 the second object
105 * @param[in] epsilon difference threshold
106 * @returns true if difference is smaller than epsilon threshold, false otherwise
109 inline bool CompareType<Vector3>(Vector3 vector1, Vector3 vector2, float epsilon)
111 return fabsf(vector1.x - vector2.x)<epsilon &&
112 fabsf(vector1.y - vector2.y)<epsilon &&
113 fabsf(vector1.z - vector2.z)<epsilon;
118 * A helper for fuzzy-comparing Vector4 objects
119 * @param[in] vector1 the first object
120 * @param[in] vector2 the second object
121 * @param[in] epsilon difference threshold
122 * @returns true if difference is smaller than epsilon threshold, false otherwise
125 inline bool CompareType<Vector4>(Vector4 vector1, Vector4 vector2, float epsilon)
127 return fabsf(vector1.x - vector2.x)<epsilon &&
128 fabsf(vector1.y - vector2.y)<epsilon &&
129 fabsf(vector1.z - vector2.z)<epsilon &&
130 fabsf(vector1.w - vector2.w)<epsilon;
134 inline bool CompareType<Quaternion>(Quaternion q1, Quaternion q2, float epsilon)
136 Quaternion q2N = -q2; // These quaternions represent the same rotation
137 return CompareType<Vector4>(q1.mVector, q2.mVector, epsilon) || CompareType<Vector4>(q1.mVector, q2N.mVector, epsilon);
141 inline bool CompareType<Radian>(Radian q1, Radian q2, float epsilon)
143 return CompareType<float>(float(q1), float(q2), epsilon);
147 inline bool CompareType<Degree>(Degree q1, Degree q2, float epsilon)
149 return CompareType<float>(float(q1), float(q2), epsilon);
152 bool operator==(TimePeriod a, TimePeriod b);
153 std::ostream& operator<< (std::ostream& o, const TimePeriod value);
156 * Test whether two values are equal.
157 * @param[in] value1 The first value
158 * @param[in] value2 The second value
159 * @param[in] location The TEST_LOCATION macro should be used here
161 template<typename TypeA, typename TypeB>
162 inline void DALI_TEST_EQUALS(TypeA value1, TypeB value2, const char* location)
164 if (!(value1 == value2))
166 std::ostringstream o;
167 o << value1 << " == " << value2 << std::endl;
168 fprintf(stderr, "%s, checking %s", location, o.str().c_str());
169 tet_result(TET_FAIL);
173 tet_result(TET_PASS);
177 template<typename Type>
178 inline void DALI_TEST_EQUALS(Type value1, Type value2, float epsilon, const char* location)
180 if( !CompareType<Type>(value1, value2, epsilon) )
182 std::ostringstream o;
183 o << value1 << " == " << value2 << std::endl;
184 fprintf(stderr, "%s, checking %s", location, o.str().c_str());
185 tet_result(TET_FAIL);
189 tet_result(TET_PASS);
194 * Test whether two TimePeriods are within a certain distance of each other.
195 * @param[in] value1 The first value
196 * @param[in] value2 The second value
197 * @param[in] epsilon The values must be within this distance of each other
198 * @param[in] location The TEST_LOCATION macro should be used here
201 inline void DALI_TEST_EQUALS<TimePeriod>( TimePeriod value1, TimePeriod value2, float epsilon, const char* location)
203 if ((fabs(value1.durationSeconds - value2.durationSeconds) > epsilon))
205 fprintf(stderr, "%s, checking durations %f == %f, epsilon %f\n", location, value1.durationSeconds, value2.durationSeconds, epsilon);
206 tet_result(TET_FAIL);
208 else if ((fabs(value1.delaySeconds - value2.delaySeconds) > epsilon))
210 fprintf(stderr, "%s, checking delays %f == %f, epsilon %f\n", location, value1.delaySeconds, value2.delaySeconds, epsilon);
211 tet_result(TET_FAIL);
215 tet_result(TET_PASS);
220 * Test whether two Matrix3 objects are equal.
221 * @param[in] matrix1 The first object
222 * @param[in] matrix2 The second object
223 * @param[in] location The TEST_LOCATION macro should be used here
225 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location);
227 /** Test whether two Matrix3 objects are equal (fuzzy compare).
228 * @param[in] matrix1 The first object
229 * @param[in] matrix2 The second object
230 * @param[in] epsilon The epsilon to use for comparison
231 * @param[in] location The TEST_LOCATION macro should be used here
233 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location);
236 * Test whether two Matrix objects are equal.
237 * @param[in] matrix1 The first object
238 * @param[in] matrix2 The second object
239 * @param[in] location The TEST_LOCATION macro should be used here
241 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location);
244 * Test whether two Matrix objects are equal (fuzzy-compare).
245 * @param[in] matrix1 The first object
246 * @param[in] matrix2 The second object
247 * @param[in] location The TEST_LOCATION macro should be used here
249 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location);
252 * Test whether two strings are equal.
253 * @param[in] str1 The first string
254 * @param[in] str2 The second string
255 * @param[in] location The TEST_LOCATION macro should be used here
258 inline void DALI_TEST_EQUALS<const char*>( const char* str1, const char* str2, const char* location)
260 if (strcmp(str1, str2))
262 fprintf(stderr, "%s, checking '%s' == '%s'\n", location, str1, str2);
263 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( const std::string &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 char* str1, const std::string &str2, const char* location);
300 * Test whether two UTF32 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
306 inline void DALI_TEST_EQUALS<const Integration::TextArray&>( const Integration::TextArray& str1, const Integration::TextArray& str2, const char* location)
308 if( !std::equal( str1.Begin(), str1.End(), str2.Begin() ) )
310 fprintf(stderr, "%s, checking '", location);
312 for( unsigned int i = 0; i < str1.Count(); ++i )
314 fprintf(stderr, "%c", str1[i]);
317 fprintf(stderr, "' == '");
319 for( unsigned int i = 0; i < str2.Count(); ++i )
321 fprintf(stderr, "%c", str2[i]);
324 fprintf(stderr, "'\n");
326 tet_result(TET_FAIL);
330 tet_result(TET_PASS);
335 * Test whether one unsigned integer value is greater than another.
336 * Test succeeds if value1 > value2
337 * @param[in] value1 The first value
338 * @param[in] value2 The second value
339 * @param[in] location The TEST_LOCATION macro should be used here
341 void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location);
344 * Test whether one float value is greater than another.
345 * Test succeeds if value1 > value2
346 * @param[in] value1 The first value
347 * @param[in] value2 The second value
348 * @param[in] location The TEST_LOCATION macro should be used here
350 void DALI_TEST_GREATER( float value1, float value2, const char* location);
353 * Test whether the assertion condition that failed and thus triggered the
354 * exception \b e contained a given substring.
355 * @param[in] e The exception that we expect was fired by a runtime assertion 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.
360 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
364 * @param[in] e The exception that we expect was fired by a runtime assertion failure.
366 inline void DALI_TEST_PRINT_ASSERT( DaliException& e )
368 tet_printf("Assertion %s failed at %s\n", e.condition, e.location );
371 // Functor to test whether an Applied signal is emitted
372 struct ConstraintAppliedCheck
374 ConstraintAppliedCheck( bool& signalReceived );
375 void operator()( ActiveConstraint& constraint );
377 void CheckSignalReceived();
378 void CheckSignalNotReceived();
379 bool& mSignalReceived; // owned by individual tests
383 * A Helper to test default functions
385 template <typename T>
386 struct DefaultFunctionCoverage
388 DefaultFunctionCoverage()
399 // Helper to Create buffer image
400 BufferImage CreateBufferImage();
402 #endif // __DALI_TEST_SUITE_UTILS_H__