ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/ccscs20/UnitTest.cpp
Revision: 14
Committed: 2004-05-15T15:41:10-07:00 (21 years, 1 month ago) by Douglas Thrift
File size: 1816 byte(s)
Log Message:
If this isn't a Unit Test, what is?

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id