trie.t.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // trie.t.h
  3. // trie
  4. //
  5. // Created by Sam Jaffe on 6/16/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "trie.hpp"
  10. class trie_TestSuite : public CxxTest::TestSuite {
  11. private:
  12. trie<int, int> data;
  13. void setUp() override {
  14. data.clear();
  15. data = 1;
  16. data[0] = 5;
  17. data[{0,1}] = 4;
  18. data[{0,1,0}] = 3;
  19. data[{0,1,2}] = 6;
  20. data[{0,2,0}] = 7;
  21. data[1] = 2;
  22. data[{1,1}] = 8;
  23. data[{1,1,0}] = 9;
  24. }
  25. template <typename Iter, typename R>
  26. static void container_equals(Iter l_it, R const & rhs) {
  27. decltype(l_it) l_end{};
  28. auto r_it = std::begin(rhs), r_end = std::end(rhs);
  29. auto l_size = std::distance(l_it, l_end), r_size = std::distance(r_it, r_end);
  30. TS_ASSERT_EQUALS(l_size, r_size);
  31. for ( ; l_it != l_end && r_it != r_end; ++l_it, ++r_it ) {
  32. TS_ASSERT_EQUALS(*l_it, *r_it);
  33. }
  34. }
  35. public:
  36. // Parent, Left -> Right
  37. void testIterationIsPreOrder() {
  38. int const expected[] = { 1, 5, 4, 3, 6, 0, 7, 2, 8, 9 };
  39. container_equals(data.begin(), expected);
  40. }
  41. // Parent, Right -> Left
  42. void testPostIterationIsPostOrder() {
  43. int const expected[] = { 1, 2, 8, 9, 5, 0, 7, 4, 6, 3 };
  44. auto it = trie<int, int>::const_post_iterator{&data};
  45. container_equals(it, expected);
  46. }
  47. // Right -> Left, Parent
  48. void testReverseIterationIsInvDepthFirst() {
  49. int const expected[] = { 9, 8, 2, 7, 0, 6, 3, 4, 5, 1 };
  50. container_equals(data.crbegin(), expected);
  51. }
  52. void testCopyCtorIsDeepCopy() {
  53. trie<int, int> copy{data};
  54. TS_ASSERT_EQUALS(data, copy);
  55. copy[{0, 1}] = -1;
  56. TS_ASSERT_DIFFERS(data, copy);
  57. }
  58. void testEqualsConsidersPaths() {
  59. trie<int, int> t1, t2;
  60. t1[1] = 1;
  61. t2[2] = 1;
  62. TS_ASSERT_DIFFERS(t1, t2);
  63. }
  64. void testInsertNewElementOutputsTrue() {
  65. trie<int, int> test;
  66. auto pair = test.insert(1, 2);
  67. TS_ASSERT_EQUALS(*pair.first, 2);
  68. TS_ASSERT(pair.second);
  69. }
  70. void testEmplaceNewElementOutputsTrue() {
  71. trie<int, int> test;
  72. auto pair = test.emplace(1, 2);
  73. TS_ASSERT_EQUALS(*pair.first, 2);
  74. TS_ASSERT(pair.second);
  75. }
  76. void testInsertArrayOfElementCreatesAllEntries() {
  77. trie<int, int> test;
  78. auto pair = test.insert({1, 1, 1}, 2);
  79. TS_ASSERT_EQUALS(*pair.first, 2);
  80. TS_ASSERT(pair.second);
  81. TS_ASSERT_EQUALS(test[1][1][1].value(), 2);
  82. }
  83. void testInsertNotDestructive() {
  84. trie<int, int> test;
  85. auto pair = test.insert(1, 2);
  86. pair = test.insert(1, 0);
  87. TS_ASSERT_EQUALS(*pair.first, 2);
  88. TS_ASSERT(!pair.second);
  89. }
  90. void testCanLocateElement() {
  91. TS_ASSERT_EQUALS(*data.find({0, 1, 2}), 6);
  92. }
  93. void testFindAbsentElementReturnsEnd() {
  94. TS_ASSERT_EQUALS(data.find({0, 3, 2}), data.end());
  95. }
  96. void testEraseDropsEntireBranch() {
  97. data.erase(0);
  98. TS_ASSERT_EQUALS(data.find(0), data.end());
  99. }
  100. };