summaryrefslogtreecommitdiff
path: root/test/test-lib.cpp
blob: 7672acc0c9181e9ba2860126092af20909d1ffda (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
#define BOOST_TEST_MODULE test_lib

#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>
#include <stream_support.h>

#include <collections.h>
#include <glArrays.h>
#include <glad/gl.h>
#include <set>

std::set<GLuint> active;

void
generator(GLsizei n, GLuint * out)
{
	static GLuint next {1};

	while (n--) {
		active.insert(next);
		*out++ = next++;
	}
}

void
deleter(GLsizei n, GLuint * in)
{
	BOOST_CHECK(std::all_of(in, in + n, [](GLuint id) {
		return active.erase(id) > 0;
	}));
}

using TestArray = glArrays<5, &generator, &deleter>;

BOOST_AUTO_TEST_CASE(generate_and_delete)
{
	{
		const TestArray a;
	}
	BOOST_CHECK(active.empty());
}

BOOST_AUTO_TEST_CASE(generate_move_and_delete)
{
	{
		TestArray a;
		BOOST_CHECK_EQUAL(TestArray::size, active.size());
		const TestArray b {std::move(a)};
		BOOST_CHECK_EQUAL(TestArray::size, active.size());
	}
	BOOST_CHECK(active.empty());
}

constexpr std::array TRIANGLE_STRIP_IN {0, 1, 2, 3, 4, 5};
static_assert(std::distance(strip_begin(TRIANGLE_STRIP_IN), strip_end(TRIANGLE_STRIP_IN)) == 4);

BOOST_AUTO_TEST_CASE(triangle_strip_iter)
{
	constexpr std::array TRIANGLE_STRIP_EXPECTED {0, 1, 2, 2, 1, 3, 2, 3, 4, 4, 3, 5};

	std::vector<int> out;
	std::for_each(strip_begin(TRIANGLE_STRIP_IN), strip_end(TRIANGLE_STRIP_IN), [&out](const auto & t) {
		const auto [a, b, c] = t;
		out.push_back(a);
		out.push_back(b);
		out.push_back(c);
	});
	BOOST_REQUIRE_EQUAL(out.size(), (TRIANGLE_STRIP_IN.size() - 2) * 3);
	BOOST_CHECK_EQUAL_COLLECTIONS(
			out.begin(), out.end(), TRIANGLE_STRIP_EXPECTED.begin(), TRIANGLE_STRIP_EXPECTED.end());
}

using MergeCloseData = std::tuple<std::vector<int>, int, std::vector<int>>;

BOOST_DATA_TEST_CASE(mergeCloseInts,
		boost::unit_test::data::make<MergeCloseData>({
				{{0}, 0, {0}},
				{{0, 1}, 0, {0, 1}},
				{{0, 1}, 2, {0, 1}},
				{{0, 1, 2}, 2, {0, 2}},
				{{0, 1, 4}, 2, {0, 4}},
				{{0, 1, 2}, 4, {0, 2}},
				{{0, 4, 8}, 4, {0, 8}},
				{{0, 4, 10, 14}, 4, {0, 14}},
				{{0, 3, 6}, 2, {0, 3, 6}},
				{{0, 3, 4}, 2, {0, 4}},
				{{0, 5, 7, 12}, 4, {0, 6, 12}},
				{{0, 3, 4, 5, 10, 17, 18, 19}, 2, {0, 4, 10, 19}},
		}),
		collection, tolerance, expected)
{
	auto mutableCollection {collection};
	BOOST_REQUIRE_NO_THROW((mergeClose(
			mutableCollection,
			[](int left, int right) {
				return std::abs(left - right);
			},
			[](int left, int right) {
				return (left + right) / 2;
			},
			tolerance)));
	BOOST_CHECK_EQUAL_COLLECTIONS(mutableCollection.begin(), mutableCollection.end(), expected.begin(), expected.end());
}