diff --git a/include/queue/Queue.h b/include/queue/Queue.h index e69de29..ab9ded7 100644 --- a/include/queue/Queue.h +++ b/include/queue/Queue.h @@ -0,0 +1,77 @@ +#pragma once + +#include +namespace STLContainer{ + template > 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()){}; + + 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(); + } + }; +} \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a375ed9..6d66afb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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) diff --git a/test/forward_list/forward_list_iterator_test.cpp b/test/forward_list/forward_list_iterator_test.cpp index b4189eb..b076c5a 100644 --- a/test/forward_list/forward_list_iterator_test.cpp +++ b/test/forward_list/forward_list_iterator_test.cpp @@ -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_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 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< + +TEST( QUEUE, PUSH) { + STLContainer::queue 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 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 input; + input.push(17); + input.push(16); + input.push(15); + input.push(14); + input.push(13); + input.push(12); + input.push(11); + STLContainer::queue 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 input; + input.push(17); + input.push(16); + input.push(15); + input.push(14); + input.push(13); + input.push(12); + input.push(11); + STLContainer::queue 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 dq = {"this", "is", "a", "test","!"}; + STLContainer::queue input(dq); + for ( auto word : dq ) { + ASSERT_EQ( word, input.front()); + input.pop(); + } +} + +TEST( QUEUE, CONSTRUCT_MOVE) { + std::deque dq = {"this", "is", "a", "test","for", "move","constructor"}; + STLContainer::queue input_move_from(dq); + + STLContainer::queue 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 dq_left = {"this", "is", "left", "side","!"}; + std::deque dq_right = {"this", "is", "right", "side","!"}; + STLContainer::queue input_left(dq_left); + STLContainer::queue 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 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 input; + input.emplace(17); + input.emplace(16); + input.emplace(15); + ASSERT_EQ( 15, input.back()); +} \ No newline at end of file