trie.t.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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[1] = 2;
  18. data[{0, 1}] = 4;
  19. }
  20. template <typename Iter, typename R>
  21. static void container_equals(Iter l_it, R const & rhs) {
  22. decltype(l_it) l_end{};
  23. auto r_it = std::begin(rhs), r_end = std::end(rhs);
  24. auto l_size = std::distance(l_it, l_end), r_size = std::distance(r_it, r_end);
  25. TS_ASSERT_EQUALS(l_size, r_size);
  26. for ( ; l_it != l_end && r_it != r_end; ++l_it, ++r_it ) {
  27. TS_ASSERT_EQUALS(*l_it, *r_it);
  28. }
  29. }
  30. public:
  31. void testIterationIsPreOrder() {
  32. int const expected[] = { -1, 5, 4, 2 };
  33. container_equals(data.begin(), expected);
  34. }
  35. void testPostIterationIsPostOrder() {
  36. int const expected[] = { -1, 2, 5, 4 };
  37. auto it = trie<int, int>::const_post_iterator{&data};
  38. container_equals(it, expected);
  39. }
  40. void testReverseIterationIsInvDepthFirst() {
  41. int const expected[] = { 2, 4, 5, -1 };
  42. container_equals(data.crbegin(), expected);
  43. }
  44. void testCopyCtorIsDeepCopy() {
  45. trie<int, int> copy{data};
  46. TS_ASSERT_EQUALS(data, copy);
  47. copy[{0, 1}] = 3;
  48. TS_ASSERT_DIFFERS(data, copy);
  49. }
  50. void testEqualsConsidersPaths() {
  51. trie<int, int> messed;
  52. messed = -1;
  53. messed[0] = 5;
  54. messed[1] = 2;
  55. messed[{0, 2}] = 4;
  56. TS_ASSERT_DIFFERS(data, messed);
  57. }
  58. void testInsertNewElementOutputsTrue() {
  59. trie<int, int> test;
  60. auto pair = test.insert(1, 2);
  61. TS_ASSERT_EQUALS(*pair.first, 2);
  62. TS_ASSERT(pair.second);
  63. }
  64. void testInsertNotDestructive() {
  65. trie<int, int> test;
  66. auto pair = test.insert(1, 2);
  67. pair = test.insert(1, 0);
  68. TS_ASSERT_EQUALS(*pair.first, 2);
  69. TS_ASSERT(!pair.second);
  70. }
  71. };