From 0a70c1726876e467835a3a833b5dda57a99d3158 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Fri, 22 Mar 2013 14:36:31 +0000 Subject: [PATCH] Convert bitset_t to gmock --- Makefile.in | 2 +- persistent-data/data-structures/array.h | 5 --- persistent-data/data-structures/bitset.h | 4 +- persistent-data/endian_utils.cc | 10 ++--- persistent-data/space-maps/disk.cc | 10 ++--- persistent-data/space-maps/disk_structures.h | 40 ++++++++--------- unit-tests/Makefile.in | 4 +- unit-tests/array_t.cc | 6 +-- unit-tests/bitset_t.cc | 45 +++++++++----------- 9 files changed, 58 insertions(+), 68 deletions(-) diff --git a/Makefile.in b/Makefile.in index 4d1733e..03b495b 100644 --- a/Makefile.in +++ b/Makefile.in @@ -174,7 +174,7 @@ cache_check: $(CACHE_CHECK_OBJECTS) cache/check.o .PHONY: clean distclean clean: - $(RM) *.o unit-tests/*.o *.d unit-tests/*.d $(TEST_PROGRAMS) $(PROGRAMS) $(OBJECTS) + $(RM) *.o unit-tests/*.o *.d unit-tests/*.d unit-tests/*.gmo $(TEST_PROGRAMS) $(PROGRAMS) $(OBJECTS) $(GMOCK_OBJECTS) lib/*.a distclean: clean $(RM) config.cache config.log config.status configure.h version.h Makefile unit-tests/Makefile diff --git a/persistent-data/data-structures/array.h b/persistent-data/data-structures/array.h index af8808b..dad961d 100644 --- a/persistent-data/data-structures/array.h +++ b/persistent-data/data-structures/array.h @@ -64,11 +64,6 @@ namespace persistent_data { unsigned nr_entries_in_last_block; unsigned nr_total_blocks; }; - - unsigned calc_max_entries(size_t value_size, size_t block_size) - { - return (block_size - sizeof(struct array_block_disk)) / value_size; - } } template diff --git a/persistent-data/data-structures/bitset.h b/persistent-data/data-structures/bitset.h index 9caada6..d0fa572 100644 --- a/persistent-data/data-structures/bitset.h +++ b/persistent-data/data-structures/bitset.h @@ -27,7 +27,7 @@ namespace persistent_data { namespace bitset_detail { struct bitset_traits { - typedef base::__le64 disk_type; + typedef base::le64 disk_type; typedef uint64_t value_type; typedef no_op_ref_counter ref_counter; @@ -36,7 +36,7 @@ namespace persistent_data { } static void pack(value_type const &value, disk_type &disk) { - disk = base::to_disk(value); + disk = base::to_disk(value); } }; } diff --git a/persistent-data/endian_utils.cc b/persistent-data/endian_utils.cc index ac557c5..169b721 100644 --- a/persistent-data/endian_utils.cc +++ b/persistent-data/endian_utils.cc @@ -25,7 +25,7 @@ using namespace base; bool base::test_bit_le(void const *bits, unsigned b) { - __le64 const *w = reinterpret_cast<__le64 const *>(bits); + le64 const *w = reinterpret_cast(bits); w += b / 64; uint64_t v = to_cpu(*w); @@ -37,20 +37,20 @@ base::test_bit_le(void const *bits, unsigned b) void base::set_bit_le(void *bits, unsigned b) { - __le64 *w = reinterpret_cast<__le64 *>(bits); + le64 *w = reinterpret_cast(bits); w += b / 64; uint64_t v = to_cpu(*w); uint64_t mask = 1; mask = mask << (b % 64); v |= mask; - *w = to_disk<__le64>(v); + *w = to_disk(v); } void base::clear_bit_le(void *bits, unsigned b) { - __le64 *w = reinterpret_cast<__le64 *>(bits); + le64 *w = reinterpret_cast(bits); w += b / 64; uint64_t v = to_cpu(*w); @@ -58,7 +58,7 @@ base::clear_bit_le(void *bits, unsigned b) mask = mask << (b % 64); mask = ~mask; v &= mask; - *w = to_disk<__le64>(v); + *w = to_disk(v); } //---------------------------------------------------------------- diff --git a/persistent-data/space-maps/disk.cc b/persistent-data/space-maps/disk.cc index 76f52e8..bf831fd 100644 --- a/persistent-data/space-maps/disk.cc +++ b/persistent-data/space-maps/disk.cc @@ -51,11 +51,11 @@ namespace { virtual void prepare(buffer<> &b, block_address location) const { bitmap_header *data = reinterpret_cast(&b); - data->blocknr = to_disk(location); + data->blocknr = to_disk(location); crc32c sum(BITMAP_CSUM_XOR); sum.append(&data->not_used, MD_BLOCK_SIZE - sizeof(uint32_t)); - data->csum = to_disk(sum.get_sum()); + data->csum = to_disk(sum.get_sum()); } }; @@ -83,11 +83,11 @@ namespace { virtual void prepare(buffer<> &b, block_address location) const { metadata_index *mi = reinterpret_cast(&b); - mi->blocknr_ = to_disk(location); + mi->blocknr_ = to_disk(location); crc32c sum(INDEX_CSUM_XOR); sum.append(&mi->padding_, MD_BLOCK_SIZE - sizeof(uint32_t)); - mi->csum_ = to_disk(sum.get_sum()); + mi->csum_ = to_disk(sum.get_sum()); } }; @@ -194,7 +194,7 @@ namespace { }; struct ref_count_traits { - typedef __le32 disk_type; + typedef le32 disk_type; typedef uint32_t value_type; typedef no_op_ref_counter ref_counter; diff --git a/persistent-data/space-maps/disk_structures.h b/persistent-data/space-maps/disk_structures.h index 475e96e..a92f490 100644 --- a/persistent-data/space-maps/disk_structures.h +++ b/persistent-data/space-maps/disk_structures.h @@ -31,9 +31,9 @@ namespace persistent_data { namespace sm_disk_detail { struct index_entry_disk { - __le64 blocknr_; - __le32 nr_free_; - __le32 none_free_before_; + le64 blocknr_; + le32 nr_free_; + le32 none_free_before_; } __attribute__ ((packed)); struct index_entry { @@ -54,9 +54,9 @@ namespace persistent_data { } static void pack(value_type const &value, disk_type &disk) { - disk.blocknr_ = to_disk<__le64>(value.blocknr_); - disk.nr_free_ = to_disk<__le32>(value.nr_free_); - disk.none_free_before_ = to_disk<__le32>(value.none_free_before_); + disk.blocknr_ = to_disk(value.blocknr_); + disk.nr_free_ = to_disk(value.nr_free_); + disk.none_free_before_ = to_disk(value.none_free_before_); } }; @@ -64,18 +64,18 @@ namespace persistent_data { unsigned const ENTRIES_PER_BYTE = 4; struct metadata_index { - __le32 csum_; - __le32 padding_; - __le64 blocknr_; + le32 csum_; + le32 padding_; + le64 blocknr_; struct index_entry_disk index[MAX_METADATA_BITMAPS]; } __attribute__ ((packed)); struct sm_root_disk { - __le64 nr_blocks_; - __le64 nr_allocated_; - __le64 bitmap_root_; - __le64 ref_count_root_; + le64 nr_blocks_; + le64 nr_allocated_; + le64 bitmap_root_; + le64 ref_count_root_; } __attribute__ ((packed)); struct sm_root { @@ -98,17 +98,17 @@ namespace persistent_data { } static void pack(value_type const &value, disk_type &disk) { - disk.nr_blocks_ = to_disk<__le64>(value.nr_blocks_); - disk.nr_allocated_ = to_disk<__le64>(value.nr_allocated_); - disk.bitmap_root_ = to_disk<__le64>(value.bitmap_root_); - disk.ref_count_root_ = to_disk<__le64>(value.ref_count_root_); + disk.nr_blocks_ = to_disk(value.nr_blocks_); + disk.nr_allocated_ = to_disk(value.nr_allocated_); + disk.bitmap_root_ = to_disk(value.bitmap_root_); + disk.ref_count_root_ = to_disk(value.ref_count_root_); } }; struct bitmap_header { - __le32 csum; - __le32 not_used; - __le64 blocknr; + le32 csum; + le32 not_used; + le64 blocknr; } __attribute__ ((packed)); } } diff --git a/unit-tests/Makefile.in b/unit-tests/Makefile.in index 32921ac..02e8b0e 100644 --- a/unit-tests/Makefile.in +++ b/unit-tests/Makefile.in @@ -34,9 +34,6 @@ unit-test: $(TEST_PROGRAMS) .PHONY: unit-test -unit-tests/bitset_t: unit-tests/bitset_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) @@ -92,6 +89,7 @@ MOCK_SOURCE=\ \ unit-tests/array_block_t.cc \ unit-tests/array_t.cc \ + unit-tests/bitset_t.cc \ unit-tests/buffer_t.cc # .gmo files are plain .o files, only they've been built with gmock diff --git a/unit-tests/array_t.cc b/unit-tests/array_t.cc index d6e1f72..f6d21f4 100644 --- a/unit-tests/array_t.cc +++ b/unit-tests/array_t.cc @@ -77,7 +77,7 @@ TEST(ArrayTests, get_elements) array::ptr tree = create_array(COUNT, 123); for (unsigned i = 0; i < COUNT; i++) - ASSERT_THAT(tree->get(i), Eq(123)); + ASSERT_THAT(tree->get(i), Eq(123u)); ASSERT_THROW(tree->get(COUNT), runtime_error); } @@ -91,7 +91,7 @@ TEST(ArrayTests, set_elements) tree->set(i, 124); for (unsigned i = 0; i < COUNT; i++) - ASSERT_THAT(tree->get(i), Eq(124)); + ASSERT_THAT(tree->get(i), Eq(124u)); ASSERT_THROW(tree->get(COUNT), runtime_error); } @@ -154,7 +154,7 @@ TEST(ArrayTests, reopen_array) typename array64::ptr a = open_array(root, COUNT); for (unsigned i = 0; i < COUNT; i++) - ASSERT_THAT(a->get(i), Eq(i % 7 ? 123: 234)); + ASSERT_THAT(a->get(i), Eq(i % 7 ? 123u : 234u)); } } diff --git a/unit-tests/bitset_t.cc b/unit-tests/bitset_t.cc index ca94b53..e2dc4cd 100644 --- a/unit-tests/bitset_t.cc +++ b/unit-tests/bitset_t.cc @@ -16,18 +16,16 @@ // with thin-provisioning-tools. If not, see // . +#include "gmock/gmock.h" #include "persistent-data/transaction_manager.h" #include "persistent-data/space-maps/core.h" #include "persistent-data/data-structures/bitset.h" #include -#define BOOST_TEST_MODULE ArrayTests -#include - using namespace std; -using namespace boost; using namespace persistent_data; +using namespace testing; //---------------------------------------------------------------- @@ -55,13 +53,13 @@ namespace { //---------------------------------------------------------------- -BOOST_AUTO_TEST_CASE(create_empty_bitset) +TEST(BitsetTests, create_empty_bitset) { bitset::ptr bs = create_bitset(); - BOOST_CHECK_THROW(bs->get(0), runtime_error); + ASSERT_THROW(bs->get(0), runtime_error); } -BOOST_AUTO_TEST_CASE(grow_default_false) +TEST(BitsetTests, grow_default_false) { unsigned const COUNT = 100000; @@ -69,10 +67,10 @@ BOOST_AUTO_TEST_CASE(grow_default_false) bs->grow(COUNT, false); for (unsigned i = 0; i < COUNT; i++) - BOOST_CHECK_EQUAL(bs->get(i), false); + ASSERT_FALSE(bs->get(i)); } -BOOST_AUTO_TEST_CASE(grow_default_true) +TEST(BitsetTests, grow_default_true) { unsigned const COUNT = 100000; @@ -80,19 +78,19 @@ BOOST_AUTO_TEST_CASE(grow_default_true) bs->grow(COUNT, true); for (unsigned i = 0; i < COUNT; i++) - BOOST_CHECK_EQUAL(bs->get(i), true); + ASSERT_TRUE(bs->get(i)); } -BOOST_AUTO_TEST_CASE(grow_throws_if_actualy_asked_to_shrink) +TEST(BitsetTests, grow_throws_if_actualy_asked_to_shrink) { unsigned const COUNT = 100000; bitset::ptr bs = create_bitset(); bs->grow(COUNT, false); - BOOST_CHECK_THROW(bs->grow(COUNT / 2, false), runtime_error); + ASSERT_THROW(bs->grow(COUNT / 2, false), runtime_error); } -BOOST_AUTO_TEST_CASE(multiple_grow_calls) +TEST(BitsetTests, multiple_grow_calls) { unsigned const COUNT = 100000; unsigned const STEP = 37; @@ -109,7 +107,7 @@ BOOST_AUTO_TEST_CASE(multiple_grow_calls) bs->grow(chunks[i], default_value); for (unsigned j = chunks[i - 1]; j < chunks[i]; j++) - BOOST_CHECK_EQUAL(bs->get(j), default_value); + ASSERT_THAT(bs->get(j), Eq(default_value)); default_value = !default_value; } @@ -117,23 +115,23 @@ BOOST_AUTO_TEST_CASE(multiple_grow_calls) default_value = true; for (unsigned i = 1; i < chunks.size(); i++) { for (unsigned j = chunks[i - 1]; j < chunks[i]; j++) - BOOST_CHECK_EQUAL(bs->get(j), default_value); + ASSERT_THAT(bs->get(j), Eq(default_value)); default_value = !default_value; } } -BOOST_AUTO_TEST_CASE(set_out_of_bounds_throws) +TEST(BitsetTests, set_out_of_bounds_throws) { unsigned const COUNT = 100000; bitset::ptr bs = create_bitset(); - BOOST_CHECK_THROW(bs->set(0, true), runtime_error); + ASSERT_THROW(bs->set(0, true), runtime_error); bs->grow(COUNT, true); - BOOST_CHECK_THROW(bs->set(COUNT, true), runtime_error); + ASSERT_THROW(bs->set(COUNT, true), runtime_error); } -BOOST_AUTO_TEST_CASE(set_works) +TEST(BitsetTests, set_works) { unsigned const COUNT = 100000; bitset::ptr bs = create_bitset(); @@ -142,12 +140,11 @@ BOOST_AUTO_TEST_CASE(set_works) for (unsigned i = 0; i < COUNT; i += 7) bs->set(i, false); - for (unsigned i = 0; i < COUNT; i++) { - BOOST_CHECK_EQUAL(bs->get(i), i % 7 ? true : false); - } + for (unsigned i = 0; i < COUNT; i++) + ASSERT_THAT(bs->get(i), Eq(i % 7 ? true : false)); } -BOOST_AUTO_TEST_CASE(reopen_works) +TEST(BitsetTests, reopen_works) { unsigned const COUNT = 100000; block_address root; @@ -165,7 +162,7 @@ BOOST_AUTO_TEST_CASE(reopen_works) { bitset::ptr bs = open_bitset(root, COUNT); for (unsigned i = 0; i < COUNT; i++) - BOOST_CHECK_EQUAL(bs->get(i), i % 7 ? true : false); + ASSERT_THAT(bs->get(i), Eq(i % 7 ? true : false)); } }