mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Added copy constructor and assignment operator
This commit is contained in:
parent
95404af1c8
commit
ee3dd66576
14
iqueue.h
14
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.
|
||||
//*************************************************************************
|
||||
|
||||
13
istack.h
13
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.
|
||||
//*************************************************************************
|
||||
|
||||
22
queue.h
22
queue.h
@ -67,6 +67,15 @@ namespace etl
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Copy constructor
|
||||
//*************************************************************************
|
||||
queue(const queue& rhs)
|
||||
: iqueue<T>(reinterpret_cast<T*>(&buffer.value[0]), SIZE)
|
||||
{
|
||||
iqueue<T>::clone(rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Destructor.
|
||||
//*************************************************************************
|
||||
@ -75,6 +84,19 @@ namespace etl
|
||||
clear();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Assignment operator.
|
||||
//*************************************************************************
|
||||
queue& operator =(const queue& rhs)
|
||||
{
|
||||
if (&rhs != this)
|
||||
{
|
||||
iqueue<T>::clone(rhs);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The unititialised buffer of T used in the stack.
|
||||
|
||||
@ -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.
|
||||
//*************************************************************************
|
||||
|
||||
22
stack.h
22
stack.h
@ -67,6 +67,15 @@ namespace etl
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Copy constructor
|
||||
//*************************************************************************
|
||||
stack(const stack& rhs)
|
||||
: istack<T>(reinterpret_cast<T*>(&buffer.value[0]), SIZE)
|
||||
{
|
||||
istack<T>::clone(rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Destructor.
|
||||
//*************************************************************************
|
||||
@ -75,6 +84,19 @@ namespace etl
|
||||
clear();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Assignment operator.
|
||||
//*************************************************************************
|
||||
stack& operator =(const stack& rhs)
|
||||
{
|
||||
if (&rhs != this)
|
||||
{
|
||||
istack<T>::clone(rhs);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The unititialised buffer of T used in the stack.
|
||||
|
||||
@ -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.
|
||||
//*************************************************************************
|
||||
|
||||
@ -34,6 +34,28 @@ namespace
|
||||
{
|
||||
SUITE(test_queue)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_copy_constructor)
|
||||
{
|
||||
etl::queue<int, 4> queue;
|
||||
|
||||
queue.push(1);
|
||||
queue.push(2);
|
||||
queue.push(3);
|
||||
queue.push(4);
|
||||
|
||||
etl::queue<int, 4> 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<int, 4> queue;
|
||||
|
||||
CHECK_EQUAL(4, queue.capacity());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_clear)
|
||||
{
|
||||
@ -199,7 +213,7 @@ namespace
|
||||
{
|
||||
etl::queue<int, 4> 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<int, 4> queue;
|
||||
|
||||
queue.push(1);
|
||||
queue.push(2);
|
||||
queue.push(3);
|
||||
queue.push(4);
|
||||
|
||||
etl::queue<int, 4> 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<int, 4> 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();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -39,6 +39,28 @@ namespace
|
||||
typedef TestDataDC<std::string> ItemDC;
|
||||
typedef TestDataNDC<std::string> ItemNDC;
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_copy_constructor)
|
||||
{
|
||||
etl::stack<int, 4> stack;
|
||||
|
||||
stack.push(1);
|
||||
stack.push(2);
|
||||
stack.push(3);
|
||||
stack.push(4);
|
||||
|
||||
etl::stack<int, 4> 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<int, 4> stack;
|
||||
|
||||
CHECK_EQUAL(4, stack.capacity());
|
||||
CHECK_EQUAL(4, stack.max_size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -136,7 +158,7 @@ namespace
|
||||
{
|
||||
etl::stack<int, 4> 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<int, 4> stack;
|
||||
|
||||
stack.push(1);
|
||||
stack.push(2);
|
||||
stack.push(3);
|
||||
stack.push(4);
|
||||
|
||||
etl::stack<int, 4> 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<int, 4> 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();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user