Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Платонова Мария. Задача 3 #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ project (ST-3)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -g")


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)
2 changes: 1 addition & 1 deletion include/TimedDoor.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 GHA Test Team
// Copyright 2024 Platonova Maria

#ifndef INCLUDE_TIMEDDOOR_H_
#define INCLUDE_TIMEDDOOR_H_
Expand Down
41 changes: 41 additions & 0 deletions src/TimedDoor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 Platonova Maria

#include "../include/TimedDoor.h"
#include <stdexcept>

DoorTimerAdapter::DoorTimerAdapter(TimedDoor &door) : door(door) {}

void DoorTimerAdapter::Timeout() { door.throwState(); }

TimedDoor::TimedDoor(int timeout) : iTimeout(timeout), isOpened(false) {
adapter = new DoorTimerAdapter(*this);
}

bool TimedDoor::isDoorOpened() { return isOpened; }

void TimedDoor::unlock() {
isOpened = true;
adapter->Timeout();
}

void TimedDoor::lock() { isOpened = false; }

int TimedDoor::getTimeOut() const {
if (!isOpened) {
throw std::runtime_error("Door Timeout");
}
return 1;
}

void TimedDoor::throwState() { getTimeOut(); }

void Timer::tregister(int timeout, TimerClient *client) {
client->Timeout();
sleep(timeout);
}

void Timer::sleep(int timeout) {
for (int i = 0; i < timeout; i++) {
i--;
}
}
65 changes: 64 additions & 1 deletion test/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,69 @@
// Copyright 2021 GHA Test Team
// Copyright 2024 Platonova Maria

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

class MockTimerClient : public TimerClient {
public:
MOCK_METHOD(void, Timeout, ());
};

class TimedDoorTest : public ::testing::Test {
protected:
void SetUp() override {
door = new TimedDoor(100);
}

void TearDown() override {
delete door;
}

TimedDoor* door;
};
TEST(TimerTest, TimerClientRegistered) {
Timer timer;
MockTimerClient client;
EXPECT_CALL(client, Timeout()).Times(1);
timer.tregister(100, &client);
}
TEST_F(TimedDoorTest, isLockable) {
door->unlock();
door->lock();
EXPECT_FALSE(door->isDoorOpened());
}

TEST_F(TimedDoorTest, isOpened) {
door->unlock();
EXPECT_TRUE(door->isDoorOpened());
}

TEST_F(TimedDoorTest, isClosed) {
EXPECT_FALSE(door->isDoorOpened());
}

TEST_F(TimedDoorTest, TimeOutDoorClosedAndOpened) {
door->unlock();
door->lock();
EXPECT_THROW(door->getTimeOut(), std::runtime_error);
EXPECT_FALSE(door->isDoorOpened());
}

TEST_F(TimedDoorTest, TimeOutDoorOpenedUnlocked) {
door->unlock();
door->unlock();
EXPECT_NO_THROW(door->getTimeOut());
EXPECT_TRUE(door->isDoorOpened());
}

TEST_F(TimedDoorTest, TimeOutDoorClosed) {
EXPECT_THROW(door->getTimeOut(), std::runtime_error);
EXPECT_FALSE(door->isDoorOpened());
}

TEST_F(TimedDoorTest, TimeOutDoorOpened) {
door->unlock();
EXPECT_NO_THROW(door->getTimeOut());
EXPECT_TRUE(door->isDoorOpened());
}
Loading