1 /*
2 *
3 * Copyright 2019, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <gtest/gtest.h>
19
20 #define TEEUI_DO_LOG_DEBUG
21
22 #include <teeui/error.h>
23 #include <teeui/log.h>
24 #include <teeui/utils.h>
25
26 namespace teeui {
27
28 namespace test {
29
TEST(TeeUIUtilsTest,intersectTest)30 TEST(TeeUIUtilsTest, intersectTest) {
31 PxVec a(0.0, 1.0);
32 PxPoint b(0.0, -2.0);
33 PxVec c(1.0, 0.0);
34 PxPoint d(3.0, 0.0);
35 auto result = intersect(a, b, c, d);
36 ASSERT_TRUE(result);
37 ASSERT_EQ(PxPoint(0.0, 0.0), *result);
38
39 // one of the directional vectors is (0,0)
40 a = {0.0, 1.0};
41 b = {0.0, -2.0};
42 c = {0.0, 0.0};
43 d = {3.0, 0.0};
44 result = intersect(a, b, c, d);
45 ASSERT_FALSE(result);
46 result = intersect(c, d, a, b);
47 ASSERT_FALSE(result);
48
49 a = {0.0, 0.0};
50 b = {0.0, -2.0};
51 c = {1.0, 0.0};
52 d = {3.0, 0.0};
53 result = intersect(a, b, c, d);
54 ASSERT_FALSE(result);
55 result = intersect(c, d, a, b);
56 ASSERT_FALSE(result);
57
58 a = {0.0, 0.0};
59 b = {0.0, -2.0};
60 c = {0.0, 0.0};
61 d = {3.0, 0.0};
62 result = intersect(a, b, c, d);
63 ASSERT_FALSE(result);
64 result = intersect(c, d, a, b);
65 ASSERT_FALSE(result);
66
67 // lines are parallel
68 a = {0.0, 1.0};
69 b = {0.0, -2.0};
70 c = {0.0, 2.0};
71 d = {3.0, 0.0};
72 result = intersect(a, b, c, d);
73 ASSERT_FALSE(result);
74 result = intersect(c, d, a, b);
75 ASSERT_FALSE(result);
76
77 a = {3.0, 1.0};
78 b = {0.0, -2.0};
79 c = {6.0, 2.0};
80 d = {0.0, 4.0};
81 result = intersect(a, b, c, d);
82 ASSERT_FALSE(result);
83 result = intersect(c, d, a, b);
84 ASSERT_FALSE(result);
85
86 a = {1.0, 1.0};
87 b = {0.0, -0.5};
88 c = {1.0, 0.0};
89 d = {0.0, 0.0};
90 result = intersect(a, b, c, d);
91 ASSERT_TRUE(result);
92 ASSERT_EQ(PxPoint(0.5, 0.0), *result);
93 result = intersect(c, d, a, b);
94 ASSERT_TRUE(result);
95 ASSERT_EQ(PxPoint(0.5, 0.0), *result);
96
97 a = {-1.0, -1.0};
98 b = {0.0, -0.5};
99 c = {1.0, 0.0};
100 d = {0.0, 0.0};
101 result = intersect(a, b, c, d);
102 ASSERT_TRUE(result);
103 ASSERT_EQ(PxPoint(0.5, 0.0), *result);
104 result = intersect(c, d, a, b);
105 ASSERT_TRUE(result);
106 ASSERT_EQ(PxPoint(0.5, 0.0), *result);
107
108 a = {1.0, -1.0};
109 b = {0.0, 1.0};
110 c = {1.0, 1.0};
111 d = {0.0, 0.0};
112 result = intersect(a, b, c, d);
113 ASSERT_TRUE(result);
114 ASSERT_EQ(PxPoint(0.5, 0.5), *result);
115 result = intersect(c, d, a, b);
116 ASSERT_TRUE(result);
117 ASSERT_EQ(PxPoint(0.5, 0.5), *result);
118 }
119
TEST(TeeUIUtilsTest,ConvexObjectConstruction)120 TEST(TeeUIUtilsTest, ConvexObjectConstruction) {
121 constexpr ConvexObject<10> o{{.0, .0}, {1.0, .0}, {1.0, 1.0}, {.0, 1.0}};
122 ASSERT_EQ(size_t(4), o.size());
123 }
124
TEST(TeeUIUtilsTest,ConvexObjectLineIntersection)125 TEST(TeeUIUtilsTest, ConvexObjectLineIntersection) {
126 constexpr ConvexObject<10> o{{.0, .0}, {1.0, .0}, {1.0, 1.0}, {.0, 1.0}};
127 ASSERT_EQ(size_t(4), o.size());
128
129 // diagonally through the corners
130 auto o2 = o.intersect<10>({.0, .0}, {1.0, 1.0});
131 ASSERT_TRUE(o2);
132 ASSERT_EQ(size_t(3), o2->size());
133 ASSERT_EQ(pxs(.5), o2->area());
134
135 // diagonally through the corners reversed
136 o2 = o.intersect<10>({1.0, 1.0}, {.0, .0});
137 ASSERT_TRUE(o2);
138 ASSERT_EQ(size_t(3), o2->size());
139 ASSERT_EQ(pxs(.5), o2->area());
140
141 // diagonally through the top right corner
142 o2 = o.intersect<10>({.0, 2.0}, {2.0, .0});
143 ASSERT_FALSE(o2);
144
145 // diagonally through the top right corner reversed
146 o2 = o.intersect<10>({2.0, .0}, {.0, 2.0});
147 ASSERT_TRUE(o2);
148 ASSERT_EQ(size_t(4), o2->size());
149 ASSERT_EQ(pxs(1.0), o2->area());
150
151 // diagonally through the top left corner
152 o2 = o.intersect<10>({-1.0, .0}, {1.0, 2.0});
153 ASSERT_FALSE(o2);
154
155 // diagonally through the top left corner reversed
156 o2 = o.intersect<10>({1.0, 2.0}, {-1.0, .0});
157 ASSERT_TRUE(o2);
158 ASSERT_EQ(size_t(4), o2->size());
159 ASSERT_EQ(pxs(1.0), o2->area());
160
161 // diagonally through the bottom right corner
162 o2 = o.intersect<10>({2.0, 1.0}, {.0, -1.0});
163 ASSERT_FALSE(o2);
164
165 // diagonally through the bottom right corner reversed
166 o2 = o.intersect<10>({.0, -1.0}, {2.0, 1.0});
167 ASSERT_TRUE(o2);
168 ASSERT_EQ(size_t(4), o2->size());
169 ASSERT_EQ(pxs(1.0), o2->area());
170
171 // diagonally through the bottom left corner
172 o2 = o.intersect<10>({1.0, -1.0}, {-1.0, 1.0});
173 ASSERT_FALSE(o2);
174
175 // diagonally through the top right corner reversed
176 o2 = o.intersect<10>({-1.0, 1.0}, {1.0, -1.0});
177 ASSERT_TRUE(o2);
178 ASSERT_EQ(size_t(4), o2->size());
179 ASSERT_EQ(pxs(1.0), o2->area());
180
181 // through two corners
182 o2 = o.intersect<10>({-1.0, 1.0}, {2.0, 1.0});
183 ASSERT_FALSE(o2);
184
185 // through two corners reversed
186 o2 = o.intersect<10>({2.0, 1.0}, {-1.0, 1.0});
187 ASSERT_TRUE(o2);
188 ASSERT_EQ(size_t(4), o2->size());
189 ASSERT_EQ(pxs(1.0), o2->area());
190
191 o2 = o.intersect<10>({.0, -.5}, {.5, .0});
192 ASSERT_TRUE(o2);
193 ASSERT_EQ(size_t(5), o2->size());
194 ASSERT_EQ(pxs(.875), o2->area());
195
196 o2 = o.intersect<10>({.0, .5}, {.5, .0});
197 ASSERT_TRUE(o2);
198 ASSERT_EQ(size_t(5), o2->size());
199 ASSERT_EQ(pxs(.875), o2->area());
200
201 ConvexObject<10> o3{{-1.3845, 23.0}, {-0.384501, 23}, {-0.384501, 24}, {-1.3845, 24}};
202 o2 = o3.intersect<10>({-3.3845, 25.3339}, {7.59022, 14.3592});
203 ASSERT_TRUE(o2);
204 ASSERT_EQ(size_t(5), o2->size());
205 }
206
TEST(TeeUIUtilsTest,ErrorOperatorOrOverloadTest)207 TEST(TeeUIUtilsTest, ErrorOperatorOrOverloadTest) {
208 // This expression should evaluate to the first (non OK) error code in the sequence.
209 ASSERT_EQ(Error::NotInitialized,
210 Error::OK || Error::NotInitialized || Error::FaceNotLoaded || Error::OK);
211 }
212
213 } // namespace test
214
215 } // namespace teeui
216