Skip to content
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: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -23,5 +25,4 @@ if(COVERAGE)
add_link_options(--coverage)
endif()

enable_testing()
include(GoogleTest)
2 changes: 2 additions & 0 deletions include/queue/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Queue {
Data pull();
Data top();
int size();
bool is_full();
bool is_empty();
};

#include "queue.tpp"
Expand Down
30 changes: 28 additions & 2 deletions include/queue/queue.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,45 @@ void Queue<Data>::clear() {

template<class Data>
void Queue<Data>::push(Data data) {
if (is_full()) {
throw std::runtime_error("Queue is full");
}

buffer[ep] = data;
ep = (ep + 1) % QUEUE_SIZE;
}

template<class Data>
Data Queue<Data>::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<class Data>
Data Queue<Data>::top() {
if (is_empty()) {
throw std::runtime_error("Queue is empty");
}

return buffer[sp];
}

template<class Data>
int Queue<Data>::size() {
return 0;
return (ep - sp + QUEUE_SIZE) % QUEUE_SIZE;
}

template<class Data>
bool Queue<Data>::is_full() {
return ep == (sp - 1 + QUEUE_SIZE) % QUEUE_SIZE;
}

template<class Data>
bool Queue<Data>::is_empty() {
return sp == ep;
}
48 changes: 46 additions & 2 deletions test/queue/queue_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ using namespace std;
class QueueTest : public ::testing::Test {
protected:
Queue<int> int_queue;
Queue<string> string_queue;

void SetUp() override {
int_queue.clear();
Expand Down Expand Up @@ -42,4 +41,49 @@ TEST_F(QueueTest, push_and_pull) {

// then
EXPECT_EQ(second, v2);
}
}

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);
}
71 changes: 71 additions & 0 deletions test/queue/string_queue_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <gtest/gtest.h>
#include <string>
#include "queue/queue.hpp"

using namespace std;

class StringQueueTest : public ::testing::Test {
protected:
Queue<string> 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);
}