• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "test_case.h"
19 
20 
21 //adopts a test case; we use "adopt" because we intend to take over
22 //the destruction of the passed object
23 void
adopt_test_case(test_case * new_case)24 test_case::adopt_test_case(test_case* new_case)
25 {
26     m_subtests.push_back(new_case);
27 }
28 
29 //the number of subtests
30 int
subtest_count(void) const31 test_case::subtest_count(void) const
32 {
33     return m_subtests.size();
34 }
35 
36 //runs all the subtests
37 void
run_subtests(void)38 test_case::run_subtests(void)
39 {
40     _VECTOR(test_case*, unit_test_allocator)::iterator iter = m_subtests.begin();
41     for (; iter != m_subtests.end();
42             ++iter)
43     {
44         (*iter)->run_test();
45         m_last_result.add_result((*iter)->last_result());
46     }
47 }
48 
49 //actually runs the test
50 void
run_test(void)51 test_case::run_test(void)
52 {
53     m_last_result.delete_contents();
54     set_up();
55     run_subtests();
56 
57     test();
58     tear_down();
59 }
60 
61 //tests to see if the supplied condition is true; called by the
62 //test_is_true macro
63 void
test_is_true_stub(bool condition,const _STRING & condition_name,const _STRING & filename,long line_number)64 test_case::test_is_true_stub(bool condition,
65                              const _STRING& condition_name,
66                              const _STRING& filename,
67                              long line_number)
68 {
69     if (condition)
70     {
71         m_last_result.add_success();
72     }
73     else
74     {
75 
76         _STRING message = "\"";
77         _APPEND(message, condition_name);
78         _APPEND(message, _STRING("\" evaluated to false."));
79 
80         m_last_result.add_failure(test_problem(filename,
81                                                line_number,
82                                                message));
83     }
84 }
85 
86 
87 //creates a test problem representing an inequality
88 test_problem
create_equality_problem_report(const _STRING & actual_message,const _STRING & expected_message,const _STRING & filename,long line_number)89 test_case::create_equality_problem_report(const _STRING& actual_message,
90         const _STRING& expected_message,
91         const _STRING& filename,
92         long line_number)
93 {
94     _STRING message = "Equality failed.  Got ";
95     _APPEND(message, actual_message);
96     _APPEND(message, ", expected ");
97     _APPEND(message, expected_message);
98     return test_problem(filename, line_number, message);
99 }
100 
101 
102 //checks to see if two longs are equal
103 //tests to see if two longs are equal
104 void
test_int_is_equal_stub(long actual,long expected,const _STRING & filename,long line_number)105 test_case::test_int_is_equal_stub(long actual,
106                                   long expected,
107                                   const _STRING& filename,
108                                   long line_number)
109 {
110     if (actual == expected)
111     {
112         m_last_result.add_success();
113     }
114     else
115     {
116         char temp[100];
117         sprintf(temp, "%ld", actual);
118         _STRING actual_message(temp);
119         sprintf(temp, "%ld", expected);
120         _STRING expected_message(temp);
121         m_last_result.add_failure(create_equality_problem_report(actual_message,
122                                   expected_message,
123                                   filename,
124                                   line_number));
125     }
126 }
127 
128 //replacement for stdio fabs routine
_fabs(double in)129 static double _fabs(double in)
130 {
131     if (in < 0)
132         return (-in);
133     else
134         return in;
135 }
136 
137 void
test_double_is_equal_stub(double actual,double expected,const _STRING & filename,long line_number,double tolerance)138 test_case::test_double_is_equal_stub(double actual,
139                                      double expected,
140                                      const _STRING& filename,
141                                      long line_number,
142                                      double tolerance)
143 {
144     if (_fabs(actual - expected) < _fabs(tolerance))
145     {
146         m_last_result.add_success();
147     }
148     else
149     {
150         char temp[100];
151         sprintf(temp, "%f", actual);
152         _STRING actual_message(temp);
153         sprintf(temp, "%f", expected);
154         _STRING expected_message(temp);
155 
156         m_last_result.add_failure(create_equality_problem_report(actual_message,
157                                   expected_message,
158                                   filename,
159                                   line_number));
160     }
161 }
162 
163 
164 //tests to see if two strings are equal
165 void
test_string_is_equal_stub(const _STRING & actual,const _STRING & expected,const _STRING & filename,long line_number)166 test_case::test_string_is_equal_stub(const _STRING& actual,
167                                      const _STRING& expected,
168                                      const _STRING& filename,
169                                      long line_number)
170 {
171     if (actual == expected)
172     {
173         m_last_result.add_success();
174     }
175     else
176     {
177         _STRING actual_message = _STRING("\"");
178         _APPEND(actual_message, actual);
179         _APPEND(actual_message, "\"");
180         _STRING expected_message = _STRING("\"");
181         _APPEND(expected_message, expected);
182         _APPEND(expected_message, "\"");
183 
184         m_last_result.add_failure(create_equality_problem_report(actual_message,
185                                   expected_message,
186                                   filename,
187                                   line_number));
188     }
189 }
190 
191 //sets up the test (here, does nothing)
192 void
set_up(void)193 test_case::set_up(void)
194 {
195 }
196 
197 //tears down the test (here, does nothing)
198 void
tear_down(void)199 test_case::tear_down(void)
200 {
201 }
202 
203 //the test itself; here, does nothing
204 void
test(void)205 test_case::test(void)
206 {
207 }
208 
209 //construction
test_case(void)210 test_case::test_case(void)
211 {
212     _cmdLinePtr = NULL;
213     m_last_result.delete_contents();
214     m_subtests.clear();
215 }
216 
217 //destruction
~test_case(void)218 test_case::~test_case(void)
219 {
220     m_last_result.delete_contents();
221 
222     _VECTOR(test_case*, unit_test_allocator)::iterator iter = m_subtests.begin();
223 
224     for (; iter != m_subtests.end();
225             ++iter)
226     {
227         delete((*iter));
228         (*iter) = NULL;
229     }
230     m_subtests.clear();
231 }
232 
233 //the last result after testing
234 const test_result&
last_result(void) const235 test_case::last_result(void) const
236 {
237     return m_last_result;
238 }
239 
240