Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SmirnovLM committed Apr 7, 2024
1 parent 40d254a commit 52b196a
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "googletest"]
path = googletest
url = https://github.com/google/googletest.git
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions include/TimedDoor.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 GHA Test Team
// Copyright 2024 Smirnov Leonid

#ifndef INCLUDE_TIMEDDOOR_H_
#define INCLUDE_TIMEDDOOR_H_
Expand All @@ -24,7 +24,7 @@ class DoorTimerAdapter : public TimerClient {
private:
TimedDoor& door;
public:
explicit DoorTimerAdapter(TimedDoor&);
explicit DoorTimerAdapter(TimedDoor &);
void Timeout();
};

Expand Down
53 changes: 53 additions & 0 deletions src/TimeDoor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 Smirnov Leonid

#include <stdexcept>
#include <thread>
#include <chrono>

#include "TimedDoor.h"

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();
}
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 GHA Test Team
// Copyright 2024 Smirnov Leonid
#include "TimedDoor.h"
#include <iostream>

Expand Down
2 changes: 1 addition & 1 deletion test/AllTests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 GHA Test Team
// Copyright 2024 Smirnov Leonid

#include <gtest/gtest.h>

Expand Down
78 changes: 77 additions & 1 deletion test/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,82 @@
// Copyright 2021 GHA Test Team
// Copyright 2024 Smirnov Leonid

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <cstdint>

#include <chrono>
#include <thread>

#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);
}

0 comments on commit 52b196a

Please sign in to comment.