Skip to content
Merged
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
77 changes: 77 additions & 0 deletions include/queue/Queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

#include <deque>
namespace STLContainer{
template <class T, class Container = std::deque<T>> class queue{

public:
// type definition
using container_type = Container;
using value_type = typename Container::value_type;
using size_type = typename Container::size_type;
using reference = typename Container::reference;
using const_reference = typename Container::const_reference;

//
private:
Container _container;

public:
queue(): queue(Container()){};

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add default destructor

explicit queue(const Container& container): _container(container){}

explicit queue(Container&& container): _container(std::move(container)){};

queue(const queue& other):_container(other._container){};

queue(queue&& other):_container(std::move(other._container)){};

queue& operator = (const queue& other){
_container = other._container;
return *this;
}

~queue() = default;

// modifier
void push(const value_type& value){
_container.push_back(value);
}
// capacity
bool empty() const{
return _container.empty();
}

size_type size() const {
return _container.size();
}

void pop() {
_container.pop_front();
}
// swap the two queues
void swap(queue& other) noexcept{
std::swap(_container, other._container);
}

void emplace(value_type&& value){
_container.emplace_back(value);
}
reference front() {
return _container.front();
}

const_reference front() const{
return _container.front();
}

reference back(){
return _container.back();
}

const_reference back() const{
return _container.back();
}
};
}
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ file(GLOB TEST_FILES
${PROJECT_SOURCE_DIR}/test/bst/*.cpp
${PROJECT_SOURCE_DIR}/test/trie/*.cpp
${PROJECT_SOURCE_DIR}/test/vector/*.cpp
${PROJECT_SOURCE_DIR}/test/queue/*.cpp
${PROJECT_SOURCE_DIR}/test/queue/*.cpp
${PROJECT_SOURCE_DIR}/test/forward_list/*.cpp)


Expand Down
18 changes: 0 additions & 18 deletions test/forward_list/forward_list_iterator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,4 @@ TEST(FORWARD_LIST, ITERATOR_PLUS_POST) {
list_iterator != int_list.end() && ref_list_iterator != ref_int_list.end(); list_iterator++, ref_list_iterator++) {
ASSERT_EQ(*ref_list_iterator, *list_iterator);
}
}

TEST(FORWARD_LIST, AUTO_ITERATOR_AU_Test) {
STLContainer::forward_list<int> int_list;
int_list.push_front(1);
int_list.push_front(2);
int_list.push_front(3);
int_list.push_front(4);
int_list.push_front(5);

std::forward_list<int> ref_int_list;
ref_int_list.push_front(1);
ref_int_list.push_front(2);
size_t ref_index = 0;

for(auto element: int_list) {
std::cout<<element<<" ";
}std::cout<<std::endl;
}
122 changes: 122 additions & 0 deletions test/queue/queue_constructor_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "queue/Queue.h"
#include <gtest/gtest.h>

TEST( QUEUE, PUSH) {
STLContainer::queue<int> input;
ASSERT_TRUE( input.empty() );
ASSERT_EQ( 0, input.size() );
input.push(17);
input.push(16);
input.push(15);
input.push(14);
input.push(13);
input.push(12);
input.push(11);
for ( auto val : {17, 16, 15, 14, 13, 12, 11} ) {
ASSERT_EQ( val, input.front());
input.pop();
}
}

TEST( QUEUE, CONSTRUCT) {
STLContainer::queue<int> input;
ASSERT_TRUE( input.empty() );
ASSERT_EQ( 0, input.size() );
input.push(17);
input.push(16);
input.push(15);
input.push(14);
input.push(13);
input.push(12);
input.push(11);
for ( auto val : {17, 16, 15, 14, 13, 12, 11} ) {
ASSERT_EQ( val, input.front());
input.pop();
}
}

TEST( QUEUE, CONSTRUCT_COPY) {
STLContainer::queue<int> input;
input.push(17);
input.push(16);
input.push(15);
input.push(14);
input.push(13);
input.push(12);
input.push(11);
STLContainer::queue<int> input_copy(input);
for ( auto val : {17, 16, 15, 14, 13, 12, 11} ) {
ASSERT_EQ( val, input_copy.front());
input_copy.pop();
}
}

TEST( QUEUE, CONSTRUCT_COPY_ASSIGMENT) {
STLContainer::queue<int> input;
input.push(17);
input.push(16);
input.push(15);
input.push(14);
input.push(13);
input.push(12);
input.push(11);
STLContainer::queue<int> input_copy = input;
for ( auto val : {17, 16, 15, 14, 13, 12, 11} ) {
ASSERT_EQ( val, input_copy.front());
input_copy.pop();
}
}

TEST( QUEUE, CONSTRUCT_ADAPTIVE) {
std::deque<std::string> dq = {"this", "is", "a", "test","!"};
STLContainer::queue<std::string> input(dq);
for ( auto word : dq ) {
ASSERT_EQ( word, input.front());
input.pop();
}
}

TEST( QUEUE, CONSTRUCT_MOVE) {
std::deque<std::string> dq = {"this", "is", "a", "test","for", "move","constructor"};
STLContainer::queue<std::string> input_move_from(dq);

STLContainer::queue<std::string> input_move_to(std::move(input_move_from));

for ( auto word : dq ) {
ASSERT_EQ( word, input_move_to.front());
input_move_to.pop();
}
}

TEST( QUEUE, SWAP) {
std::deque<std::string> dq_left = {"this", "is", "left", "side","!"};
std::deque<std::string> dq_right = {"this", "is", "right", "side","!"};
STLContainer::queue<std::string> input_left(dq_left);
STLContainer::queue<std::string> input_right(dq_right);

input_left.swap(input_right);

for ( auto word : dq_right ) {
ASSERT_EQ( word, input_left.front());
input_left.pop();
}
}

TEST( QUEUE, EMPLACE) {
STLContainer::queue<int> input;
input.emplace(17);
input.emplace(16);
input.emplace(15);
for ( auto val : {17, 16, 15} ) {
ASSERT_EQ( val, input.front());
input.pop();
}
}

TEST( QUEUE, BACK) {
STLContainer::queue<int> input;
input.emplace(17);
input.emplace(16);
input.emplace(15);
ASSERT_EQ( 15, input.back());
}