diff --git a/include/queue/queue.tpp b/include/queue/queue.tpp index b7f341c..aaf395e 100644 --- a/include/queue/queue.tpp +++ b/include/queue/queue.tpp @@ -18,11 +18,17 @@ void Queue::clear() { template void Queue::push(Data data) { + if((ep+1)%QUEUE_SIZE==sp) throw std::overflow_error("Queue is full"); + buffer[ep] = data; + ep=(ep+1)%QUEUE_SIZE; } template Data Queue::pull() { - return buffer[sp]; + if(ep==sp) throw std::underflow_error("Queue is empty"); + Data tmp = buffer[sp]; + sp=(sp+1)%QUEUE_SIZE; + return tmp; } template @@ -32,5 +38,6 @@ Data Queue::top() { template int Queue::size() { - return 0; + int size = (ep-sp+QUEUE_SIZE)%QUEUE_SIZE; + return size; } \ No newline at end of file diff --git a/test/queue/queue_test.cpp b/test/queue/queue_test.cpp index 53b76a1..8513c36 100644 --- a/test/queue/queue_test.cpp +++ b/test/queue/queue_test.cpp @@ -42,4 +42,81 @@ TEST_F(QueueTest, push_and_pull) { // then EXPECT_EQ(second, v2); +} + +TEST_F(QueueTest, full_push){ + // given + for(int i=0;i<19;i++){ + int_queue.push(i); + } + // when + EXPECT_EQ(int_queue.size(), 19); + // then + EXPECT_THROW(int_queue.push(1),std::overflow_error); +} + +TEST_F(QueueTest, empty_pull) { + // given + // when + // then + EXPECT_THROW(int_queue.pull(),std::underflow_error); +} + +TEST_F(QueueTest, push_and_size){ + // given + int v1 = 1; + int v2 = 3; + int_queue.push(v1); + int_queue.push(v2); + + // when + int size = int_queue.size(); + + // then + EXPECT_EQ(size, 2); +} + +TEST_F(QueueTest, push19_size){ + // given + for(int i=0;i<19;i++){ + int_queue.push(i); + } + // when + int size = int_queue.size(); + + // then + EXPECT_EQ(size, 19); +} + +TEST_F(QueueTest, push19_pull10_and_size){ + // given + for(int i=0;i<19;i++){ + int_queue.push(i); + } + for(int i=0;i<10;i++){ + int_queue.pull(); + } + // when + int size = int_queue.size(); + + // then + EXPECT_EQ(size, 9); +} + +TEST_F(QueueTest, push19_pull10_push5_and_size){ + // given + for(int i=0;i<19;i++){ + int_queue.push(i); + } + for(int i=0;i<10;i++){ + int_queue.pull(); + } + for(int i=0;i<5;i++){ + int_queue.push(i); + } + // when + int size = int_queue.size(); + + // then + EXPECT_EQ(size, 14); } \ No newline at end of file