From 1360e6a013848ce40b5ee9a051ba8dda32570edb Mon Sep 17 00:00:00 2001 From: qizhangncs Date: Thu, 15 Nov 2018 02:27:00 -0500 Subject: [PATCH 1/2] add queue implementation --- include/queue/Queue.h | 73 +++++++++++++++ test/CMakeLists.txt | 4 +- test/queue/queue_constructor_test.cpp | 122 ++++++++++++++++++++++++++ 3 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 test/queue/queue_constructor_test.cpp diff --git a/include/queue/Queue.h b/include/queue/Queue.h index e69de29..95ff20d 100644 --- a/include/queue/Queue.h +++ b/include/queue/Queue.h @@ -0,0 +1,73 @@ +#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()){};// default constuct + + queue(const Container& container): _container(container){}; + + queue(const queue& other):_container(other._container){}; + + queue(queue&& other):_container(std::move(other._container)){}; + + queue& operator = (const queue& other){ + this->_container = other._container; + return *this; + } + + // 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 94dc0d8..ea40aa8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -15,8 +15,8 @@ file(GLOB TEST_FILES ${PROJECT_SOURCE_DIR}/test/stack/*.cpp ${PROJECT_SOURCE_DIR}/test/bst/*.cpp ${PROJECT_SOURCE_DIR}/test/trie/*.cpp - ${PROJECT_SOURCE_DIR}/test/vector/*.cpp) - #${PROJECT_SOURCE_DIR}/test/bsts/*.cpp + ${PROJECT_SOURCE_DIR}/test/vector/*.cpp + ${PROJECT_SOURCE_DIR}/test/queue/*.cpp) add_executable(unittest_container unit_tests.cpp ${TEST_FILES}) diff --git a/test/queue/queue_constructor_test.cpp b/test/queue/queue_constructor_test.cpp new file mode 100644 index 0000000..42ae1b9 --- /dev/null +++ b/test/queue/queue_constructor_test.cpp @@ -0,0 +1,122 @@ +#include "queue/Queue.h" +#include + +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 From 82003ddac969a574b528bb9cf405c74872fef8aa Mon Sep 17 00:00:00 2001 From: qizhangncs Date: Thu, 15 Nov 2018 13:40:42 -0500 Subject: [PATCH 2/2] add default destruct --- include/queue/Queue.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/include/queue/Queue.h b/include/queue/Queue.h index 95ff20d..ab9ded7 100644 --- a/include/queue/Queue.h +++ b/include/queue/Queue.h @@ -17,19 +17,23 @@ namespace STLContainer{ Container _container; public: - queue(): queue(Container()){};// default constuct + queue(): queue(Container()){}; - queue(const Container& container): _container(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){ - this->_container = other._container; + _container = other._container; return *this; } + ~queue() = default; + // modifier void push(const value_type& value){ _container.push_back(value);