Permalink
Browse files

Add a tag database

  • Loading branch information...
lauft committed Jul 29, 2018
1 parent 9489ed5 commit 064dc2485aeb39853db01e8740de71a15c4d71b0
Showing with 259 additions and 1 deletion.
  1. +2 −0 src/CMakeLists.txt
  2. +42 −0 src/TagInfo.cpp
  3. +42 −0 src/TagInfo.h
  4. +66 −0 src/TagInfoDatabase.cpp
  5. +44 −0 src/TagInfoDatabase.h
  6. +1 −1 test/CMakeLists.txt
  7. +62 −0 test/TagInfoDatabase.t.cpp
@@ -13,6 +13,8 @@ set (timew_SRCS CLI.cpp CLI.h
Interval.cpp Interval.h
Range.cpp Range.h
Rules.cpp Rules.h
TagInfo.cpp TagInfo.h
TagInfoDatabase.cpp TagInfoDatabase.h
Transaction.cpp Transaction.h
TransactionsFactory.cpp TransactionsFactory.h
UndoAction.cpp UndoAction.h
@@ -0,0 +1,42 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////

#include "TagInfo.h"

TagInfo::TagInfo (int count)
{
_count = count;
}

int TagInfo::increment ()
{
return _count++;
}

int TagInfo::decrement ()
{
return --_count;
}
@@ -0,0 +1,42 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////

#ifndef INCLUDED_TAGINFO
#define INCLUDED_TAGINFO

class TagInfo
{
public:
explicit TagInfo (int);

int increment ();
int decrement ();

private:
int _count = 0;
};

#endif
@@ -0,0 +1,66 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////

#include <TagInfoDatabase.h>
#include <format.h>
#include <TagInfo.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)
{
auto search = _tagInformation.find (tag);

if (search == _tagInformation.end ())
{
_tagInformation.emplace (tag, TagInfo (1));

return -1;
}

return search->second.increment ();
}

///////////////////////////////////////////////////////////////////////////////
// Decrement tag count
//
// Returns the new tag count
//
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);
}

return search->second.decrement ();
}
@@ -0,0 +1,44 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////

#ifndef INCLUDED_TAGINFODATABASE
#define INCLUDED_TAGINFODATABASE

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

class TagInfoDatabase
{
public:
int incrementTag (const std::string&);
int decrementTag (const std::string&);

private:
std::map <std::string, TagInfo> _tagInformation {};
};

#endif
@@ -14,7 +14,7 @@ include_directories (${CMAKE_SOURCE_DIR}
include_directories (${CMAKE_INSTALL_PREFIX}/include)
link_directories(${CMAKE_INSTALL_PREFIX}/lib)

set (test_SRCS data.t exclusion.t helper.t interval.t range.t rules.t util.t)
set (test_SRCS data.t exclusion.t helper.t interval.t range.t rules.t util.t TagInfoDatabase.t)

add_custom_target (test ./run_all --verbose
DEPENDS ${test_SRCS}
@@ -0,0 +1,62 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////

#include <cmake.h>
#include <test.h>
#include <TagInfoDatabase.h>

////////////////////////////////////////////////////////////////////////////////
int main (int, char**)
{
UnitTest t (9);


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");

try
{
tagInfoDatabase.decrementTag ("xyz");
t.fail ("Deletion of non-existent tag throws an exception");
}
catch (...)
{
t.pass ("Deletion of non-existent tag throws an exception");
}

return 0;
}



0 comments on commit 064dc24

Please sign in to comment.