diff --git a/src/TagInfoDatabase.cpp b/src/TagInfoDatabase.cpp index 49fe9eba..66cf946f 100644 --- a/src/TagInfoDatabase.cpp +++ b/src/TagInfoDatabase.cpp @@ -27,6 +27,7 @@ #include #include #include +#include "timew.h" /////////////////////////////////////////////////////////////////////////////// // Increment tag count @@ -34,13 +35,13 @@ // // Returns the previous tag count, -1 if it did not exist // -int TagInfoDatabase::incrementTag (const std::string &tag) +int TagInfoDatabase::incrementTag (const std::string& tag) { auto search = _tagInformation.find (tag); if (search == _tagInformation.end ()) { - _tagInformation.emplace (tag, TagInfo (1)); + add (tag, TagInfo {1}); return -1; } @@ -53,14 +54,22 @@ int TagInfoDatabase::incrementTag (const std::string &tag) // // Returns the new tag count // -int TagInfoDatabase::decrementTag (const std::string &tag) +int TagInfoDatabase::decrementTag (const std::string& tag) { auto search = _tagInformation.find (tag); if (search == _tagInformation.end ()) { - throw format ("Trying to remove non-existent tag '{}'", tag); + throw format ("Trying to decrement non-existent tag '{1}'", tag); } return search->second.decrement (); } + +/////////////////////////////////////////////////////////////////////////////// +// Add tag to database +// +void TagInfoDatabase::add (const std::string& tag, const TagInfo& tagInfo) +{ + _tagInformation.emplace (tag, tagInfo); +} diff --git a/src/TagInfoDatabase.h b/src/TagInfoDatabase.h index 9e56bd72..a12a46df 100644 --- a/src/TagInfoDatabase.h +++ b/src/TagInfoDatabase.h @@ -28,8 +28,8 @@ #define INCLUDED_TAGINFODATABASE #include -#include #include +#include class TagInfoDatabase { @@ -37,6 +37,8 @@ public: int incrementTag (const std::string&); int decrementTag (const std::string&); + void add (const std::string&, const TagInfo&); + private: std::map _tagInformation {}; }; diff --git a/test/TagInfoDatabase.t.cpp b/test/TagInfoDatabase.t.cpp index 82899c04..d967d50b 100644 --- a/test/TagInfoDatabase.t.cpp +++ b/test/TagInfoDatabase.t.cpp @@ -31,28 +31,25 @@ //////////////////////////////////////////////////////////////////////////////// int main (int, char**) { - UnitTest t (9); - + UnitTest t (4); TagInfoDatabase tagInfoDatabase {}; - t.is (tagInfoDatabase.incrementTag ("foo"), -1, "First insertion of 'foo' returns -1"); - t.is (tagInfoDatabase.incrementTag ("foo"), 1, "New insertion of 'foo' increments count"); - t.is (tagInfoDatabase.incrementTag ("bar"), -1, "First insertion of 'bar' returns -1"); - t.is (tagInfoDatabase.incrementTag ("bar"), 1, "New insertion of 'bar' increments count"); - t.is (tagInfoDatabase.decrementTag ("foo"), 1, "Removal of 'foo' decrements count"); - t.is (tagInfoDatabase.decrementTag ("bar"), 1, "Removal of 'bar' decrements count"); - t.is (tagInfoDatabase.decrementTag ("foo"), 0, "Deletion of 'foo' returns 0"); - t.is (tagInfoDatabase.decrementTag ("bar"), 0, "Deletion of 'bar' returns 0"); + tagInfoDatabase.add("foo", TagInfo {1}); + tagInfoDatabase.add("bar", TagInfo {2}); + + t.is (tagInfoDatabase.incrementTag ("baz"), -1, "Insertion of new tag returns -1"); + t.is (tagInfoDatabase.incrementTag ("foo"), 1, "Increment of existing tag returns previous count"); + t.is (tagInfoDatabase.decrementTag ("bar"), 1, "Decrement of existing tag returns new count"); try { tagInfoDatabase.decrementTag ("xyz"); - t.fail ("Deletion of non-existent tag throws an exception"); + t.fail ("Decrement of non-existent tag throws an exception"); } catch (...) { - t.pass ("Deletion of non-existent tag throws an exception"); + t.pass ("Decrement of non-existent tag throws an exception"); } return 0;