Commit eeeee7ff authored by Davis King's avatar Davis King

Added unit tests for the circular_buffer and fixed a few things.

parent cd100541
......@@ -59,8 +59,20 @@ namespace dlib
return data[(i+offset)%data.size()];
}
void resize(unsigned long size, const T& value = T()) { data.resize(size,value); }
void assign(unsigned long size, const T& value) { data.assign(size,value); }
void resize(unsigned long size)
{
offset = 0;
data.resize(size);
}
void assign(
unsigned long size,
const T& value
)
{
offset = 0;
data.assign(size,value);
}
unsigned long size() const { return data.size(); }
......@@ -126,6 +138,13 @@ namespace dlib
return (*this)[size()-1];
}
void swap( circular_buffer& item)
{
std::swap(item.offset, offset);
data.swap(item.data);
}
private:
std::vector<T> data;
......
......@@ -78,15 +78,22 @@ namespace dlib
!*/
void resize(
unsigned long new_size,
const T& c = T()
unsigned long new_size
);
/*!
ensures
- #size() == new_size
- any element with index between 0 and new_size - 1 which was in the
circular_buffer before the call to resize() retains its value and index.
All other elements have a value given by c.
!*/
void assign(
unsigned long new_size,
const T& value
);
/*!
ensures
- #size() == new_size
- for all valid i:
- (*this)[i] == value
!*/
unsigned long size(
......
......@@ -304,6 +304,113 @@ namespace
void test_circular_buffer()
{
circular_buffer<int> buf;
DLIB_TEST(buf.size() == 0);
buf.assign(4, 0);
DLIB_TEST(buf.size() == 4);
DLIB_TEST(buf[0] == 0);
DLIB_TEST(buf[1] == 0);
DLIB_TEST(buf[2] == 0);
DLIB_TEST(buf[3] == 0);
buf.push_back(1);
DLIB_TEST(buf[0] == 0);
DLIB_TEST(buf[1] == 0);
DLIB_TEST(buf[2] == 0);
DLIB_TEST(buf[3] == 1);
buf.push_back(2);
DLIB_TEST(buf[0] == 0);
DLIB_TEST(buf[1] == 0);
DLIB_TEST(buf[2] == 1);
DLIB_TEST(buf[3] == 2);
buf.push_front(3);
DLIB_TEST(buf[0] == 3);
DLIB_TEST(buf[1] == 0);
DLIB_TEST(buf[2] == 0);
DLIB_TEST(buf[3] == 1);
buf.push_front(4);
DLIB_TEST(buf.front() == 4);
DLIB_TEST(buf[0] == 4);
DLIB_TEST(buf[1] == 3);
DLIB_TEST(buf[2] == 0);
DLIB_TEST(buf[3] == 0);
buf.assign(4, 5);
DLIB_TEST(buf[0] == 5);
DLIB_TEST(buf[1] == 5);
DLIB_TEST(buf[2] == 5);
DLIB_TEST(buf[3] == 5);
buf.push_back(3);
DLIB_TEST(buf[0] == 5);
DLIB_TEST(buf[1] == 5);
DLIB_TEST(buf[2] == 5);
DLIB_TEST(buf[3] == 3);
buf.push_back(2);
DLIB_TEST(buf[0] == 5);
DLIB_TEST(buf[1] == 5);
DLIB_TEST(buf[2] == 3);
DLIB_TEST(buf[3] == 2);
buf.push_back(1);
DLIB_TEST(buf[0] == 5);
DLIB_TEST(buf[1] == 3);
DLIB_TEST(buf[2] == 2);
DLIB_TEST(buf[3] == 1);
buf.push_back(0);
DLIB_TEST(buf[0] == 3);
DLIB_TEST(buf[1] == 2);
DLIB_TEST(buf[2] == 1);
DLIB_TEST(buf[3] == 0);
buf.push_back(-1);
DLIB_TEST(buf.back() == -1);
DLIB_TEST(buf[0] == 2);
DLIB_TEST(buf[1] == 1);
DLIB_TEST(buf[2] == 0);
DLIB_TEST(buf[3] == -1);
buf.resize(1);
buf[0] = 9;
DLIB_TEST(buf.size() == 1);
DLIB_TEST(buf[0] == 9);
buf.push_back(1);
DLIB_TEST(buf[0] == 1);
buf.push_back(4);
DLIB_TEST(buf[0] == 4);
buf.push_front(3);
DLIB_TEST(buf[0] == 3);
buf.clear();
DLIB_TEST(buf.size() == 0);
buf.assign(3, 0);
circular_buffer<int> buf2, buf3;
buf.push_back(1);
buf.push_back(2);
ostringstream sout;
serialize(buf, sout);
istringstream sin(sout.str());
deserialize(buf2, sin);
DLIB_TEST(buf.size() == buf2.size());
for (unsigned long i = 0; i < buf.size(); ++i)
DLIB_TEST(buf[i] == buf2[i]);
buf.swap(buf3);
DLIB_TEST(buf.size() == 0);
DLIB_TEST(buf3.size() == buf2.size());
for (unsigned long i = 0; i < buf3.size(); ++i)
DLIB_TEST(buf3[i] == buf2[i]);
}
......@@ -323,6 +430,8 @@ namespace
sliding_buffer_kernel_test<sliding_buffer<unsigned char>::kernel_1a> ();
dlog << LINFO << "testing kernel_1a_c";
sliding_buffer_kernel_test<sliding_buffer<unsigned char>::kernel_1a_c>();
test_circular_buffer();
}
} a;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment