Blame view
test_BST.cpp
1.97 KB
6040db9cb
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include "BST.hpp" #include <iostream> #include <algorithm> #include <vector> using namespace std; /** * A simple test driver for the BST class template. * P1 CSE 100 2013 * Author: P. Kube (c) 2013 */ int main() { /* Create an STL vector of some ints */ vector<int> v; v.push_back(3); v.push_back(4); v.push_back(1); v.push_back(100); v.push_back(-33); /* Create an instance of BST holding int */ BST<int> b; /* Insert a few data items. */ vector<int>::iterator vit = v.begin(); vector<int>::iterator ven = v.end(); for(; vit != ven; ++vit) { // all these inserts are unique, so should return a std::pair // with second part true std::pair<BST<int>::iterator,bool> pr = b.insert(*vit); if(! pr.second ) { cout << "Incorrect bool return value when inserting " << *vit << endl; return -1; } if(*(pr.first) != *vit) { cout << "Incorrect iterator return value when inserting " << *vit << endl; return -1; } } /* Test size. */ cout << "Size is: " << b.size() << endl; if(b.size() != v.size()) { cout << "... which is incorrect." << endl; return -1; } /* Test find return value. */ vit = v.begin(); for(; vit != ven; ++vit) { if(*(b.find(*vit)) != *vit) { cout << "Incorrect return value when finding " << *vit << endl; return -1; } } /* Sort the vector, to compare with inorder iteration on the BST */ sort(v.begin(),v.end()); /* Test BST iterator; should iterate inorder */ cout << "traversal using iterator:" << endl; vit = v.begin(); BST<int>::iterator en = b.end(); BST<int>::iterator it = b.begin(); for(; vit != ven; ++vit) { if(! (it != en) ) { cout << *it << "," << *vit << ": Early termination of BST iteration." << endl; return -1; } cout << *it << endl; if(*it != *vit) { cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl; return -1; } ++it; } cout << "OK." << endl; } |