OXYGENHUB
◆ main openmsx / src / unittest / VectorPathDsl_test.cc
5.5 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "catch.hpp"
2
#include "VectorPathDsl.hh"
3
#include "VectorPath.hh"
4
5
#include <array>
6
#include <cstdint>
7
#include <span>
8
9
using namespace VectorPath;
10
using namespace VectorPath::dsl;
11
12
template<size_t N>
13
static void checkEqualBytes(std::span<const uint8_t> actual,
14
                            const std::array<uint8_t, N>& expected)
15
{
16
	CHECK(actual.size() == N);
17
	for (size_t i = 0; i < N; ++i) {
18
		CHECK(actual[i] == expected[i]);
19
	}
20
}
21
22
23
TEST_CASE("path_single_line_int16")
24
{
25
	static constexpr auto blob = path<int16_t>(
26
		from(10, 20),
27
		line(30, 40));
28
29
	static constexpr std::array<uint8_t, 10> expected = {
30
		1,                   // segment count
31
		10, 0, 20, 0,        // anchor
32
		0,                   // flags: line
33
		30, 0, 40, 0,        // line endpoint
34
	};
35
	checkEqualBytes(std::span(blob), expected);
36
37
	ByteReader reader(blob);
38
	CHECK(reader.get<uint8_t>() ==  1); // segment count
39
	CHECK(reader.get<int16_t>() == 10); // anchor x
40
	CHECK(reader.get<int16_t>() == 20); // anchor y
41
	CHECK(reader.get<uint8_t>() ==  0); // segment flags
42
	CHECK(reader.get<int16_t>() == 30); // line x
43
	CHECK(reader.get<int16_t>() == 40); // line y
44
	CHECK(reader.empty());              // fully consumed
45
}
46
47
TEST_CASE("path_cubic_sets_flag_bit")
48
{
49
	static constexpr auto blob = path<int16_t>(
50
		from(0, 0),
51
		curve(1, 2, 3, 4, 5, 6));
52
53
	ByteReader reader(blob);
54
	CHECK(reader.get<uint8_t>() == 1); // segment count
55
	CHECK(reader.get<int16_t>() == 0); // anchor x
56
	CHECK(reader.get<int16_t>() == 0); // anchor y
57
	CHECK(reader.get<uint8_t>() == 1); // cubic flag bit 0
58
	CHECK(reader.get<int16_t>() == 1); // cx1
59
	CHECK(reader.get<int16_t>() == 2); // cy1
60
	CHECK(reader.get<int16_t>() == 3); // cx2
61
	CHECK(reader.get<int16_t>() == 4); // cy2
62
	CHECK(reader.get<int16_t>() == 5); // x
63
	CHECK(reader.get<int16_t>() == 6); // y
64
	CHECK(reader.empty());             // fully consumed
65
}
66
67
TEST_CASE("path_mixed_segments_and_second_flag_byte")
68
{
69
	static constexpr auto blob = path<int16_t>(
70
		from(0, 0),
71
		line(1, 1), line(2, 2), line(3, 3), line(4, 4),
72
		line(5, 5), line(6, 6), line(7, 7), line(8, 8),
73
		curve(9, 9, 10, 10, 11, 11));
74
75
	CHECK(blob.size() == 1 + 4 + 1 + 8 * 4 + 1 + 12); // nine-segment path size
76
77
	ByteReader reader(blob);
78
	CHECK(reader.get<uint8_t>() == 9); // segment count
79
	(void)reader.get<int16_t>();
80
	(void)reader.get<int16_t>(); // anchor
81
	CHECK(reader.get<uint8_t>() == 0); // first flag byte: all lines
82
	for (int i = 0; i < 8; ++i) {
83
		(void)reader.get<int16_t>();
84
		(void)reader.get<int16_t>();
85
	}
86
	CHECK(reader.get<uint8_t>() == 1); // second flag byte: cubic
87
	CHECK(reader.get<int16_t>() == 9); // curve start
88
	CHECK(reader.empty() == false); // curve not finished
89
}
90
91
TEST_CASE("path_int8_coordinates")
92
{
93
	static constexpr auto blob = path<int8_t>(
94
		from(1, 2),
95
		line(3, 4));
96
97
	CHECK(blob.size() == 1 + 2 + 1 + 2); // int8 path size
98
99
	ByteReader reader(blob);
100
	CHECK(reader.get<uint8_t>() == 1); // segment count
101
	CHECK(reader.get<int8_t>()  == 1); // anchor x
102
	CHECK(reader.get<int8_t>()  == 2); // anchor y
103
	CHECK(reader.get<uint8_t>() == 0); // flags
104
	CHECK(reader.get<int8_t>()  == 3); // line x
105
	CHECK(reader.get<int8_t>()  == 4); // line y
106
	CHECK(reader.empty());             // fully consumed
107
}
108
109
TEST_CASE("stroke_collection_open_and_closed")
110
{
111
	static constexpr auto blob = strokeCollection<int16_t>(
112
		openPath(from(0, 0), line(1, 1)),
113
		closedPath(from(2, 2), line(3, 3), line(4, 4)));
114
115
	static constexpr size_t path0Size = 1 + 4 + 1 + 4;
116
	static constexpr size_t path1Size = 1 + 4 + 1 + 4 + 4;
117
	CHECK(blob.size() == 1 + 1 + path0Size + path1Size); // collection size
118
119
	ByteReader reader(blob);
120
	CHECK(reader.get<uint8_t>() == 2); // path count
121
	CHECK(reader.get<uint8_t>() == 2); // open=0, closed=1 -> bit 1 set
122
123
	// path 0
124
	CHECK(reader.get<uint8_t>() == 1); // path0 segment count
125
	CHECK(reader.get<int16_t>() == 0); // path0 anchor x
126
	CHECK(reader.get<int16_t>() == 0); // path0 anchor y
127
	CHECK(reader.get<uint8_t>() == 0); // path0 flags
128
	CHECK(reader.get<int16_t>() == 1); // path0 line x
129
	CHECK(reader.get<int16_t>() == 1); // path0 line y
130
131
	// path 1
132
	CHECK(reader.get<uint8_t>() == 2); // path1 segment count
133
	CHECK(reader.get<int16_t>() == 2); // path1 anchor x
134
	CHECK(reader.get<int16_t>() == 2); // path1 anchor y
135
	CHECK(reader.get<uint8_t>() == 0); // path1 flags
136
	CHECK(reader.get<int16_t>() == 3); // path1 line1 x
137
	CHECK(reader.get<int16_t>() == 3); // path1 line1 y
138
	CHECK(reader.get<int16_t>() == 4); // path1 line2 x
139
	CHECK(reader.get<int16_t>() == 4); // path1 line2 y
140
	CHECK(reader.empty());             // collection fully consumed
141
}
142
143
TEST_CASE("stroke_collection_nine_paths_second_stroke_flag_byte")
144
{
145
	static constexpr auto blob = strokeCollection<int16_t>(
146
		openPath(from(0, 0), line(1, 1)),
147
		openPath(from(0, 0), line(1, 1)),
148
		openPath(from(0, 0), line(1, 1)),
149
		openPath(from(0, 0), line(1, 1)),
150
		openPath(from(0, 0), line(1, 1)),
151
		openPath(from(0, 0), line(1, 1)),
152
		openPath(from(0, 0), line(1, 1)),
153
		openPath(from(0, 0), line(1, 1)),
154
		closedPath(from(0, 0), line(1, 1)));
155
156
	constexpr size_t pathSize = 1 + 4 + 1 + 4;
157
	CHECK(blob.size() == 1 + 2 + 9 * pathSize); // nine-path collection size
158
159
	ByteReader reader(blob);
160
	CHECK(reader.get<uint8_t>() == 9); // path count
161
	CHECK(reader.get<uint8_t>() == 0); // first stroke-flag byte
162
	CHECK(reader.get<uint8_t>() == 1); // second stroke-flag byte: path 8 closed
163
}
164
165
TEST_CASE("compile_time_path_sizes")
166
{
167
	static constexpr auto linePath = path<int16_t>(from(0, 0), line(1, 1));
168
	static constexpr auto curvePath = path<int16_t>(
169
		from(0, 0), curve(1, 2, 3, 4, 5, 6));
170
	static_assert(linePath.size() == 10);
171
	static_assert(curvePath.size() == 1 + 4 + 1 + 12);
172
}