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 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
11 // http://floralicense.org/license/
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.
21 #include <dali/dali.h>
24 void tet_infoline(const char*str);
25 void tet_printf(const char *format, ...);
27 #include "test-application.h"
31 #define STRINGIZE_I(text) #text
32 #define STRINGIZE(text) STRINGIZE_I(text)
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__) ) ) )
44 extern int test_return_value;
46 void tet_result(int value);
49 return ((test_return_value>0)?1:0)
51 void tet_infoline(const char* str);
52 void tet_printf(const char *format, ...);
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
59 #define DALI_TEST_CHECK(condition) \
62 tet_result(TET_PASS); \
66 fprintf(stderr, "%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__); \
67 tet_result(TET_FAIL); \
70 template <typename Type>
71 inline bool CompareType(Type value1, Type value2, float epsilon);
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
81 inline bool CompareType<float>(float value1, float value2, float epsilon)
83 return fabsf(value1 - value2) < epsilon;
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
94 inline bool CompareType<Vector2>(Vector2 vector1, Vector2 vector2, float epsilon)
96 return fabsf(vector1.x - vector2.x)<epsilon && fabsf(vector1.y - vector2.y)<epsilon;
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
107 inline bool CompareType<Vector3>(Vector3 vector1, Vector3 vector2, float epsilon)
109 return fabsf(vector1.x - vector2.x)<epsilon &&
110 fabsf(vector1.y - vector2.y)<epsilon &&
111 fabsf(vector1.z - vector2.z)<epsilon;
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
123 inline bool CompareType<Vector4>(Vector4 vector1, Vector4 vector2, float epsilon)
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;
132 inline bool CompareType<Quaternion>(Quaternion q1, Quaternion q2, float epsilon)
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);
139 inline bool CompareType<Radian>(Radian q1, Radian q2, float epsilon)
141 return CompareType<float>(float(q1), float(q2), epsilon);
145 inline bool CompareType<Degree>(Degree q1, Degree q2, float epsilon)
147 return CompareType<float>(float(q1), float(q2), epsilon);
150 bool operator==(TimePeriod a, TimePeriod b);
151 std::ostream& operator<< (std::ostream& o, const TimePeriod value);
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
159 template<typename TypeA, typename TypeB>
160 inline void DALI_TEST_EQUALS(TypeA value1, TypeB value2, const char* location)
162 if (!(value1 == value2))
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);
171 tet_result(TET_PASS);
175 template<typename Type>
176 inline void DALI_TEST_EQUALS(Type value1, Type value2, float epsilon, const char* location)
178 if( !CompareType<Type>(value1, value2, epsilon) )
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);
187 tet_result(TET_PASS);
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
199 inline void DALI_TEST_EQUALS<TimePeriod>( TimePeriod value1, TimePeriod value2, float epsilon, const char* location)
201 if ((fabs(value1.durationSeconds - value2.durationSeconds) > epsilon))
203 fprintf(stderr, "%s, checking durations %f == %f, epsilon %f\n", location, value1.durationSeconds, value2.durationSeconds, epsilon);
204 tet_result(TET_FAIL);
206 else if ((fabs(value1.delaySeconds - value2.delaySeconds) > epsilon))
208 fprintf(stderr, "%s, checking delays %f == %f, epsilon %f\n", location, value1.delaySeconds, value2.delaySeconds, epsilon);
209 tet_result(TET_FAIL);
213 tet_result(TET_PASS);
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, "%s, checking '%s' == '%s'\n", location, str1, str2);
261 tet_result(TET_FAIL);
265 tet_result(TET_PASS);
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
276 inline void DALI_TEST_EQUALS<const std::string&>( const std::string &str1, const std::string &str2, const char* location)
278 DALI_TEST_EQUALS(str1.c_str(), str2.c_str(), location);
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
287 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location);
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
295 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location);
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
305 inline void DALI_TEST_EQUALS<const TextArray&>( const TextArray& str1, const TextArray& str2, const char* location)
307 if (!std::equal(str1.begin(), str1.end(), str2.begin()))
309 fprintf(stderr, "%s, checking '", location);
311 for( unsigned int i = 0; i < str1.size(); ++i)
313 fprintf(stderr, "%c", str1[i]);
316 fprintf(stderr, "' == '");
318 for( unsigned int i = 0; i < str2.size(); ++i)
320 fprintf(stderr, "%c", str2[i]);
323 fprintf(stderr, "'\n");
325 tet_result(TET_FAIL);
329 tet_result(TET_PASS);
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
340 void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location);
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
349 void DALI_TEST_GREATER( float value1, float value2, const char* location);
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
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 * @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.
365 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
367 /** Self-documenting wrapper for DALI_TEST_ASSERT.
368 * @copydoc DALI_TEST_ASSERT()
370 void DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( DaliException& exceptionFromAssertion, std::string conditionSubString, const char* location );
373 // Functor to test whether an Applied signal is emitted
374 struct ConstraintAppliedCheck
376 ConstraintAppliedCheck( bool& signalReceived );
377 void operator()( ActiveConstraint& constraint );
379 void CheckSignalReceived();
380 void CheckSignalNotReceived();
381 bool& mSignalReceived; // owned by individual tests
385 * A Helper to test default functions
387 template <typename T>
388 struct DefaultFunctionCoverage
390 DefaultFunctionCoverage()
401 // Helper to Create bitmap image
402 BitmapImage CreateBitmapImage();
404 #endif // __DALI_TEST_SUITE_UTILS_H__