Convert bitset_t to gmock

This commit is contained in:
Joe Thornber 2013-03-22 14:36:31 +00:00
parent 1e141f83bb
commit 0a70c17268
9 changed files with 58 additions and 68 deletions

View File

@ -174,7 +174,7 @@ cache_check: $(CACHE_CHECK_OBJECTS) cache/check.o
.PHONY: clean distclean .PHONY: clean distclean
clean: 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 distclean: clean
$(RM) config.cache config.log config.status configure.h version.h Makefile unit-tests/Makefile $(RM) config.cache config.log config.status configure.h version.h Makefile unit-tests/Makefile

View File

@ -64,11 +64,6 @@ namespace persistent_data {
unsigned nr_entries_in_last_block; unsigned nr_entries_in_last_block;
unsigned nr_total_blocks; 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 <typename ValueTraits> template <typename ValueTraits>

View File

@ -27,7 +27,7 @@
namespace persistent_data { namespace persistent_data {
namespace bitset_detail { namespace bitset_detail {
struct bitset_traits { struct bitset_traits {
typedef base::__le64 disk_type; typedef base::le64 disk_type;
typedef uint64_t value_type; typedef uint64_t value_type;
typedef no_op_ref_counter<uint64_t> ref_counter; typedef no_op_ref_counter<uint64_t> ref_counter;
@ -36,7 +36,7 @@ namespace persistent_data {
} }
static void pack(value_type const &value, disk_type &disk) { 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,7 +25,7 @@ using namespace base;
bool bool
base::test_bit_le(void const *bits, unsigned b) base::test_bit_le(void const *bits, unsigned b)
{ {
__le64 const *w = reinterpret_cast<__le64 const *>(bits); le64 const *w = reinterpret_cast<le64 const *>(bits);
w += b / 64; w += b / 64;
uint64_t v = to_cpu<uint64_t>(*w); uint64_t v = to_cpu<uint64_t>(*w);
@ -37,20 +37,20 @@ base::test_bit_le(void const *bits, unsigned b)
void void
base::set_bit_le(void *bits, unsigned b) base::set_bit_le(void *bits, unsigned b)
{ {
__le64 *w = reinterpret_cast<__le64 *>(bits); le64 *w = reinterpret_cast<le64 *>(bits);
w += b / 64; w += b / 64;
uint64_t v = to_cpu<uint64_t>(*w); uint64_t v = to_cpu<uint64_t>(*w);
uint64_t mask = 1; uint64_t mask = 1;
mask = mask << (b % 64); mask = mask << (b % 64);
v |= mask; v |= mask;
*w = to_disk<__le64>(v); *w = to_disk<le64>(v);
} }
void void
base::clear_bit_le(void *bits, unsigned b) base::clear_bit_le(void *bits, unsigned b)
{ {
__le64 *w = reinterpret_cast<__le64 *>(bits); le64 *w = reinterpret_cast<le64 *>(bits);
w += b / 64; w += b / 64;
uint64_t v = to_cpu<uint64_t>(*w); uint64_t v = to_cpu<uint64_t>(*w);
@ -58,7 +58,7 @@ base::clear_bit_le(void *bits, unsigned b)
mask = mask << (b % 64); mask = mask << (b % 64);
mask = ~mask; mask = ~mask;
v &= mask; v &= mask;
*w = to_disk<__le64>(v); *w = to_disk<le64>(v);
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -51,11 +51,11 @@ namespace {
virtual void prepare(buffer<> &b, block_address location) const { virtual void prepare(buffer<> &b, block_address location) const {
bitmap_header *data = reinterpret_cast<bitmap_header *>(&b); bitmap_header *data = reinterpret_cast<bitmap_header *>(&b);
data->blocknr = to_disk<base::__le64, uint64_t>(location); data->blocknr = to_disk<base::le64, uint64_t>(location);
crc32c sum(BITMAP_CSUM_XOR); crc32c sum(BITMAP_CSUM_XOR);
sum.append(&data->not_used, MD_BLOCK_SIZE - sizeof(uint32_t)); sum.append(&data->not_used, MD_BLOCK_SIZE - sizeof(uint32_t));
data->csum = to_disk<base::__le32>(sum.get_sum()); data->csum = to_disk<base::le32>(sum.get_sum());
} }
}; };
@ -83,11 +83,11 @@ namespace {
virtual void prepare(buffer<> &b, block_address location) const { virtual void prepare(buffer<> &b, block_address location) const {
metadata_index *mi = reinterpret_cast<metadata_index *>(&b); metadata_index *mi = reinterpret_cast<metadata_index *>(&b);
mi->blocknr_ = to_disk<base::__le64, uint64_t>(location); mi->blocknr_ = to_disk<base::le64, uint64_t>(location);
crc32c sum(INDEX_CSUM_XOR); crc32c sum(INDEX_CSUM_XOR);
sum.append(&mi->padding_, MD_BLOCK_SIZE - sizeof(uint32_t)); sum.append(&mi->padding_, MD_BLOCK_SIZE - sizeof(uint32_t));
mi->csum_ = to_disk<base::__le32>(sum.get_sum()); mi->csum_ = to_disk<base::le32>(sum.get_sum());
} }
}; };
@ -194,7 +194,7 @@ namespace {
}; };
struct ref_count_traits { struct ref_count_traits {
typedef __le32 disk_type; typedef le32 disk_type;
typedef uint32_t value_type; typedef uint32_t value_type;
typedef no_op_ref_counter<uint32_t> ref_counter; typedef no_op_ref_counter<uint32_t> ref_counter;

View File

@ -31,9 +31,9 @@ namespace persistent_data {
namespace sm_disk_detail { namespace sm_disk_detail {
struct index_entry_disk { struct index_entry_disk {
__le64 blocknr_; le64 blocknr_;
__le32 nr_free_; le32 nr_free_;
__le32 none_free_before_; le32 none_free_before_;
} __attribute__ ((packed)); } __attribute__ ((packed));
struct index_entry { struct index_entry {
@ -54,9 +54,9 @@ namespace persistent_data {
} }
static void pack(value_type const &value, disk_type &disk) { static void pack(value_type const &value, disk_type &disk) {
disk.blocknr_ = to_disk<__le64>(value.blocknr_); disk.blocknr_ = to_disk<le64>(value.blocknr_);
disk.nr_free_ = to_disk<__le32>(value.nr_free_); disk.nr_free_ = to_disk<le32>(value.nr_free_);
disk.none_free_before_ = to_disk<__le32>(value.none_free_before_); disk.none_free_before_ = to_disk<le32>(value.none_free_before_);
} }
}; };
@ -64,18 +64,18 @@ namespace persistent_data {
unsigned const ENTRIES_PER_BYTE = 4; unsigned const ENTRIES_PER_BYTE = 4;
struct metadata_index { struct metadata_index {
__le32 csum_; le32 csum_;
__le32 padding_; le32 padding_;
__le64 blocknr_; le64 blocknr_;
struct index_entry_disk index[MAX_METADATA_BITMAPS]; struct index_entry_disk index[MAX_METADATA_BITMAPS];
} __attribute__ ((packed)); } __attribute__ ((packed));
struct sm_root_disk { struct sm_root_disk {
__le64 nr_blocks_; le64 nr_blocks_;
__le64 nr_allocated_; le64 nr_allocated_;
__le64 bitmap_root_; le64 bitmap_root_;
__le64 ref_count_root_; le64 ref_count_root_;
} __attribute__ ((packed)); } __attribute__ ((packed));
struct sm_root { struct sm_root {
@ -98,17 +98,17 @@ namespace persistent_data {
} }
static void pack(value_type const &value, disk_type &disk) { static void pack(value_type const &value, disk_type &disk) {
disk.nr_blocks_ = to_disk<__le64>(value.nr_blocks_); disk.nr_blocks_ = to_disk<le64>(value.nr_blocks_);
disk.nr_allocated_ = to_disk<__le64>(value.nr_allocated_); disk.nr_allocated_ = to_disk<le64>(value.nr_allocated_);
disk.bitmap_root_ = to_disk<__le64>(value.bitmap_root_); disk.bitmap_root_ = to_disk<le64>(value.bitmap_root_);
disk.ref_count_root_ = to_disk<__le64>(value.ref_count_root_); disk.ref_count_root_ = to_disk<le64>(value.ref_count_root_);
} }
}; };
struct bitmap_header { struct bitmap_header {
__le32 csum; le32 csum;
__le32 not_used; le32 not_used;
__le64 blocknr; le64 blocknr;
} __attribute__ ((packed)); } __attribute__ ((packed));
} }
} }

View File

@ -34,9 +34,6 @@ unit-test: $(TEST_PROGRAMS)
.PHONY: unit-test .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) unit-tests/block_t: unit-tests/block_t.o $(OBJECTS)
g++ $(CXXFLAGS) $(INCLUDES) -o $@ $+ $(LIBS) $(LIBEXPAT) g++ $(CXXFLAGS) $(INCLUDES) -o $@ $+ $(LIBS) $(LIBEXPAT)
@ -92,6 +89,7 @@ MOCK_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/buffer_t.cc unit-tests/buffer_t.cc
# .gmo files are plain .o files, only they've been built with gmock # .gmo files are plain .o files, only they've been built with gmock

View File

@ -77,7 +77,7 @@ TEST(ArrayTests, get_elements)
array<uint64_traits>::ptr tree = create_array(COUNT, 123); array<uint64_traits>::ptr tree = create_array(COUNT, 123);
for (unsigned i = 0; i < COUNT; i++) 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); ASSERT_THROW(tree->get(COUNT), runtime_error);
} }
@ -91,7 +91,7 @@ TEST(ArrayTests, set_elements)
tree->set(i, 124); tree->set(i, 124);
for (unsigned i = 0; i < COUNT; i++) 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); ASSERT_THROW(tree->get(COUNT), runtime_error);
} }
@ -154,7 +154,7 @@ TEST(ArrayTests, reopen_array)
typename array64::ptr a = open_array(root, COUNT); typename array64::ptr a = open_array(root, COUNT);
for (unsigned i = 0; i < COUNT; i++) 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));
} }
} }

View File

@ -16,18 +16,16 @@
// with thin-provisioning-tools. If not, see // with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
#include "gmock/gmock.h"
#include "persistent-data/transaction_manager.h" #include "persistent-data/transaction_manager.h"
#include "persistent-data/space-maps/core.h" #include "persistent-data/space-maps/core.h"
#include "persistent-data/data-structures/bitset.h" #include "persistent-data/data-structures/bitset.h"
#include <vector> #include <vector>
#define BOOST_TEST_MODULE ArrayTests
#include <boost/test/included/unit_test.hpp>
using namespace std; using namespace std;
using namespace boost;
using namespace persistent_data; 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(); 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; unsigned const COUNT = 100000;
@ -69,10 +67,10 @@ BOOST_AUTO_TEST_CASE(grow_default_false)
bs->grow(COUNT, false); bs->grow(COUNT, false);
for (unsigned i = 0; i < COUNT; i++) 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; unsigned const COUNT = 100000;
@ -80,19 +78,19 @@ BOOST_AUTO_TEST_CASE(grow_default_true)
bs->grow(COUNT, true); bs->grow(COUNT, true);
for (unsigned i = 0; i < COUNT; i++) 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; unsigned const COUNT = 100000;
bitset::ptr bs = create_bitset(); bitset::ptr bs = create_bitset();
bs->grow(COUNT, false); 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 COUNT = 100000;
unsigned const STEP = 37; unsigned const STEP = 37;
@ -109,7 +107,7 @@ BOOST_AUTO_TEST_CASE(multiple_grow_calls)
bs->grow(chunks[i], default_value); bs->grow(chunks[i], default_value);
for (unsigned j = chunks[i - 1]; j < chunks[i]; j++) 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; default_value = !default_value;
} }
@ -117,23 +115,23 @@ BOOST_AUTO_TEST_CASE(multiple_grow_calls)
default_value = true; default_value = true;
for (unsigned i = 1; i < chunks.size(); i++) { for (unsigned i = 1; i < chunks.size(); i++) {
for (unsigned j = chunks[i - 1]; j < chunks[i]; j++) 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; default_value = !default_value;
} }
} }
BOOST_AUTO_TEST_CASE(set_out_of_bounds_throws) TEST(BitsetTests, set_out_of_bounds_throws)
{ {
unsigned const COUNT = 100000; unsigned const COUNT = 100000;
bitset::ptr bs = create_bitset(); 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); 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; unsigned const COUNT = 100000;
bitset::ptr bs = create_bitset(); bitset::ptr bs = create_bitset();
@ -142,12 +140,11 @@ BOOST_AUTO_TEST_CASE(set_works)
for (unsigned i = 0; i < COUNT; i += 7) for (unsigned i = 0; i < COUNT; i += 7)
bs->set(i, false); bs->set(i, false);
for (unsigned i = 0; i < COUNT; i++) { 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));
}
} }
BOOST_AUTO_TEST_CASE(reopen_works) TEST(BitsetTests, reopen_works)
{ {
unsigned const COUNT = 100000; unsigned const COUNT = 100000;
block_address root; block_address root;
@ -165,7 +162,7 @@ BOOST_AUTO_TEST_CASE(reopen_works)
{ {
bitset::ptr bs = open_bitset(root, COUNT); bitset::ptr bs = open_bitset(root, COUNT);
for (unsigned i = 0; i < COUNT; i++) 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));
} }
} }