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, then the function & line number is printed.
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); \
71 template <typename Type>
72 inline bool CompareType(Type value1, Type value2, float epsilon);
75 * A helper for fuzzy-comparing Vector2 objects
76 * @param[in] vector1 the first object
77 * @param[in] vector2 the second object
78 * @param[in] epsilon difference threshold
79 * @returns true if difference is smaller than epsilon threshold, false otherwise
82 inline bool CompareType<float>(float value1, float value2, float epsilon)
84 return fabsf(value1 - value2) < epsilon;
88 * A helper for fuzzy-comparing Vector2 objects
89 * @param[in] vector1 the first object
90 * @param[in] vector2 the second object
91 * @param[in] epsilon difference threshold
92 * @returns true if difference is smaller than epsilon threshold, false otherwise
95 inline bool CompareType<Vector2>(Vector2 vector1, Vector2 vector2, float epsilon)
97 return fabsf(vector1.x - vector2.x)<epsilon && fabsf(vector1.y - vector2.y)<epsilon;
101 * A helper for fuzzy-comparing Vector3 objects
102 * @param[in] vector1 the first object
103 * @param[in] vector2 the second object
104 * @param[in] epsilon difference threshold
105 * @returns true if difference is smaller than epsilon threshold, false otherwise
108 inline bool CompareType<Vector3>(Vector3 vector1, Vector3 vector2, float epsilon)
110 return fabsf(vector1.x - vector2.x)<epsilon &&
111 fabsf(vector1.y - vector2.y)<epsilon &&
112 fabsf(vector1.z - vector2.z)<epsilon;
117 * A helper for fuzzy-comparing Vector4 objects
118 * @param[in] vector1 the first object
119 * @param[in] vector2 the second object
120 * @param[in] epsilon difference threshold
121 * @returns true if difference is smaller than epsilon threshold, false otherwise
124 inline bool CompareType<Vector4>(Vector4 vector1, Vector4 vector2, float epsilon)
126 return fabsf(vector1.x - vector2.x)<epsilon &&
127 fabsf(vector1.y - vector2.y)<epsilon &&
128 fabsf(vector1.z - vector2.z)<epsilon &&
129 fabsf(vector1.w - vector2.w)<epsilon;
133 inline bool CompareType<Quaternion>(Quaternion q1, Quaternion q2, float epsilon)
135 Quaternion q2N = -q2; // These quaternions represent the same rotation
136 return CompareType<Vector4>(q1.mVector, q2.mVector, epsilon) || CompareType<Vector4>(q1.mVector, q2N.mVector, epsilon);
140 inline bool CompareType<Radian>(Radian q1, Radian q2, float epsilon)
142 return CompareType<float>(float(q1), float(q2), epsilon);
146 inline bool CompareType<Degree>(Degree q1, Degree q2, float epsilon)
148 return CompareType<float>(float(q1), float(q2), epsilon);
151 bool operator==(TimePeriod a, TimePeriod b);
152 std::ostream& operator<< (std::ostream& o, const TimePeriod value);
155 * Test whether two values are equal.
156 * @param[in] value1 The first value
157 * @param[in] value2 The second value
158 * @param[in] location The TEST_LOCATION macro should be used here
160 template<typename TypeA, typename TypeB>
161 inline void DALI_TEST_EQUALS(TypeA value1, TypeB value2, const char* location)
163 if (!(value1 == value2))
165 std::ostringstream o;
166 o << value1 << " == " << value2 << std::endl;
167 fprintf(stderr, "%s, checking %s", location, o.str().c_str());
168 tet_result(TET_FAIL);
172 tet_result(TET_PASS);
176 template<typename Type>
177 inline void DALI_TEST_EQUALS(Type value1, Type value2, float epsilon, const char* location)
179 if( !CompareType<Type>(value1, value2, epsilon) )
181 std::ostringstream o;
182 o << value1 << " == " << value2 << std::endl;
183 fprintf(stderr, "%s, checking %s", location, o.str().c_str());
184 tet_result(TET_FAIL);
188 tet_result(TET_PASS);
193 * Test whether two TimePeriods are within a certain distance of each other.
194 * @param[in] value1 The first value
195 * @param[in] value2 The second value
196 * @param[in] epsilon The values must be within this distance of each other
197 * @param[in] location The TEST_LOCATION macro should be used here
200 inline void DALI_TEST_EQUALS<TimePeriod>( TimePeriod value1, TimePeriod value2, float epsilon, const char* location)
202 if ((fabs(value1.durationSeconds - value2.durationSeconds) > epsilon))
204 fprintf(stderr, "%s, checking durations %f == %f, epsilon %f\n", location, value1.durationSeconds, value2.durationSeconds, epsilon);
205 tet_result(TET_FAIL);
207 else if ((fabs(value1.delaySeconds - value2.delaySeconds) > epsilon))
209 fprintf(stderr, "%s, checking delays %f == %f, epsilon %f\n", location, value1.delaySeconds, value2.delaySeconds, epsilon);
210 tet_result(TET_FAIL);
214 tet_result(TET_PASS);
219 * Test whether two Matrix3 objects are equal.
220 * @param[in] matrix1 The first object
221 * @param[in] matrix2 The second object
222 * @param[in] location The TEST_LOCATION macro should be used here
224 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location);
226 /** Test whether two Matrix3 objects are equal (fuzzy compare).
227 * @param[in] matrix1 The first object
228 * @param[in] matrix2 The second object
229 * @param[in] epsilon The epsilon to use for comparison
230 * @param[in] location The TEST_LOCATION macro should be used here
232 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location);
235 * Test whether two Matrix objects are equal.
236 * @param[in] matrix1 The first object
237 * @param[in] matrix2 The second object
238 * @param[in] location The TEST_LOCATION macro should be used here
240 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location);
243 * Test whether two Matrix objects are equal (fuzzy-compare).
244 * @param[in] matrix1 The first object
245 * @param[in] matrix2 The second object
246 * @param[in] location The TEST_LOCATION macro should be used here
248 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location);
251 * Test whether two strings are equal.
252 * @param[in] str1 The first string
253 * @param[in] str2 The second string
254 * @param[in] location The TEST_LOCATION macro should be used here
257 inline void DALI_TEST_EQUALS<const char*>( const char* str1, const char* str2, const char* location)
259 if (strcmp(str1, str2))
261 fprintf(stderr, "%s, checking '%s' == '%s'\n", location, str1, str2);
262 tet_result(TET_FAIL);
266 tet_result(TET_PASS);
271 * Test whether two strings are equal.
272 * @param[in] str1 The first string
273 * @param[in] str2 The second string
274 * @param[in] location The TEST_LOCATION macro should be used here
277 inline void DALI_TEST_EQUALS<const std::string&>( const std::string &str1, const std::string &str2, const char* location)
279 DALI_TEST_EQUALS(str1.c_str(), str2.c_str(), location);
283 * Test whether two strings are equal.
284 * @param[in] str1 The first string
285 * @param[in] str2 The second string
286 * @param[in] location The TEST_LOCATION macro should be used here
288 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location);
291 * Test whether two strings are equal.
292 * @param[in] str1 The first string
293 * @param[in] str2 The second string
294 * @param[in] location The TEST_LOCATION macro should be used here
296 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 TextArray&>( const TextArray& str1, const 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.size(); ++i)
314 fprintf(stderr, "%c", str1[i]);
317 fprintf(stderr, "' == '");
319 for( unsigned int i = 0; i < str2.size(); ++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 at the start of its literal text.
355 * @param[in] e The exception that we expect was fired by a runtime assertion
357 * @param[in] conditionSubString The text that we expect to be present in an
358 * assertion which triggered the exception.
359 * @param[in] location The TEST_LOCATION macro should be used here.
361 * @remark **Side-effects:** The result of the tet test is set to TET_PASS if
362 * the substring is at the start of the exception's condition and
363 * TET_FAIL if it isn't. Note, if the result of a test is set multiple
364 * times, a TET_FAIL will override any number of TET_PASSes.
366 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
368 /** Self-documenting wrapper for DALI_TEST_ASSERT.
369 * @copydoc DALI_TEST_ASSERT()
371 void DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( DaliException& exceptionFromAssertion, std::string conditionSubString, const char* location );
374 // Functor to test whether an Applied signal is emitted
375 struct ConstraintAppliedCheck
377 ConstraintAppliedCheck( bool& signalReceived );
378 void operator()( ActiveConstraint& constraint );
380 void CheckSignalReceived();
381 void CheckSignalNotReceived();
382 bool& mSignalReceived; // owned by individual tests
386 * A Helper to test default functions
388 template <typename T>
389 struct DefaultFunctionCoverage
391 DefaultFunctionCoverage()
402 // Helper to Create bitmap image
403 BitmapImage CreateBitmapImage();
404 BitmapImage CreateBitmapImage(int width, int height, const Vector4& color);
407 #endif // __DALI_TEST_SUITE_UTILS_H__