-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathUnitTypesTest.cpp
More file actions
244 lines (202 loc) · 8.45 KB
/
UnitTypesTest.cpp
File metadata and controls
244 lines (202 loc) · 8.45 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/******************************************************************************
Copyright 2021 Evgeny Gorodetskiy
Licensed under the Apache License, Version 2.0 (the "License"),
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*******************************************************************************
FILE: Tests/UserInterface/Types/UnitTypesTest.cpp
Unit-tests of the Unit Types
******************************************************************************/
#include "UnitTypeCatchHelpers.hpp"
#include <Methane/Data/TypeTraits.hpp>
#include <catch2/catch_template_test_macros.hpp>
using namespace Methane;
using namespace Methane::UserInterface;
TEMPLATE_TEST_CASE("Unit Type Initialization", "[unit][type][init]", ALL_BASE_TYPES)
{
SECTION("Default constructor initialization")
{
CheckUnitType(UnitType<TestType>(), TestType(), Units::Pixels);
}
SECTION("Initialize with a original type reference")
{
const TestType test_item = CreateTestItem<TestType>();
CheckUnitType(UnitType<TestType>(test_item), test_item, Units::Pixels);
}
SECTION("Initialize with units and original type reference")
{
const TestType test_item = CreateTestItem<TestType>();
CheckUnitType(UnitType<TestType>(Units::Dots, test_item), test_item, Units::Dots);
}
SECTION("Initialize with units and original type move")
{
const TestType test_item = CreateTestItem<TestType>();
TestType copy_item(test_item);
CheckUnitType(UnitType<TestType>(Units::Dots, std::move(copy_item)), test_item, Units::Dots);
}
SECTION("Initialize with units and original type construction arguments")
{
CheckUnitType(CreateUnitItem<TestType>(Units::Dots), CreateTestItem<TestType>(), Units::Dots);
}
if constexpr (std::is_same_v<FramePoint, TestType>)
{
SECTION("Initialize frame point with frame size")
{
const UnitSize test_size = CreateUnitItem<FrameSize>(Units::Dots);
CheckUnitType(UnitType<FramePoint>(test_size), FramePoint(test_size.GetWidth(), test_size.GetHeight()), Units::Dots);
}
}
}
TEMPLATE_TEST_CASE("Unit Type Conversions", "[unit][type][convert]", ALL_BASE_TYPES)
{
SECTION("Convert to base type reference")
{
const TestType base_item = CreateTestItem<TestType>();
UnitType<TestType> unit_item = CreateUnitItem<TestType>(Units::Dots);
CHECK(unit_item.AsBase() == base_item);
}
SECTION("Convert to base type const reference")
{
const TestType base_item = CreateTestItem<TestType>();
const UnitType<TestType> unit_item = CreateUnitItem<TestType>(Units::Dots);
CHECK(unit_item.AsBase() == base_item);
}
SECTION("Convert Pixels type to string")
{
const UnitType<TestType> unit_item = CreateUnitItem<TestType>(Units::Pixels);
const std::string unit_str = static_cast<std::string>(unit_item.AsBase()) + " in pixels";
CHECK(static_cast<std::string>(unit_item) == unit_str);
}
SECTION("Convert Dots type to string")
{
const UnitType<TestType> unit_item = CreateUnitItem<TestType>(Units::Dots);
const std::string unit_str = static_cast<std::string>(unit_item.AsBase()) + " in dots";
CHECK(static_cast<std::string>(unit_item) == unit_str);
}
if constexpr (TypeTraits<TestType>::type_of == TypeOf::Rect)
{
using CoordType = typename TestType::CoordinateType;
using DimType = typename TestType::DimensionType;
SECTION("Convert to unit origin")
{
const auto unit_point = CreateUnitItem<Data::Point2T<CoordType>>(Units::Dots);
const UnitType<TestType> unit_rect = CreateUnitItem<TestType>(Units::Dots);
CHECK(unit_rect.GetUnitOrigin() == unit_point);
}
SECTION("Convert to unit size")
{
const auto unit_size = CreateUnitItem<Data::RectSize<DimType>>(Units::Dots);
const UnitType<TestType> unit_rect = CreateUnitItem<TestType>(Units::Dots);
CHECK(unit_rect.GetUnitSize() == unit_size);
}
}
}
TEMPLATE_TEST_CASE("Unit Types Comparison", "[unit][type][compare]", ALL_BASE_TYPES)
{
const UnitType<TestType> dot_item_a = CreateUnitItem<TestType>(Units::Dots);
const UnitType<TestType> pix_item_a = CreateUnitItem<TestType>(Units::Pixels);
const UnitType<TestType> dot_item_b = CreateUnitItem<TestType>(Units::Dots, 2);
SECTION("Equality")
{
CHECK(dot_item_a == CreateUnitItem<TestType>(Units::Dots));
CHECK_FALSE(dot_item_a == dot_item_b);
CHECK_FALSE(dot_item_a == pix_item_a);
}
SECTION("Inequality")
{
CHECK_FALSE(dot_item_a != CreateUnitItem<TestType>(Units::Dots));
CHECK(dot_item_a != dot_item_b);
CHECK(dot_item_a != pix_item_a);
}
if constexpr (TypeTraits<TestType>::type_of == TypeOf::Point ||
TypeTraits<TestType>::type_of == TypeOf::RectSize)
{
const UnitType<TestType> pix_item_b = CreateUnitItem<TestType>(Units::Pixels, 2);
SECTION("Less")
{
CHECK(dot_item_a < dot_item_b);
CHECK(pix_item_a < pix_item_b);
CHECK_THROWS(dot_item_a < pix_item_b);
}
SECTION("Less or equal")
{
CHECK(dot_item_a <= dot_item_b);
CHECK(pix_item_a <= pix_item_b);
CHECK_THROWS(dot_item_a <= pix_item_b);
}
SECTION("Greater")
{
CHECK(dot_item_b > dot_item_a);
CHECK(pix_item_b > pix_item_a);
CHECK_THROWS(dot_item_b > pix_item_a);
}
SECTION("Greater or equal")
{
CHECK(dot_item_b >= dot_item_a);
CHECK(pix_item_b >= pix_item_a);
CHECK_THROWS(dot_item_b >= pix_item_a);
}
}
}
TEMPLATE_TEST_CASE("Unit Type Math Operations", "[unit][type][math]", ALL_BASE_TYPES)
{
const UnitType<TestType> test_item_1dt = CreateUnitItem<TestType>(Units::Dots, 1);
const UnitType<TestType> test_item_2dt = CreateUnitItem<TestType>(Units::Dots, 2);
SECTION("Multiplication by scalar")
{
CHECK(test_item_1dt * 2 == CreateUnitItem<TestType>(Units::Dots, 2));
}
SECTION("Division by scalar")
{
CHECK(test_item_2dt / 2 == CreateUnitItem<TestType>(Units::Dots));
}
SECTION("Inplace multiplication by scalar")
{
UnitType<TestType> test_item = test_item_1dt;
test_item *= 2;
CHECK(test_item == CreateUnitItem<TestType>(Units::Dots, 2));
}
SECTION("Inplace division by scalar")
{
UnitType<TestType> test_item = test_item_2dt;
test_item /= 2;
CHECK(test_item == CreateUnitItem<TestType>(Units::Dots));
}
if constexpr (TypeTraits<TestType>::type_of == TypeOf::Point ||
TypeTraits<TestType>::type_of == TypeOf::RectSize)
{
const UnitType<TestType> test_item_1px = CreateUnitItem<TestType>(Units::Pixels, 1);
const UnitType<TestType> test_item_2px = CreateUnitItem<TestType>(Units::Pixels, 2);
const UnitType<TestType> test_item_3px = CreateUnitItem<TestType>(Units::Pixels, 3);
SECTION("Addition")
{
CHECK(test_item_1px + test_item_2px == test_item_3px);
CHECK_THROWS(test_item_1px + test_item_2dt);
}
SECTION("Subtraction")
{
CHECK(test_item_3px - test_item_1px == test_item_2px);
CHECK_THROWS(test_item_2dt - test_item_1px);
}
SECTION("Inplace Addition")
{
UnitType<TestType> test_item_px = CreateUnitItem<TestType>(Units::Pixels, 1);
test_item_px += test_item_2px;
CHECK(test_item_px == test_item_3px);
CHECK_THROWS(test_item_px += test_item_2dt);
}
SECTION("Inplace Subtraction")
{
UnitType<TestType> test_item_px = CreateUnitItem<TestType>(Units::Pixels, 3);
test_item_px -= test_item_1px;
CHECK(test_item_px == test_item_2px);
CHECK_THROWS(test_item_px -= test_item_2dt);
}
}
}