1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 #ifndef TEST_CASE_H 19 #define TEST_CASE_H 20 21 #ifndef TEST_RESULT_H 22 #include "test_result.h" 23 #endif 24 #ifndef STRINGABLE_H 25 #include "stringable.h" 26 #endif 27 28 #include "unit_test_args.h" 29 30 // $Id 31 // A composite test case; its atomic test can be redefined by overriding 32 // test(); it can also serve as a suite of tests by adding test_cases 33 // to it via the add_test_case function. It functions as a composite 34 // object (see the composite pattern in Design Patterns, p 163) without 35 // methods for getting individual children. 36 37 class test_case 38 { 39 protected: 40 //private methods and data 41 //the result of the last test 42 test_result m_last_result; 43 44 cmd_line* _cmdLinePtr; 45 46 //the collection of subtests 47 _VECTOR(test_case*, unit_test_allocator) m_subtests; 48 //runs the subtests 49 void run_subtests(void); 50 51 public: 52 //construction/destruction 53 test_case(void); 54 55 virtual ~test_case(void); 56 SetCommandLine(cmd_line * cmd_line)57 void SetCommandLine(cmd_line* cmd_line) 58 { 59 _cmdLinePtr = cmd_line; 60 }; 61 getCommandLine()62 cmd_line* getCommandLine() 63 { 64 return (_cmdLinePtr); 65 }; 66 67 //composition methods 68 //adds a child to the test case 69 void adopt_test_case(test_case* new_case); 70 //number of child test cases 71 int subtest_count(void) const; 72 73 //testing methods 74 //runs the test. This is a template method (Design Patterns 325) 75 void run_test(void); 76 //tests to see if the supplied condition is true; called by the test_is_true macro 77 void test_is_true_stub(bool condition, 78 const _STRING& condition_name, 79 const _STRING& filename, 80 long line_number); 81 //tests to see if two longs are equal 82 void test_int_is_equal_stub(long actual, 83 long expected, 84 const _STRING& filename, 85 long line_number); 86 //tests to see if two doubles are within limits 87 void test_double_is_equal_stub(double actual, 88 double expected, 89 const _STRING& filename, 90 long line_number, 91 double tolerance = 0.005); 92 //tests to see if two strings are equal 93 void test_string_is_equal_stub(const _STRING& actual, 94 const _STRING& expected, 95 const _STRING& filename, 96 long line_number); 97 //creates an "equality test failed" problem report 98 test_problem create_equality_problem_report(const _STRING& actual_message, 99 const _STRING& expected_message, 100 const _STRING& filename, 101 long line_number); 102 103 test_is_equal_stub(const T & actual,const T & expected,const _STRING & filename,long line_number)104 template< class T >void test_is_equal_stub(const T& actual, 105 const T& expected, 106 const _STRING& filename, 107 long line_number) 108 { 109 if (actual == expected) 110 { 111 m_last_result.add_success(); 112 } 113 else 114 { 115 _STRING actual_message = valueToString(actual); 116 _STRING expected_message = valueToString(expected); 117 118 m_last_result.add_failure(create_equality_problem_report(actual_message, 119 expected_message, 120 filename, 121 line_number)); 122 } 123 } 124 //the last result 125 const test_result& last_result(void) const; 126 127 //overrideables 128 //sets up the test 129 virtual void set_up(void); 130 //tears down after the test, and cleans up 131 virtual void tear_down(void); 132 //the test itself. It should either use the test_is_true/test_is_equal methods or 133 //append to the result itself 134 virtual void test(void); 135 }; 136 137 138 // A version of test case that does not do any native new/delete calls. 139 // This can be used to avoid invoking Oscl memory manager 140 // when the global new/delete overload is used. 141 // It's necessary when testing low-level Oscl features, but 142 // can be used anytime it's undesirable to have the test case 143 // allocated through Oscl memory manager. 144 class test_case_LL: public test_case, public UnitTest_HeapBase 145 { 146 }; 147 148 149 //these macros are used instead of the code stubs in test_case 150 //in order to use preprocessor features to get filename/line number 151 #define test_is_true( condition ) (this->test_is_true_stub( (condition), (#condition), __FILE__, __LINE__ )) 152 #define test_int_is_equal( actual_value, expected_value ) (this->test_int_is_equal_stub( actual_value, expected_value, __FILE__, __LINE__ )) 153 #define test_double_is_equal( actual_value, expected_value ) (this->test_double_is_equal_stub( actual_value, expected_value, __FILE__, __LINE__ )) 154 #define double_is_within_tolerance( actual_value, expected_value, tolerance ) ( this->test_double_is_equal_stub( actual_value, expected_value, __FILE__, __LINE__, tolerance )) 155 #define test_string_is_equal( actual_value, expected_value ) (this->test_string_is_equal_stub( actual_value, expected_value, __FILE__, __LINE__ )) 156 #define test_is_equal( actual_value, expected_value ) (this->test_is_equal_stub( actual_value, expected_value, __FILE__, __LINE__ )) 157 158 #endif 159 160 161