thin-provisioning-tools/unit-tests/array_block_t.cc

314 lines
8.2 KiB
C++
Raw Normal View History

2013-02-01 17:30:49 +05:30
// Copyright (C) 2013 Red Hat, Inc. All rights reserved.
//
// This file is part of the thin-provisioning-tools source.
//
// thin-provisioning-tools is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// thin-provisioning-tools is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>.
2013-03-22 19:43:58 +05:30
#include "gmock/gmock.h"
2013-02-01 17:30:49 +05:30
#include "persistent-data/transaction_manager.h"
#include "persistent-data/space-maps/core.h"
#include "persistent-data/data-structures/array_block.h"
#include "test_utils.h"
#include <utility>
#include <vector>
using namespace persistent_data;
using namespace std;
using namespace test;
2013-03-22 19:43:58 +05:30
using namespace testing;
2013-02-01 17:30:49 +05:30
//----------------------------------------------------------------
namespace {
uint64_t MAX_VALUE = 1000ull;
block_address const NR_BLOCKS = 1024;
typedef typename block_manager<>::noop_validator noop_validator;
typedef typename block_manager<>::read_ref read_ref;
typedef typename block_manager<>::write_ref write_ref;
// FIXME: lift to utils?
class simple_ref_counter {
public:
simple_ref_counter(uint64_t nr_counts)
: counts_(nr_counts, 0u) {
}
void inc(uint64_t n) {
counts_.at(n)++;
}
void dec(uint64_t n) {
counts_.at(n)--;
}
unsigned get(uint64_t n) const {
return counts_.at(n);
}
private:
vector<unsigned> counts_;
};
struct uint64_traits {
2013-03-22 19:43:58 +05:30
typedef base::le64 disk_type;
2013-02-01 17:30:49 +05:30
typedef uint64_t value_type;
typedef simple_ref_counter ref_counter;
static void unpack(disk_type const &disk, value_type &value) {
value = base::to_cpu<uint64_t>(disk);
}
static void pack(value_type const &value, disk_type &disk) {
2013-03-22 19:43:58 +05:30
disk = base::to_disk<base::le64>(value);
2013-02-01 17:30:49 +05:30
}
};
typedef array_block<uint64_traits, write_ref> ablock64;
typedef array_block<uint64_traits, read_ref> ablock64_r;
block_manager<>::validator::ptr
validator() {
return block_manager<>::validator::ptr(new block_manager<>::noop_validator);
}
transaction_manager::ptr
create_tm() {
block_manager<>::ptr bm = create_bm<4096>(NR_BLOCKS);
space_map::ptr sm(new core_map(NR_BLOCKS));
transaction_manager::ptr tm(new transaction_manager(bm, sm));
return tm;
}
pair<ablock64, block_address>
new_array_block(transaction_manager::ptr tm) {
uint64_traits::ref_counter rc(MAX_VALUE);
write_ref wr = tm->new_block(validator());
2013-03-13 18:29:11 +05:30
ablock64 b(wr, rc);
b.setup_empty();
return make_pair(b, wr.get_location());
2013-02-01 17:30:49 +05:30
}
ablock64
open_array_block(transaction_manager::ptr tm, block_address loc) {
uint64_traits::ref_counter rc(MAX_VALUE);
pair<write_ref, bool> p = tm->shadow(loc, validator());
2013-03-22 19:43:58 +05:30
// FIXME: no idea why this isn't working.
// ASSERT_FALSE(p.second);
2013-02-01 17:30:49 +05:30
return ablock64(p.first, rc);
}
ablock64_r
read_array_block(transaction_manager::ptr tm, block_address loc) {
uint64_traits::ref_counter rc(MAX_VALUE);
read_ref rr = tm->read_lock(loc, validator());
return ablock64_r(rr, rc);
}
}
//----------------------------------------------------------------
2013-03-22 19:43:58 +05:30
TEST(ArrayBlockTests, can_create_an_empty_array)
2013-02-01 17:30:49 +05:30
{
block_address loc;
transaction_manager::ptr tm = create_tm();
{
pair<ablock64, block_address> p = new_array_block(tm);
ablock64 &b = p.first;
loc = p.second;
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.nr_entries(), Eq(0u));
ASSERT_THAT(b.value_size(), Eq(sizeof(uint64_t)));
ASSERT_THAT(b.max_entries(), Eq(static_cast<unsigned>((4096 - 24) / 8)));
2013-02-01 17:30:49 +05:30
2013-03-22 19:43:58 +05:30
ASSERT_THROW(b.get(0), runtime_error);
ASSERT_THROW(b.set(0, 12345LL), runtime_error);
2013-02-01 17:30:49 +05:30
}
{
ablock64 b = open_array_block(tm, loc);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.nr_entries(), Eq(0u));
ASSERT_THAT(b.value_size(), Eq(sizeof(uint64_t)));
ASSERT_THAT(b.max_entries(), Eq(static_cast<unsigned>((4096 - 24) / 8)));
2013-02-01 17:30:49 +05:30
2013-03-22 19:43:58 +05:30
ASSERT_THROW(b.get(0), runtime_error);
ASSERT_THROW(b.set(0, 12345LL), runtime_error);
2013-02-01 17:30:49 +05:30
}
}
2013-03-22 19:43:58 +05:30
TEST(ArrayBlockTests, read_only_array_blocks_are_possible)
2013-02-01 17:30:49 +05:30
{
2013-03-14 18:43:18 +05:30
unsigned const COUNT = 10;
2013-02-01 17:30:49 +05:30
block_address loc;
transaction_manager::ptr tm = create_tm();
{
pair<ablock64, block_address> p = new_array_block(tm);
ablock64 &b = p.first;
loc = p.second;
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.nr_entries(), Eq(0u));
ASSERT_THAT(b.value_size(), Eq(sizeof(uint64_t)));
ASSERT_THAT(b.max_entries(), Eq(static_cast<unsigned>((4096 - 24) / 8)));
2013-02-01 17:30:49 +05:30
2013-03-22 19:43:58 +05:30
ASSERT_THROW(b.get(0), runtime_error);
ASSERT_THROW(b.set(0, 12345LL), runtime_error);
2013-03-14 18:43:18 +05:30
b.grow(COUNT, 0);
for (unsigned i = 0; i < COUNT; i++)
b.set(i, i);
2013-02-01 17:30:49 +05:30
}
{
ablock64_r b = read_array_block(tm, loc);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.nr_entries(), Eq(COUNT));
ASSERT_THAT(b.value_size(), Eq(sizeof(uint64_t)));
ASSERT_THAT(b.max_entries(), Eq(static_cast<unsigned>((4096 - 24) / 8)));
2013-03-14 18:43:18 +05:30
for (unsigned i = 0; i < COUNT; i++)
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.get(i), Eq(i));
2013-03-14 18:43:18 +05:30
}
}
2013-03-22 19:43:58 +05:30
TEST(ArrayBlockTests, updating_reopened_array_block)
2013-03-14 18:43:18 +05:30
{
unsigned const COUNT = 10;
block_address loc;
transaction_manager::ptr tm = create_tm();
{
pair<ablock64, block_address> p = new_array_block(tm);
ablock64 &b = p.first;
loc = p.second;
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.nr_entries(), Eq(0u));
ASSERT_THAT(b.value_size(), Eq(sizeof(uint64_t)));
ASSERT_THAT(b.max_entries(), Eq(static_cast<unsigned>((4096 - 24) / 8)));
2013-02-01 17:30:49 +05:30
2013-03-22 19:43:58 +05:30
ASSERT_THROW(b.get(0), runtime_error);
ASSERT_THROW(b.set(0, 12345LL), runtime_error);
2013-02-01 17:30:49 +05:30
2013-03-14 18:43:18 +05:30
b.grow(COUNT, 0);
for (unsigned i = 0; i < COUNT; i++)
b.set(i, i);
}
{
ablock64 b = open_array_block(tm, loc);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.nr_entries(), Eq(COUNT));
ASSERT_THAT(b.value_size(), Eq(sizeof(uint64_t)));
ASSERT_THAT(b.max_entries(), static_cast<unsigned>((4096 - 24) / 8));
2013-03-14 18:43:18 +05:30
for (unsigned i = 0; i < COUNT; i++)
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.get(i), Eq(i));
2013-03-14 18:43:18 +05:30
for (unsigned i = 0; i < COUNT; i++)
b.set(i, i + 37);
}
{
ablock64_r b = read_array_block(tm, loc);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.nr_entries(), Eq(COUNT));
ASSERT_THAT(b.value_size(), Eq(sizeof(uint64_t)));
ASSERT_THAT(b.max_entries(), static_cast<unsigned>((4096 - 24) / 8));
2013-03-14 18:43:18 +05:30
for (unsigned i = 0; i < COUNT; i++)
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.get(i), Eq(i + 37u));
2013-02-01 17:30:49 +05:30
}
}
2013-03-22 19:43:58 +05:30
TEST(ArrayBlockTests, growing)
2013-02-01 17:30:49 +05:30
{
uint64_t default_value = 123, new_value = 234;
transaction_manager::ptr tm = create_tm();
pair<ablock64, block_address> p = new_array_block(tm);
ablock64 &b = p.first;
for (unsigned i = 1; i < b.max_entries(); i++) {
2013-03-22 19:43:58 +05:30
ASSERT_THROW(b.get(i - 1), runtime_error);
2013-02-01 17:30:49 +05:30
b.grow(i, default_value);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.get(i - 1), Eq(default_value));
2013-02-01 17:30:49 +05:30
b.set(i - 1, new_value);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.get(i - 1), Eq(new_value));
2013-02-01 17:30:49 +05:30
2013-03-22 19:43:58 +05:30
ASSERT_THROW(b.grow(i - 1, default_value), runtime_error);
2013-02-01 17:30:49 +05:30
}
}
2013-03-22 19:43:58 +05:30
TEST(ArrayBlockTests, grow_does_not_touch_existing_values)
2013-03-14 18:43:18 +05:30
{
transaction_manager::ptr tm = create_tm();
pair<ablock64, block_address> p = new_array_block(tm);
ablock64 &b = p.first;
b.grow(10, 123);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.get(9), Eq(123u));
2013-03-14 18:43:18 +05:30
b.grow(20, 234);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.get(9), Eq(123u));
2013-03-14 18:43:18 +05:30
}
2013-03-22 19:43:58 +05:30
TEST(ArrayBlockTests, shrinking)
2013-02-01 17:30:49 +05:30
{
uint64_t default_value = 123;
transaction_manager::ptr tm = create_tm();
pair<ablock64, block_address> p = new_array_block(tm);
ablock64 &b = p.first;
b.grow(b.max_entries() - 1, default_value);
for (unsigned i = b.max_entries() - 2; i; i--) {
2013-03-22 19:43:58 +05:30
ASSERT_THAT(b.get(i - 1), Eq(default_value));
2013-02-01 17:30:49 +05:30
b.shrink(i);
2013-03-22 19:43:58 +05:30
ASSERT_THROW(b.get(i), runtime_error);
ASSERT_THROW(b.shrink(i), runtime_error);
2013-02-01 17:30:49 +05:30
}
}
2013-03-22 19:43:58 +05:30
TEST(ArrayBlockTests, ref_counting)
2013-02-01 17:30:49 +05:30
{
transaction_manager::ptr tm = create_tm();
pair<ablock64, block_address> p = new_array_block(tm);
ablock64 &b = p.first;
simple_ref_counter const &rc = b.get_ref_counter();
2013-03-22 19:43:58 +05:30
ASSERT_THAT(rc.get(123), Eq(0u));
2013-02-01 17:30:49 +05:30
b.grow(b.max_entries() - 1, 123);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(rc.get(123), Eq(b.max_entries() - 1u));
2013-02-01 17:30:49 +05:30
b.shrink(100);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(rc.get(123), Eq(100u));
2013-02-01 17:30:49 +05:30
b.set(1, 0);
b.set(2, 2);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(rc.get(123), Eq(98u));
ASSERT_THAT(rc.get(0), Eq(1u));
2013-02-01 17:30:49 +05:30
b.set(2, 2);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(rc.get(2), Eq(1u));
2013-02-01 17:30:49 +05:30
b.set(10, 2);
2013-03-22 19:43:58 +05:30
ASSERT_THAT(rc.get(2), Eq(2u));
ASSERT_THAT(rc.get(123), Eq(97u));
2013-02-01 17:30:49 +05:30
}
//----------------------------------------------------------------