summaryrefslogtreecommitdiff
path: root/test/test-collection.cpp
blob: 5c67a8c5530d25cf2b7974a31e225b122168d7f7 (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
#define BOOST_TEST_MODULE test_collection

#include <boost/test/unit_test.hpp>

#include <collection.h>
#include <memory>
#include <ptr.h>
#include <special_members.h>
#include <vector>

class Base {
public:
	Base() = default;
	virtual ~Base() = default;
	DEFAULT_MOVE_COPY(Base);

	virtual bool
	add()
	{
		total += 1;
		return false;
	}

	unsigned int total {0};
};

class Sub : public Base {
public:
	bool
	add() override
	{
		total += 2;
		return true;
	}
};

using TestCollection = SharedCollection<Base, Sub>;

BOOST_TEST_DONT_PRINT_LOG_VALUE(TestCollection::Objects::const_iterator)
BOOST_TEST_DONT_PRINT_LOG_VALUE(TestCollection::Objects::const_reverse_iterator)

BOOST_FIXTURE_TEST_SUITE(tc, TestCollection)

BOOST_AUTO_TEST_CASE(empty)
{
	BOOST_REQUIRE(!apply(&Base::add));
	const auto i = applyOne(&Base::add);
	BOOST_CHECK_EQUAL(i, end());
}

BOOST_AUTO_TEST_CASE(a_base)
{
	auto b = create<Base>();
	BOOST_CHECK_EQUAL(objects.size(), 1);
	BOOST_CHECK(std::get<OtherObjects<Sub>>(otherObjects).empty());
	BOOST_REQUIRE(apply(&Base::add));
	BOOST_CHECK_EQUAL(b->total, 1);
	const auto i = applyOne(&Base::add);
	BOOST_CHECK_EQUAL(i, end());
}

BOOST_AUTO_TEST_CASE(emplace_others)
{
	emplace(std::make_shared<Base>());
	BOOST_CHECK_EQUAL(objects.size(), 1);
	BOOST_CHECK(std::get<OtherObjects<Sub>>(otherObjects).empty());
	emplace(std::make_shared<Sub>());
	BOOST_CHECK_EQUAL(objects.size(), 2);
	BOOST_CHECK_EQUAL(std::get<OtherObjects<Sub>>(otherObjects).size(), 1);
}

BOOST_AUTO_TEST_CASE(a_rbase)
{
	auto b = create<Base>();
	BOOST_REQUIRE(rapply(&Base::add));
	BOOST_CHECK_EQUAL(b->total, 1);
	const auto i = rapplyOne(&Base::add);
	BOOST_CHECK_EQUAL(i, rend());
}

BOOST_AUTO_TEST_CASE(a_sub)
{
	auto s = create<Sub>();
	BOOST_CHECK_EQUAL(objects.size(), 1);
	BOOST_CHECK_EQUAL(std::get<OtherObjects<Sub>>(otherObjects).size(), 1);
	BOOST_REQUIRE(apply(&Base::add));
	BOOST_CHECK_EQUAL(s->total, 2);
	const auto i = applyOne(&Base::add);
	BOOST_CHECK_NE(i, end());
	BOOST_CHECK_EQUAL(*i, s);
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_CASE(wrapped_ptr_file_cons)
{
	using FilePtr = wrapped_ptr<FILE, &fclose>;
	const FilePtr fp {fopen, "/dev/null", "r"};
	BOOST_REQUIRE(fp);
	BOOST_CHECK_NO_THROW(fflush(fp));

	BOOST_CHECK_EQUAL(fp.get(), fp.operator->());
	BOOST_CHECK_EQUAL(fp.get(), fp.operator FILE *());
}

BOOST_AUTO_TEST_CASE(wrapped_ptr_file_move)
{
	using FilePtr = wrapped_ptr<FILE, &fclose>;
	FilePtr fp {fopen, "/dev/null", "r"};
	BOOST_REQUIRE(fp);

	FilePtr fp2 {std::move(fp)};
	BOOST_REQUIRE(!fp);
	BOOST_REQUIRE(fp2);

	fp = std::move(fp2);
	BOOST_REQUIRE(fp);
	BOOST_REQUIRE(!fp2);

	FilePtr fp3 {fopen, "/dev/null", "r"};
	fp = std::move(fp3);
}

BOOST_AUTO_TEST_CASE(wrapped_ptr_file_typed)
{
	using FilePtr = wrapped_ptrt<FILE, &fopen, &fclose>;
	const FilePtr fp {"/dev/null", "r"};
	BOOST_REQUIRE(fp);
	BOOST_CHECK_NO_THROW(fflush(fp));
}