blob: 020c58f61579dc907cc89f26c64140bb518314c1 (
plain) (
blame)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/util/ptrholder.h>
#include <vespa/vespalib/testkit/testapp.h>
using vespalib::PtrHolder;
class Test : public vespalib::TestApp
{
public:
void testEmpty();
void testSimple();
int Main() override;
};
struct Data
{
int ctorCnt;
int dtorCnt;
Data() : ctorCnt(0), dtorCnt(0) {}
};
class DataRef
{
private:
Data &_d;
DataRef(const DataRef &);
DataRef &operator=(const DataRef &);
public:
DataRef(Data &d) : _d(d) { ++d.ctorCnt; }
~DataRef() { ++_d.dtorCnt; }
int getCtorCnt() const { return _d.ctorCnt; }
int getDtorCnt() const { return _d.dtorCnt; }
};
typedef std::shared_ptr<DataRef> PT;
typedef PtrHolder<DataRef> HOLD;
void
Test::testEmpty()
{
HOLD hold;
EXPECT_TRUE(hold.get().get() == NULL);
EXPECT_TRUE(!hold.hasValue());
EXPECT_TRUE(!hold.hasNewValue());
EXPECT_TRUE(!hold.latch());
EXPECT_TRUE(hold.get().get() == NULL);
EXPECT_TRUE(!hold.hasValue());
EXPECT_TRUE(!hold.hasNewValue());
hold.set(NULL);
EXPECT_TRUE(!hold.hasValue());
EXPECT_TRUE(!hold.hasNewValue());
}
void
Test::testSimple()
{
Data data;
HOLD hold;
{
hold.set(new DataRef(data));
EXPECT_TRUE(hold.hasValue());
EXPECT_TRUE(!hold.hasNewValue());
EXPECT_TRUE(!hold.latch());
PT pt1(hold.get());
EXPECT_TRUE(pt1.get() == hold.get().get());
hold.set(new DataRef(data));
EXPECT_TRUE(pt1.get() == hold.get().get());
EXPECT_TRUE(hold.hasValue());
EXPECT_TRUE(hold.hasNewValue());
EXPECT_TRUE(hold.latch());
EXPECT_TRUE(hold.hasValue());
EXPECT_TRUE(!hold.hasNewValue());
EXPECT_TRUE(pt1.get() != hold.get().get());
EXPECT_TRUE(data.ctorCnt == 2);
EXPECT_TRUE(data.dtorCnt == 0);
}
EXPECT_TRUE(data.ctorCnt == 2);
EXPECT_TRUE(data.dtorCnt == 1);
hold.clear();
EXPECT_TRUE(data.ctorCnt == 2);
EXPECT_TRUE(data.dtorCnt == 2);
}
int
Test::Main()
{
TEST_INIT("ptrholder_test");
testEmpty();
testSimple();
TEST_DONE();
}
TEST_APPHOOK(Test)
|