This source file includes following definitions.
- TEST
- TEST
- TEST
#include <vector>
#include "base/scoped_generic.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
struct IntTraits {
IntTraits(std::vector<int>* freed) : freed_ints(freed) {}
static int InvalidValue() {
return -1;
}
void Free(int value) {
freed_ints->push_back(value);
}
std::vector<int>* freed_ints;
};
typedef ScopedGeneric<int, IntTraits> ScopedInt;
}
TEST(ScopedGenericTest, ScopedGeneric) {
std::vector<int> values_freed;
IntTraits traits(&values_freed);
{
ScopedInt a(IntTraits::InvalidValue(), traits);
}
EXPECT_TRUE(values_freed.empty());
static const int kFirst = 0;
{
ScopedInt a(kFirst, traits);
}
ASSERT_EQ(1u, values_freed.size());
ASSERT_EQ(kFirst, values_freed[0]);
values_freed.clear();
{
ScopedInt a(kFirst, traits);
EXPECT_EQ(kFirst, a.release());
ScopedInt b(IntTraits::InvalidValue(), traits);
EXPECT_EQ(IntTraits::InvalidValue(), b.release());
}
ASSERT_TRUE(values_freed.empty());
static const int kSecond = 1;
{
ScopedInt b(kFirst, traits);
b.reset(kSecond);
ASSERT_EQ(1u, values_freed.size());
ASSERT_EQ(kFirst, values_freed[0]);
}
ASSERT_EQ(2u, values_freed.size());
ASSERT_EQ(kSecond, values_freed[1]);
values_freed.clear();
{
ScopedInt a(kFirst, traits);
ScopedInt b(kSecond, traits);
a.swap(b);
EXPECT_TRUE(values_freed.empty());
EXPECT_EQ(kSecond, a.get());
EXPECT_EQ(kFirst, b.get());
}
ASSERT_EQ(2u, values_freed.size());
EXPECT_EQ(kFirst, values_freed[0]);
EXPECT_EQ(kSecond, values_freed[1]);
values_freed.clear();
{
ScopedInt a(kFirst, traits);
ScopedInt b(a.Pass());
EXPECT_TRUE(values_freed.empty());
ASSERT_EQ(IntTraits::InvalidValue(), a.get());
ASSERT_EQ(kFirst, b.get());
}
ASSERT_EQ(1u, values_freed.size());
ASSERT_EQ(kFirst, values_freed[0]);
}
TEST(ScopedGenericTest, Operators) {
std::vector<int> values_freed;
IntTraits traits(&values_freed);
static const int kFirst = 0;
static const int kSecond = 1;
{
ScopedInt a(kFirst, traits);
EXPECT_TRUE(a == kFirst);
EXPECT_FALSE(a != kFirst);
EXPECT_FALSE(a == kSecond);
EXPECT_TRUE(a != kSecond);
EXPECT_TRUE(kFirst == a);
EXPECT_FALSE(kFirst != a);
EXPECT_FALSE(kSecond == a);
EXPECT_TRUE(kSecond != a);
}
{
ScopedInt a(kFirst, traits);
EXPECT_TRUE(a.is_valid());
a.reset();
EXPECT_FALSE(a.is_valid());
}
}
#if 0
TEST(ScopedGenericTest, NoCompile) {
}
#endif
}