summaryrefslogtreecommitdiff
path: root/libadhocutil/unittests/testLazyPointer.cpp
blob: 6c4ddcd9bcd1947396c2979c3098827f93b64c32 (plain)
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
#define BOOST_TEST_MODULE LazyPointer
#include <boost/test/unit_test.hpp>

#include <boost/bind.hpp>
#include "intrusivePtrBase.h"
#include "lazyPointer.h"

class Test : public IntrusivePtrBase {
	public:
		Test(int v) :
			val(v)
		{
		}
		const int val;
};

typedef LazyPointer<Test> TestLazyPointer;

static
TestLazyPointer::pointer_type
factory()
{
	return new Test(3);
}

static
TestLazyPointer::pointer_type
paramFactory(const std::string & str)
{
	return new Test(str.length());
}

BOOST_AUTO_TEST_CASE ( islazy )
{
	TestLazyPointer p(boost::bind(&factory));
	BOOST_REQUIRE_EQUAL(false, p.hasValue());
	Test * t = p.get();
	BOOST_REQUIRE(t);
	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 = boost::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(boost::bind(&paramFactory, "some string"));
	BOOST_REQUIRE_EQUAL(false, p.hasValue());
	BOOST_REQUIRE_EQUAL(11, (*p).val);
	BOOST_REQUIRE_EQUAL(true, p.hasValue());
}