1 |
Douglas Thrift |
1 |
// Unit Test |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
Douglas Thrift |
4 |
#include <string> |
8 |
Douglas Thrift |
14 |
#include <sstream> |
9 |
|
|
#include <cassert> |
10 |
Douglas Thrift |
1 |
#include "DoublyLinkedList.hpp" |
11 |
|
|
|
12 |
Douglas Thrift |
4 |
using namespace std; |
13 |
|
|
|
14 |
Douglas Thrift |
9 |
template<typename Type> |
15 |
Douglas Thrift |
14 |
inline bool contains(const Type& value, DoublyLinkedList<Type>& list) |
16 |
Douglas Thrift |
9 |
{ |
17 |
|
|
for (Iterator<Type> itor(list.iterator()); !itor.end(); itor++) |
18 |
|
|
{ |
19 |
|
|
if (*itor == value) return true; |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
return false; |
23 |
|
|
} |
24 |
|
|
|
25 |
Douglas Thrift |
10 |
class VowelVisitor : public Visitor<string> |
26 |
|
|
{ |
27 |
Douglas Thrift |
14 |
private: |
28 |
|
|
ostream* os; |
29 |
Douglas Thrift |
10 |
public: |
30 |
Douglas Thrift |
14 |
VowelVisitor(ostream& os) { this->os = &os; } |
31 |
|
|
void visit(const string& value) { if (value.find_first_of("aAeEiIoOuU") == 0) *os << value << '\n'; } |
32 |
Douglas Thrift |
10 |
}; |
33 |
|
|
|
34 |
Douglas Thrift |
1 |
int main(int argc, char* argv[]) |
35 |
|
|
{ |
36 |
|
|
DoublyLinkedList<int> list; |
37 |
|
|
|
38 |
Douglas Thrift |
4 |
list.addFront(1); |
39 |
|
|
list.addBack(2); |
40 |
Douglas Thrift |
6 |
list.addFront(4); |
41 |
|
|
list.addBack(5); |
42 |
Douglas Thrift |
4 |
|
43 |
Douglas Thrift |
7 |
// 4 1 2 5 |
44 |
|
|
|
45 |
Douglas Thrift |
14 |
assert(list.contains(1) == true); |
46 |
|
|
assert(list.contains(2) == true); |
47 |
|
|
assert(list.contains(3) == false); |
48 |
|
|
assert(list.contains(4) == true); |
49 |
|
|
assert(list.contains(5) == true); |
50 |
Douglas Thrift |
4 |
|
51 |
Douglas Thrift |
6 |
list.removeFirst(2); |
52 |
|
|
|
53 |
Douglas Thrift |
7 |
// 4 1 5 |
54 |
Douglas Thrift |
6 |
|
55 |
Douglas Thrift |
14 |
assert(list.contains(2) == false); |
56 |
|
|
assert(list.contains(5) == true); |
57 |
Douglas Thrift |
7 |
|
58 |
Douglas Thrift |
14 |
ostringstream sout; |
59 |
|
|
|
60 |
Douglas Thrift |
8 |
for (Iterator<int> itor(list.iterator()); !itor.end(); itor++) |
61 |
Douglas Thrift |
7 |
{ |
62 |
Douglas Thrift |
14 |
sout << *itor << '\n'; |
63 |
Douglas Thrift |
7 |
} |
64 |
|
|
|
65 |
Douglas Thrift |
14 |
assert(sout.str() == "4\n1\n5\n"); |
66 |
|
|
sout.str(""); |
67 |
|
|
assert(contains(3, list) == false); |
68 |
|
|
assert(contains(4, list) == true); |
69 |
Douglas Thrift |
9 |
|
70 |
|
|
DoublyLinkedList<string> words; |
71 |
|
|
|
72 |
|
|
words.addFront("Douglas"); |
73 |
|
|
words.addBack("Allen"); |
74 |
|
|
words.addFront("Edward"); |
75 |
|
|
words.addBack("attack"); |
76 |
|
|
words.addFront("Thrift"); |
77 |
|
|
|
78 |
|
|
for (Iterator<string> itor(words.iterator()); !itor.end(); itor++) |
79 |
|
|
{ |
80 |
Douglas Thrift |
14 |
if (itor->find_first_of("aAeEiIoOuU") == 0) sout << *itor << '\n'; |
81 |
Douglas Thrift |
9 |
} |
82 |
|
|
|
83 |
Douglas Thrift |
14 |
assert(sout.str() == "Edward\nAllen\nattack\n"); |
84 |
|
|
sout.str(""); |
85 |
Douglas Thrift |
10 |
|
86 |
Douglas Thrift |
14 |
VowelVisitor vowels(sout); |
87 |
|
|
|
88 |
Douglas Thrift |
10 |
words.accept(dynamic_cast<Visitor<string>*>(&vowels)); |
89 |
|
|
|
90 |
Douglas Thrift |
14 |
assert(sout.str() == "Edward\nAllen\nattack\n"); |
91 |
|
|
|
92 |
Douglas Thrift |
1 |
return 0; |
93 |
|
|
} |