--- UnitTest.cpp 2004/05/14 00:50:29 4 +++ UnitTest.cpp 2004/05/15 22:41:10 14 @@ -4,22 +4,90 @@ // // $Id$ -#include #include +#include +#include #include "DoublyLinkedList.hpp" using namespace std; +template +inline bool contains(const Type& value, DoublyLinkedList& list) +{ + for (Iterator itor(list.iterator()); !itor.end(); itor++) + { + if (*itor == value) return true; + } + + return false; +} + +class VowelVisitor : public Visitor +{ +private: + ostream* os; +public: + VowelVisitor(ostream& os) { this->os = &os; } + void visit(const string& value) { if (value.find_first_of("aAeEiIoOuU") == 0) *os << value << '\n'; } +}; + int main(int argc, char* argv[]) { DoublyLinkedList list; list.addFront(1); list.addBack(2); - cout.setf(ios_base::boolalpha); + list.addFront(4); + list.addBack(5); + + // 4 1 2 5 + + assert(list.contains(1) == true); + assert(list.contains(2) == true); + assert(list.contains(3) == false); + assert(list.contains(4) == true); + assert(list.contains(5) == true); + + list.removeFirst(2); + + // 4 1 5 + + assert(list.contains(2) == false); + assert(list.contains(5) == true); + + ostringstream sout; + + for (Iterator itor(list.iterator()); !itor.end(); itor++) + { + sout << *itor << '\n'; + } + + assert(sout.str() == "4\n1\n5\n"); + sout.str(""); + assert(contains(3, list) == false); + assert(contains(4, list) == true); + + DoublyLinkedList words; + + words.addFront("Douglas"); + words.addBack("Allen"); + words.addFront("Edward"); + words.addBack("attack"); + words.addFront("Thrift"); + + for (Iterator itor(words.iterator()); !itor.end(); itor++) + { + if (itor->find_first_of("aAeEiIoOuU") == 0) sout << *itor << '\n'; + } + + assert(sout.str() == "Edward\nAllen\nattack\n"); + sout.str(""); + + VowelVisitor vowels(sout); + + words.accept(dynamic_cast*>(&vowels)); - cout << "list.contains(1) = " << list.contains(1) << '\n' - << "list.contains(3) = " << list.contains(3) << '\n'; + assert(sout.str() == "Edward\nAllen\nattack\n"); return 0; }