Browse Source

Add test for empty tree and move constructor
- Fixing bug where a tree with only the root node crashes when attempting to iterate.

Sam Jaffe 7 years ago
parent
commit
9f5f144cd6
2 changed files with 11 additions and 0 deletions
  1. 1 0
      trie_iterator.hpp
  2. 10 0
      trie_test.cpp

+ 1 - 0
trie_iterator.hpp

@@ -61,6 +61,7 @@ namespace detail {
     }
   protected:
     void push(impl_t it) {
+      if (it.done()) { done = true; return; }
       iters.push(it);
       keys.push_back(it->first);
       stk.push(it->second.get());

+ 10 - 0
trie_test.cpp

@@ -45,6 +45,16 @@ std::vector<int> flatten(Iter it) {
   return out;
 }
 
+TEST_F(TrieTest, DefaultConstructorHasNoElementsExceptRoot) {
+  Tree empty;
+  EXPECT_THAT(flatten(empty.cbegin()), std::vector<int>{0});
+}
+
+TEST_F(TrieTest, MoveConstructorIsDestructive) {
+  Tree moved = std::move(data);
+  EXPECT_THAT(flatten(data.cbegin()), std::vector<int>{0});
+}
+
 TEST_F(TrieTest, CopyConstructorIsDeep) {
   Tree copy = data;
   copy[{0, 1}] += 1;