From c044c2c7294b5481fb0120f8849364865a3276ed Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Fri, 22 Mar 2013 12:04:38 +0000 Subject: [PATCH] Convert buffer_t to gmock. --- features/cache_check.feature | 12 +- get-gmock.sh | 2 - persistent-data/buffer.h | 1 + unit-tests/Makefile.in | 23 +- unit-tests/buffer_t.cc | 330 +++++++---------------- unit-tests/{gmock_t.cc => gmock_main.cc} | 16 -- 6 files changed, 121 insertions(+), 263 deletions(-) rename unit-tests/{gmock_t.cc => gmock_main.cc} (51%) diff --git a/features/cache_check.feature b/features/cache_check.feature index a2f4135..773aaa3 100644 --- a/features/cache_check.feature +++ b/features/cache_check.feature @@ -70,7 +70,17 @@ Feature: cache_check And block 1 is zeroed When I run `cache_check input` + And the stderr should contain: """ input: No superblock found - """ \ No newline at end of file + """ + + Scenario: A valid metadata area passes + Given metadata containing: + """ + """ + + When I run cache_check + + Then it should pass \ No newline at end of file diff --git a/get-gmock.sh b/get-gmock.sh index eda6bcb..5335350 100755 --- a/get-gmock.sh +++ b/get-gmock.sh @@ -4,5 +4,3 @@ wget https://googlemock.googlecode.com/files/gmock-1.6.0.zip unzip gmock-1.6.0.zip cd gmock-1.6.0 ./configure -make -make check \ No newline at end of file diff --git a/persistent-data/buffer.h b/persistent-data/buffer.h index 8ddda70..a8f755b 100644 --- a/persistent-data/buffer.h +++ b/persistent-data/buffer.h @@ -47,6 +47,7 @@ namespace persistent_data { BOOST_STATIC_ASSERT_MSG((Alignment > 1) & !(Alignment & (Alignment - 1)), "Alignment must be a power of two."); + static uint32_t const ALIGNMENT = Alignment; typedef boost::shared_ptr ptr; typedef boost::shared_ptr const_ptr; diff --git a/unit-tests/Makefile.in b/unit-tests/Makefile.in index e426d68..05df04b 100644 --- a/unit-tests/Makefile.in +++ b/unit-tests/Makefile.in @@ -20,7 +20,6 @@ TEST_SOURCE=\ unit-tests/array_block_t.cc \ unit-tests/array_t.cc \ unit-tests/bitset_t.cc \ - unit-tests/buffer_t.cc \ unit-tests/cache_t.cc \ unit-tests/block_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) 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) 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 ar -rv lib/libgmock.a gtest-all.o gmock-all.o -unit-tests/gmock_t.o: unit-tests/gmock_t.cc - g++ -c $(INCLUDES) $(GMOCK_INCLUDES) $(CXXFLAGS) -o $@ $< +MOCK_SOURCE=\ + unit-tests/gmock_main.cc \ + \ + unit-tests/buffer_t.cc -unit-tests/gmock_t: unit-tests/gmock_t.o lib/gmock.a - g++ $(CXXFLAGS) -o $@ $< $(LIBS) $(GMOCK_LIBS) +# .gmo files are plain .o files, only they've been built with gmock +# 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) diff --git a/unit-tests/buffer_t.cc b/unit-tests/buffer_t.cc index 2d87910..41ce794 100644 --- a/unit-tests/buffer_t.cc +++ b/unit-tests/buffer_t.cc @@ -19,12 +19,13 @@ // Set to one to add compile time checks. #define COMPILE_TIME_ERROR 0 +#include "gmock/gmock.h" #include "persistent-data/buffer.h" -#define BOOST_TEST_MODULE BufferTests -#include - 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 // alignment. Try for various Alignments. If memalign has @@ -48,12 +49,92 @@ namespace { create_buffer(void) { return typename buffer::ptr(new buffer()); } + + template + 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::ptr b = create_buffer(); + 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::ptr b = create_buffer(); + + (*b)[0] = 0; + ASSERT_THAT((*b)[0], Eq(0)); +} + +TEST(BufferTest, buffer_8_a_8_oob) +{ + uint32_t const sz = 8, align = 8; + buffer::ptr b = create_buffer(); + + ASSERT_THROW((*b)[8], std::range_error); +} + +TEST(BufferTest, buffer_128_a_2_succeeds) +{ + uint32_t const sz = 128, align = 2; + buffer::ptr b = create_buffer(); + + ASSERT_TRUE(b); +} + +TEST(BufferTest, buffer_128_a_4_succeeds) +{ + uint32_t const sz = 128, align = 4; + buffer::ptr b = create_buffer(); + + 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 -BOOST_AUTO_TEST_CASE(buffer_copy_fails) +TEST(BufferTest, buffer_copy_fails) { uint32_t const sz = 8, align = 8; buffer::ptr b1 = create_buffer(); @@ -63,21 +144,8 @@ BOOST_AUTO_TEST_CASE(buffer_copy_fails) } #endif -BOOST_AUTO_TEST_CASE(buffer_8_a_8_raw_access) -{ - uint32_t const sz = 8, align = 8; - buffer::ptr b = create_buffer(); - 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 -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; buffer::ptr b = create_buffer(); @@ -87,17 +155,8 @@ BOOST_AUTO_TEST_CASE(buffer_8_a_8_raw_const_access) } #endif -BOOST_AUTO_TEST_CASE(buffer_8_a_8_access) -{ - uint32_t const sz = 8, align = 8; - buffer::ptr b = create_buffer(); - - (*b)[0] = 0; - BOOST_CHECK_EQUAL((*b)[0], 0); -} - #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; buffer::const_ptr b = create_buffer(); @@ -106,36 +165,21 @@ BOOST_AUTO_TEST_CASE(buffer_8_a_8_const_access) } #endif -BOOST_AUTO_TEST_CASE(buffer_8_a_8_oob) -{ - uint32_t const sz = 8, align = 8; - buffer::ptr b = create_buffer(); - - BOOST_CHECK_THROW((*b)[8], std::range_error); -} // 8 byte buffer size, varying alignment from 1 - 7 #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; buffer::ptr b = create_buffer(); BOOST_CHECK(!b); - BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 1); + ASSERT_THAT((unsigned long) b->raw() & (align - 1), 1); } #endif -BOOST_AUTO_TEST_CASE(buffer_128_a_2_succeeds) -{ - uint32_t const sz = 128, align = 2; - buffer::ptr b = create_buffer(); - - BOOST_CHECK(b); -} - #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; buffer::ptr b = create_buffer(); @@ -144,16 +188,8 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_3_fails) } #endif -BOOST_AUTO_TEST_CASE(buffer_128_a_4_succeeds) -{ - uint32_t const sz = 128, align = 4; - buffer::ptr b = create_buffer(); - - BOOST_CHECK(b); -} - #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; buffer::ptr b = create_buffer(); @@ -161,7 +197,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_5_fails) 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; buffer::ptr b = create_buffer(); @@ -169,7 +205,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_6_fails) 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; buffer::ptr b = create_buffer(); @@ -178,185 +214,7 @@ BOOST_AUTO_TEST_CASE(buffer_128_a_7_fails) } #endif -// Varying buffer size, alignment 8 -BOOST_AUTO_TEST_CASE(buffer_8_a_8) -{ - uint32_t const sz = 8, align = 8; - buffer::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - 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::ptr b = create_buffer(); - - BOOST_CHECK_EQUAL((unsigned long) b->raw() & (align - 1), 0); -} #undef COMPILE_TIME_ERROR - +#endif //---------------------------------------------------------------- diff --git a/unit-tests/gmock_t.cc b/unit-tests/gmock_main.cc similarity index 51% rename from unit-tests/gmock_t.cc rename to unit-tests/gmock_main.cc index dc83182..fe5182b 100644 --- a/unit-tests/gmock_t.cc +++ b/unit-tests/gmock_main.cc @@ -1,23 +1,7 @@ -#include "gmock/gmock.h" #include "gtest/gtest.h" //---------------------------------------------------------------- -namespace { - class MyClass { - - }; -} - -//---------------------------------------------------------------- - -TEST(MyClass, can_construct) -{ - MyClass c; -} - -//---------------------------------------------------------------- - int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv);