Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. #include "./util.h"
  5. #include "./Person.h"
  6. #include "./LinkedList.h"
  7. #include "./BinaryTree.h"
  8.  
  9. using namespace std;
  10.  
  11. //------------------------------------------------------------
  12. // Tests
  13. //------------------------------------------------------------
  14. int runTests() {
  15.  
  16.   int numFails = 0;
  17.   int testNum = 0;
  18.  
  19.    {
  20.      LinkedList<int> list;
  21.      list.insert(new ListNode<int>(3));
  22.      TEST(list.toString() == "3");
  23.  
  24.      list.insert(new ListNode<int>(8));
  25.      cout << list << endl;
  26.      TEST(list.toString() == "8 -> 3");
  27.  
  28.      list.insert(new ListNode<int>(5));
  29.      TEST(list.toString() == "5 -> 8 -> 3");
  30.  
  31.      // find() needs to be implemented.
  32.      ListNode<int>* node;
  33.      node = list.find(8);
  34.      TEST(node->data() == 8);
  35.  
  36.      node = list.find(7);
  37.      TEST(node == NULL);
  38.  
  39.      // remove() needs to be implemented. It will use the
  40.      // find() function.
  41.      list.remove(8);
  42.      TEST(list.toString() == "5 -> 3");
  43.  
  44.      list.insert(new ListNode<int>(2));
  45.      list.insert(new ListNode<int>(11));
  46.      list.insert(new ListNode<int>(7));
  47.      TEST(list.toString() == "7 -> 11 -> 2 -> 5 -> 3");
  48.  
  49.      list.remove(7);
  50.      TEST(list.toString() == "11 -> 2 -> 5 -> 3");
  51.  
  52.      list.remove(3);
  53.      TEST(list.toString() == "11 -> 2 -> 5");
  54.  
  55.    }
  56.  
  57. //   {
  58. //     LinkedList<string> list;
  59. //     list.insert(new ListNode<string>("ABC"));
  60. //     list.insert(new ListNode<string>("DEF"));
  61. //     list.insert(new ListNode<string>("GHI"));
  62. //     list.insert(new ListNode<string>("JKL"));
  63. //     list.remove("GHI");
  64. //     TEST(list.toString() == "JKL -> DEF -> ABC");
  65. //   }
  66.  
  67.    {
  68.      LinkedList<Person> list;
  69.      list.insert(new ListNode<Person>(Person("Theresa May", 55)));
  70.      list.insert(new ListNode<Person>(Person("Tony Blair", 70)));
  71.      list.insert(new ListNode<Person>(Person("Margaret Thatcher", 91)));
  72.      list.insert(new ListNode<Person>(Person("Winston Churchill", 88)));
  73.      cout << list.toString() << endl;
  74.      TEST(list.toString() == "Winston Churchill 88 -> Margaret Thatcher 91 -> Tony Blair 70 -> Theresa May 55");
  75.    }
  76.  
  77. //   {
  78. //     // Implement BinaryTree in BinaryTree.h. Use the LinkedList class
  79. //     // as a model.
  80. //     BinaryTree<int> tree;
  81. //     tree.setHead(new BNode<int>(3));
  82. //     tree.head()->setLeft(new BNode<int>(8));
  83. //     tree.head()->setRight(new BNode<int>(12));
  84. //     TEST(tree.head()->data() == 3);
  85. //     TEST(tree.head()->left()->data() == 8);
  86. //     TEST(tree.head()->right()->data() == 12);
  87. //     cout << tree.toDot() << endl;
  88.  
  89. //     // This syntax allows us to put a string literal in the code
  90. //     // without escaping the double quotes. Please let Dr. Edwards
  91. //     // know if it doesn't compile.
  92. //     string target = R"str(
  93. // digraph G {
  94. // graph [ordering="out"]
  95. // "3" -> "8"
  96. // "3" -> "12"
  97. // }
  98. // )str";
  99. //     // stringEquals tests strings and ignores whitespace.
  100. //     TEST(stringEquals(tree.toDot(), target));
  101.  
  102. //     tree.head()->left()->setLeft(new BNode<int>(2));
  103. //     tree.head()->left()->setRight(new BNode<int>(18));
  104. //     tree.head()->right()->setLeft(new BNode<int>(5));
  105. //     cout << tree.toDot() << endl;
  106. //     target = R"str(
  107. // digraph G {
  108. // graph [ordering="out"]
  109. // "3" -> "8"
  110. // "3" -> "12"
  111. // "8" -> "2"
  112. // "8" -> "18"
  113. // "12" -> "5"
  114. // }
  115. // )str";
  116. //     TEST(stringEquals(tree.toDot(), target));
  117.  
  118. //     // While the linked list find function is iterative, you
  119. //     // will want to implement your tree algorithm recursively.
  120. //     BNode<int>* n = tree.find(18, tree.head());
  121. //     TEST(n->data() == 18);
  122. //     n = tree.find(8, tree.head());
  123. //     TEST(n->data() == 8);
  124. //     n = tree.find(7, tree.head());
  125. //     TEST(n == NULL);
  126.  
  127. //     n = tree.find(18, tree.head());
  128. //     n->setLeft(new BNode<int>(11));
  129. //     n->setRight(new BNode<int>(13));
  130. //     cout << tree.toDot() << endl;
  131. //     target = R"str(
  132. // digraph G {
  133. // graph [ordering="out"]
  134. // "3" -> "8"
  135. // "3" -> "12"
  136. // "8" -> "2"
  137. // "8" -> "18"
  138. // "18" -> "11"
  139. // "18" -> "13"
  140. // "12" -> "5"
  141. // }
  142. // )str";
  143. //     TEST(stringEquals(tree.toDot(), target));
  144.  
  145. //     n = tree.find(3, tree.head());
  146. //     n->setRight(new BNode<int>(4));
  147. //     cout << tree.toDot() << endl;
  148. //     target = R"str(
  149. // digraph G {
  150. // graph [ordering="out"]
  151. // "3" -> "8"
  152. // "3" -> "4"
  153. // "8" -> "2"
  154. // "8" -> "18"
  155. // "18" -> "11"
  156. // "18" -> "13"
  157. // }
  158. // )str";
  159. //     TEST(stringEquals(tree.toDot(), target));
  160. //   }
  161.  
  162. //   {
  163. //     BinaryTree<Person> tree;
  164. //     tree.setHead(new BNode<Person>(Person("Theresa May", 55)));
  165. //     tree.head()->setLeft(new BNode<Person>(Person("Tony Blair", 70)));
  166. //     tree.head()->setRight(
  167. //         new BNode<Person>(Person("Margaret Thatcher", 91)));
  168. //     tree.head()->left()->setLeft(
  169. //         new BNode<Person>(Person("Winston Churchill", 88)));
  170. //     cout << tree.toDot() << endl;
  171. //     string target = R"str(
  172. // digraph G {
  173. // graph [ordering="out"]
  174. // "Theresa May 55" -> "Tony Blair 70"
  175. // "Theresa May 55" -> "Margaret Thatcher 91"
  176. // "Tony Blair 70" -> "Winston Churchill 88"
  177. // }
  178. // )str";
  179. //     TEST(stringEquals(tree.toDot(), target));
  180.  
  181. //   }
  182.  
  183.   const int numSuccesses = testNum - numFails;
  184.   cout << numSuccesses << "/" << testNum << " tests succeeded" << endl;
  185.  
  186.   return numFails;
  187. }
  188.  
  189. int main() {
  190.   return runTests();
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement