| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- //
- // trie.t.h
- // trie
- //
- // Created by Sam Jaffe on 6/16/17.
- //
- #pragma once
- #include <cxxtest/TestSuite.h>
- #include "trie.hpp"
- class trie_TestSuite : public CxxTest::TestSuite {
- private:
- trie<int, int> data;
-
- void setUp() override {
- data.clear();
- data = -1;
- data[0] = 5;
- data[1] = 2;
- data[{0, 1}] = 4;
- }
-
- template <typename Iter, typename R>
- static void container_equals(Iter l_it, R const & rhs) {
- decltype(l_it) l_end{};
- auto r_it = std::begin(rhs), r_end = std::end(rhs);
- auto l_size = std::distance(l_it, l_end), r_size = std::distance(r_it, r_end);
- TS_ASSERT_EQUALS(l_size, r_size);
- for ( ; l_it != l_end && r_it != r_end; ++l_it, ++r_it ) {
- TS_ASSERT_EQUALS(*l_it, *r_it);
- }
- }
- public:
- void testIterationIsPreOrder() {
- int const expected[] = { -1, 5, 4, 2 };
- container_equals(data.begin(), expected);
- }
-
- void testPostIterationIsPostOrder() {
- int const expected[] = { -1, 2, 5, 4 };
- auto it = trie<int, int>::const_post_iterator{&data};
- container_equals(it, expected);
- }
- void testReverseIterationIsInvDepthFirst() {
- int const expected[] = { 2, 4, 5, -1 };
- container_equals(data.crbegin(), expected);
- }
- void testCopyCtorIsDeepCopy() {
- trie<int, int> copy{data};
- TS_ASSERT_EQUALS(data, copy);
- copy[{0, 1}] = 3;
- TS_ASSERT_DIFFERS(data, copy);
- }
-
- void testEqualsConsidersPaths() {
- trie<int, int> messed;
- messed = -1;
- messed[0] = 5;
- messed[1] = 2;
- messed[{0, 2}] = 4;
- TS_ASSERT_DIFFERS(data, messed);
- }
-
- void testInsertNewElementOutputsTrue() {
- trie<int, int> test;
- auto pair = test.insert(1, 2);
- TS_ASSERT_EQUALS(*pair.first, 2);
- TS_ASSERT(pair.second);
- }
- void testInsertNotDestructive() {
- trie<int, int> test;
- auto pair = test.insert(1, 2);
- pair = test.insert(1, 0);
- TS_ASSERT_EQUALS(*pair.first, 2);
- TS_ASSERT(!pair.second);
- }
- };
|