1 | //////////////////////////////////////////////////////////////////////////////
|
2 | // CTA++, C++ Test Aider/Visual Studio Integration v2.0 //
|
3 | // //
|
4 | // GENERATED TEST SCRIPT FILE //
|
5 | // //
|
6 | // Copyright (c) [generated parts] 2003-2004 Testwell Oy //
|
7 | //////////////////////////////////////////////////////////////////////////////
|
8 | //
|
9 | // CTA/VSI: File created: Jan 22 2004 11:09:47
|
10 | // CTA/VSI: Last CTA/VSI update: Jan 23 2004 11:31:16
|
11 | // Last user update: <date> (when edit, you may wish to maintain this)
|
12 | //
|
13 | // Here you may wish to insert version control hooks, like
|
14 | // $RCSfile$, $Revision$, $Date$, $Author$ and $Log$
|
15 | //
|
16 | // Note 1. Lines starting with '// CTA/VSI:' are auxiliary lines to
|
17 | // CTA++/Visual Studio Integration. DO NOT EDIT THESE LINES!
|
18 | // Note 2. Lines starting with '// TODO:' advises where especially you
|
19 | // can and sometimes must insert test code. You can edit these freely.
|
20 | //////////////////////////////////////////////////////////////////////////////
|
21 | //
|
22 | // TODO: Uncomment the following definition, if old streams...
|
23 | // #define CTA_OLDSTREAM
|
24 |
|
25 | // Enable the CTA++ macro language and class interface
|
26 | #include <cta.h>
|
27 |
|
28 | // TODO: Include code under test headers as required by C++ rules.
|
29 | // Remember extern "C" {...} wrapping if testing plain C code.
|
30 |
|
31 | // ----------------------------------------------------------------------
|
32 | // The class under test, 'List'. Also auxiliary classes
|
33 | // 'List_element', 'List_iterator' and 'List_reverse_iterator'.
|
34 | // ----------------------------------------------------------------------
|
35 |
|
36 | #include "list.h"
|
37 |
|
38 | // Get CTA_ prefixless aliases for the test macros defined in 'cta.h'
|
39 | // If name conflicts, see CTA++ User's Guide for resolving.
|
40 | #include <ctanames.h>
|
41 |
|
42 | // Images //
|
43 | // TODO: Add image macros IMAGE... ENDIMAGE and IMAGE_CAST
|
44 | // to display objects of user's type
|
45 |
|
46 | // ----------------------------------------------------------------------
|
47 | // Image-function to display objects of user's type
|
48 | // 'ExceptionIteratorOutOfBounds'
|
49 | // ----------------------------------------------------------------------
|
50 |
|
51 | IMAGE(ExceptionIteratorOutOfBounds e)
|
52 | CTA_img.noAggregate(); // CTA_img is an "implicit" parameter to IMAGE
|
53 | CTA_img << e.getMsg();
|
54 | ENDIMAGE
|
55 |
|
56 | // Exception handlers //
|
57 | // TODO: User's exceptions to be caught by name by the test driver
|
58 | // (this means: when the user's code escapes with one of the exceptions
|
59 | // below, CTA++ is able to report the type of the exception)
|
60 | // ----------------------------------------------------------------------
|
61 |
|
62 | HANDLER
|
63 | CATCH(ExceptionEmptyList)
|
64 | CATCH(ExceptionIteratorOutOfBounds)
|
65 | CATCH(ExceptionOutOfMemory)
|
66 | ENDHANDLER
|
67 |
|
68 | // Stubs //
|
69 | // TODO: Add stub functions (if introduced them manually)
|
70 | // Remember extern "C" {...} wrapping, if stubbing plain C code.
|
71 |
|
72 | // ----------------------------------------------------------------------
|
73 | // Stubs needed by class 'List'.
|
74 | // ----------------------------------------------------------------------
|
75 | #include "CTA_memory.h_stb.inc"
|
76 | // CTA/VSI: Include stub function files above
|
77 |
|
78 | // Test case functions //
|
79 | // TODO: Define test case functions
|
80 |
|
81 | // Below are the test-case functions used in testing the class 'List'
|
82 |
|
83 | // ----------------------------------------------------------------------
|
84 | // Test-case function testing an empty 'List'
|
85 | // ----------------------------------------------------------------------
|
86 | void CTA_test_case_1(void) {
|
87 | // TODO: Add test macros here
|
88 | HARD_MSG("This test case tests the functionality of 'List' methods "
|
89 | "when called on an empty 'List' object."); NL;
|
90 |
|
91 | MSG("Create a List");
|
92 | DECL(List l);
|
93 |
|
94 | NL; MSG("The List should be initially empty.");
|
95 | ASSERT(l.empty());
|
96 |
|
97 | NL; MSG("It means that item count should be 0");
|
98 | ASSERT_EQ(l.itemCount(), 0);
|
99 |
|
100 | NL; MSG("An attempt to extract something from an empty list should "
|
101 | "cause an exception to be thrown.");
|
102 | DECL(int i);
|
103 | ASSERT_EXCEPTION(i = l.extract(), ExceptionEmptyList);
|
104 |
|
105 | NL; MSG("Taking an iterator to the begin and end of the list should yield "
|
106 | "iterators with the status 'end'");
|
107 | ASSERT(l.begin().end());
|
108 | ASSERT(l.last().end());
|
109 | ASSERT(l.rbegin().rend());
|
110 |
|
111 | NL; MSG("Trying to access with '*' an item in empty list should "
|
112 | "cause exception");
|
113 | ASSERT_EXCEPTION(*l.begin(), ExceptionIteratorOutOfBounds);
|
114 |
|
115 | NL; MSG("Trying to update an empty iterator with '++' should "
|
116 | "cause exception");
|
117 | ASSERT_EXCEPTION(++(l.begin()), ExceptionIteratorOutOfBounds);
|
118 |
|
119 | NL; MSG("Inserting an item (7) into the list.");
|
120 | DO(l.insert(7));
|
121 |
|
122 | MSG("The list should no longer be empty.");
|
123 | ASSERT(!l.empty());
|
124 |
|
125 | MSG("The item count should be 1");
|
126 | ASSERT_EQ(l.itemCount(), 1);
|
127 |
|
128 | MSG("Extracting the item...");
|
129 | ASSERT_EQ(l.extract(), 7);
|
130 |
|
131 | MSG("Now the list should be empty again.");
|
132 | ASSERT(l.empty());
|
133 | ASSERT_EQ(l.itemCount(), 0);
|
134 |
|
135 | MSG("Now the stub 'List_free' should be invoked because 'l' "
|
136 | "goes out of scope and its destructor calls 'List_free'.");
|
137 | NL;
|
138 | }
|
139 |
|
140 | // ----------------------------------------------------------------------
|
141 | // Test-case function that inserts and extracts integers into and
|
142 | // out of a 'List', asserting that correct items were extracted.
|
143 | // ----------------------------------------------------------------------
|
144 | void CTA_test_case_2(CTA_TestData& td) {
|
145 | // TODO: Add test macros here
|
146 | HARD_MSG("This test case inserts integers into a 'List', and "
|
147 | "then extracts them, asserting that correct items get "
|
148 | "extracted."); NL;
|
149 |
|
150 | MSG("At this point we set the verbosity of the stubs to BRIEF."); NL;
|
151 |
|
152 | DO(List_malloc_stub.beBrief());
|
153 | DO(List_realloc_stub.beBrief());
|
154 | DO(List_free_stub.beBrief());
|
155 |
|
156 | NL; MSG("Create some variables."); NL;
|
157 |
|
158 | DECL(List l);
|
159 | DECL(int i);
|
160 | DECL(int n = 0);
|
161 |
|
162 | NL; MSG("Insert integers read from a data file into List 'l'");
|
163 |
|
164 | while (!td.end()) {
|
165 | DO(td >> i);
|
166 | PUT(i);
|
167 | DO(l.insert(i); n++);
|
168 | }
|
169 |
|
170 | NL; MSG("Assert that item count equals the number of items inserted "
|
171 | "into the List");
|
172 | ASSERT_EQ(l.itemCount(), n);
|
173 |
|
174 | NL; MSG("Assert that the List contains correct items, i.e. those that "
|
175 | "were just inserted there; a List_iterator is created "
|
176 | "for scanning through the List");
|
177 |
|
178 | DO(td.rewind());
|
179 | DECL(List_iterator iter = l.begin());
|
180 |
|
181 | while (!td.end()) {
|
182 | DO(td >> i);
|
183 | PUT(i);
|
184 | ASSERT_EQ(*iter, i);
|
185 | DO(++iter);
|
186 | NL;
|
187 | }
|
188 |
|
189 | NL; MSG("Assert that there are no more items to be iterated in the List");
|
190 | ASSERT(iter.end());
|
191 |
|
192 | NL; MSG("And the List 'l' goes out of scope...");
|
193 | }
|
194 |
|
195 | // ----------------------------------------------------------------------
|
196 | // Test-case function testing the List method 'remove'
|
197 | // ----------------------------------------------------------------------
|
198 | void CTA_test_case_3(void) {
|
199 | // TODO: Add test macros here
|
200 | HARD_MSG("Test the List method 'remove'"); NL;
|
201 |
|
202 | DECL(List l);
|
203 |
|
204 | NL; MSG("Insert some int's into the List l"); NL;
|
205 |
|
206 | DO(l.insert(3));
|
207 | DO(l.insert(1));
|
208 | DO(l.insert(2));
|
209 | DO(l.insert(0));
|
210 | DO(l.insert(123));
|
211 |
|
212 | NL; MSG("Remove the int's from the list one by one; assert that "
|
213 | "item count has decreased accordingly"); NL;
|
214 |
|
215 | ASSERT_EQ(l.itemCount(), 5);
|
216 | DO(l.remove(2));
|
217 | ASSERT_EQ(l.itemCount(), 4);
|
218 | DO(l.remove(3));
|
219 | ASSERT_EQ(l.itemCount(), 3);
|
220 | DO(l.remove(123));
|
221 | ASSERT_EQ(l.itemCount(), 2);
|
222 | DO(l.remove(0));
|
223 | ASSERT_EQ(l.itemCount(), 1);
|
224 | DO(l.remove(1));
|
225 | ASSERT_EQ(l.itemCount(), 0);
|
226 | ASSERT(l.empty());
|
227 | ASSERT_EXCEPTION(l.extract(), ExceptionEmptyList);
|
228 | }
|
229 |
|
230 | // ----------------------------------------------------------------------
|
231 | // Test-case function doing a stress test on 'List'; insert a lot of
|
232 | // items into the list and extract them; assert that correct items
|
233 | // get extracted
|
234 | // ----------------------------------------------------------------------
|
235 | void CTA_test_case_4(int stress) {
|
236 | // TODO: Add test macros here
|
237 | HARD_MSG("Do a little stress test. "
|
238 | "Insert a lot of integers!"); NL;
|
239 |
|
240 | PUT(stress);
|
241 |
|
242 | DECL(List l2);
|
243 |
|
244 | for (int j = 0; j < stress; ++j) {
|
245 | l2.insert(j);
|
246 | }
|
247 |
|
248 | ASSERT_EQ(l2.itemCount(), stress);
|
249 |
|
250 | NL; MSG("Now doing so many assertions that BRIEF mode is entered "
|
251 | "for the time of the assertions in order to avoid unreasonably "
|
252 | "long trace...");
|
253 |
|
254 | DO(CTA_TestDriver::whichDriver()->beBrief());
|
255 |
|
256 | for (int jj = stress - 1; jj >= 0; --jj) {
|
257 | ASSERT_EQ(l2.extract(), jj);
|
258 | }
|
259 |
|
260 | DO(CTA_TestDriver::whichDriver()->beVerbose());
|
261 |
|
262 | MSG("Assertions done");
|
263 | }
|
264 |
|
265 | // ----------------------------------------------------------------------
|
266 | // Test-case function testing 'List' in out of memory conditions.
|
267 | // ----------------------------------------------------------------------
|
268 | void CTA_test_case_5(void) {
|
269 | // TODO: Add test macros here
|
270 | HARD_MSG("This test case tests the class 'List' in out-of-memory "
|
271 | "condition. The condition is achieved by setting the stubs "
|
272 | "List_malloc and List_realloc to return 0 when called. "
|
273 | "This is done via the corresponding stub objects "
|
274 | "List_malloc_stub and List_realloc_stub."); NL;
|
275 |
|
276 |
|
277 | MSG("Set the stub List_realloc to return 0 when called "
|
278 | "to simulate the out of memory condition");
|
279 | DO(List_realloc_stub << (void*)0);
|
280 |
|
281 | DECL(List l);
|
282 |
|
283 | NL; MSG("The statement block below should throw exception at the point "
|
284 | "where 'insert' finally calls 'List_realloc'!");
|
285 |
|
286 | ASSERT_TRY
|
287 | for (int i = 0; i < 20; ++i) {
|
288 | DO(l.insert(i*i));
|
289 | }
|
290 | ASSERT_CATCH(ExceptionOutOfMemory);
|
291 |
|
292 | NL; MSG("Set the stub List_malloc to return 0 when called "
|
293 | "to simulate the out of memory condition");
|
294 | DO(List_malloc_stub << (void*)0);
|
295 |
|
296 | MSG("Try to create a 'List', should throw exception!");
|
297 | ASSERT_EXCEPTION(DECL(List l2), ExceptionOutOfMemory);
|
298 | }
|
299 |
|
300 | // ----------------------------------------------------------------------
|
301 | // Test-case function testing 'List_reverse_iterator'
|
302 | // ----------------------------------------------------------------------
|
303 | void CTA_test_case_6(void) {
|
304 | // TODO: Add test macros here
|
305 | HARD_MSG("This test case tests the List_reverse_iterator");
|
306 |
|
307 | List_malloc_stub.beSilent();
|
308 | List_realloc_stub.beSilent();
|
309 | List_free_stub.beSilent();
|
310 |
|
311 | List_malloc_stub << CTA_Stub::body(1);
|
312 | List_realloc_stub << CTA_Stub::body(1);
|
313 | List_free_stub << CTA_Stub::body(1);
|
314 |
|
315 | DECL(List l);
|
316 | for (int i = -5; i <= 5; ++i) {
|
317 | DO(l.insert(i));
|
318 | }
|
319 | DECL(List_reverse_iterator riter = l.rbegin());
|
320 | DECL(List l2);
|
321 | while (!riter.rend()) {
|
322 | DO(l2.insert(*riter));
|
323 | DO(--riter);
|
324 | }
|
325 |
|
326 | DECL(List_iterator iter = l2.begin());
|
327 | DO(riter = l.rbegin());
|
328 | while (!iter.end()) {
|
329 | ASSERT_EQ(*riter, *iter);
|
330 | DO(--riter; ++iter);
|
331 | }
|
332 |
|
333 | List_malloc_stub.beBrief();
|
334 | List_realloc_stub.beBrief();
|
335 | List_free_stub.beBrief();
|
336 | }
|
337 |
|
338 | // CTA/VSI: Define test case functions above
|
339 |
|
340 | // The main test script function //
|
341 | int main(int argc, char* argv[])
|
342 | {
|
343 | // Create a test driver object 'd'
|
344 | DRIVER(d);
|
345 |
|
346 | // TODO: Add object definitions needed in test case function registrations
|
347 |
|
348 | d.add(CTA_TestCase<void>(CTA_test_case_1,
|
349 | "1", "test empty list"));
|
350 | d.add(CTA_TestCase<void>(CTA_test_case_2, "2@CTA_vsList_drv.dat",
|
351 | "2", "test list"));
|
352 | d.add(CTA_TestCase<void>(CTA_test_case_3,
|
353 | "3", "test 'List::remove'"));
|
354 | d.add(CTA_TestCase<int>(CTA_test_case_4, 132,
|
355 | "4", "stress test"));
|
356 | d.add(CTA_TestCase<void>(CTA_test_case_5,
|
357 | "5", "out of memory-test"));
|
358 | d.add(CTA_TestCase<void>(CTA_test_case_6,
|
359 | "6", "test List_reverse_iterator"));
|
360 | // CTA/VSI: Register test cases in 'd' above
|
361 |
|
362 | // Run the test cases and return status code to the shell level
|
363 | return d.run(argc, argv);
|
364 | }
|
365 | // EOF
|