From 40d254a53bd56a5104205cae2316a5f024f12c55 Mon Sep 17 00:00:00 2001 From: anton Date: Mon, 18 Mar 2024 17:40:02 +0300 Subject: [PATCH 1/6] TimedDoor.h edited --- README.md | 4 ++-- include/TimedDoor.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f19f864..19caea0 100644 --- a/README.md +++ b/README.md @@ -44,13 +44,13 @@ class TimedDoor : public Door { private: DoorTimerAdapter * adapter; int iTimeout; - bool opened; + bool isOpened; public: explicit TimedDoor(int); bool isDoorOpened(); void unlock(); void lock(); - void DoorTimeOut(); + int getTimeOut(); void throwState(); }; diff --git a/include/TimedDoor.h b/include/TimedDoor.h index e1eade2..04429ad 100644 --- a/include/TimedDoor.h +++ b/include/TimedDoor.h @@ -32,13 +32,13 @@ class TimedDoor : public Door { private: DoorTimerAdapter * adapter; int iTimeout; - bool opened; + bool isOpened; public: explicit TimedDoor(int); bool isDoorOpened(); void unlock(); void lock(); - void DoorTimeOut(); + int getTimeOut() const; void throwState(); }; From 937a53d801da1dcddc15febe2dbe1a4a2cd0ca3d Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 20 May 2024 07:24:16 +0300 Subject: [PATCH 2/6] initial --- include/TimedDoor.h | 20 ++++++------ src/TimedDoor.cpp | 44 +++++++++++++++++++++++++++ src/main.cpp | 6 ++-- test/tests.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 130 insertions(+), 14 deletions(-) create mode 100644 src/TimedDoor.cpp diff --git a/include/TimedDoor.h b/include/TimedDoor.h index 04429ad..b79a698 100644 --- a/include/TimedDoor.h +++ b/include/TimedDoor.h @@ -1,7 +1,6 @@ -// Copyright 2021 GHA Test Team +// Copyright 2024 Derun Andrey -#ifndef INCLUDE_TIMEDDOOR_H_ -#define INCLUDE_TIMEDDOOR_H_ +#pragma once class DoorTimerAdapter; class Timer; @@ -22,31 +21,32 @@ class Door { class DoorTimerAdapter : public TimerClient { private: - TimedDoor& door; + TimedDoor &door; + public: - explicit DoorTimerAdapter(TimedDoor&); + explicit DoorTimerAdapter(TimedDoor &); void Timeout(); }; class TimedDoor : public Door { private: - DoorTimerAdapter * adapter; + DoorTimerAdapter *adapter; int iTimeout; bool isOpened; + public: explicit TimedDoor(int); bool isDoorOpened(); void unlock(); void lock(); - int getTimeOut() const; + int getTimeOut() const; void throwState(); }; class Timer { TimerClient *client; void sleep(int); + public: - void tregister(int, TimerClient*); + void tregister(int, TimerClient *); }; - -#endif // INCLUDE_TIMEDDOOR_H_ diff --git a/src/TimedDoor.cpp b/src/TimedDoor.cpp new file mode 100644 index 0000000..5d084f5 --- /dev/null +++ b/src/TimedDoor.cpp @@ -0,0 +1,44 @@ +// Copyright 2024 Derun Andrey + +#include "TimedDoor.h" + +#include // NOLINT [build/c++11] +#include +#include // NOLINT [build/c++11] + +DoorTimerAdapter::DoorTimerAdapter(TimedDoor& door_) : door(door_) {} + +void DoorTimerAdapter::Timeout() { + if (door.isDoorOpened()) throw std::runtime_error("Time's up!"); + return; +} + +TimedDoor::TimedDoor(int timeout_) : iTimeout(timeout_), isOpened(false) { + adapter = new DoorTimerAdapter(*this); +} + +bool TimedDoor::isDoorOpened() { return isOpened; } + +void TimedDoor::unlock() { + if (isOpened) throw std::logic_error("Door is already opened"); + isOpened = true; +} + +void TimedDoor::lock() { + if (!isOpened) throw std::logic_error("Door is already closed"); + isOpened = false; +} + +int TimedDoor::getTimeOut() const { return iTimeout; } + +void TimedDoor::throwState() { adapter->Timeout(); } + +void Timer::sleep(int time_) { + std::this_thread::sleep_for(std::chrono::seconds(time_)); +} + +void Timer::tregister(int time_, TimerClient* client_) { + this->client = client_; + sleep(time_); + client_->Timeout(); +} diff --git a/src/main.cpp b/src/main.cpp index 9b83162..afb6311 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,9 @@ -// Copyright 2021 GHA Test Team -#include "TimedDoor.h" +// Copyright 2024 Derun Andrey #include +#include "TimedDoor.h" + + int main() { TimedDoor tDoor(5); tDoor.lock(); diff --git a/test/tests.cpp b/test/tests.cpp index 605e570..609444e 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -1,6 +1,76 @@ -// Copyright 2021 GHA Test Team +// Copyright 2024 Derun Andrey -#include #include +#include + +#include // NOLINT [build/c++11] #include +#include // NOLINT [build/c++11] + #include "TimedDoor.h" + +class MockTimerClient : public TimerClient { + public: + MOCK_METHOD(void, Timeout, (), (override)); +}; + +class TimedDoorTest : public ::testing::Test { + protected: + TimedDoor door; + MockTimerClient mockClient; + Timer timer; + + TimedDoorTest() : door(1), timer() {} + + void SetUp() override { timer.tregister(door.getTimeOut(), &mockClient); } + + void TearDown() override { + testing::Mock::VerifyAndClearExpectations(&mockClient); + } +}; + +TEST_F(TimedDoorTest, UNL_D_TH_TIMEOUT) { + door.unlock(); + std::this_thread::sleep_for(std::chrono::seconds(door.getTimeOut())); + EXPECT_THROW(door.throwState(), std::runtime_error); +} + +TEST_F(TimedDoorTest, LOCK_D_TH_UNLOCK) { + std::this_thread::sleep_for(std::chrono::seconds(door.getTimeOut() + 1)); + EXPECT_NO_THROW(door.throwState()); +} + +TEST_F(TimedDoorTest, LOCK_BF_TIMEOUT_TH_OPEN) { + std::this_thread::sleep_for(std::chrono::seconds(door.getTimeOut() + 1)); + door.unlock(); + EXPECT_THROW(door.throwState(), std::runtime_error); +} + +TEST_F(TimedDoorTest, UNL_MTH) { + door.unlock(); + EXPECT_TRUE(door.isDoorOpened()); +} + +TEST_F(TimedDoorTest, LOCK_MTH) { + door.unlock(); + door.lock(); + EXPECT_FALSE(door.isDoorOpened()); +} + +TEST_F(TimedDoorTest, FST_D_CLO) { EXPECT_FALSE(door.isDoorOpened()); } + +TEST_F(TimedDoorTest, UNL_D_TW) { + door.unlock(); + EXPECT_THROW(door.unlock(), std::logic_error); +} + +TEST_F(TimedDoorTest, OPEN_EX) { + door.unlock(); + EXPECT_THROW(door.throwState(), std::runtime_error); +} + +TEST_F(TimedDoorTest, CLO_BF) { EXPECT_NO_THROW(door.throwState()); } + +TEST_F(TimedDoorTest, LOCK_D_TW) { + EXPECT_THROW(door.lock(), std::logic_error); +} From f469cf7524984a2d48482ad518517b8c49ec6b1d Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 20 May 2024 07:44:31 +0300 Subject: [PATCH 3/6] gtest --- .github/workflows/release.yml | 2 +- CMakeLists.txt | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e67c64f..df92952 100755 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Install gtest manually - run: sudo apt-get install libgtest-dev && cd /usr/src/gtest && sudo cmake CMakeLists.txt && sudo make && ls -l lib && sudo cp lib/*.a /usr/lib && sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a && sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a + run: sudo apt-get install libgtest-dev libgmock-dev && cd /usr/src/gtest && sudo cmake CMakeLists.txt && sudo make && ls -l lib && sudo cp lib/*.a /usr/lib && sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a && sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a - uses: actions/checkout@v1 - name: configure run: mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-Werror" .. diff --git a/CMakeLists.txt b/CMakeLists.txt index 36758e8..65f04a3 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,5 +9,13 @@ set(tool_dest "bin") set(lib_dest "lib") set(include_dest "include/") +include(FetchContent) +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.10.0 +) +FetchContent_MakeAvailable(googletest) + add_subdirectory(src) add_subdirectory(test) From 52349ae9ff6e581fc09a79f6abc2b92411f24685 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 30 May 2024 21:11:30 +0300 Subject: [PATCH 4/6] fix --- src/TimedDoor.cpp | 23 ++++++++++++++++------- src/main.cpp | 1 - 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/TimedDoor.cpp b/src/TimedDoor.cpp index 5d084f5..bd96387 100644 --- a/src/TimedDoor.cpp +++ b/src/TimedDoor.cpp @@ -6,26 +6,34 @@ #include #include // NOLINT [build/c++11] +// DoorTimerAdapter Implementation DoorTimerAdapter::DoorTimerAdapter(TimedDoor& door_) : door(door_) {} void DoorTimerAdapter::Timeout() { - if (door.isDoorOpened()) throw std::runtime_error("Time's up!"); - return; + if (door.isDoorOpened()) { + throw std::runtime_error("Time's up!"); + } } -TimedDoor::TimedDoor(int timeout_) : iTimeout(timeout_), isOpened(false) { - adapter = new DoorTimerAdapter(*this); -} +// TimedDoor Implementation +TimedDoor::TimedDoor(int timeout_) + : iTimeout(timeout_), + isOpened(false), + adapter(new DoorTimerAdapter(*this)) {} bool TimedDoor::isDoorOpened() { return isOpened; } void TimedDoor::unlock() { - if (isOpened) throw std::logic_error("Door is already opened"); + if (isOpened) { + throw std::logic_error("Door is already opened"); + } isOpened = true; } void TimedDoor::lock() { - if (!isOpened) throw std::logic_error("Door is already closed"); + if (!isOpened) { + throw std::logic_error("Door is already closed"); + } isOpened = false; } @@ -33,6 +41,7 @@ int TimedDoor::getTimeOut() const { return iTimeout; } void TimedDoor::throwState() { adapter->Timeout(); } +// Timer Implementation void Timer::sleep(int time_) { std::this_thread::sleep_for(std::chrono::seconds(time_)); } diff --git a/src/main.cpp b/src/main.cpp index afb6311..4faf2c1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,6 @@ #include "TimedDoor.h" - int main() { TimedDoor tDoor(5); tDoor.lock(); From 59ed000b8b962939cd8dc418c2cdf7ae4fea65dc Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 30 May 2024 21:15:04 +0300 Subject: [PATCH 5/6] fix --- src/main.cpp | 3 ++- test/tests.cpp | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4faf2c1..61cde30 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,8 @@ #include "TimedDoor.h" int main() { - TimedDoor tDoor(5); + int time = 5; + TimedDoor tDoor(time); tDoor.lock(); tDoor.unlock(); diff --git a/test/tests.cpp b/test/tests.cpp index 609444e..c892194 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -9,11 +9,13 @@ #include "TimedDoor.h" +// Mock Timer Client class MockTimerClient : public TimerClient { public: MOCK_METHOD(void, Timeout, (), (override)); }; +// Test Fixture for TimedDoor class TimedDoorTest : public ::testing::Test { protected: TimedDoor door; @@ -73,4 +75,4 @@ TEST_F(TimedDoorTest, CLO_BF) { EXPECT_NO_THROW(door.throwState()); } TEST_F(TimedDoorTest, LOCK_D_TW) { EXPECT_THROW(door.lock(), std::logic_error); -} +} \ No newline at end of file From 64309dc2cb7c9e7e44a9ef7be6006a638a7bfd7d Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 30 May 2024 21:15:12 +0300 Subject: [PATCH 6/6] fix --- test/tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tests.cpp b/test/tests.cpp index c892194..7343b7d 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -75,4 +75,4 @@ TEST_F(TimedDoorTest, CLO_BF) { EXPECT_NO_THROW(door.throwState()); } TEST_F(TimedDoorTest, LOCK_D_TW) { EXPECT_THROW(door.lock(), std::logic_error); -} \ No newline at end of file +}