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>
23 #include <dali/public-api/dali-core-capi-internal.h>
26 void tet_infoline(const char*str);
27 void tet_printf(const char *format, ...);
29 #include "test-application.h"
33 #define STRINGIZE_I(text) #text
34 #define STRINGIZE(text) STRINGIZE_I(text)
36 // the following is the other compilers way of token pasting, gcc seems to just concatenate strings automatically
37 //#define TOKENPASTE(x,y) x ## y
38 #define TOKENPASTE(x,y) x y
39 #define TOKENPASTE2(x,y) TOKENPASTE( x, y )
40 #define TEST_LOCATION TOKENPASTE2( "Test failed in ", TOKENPASTE2( __FILE__, TOKENPASTE2( ", line ", STRINGIZE(__LINE__) ) ) )
46 extern int test_return_value;
48 void tet_result(int value);
51 return ((test_return_value>0)?1:0)
53 void tet_infoline(const char* str);
54 void tet_printf(const char *format, ...);
57 * DALI_TEST_CHECK is a wrapper for tet_result.
58 * If the condition evaluates to false, then the function & line number is printed.
59 * @param[in] The boolean expression to check
61 #define DALI_TEST_CHECK(condition) \
64 tet_result(TET_PASS); \
68 fprintf(stderr, "%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__); \
69 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);
301 * Test whether two UTF32 strings are equal.
302 * @param[in] str1 The first string
303 * @param[in] str2 The second string
304 * @param[in] location The TEST_LOCATION macro should be used here
307 inline void DALI_TEST_EQUALS<const TextArray&>( const TextArray& str1, const TextArray& str2, const char* location)
309 if (!std::equal(str1.begin(), str1.end(), str2.begin()))
311 fprintf(stderr, "%s, checking '", location);
313 for( unsigned int i = 0; i < str1.size(); ++i)
315 fprintf(stderr, "%c", str1[i]);
318 fprintf(stderr, "' == '");
320 for( unsigned int i = 0; i < str2.size(); ++i)
322 fprintf(stderr, "%c", str2[i]);
325 fprintf(stderr, "'\n");
327 tet_result(TET_FAIL);
331 tet_result(TET_PASS);
336 * Test whether one unsigned integer value is greater than another.
337 * Test succeeds if value1 > value2
338 * @param[in] value1 The first value
339 * @param[in] value2 The second value
340 * @param[in] location The TEST_LOCATION macro should be used here
342 void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location);
345 * Test whether one float value is greater than another.
346 * Test succeeds if value1 > value2
347 * @param[in] value1 The first value
348 * @param[in] value2 The second value
349 * @param[in] location The TEST_LOCATION macro should be used here
351 void DALI_TEST_GREATER( float value1, float value2, const char* location);
354 * Test whether the assertion condition that failed and thus triggered the
355 * exception \b e contained a given substring at the start of its literal text.
356 * @param[in] e The exception that we expect was fired by a runtime assertion
358 * @param[in] conditionSubString The text that we expect to be present in an
359 * assertion which triggered the exception.
360 * @param[in] location The TEST_LOCATION macro should be used here.
362 * @remark **Side-effects:** The result of the tet test is set to TET_PASS if
363 * the substring is at the start of the exception's condition and
364 * TET_FAIL if it isn't. Note, if the result of a test is set multiple
365 * times, a TET_FAIL will override any number of TET_PASSes.
367 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
369 /** Self-documenting wrapper for DALI_TEST_ASSERT.
370 * @copydoc DALI_TEST_ASSERT()
372 void DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( DaliException& exceptionFromAssertion, std::string conditionSubString, const char* location );
375 // Functor to test whether an Applied signal is emitted
376 struct ConstraintAppliedCheck
378 ConstraintAppliedCheck( bool& signalReceived );
379 void operator()( ActiveConstraint& constraint );
381 void CheckSignalReceived();
382 void CheckSignalNotReceived();
383 bool& mSignalReceived; // owned by individual tests
387 * A Helper to test default functions
389 template <typename T>
390 struct DefaultFunctionCoverage
392 DefaultFunctionCoverage()
403 // Helper to Create bitmap image
404 BitmapImage CreateBitmapImage();
405 BitmapImage CreateBitmapImage(int width, int height, const Vector4& color);
408 #endif // __DALI_TEST_SUITE_UTILS_H__