1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <gtest/gtest.h>
#include <cpp-utils/pointer/unique_ref.h>
#include "blockstore/implementations/caching/cache/Cache.h"
#include "testutils/MinimalKeyType.h"
#include "testutils/CopyableMovableValueType.h"
using namespace blockstore::caching;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using ::testing::Test;
//Test that Cache uses a move constructor for Value if possible
class CacheTest_MoveConstructor: public Test {
public:
CacheTest_MoveConstructor(): cache(make_unique_ref<Cache<MinimalKeyType, CopyableMovableValueType, 100>>("test")) {
CopyableMovableValueType::numCopyConstructorCalled = 0;
}
unique_ref<Cache<MinimalKeyType, CopyableMovableValueType, 100>> cache;
};
TEST_F(CacheTest_MoveConstructor, MoveIntoCache) {
cache->push(MinimalKeyType::create(0), CopyableMovableValueType(2));
const CopyableMovableValueType val = cache->pop(MinimalKeyType::create(0)).value();
val.value(); //Access it to avoid the compiler optimizing the assignment away
EXPECT_EQ(0, CopyableMovableValueType::numCopyConstructorCalled);
}
TEST_F(CacheTest_MoveConstructor, CopyIntoCache) {
const CopyableMovableValueType value(2);
cache->push(MinimalKeyType::create(0), value);
const CopyableMovableValueType val = cache->pop(MinimalKeyType::create(0)).value();
val.value(); //Access it to avoid the compiler optimizing the assignment away
EXPECT_EQ(1, CopyableMovableValueType::numCopyConstructorCalled);
}
|