From 4d03dcd54581cc4f3ce5955e561bd130fcbf34ff Mon Sep 17 00:00:00 2001 From: Marko Budiselic Date: Sun, 5 Jun 2016 14:30:40 +0200 Subject: [PATCH] Number of tests is now 6 --- CMakeLists.txt | 2 +- src/data_structures/map/hashmap.hpp | 6 +- src/utils/timer/timer.hpp | 10 +++- tests/CMakeLists.txt | 5 +- tests/dummy.cpp | 7 --- {src/test => tests}/lockfree_hashmap.cpp | 2 + tests/skiplist.cpp | 60 +++++++++++++++++++ .../unit/timer/main.cpp => tests/timer.cpp | 7 +-- 8 files changed, 82 insertions(+), 17 deletions(-) delete mode 100644 tests/dummy.cpp rename {src/test => tests}/lockfree_hashmap.cpp (89%) create mode 100644 tests/skiplist.cpp rename src/test/unit/timer/main.cpp => tests/timer.cpp (80%) diff --git a/CMakeLists.txt b/CMakeLists.txt index bc97ddaaf..e859d5111 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.1) # set directory name as the project name # get directory name diff --git a/src/data_structures/map/hashmap.hpp b/src/data_structures/map/hashmap.hpp index 4c0238d58..621825198 100644 --- a/src/data_structures/map/hashmap.hpp +++ b/src/data_structures/map/hashmap.hpp @@ -9,20 +9,20 @@ namespace lockfree { template -class HashMap: Lockable +class HashMap : Lockable { public: V at(const K& key) { - auto guard = acquire(); + auto guard = acquire_unique(); return hashmap[key]; } void put(const K& key, const K& value) { - auto quard = acquire(); + auto guard = acquire_unique(); hashmap[key] = value; } diff --git a/src/utils/timer/timer.hpp b/src/utils/timer/timer.hpp index 1e56cf8f2..1cf53ea2b 100644 --- a/src/utils/timer/timer.hpp +++ b/src/utils/timer/timer.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "utils/log/logger.hpp" @@ -110,8 +111,9 @@ public: void run() { + is_running.store(true); run_thread = std::thread([this]() { - while (true) { + while (is_running.load()) { std::this_thread::sleep_for(delta_time_type(delta_time)); timer_container.process(); LOG_INFO("timer_container.process()"); @@ -119,6 +121,11 @@ public: }); } + void stop() + { + is_running.store(false); + } + ~TimerScheduler() { run_thread.join(); @@ -128,4 +135,5 @@ public: private: timer_container_type timer_container; std::thread run_thread; + std::atomic is_running; }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a2c42272d..5aa4ebb75 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,8 +1,9 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.1) project(memgraph_tests) # setup dependencies +find_package(Threads REQUIRED) # Catch (C++ Automated Test Cases in Headers) dependency ExternalProject_Add( @@ -35,8 +36,10 @@ file(COPY ${CMAKE_SOURCE_DIR}/tests/data DESTINATION ${CMAKE_BINARY_DIR}/tests) # build tests foreach(test ${tests}) add_executable(${test} ${test}.cpp) + # TODO: separate dependencies target_link_libraries(${test} stdc++fs) target_link_libraries(${test} cypher_lib) + target_link_libraries(${test} Threads::Threads) add_test(NAME ${test} COMMAND ${test}) set_property(TARGET ${test} PROPERTY CXX_STANDARD 14) endforeach() diff --git a/tests/dummy.cpp b/tests/dummy.cpp deleted file mode 100644 index 6e384f3d4..000000000 --- a/tests/dummy.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#define CATCH_CONFIG_MAIN -#include "catch.hpp" - -TEST_CASE("Dummy test") -{ - REQUIRE(true == true); -} diff --git a/src/test/lockfree_hashmap.cpp b/tests/lockfree_hashmap.cpp similarity index 89% rename from src/test/lockfree_hashmap.cpp rename to tests/lockfree_hashmap.cpp index 10af83438..2053a929e 100644 --- a/src/test/lockfree_hashmap.cpp +++ b/tests/lockfree_hashmap.cpp @@ -1,4 +1,6 @@ +#define CATCH_CONFIG_MAIN #include "catch.hpp" + #include "data_structures/map/hashmap.hpp" TEST_CASE("Lockfree HashMap basic functionality") diff --git a/tests/skiplist.cpp b/tests/skiplist.cpp new file mode 100644 index 000000000..8810b2d4e --- /dev/null +++ b/tests/skiplist.cpp @@ -0,0 +1,60 @@ +#include + +#include "data_structures/skiplist/skiplist.hpp" + +using std::cout; +using std::endl; + +using skiplist_t = SkipList; + +void print_skiplist(const skiplist_t::Accessor& skiplist) +{ + cout << "---- skiplist now has: "; + + for(auto& kv : skiplist) + cout << "(" << kv.first << ", " << kv.second << ") "; + + cout << "----" << endl; +} + +int main(void) +{ + skiplist_t skiplist; + auto accessor = skiplist.access(); + + // insert 10 + assert(accessor.insert_unique(1, 10).second == true); + + // try insert 10 again (should fail) + assert(accessor.insert_unique(1, 10).second == false); + + // insert 20 + assert(accessor.insert_unique(2, 20).second == true); + + print_skiplist(accessor); + + // value at key 3 shouldn't exist + assert((accessor.find(3) == accessor.end()) == true); + + // value at key 2 should exist + assert((accessor.find(2) != accessor.end()) == true); + + // at key 2 is 20 (true) + assert(accessor.find(2)->second == 20); + + // removed existing (1) + assert(accessor.remove(1) == true); + + // removed non-existing (3) + assert(accessor.remove(3) == false); + + // insert (1, 10) + assert(accessor.insert_unique(1, 10).second == true); + + // insert (4, 40) + assert(accessor.insert_unique(4, 40).second == true); + + print_skiplist(accessor); + + return 0; +} diff --git a/src/test/unit/timer/main.cpp b/tests/timer.cpp similarity index 80% rename from src/test/unit/timer/main.cpp rename to tests/timer.cpp index 244c9b91e..4d97693f3 100644 --- a/src/test/unit/timer/main.cpp +++ b/tests/timer.cpp @@ -1,9 +1,6 @@ #include #include -// #define NOT_LOG_INFO -// compile this with: c++ -std=c++1y -o a.out -I../../../ -pthread main.cpp - #include "utils/log/logger.hpp" #include "utils/timer/timer.hpp" @@ -23,7 +20,9 @@ int main(void) for (int64_t i = 1; i <= 3; ++i) { timer_scheduler.add(create_test_timer(i)); } - std::this_thread::sleep_for(10s); + std::this_thread::sleep_for(4s); timer_scheduler.add(create_test_timer(1)); + std::this_thread::sleep_for(2s); + timer_scheduler.stop(); return 0; }