瀏覽代碼

Add initializer_list-ish constructor.
Increase test coverage to 100%.

Sam Jaffe 5 年之前
父節點
當前提交
adb5222748
共有 4 個文件被更改,包括 130 次插入71 次删除
  1. 2 1
      include/trie/trie.hpp
  2. 8 0
      include/trie/trie.tpp
  3. 116 66
      test/trie_test.cpp
  4. 4 4
      trie.xcodeproj/project.pbxproj

+ 2 - 1
include/trie/trie.hpp

@@ -78,9 +78,10 @@ private:
   map_type impl_{};
 
 public:
-  trie() {}
+  trie() = default;
   trie(trie const & other);
   trie(trie && other) : trie() { swap(*this, other); }
+  trie(mapped_type const & root, std::map<K, trie, Compare> && children);
   ~trie() { clear(); }
   trie & operator=(mapped_type const & value);
   trie & operator=(trie const & value);

+ 8 - 0
include/trie/trie.tpp

@@ -26,6 +26,14 @@ trie<K, V, C>::trie(trie const & other) : value_(other.value_) {
   }
 }
 
+template <typename K, typename V, typename C>
+trie<K, V, C>::trie(mapped_type const & root, std::map<K, trie, C> && children)
+    : value_(root) {
+  for (auto & pair : children) {
+    operator[](pair.first) = std::move(pair.second);
+  }
+}
+
 template <typename K, typename V, typename C>
 auto trie<K, V, C>::operator=(mapped_type const & value) -> trie & {
   value_ = value;

+ 116 - 66
test/trie_test.cpp

@@ -19,21 +19,17 @@ protected:
 };
 
 void TrieTest::SetUp() {
-  data = 1;
-  data[0] = 5;
-  data[{0, 1}] = 4;
-  data[{0, 1, 0}] = 3;
-  data[{0, 1, 2}] = 6;
-  data[{0, 2, 0}] = 7;
-  data[1] = 2;
-  data[{1, 1}] = 8;
-  data[{1, 1, 0}] = 9;
+  data = {
+      1,
+      {{0,
+        {5,
+         {{1, {4, {{0, {3, {}}}, {2, {6, {}}}}}}, {2, {0, {{0, {7, {}}}}}}}}},
+       {1, {2, {{1, {8, {{0, {9, {}}}}}}}}}}};
 }
 
 void TrieTest::TearDown() { data.clear(); }
 
-template <typename Iter> std::vector<int> flatten(Iter it) {
-  decltype(it) end{};
+template <typename Iter> std::vector<int> flatten(Iter it, Iter const & end) {
   std::vector<int> out;
   out.reserve(size_t(std::distance(it, end)));
   std::copy(it, end, std::back_inserter(out));
@@ -42,65 +38,12 @@ template <typename Iter> std::vector<int> flatten(Iter it) {
 
 TEST_F(TrieTest, DefaultConstructorHasNoElementsExceptRoot) {
   trie<int, int> empty;
-  EXPECT_THAT(flatten(empty.cbegin()), std::vector<int>{0});
+  EXPECT_THAT(flatten(empty.cbegin(), empty.cend()), std::vector<int>{0});
 }
 
 TEST_F(TrieTest, MoveConstructorIsDestructive) {
   trie<int, int> moved = std::move(data);
-  EXPECT_THAT(flatten(data.cbegin()), std::vector<int>{0});
-}
-
-TEST_F(TrieTest, MoveAssignmentOverwritesAnfRelocates) {
-  trie<int, int> value;
-  value[2] = 1;
-  value = std::move(data);
-  EXPECT_THAT(value.find(2), value.end());
-  EXPECT_THAT(value, ::testing::Ne(data));
-}
-
-/**
- * This is important because a trie is implemented as a
- * map<key_type, self_type*>. Therefore, a naive copy-ctor would result in the
- * copy having the same data after the first level (root value and inserts with
- * one key wouldn't backflow, but everything else would).
- */
-TEST_F(TrieTest, CopyConstructorIsDeep) {
-  trie<int, int> copy = data;
-  copy[{0, 1}] += 1;
-  EXPECT_THAT(copy, ::testing::Ne(data));
-}
-
-/**
- * Ensure that copy-assign doesn't simply add-all.
- */
-TEST_F(TrieTest, CopyAssignmentOverwritesData) {
-  trie<int, int> value;
-  value[2] = 1;
-  value = data;
-  EXPECT_THAT(value, data);
-}
-
-TEST_F(TrieTest, EqualityIsPathSensitive) {
-  trie<int, int> t1, t2;
-  t1[1] = 1;
-  t2[2] = 1;
-  EXPECT_THAT(flatten(t1.cbegin()), flatten(t2.cbegin()));
-  EXPECT_THAT(t1, ::testing::Ne(t2));
-}
-
-TEST_F(TrieTest, NormalIterationIsPreOrdered) {
-  EXPECT_THAT(flatten(data.cbegin()),
-              std::vector<int>({1, 5, 4, 3, 6, 0, 7, 2, 8, 9}));
-}
-
-TEST_F(TrieTest, PostIteratorUsesPostOrder) {
-  EXPECT_THAT(flatten(trie<int, int>::const_post_iterator(&data)),
-              std::vector<int>({3, 6, 4, 7, 0, 5, 9, 8, 2, 1}));
-}
-
-TEST_F(TrieTest, ReverseIterationIsBackwardsPreOrdered) {
-  EXPECT_THAT(flatten(data.crbegin()),
-              std::vector<int>({9, 8, 2, 7, 0, 6, 3, 4, 5, 1}));
+  EXPECT_THAT(flatten(data.cbegin(), data.cend()), std::vector<int>{0});
 }
 
 TEST_F(TrieTest, InsertingNewElementReturnsSuccess) {
@@ -118,6 +61,16 @@ TEST_F(TrieTest, EmplacingNewElementReturnsSuccess) {
 }
 
 TEST_F(TrieTest, InsertingWithPathWillCreateParents) {
+  trie<int, int> example;
+  std::pair<trie<int, int>::iterator, bool> rval = example.insert({1, 1, 1}, 2);
+  EXPECT_TRUE(rval.second);
+  EXPECT_THAT(*rval.first, 2);
+  EXPECT_THAT(example.find({1}), testing::Ne(example.end()));
+  EXPECT_THAT(example.find({1, 1}), testing::Ne(example.end()));
+  EXPECT_THAT(example.find({1, 1, 1}), rval.first);
+}
+
+TEST_F(TrieTest, EmplacingWithPathWillCreateParents) {
   trie<int, int> example;
   std::pair<trie<int, int>::iterator, bool> rval =
       example.emplace({1, 1, 1}, 2);
@@ -128,6 +81,24 @@ TEST_F(TrieTest, InsertingWithPathWillCreateParents) {
   EXPECT_THAT(example.find({1, 1, 1}), rval.first);
 }
 
+TEST_F(TrieTest, CanInsertWithAnyIterablePath) {
+  trie<int, int> example;
+  std::pair<trie<int, int>::iterator, bool> rval =
+      example.insert(std::vector<int>{1, 1, 1}, 2);
+  EXPECT_TRUE(rval.second);
+  EXPECT_THAT(*rval.first, 2);
+  EXPECT_THAT(example.find({1, 1, 1}), rval.first);
+}
+
+TEST_F(TrieTest, CanEmplaceWithAnyIterablePath) {
+  trie<int, int> example;
+  std::pair<trie<int, int>::iterator, bool> rval =
+      example.emplace(std::vector<int>{1, 1, 1}, 2);
+  EXPECT_TRUE(rval.second);
+  EXPECT_THAT(*rval.first, 2);
+  EXPECT_THAT(example.find({1, 1, 1}), rval.first);
+}
+
 TEST_F(TrieTest, InsertingInAlreadyExistantPathNonDestructive) {
   trie<int, int> example;
   example.insert(1, 2);
@@ -146,10 +117,75 @@ TEST_F(TrieTest, EmplacingInAlreadyExistantPathNonDestructive) {
 
 TEST_F(TrieTest, FindWithPathWillReturnIteratorToEntry) {
   EXPECT_THAT(*data.find({0, 1, 2}), 6);
+  trie<int, int> const & cref = data;
+  EXPECT_THAT(*cref.find({0, 1, 2}), 6);
 }
 
 TEST_F(TrieTest, FindMissingPathWillReturnEnd) {
   EXPECT_THAT(data.find({0, 3, 2}), data.end());
+  trie<int, int> const & cref = data;
+  EXPECT_THAT(cref.find({0, 3, 2}), cref.end());
+}
+
+TEST_F(TrieTest, SingleElementPathFindsSameAsKeyFind) {
+  EXPECT_THAT(data.find({0}), data.find(0));
+  EXPECT_THAT(data.find(std::vector<int>{0}), data.find(0));
+  trie<int, int> const & cref = data;
+  EXPECT_THAT(cref.find({0}), cref.find(0));
+  EXPECT_THAT(cref.find(std::vector<int>{0}), cref.find(0));
+}
+
+TEST_F(TrieTest, MoveAssignmentOverwritesAndRelocates) {
+  trie<int, int> value;
+  value[2] = 1;
+  value = std::move(data);
+  EXPECT_THAT(value.find(2), value.end());
+  EXPECT_THAT(value, ::testing::Ne(data));
+}
+
+/**
+ * This is important because a trie is implemented as a
+ * map<key_type, self_type*>. Therefore, a naive copy-ctor would result in the
+ * copy having the same data after the first level (root value and inserts with
+ * one key wouldn't backflow, but everything else would).
+ */
+TEST_F(TrieTest, CopyConstructorIsDeep) {
+  trie<int, int> copy = data;
+  copy[{0, 1}] += 1;
+  EXPECT_THAT(copy, ::testing::Ne(data));
+}
+
+/**
+ * Ensure that copy-assign doesn't simply add-all.
+ */
+TEST_F(TrieTest, CopyAssignmentOverwritesData) {
+  trie<int, int> value;
+  value[2] = 1;
+  value = data;
+  EXPECT_THAT(value, data);
+}
+
+TEST_F(TrieTest, EqualityIsPathSensitive) {
+  trie<int, int> t1, t2;
+  t1[1] = 1;
+  t2[2] = 1;
+  EXPECT_THAT(flatten(t1.begin(), t1.end()), flatten(t2.begin(), t2.end()));
+  EXPECT_THAT(t1, ::testing::Ne(t2));
+}
+
+TEST_F(TrieTest, NormalIterationIsPreOrdered) {
+  EXPECT_THAT(flatten(data.cbegin(), data.cend()),
+              std::vector<int>({1, 5, 4, 3, 6, 0, 7, 2, 8, 9}));
+}
+
+TEST_F(TrieTest, PostIteratorUsesPostOrder) {
+  EXPECT_THAT(flatten(trie<int, int>::const_post_iterator(&data), {}),
+              std::vector<int>({3, 6, 4, 7, 0, 5, 9, 8, 2, 1}));
+}
+
+TEST_F(TrieTest, ReverseIterationIsBackwardsPreOrdered) {
+  EXPECT_THAT(flatten(data.crbegin(), data.crend()),
+              std::vector<int>({9, 8, 2, 7, 0, 6, 3, 4, 5, 1}));
 }
 
 TEST_F(TrieTest, DereferenceWithMissingPathThrows) {
@@ -163,6 +199,20 @@ TEST_F(TrieTest, EraseDropsWholeBranch) {
   EXPECT_THAT(data.find(0), data.end());
 }
 
+TEST_F(TrieTest, CanEraseAtPath) {
+  EXPECT_THAT(data.find({0, 1}), ::testing::Ne(data.end()));
+  data.erase({0, 1});
+  EXPECT_THAT(data.find({0, 1}), data.end());
+  EXPECT_THAT(data.find(0), ::testing::Ne(data.end()));
+}
+
+TEST_F(TrieTest, CanEraseAtAnyIterablePath) {
+  EXPECT_THAT(data.find({0, 1}), ::testing::Ne(data.end()));
+  data.erase(std::vector<int>{0, 1});
+  EXPECT_THAT(data.find({0, 1}), data.end());
+  EXPECT_THAT(data.find(0), ::testing::Ne(data.end()));
+}
+
 TEST_F(TrieTest, EraseNonExistentElementIsNoOp) {
   trie<int, int> copy = data;
   copy.erase(-1);

+ 4 - 4
trie.xcodeproj/project.pbxproj

@@ -7,7 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
-		CDCB3C2024E381270029B771 /* trie_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD0C595420C2DBFB00454F82 /* trie_test.cpp */; };
+		CDCB3C2024E381270029B771 /* trie_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD0C595420C2DBFB00454F82 /* trie_test.cxx */; };
 		CDCB3C2224E381300029B771 /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD0C594D20C2DBE700454F82 /* GoogleMock.framework */; };
 /* End PBXBuildFile section */
 
@@ -51,7 +51,7 @@
 
 /* Begin PBXFileReference section */
 		CD0C594520C2DBE700454F82 /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
-		CD0C595420C2DBFB00454F82 /* trie_test.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = trie_test.cpp; sourceTree = "<group>"; };
+		CD0C595420C2DBFB00454F82 /* trie_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = trie_test.cxx; sourceTree = "<group>"; };
 		CDCB3C1224E380D80029B771 /* trie */ = {isa = PBXFileReference; lastKnownFileType = folder; name = trie; path = include/trie; sourceTree = "<group>"; };
 		CDCB3C1824E3811E0029B771 /* trie-test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "trie-test.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
 		CDCB3C1C24E3811E0029B771 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -103,7 +103,7 @@
 		CD46DF411EF3FD670092D121 /* test */ = {
 			isa = PBXGroup;
 			children = (
-				CD0C595420C2DBFB00454F82 /* trie_test.cpp */,
+				CD0C595420C2DBFB00454F82 /* trie_test.cxx */,
 			);
 			path = test;
 			sourceTree = "<group>";
@@ -229,7 +229,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				CDCB3C2024E381270029B771 /* trie_test.cpp in Sources */,
+				CDCB3C2024E381270029B771 /* trie_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};