diff --git a/iqueue.h b/iqueue.h index fb61930c..b68ea890 100644 --- a/iqueue.h +++ b/iqueue.h @@ -197,6 +197,20 @@ namespace etl protected: + //************************************************************************* + /// Make this a clone of the supplied queue + //************************************************************************* + void clone(const iqueue& other) + { + size_t index = other.out; + + for (size_t i = 0; i < other.size(); ++i) + { + push(other.p_buffer[index]); + index = (index == (MAX_SIZE - 1)) ? 0 : index + 1; + } + } + //************************************************************************* /// The constructor that is called from derived classes. //************************************************************************* diff --git a/istack.h b/istack.h index 81499836..ab301565 100644 --- a/istack.h +++ b/istack.h @@ -170,6 +170,19 @@ namespace etl protected: + //************************************************************************* + /// Make this a clone of the supplied stack + //************************************************************************* + void clone(const istack& other) + { + size_t index = 0; + + for (size_t i = 0; i < other.size(); ++i) + { + push(other.p_buffer[index++]); + } + } + //************************************************************************* /// The constructor that is called from derived classes. //************************************************************************* diff --git a/queue.h b/queue.h index 77f16953..d8a279dc 100644 --- a/queue.h +++ b/queue.h @@ -67,6 +67,15 @@ namespace etl { } + //************************************************************************* + /// Copy constructor + //************************************************************************* + queue(const queue& rhs) + : iqueue(reinterpret_cast(&buffer.value[0]), SIZE) + { + iqueue::clone(rhs); + } + //************************************************************************* /// Destructor. //************************************************************************* @@ -75,6 +84,19 @@ namespace etl clear(); } + //************************************************************************* + /// Assignment operator. + //************************************************************************* + queue& operator =(const queue& rhs) + { + if (&rhs != this) + { + iqueue::clone(rhs); + } + + return *this; + } + private: /// The unititialised buffer of T used in the stack. diff --git a/queue_base.h b/queue_base.h index c902fb97..eb0e1a34 100644 --- a/queue_base.h +++ b/queue_base.h @@ -85,14 +85,6 @@ namespace etl return current_size; } - //************************************************************************* - /// Returns the maximum number of items that can be queued. - //************************************************************************* - size_type capacity() const - { - return MAX_SIZE; - } - //************************************************************************* /// Returns the maximum number of items that can be queued. //************************************************************************* diff --git a/stack.h b/stack.h index 35d2eff5..c085e2eb 100644 --- a/stack.h +++ b/stack.h @@ -67,6 +67,15 @@ namespace etl { } + //************************************************************************* + /// Copy constructor + //************************************************************************* + stack(const stack& rhs) + : istack(reinterpret_cast(&buffer.value[0]), SIZE) + { + istack::clone(rhs); + } + //************************************************************************* /// Destructor. //************************************************************************* @@ -75,6 +84,19 @@ namespace etl clear(); } + //************************************************************************* + /// Assignment operator. + //************************************************************************* + stack& operator =(const stack& rhs) + { + if (&rhs != this) + { + istack::clone(rhs); + } + + return *this; + } + private: /// The unititialised buffer of T used in the stack. diff --git a/stack_base.h b/stack_base.h index 69222bc0..34524a7f 100644 --- a/stack_base.h +++ b/stack_base.h @@ -104,14 +104,6 @@ namespace etl return current_size; } - //************************************************************************* - /// Returns the maximum number of items that can be stacked. - //************************************************************************* - size_type capacity() const - { - return MAX_SIZE; - } - //************************************************************************* /// Returns the maximum number of items that can be stacked. //************************************************************************* diff --git a/test/test_queue.cpp b/test/test_queue.cpp index cfabcdf6..f8335976 100644 --- a/test/test_queue.cpp +++ b/test/test_queue.cpp @@ -34,6 +34,28 @@ namespace { SUITE(test_queue) { + //************************************************************************* + TEST(test_copy_constructor) + { + etl::queue queue; + + queue.push(1); + queue.push(2); + queue.push(3); + queue.push(4); + + etl::queue queue2(queue); + + CHECK(queue.size() == queue2.size()); + + while (!queue.empty()) + { + CHECK_EQUAL(queue.front(), queue2.front()); + queue.pop(); + queue2.pop(); + } + } + //************************************************************************* TEST(test_size) { @@ -46,14 +68,6 @@ namespace CHECK_EQUAL(3, queue.size()); } - //************************************************************************* - TEST(test_capacity) - { - etl::queue queue; - - CHECK_EQUAL(4, queue.capacity()); - } - //************************************************************************* TEST(test_clear) { @@ -199,7 +213,7 @@ namespace { etl::queue queue; - for (size_t i = 0; i < queue.capacity(); ++i) + for (size_t i = 0; i < queue.max_size(); ++i) { queue.push(1); } @@ -283,5 +297,56 @@ namespace queue.pop(); CHECK_EQUAL(1, queue.size()); } + + //************************************************************************* + TEST(test_assignment) + { + etl::queue queue; + + queue.push(1); + queue.push(2); + queue.push(3); + queue.push(4); + + etl::queue queue2; + + queue2 = queue; + + CHECK(queue.size() == queue2.size()); + + while (!queue.empty()) + { + CHECK_EQUAL(queue.front(), queue2.front()); + queue.pop(); + queue2.pop(); + } + } + + //************************************************************************* + TEST(test_self_assignment) + { + etl::queue queue; + + queue.push(1); + queue.push(2); + queue.push(3); + queue.push(4); + + queue = queue; + + CHECK(queue.max_size() == queue.size()); + + CHECK_EQUAL(1, queue.front()); + queue.pop(); + + CHECK_EQUAL(2, queue.front()); + queue.pop(); + + CHECK_EQUAL(3, queue.front()); + queue.pop(); + + CHECK_EQUAL(4, queue.front()); + queue.pop(); + } }; } diff --git a/test/test_stack.cpp b/test/test_stack.cpp index 3c38ffe2..afefff4f 100644 --- a/test/test_stack.cpp +++ b/test/test_stack.cpp @@ -39,6 +39,28 @@ namespace typedef TestDataDC ItemDC; typedef TestDataNDC ItemNDC; + //************************************************************************* + TEST(test_copy_constructor) + { + etl::stack stack; + + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + + etl::stack stack2(stack); + + CHECK(stack.size() == stack2.size()); + + while (!stack.empty()) + { + CHECK_EQUAL(stack.top(), stack2.top()); + stack.pop(); + stack2.pop(); + } + } + //************************************************************************* TEST(test_empty) { @@ -79,11 +101,11 @@ namespace } //************************************************************************* - TEST(test_capacity) + TEST(test_max_size) { etl::stack stack; - CHECK_EQUAL(4, stack.capacity()); + CHECK_EQUAL(4, stack.max_size()); } //************************************************************************* @@ -136,7 +158,7 @@ namespace { etl::stack stack; - for (size_t i = 0; i < stack.capacity(); ++i) + for (size_t i = 0; i < stack.max_size(); ++i) { stack.push(1); } @@ -257,5 +279,56 @@ namespace CHECK(pass); } + + //************************************************************************* + TEST(test_assignment) + { + etl::stack stack; + + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + + etl::stack stack2; + + stack2 = stack; + + CHECK(stack.size() == stack2.size()); + + while (!stack.empty()) + { + CHECK_EQUAL(stack.top(), stack2.top()); + stack.pop(); + stack2.pop(); + } + } + + //************************************************************************* + TEST(test_self_assignment) + { + etl::stack stack; + + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + + stack = stack; + + CHECK(stack.max_size() == stack.size()); + + CHECK_EQUAL(4, stack.top()); + stack.pop(); + + CHECK_EQUAL(3, stack.top()); + stack.pop(); + + CHECK_EQUAL(2, stack.top()); + stack.pop(); + + CHECK_EQUAL(1, stack.top()); + stack.pop(); + } }; }