1/*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "gfx_utils/geometry2d.h"
17
18#include <climits>
19#include <gtest/gtest.h>
20
21using namespace testing::ext;
22
23namespace OHOS {
24namespace {
25    const uint16_t VECTOR2_SIZE = 4;
26    const int16_t BASE_VALUE = 1;
27    const int16_t TWOFOLD_VALUE = 2 * BASE_VALUE;
28    const int16_t THREEFOLD_VALUE = 3 * BASE_VALUE;
29    const int16_t FOURFOLD_VALUE = 4 * BASE_VALUE;
30    const int16_t FIVEFOLD_VALUE = 5 * BASE_VALUE;
31}
32
33class Geometry2dTest : public testing::Test {
34public:
35    static void SetUpTestCase(void) {}
36    static void TearDownTestCase(void) {}
37};
38
39/**
40 * @tc.name: LineOperator_001
41 * @tc.desc: Verify index operator function, equal.
42 * @tc.type: FUNC
43 * @tc.require: AR000EEMQ9
44 */
45HWTEST_F(Geometry2dTest, LineOperator_001, TestSize.Level0)
46{
47    Vector2<int16_t> pt1 = { BASE_VALUE, TWOFOLD_VALUE };
48    Vector2<int16_t> pt2 = { THREEFOLD_VALUE, FOURFOLD_VALUE };
49    Line* line = new Line(pt1, pt2);
50
51    EXPECT_EQ(line->operator[](0).x_, BASE_VALUE);
52    EXPECT_EQ(line->operator[](0).y_, TWOFOLD_VALUE);
53    EXPECT_EQ(line->operator[](1).x_, THREEFOLD_VALUE);
54    EXPECT_EQ(line->operator[](1).y_, FOURFOLD_VALUE);
55    delete line;
56}
57
58/**
59 * @tc.name: LineOperator_002
60 * @tc.desc: Verify index operator function, equal.
61 * @tc.type: FUNC
62 * @tc.require: AR000EEMQ9
63 */
64HWTEST_F(Geometry2dTest, LineOperator_002, TestSize.Level1)
65{
66    Line* line = new Line(BASE_VALUE, TWOFOLD_VALUE, THREEFOLD_VALUE, FOURFOLD_VALUE);
67    if (line == nullptr) {
68        EXPECT_EQ(1, 0);
69        return;
70    }
71    EXPECT_EQ(line->operator[](0).x_, BASE_VALUE);
72    EXPECT_EQ(line->operator[](0).y_, TWOFOLD_VALUE);
73    EXPECT_EQ(line->operator[](1).x_, THREEFOLD_VALUE);
74    EXPECT_EQ(line->operator[](1).y_, FOURFOLD_VALUE);
75    delete line;
76}
77
78/**
79 * @tc.name: PolygonMakeAABB_001
80 * @tc.desc: Verify MakeAABB function, equal.
81 * @tc.type: FUNC
82 * @tc.require: AR000EEMQ9
83 */
84HWTEST_F(Geometry2dTest, PolygonMakeAABB_001, TestSize.Level0)
85{
86    Vector2<int16_t> vertexes[VECTOR2_SIZE] =
87        { {0, 0}, {FIVEFOLD_VALUE, BASE_VALUE},
88        {THREEFOLD_VALUE, FIVEFOLD_VALUE}, {BASE_VALUE, THREEFOLD_VALUE} };
89    Polygon* polygon = new Polygon(vertexes, VECTOR2_SIZE);
90    if (polygon == nullptr) {
91        EXPECT_EQ(1, 0);
92        return;
93    }
94    Rect rect = polygon->MakeAABB();
95
96    EXPECT_EQ(rect.GetLeft(), 0);
97    EXPECT_EQ(rect.GetTop(), 0);
98    EXPECT_EQ(rect.GetRight(), FIVEFOLD_VALUE);
99    EXPECT_EQ(rect.GetBottom(), FIVEFOLD_VALUE);
100    delete polygon;
101}
102
103/**
104 * @tc.name: PolygonGetVertexNum_001
105 * @tc.desc: Verify GetVertexNum function, equal.
106 * @tc.type: FUNC
107 * @tc.require: AR000EEMQ9
108 */
109HWTEST_F(Geometry2dTest, PolygonGetVertexNum_001, TestSize.Level0)
110{
111    Vector2<int16_t> vertexes[VECTOR2_SIZE] =
112        { { 0, 0 }, { FIVEFOLD_VALUE, BASE_VALUE },
113        { THREEFOLD_VALUE, FIVEFOLD_VALUE }, { BASE_VALUE, THREEFOLD_VALUE } };
114    Polygon* polygon = new Polygon(vertexes, VECTOR2_SIZE);
115    if (polygon == nullptr) {
116        EXPECT_EQ(1, 0);
117        return;
118    }
119
120    EXPECT_EQ(polygon->GetVertexNum(), VECTOR2_SIZE);
121    delete polygon;
122}
123
124/**
125 * @tc.name: PolygonSetVertexNum_001
126 * @tc.desc: Verify SetVertexNum function, equal.
127 * @tc.type: FUNC
128 * @tc.require: AR000EEMQ9
129 */
130HWTEST_F(Geometry2dTest, PolygonSetVertexNum_001, TestSize.Level0)
131{
132    Polygon* polygon = new Polygon();
133    if (polygon == nullptr) {
134        EXPECT_EQ(1, 0);
135        return;
136    }
137    const uint16_t VERTEX_NUM = 8;
138    polygon->SetVertexNum(VERTEX_NUM);
139
140    EXPECT_EQ(polygon->GetVertexNum(), VERTEX_NUM);
141    delete polygon;
142}
143
144/**
145 * @tc.name: PolygonOperator_001
146 * @tc.desc: Verify operator[] function, equal.
147 * @tc.type: FUNC
148 * @tc.require: AR000EEMQ9
149 */
150HWTEST_F(Geometry2dTest, PolygonOperator_001, TestSize.Level0)
151{
152    Vector2<int16_t> vertexes[VECTOR2_SIZE] =
153        { { 0, 0 }, { FIVEFOLD_VALUE, BASE_VALUE },
154        { THREEFOLD_VALUE, FIVEFOLD_VALUE }, { BASE_VALUE, THREEFOLD_VALUE } };
155    Polygon* polygon = new Polygon(vertexes, VECTOR2_SIZE);
156    if (polygon == nullptr) {
157        EXPECT_EQ(1, 0);
158        return;
159    }
160    EXPECT_EQ(polygon->operator[](1).x_, FIVEFOLD_VALUE);
161    EXPECT_EQ(polygon->operator[](2).y_, FIVEFOLD_VALUE);
162
163    delete polygon;
164}
165
166/**
167 * @tc.name: PolygonOperator_002
168 * @tc.desc: Verify operator[] function, equal.
169 * @tc.type: FUNC
170 * @tc.require: AR000EEMQ9
171 */
172HWTEST_F(Geometry2dTest, PolygonOperator_002, TestSize.Level0)
173{
174    Rect rect(BASE_VALUE, TWOFOLD_VALUE, THREEFOLD_VALUE, FOURFOLD_VALUE);
175    Polygon* polygon = new Polygon(rect);
176    if (polygon == nullptr) {
177        EXPECT_EQ(1, 0);
178        return;
179    }
180    uint16_t i = 0;
181    EXPECT_EQ(polygon->operator[](i).x_, BASE_VALUE);
182    EXPECT_EQ(polygon->operator[](i++).y_, TWOFOLD_VALUE);
183    EXPECT_EQ(polygon->operator[](i).x_, THREEFOLD_VALUE);
184    EXPECT_EQ(polygon->operator[](i++).y_, TWOFOLD_VALUE);
185    EXPECT_EQ(polygon->operator[](i).x_, THREEFOLD_VALUE);
186    EXPECT_EQ(polygon->operator[](i++).y_, FOURFOLD_VALUE);
187    EXPECT_EQ(polygon->operator[](i).x_, BASE_VALUE);
188    EXPECT_EQ(polygon->operator[](i++).y_, FOURFOLD_VALUE);
189    EXPECT_EQ(polygon->GetVertexNum(), VECTOR2_SIZE);
190
191    delete polygon;
192}
193
194/**
195 * @tc.name: Geometry2dIntersect_001
196 * @tc.desc: Verify Intersect function, equal.
197 * @tc.type: FUNC
198 * @tc.require: AR000EEMQ9
199 */
200HWTEST_F(Geometry2dTest, Geometry2dIntersect_001, TestSize.Level0)
201{
202    Vector2<int16_t> pt11 = { 0, TWOFOLD_VALUE };
203    Vector2<int16_t> pt12 = { FOURFOLD_VALUE, TWOFOLD_VALUE };
204    Line* line1 = new Line(pt11, pt12);
205    if (line1 == nullptr) {
206        EXPECT_EQ(1, 0);
207        return;
208    }
209    Vector2<int16_t> pt21 = { TWOFOLD_VALUE, 0 };
210    Vector2<int16_t> pt22 = { TWOFOLD_VALUE, FOURFOLD_VALUE };
211    Line* line2 = new Line(pt21, pt22);
212    if (line2 == nullptr) {
213        delete line1;
214        EXPECT_EQ(1, 0);
215        return;
216    }
217    Vector2<int16_t> pt31 = { 0, 0 };
218    Vector2<int16_t> pt32 = { FIVEFOLD_VALUE, 0 };
219    Line* line3 = new Line(pt31, pt32);
220    if (line3 == nullptr) {
221        delete line1;
222        delete line2;
223        EXPECT_EQ(1, 0);
224        return;
225    }
226    Vector2<int16_t> out;
227
228    EXPECT_EQ(Intersect(*line1, *line2, out), true);
229    EXPECT_EQ(out.x_, TWOFOLD_VALUE);
230    EXPECT_EQ(out.y_, TWOFOLD_VALUE);
231
232    delete line1;
233    delete line2;
234    delete line3;
235}
236
237/**
238 * @tc.name: Geometry2dIsIntersect_001
239 * @tc.desc: Verify IsIntersect function, equal.
240 * @tc.type: FUNC
241 * @tc.require: AR000EEMQ9
242 */
243HWTEST_F(Geometry2dTest, Geometry2dIsIntersect_001, TestSize.Level0)
244{
245    Vector2<int16_t> pt11 = { 0, TWOFOLD_VALUE };
246    Vector2<int16_t> pt12 = { FOURFOLD_VALUE, TWOFOLD_VALUE };
247    Line* line1 = new Line(pt11, pt12);
248    if (line1 == nullptr) {
249        EXPECT_EQ(1, 0);
250        return;
251    }
252    Vector2<int16_t> pt21 = { TWOFOLD_VALUE, 0 };
253    Vector2<int16_t> pt22 = { TWOFOLD_VALUE, FOURFOLD_VALUE };
254    Line* line2 = new Line(pt21, pt22);
255    if (line2 == nullptr) {
256        delete line1;
257        EXPECT_EQ(1, 0);
258        return;
259    }
260    Vector2<int16_t> pt31 = { 0, 0 };
261    Vector2<int16_t> pt32 = { FIVEFOLD_VALUE, 0 };
262    Line* line3 = new Line(pt31, pt32);
263    if (line3 == nullptr) {
264        delete line1;
265        delete line2;
266        EXPECT_EQ(1, 0);
267        return;
268    }
269    EXPECT_EQ(IsIntersect(*line1, *line2), true);
270
271    delete line1;
272    delete line2;
273    delete line3;
274}
275
276/**
277 * @tc.name: Geometry2dClip_001
278 * @tc.desc: Verify Clip function, equal.
279 * @tc.type: FUNC
280 * @tc.require: AR000EEMQ9
281 */
282HWTEST_F(Geometry2dTest, Geometry2dClip_001, TestSize.Level0)
283{
284    Vector2<int16_t> vertexes[VECTOR2_SIZE] =
285        { { 0, 0 }, { FIVEFOLD_VALUE, BASE_VALUE },
286        { THREEFOLD_VALUE, FIVEFOLD_VALUE }, { BASE_VALUE, THREEFOLD_VALUE } };
287    Polygon* polygon = new Polygon(vertexes, VECTOR2_SIZE);
288    if (polygon == nullptr) {
289        EXPECT_EQ(1, 0);
290        return;
291    }
292    Vector2<int16_t> pt11 = { BASE_VALUE, THREEFOLD_VALUE };
293    Vector2<int16_t> pt12 = { FIVEFOLD_VALUE, BASE_VALUE };
294    Line* line = new Line(pt11, pt12);
295    if (line == nullptr) {
296        delete polygon;
297        EXPECT_EQ(1, 0);
298        return;
299    }
300    Clip(*polygon, *line);
301    uint16_t i = 0;
302    EXPECT_EQ(polygon->GetVertexNum(), 3);
303    EXPECT_EQ(polygon->operator[](i).x_, FIVEFOLD_VALUE);
304    EXPECT_EQ(polygon->operator[](i++).y_, BASE_VALUE);
305    EXPECT_EQ(polygon->operator[](i).x_, BASE_VALUE);
306    EXPECT_EQ(polygon->operator[](i++).y_, THREEFOLD_VALUE);
307    EXPECT_EQ(polygon->operator[](i).x_, 0);
308    EXPECT_EQ(polygon->operator[](i++).y_, 0);
309
310    delete polygon;
311    delete line;
312}
313} // namespace OHOS
314