/ds/fifo/dotest.cpp

http://stuffpack.googlecode.com/ · C++ · 382 lines · 307 code · 45 blank · 30 comment · 2 complexity · adf280a277b22517f4678e30733fbcc6 MD5 · raw file

  1. #include <iostream>
  2. #include "dotest.h"
  3. #include "lifo.h"
  4. #include "fifo.h"
  5. #include "ll.h"
  6. #include "sorted.h"
  7. #include "OutOfBoundsException.h"
  8. #include "DuplicateEntry.h"
  9. /*
  10. * try initilization copy and copy constructor
  11. */
  12. /* all functions return 0 on success and non-zero otherwise */
  13. /* t_ == test */
  14. Node<int>* t_node;
  15. Fifo<int> t_fifo;
  16. Lifo<int> t_lifo;
  17. LinkedList<int> myll;
  18. Sorted<int> t_sorted;
  19. /* m_ == magic */
  20. const int m_node_data = 5;
  21. const int m_fo_nodata = -1;
  22. int suite_node_init(void)
  23. {
  24. t_node = new Node<int>(m_node_data);
  25. return 0;
  26. }
  27. int suite_node_clean(void)
  28. {
  29. delete t_node;
  30. return 0;
  31. }
  32. void test_node_f_data(void)
  33. {
  34. CU_ASSERT_EQUAL(t_node->data, m_node_data);
  35. }
  36. void test_fifo_pop(void)
  37. {
  38. int i=1;
  39. t_fifo.push(i++);
  40. t_fifo.push(i++);
  41. t_fifo.push(i++);
  42. i=1;
  43. /* in init we pushed 1, 2, 3 */
  44. CU_ASSERT_EQUAL(t_fifo.pop(), i++);
  45. CU_ASSERT_EQUAL(t_fifo.pop(), i++);
  46. CU_ASSERT_EQUAL(t_fifo.pop(), i++);
  47. // CU_ASSERT_EQUAL(t_fifo.pop(), m_fo_nodata);
  48. // i++;
  49. }
  50. void test_fifo_pushpop(void)
  51. {
  52. t_fifo.push(6);
  53. CU_ASSERT_EQUAL(t_fifo.pop(), 6);
  54. }
  55. void test_fifo_hasnext(void)
  56. {
  57. CU_ASSERT_FALSE(t_fifo.hasNext());
  58. t_fifo.push(12);
  59. CU_ASSERT_TRUE(t_fifo.hasNext());
  60. (void)t_fifo.pop();
  61. CU_ASSERT_FALSE(t_fifo.hasNext());
  62. }
  63. void test_lifo_pop(void)
  64. {
  65. t_lifo.push(1);
  66. t_lifo.push(2);
  67. t_lifo.push(3);
  68. CU_ASSERT_EQUAL(t_lifo.pop(), 3);
  69. CU_ASSERT_EQUAL(t_lifo.pop(), 2);
  70. CU_ASSERT_EQUAL(t_lifo.pop(), 1);
  71. }
  72. void test_ll_works(void)
  73. {
  74. //myll.clear();
  75. myll.push(10);
  76. myll.push(11);
  77. myll.push(12);
  78. //LL now looks like 10 11 12
  79. CU_ASSERT_EQUAL(myll.read(0),10);
  80. CU_ASSERT_EQUAL(myll.read(1),11);
  81. CU_ASSERT_EQUAL(myll.read(2),12);
  82. CU_ASSERT_EQUAL(myll.getNumNodes(),3);
  83. myll.push(4,1);
  84. //LL now looks like 10 4 11 12
  85. CU_ASSERT_EQUAL(myll.read(0),10);
  86. CU_ASSERT_EQUAL(myll.read(1),4);
  87. CU_ASSERT_EQUAL(myll.read(2),11);
  88. CU_ASSERT_EQUAL(myll.read(3),12);
  89. CU_ASSERT_EQUAL(myll.getNumNodes(),4);
  90. //LL now looks like 10 4 11 12
  91. // 0 1 2 3
  92. myll.remove(2);
  93. //LL now looks like 10 4 12
  94. CU_ASSERT_EQUAL(myll.read(0),10);
  95. CU_ASSERT_EQUAL(myll.read(1),4);
  96. CU_ASSERT_EQUAL(myll.read(2),12);
  97. CU_ASSERT_EQUAL(myll.getNumNodes(),3);
  98. myll.remove(0);
  99. //LL now looks like 4 12
  100. CU_ASSERT_EQUAL(myll.read(0),4);
  101. CU_ASSERT_EQUAL(myll.read(1),12);
  102. CU_ASSERT_EQUAL(myll.getNumNodes(),2);
  103. }
  104. //This gets run after works();
  105. void test_ll_operators(void)
  106. {
  107. //The [] operator assumes we are sane so don't bother testing undefuned values
  108. CU_ASSERT_EQUAL(myll[0],4);
  109. CU_ASSERT_EQUAL(myll[1],12);
  110. //Test both sane and insane values
  111. CU_ASSERT_EQUAL(myll(0),4);
  112. CU_ASSERT_EQUAL(myll(1),12);
  113. bool did_catch_outofbounds_exception = false;
  114. try {
  115. CU_ASSERT_EQUAL(myll(myll.getNumNodes()+1),4);
  116. }
  117. catch (OutOfBoundsException e){
  118. did_catch_outofbounds_exception = true;
  119. }
  120. CU_ASSERT_TRUE(did_catch_outofbounds_exception);
  121. }
  122. void test_ll_clear(void)
  123. {
  124. myll.clear();
  125. CU_ASSERT_EQUAL(myll.getNumNodes(),0);
  126. }
  127. void test_ll_except(void)
  128. {
  129. bool did_catch_read_exception = false;
  130. try
  131. {
  132. myll.read(0);
  133. }
  134. catch(OutOfBoundsException& e)
  135. {
  136. did_catch_read_exception = true;
  137. }
  138. CU_ASSERT_TRUE(did_catch_read_exception);
  139. did_catch_read_exception = false;
  140. try
  141. {
  142. myll.read(10);
  143. }
  144. catch(OutOfBoundsException& e)
  145. {
  146. did_catch_read_exception = true;
  147. }
  148. CU_ASSERT_TRUE(did_catch_read_exception);
  149. myll.push(1);
  150. myll.push(2);
  151. myll.push(3);
  152. bool did_catch_push_exception = false;
  153. try
  154. {
  155. myll.push(10,500);
  156. }
  157. catch(OutOfBoundsException& e)
  158. {
  159. did_catch_push_exception = true;
  160. }
  161. CU_ASSERT_TRUE(did_catch_push_exception);
  162. bool did_catch_swap_exception = false;
  163. try
  164. {
  165. myll.swap(0,45);
  166. }
  167. catch(OutOfBoundsException& e)
  168. {
  169. did_catch_swap_exception = true;
  170. }
  171. CU_ASSERT_TRUE(did_catch_swap_exception)
  172. did_catch_swap_exception = false;
  173. try
  174. {
  175. myll.swap(45,0);
  176. }
  177. catch(OutOfBoundsException& e)
  178. {
  179. did_catch_swap_exception = true;
  180. }
  181. CU_ASSERT_TRUE(did_catch_swap_exception)
  182. /*
  183. Create test for swap bound exceptions
  184. */
  185. }
  186. void test_ll_swap(void)
  187. {
  188. // myll.clear();
  189. myll.push(1);
  190. myll.push(2);
  191. myll.push(3);
  192. myll.push(4);
  193. myll.push(5);
  194. /* Array now looks like 1 2 3 4 5*/
  195. myll.swap(0,2);
  196. /* Array now looks like 3 2 1 4 5*/
  197. CU_ASSERT_EQUAL(myll.read(0), 3);
  198. CU_ASSERT_EQUAL(myll.read(1), 2);
  199. CU_ASSERT_EQUAL(myll.read(2), 1);
  200. CU_ASSERT_EQUAL(myll.read(3), 4);
  201. CU_ASSERT_EQUAL(myll.read(4), 5);
  202. }
  203. void test_lifo_pushpop(void)
  204. {
  205. t_lifo.push(4);
  206. CU_ASSERT_EQUAL(t_lifo.pop(),4);
  207. }
  208. void test_lifo_hasnext(void)
  209. {
  210. CU_ASSERT_FALSE(t_lifo.hasNext());
  211. t_lifo.push(12);
  212. CU_ASSERT_TRUE(t_lifo.hasNext());
  213. (void)t_lifo.pop();
  214. CU_ASSERT_FALSE(t_lifo.hasNext());
  215. }
  216. void test_lifo_numnodes(void)
  217. {
  218. t_lifo.push(9);
  219. t_lifo.push(10);
  220. t_lifo.push(11);
  221. CU_ASSERT_EQUAL(t_lifo.getNumNodes(),3);
  222. }
  223. void test_sorted_read(void)
  224. {
  225. /* perhaps we should also warn when data is out of order on a (data,location) push ?*/
  226. t_sorted.push(2);
  227. t_sorted.push(1);
  228. t_sorted.push(3);
  229. t_sorted.push(7);
  230. t_sorted.push(5);
  231. t_sorted.push(6);
  232. t_sorted.push(4);
  233. CU_ASSERT_EQUAL(t_sorted.read(0), 1);
  234. CU_ASSERT_EQUAL(t_sorted.read(1), 2);
  235. CU_ASSERT_EQUAL(t_sorted.read(2), 3);
  236. CU_ASSERT_EQUAL(t_sorted.read(3), 4);
  237. CU_ASSERT_EQUAL(t_sorted.read(4), 5);
  238. CU_ASSERT_EQUAL(t_sorted.read(5), 6);
  239. CU_ASSERT_EQUAL(t_sorted.read(6), 7);
  240. }
  241. void test_sorted_duplicate(void)
  242. {
  243. /* test high number */
  244. bool did_catch_duplicate_exception = false;
  245. try
  246. {
  247. t_sorted.push(1000);
  248. t_sorted.push(1000);
  249. }
  250. catch(DuplicateEntryException& e)
  251. {
  252. did_catch_duplicate_exception = true;
  253. }
  254. CU_ASSERT_TRUE(did_catch_duplicate_exception);
  255. /* test head */
  256. did_catch_duplicate_exception = false;
  257. try
  258. {
  259. t_sorted.push(1);
  260. }
  261. catch(DuplicateEntryException& e)
  262. {
  263. did_catch_duplicate_exception = true;
  264. }
  265. CU_ASSERT_TRUE(did_catch_duplicate_exception);
  266. }
  267. void test_ll_copy(void)
  268. {
  269. myll.clear();
  270. myll.push(1);
  271. CU_ASSERT_EQUAL(1,myll.read(0));
  272. LinkedList<int> icopyll = myll;
  273. CU_ASSERT_EQUAL(1,myll.read(0));
  274. CU_ASSERT_EQUAL(1,icopyll.read(0));
  275. icopyll.remove(0);
  276. CU_ASSERT_TRUE(myll.has(1));
  277. CU_ASSERT_FALSE(icopyll.has(1));
  278. LinkedList<int> copyll;
  279. copyll = myll;
  280. CU_ASSERT_EQUAL(1,myll.read(0));
  281. CU_ASSERT_EQUAL(1,copyll.read(0));
  282. copyll.remove(0);
  283. CU_ASSERT_TRUE(myll.has(1));
  284. CU_ASSERT_FALSE(copyll.has(1));
  285. }
  286. int fifo_doTest(void)
  287. {
  288. /* create the registry */
  289. if (CUE_SUCCESS != CU_initialize_registry())
  290. {
  291. return CU_get_error();
  292. }
  293. CU_TestInfo test_array_node[] = {
  294. { "\t accepts data correctly", test_node_f_data },
  295. CU_TEST_INFO_NULL,
  296. };
  297. CU_TestInfo test_array_fifo[] = {
  298. { "\t pops data correctly", test_fifo_pop },
  299. { "\t pops data correctly after new push", test_fifo_pushpop },
  300. { "\t hasnext works", test_fifo_hasnext },
  301. CU_TEST_INFO_NULL,
  302. };
  303. CU_TestInfo test_array_lifo[] = {
  304. { "\t pops data correctly", test_lifo_pop },
  305. { "\t pops data correctly after new push", test_lifo_pushpop },
  306. { "\t hasnext works", test_lifo_hasnext },
  307. CU_TEST_INFO_NULL,
  308. };
  309. CU_TestInfo test_array_ll[] = {
  310. { "\t can accept data and get data from any point", test_ll_works},
  311. { "\t operators work as expected", test_ll_operators },
  312. { "\t clear() clears all elements", test_ll_clear },
  313. { "\t exceptions work", test_ll_except },
  314. { "\t swapping works", test_ll_swap },
  315. { "\t copying array works; modifying new data does not affect old data", test_ll_copy },
  316. CU_TEST_INFO_NULL,
  317. };
  318. CU_TestInfo test_array_sorted[] = {
  319. { "\t pushes data in correct order", test_sorted_read },
  320. { "\t returns exception on duplicate entry", test_sorted_duplicate },
  321. CU_TEST_INFO_NULL,
  322. };
  323. CU_SuiteInfo suites[] = {
  324. { "Node", suite_node_init, suite_node_clean, test_array_node },
  325. { "Fifo", NULL, NULL, test_array_fifo },
  326. { "Lifo", NULL, NULL, test_array_lifo },
  327. { "LinkedList", NULL, NULL, test_array_ll },
  328. { "Sorted LinkedLink", NULL, NULL, test_array_sorted },
  329. CU_SUITE_INFO_NULL,
  330. };
  331. CU_ErrorCode error = CU_register_suites(suites);
  332. /* Run all tests using the CUnit Basic interface */
  333. CU_basic_set_mode(CU_BRM_VERBOSE);
  334. CU_basic_run_tests();
  335. CU_cleanup_registry();
  336. return CU_get_error();
  337. }