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 <format.h>
#include <TagInfo.h>
#include "timew.h"
///////////////////////////////////////////////////////////////////////////////
// Increment tag count
@ -40,7 +41,7 @@ int TagInfoDatabase::incrementTag (const std::string &tag)
if (search == _tagInformation.end ())
{
_tagInformation.emplace (tag, TagInfo (1));
add (tag, TagInfo {1});
return -1;
}
@ -59,8 +60,16 @@ int TagInfoDatabase::decrementTag (const std::string &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);
}

View file

@ -28,8 +28,8 @@
#define INCLUDED_TAGINFODATABASE
#include <string>
#include <TagInfo.h>
#include <map>
#include <TagInfo.h>
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 <std::string, TagInfo> _tagInformation {};
};

View file

@ -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;