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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2010-2014 Baidu.com, Inc. All Rights Reserved
//
// Author: Ge,Jun (gejun@baidu.com)
// Date: 2010-12-04 11:59
#include <gtest/gtest.h>
#include "base/unique_ptr.h"
namespace {
class UniquePtrTest : public testing::Test {
protected:
virtual void SetUp() {
};
};
struct Foo {
Foo() : destroyed(false), called_func(false) {}
void destroy() { destroyed = true; }
void func() { called_func = true; }
bool destroyed;
bool called_func;
};
struct FooDeleter {
void operator()(Foo* f) const {
f->destroy();
}
};
TEST_F(UniquePtrTest, basic) {
Foo foo;
ASSERT_FALSE(foo.destroyed);
ASSERT_FALSE(foo.called_func);
{
std::unique_ptr<Foo, FooDeleter> foo_ptr(&foo);
foo_ptr->func();
ASSERT_TRUE(foo.called_func);
}
ASSERT_TRUE(foo.destroyed);
}
static std::unique_ptr<Foo> generate_foo(Foo* foo) {
std::unique_ptr<Foo> foo_ptr(foo);
return foo_ptr;
}
TEST_F(UniquePtrTest, return_unique_ptr) {
Foo* foo = new Foo;
std::unique_ptr<Foo> foo_ptr = generate_foo(foo);
ASSERT_EQ(foo, foo_ptr.get());
}
}