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

173 lines
3.6 KiB
C++
Raw Normal View History

2011-12-16 00:04:31 +05:30
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
2011-12-06 19:23:05 +05:30
//
2011-12-06 19:13:56 +05:30
// 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 20:16:43 +05:30
#include "gmock/gmock.h"
#include "persistent-data/cache.h"
2011-10-21 21:27:28 +05:30
#include <boost/shared_ptr.hpp>
2011-10-21 21:27:28 +05:30
using namespace boost;
using namespace base;
using namespace std;
2013-03-22 20:16:43 +05:30
using namespace testing;
2011-10-21 21:27:28 +05:30
//----------------------------------------------------------------
namespace {
struct Thing {
Thing()
: key_(0),
desc_("default constructed") {
}
2011-10-24 22:34:19 +05:30
explicit Thing(unsigned n)
2011-10-21 21:27:28 +05:30
: key_(n),
desc_("foo bar") {
}
unsigned key_;
string desc_;
};
struct ThingTraits {
typedef unsigned key_type;
typedef Thing value_type;
static key_type get_key(value_type const &t) {
return t.key_;
}
};
struct SharedThingTraits {
typedef unsigned key_type;
typedef boost::shared_ptr<Thing> value_type;
2011-10-21 21:27:28 +05:30
static key_type get_key(value_type const &p) {
return p->key_;
}
};
}
//----------------------------------------------------------------
2013-03-22 20:16:43 +05:30
TEST(CacheTests, cache_creation)
2011-10-21 21:27:28 +05:30
{
cache<ThingTraits> c(16);
}
2013-03-22 20:16:43 +05:30
TEST(CacheTests, cache_caches)
2011-10-21 21:27:28 +05:30
{
unsigned const COUNT = 16;
cache<ThingTraits> c(COUNT);
2011-10-24 22:34:19 +05:30
for (unsigned i = 0; i < COUNT; i++) {
2011-10-21 21:27:28 +05:30
c.insert(Thing(i));
2011-10-24 22:34:19 +05:30
c.put(Thing(i));
}
2011-10-21 21:27:28 +05:30
for (unsigned i = 0; i < COUNT; i++)
2013-03-22 20:16:43 +05:30
ASSERT_TRUE(c.get(i));
2011-10-21 21:27:28 +05:30
}
2013-03-22 20:16:43 +05:30
TEST(CacheTests, new_entries_have_a_ref_count_of_one)
2011-10-24 22:34:19 +05:30
{
cache<ThingTraits> c(16);
c.insert(Thing(0));
c.put(Thing(0));
2013-03-22 20:16:43 +05:30
ASSERT_THROW(c.put(Thing(0)), runtime_error);
2011-10-24 22:34:19 +05:30
}
2013-03-22 20:16:43 +05:30
TEST(CacheTests, cache_drops_elements)
2011-10-21 21:27:28 +05:30
{
unsigned const COUNT = 1024;
unsigned const CACHE_SIZE = 16;
cache<ThingTraits> c(CACHE_SIZE);
2011-10-24 22:34:19 +05:30
for (unsigned i = 0; i < COUNT; i++) {
2011-10-21 21:27:28 +05:30
c.insert(Thing(i));
2011-10-24 22:34:19 +05:30
c.put(Thing(i));
}
2011-10-21 21:27:28 +05:30
for (unsigned i = 0; i < COUNT - CACHE_SIZE; i++)
2013-03-22 20:16:43 +05:30
ASSERT_FALSE(c.get(i));
2011-10-21 21:27:28 +05:30
for (unsigned i = COUNT - CACHE_SIZE; i < COUNT; i++)
2013-03-22 20:16:43 +05:30
ASSERT_TRUE(c.get(i));
2011-10-21 21:27:28 +05:30
}
2013-03-22 20:16:43 +05:30
TEST(CacheTests, held_entries_count_towards_the_cache_limit)
2011-10-21 21:27:28 +05:30
{
unsigned const CACHE_SIZE = 16;
cache<ThingTraits> c(CACHE_SIZE);
unsigned i;
2011-10-24 22:34:19 +05:30
for (i = 0; i < CACHE_SIZE; i++)
2011-10-21 21:27:28 +05:30
c.insert(Thing(i));
2013-03-22 20:16:43 +05:30
ASSERT_THROW(c.insert(Thing(i)), runtime_error);
2011-10-21 21:27:28 +05:30
}
2013-03-22 20:16:43 +05:30
TEST(CacheTests, put_works)
2011-10-21 21:27:28 +05:30
{
unsigned const CACHE_SIZE = 16;
cache<ThingTraits> c(CACHE_SIZE);
unsigned i;
for (i = 0; i < CACHE_SIZE; i++) {
c.insert(Thing(i));
c.put(Thing(i));
}
// should succeed
c.insert(Thing(i));
}
2013-03-22 20:16:43 +05:30
TEST(CacheTests, multiple_gets_works)
2011-10-21 21:27:28 +05:30
{
unsigned const CACHE_SIZE = 16;
cache<ThingTraits> c(CACHE_SIZE);
unsigned i;
for (i = 0; i < CACHE_SIZE; i++) {
c.insert(Thing(i));
c.get(i);
c.get(i);
c.put(Thing(i));
}
2013-03-22 20:16:43 +05:30
ASSERT_THROW(c.insert(Thing(i)), runtime_error);
2011-10-21 21:27:28 +05:30
}
2013-03-22 20:16:43 +05:30
TEST(CacheTests, shared_ptr_cache_works)
2011-10-21 21:27:28 +05:30
{
unsigned const CACHE_SIZE = 16;
cache<SharedThingTraits> c(CACHE_SIZE);
for (unsigned i = 0; i < CACHE_SIZE; i++) {
c.insert(shared_ptr<Thing>(new Thing(i)));
optional<shared_ptr<Thing> > maybe_ptr = c.get(i);
2013-03-22 20:16:43 +05:30
ASSERT_TRUE(maybe_ptr);
ASSERT_THAT((*maybe_ptr)->key_, Eq(i));
2011-10-21 21:27:28 +05:30
}
}
//----------------------------------------------------------------