From 9d6b5e39f775b7618c1b4e8acdc8e3dd1154cffc Mon Sep 17 00:00:00 2001 From: Michael Valladolid Date: Wed, 6 Sep 2023 19:43:10 +0900 Subject: [PATCH] PerpetualCache with tests --- .gitignore | 3 ++ CMakeLists.txt | 32 +++++++++++++++++++ ExpireableCache.hpp | 37 ---------------------- GenericCache.hpp | 10 +++--- PerpetualCache.hpp | 52 ++++++++++++++++++++++++++++++ PerpetualCacheTest.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++ main.cpp | 12 ------- 7 files changed, 164 insertions(+), 54 deletions(-) create mode 100644 CMakeLists.txt delete mode 100644 ExpireableCache.hpp create mode 100644 PerpetualCache.hpp create mode 100644 PerpetualCacheTest.cpp delete mode 100644 main.cpp diff --git a/.gitignore b/.gitignore index 875eed3..db4479c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .vscode +.idea + build/ +cmake-build-debug/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f82684a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,32 @@ +cmake_minimum_required(VERSION 3.14) +project(my_project) + +# GoogleTest requires at least C++14 +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip +) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +enable_testing() + +enable_testing() + +add_executable( + PerpetualCacheTest + PerpetualCacheTest.cpp +) +target_link_libraries( + PerpetualCacheTest + GTest::gtest_main +) + +include(GoogleTest) +gtest_discover_tests(PerpetualCacheTest) \ No newline at end of file diff --git a/ExpireableCache.hpp b/ExpireableCache.hpp deleted file mode 100644 index e32911d..0000000 --- a/ExpireableCache.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "GenericCache.hpp" - -using namespace std; - -template -class ExpirableCache: public GenericCache -{ - public: - void set(K key, V value); - V get(K key); - void remove(K key); - void clear(); -}; - -template -void ExpirableCache::set(K key, V value) -{ - cout << "set" << endl; -} - -template -V ExpirableCache::get(K key) -{ - -} - -template -void ExpirableCache::remove(K key) -{ - -} - -template -void ExpirableCache::clear() -{ - -} diff --git a/GenericCache.hpp b/GenericCache.hpp index d3aa50a..2a9e1ce 100644 --- a/GenericCache.hpp +++ b/GenericCache.hpp @@ -1,9 +1,9 @@ template class GenericCache { - public: - virtual void set(K key, V value) = 0; - virtual V get(K key) = 0; - virtual void remove(K key) = 0; - virtual void clear() = 0; +public: + virtual void set(const K& key, const V& value) = 0; + virtual V& get(const K& key) = 0; + virtual void remove(const K& key) = 0; + virtual void clear() = 0; }; diff --git a/PerpetualCache.hpp b/PerpetualCache.hpp new file mode 100644 index 0000000..1980303 --- /dev/null +++ b/PerpetualCache.hpp @@ -0,0 +1,52 @@ +#include +#include "GenericCache.hpp" + +using namespace std; + +template +class PerpetualCache : public GenericCache +{ +public: + void set(const K& key, const V& value); + V& get(const K& key); + void remove(const K& key); + void clear(); + +private: + bool exists(const K& key); + unordered_map cache; +}; + +template +void PerpetualCache::set(const K& key, const V& value) +{ + this->cache[key] = value; +} + +template +V& PerpetualCache::get(const K& key) +{ + if (!this->exists(key)) + { + throw std::invalid_argument("Unknown key!"); + } + return this->cache[key]; +} + +template +void PerpetualCache::remove(const K& key) +{ + this->cache.erase(key); +} + +template +void PerpetualCache::clear() +{ + this->cache.clear(); +} + +template +bool PerpetualCache::exists(const K& key) +{ + return this->cache.find(key) != this->cache.end(); +} diff --git a/PerpetualCacheTest.cpp b/PerpetualCacheTest.cpp new file mode 100644 index 0000000..2cce549 --- /dev/null +++ b/PerpetualCacheTest.cpp @@ -0,0 +1,72 @@ +#include +#include "PerpetualCache.hpp" + +using namespace std; + +// Demonstrate some basic assertions. +TEST(PerpetualCache, test_set_get_present) +{ + PerpetualCache cache; + + cache.set("key", 2); + EXPECT_EQ(2, cache.get("key")); +} + +TEST(PerpetualCache, test_set_get_absent) +{ + PerpetualCache cache; + + try + { + cache.remove("key"); + } + catch (invalid_argument const & err) + { + EXPECT_EQ(err.what(), string("Unknown key!")); + } +} + +TEST(PerpetualCache, test_remove_present) +{ + PerpetualCache cache; + + cache.set("key", 2); + cache.remove("key"); + + try + { + cache.get("key"); + } + catch (invalid_argument const & err) + { + EXPECT_EQ(err.what(), string("Unknown key!")); + } +} + +TEST(PerpetualCache, test_remove_absent) +{ + PerpetualCache cache; + + cache.set("key", 2); + cache.remove("absent"); + + EXPECT_EQ(2, cache.get("key")); +} + +TEST(PerpetualCache, test_clear) +{ + PerpetualCache cache; + + cache.set("key", 2); + EXPECT_EQ(2, cache.get("key")); + cache.clear(); + + try + { + cache.get("key"); + } + catch (invalid_argument const & err) + { + EXPECT_EQ(err.what(), string("Unknown key!")); + } +} \ No newline at end of file diff --git a/main.cpp b/main.cpp deleted file mode 100644 index e47562c..0000000 --- a/main.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include "ExpireableCache.hpp" - -using namespace std; - -int main() -{ - cout << "hello world" << endl; - - ExpirableCache cache; - cache.set("key", 0); -}