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 #49

Open
wants to merge 7 commits into
base: b11
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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ..
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
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 v1.15.2
)
FetchContent_MakeAvailable(googletest)

add_subdirectory(src)
add_subdirectory(test)
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
4 changes: 2 additions & 2 deletions include/TimedDoor.h
Original file line number Diff line number Diff line change
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
50 changes: 50 additions & 0 deletions src/TimedDoor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 Mironov I.

#include "./TimedDoor.h"

#include <chrono> // NOLINT [build/c++11]
#include <stdexcept>
#include <thread> // NOLINT [build/c++11]

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

void DoorTimerAdapter::Timeout() {
if (door.isDoorOpened()) {
throw std::runtime_error("Time's up!");
}
}

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();
}
74 changes: 72 additions & 2 deletions test/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,76 @@
// Copyright 2021 GHA Test Team
// Copyright 2024 Mironov I.

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

#include <chrono> // NOLINT [build/c++11]
#include <cstdint>
#include <thread> // 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, unlockExpectThrow) {
door.unlock();
std::this_thread::sleep_for(std::chrono::seconds(door.getTimeOut()));
EXPECT_THROW(door.throwState(), std::runtime_error);
}

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

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

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

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

TEST_F(TimedDoorTest, isDoorCheck) { EXPECT_FALSE(door.isDoorOpened()); }

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

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

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

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