thin-provisioning-tools/space_map_disk_t.cc

109 lines
2.3 KiB
C++
Raw Normal View History

2011-07-22 20:39:56 +05:30
#include "space_map_disk.h"
#include "core_map.h"
#define BOOST_TEST_MODULE SpaceMapDiskTests
#include <boost/test/included/unit_test.hpp>
using namespace std;
using namespace boost;
using namespace persistent_data;
//----------------------------------------------------------------
namespace {
block_address const NR_BLOCKS = 1023;
block_address const SUPERBLOCK = 0;
unsigned const BLOCK_SIZE = 4096;
transaction_manager<BLOCK_SIZE>::ptr
create_tm() {
block_manager<BLOCK_SIZE>::ptr bm(
new block_manager<BLOCK_SIZE>("./test.data", NR_BLOCKS));
space_map::ptr sm(new core_map(1024));
transaction_manager<BLOCK_SIZE>::ptr tm(
new transaction_manager<BLOCK_SIZE>(bm, sm));
return tm;
}
persistent_space_map::ptr
create_sm_disk() {
auto tm = create_tm();
return persistent_data::create_disk_sm<BLOCK_SIZE>(tm, NR_BLOCKS);
}
}
//----------------------------------------------------------------
BOOST_AUTO_TEST_CASE(reopen_an_sm)
{
auto sm = create_sm_disk();
}
BOOST_AUTO_TEST_CASE(test_get_nr_blocks)
{
auto sm = create_sm_disk();
BOOST_CHECK_EQUAL(sm->get_nr_blocks(), NR_BLOCKS);
}
BOOST_AUTO_TEST_CASE(test_get_nr_free)
{
auto sm = create_sm_disk();
BOOST_CHECK_EQUAL(sm->get_nr_free(), NR_BLOCKS);
for (unsigned i = 0; i < NR_BLOCKS; i++) {
sm->new_block();
BOOST_CHECK_EQUAL(sm->get_nr_free(), NR_BLOCKS - i - 1);
}
for (unsigned i = 0; i < NR_BLOCKS; i++) {
sm->dec(i);
BOOST_CHECK_EQUAL(sm->get_nr_free(), i + 1);
}
}
BOOST_AUTO_TEST_CASE(test_throws_no_space)
{
auto sm = create_sm_disk();
for (unsigned i = 0; i < NR_BLOCKS; i++)
sm->new_block();
BOOST_CHECK_THROW(sm->new_block(), std::runtime_error);
}
BOOST_AUTO_TEST_CASE(test_inc_and_dec)
{
auto sm = create_sm_disk();
block_address b = 63;
for (unsigned i = 0; i < 50; i++) {
BOOST_CHECK_EQUAL(sm->get_count(b), i);
sm->inc(b);
}
for (unsigned i = 50; i > 0; i--) {
BOOST_CHECK_EQUAL(sm->get_count(b), i);
sm->dec(b);
}
}
BOOST_AUTO_TEST_CASE(test_not_allocated_twice)
{
auto sm = create_sm_disk();
block_address b = sm->new_block();
try {
for (;;)
BOOST_CHECK(sm->new_block() != b);
} catch (...) {
}
}
BOOST_AUTO_TEST_CASE(test_set_count)
{
auto sm = create_sm_disk();
sm->set_count(43, 5);
BOOST_CHECK_EQUAL(sm->get_count(43), 5);
}
//----------------------------------------------------------------