From f9bf18adf685782bc09d6768f388f2f907aa1ed8 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Fri, 22 Mar 2013 13:42:20 +0000 Subject: [PATCH] Move array_t to gmock --- Makefile.in | 46 ++++++++++++------- persistent-data/data-structures/array.h | 8 ++-- persistent-data/data-structures/array_block.h | 18 ++++---- persistent-data/data-structures/btree.h | 20 ++++---- persistent-data/data-structures/btree.tcc | 14 +++--- persistent-data/endian_utils.h | 30 ++++++------ unit-tests/Makefile.in | 12 ++--- unit-tests/array_t.cc | 41 ++++++++--------- 8 files changed, 98 insertions(+), 91 deletions(-) diff --git a/Makefile.in b/Makefile.in index 7c0e849..4d1733e 100644 --- a/Makefile.in +++ b/Makefile.in @@ -21,8 +21,6 @@ V=@ PROGRAMS=\ cache_check \ - cache_dump \ - cache_restore \ \ thin_debug \ thin_check \ @@ -31,7 +29,7 @@ PROGRAMS=\ all: $(PROGRAMS) -SOURCE=\ +PDATA_SOURCE=\ persistent-data/checksum.cc \ persistent-data/endian_utils.cc \ persistent-data/error_set.cc \ @@ -42,7 +40,11 @@ SOURCE=\ persistent-data/space_map.cc \ persistent-data/space-maps/disk.cc \ persistent-data/space-maps/recursive.cc \ - persistent-data/space-maps/careful_alloc.cc \ + persistent-data/space-maps/careful_alloc.cc +PDATA_OBJECTS=$(subst .cc,.o,$(PDATA_SOURCE)) + +SOURCE=\ + $(PDATA_SOURCE) \ \ cache/metadata_disk_structures.cc \ \ @@ -57,8 +59,6 @@ SOURCE=\ PROGRAM_SOURCE=\ cache/check.cc \ - cache/dump.cc \ - cache/restore.cc \ \ thin-provisioning/thin_check.cc \ thin-provisioning/thin_dump.cc \ @@ -93,15 +93,20 @@ test-programs: $(TEST_PROGRAMS) .SUFFIXES: .d %.d: %.cc - @echo " [dependencies] $<" - $(V) $(CXX) -MM -MT $(subst .cc,.o,$<) $(INCLUDES) $(CXXFLAGS) $< > $@.$$$$;\ - $(V) sed 's,\([^ :]*\)\.o[ :]*,\1.o $@ : Makefile ,g' < $@.$$$$ > $@; \ - $(V) $(RM) $@.$$$$ + @echo " [dependencies] $@" + $(CXX) -MM -MT $(subst .cc,.o,$<) $(INCLUDES) $(CXXFLAGS) $< > $@.$$$$;\ + sed 's,\([^ :]*\)\.o[ :]*,\1.o $@ : Makefile ,g' < $@.$$$$ > $@; \ + $(RM) $@.$$$$ %.o: %.cc - @echo " [CXX] $<" + @echo " [CXX] $@" $(V) $(CXX) -c $(INCLUDES) $(CXXFLAGS) -o $@ $< +#---------------------------------------------------------------- +# libpdata +lib/libpdata.a: $(PDATA_OBJECTS) + ar -rv $@ $(PDATA_OBJECTS) + #---------------------------------------------------------------- # Thin provisioning tools @@ -129,16 +134,20 @@ THIN_RESTORE_OBJECTS=$(subst .cc,.o,$(THIN_RESTORE_SOURCE)) THIN_CHECK_OBJECTS=$(subst .cc,.o,$(THIN_CHECK_SOURCE)) thin_debug: $(THIN_DEBUG_OBJECTS) thin-provisioning/thin_debug.o - $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS) $(LIBEXPAT) + @echo " [LD] $@" + $(V) $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS) $(LIBEXPAT) thin_dump: $(THIN_DUMP_OBJECTS) thin-provisioning/thin_dump.o - $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS) $(LIBEXPAT) + @echo " [LD] $@" + $(V) $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS) $(LIBEXPAT) thin_restore: $(THIN_RESTORE_OBJECTS) thin-provisioning/thin_restore.o - $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS) $(LIBEXPAT) + @echo " [LD] $@" + $(V) $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS) $(LIBEXPAT) thin_check: $(THIN_CHECK_OBJECTS) thin-provisioning/thin_check.o - $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS) + @echo " [LD] $@" + $(V) $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS) #---------------------------------------------------------------- # Cache tools @@ -159,13 +168,16 @@ CACHE_CHECK_SOURCE=\ CACHE_CHECK_OBJECTS=$(subst .cc,.o,$(CACHE_CHECK_SOURCE)) cache_check: $(CACHE_CHECK_OBJECTS) cache/check.o - $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS) + @echo " [LD] $@" + $(V) $(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS) + +.PHONY: clean distclean clean: $(RM) *.o unit-tests/*.o *.d unit-tests/*.d $(TEST_PROGRAMS) $(PROGRAMS) $(OBJECTS) + distclean: clean $(RM) config.cache config.log config.status configure.h version.h Makefile unit-tests/Makefile -.PHONY: clean distclean install: $(PROGRAMS) $(INSTALL_DIR) $(BINDIR) diff --git a/persistent-data/data-structures/array.h b/persistent-data/data-structures/array.h index 868cf59..af8808b 100644 --- a/persistent-data/data-structures/array.h +++ b/persistent-data/data-structures/array.h @@ -45,11 +45,11 @@ namespace persistent_data { virtual void prepare(buffer<> &b, block_address location) const { array_block_disk *data = reinterpret_cast(&b); - data->blocknr = to_disk(location); + data->blocknr = to_disk(location); crc32c sum(ARRAY_CSUM_XOR); sum.append(&data->max_entries, MD_BLOCK_SIZE - sizeof(uint32_t)); - data->csum = to_disk(sum.get_sum()); + data->csum = to_disk(sum.get_sum()); } }; @@ -109,7 +109,7 @@ namespace persistent_data { friend class block_ref_counter; struct block_traits { - typedef base::__le64 disk_type; + typedef base::le64 disk_type; typedef block_address value_type; typedef block_ref_counter ref_counter; @@ -118,7 +118,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/data-structures/array_block.h b/persistent-data/data-structures/array_block.h index 74c062d..1bb3d4c 100644 --- a/persistent-data/data-structures/array_block.h +++ b/persistent-data/data-structures/array_block.h @@ -25,11 +25,11 @@ namespace persistent_data { struct array_block_disk { - base::__le32 csum; - base::__le32 max_entries; - base::__le32 nr_entries; - base::__le32 value_size; - base::__le64 blocknr; + base::le32 csum; + base::le32 max_entries; + base::le32 nr_entries; + base::le32 value_size; + base::le64 blocknr; } __attribute__((packed)); // RefType should be either a read_ref or write_ref from block_manager @@ -58,9 +58,9 @@ namespace persistent_data { void setup_empty() { using namespace base; struct array_block_disk *header = get_header(); - header->max_entries = to_disk<__le32>(calc_max_entries()); - header->nr_entries = to_disk<__le32>(static_cast(0)); - header->value_size = to_disk<__le32>(static_cast(sizeof(disk_type))); + header->max_entries = to_disk(calc_max_entries()); + header->nr_entries = to_disk(static_cast(0)); + header->value_size = to_disk(static_cast(sizeof(disk_type))); } uint32_t max_entries() const { @@ -142,7 +142,7 @@ namespace persistent_data { void set_nr_entries(uint32_t nr) { using namespace base; array_block_disk *h = get_header(); - h->nr_entries = to_disk<__le32>(nr); + h->nr_entries = to_disk(nr); } void grow_(uint32_t nr, value_type const &default_value) { diff --git a/persistent-data/data-structures/btree.h b/persistent-data/data-structures/btree.h index 62cec9d..776b365 100644 --- a/persistent-data/data-structures/btree.h +++ b/persistent-data/data-structures/btree.h @@ -31,7 +31,7 @@ namespace persistent_data { struct uint64_traits { - typedef base::__le64 disk_type; + typedef base::le64 disk_type; typedef uint64_t value_type; typedef no_op_ref_counter ref_counter; @@ -40,7 +40,7 @@ namespace persistent_data { } static void pack(value_type const &value, disk_type &disk) { - disk = base::to_disk(value); + disk = base::to_disk(value); } }; @@ -59,19 +59,19 @@ namespace persistent_data { }; struct node_header { - __le32 csum; - __le32 flags; - __le64 blocknr; /* which block this node is supposed to live in */ + le32 csum; + le32 flags; + le64 blocknr; /* which block this node is supposed to live in */ - __le32 nr_entries; - __le32 max_entries; - __le32 value_size; - __le32 padding; + le32 nr_entries; + le32 max_entries; + le32 value_size; + le32 padding; } __attribute__((packed)); struct disk_node { struct node_header header; - __le64 keys[0]; + le64 keys[0]; } __attribute__((packed)); enum node_type { diff --git a/persistent-data/data-structures/btree.tcc b/persistent-data/data-structures/btree.tcc index 702b69b..d281cdd 100644 --- a/persistent-data/data-structures/btree.tcc +++ b/persistent-data/data-structures/btree.tcc @@ -48,11 +48,11 @@ namespace { virtual void prepare(buffer<> &b, block_address location) const { disk_node *data = reinterpret_cast(&b); node_header *n = &data->header; - n->blocknr = to_disk(location); + n->blocknr = to_disk(location); crc32c sum(BTREE_CSUM_XOR); sum.append(&n->flags, MD_BLOCK_SIZE - sizeof(uint32_t)); - n->csum = to_disk(sum.get_sum()); + n->csum = to_disk(sum.get_sum()); } }; @@ -138,7 +138,7 @@ node_ref::set_type(node_type t) flags = LEAF_NODE; break; } - raw_->header.flags = to_disk<__le32>(flags); + raw_->header.flags = to_disk(flags); } template @@ -152,7 +152,7 @@ template void node_ref::set_nr_entries(unsigned n) { - raw_->header.nr_entries = to_disk<__le32>(n); + raw_->header.nr_entries = to_disk(n); } template @@ -166,7 +166,7 @@ template void node_ref::set_max_entries(unsigned n) { - raw_->header.max_entries = to_disk<__le32>(n); + raw_->header.max_entries = to_disk(n); } template @@ -187,7 +187,7 @@ template void node_ref::set_value_size(size_t s) { - raw_->header.value_size = to_disk<__le32>(static_cast(s)); + raw_->header.value_size = to_disk(static_cast(s)); } template @@ -203,7 +203,7 @@ template void node_ref::set_key(unsigned i, uint64_t k) { - raw_->keys[i] = to_disk<__le64>(k); + raw_->keys[i] = to_disk(k); } template diff --git a/persistent-data/endian_utils.h b/persistent-data/endian_utils.h index 50a65e2..39276e6 100644 --- a/persistent-data/endian_utils.h +++ b/persistent-data/endian_utils.h @@ -31,24 +31,24 @@ namespace base { // These are just little wrapper types to make the compiler // understand that the le types are not assignable to the // corresponding cpu type. - struct __le16 { - explicit __le16(uint16_t v = 0) + struct le16 { + explicit le16(uint16_t v = 0) : v_(v) { } uint16_t v_; } __attribute__((packed)); - struct __le32 { - explicit __le32(uint32_t v = 0) + struct le32 { + explicit le32(uint32_t v = 0) : v_(v) { } uint32_t v_; } __attribute__((packed)); - struct __le64 { - explicit __le64(uint64_t v = 0) + struct le64 { + explicit le64(uint64_t v = 0) : v_(v) { } @@ -69,33 +69,33 @@ namespace base { } template <> - inline uint16_t to_cpu(__le16 const &d) { + inline uint16_t to_cpu(le16 const &d) { return le16toh(d.v_); } template <> - inline __le16 to_disk<__le16, uint16_t>(uint16_t const &v) { - return __le16(htole16(v)); + inline le16 to_disk(uint16_t const &v) { + return le16(htole16(v)); } template <> - inline uint32_t to_cpu(__le32 const &d) { + inline uint32_t to_cpu(le32 const &d) { return le32toh(d.v_); } template <> - inline __le32 to_disk<__le32, uint32_t>(uint32_t const &v) { - return __le32(htole32(v)); + inline le32 to_disk(uint32_t const &v) { + return le32(htole32(v)); } template <> - inline uint64_t to_cpu(__le64 const &d) { + inline uint64_t to_cpu(le64 const &d) { return le64toh(d.v_); } template <> - inline __le64 to_disk<__le64, uint64_t>(uint64_t const &v) { - return __le64(htole64(v)); + inline le64 to_disk(uint64_t const &v) { + return le64(htole64(v)); } //-------------------------------- diff --git a/unit-tests/Makefile.in b/unit-tests/Makefile.in index 296ca81..b23abf9 100644 --- a/unit-tests/Makefile.in +++ b/unit-tests/Makefile.in @@ -16,7 +16,7 @@ # with thin-provisioning-tools. If not, see # . -TEST_SOURCE=\ +TEST_SOURCE=$(GMOCK_SOURCE) \ unit-tests/array_block_t.cc \ unit-tests/array_t.cc \ unit-tests/bitset_t.cc \ @@ -34,9 +34,6 @@ unit-test: $(TEST_PROGRAMS) .PHONY: unit-test -unit-tests/array_t: unit-tests/array_t.o $(OBJECTS) - g++ $(CXXFLAGS) $(INCLUDES) -o $@ $+ $(LIBS) $(LIBEXPAT) - unit-tests/array_block_t: unit-tests/array_block_t.o $(OBJECTS) g++ $(CXXFLAGS) $(INCLUDES) -o $@ $+ $(LIBS) $(LIBEXPAT) @@ -80,7 +77,7 @@ GMOCK_INCLUDES=\ -Igmock-1.6.0/gtest/include GMOCK_LIBS=\ - -Llib -lgmock -lpthread + -Llib -lpdata -lgmock -lpthread GMOCK_DEPS=\ $(wildcard $(GMOCK_DIR)/include/*.h) \ @@ -96,6 +93,7 @@ lib/libgmock.a: $(GMOCK_DEPS) MOCK_SOURCE=\ unit-tests/gmock_main.cc \ \ + unit-tests/array_t.cc \ unit-tests/buffer_t.cc # .gmo files are plain .o files, only they've been built with gmock @@ -108,8 +106,8 @@ MOCK_SOURCE=\ MOCK_OBJECTS=$(subst .cc,.gmo,$(MOCK_SOURCE)) -unit-tests/gmock_tests: $(MOCK_OBJECTS) lib/libgmock.a +unit-tests/gmock_tests: $(MOCK_OBJECTS) lib/libgmock.a lib/libpdata.a @echo " [LD] $<" - $(V) g++ $(CXXFLAGS) -o $@ $(MOCK_OBJECTS) $(LIBS) $(GMOCK_LIBS) + g++ $(CXXFLAGS) -o $@ $(MOCK_OBJECTS) $(LIBS) $(GMOCK_LIBS) $(LIBEXPAT) all: $(TEST_PROGRAMS) diff --git a/unit-tests/array_t.cc b/unit-tests/array_t.cc index e63e16d..d6e1f72 100644 --- a/unit-tests/array_t.cc +++ b/unit-tests/array_t.cc @@ -16,18 +16,17 @@ // 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/array.h" #include -#define BOOST_TEST_MODULE ArrayTests -#include - using namespace std; using namespace boost; using namespace persistent_data; +using namespace testing; //---------------------------------------------------------------- @@ -66,26 +65,24 @@ namespace { //---------------------------------------------------------------- -BOOST_AUTO_TEST_CASE(can_create_an_empty_array) +TEST(ArrayTests, can_create_an_empty_array) { array::ptr tree = create_array(0, 0); - - BOOST_CHECK_THROW(tree->get(0), runtime_error); + ASSERT_THROW(tree->get(0), runtime_error); } -BOOST_AUTO_TEST_CASE(get_elements) +TEST(ArrayTests, get_elements) { unsigned const COUNT = 10000; array::ptr tree = create_array(COUNT, 123); - for (unsigned i = 0; i < COUNT; i++) { - BOOST_CHECK_EQUAL(tree->get(i), 123); - } + for (unsigned i = 0; i < COUNT; i++) + ASSERT_THAT(tree->get(i), Eq(123)); - BOOST_CHECK_THROW(tree->get(COUNT), runtime_error); + ASSERT_THROW(tree->get(COUNT), runtime_error); } -BOOST_AUTO_TEST_CASE(set_elements) +TEST(ArrayTests, set_elements) { unsigned const COUNT = 10000; array::ptr tree = create_array(COUNT, 123); @@ -94,9 +91,9 @@ BOOST_AUTO_TEST_CASE(set_elements) tree->set(i, 124); for (unsigned i = 0; i < COUNT; i++) - BOOST_CHECK_EQUAL(tree->get(i), 124); + ASSERT_THAT(tree->get(i), Eq(124)); - BOOST_CHECK_THROW(tree->get(COUNT), runtime_error); + ASSERT_THROW(tree->get(COUNT), runtime_error); } template @@ -104,7 +101,7 @@ unsigned array_size(T (&)[size]) { return size; } -BOOST_AUTO_TEST_CASE(grow) +TEST(ArrayTests, grow) { unsigned const COUNT = 10000; unsigned const STEPS[] = { @@ -124,22 +121,22 @@ BOOST_AUTO_TEST_CASE(grow) for (unsigned i = 1; i < chunks.size(); i++) { if (i > 1) - BOOST_CHECK_EQUAL(a->get(chunks[i - 1] - 1), i - 1); + ASSERT_THAT(a->get(chunks[i - 1] - 1), Eq(i - 1)); a->grow(chunks[i], i); if (i > 1) - BOOST_CHECK_EQUAL(a->get(chunks[i - 1] - 1), i - 1); + ASSERT_THAT(a->get(chunks[i - 1] - 1), Eq(i - 1)); for (unsigned j = chunks[i - 1]; j < chunks[i]; j++) - BOOST_CHECK_EQUAL(a->get(j), i); + ASSERT_THAT(a->get(j), Eq(i)); - BOOST_CHECK_THROW(a->get(chunks[i] + 1), runtime_error); + ASSERT_THROW(a->get(chunks[i] + 1), runtime_error); } } } -BOOST_AUTO_TEST_CASE(reopen_array) +TEST(ArrayTests, reopen_array) { unsigned const COUNT = 10000; block_address root; @@ -157,11 +154,11 @@ BOOST_AUTO_TEST_CASE(reopen_array) typename array64::ptr a = open_array(root, COUNT); for (unsigned i = 0; i < COUNT; i++) - BOOST_CHECK_EQUAL(a->get(i), i % 7 ? 123: 234); + ASSERT_THAT(a->get(i), Eq(i % 7 ? 123: 234)); } } -BOOST_AUTO_TEST_CASE(destroy) +TEST(Array_Tests, destroy) { // FIXME: pending }