From 2a046bba786dc741433f5b12bd8e2044b31a3a20 Mon Sep 17 00:00:00 2001 From: Marko Budiselic Date: Sun, 19 Feb 2017 23:47:09 +0100 Subject: [PATCH] all unit tests are gtests --- CMakeLists.txt | 18 ++++++++---- tests/unit/id.cpp | 37 ++++++++++-------------- tests/unit/log.cpp | 25 ---------------- tests/unit/logging.cpp | 62 ++++++++++++++++++++++++++++++++++++++++ tests/unit/timestamp.cpp | 12 ++++++-- 5 files changed, 100 insertions(+), 54 deletions(-) delete mode 100644 tests/unit/log.cpp create mode 100644 tests/unit/logging.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 93749a803..2e4715d63 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -409,8 +409,16 @@ endif() # ----------------------------------------------------------------------------- # make CLion aware of all source files so we get refactoring etc -# TODO: fix snapshots and everything from src and then add custom target -#file(GLOB_RECURSE __SOURCES ${CMAKE_SOURCE_DIR}/src/*.hpp ${CMAKE_SOURCE_DIR}/src/*.cpp) -#add_executable(__refactor_target ${__SOURCES}) -#target_link_libraries(__refactor_target memgraph_lib stdc++fs Threads::Threads ${fmt_static_lib} ${yaml_static_lib} ${antlr_static_lib} antlr_opencypher_parser_lib dl) -#add_dependencies(__refactor_target generate_opencypher_parser) +file(GLOB_RECURSE __SOURCES ${CMAKE_SOURCE_DIR}/src/*.hpp ${CMAKE_SOURCE_DIR}/src/*.cpp) +# TODO: those files have to be refactored (snapshot & cleaner related) +list(REMOVE_ITEM __SOURCES ${CMAKE_SOURCE_DIR}/src/snapshot/snapshot_engine.cpp) +list(REMOVE_ITEM __SOURCES ${CMAKE_SOURCE_DIR}/src/snapshot/snapshot_decoder.cpp) +list(REMOVE_ITEM __SOURCES ${CMAKE_SOURCE_DIR}/src/snapshot/snapshot_encoder.cpp) +list(REMOVE_ITEM __SOURCES ${CMAKE_SOURCE_DIR}/src/snapshot/snapshoter.cpp) +list(REMOVE_ITEM __SOURCES ${CMAKE_SOURCE_DIR}/src/dbms/cleaner.cpp) +list(REMOVE_ITEM __SOURCES ${CMAKE_SOURCE_DIR}/src/storage/garbage/garbage.cpp) +# TODO: figure out why the file isn't compilable it should be (typed_value_store.cpp) +list(REMOVE_ITEM __SOURCES ${CMAKE_SOURCE_DIR}/src/storage/typed_value_store.cpp) +list(REMOVE_ITEM __SOURCES ${CMAKE_SOURCE_DIR}/src/io/network/tls.cpp) +add_executable(__refactor_target ${__SOURCES}) +target_link_libraries(__refactor_target memgraph_lib stdc++fs Threads::Threads ${fmt_static_lib} ${yaml_static_lib} ${antlr_static_lib} antlr_opencypher_parser_lib dl) diff --git a/tests/unit/id.cpp b/tests/unit/id.cpp index f00331d5f..292c1bb33 100644 --- a/tests/unit/id.cpp +++ b/tests/unit/id.cpp @@ -1,11 +1,8 @@ -#include +#include "gtest/gtest.h" #include "mvcc/id.hpp" -using std::cout; -using std::endl; - -int main() { +TEST(IdTest, BasicUsageAndTotalOrdering) { Id id0(0); Id id1(1); Id id2(1); @@ -13,21 +10,17 @@ int main() { Id id4 = id3; Id id5(5); - cout << id5 << " " << id0 << endl; - - if (id0 < id5) cout << "id0 < id5" << endl; - - if (id1 == id2) cout << "are equal" << endl; - - if (id3 == id4) cout << "id3 == id4" << endl; - - if (id5 > id0) cout << "id5 > id0" << endl; - - if (id5 != id3) cout << "id5 != id3" << endl; - - if (id1 >= id2) cout << "id1 >= id2" << endl; - - if (id3 <= id4) cout << "id3 <= id4" << endl; - - return 0; + ASSERT_EQ(id0 < id5, true); + ASSERT_EQ(id1 == id2, true); + ASSERT_EQ(id3 == id4, true); + ASSERT_EQ(id5 > id0, true); + ASSERT_EQ(id5 > id0, true); + ASSERT_EQ(id5 != id3, true); + ASSERT_EQ(id1 >= id2, true); + ASSERT_EQ(id3 <= id4, true); +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); } diff --git a/tests/unit/log.cpp b/tests/unit/log.cpp deleted file mode 100644 index 3065fc5bf..000000000 --- a/tests/unit/log.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "logging/logger.hpp" -#include "logging/logs/async_log.hpp" -#include "logging/logs/sync_log.hpp" - -#include "logging/streams/stdout.hpp" - -int main(void) { - // Log::uptr log = std::make_unique(); - Log::uptr log = std::make_unique(); - - log->pipe(std::make_unique()); - - auto logger = log->logger("main"); - - logger.info("This is very {}!", "awesome"); - logger.warn("This is very {}!", "awesome"); - logger.error("This is very {}!", "awesome"); - logger.trace("This is very {}!", "awesome"); - logger.debug("This is very {}!", "awesome"); - - using namespace std::chrono; - /* std::this_thread::sleep_for(1s); */ - - return 0; -} diff --git a/tests/unit/logging.cpp b/tests/unit/logging.cpp new file mode 100644 index 000000000..c9328c134 --- /dev/null +++ b/tests/unit/logging.cpp @@ -0,0 +1,62 @@ +#include +#include "gtest/gtest.h" +#include "logging/logger.hpp" +#include "logging/logs/async_log.hpp" +#include "logging/logs/sync_log.hpp" +#include "logging/streams/stdout.hpp" + +class Testout : public Log::Stream { + public: + void emit(const Log::Record& record) override { + counter_.fetch_add(1); + sequence_.emplace_back(counter_.load()); + texts_.insert(record.text()); + } + std::atomic counter_; + std::vector sequence_; + std::set texts_; +}; + +TEST(LoggingTest, SyncLogger) { + Log::uptr log = std::make_unique(); + auto test_stream = std::make_unique(); + auto check = test_stream.get(); + log->pipe(std::move(test_stream)); + auto logger = log->logger("sync_logger"); + + logger.info("{}", "info"); + logger.warn("{}", "warn"); + logger.error("{}", "error"); + logger.trace("{}", "trace"); + logger.debug("{}", "debug"); + + ASSERT_EQ(check->counter_, 5); + ASSERT_EQ(check->sequence_[4], 5); + ASSERT_EQ(check->texts_.size(), 5); +} + +TEST(LoggingTest, AysncLogger) { + Log::uptr log = std::make_unique(); + auto test_stream = std::make_unique(); + auto check = test_stream.get(); + log->pipe(std::move(test_stream)); + auto logger = log->logger("async_logger"); + + logger.info("{}", "info"); + logger.warn("{}", "warn"); + logger.error("{}", "error"); + logger.trace("{}", "trace"); + logger.debug("{}", "debug"); + + using namespace std::chrono; + std::this_thread::sleep_for(1s); + + ASSERT_EQ(check->counter_, 5); + ASSERT_EQ(check->sequence_.size(), 5); + ASSERT_EQ(check->texts_.size(), 5); +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tests/unit/timestamp.cpp b/tests/unit/timestamp.cpp index 587c6367d..8c7b1ac1c 100644 --- a/tests/unit/timestamp.cpp +++ b/tests/unit/timestamp.cpp @@ -2,9 +2,10 @@ #include #include +#include "gtest/gtest.h" #include "utils/datetime/timestamp.hpp" -int main(void) { +TEST(TimestampTest, BasicUsage) { auto timestamp = Timestamp::now(); std::cout << timestamp << std::endl; @@ -14,9 +15,16 @@ int main(void) { std::cout << Timestamp::now().to_iso8601() << std::endl; + ASSERT_GT(Timestamp::now(), timestamp); + std::cout << std::boolalpha; std::cout << (timestamp == Timestamp::now()) << std::endl; - return 0; + ASSERT_NE(timestamp, Timestamp::now()); +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); }