Move array_t to gmock

This commit is contained in:
Joe Thornber 2013-03-22 13:42:20 +00:00
parent 50bab1c90a
commit f9bf18adf6
8 changed files with 98 additions and 91 deletions

View File

@ -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)

View File

@ -45,11 +45,11 @@ namespace persistent_data {
virtual void prepare(buffer<> &b, block_address location) const {
array_block_disk *data = reinterpret_cast<array_block_disk *>(&b);
data->blocknr = to_disk<base::__le64, uint64_t>(location);
data->blocknr = to_disk<base::le64, uint64_t>(location);
crc32c sum(ARRAY_CSUM_XOR);
sum.append(&data->max_entries, MD_BLOCK_SIZE - sizeof(uint32_t));
data->csum = to_disk<base::__le32>(sum.get_sum());
data->csum = to_disk<base::le32>(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<base::__le64>(value);
disk = base::to_disk<base::le64>(value);
}
};

View File

@ -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<uint32_t>(0));
header->value_size = to_disk<__le32>(static_cast<uint32_t>(sizeof(disk_type)));
header->max_entries = to_disk<le32>(calc_max_entries());
header->nr_entries = to_disk<le32>(static_cast<uint32_t>(0));
header->value_size = to_disk<le32>(static_cast<uint32_t>(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<le32>(nr);
}
void grow_(uint32_t nr, value_type const &default_value) {

View File

@ -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<uint64_t> ref_counter;
@ -40,7 +40,7 @@ namespace persistent_data {
}
static void pack(value_type const &value, disk_type &disk) {
disk = base::to_disk<base::__le64>(value);
disk = base::to_disk<base::le64>(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 {

View File

@ -48,11 +48,11 @@ namespace {
virtual void prepare(buffer<> &b, block_address location) const {
disk_node *data = reinterpret_cast<disk_node *>(&b);
node_header *n = &data->header;
n->blocknr = to_disk<base::__le64, uint64_t>(location);
n->blocknr = to_disk<base::le64, uint64_t>(location);
crc32c sum(BTREE_CSUM_XOR);
sum.append(&n->flags, MD_BLOCK_SIZE - sizeof(uint32_t));
n->csum = to_disk<base::__le32>(sum.get_sum());
n->csum = to_disk<base::le32>(sum.get_sum());
}
};
@ -138,7 +138,7 @@ node_ref<ValueTraits>::set_type(node_type t)
flags = LEAF_NODE;
break;
}
raw_->header.flags = to_disk<__le32>(flags);
raw_->header.flags = to_disk<le32>(flags);
}
template <typename ValueTraits>
@ -152,7 +152,7 @@ template <typename ValueTraits>
void
node_ref<ValueTraits>::set_nr_entries(unsigned n)
{
raw_->header.nr_entries = to_disk<__le32>(n);
raw_->header.nr_entries = to_disk<le32>(n);
}
template <typename ValueTraits>
@ -166,7 +166,7 @@ template <typename ValueTraits>
void
node_ref<ValueTraits>::set_max_entries(unsigned n)
{
raw_->header.max_entries = to_disk<__le32>(n);
raw_->header.max_entries = to_disk<le32>(n);
}
template <typename ValueTraits>
@ -187,7 +187,7 @@ template <typename ValueTraits>
void
node_ref<ValueTraits>::set_value_size(size_t s)
{
raw_->header.value_size = to_disk<__le32>(static_cast<uint32_t>(s));
raw_->header.value_size = to_disk<le32>(static_cast<uint32_t>(s));
}
template <typename ValueTraits>
@ -203,7 +203,7 @@ template <typename ValueTraits>
void
node_ref<ValueTraits>::set_key(unsigned i, uint64_t k)
{
raw_->keys[i] = to_disk<__le64>(k);
raw_->keys[i] = to_disk<le64>(k);
}
template <typename ValueTraits>

View File

@ -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<uint16_t, __le16>(__le16 const &d) {
inline uint16_t to_cpu<uint16_t, le16>(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<le16, uint16_t>(uint16_t const &v) {
return le16(htole16(v));
}
template <>
inline uint32_t to_cpu<uint32_t, __le32>(__le32 const &d) {
inline uint32_t to_cpu<uint32_t, le32>(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<le32, uint32_t>(uint32_t const &v) {
return le32(htole32(v));
}
template <>
inline uint64_t to_cpu<uint64_t, __le64>(__le64 const &d) {
inline uint64_t to_cpu<uint64_t, le64>(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<le64, uint64_t>(uint64_t const &v) {
return le64(htole64(v));
}
//--------------------------------

View File

@ -16,7 +16,7 @@
# with thin-provisioning-tools. If not, see
# <http://www.gnu.org/licenses/>.
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)

View File

@ -16,18 +16,17 @@
// with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>.
#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 <vector>
#define BOOST_TEST_MODULE ArrayTests
#include <boost/test/included/unit_test.hpp>
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<uint64_traits>::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<uint64_traits>::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<uint64_traits>::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 <typename T, unsigned size>
@ -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
}