Permalink
Browse files

Introduce add function

  • Loading branch information...
lauft committed Jul 31, 2018
1 parent 8aafcbd commit 3abff67264ccd26dec0df73fedf6af1d41348026
Showing with 25 additions and 17 deletions.
  1. +13 −4 src/TagInfoDatabase.cpp
  2. +3 −1 src/TagInfoDatabase.h
  3. +9 −12 test/TagInfoDatabase.t.cpp
@@ -27,20 +27,21 @@
#include <TagInfoDatabase.h>
#include <format.h>
#include <TagInfo.h>
#include "timew.h"

///////////////////////////////////////////////////////////////////////////////
// Increment tag count
// If it does not exist, a new entry is created
//
// 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);
}
@@ -28,15 +28,17 @@
#define INCLUDED_TAGINFODATABASE

#include <string>
#include <TagInfo.h>
#include <map>
#include <TagInfo.h>

class TagInfoDatabase
{
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 {};
};
@@ -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;

0 comments on commit 3abff67

Please sign in to comment.