diff --git a/CMakeLists.txt b/CMakeLists.txt index 7dedc63..7d97db8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,12 +4,14 @@ project(TDD) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +enable_testing() add_subdirectory(src/calculator) add_subdirectory(src/bj1094) # GoogleTest + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) add_subdirectory(gtest) add_subdirectory(test/calculator) @@ -23,5 +25,4 @@ if(COVERAGE) add_link_options(--coverage) endif() -enable_testing() include(GoogleTest) diff --git a/include/queue/queue.hpp b/include/queue/queue.hpp index 05f2dfe..d5147b6 100644 --- a/include/queue/queue.hpp +++ b/include/queue/queue.hpp @@ -18,6 +18,8 @@ class Queue { Data pull(); Data top(); int size(); + bool is_full(); + bool is_empty(); }; #include "queue.tpp" diff --git a/include/queue/queue.tpp b/include/queue/queue.tpp index b7f341c..f2da377 100644 --- a/include/queue/queue.tpp +++ b/include/queue/queue.tpp @@ -18,19 +18,45 @@ void Queue::clear() { template void Queue::push(Data data) { + if (is_full()) { + throw std::runtime_error("Queue is full"); + } + + buffer[ep] = data; + ep = (ep + 1) % QUEUE_SIZE; } template Data Queue::pull() { - return buffer[sp]; + if (is_empty()) { + throw std::runtime_error("Queue is empty"); + } + + Data result = buffer[sp]; + sp = (sp + 1) % QUEUE_SIZE; + return result; } template Data Queue::top() { + if (is_empty()) { + throw std::runtime_error("Queue is empty"); + } + return buffer[sp]; } template int Queue::size() { - return 0; + return (ep - sp + QUEUE_SIZE) % QUEUE_SIZE; +} + +template +bool Queue::is_full() { + return ep == (sp - 1 + QUEUE_SIZE) % QUEUE_SIZE; +} + +template +bool Queue::is_empty() { + return sp == ep; } \ No newline at end of file diff --git a/test/queue/queue_test.cpp b/test/queue/queue_test.cpp index 53b76a1..dcbfeee 100644 --- a/test/queue/queue_test.cpp +++ b/test/queue/queue_test.cpp @@ -7,7 +7,6 @@ using namespace std; class QueueTest : public ::testing::Test { protected: Queue int_queue; - Queue string_queue; void SetUp() override { int_queue.clear(); @@ -42,4 +41,49 @@ TEST_F(QueueTest, push_and_pull) { // then EXPECT_EQ(second, v2); -} \ No newline at end of file +} + +TEST_F(QueueTest, size) { + // given + int_queue.push(1); + int_queue.push(2); + int_queue.push(3); + + // when + int size = int_queue.size(); + + // then + EXPECT_EQ(size, 3); +} + +TEST_F(QueueTest, clear) { + // given + int_queue.push(1); + int_queue.push(2); + int_queue.push(3); + + // when + int_queue.clear(); + int size = int_queue.size(); + + // then + EXPECT_EQ(size, 0); +} + +TEST_F(QueueTest, empty_pull) { + EXPECT_THROW(int_queue.pull(), std::runtime_error); +} + +TEST_F(QueueTest, empty_top) { + EXPECT_THROW(int_queue.top(), std::runtime_error); +} + +TEST_F(QueueTest, full_push) { + // given + for (int i = 0; i < QUEUE_SIZE - 1; i++) { + int_queue.push(i); + } + + // then + EXPECT_THROW(int_queue.push(QUEUE_SIZE), std::runtime_error); +} \ No newline at end of file diff --git a/test/queue/string_queue_test.cpp b/test/queue/string_queue_test.cpp new file mode 100644 index 0000000..2367351 --- /dev/null +++ b/test/queue/string_queue_test.cpp @@ -0,0 +1,71 @@ +#include +#include +#include "queue/queue.hpp" + +using namespace std; + +class StringQueueTest : public ::testing::Test { +protected: + Queue string_queue; + + void SetUp() override { + string_queue.clear(); + } + + void TearDown() override { + } +}; + +TEST_F(StringQueueTest, push_and_top) { + // given + string value = "hello"; + string_queue.push(value); + + // when + string top = string_queue.top(); + + // then + EXPECT_EQ(top, value); +} + +TEST_F(StringQueueTest, push_and_pull) { + // given + string v1 = "hello"; + string v2 = "world"; + string_queue.push(v1); + string_queue.push(v2); + + // when + string_queue.pull(); + string second = string_queue.pull(); + + // then + EXPECT_EQ(second, v2); +} + +TEST_F(StringQueueTest, size) { + // given + string_queue.push("hello"); + string_queue.push("world"); + string_queue.push("!"); + + // when + int size = string_queue.size(); + + // then + EXPECT_EQ(size, 3); +} + +TEST_F(StringQueueTest, clear) { + // given + string_queue.push("hello"); + string_queue.push("world"); + string_queue.push("!"); + + // when + string_queue.clear(); + int size = string_queue.size(); + + // then + EXPECT_EQ(size, 0); +} \ No newline at end of file