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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#define BOOST_TEST_MODULE LazyPointer
#include <boost/test/unit_test.hpp>
#include "lazyPointer.h"
using namespace AdHoc;
class Test {
public:
explicit Test(int v) :
val(v)
{
}
const int val;
};
using TestLazyPointer = LazyPointer<Test>;
using RawLazyPointer = LazyPointer<int, int *>;
static
TestLazyPointer::pointer_type
factory()
{
return std::make_shared<Test>(3);
}
static
TestLazyPointer::pointer_type
paramFactory(const std::string & str)
{
return std::make_shared<Test>(str.length());
}
BOOST_AUTO_TEST_CASE ( islazy )
{
TestLazyPointer p(std::bind(&factory));
BOOST_REQUIRE_EQUAL(false, p.hasValue());
Test * t = p.get();
BOOST_REQUIRE(t);
bool pbool = p;
BOOST_REQUIRE(pbool);
BOOST_REQUIRE_EQUAL(true, p.hasValue());
BOOST_REQUIRE_EQUAL(p, t);
BOOST_REQUIRE_EQUAL(3, t->val);
BOOST_REQUIRE_EQUAL(3, p->val);
}
BOOST_AUTO_TEST_CASE ( preinit )
{
Test * t = new Test(4);
TestLazyPointer p(t);
BOOST_REQUIRE_EQUAL(true, p.hasValue());
BOOST_REQUIRE_EQUAL(p, t);
BOOST_REQUIRE_EQUAL(4, p->val);
}
BOOST_AUTO_TEST_CASE ( reset )
{
Test * t = new Test(4);
TestLazyPointer p(t);
BOOST_REQUIRE_EQUAL(true, p.hasValue());
BOOST_REQUIRE_EQUAL(4, p->val);
p = nullptr;
BOOST_REQUIRE_EQUAL(true, p.hasValue());
BOOST_REQUIRE_EQUAL(true, !p);
p = std::bind(&factory);
BOOST_REQUIRE_EQUAL(false, p.hasValue());
p.get();
BOOST_REQUIRE_EQUAL(true, p.hasValue());
BOOST_REQUIRE_EQUAL(3, p->val);
}
BOOST_AUTO_TEST_CASE ( nondefault )
{
TestLazyPointer p(std::bind(¶mFactory, "some string"));
BOOST_REQUIRE_EQUAL(false, p.hasValue());
BOOST_REQUIRE_EQUAL(11, (*p).val);
BOOST_REQUIRE_EQUAL(true, p.hasValue());
}
BOOST_AUTO_TEST_CASE( rawPointerNull )
{
RawLazyPointer null;
BOOST_REQUIRE(null.hasValue());
BOOST_REQUIRE(!null);
BOOST_REQUIRE(!null.get());
}
BOOST_AUTO_TEST_CASE( rawPointerNonNull )
{
RawLazyPointer value(new int(3));
BOOST_REQUIRE(value.hasValue());
BOOST_REQUIRE(value);
BOOST_REQUIRE(value.get());
BOOST_REQUIRE_EQUAL(*value, 3);
int * x = value;
BOOST_REQUIRE_EQUAL(*x, 3);
delete value;
}
int *
rawFactory(const std::string & s)
{
return new int(s.length());
}
BOOST_AUTO_TEST_CASE( rawPointerFactory )
{
RawLazyPointer value(std::bind(&rawFactory, std::string("four")));
BOOST_REQUIRE(!value.hasValue());
BOOST_REQUIRE_EQUAL(*value, 4);
BOOST_REQUIRE(value.hasValue());
delete value;
}
|