ed minetest for gpcfs purposes
aboutsummaryrefslogtreecommitdiff
path: root/src/unittest/test_irrptr.cpp
blob: 2fb7cfcd617a70bc5a9d106658cda95072fe996a (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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Minetest
Copyright (C) 2018 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "test.h"

#include "exceptions.h"
#include "irr_ptr.h"

class TestIrrPtr : public TestBase
{
public:
	TestIrrPtr() { TestManager::registerTestModule(this); }
	const char *getName() { return "TestIrrPtr"; }

	void runTests(IGameDef *gamedef);

	void testRefCounting();
	void testSelfAssignment();
	void testNullHandling();
};

static TestIrrPtr g_test_instance;

void TestIrrPtr::runTests(IGameDef *gamedef)
{
	TEST(testRefCounting);
	TEST(testSelfAssignment);
	TEST(testNullHandling);
}

////////////////////////////////////////////////////////////////////////////////

#define UASSERT_REFERENCE_COUNT(object, value, info)                                     \
	UTEST((object)->getReferenceCount() == value,                                    \
			info "Reference count is %d instead of " #value,                 \
			(object)->getReferenceCount())

void TestIrrPtr::testRefCounting()
{
	IReferenceCounted *obj = new IReferenceCounted(); // RC=1
	obj->grab();
	UASSERT_REFERENCE_COUNT(obj, 2, "Pre-condition failed: ");
	{
		irr_ptr<IReferenceCounted> p1{obj}; // move semantics
		UASSERT(p1.get() == obj);
		UASSERT_REFERENCE_COUNT(obj, 2, );

		irr_ptr<IReferenceCounted> p2{p1}; // copy ctor
		UASSERT(p1.get() == obj);
		UASSERT(p2.get() == obj);
		UASSERT_REFERENCE_COUNT(obj, 3, );

		irr_ptr<IReferenceCounted> p3{std::move(p1)}; // move ctor
		UASSERT(p1.get() == nullptr);
		UASSERT(p3.get() == obj);
		UASSERT_REFERENCE_COUNT(obj, 3, );

		p1 = std::move(p2); // move assignment
		UASSERT(p1.get() == obj);
		UASSERT(p2.get() == nullptr);
		UASSERT_REFERENCE_COUNT(obj, 3, );

		p2 = p3; // copy assignment
		UASSERT(p2.get() == obj);
		UASSERT(p3.get() == obj);
		UASSERT_REFERENCE_COUNT(obj, 4, );

		p1.release();
		UASSERT(p1.get() == nullptr);
		UASSERT_REFERENCE_COUNT(obj, 4, );
	}
	UASSERT_REFERENCE_COUNT(obj, 2, );
	obj->drop();
	UTEST(obj->drop(), "Dropping failed: reference count is %d",
			obj->getReferenceCount());
}

#if defined(__clang__)
	#pragma GCC diagnostic push
	#if __clang_major__ >= 7
		#pragma GCC diagnostic ignored "-Wself-assign-overloaded"
	#endif
	#pragma GCC diagnostic ignored "-Wself-move"
#endif

void TestIrrPtr::testSelfAssignment()
{
	irr_ptr<IReferenceCounted> p1{new IReferenceCounted()};
	UASSERT(p1);
	UASSERT_REFERENCE_COUNT(p1, 1, );
	p1 = p1;
	UASSERT(p1);
	UASSERT_REFERENCE_COUNT(p1, 1, );
	p1 = std::move(p1);
	UASSERT(p1);
	UASSERT_REFERENCE_COUNT(p1, 1, );
}

void TestIrrPtr::testNullHandling()
{
	// In the case of an error, it will probably crash with SEGV.
	// Nevertheless, UASSERTs are used to catch possible corner cases.
	irr_ptr<IReferenceCounted> p1{new IReferenceCounted()};
	UASSERT(p1);
	irr_ptr<IReferenceCounted> p2;
	UASSERT(!p2);
	irr_ptr<IReferenceCounted> p3{p2};
	UASSERT(!p2);
	UASSERT(!p3);
	irr_ptr<IReferenceCounted> p4{std::move(p2)};
	UASSERT(!p2);
	UASSERT(!p4);
	p2 = p2;
	UASSERT(!p2);
	p2 = std::move(p2);
	UASSERT(!p2);
	p3 = p2;
	UASSERT(!p2);
	UASSERT(!p3);
	p3 = std::move(p2);
	UASSERT(!p2);
	UASSERT(!p3);
}

#if defined(__clang__)
	#pragma GCC diagnostic pop
#endif