Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include "BST.hpp"
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9.  
  10. /**
  11. * A simple test driver for the BST class template.
  12. * P1 CSE 100 2012
  13. * Author: P. Kube (c) 2012
  14. */
  15. int main() {
  16.  
  17. /* Create an STL vector of some ints */
  18. vector<int> v;
  19. v.push_back(3);
  20. v.push_back(4);
  21. v.push_back(1);
  22. v.push_back(100);
  23. v.push_back(-33);
  24.  
  25. /* Create an instance of BST holding int */
  26. BST<int> b;
  27.  
  28. /* Insert a few data items. */
  29. vector<int>::iterator vit = v.begin();
  30. vector<int>::iterator ven = v.end();
  31. cout << "Performing BST insertion test .... " << endl;
  32. for(; vit != ven; ++vit) {
  33. // all these inserts are unique, so should return true
  34. if(! b.insert(*vit) ) {
  35. cout << "Incorrect return value when inserting " << *vit << endl;
  36. return -1;
  37. }
  38. }
  39.  
  40. vit = v.begin();
  41. for(; vit != ven; ++vit) {
  42. // all these inserts are duplicates, so should return false
  43. if( b.insert(*vit) ) {
  44. cout << "Incorrect return value when re-inserting " << *vit << endl;
  45. return -1;
  46. }
  47. }
  48. cout << "Insertion test passed!" << endl;
  49.  
  50. /* Test size. */
  51. cout << "Performing get size test ...." << endl;
  52. cout << "Size is: " << b.size() << endl;
  53. if(b.size() != v.size()) {
  54. cout << "... which is incorrect." << endl;
  55. return -1;
  56. }
  57. cout << "Get size test passed!" << endl;
  58.  
  59. /* Test find return value. */
  60. cout << "Performing find item test ...." << endl;
  61. vit = v.begin();
  62. for(; vit != ven; ++vit) {
  63. if(*(b.find(*vit)) != *vit) {
  64. cout << "Incorrect return value when finding " << *vit << endl;
  65. return -1;
  66. }
  67. }
  68. cout << "Find item test passed!" << endl;
  69.  
  70.  
  71. /* Sort the vector, to compare with inorder iteration on the BST */
  72. sort(v.begin(),v.end());
  73.  
  74.  
  75. /* Test BST iterator; should iterate inorder */
  76. cout << "Performing iterator test ...." << endl;
  77. cout << "traversal using iterator:" << endl;
  78. vit = v.begin();
  79. BST<int>::iterator it = b.begin();
  80. for(; vit != ven; ++vit) {
  81. cout << *it << endl;
  82. if(*it != *vit) {
  83. cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
  84. return -1;
  85. }
  86. ++it;
  87. }
  88. cout << "Iterator test passed!" << endl;
  89.  
  90. cout << "Great! All tests passed!" << endl;
  91. cout << "Keep in mind that this testbench is not comprehensive." << endl;
  92. cout << "We will use more complicated testbench to grade your project" << endl;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement