Introduce add function

This commit is contained in:
Thomas Lauf 2018-07-31 06:48:21 +02:00 committed by lauft
parent d974742654
commit 7d93e23a20
3 changed files with 25 additions and 17 deletions

View file

@ -27,6 +27,7 @@
#include <TagInfoDatabase.h> #include <TagInfoDatabase.h>
#include <format.h> #include <format.h>
#include <TagInfo.h> #include <TagInfo.h>
#include "timew.h"
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Increment tag count // Increment tag count
@ -34,13 +35,13 @@
// //
// Returns the previous tag count, -1 if it did not exist // 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); auto search = _tagInformation.find (tag);
if (search == _tagInformation.end ()) if (search == _tagInformation.end ())
{ {
_tagInformation.emplace (tag, TagInfo (1)); add (tag, TagInfo {1});
return -1; return -1;
} }
@ -53,14 +54,22 @@ int TagInfoDatabase::incrementTag (const std::string &tag)
// //
// Returns the new tag count // Returns the new tag count
// //
int TagInfoDatabase::decrementTag (const std::string &tag) int TagInfoDatabase::decrementTag (const std::string& tag)
{ {
auto search = _tagInformation.find (tag); auto search = _tagInformation.find (tag);
if (search == _tagInformation.end ()) 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 (); return search->second.decrement ();
} }
///////////////////////////////////////////////////////////////////////////////
// Add tag to database
//
void TagInfoDatabase::add (const std::string& tag, const TagInfo& tagInfo)
{
_tagInformation.emplace (tag, tagInfo);
}

View file

@ -28,8 +28,8 @@
#define INCLUDED_TAGINFODATABASE #define INCLUDED_TAGINFODATABASE
#include <string> #include <string>
#include <TagInfo.h>
#include <map> #include <map>
#include <TagInfo.h>
class TagInfoDatabase class TagInfoDatabase
{ {
@ -37,6 +37,8 @@ public:
int incrementTag (const std::string&); int incrementTag (const std::string&);
int decrementTag (const std::string&); int decrementTag (const std::string&);
void add (const std::string&, const TagInfo&);
private: private:
std::map <std::string, TagInfo> _tagInformation {}; std::map <std::string, TagInfo> _tagInformation {};
}; };

View file

@ -31,28 +31,25 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int, char**) int main (int, char**)
{ {
UnitTest t (9); UnitTest t (4);
TagInfoDatabase tagInfoDatabase {}; TagInfoDatabase tagInfoDatabase {};
t.is (tagInfoDatabase.incrementTag ("foo"), -1, "First insertion of 'foo' returns -1"); tagInfoDatabase.add("foo", TagInfo {1});
t.is (tagInfoDatabase.incrementTag ("foo"), 1, "New insertion of 'foo' increments count"); tagInfoDatabase.add("bar", TagInfo {2});
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.incrementTag ("baz"), -1, "Insertion of new tag returns -1");
t.is (tagInfoDatabase.decrementTag ("foo"), 1, "Removal of 'foo' decrements count"); t.is (tagInfoDatabase.incrementTag ("foo"), 1, "Increment of existing tag returns previous count");
t.is (tagInfoDatabase.decrementTag ("bar"), 1, "Removal of 'bar' decrements count"); t.is (tagInfoDatabase.decrementTag ("bar"), 1, "Decrement of existing tag returns new count");
t.is (tagInfoDatabase.decrementTag ("foo"), 0, "Deletion of 'foo' returns 0");
t.is (tagInfoDatabase.decrementTag ("bar"), 0, "Deletion of 'bar' returns 0");
try try
{ {
tagInfoDatabase.decrementTag ("xyz"); tagInfoDatabase.decrementTag ("xyz");
t.fail ("Deletion of non-existent tag throws an exception"); t.fail ("Decrement of non-existent tag throws an exception");
} }
catch (...) catch (...)
{ {
t.pass ("Deletion of non-existent tag throws an exception"); t.pass ("Decrement of non-existent tag throws an exception");
} }
return 0; return 0;