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. b62 #1

Open
wants to merge 8 commits into
base: b62
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
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
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,14 @@ 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)
20 changes: 20 additions & 0 deletions CMakeLists.txt.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required (VERSION 3.10.2)
project (ST-3 CXX)

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/")

add_subdirectory(src)
add_subdirectory(test)

add_executable(ST-3 src/TimedDoor.cpp)
target_include_directories(ST-3 PRIVATE include)

add_executable(tests test/tests.cpp)
target_include_directories(tests PRIVATE include)
target_link_libraries(tests PRIVATE gtest gmock)
target_link_libraries(tests PRIVATE ST-3)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down
6 changes: 3 additions & 3 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 Kirill Nikitin

#ifndef INCLUDE_TIMEDDOOR_H_
#define INCLUDE_TIMEDDOOR_H_
Expand Down Expand Up @@ -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();
};

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

#include "TimedDoor.h"
#include <iostream>
#include <stdexcept>
#include <thread> // NOLINT [build/c++11]
#include <chrono> // NOLINT [build/c++11]

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

void DoorTimerAdapter::Timeout() {
if (door.isDoorOpened())
throw std::runtime_error("Your 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 open!!!");
isOpened = true;
}

void TimedDoor::lock() {
if (!isOpened)
throw std::logic_error("Door is close!!!");
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 timeout, TimerClient* client) {
client->Timeout();
sleep(timeout);
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 Kirill Nikitin
#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 Kirill Nikitin

#include <gtest/gtest.h>

Expand Down
76 changes: 75 additions & 1 deletion test/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,80 @@
// Copyright 2021 GHA Test Team
// Copyright 2024 Kirill Nikitin

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <cstdint>
#include <chrono> // NOLINT [build/c++11]
#include <thread> // NOLINT [build/c++11]
#include "TimedDoor.h"

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

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, id_door_open_method_test) {
EXPECT_FALSE(door.isDoorOpened());
}

TEST_F(TimedDoorTest, close_door_method_test) {
door.unlock();
door.lock();
EXPECT_FALSE(door.isDoorOpened());
}

TEST_F(TimedDoorTest, unlock_door_method_test) {
door.unlock();
EXPECT_TRUE(door.isDoorOpened());
}

TEST_F(TimedDoorTest, try_to_open_door_second_time_test) {
door.unlock();
EXPECT_THROW(door.unlock(), std::logic_error);
}

TEST_F(TimedDoorTest, lock_door_after_unlock_timeout_test) {
std::this_thread::sleep_for(std::chrono::seconds(door.getTimeOut() + 1));
EXPECT_NO_THROW(door.throwState());
}

TEST_F(TimedDoorTest, lock_before_timeout_then_open_throw_test) {
std::this_thread::sleep_for(std::chrono::seconds(door.getTimeOut() + 1));
door.unlock();
EXPECT_THROW(door.throwState(), std::runtime_error);
}

TEST_F(TimedDoorTest, unlock_door_then_timeout_exception_test) {
door.unlock();
std::this_thread::sleep_for(std::chrono::seconds(door.getTimeOut()));
EXPECT_THROW(door.throwState(), std::runtime_error);
}

TEST_F(TimedDoorTest, try_to_close_door_second_time) {
EXPECT_THROW(door.lock(), std::logic_error);
}

TEST_F(TimedDoorTest, open_exception_test) {
door.unlock();
EXPECT_THROW(door.throwState(), std::runtime_error);
}

TEST_F(TimedDoorTest, close_timeout_test) {
EXPECT_NO_THROW(door.throwState());
}
Loading