diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 926935df..4a544127 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/TagInfo.cpp b/src/TagInfo.cpp new file mode 100644 index 00000000..0c28e4e8 --- /dev/null +++ b/src/TagInfo.cpp @@ -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; +} diff --git a/src/TagInfo.h b/src/TagInfo.h new file mode 100644 index 00000000..86752862 --- /dev/null +++ b/src/TagInfo.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 +// +//////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDED_TAGINFO +#define INCLUDED_TAGINFO + +class TagInfo +{ +public: + explicit TagInfo (int); + + int increment (); + int decrement (); + +private: + int _count = 0; +}; + +#endif diff --git a/src/TagInfoDatabase.cpp b/src/TagInfoDatabase.cpp new file mode 100644 index 00000000..49fe9eba --- /dev/null +++ b/src/TagInfoDatabase.cpp @@ -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 +#include +#include + +/////////////////////////////////////////////////////////////////////////////// +// 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 (); +} diff --git a/src/TagInfoDatabase.h b/src/TagInfoDatabase.h new file mode 100644 index 00000000..9e56bd72 --- /dev/null +++ b/src/TagInfoDatabase.h @@ -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 +#include +#include + +class TagInfoDatabase +{ +public: + int incrementTag (const std::string&); + int decrementTag (const std::string&); + +private: + std::map _tagInformation {}; +}; + +#endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 19f58d46..0bcf23d5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -16,7 +16,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} diff --git a/test/TagInfoDatabase.t.cpp b/test/TagInfoDatabase.t.cpp new file mode 100644 index 00000000..82899c04 --- /dev/null +++ b/test/TagInfoDatabase.t.cpp @@ -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 +#include +#include + +//////////////////////////////////////////////////////////////////////////////// +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; +} + + +