Merge remote-tracking branch 'github-heinz/Work' into heinz

Conflicts:
	unit-tests/buffer_t.cc
This commit is contained in:
Joe Thornber 2013-01-29 11:40:56 +00:00
commit 0f3a9cc567
2 changed files with 35 additions and 6 deletions

View File

@ -93,7 +93,7 @@ namespace persistent_data {
static void check_index(unsigned index) { static void check_index(unsigned index) {
if (index >= Size) if (index >= Size)
throw std::runtime_error("buffer index out of bounds"); throw std::range_error("buffer index out of bounds");
} }
}; };
} }

View File

@ -16,6 +16,9 @@
// with thin-provisioning-tools. If not, see // with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
// Set to one to add compile time checks.
#define COMPILE_TIME_ERROR 0
#include "persistent-data/buffer.h" #include "persistent-data/buffer.h"
#define BOOST_TEST_MODULE BufferTests #define BOOST_TEST_MODULE BufferTests
@ -49,14 +52,16 @@ namespace {
//---------------------------------------------------------------- //----------------------------------------------------------------
#if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_copy_fails) BOOST_AUTO_TEST_CASE(buffer_copy_fails)
{ {
uint32_t const sz = 8, align = 8; uint32_t const sz = 8, align = 8;
buffer<sz, align>::ptr b1 = create_buffer<sz, align>(); buffer<sz, align>::ptr b1 = create_buffer<sz, align>();
buffer<sz, align>::ptr b2; buffer<sz, align>::ptr b2;
// *b2 = *b1; // Compile time failure *b2 = *b1; // Compile time failure
} }
#endif
BOOST_AUTO_TEST_CASE(buffer_8_a_8_raw_access) BOOST_AUTO_TEST_CASE(buffer_8_a_8_raw_access)
{ {
@ -69,20 +74,29 @@ BOOST_AUTO_TEST_CASE(buffer_8_a_8_raw_access)
BOOST_CHECK(p[0] == 0); BOOST_CHECK(p[0] == 0);
p[0] = 4; p[0] = 4;
BOOST_CHECK(p[0] == 4); BOOST_CHECK(p[0] == 4);
// pc[0] = 5; // Compile time error accessing read-only location
} }
#if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_8_a_8_raw_const_access)
{
uint32_t const sz = 8, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
unsigned char const *pc = b->raw();
pc[0] = 5; // Compile time error accessing read-only location
}
#endif
BOOST_AUTO_TEST_CASE(buffer_8_a_8_access) BOOST_AUTO_TEST_CASE(buffer_8_a_8_access)
{ {
uint32_t const sz = 8, align = 8; uint32_t const sz = 8, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>(); buffer<sz, align>::ptr b = create_buffer<sz, align>();
(*b)[0] = 0; (*b)[0] = 0;
BOOST_CHECK_EQUAL((*b)[0], '\0'); BOOST_CHECK_EQUAL((*b)[0], 0);
} }
#if 0 #if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_8_a_8_const_access) BOOST_AUTO_TEST_CASE(buffer_8_a_8_const_access)
{ {
uint32_t const sz = 8, align = 8; uint32_t const sz = 8, align = 8;
@ -92,7 +106,16 @@ BOOST_AUTO_TEST_CASE(buffer_8_a_8_const_access)
} }
#endif #endif
BOOST_AUTO_TEST_CASE(buffer_8_a_8_oob)
{
uint32_t const sz = 8, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_THROW((*b)[8], std::range_error);
}
// 8 byte buffer size, varying alignment from 1 - 7 // 8 byte buffer size, varying alignment from 1 - 7
#if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_128_a_1_fails) BOOST_AUTO_TEST_CASE(buffer_128_a_1_fails)
{ {
uint32_t const sz = 128, align = 1; uint32_t const sz = 128, align = 1;
@ -101,6 +124,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_1_fails)
BOOST_CHECK(!b); BOOST_CHECK(!b);
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 1); BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 1);
} }
#endif
BOOST_AUTO_TEST_CASE(buffer_128_a_2_succeeds) BOOST_AUTO_TEST_CASE(buffer_128_a_2_succeeds)
{ {
@ -110,6 +134,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_2_succeeds)
BOOST_CHECK(b); BOOST_CHECK(b);
} }
#if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_128_a_3_fails) BOOST_AUTO_TEST_CASE(buffer_128_a_3_fails)
{ {
uint32_t const sz = 128, align = 3; uint32_t const sz = 128, align = 3;
@ -117,6 +142,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_3_fails)
BOOST_CHECK(!b); BOOST_CHECK(!b);
} }
#endif
BOOST_AUTO_TEST_CASE(buffer_128_a_4_succeeds) BOOST_AUTO_TEST_CASE(buffer_128_a_4_succeeds)
{ {
@ -126,6 +152,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_4_succeeds)
BOOST_CHECK(b); BOOST_CHECK(b);
} }
#if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_128_a_5_fails) BOOST_AUTO_TEST_CASE(buffer_128_a_5_fails)
{ {
uint32_t const sz = 128, align = 5; uint32_t const sz = 128, align = 5;
@ -149,6 +176,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_7_fails)
BOOST_CHECK(!b); BOOST_CHECK(!b);
} }
#endif
// Varying buffer size, alignment 8 // Varying buffer size, alignment 8
BOOST_AUTO_TEST_CASE(buffer_8_a_8) BOOST_AUTO_TEST_CASE(buffer_8_a_8)
@ -329,5 +357,6 @@ BOOST_AUTO_TEST_CASE(buffer_8192_a_16)
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0); BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
} }
#undef COMPILE_TIME_ERROR
//---------------------------------------------------------------- //----------------------------------------------------------------