libcryfs/test/implementations/caching/cache/CacheTest_MoveConstructor.cpp

36 lines
1.4 KiB
C++
Raw Normal View History

2015-04-28 11:56:07 +02:00
#include <google/gtest/gtest.h>
#include <messmer/cpp-utils/pointer/unique_ref.h>
2015-04-28 11:56:07 +02:00
#include "../../../../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;
2015-04-28 11:56:07 +02:00
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>>()) {
2015-04-28 11:56:07 +02:00
CopyableMovableValueType::numCopyConstructorCalled = 0;
}
unique_ref<Cache<MinimalKeyType, CopyableMovableValueType>> cache;
2015-04-28 11:56:07 +02:00
};
TEST_F(CacheTest_MoveConstructor, MoveIntoCache) {
cache->push(MinimalKeyType::create(0), CopyableMovableValueType(2));
CopyableMovableValueType val = cache->pop(MinimalKeyType::create(0)).value();
val.value(); //Access it to avoid the compiler optimizing the assignment away
2015-04-28 11:56:07 +02:00
EXPECT_EQ(0, CopyableMovableValueType::numCopyConstructorCalled);
}
TEST_F(CacheTest_MoveConstructor, CopyIntoCache) {
CopyableMovableValueType value(2);
cache->push(MinimalKeyType::create(0), value);
CopyableMovableValueType val = cache->pop(MinimalKeyType::create(0)).value();
val.value(); //Access it to avoid the compiler optimizing the assignment away
2015-04-28 11:56:07 +02:00
EXPECT_EQ(1, CopyableMovableValueType::numCopyConstructorCalled);
}