Convert buffer_t to gmock.

This commit is contained in:
Joe Thornber 2013-03-22 12:04:38 +00:00
parent e3375443eb
commit c044c2c729
6 changed files with 121 additions and 263 deletions

View File

@ -70,7 +70,17 @@ Feature: cache_check
And block 1 is zeroed And block 1 is zeroed
When I run `cache_check input` When I run `cache_check input`
And the stderr should contain: And the stderr should contain:
""" """
input: No superblock found input: No superblock found
""" """
Scenario: A valid metadata area passes
Given metadata containing:
"""
"""
When I run cache_check
Then it should pass

View File

@ -4,5 +4,3 @@ wget https://googlemock.googlecode.com/files/gmock-1.6.0.zip
unzip gmock-1.6.0.zip unzip gmock-1.6.0.zip
cd gmock-1.6.0 cd gmock-1.6.0
./configure ./configure
make
make check

View File

@ -47,6 +47,7 @@ namespace persistent_data {
BOOST_STATIC_ASSERT_MSG((Alignment > 1) & !(Alignment & (Alignment - 1)), BOOST_STATIC_ASSERT_MSG((Alignment > 1) & !(Alignment & (Alignment - 1)),
"Alignment must be a power of two."); "Alignment must be a power of two.");
static uint32_t const ALIGNMENT = Alignment;
typedef boost::shared_ptr<buffer> ptr; typedef boost::shared_ptr<buffer> ptr;
typedef boost::shared_ptr<buffer const> const_ptr; typedef boost::shared_ptr<buffer const> const_ptr;

View File

@ -20,7 +20,6 @@ TEST_SOURCE=\
unit-tests/array_block_t.cc \ unit-tests/array_block_t.cc \
unit-tests/array_t.cc \ unit-tests/array_t.cc \
unit-tests/bitset_t.cc \ unit-tests/bitset_t.cc \
unit-tests/buffer_t.cc \
unit-tests/cache_t.cc \ unit-tests/cache_t.cc \
unit-tests/block_t.cc \ unit-tests/block_t.cc \
unit-tests/btree_t.cc \ unit-tests/btree_t.cc \
@ -44,9 +43,6 @@ unit-tests/array_block_t: unit-tests/array_block_t.o $(OBJECTS)
unit-tests/bitset_t: unit-tests/bitset_t.o $(OBJECTS) unit-tests/bitset_t: unit-tests/bitset_t.o $(OBJECTS)
g++ $(CXXFLAGS) $(INCLUDES) -o $@ $+ $(LIBS) $(LIBEXPAT) g++ $(CXXFLAGS) $(INCLUDES) -o $@ $+ $(LIBS) $(LIBEXPAT)
unit-tests/buffer_t: unit-tests/buffer_t.o $(OBJECTS)
g++ $(CXXFLAGS) $(INCLUDES) -o $@ $+ $(LIBS) $(LIBEXPAT)
unit-tests/block_t: unit-tests/block_t.o $(OBJECTS) unit-tests/block_t: unit-tests/block_t.o $(OBJECTS)
g++ $(CXXFLAGS) $(INCLUDES) -o $@ $+ $(LIBS) $(LIBEXPAT) g++ $(CXXFLAGS) $(INCLUDES) -o $@ $+ $(LIBS) $(LIBEXPAT)
@ -91,10 +87,21 @@ lib/gmock.a:
g++ $(GMOCK_INCLUDES) -I$(GMOCK_DIR) -c $(GMOCK_DIR)/src/gmock-all.cc g++ $(GMOCK_INCLUDES) -I$(GMOCK_DIR) -c $(GMOCK_DIR)/src/gmock-all.cc
ar -rv lib/libgmock.a gtest-all.o gmock-all.o ar -rv lib/libgmock.a gtest-all.o gmock-all.o
unit-tests/gmock_t.o: unit-tests/gmock_t.cc MOCK_SOURCE=\
g++ -c $(INCLUDES) $(GMOCK_INCLUDES) $(CXXFLAGS) -o $@ $< unit-tests/gmock_main.cc \
\
unit-tests/buffer_t.cc
unit-tests/gmock_t: unit-tests/gmock_t.o lib/gmock.a # .gmo files are plain .o files, only they've been built with gmock
g++ $(CXXFLAGS) -o $@ $< $(LIBS) $(GMOCK_LIBS) # include paths.
.SUFFIXES: .gmo
%.gmo: %.cc
$(CXX) -c $(INCLUDES) $(GMOCK_INCLUDES) $(CXXFLAGS) -o $@ $<
MOCK_OBJECTS=$(subst .cc,.gmo,$(MOCK_SOURCE))
unit-tests/gmock_tests: $(MOCK_OBJECTS) lib/gmock.a
g++ $(CXXFLAGS) -o $@ $(MOCK_OBJECTS) $(LIBS) $(GMOCK_LIBS)
all: $(TEST_PROGRAMS) all: $(TEST_PROGRAMS)

View File

@ -19,12 +19,13 @@
// Set to one to add compile time checks. // Set to one to add compile time checks.
#define COMPILE_TIME_ERROR 0 #define COMPILE_TIME_ERROR 0
#include "gmock/gmock.h"
#include "persistent-data/buffer.h" #include "persistent-data/buffer.h"
#define BOOST_TEST_MODULE BufferTests
#include <boost/test/included/unit_test.hpp>
using namespace persistent_data; using namespace persistent_data;
using namespace testing;
// FIXME: get rid of these comments, the tests should be self explanatory
// - Allocate several on the heap, check they have the requested // - Allocate several on the heap, check they have the requested
// alignment. Try for various Alignments. If memalign has // alignment. Try for various Alignments. If memalign has
@ -48,12 +49,92 @@ namespace {
create_buffer(void) { create_buffer(void) {
return typename buffer<Size, Alignment>::ptr(new buffer<Size, Alignment>()); return typename buffer<Size, Alignment>::ptr(new buffer<Size, Alignment>());
} }
template <typename buffer>
void assert_aligned(buffer const &b) {
ASSERT_THAT((unsigned long) b.raw() & (buffer::ALIGNMENT - 1), Eq(0u));
}
} }
//---------------------------------------------------------------- //----------------------------------------------------------------
TEST(BufferTest, buffer_8_a_8_raw_access)
{
uint32_t const sz = 8, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
unsigned char *p = b->raw();
// unsigned char const *pc = b->raw();
p[0] = 0;
ASSERT_THAT(p[0], Eq(0));
p[0] = 4;
ASSERT_THAT(p[0], Eq(4));
}
TEST(BufferTest, buffer_8_a_8_access)
{
uint32_t const sz = 8, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
(*b)[0] = 0;
ASSERT_THAT((*b)[0], Eq(0));
}
TEST(BufferTest, buffer_8_a_8_oob)
{
uint32_t const sz = 8, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
ASSERT_THROW((*b)[8], std::range_error);
}
TEST(BufferTest, buffer_128_a_2_succeeds)
{
uint32_t const sz = 128, align = 2;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
ASSERT_TRUE(b);
}
TEST(BufferTest, buffer_128_a_4_succeeds)
{
uint32_t const sz = 128, align = 4;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
ASSERT_TRUE(b);
}
// Varying buffer size, alignment 8
TEST(BufferTest, obeys_alignment)
{
assert_aligned(*create_buffer<8, 8>());
assert_aligned(*create_buffer<16, 8>());
assert_aligned(*create_buffer<32, 8>());
assert_aligned(*create_buffer<64, 8>());
assert_aligned(*create_buffer<128, 8>());
assert_aligned(*create_buffer<256, 8>());
assert_aligned(*create_buffer<512, 8>());
assert_aligned(*create_buffer<1024, 8>());
assert_aligned(*create_buffer<2048, 8>());
assert_aligned(*create_buffer<4096, 8>());
assert_aligned(*create_buffer<8192, 8>());
assert_aligned(*create_buffer<8, 16>());
assert_aligned(*create_buffer<16, 16>());
assert_aligned(*create_buffer<32, 16>());
assert_aligned(*create_buffer<64, 16>());
assert_aligned(*create_buffer<128, 16>());
assert_aligned(*create_buffer<256, 16>());
assert_aligned(*create_buffer<512, 16>());
assert_aligned(*create_buffer<1024, 16>());
assert_aligned(*create_buffer<2048, 16>());
assert_aligned(*create_buffer<4096, 16>());
assert_aligned(*create_buffer<8192, 16>());
}
#if 0
#if COMPILE_TIME_ERROR #if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_copy_fails) TEST(BufferTest, 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>();
@ -63,21 +144,8 @@ BOOST_AUTO_TEST_CASE(buffer_copy_fails)
} }
#endif #endif
BOOST_AUTO_TEST_CASE(buffer_8_a_8_raw_access)
{
uint32_t const sz = 8, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
unsigned char *p = b->raw();
// unsigned char const *pc = b->raw();
p[0] = 0;
BOOST_CHECK(p[0] == 0);
p[0] = 4;
BOOST_CHECK(p[0] == 4);
}
#if COMPILE_TIME_ERROR #if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_8_a_8_raw_const_access) TEST(BufferTest, buffer_8_a_8_raw_const_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>();
@ -87,17 +155,8 @@ BOOST_AUTO_TEST_CASE(buffer_8_a_8_raw_const_access)
} }
#endif #endif
BOOST_AUTO_TEST_CASE(buffer_8_a_8_access)
{
uint32_t const sz = 8, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
(*b)[0] = 0;
BOOST_CHECK_EQUAL((*b)[0], 0);
}
#if COMPILE_TIME_ERROR #if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_8_a_8_const_access) TEST(BufferTest, buffer_8_a_8_const_access)
{ {
uint32_t const sz = 8, align = 8; uint32_t const sz = 8, align = 8;
buffer<sz, align>::const_ptr b = create_buffer<sz, align>(); buffer<sz, align>::const_ptr b = create_buffer<sz, align>();
@ -106,36 +165,21 @@ 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 #if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_128_a_1_fails) TEST(BufferTest, buffer_128_a_1_fails)
{ {
uint32_t const sz = 128, align = 1; uint32_t const sz = 128, align = 1;
buffer<sz, align>::ptr b = create_buffer<sz, align>(); buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK(!b); BOOST_CHECK(!b);
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 1); ASSERT_THAT((unsigned long) b->raw() & (align - 1), 1);
} }
#endif #endif
BOOST_AUTO_TEST_CASE(buffer_128_a_2_succeeds)
{
uint32_t const sz = 128, align = 2;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK(b);
}
#if COMPILE_TIME_ERROR #if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_128_a_3_fails) TEST(BufferTest, buffer_128_a_3_fails)
{ {
uint32_t const sz = 128, align = 3; uint32_t const sz = 128, align = 3;
buffer<sz, align>::ptr b = create_buffer<sz, align>(); buffer<sz, align>::ptr b = create_buffer<sz, align>();
@ -144,16 +188,8 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_3_fails)
} }
#endif #endif
BOOST_AUTO_TEST_CASE(buffer_128_a_4_succeeds)
{
uint32_t const sz = 128, align = 4;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK(b);
}
#if COMPILE_TIME_ERROR #if COMPILE_TIME_ERROR
BOOST_AUTO_TEST_CASE(buffer_128_a_5_fails) TEST(BufferTest, buffer_128_a_5_fails)
{ {
uint32_t const sz = 128, align = 5; uint32_t const sz = 128, align = 5;
buffer<sz, align>::ptr b = create_buffer<sz, align>(); buffer<sz, align>::ptr b = create_buffer<sz, align>();
@ -161,7 +197,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_5_fails)
BOOST_CHECK(!b); BOOST_CHECK(!b);
} }
BOOST_AUTO_TEST_CASE(buffer_128_a_6_fails) TEST(BufferTest, buffer_128_a_6_fails)
{ {
uint32_t const sz = 128, align = 6; uint32_t const sz = 128, align = 6;
buffer<sz, align>::ptr b = create_buffer<sz, align>(); buffer<sz, align>::ptr b = create_buffer<sz, align>();
@ -169,7 +205,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_6_fails)
BOOST_CHECK(!b); BOOST_CHECK(!b);
} }
BOOST_AUTO_TEST_CASE(buffer_128_a_7_fails) TEST(BufferTest, buffer_128_a_7_fails)
{ {
uint32_t const sz = 128, align = 7; uint32_t const sz = 128, align = 7;
buffer<sz, align>::ptr b = create_buffer<sz, align>(); buffer<sz, align>::ptr b = create_buffer<sz, align>();
@ -178,185 +214,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_7_fails)
} }
#endif #endif
// Varying buffer size, alignment 8
BOOST_AUTO_TEST_CASE(buffer_8_a_8)
{
uint32_t const sz = 8, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_16_a_8)
{
uint32_t const sz = 16, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_32_a_8)
{
uint32_t const sz = 32, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_64_a_8)
{
uint32_t const sz = 64, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_128_a_8)
{
uint32_t const sz = 128, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_256_a_8)
{
uint32_t const sz = 256, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_512_a_8)
{
uint32_t const sz = 512, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_1024_a_8)
{
uint32_t const sz = 1024, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_2048_a_8)
{
uint32_t const sz = 2048, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_4096_a_8)
{
uint32_t const sz = 4096, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_8192_a_8)
{
uint32_t const sz = 8192, align = 8;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
// Varying buffer size, alignment 16
BOOST_AUTO_TEST_CASE(buffer_8_a_16)
{
uint32_t const sz = 8, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_16_a_16)
{
uint32_t const sz = 16, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_32_a_16)
{
uint32_t const sz = 32, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_64_a_16)
{
uint32_t const sz = 64, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_128_a_16)
{
uint32_t const sz = 128, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_256_a_16)
{
uint32_t const sz = 256, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_512_a_16)
{
uint32_t const sz = 512, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_1024_a_16)
{
uint32_t const sz = 1024, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_2048_a_16)
{
uint32_t const sz = 2048, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_4096_a_16)
{
uint32_t const sz = 4096, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
BOOST_AUTO_TEST_CASE(buffer_8192_a_16)
{
uint32_t const sz = 8192, align = 16;
buffer<sz, align>::ptr b = create_buffer<sz, align>();
BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0);
}
#undef COMPILE_TIME_ERROR #undef COMPILE_TIME_ERROR
#endif
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -1,23 +1,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace {
class MyClass {
};
}
//----------------------------------------------------------------
TEST(MyClass, can_construct)
{
MyClass c;
}
//----------------------------------------------------------------
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);