all: fix warnings on MSVC
[platform/core/graphics/tizenvg.git] / test / testShape.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #include <thorvg.h>
24 #include "catch.hpp"
25
26 using namespace tvg;
27
28
29 TEST_CASE("Appending Commands", "[tvgShape]")
30 {
31     auto shape = Shape::gen();
32     REQUIRE(shape);
33
34     REQUIRE(shape->close() == Result::Success);
35
36     REQUIRE(shape->moveTo(100, 100) == Result::Success);
37     REQUIRE(shape->moveTo(99999999.0f, -99999999.0f) == Result::Success);
38     REQUIRE(shape->moveTo(0, 0) == Result::Success);
39
40     REQUIRE(shape->lineTo(120, 140) == Result::Success);
41     REQUIRE(shape->lineTo(99999999.0f, -99999999.0f) == Result::Success);
42     REQUIRE(shape->lineTo(0, 0) == Result::Success);
43
44     REQUIRE(shape->cubicTo(0, 0, 0, 0, 0, 0) == Result::Success);
45     REQUIRE(shape->cubicTo(0, 0, 99999999.0f, -99999999.0f, 0, 0) == Result::Success);
46     REQUIRE(shape->cubicTo(0, 0, 99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f) == Result::Success);
47     REQUIRE(shape->cubicTo(99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f) == Result::Success);
48
49     REQUIRE(shape->close() == Result::Success);
50
51     REQUIRE(shape->reset() == Result::Success);
52     REQUIRE(shape->reset() == Result::Success);
53 }
54
55 TEST_CASE("Appending Shapes", "[tvgShape]")
56 {
57     auto shape = Shape::gen();
58     REQUIRE(shape);
59
60     REQUIRE(shape->moveTo(100, 100) == Result::Success);
61     REQUIRE(shape->lineTo(120, 140) == Result::Success);
62
63     REQUIRE(shape->appendRect(0, 0, 0, 0, 0, 0) == Result::Success);
64     REQUIRE(shape->appendRect(0, 0,99999999.0f, -99999999.0f, 0, 0) == Result::Success);
65     REQUIRE(shape->appendRect(0, 0, 0, 0, -99999999.0f, 99999999.0f) == Result::Success);
66     REQUIRE(shape->appendRect(99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, -99999999.0f) == Result::Success);
67
68     REQUIRE(shape->appendCircle(0, 0, 0, 0) == Result::Success);
69     REQUIRE(shape->appendCircle(-99999999.0f, 99999999.0f, 0, 0) == Result::Success);
70     REQUIRE(shape->appendCircle(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f) == Result::Success);
71
72     REQUIRE(shape->appendArc(0, 0, 0, 0, 0, false) == Result::Success);
73     REQUIRE(shape->appendArc(0, 0, 0, 0, 0, true) == Result::Success);
74     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, 0, 0, 0, false) == Result::Success);
75     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, 0, 0, 0, true) == Result::Success);
76     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, 0, false) == Result::Success);
77     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, 0, true) == Result::Success);
78     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, -400, false) == Result::Success);
79     REQUIRE(shape->appendArc(-99999999.0f, 99999999.0f, -99999999.0f, 99999999.0f, 400, true) == Result::Success);
80 }
81
82 TEST_CASE("Appending Pathes", "[tvgShape]")
83 {
84     auto shape = Shape::gen();
85     REQUIRE(shape);
86
87     //Negative cases
88     REQUIRE(shape->appendPath(nullptr, 0, nullptr, 0) == Result::InvalidArguments);
89     REQUIRE(shape->appendPath(nullptr, 100, nullptr, 0) == Result::InvalidArguments);
90     REQUIRE(shape->appendPath(nullptr, 0, nullptr, 100) == Result::InvalidArguments);
91
92     PathCommand cmds[5] = {
93         PathCommand::Close,
94         PathCommand::MoveTo,
95         PathCommand::LineTo,
96         PathCommand::CubicTo,
97         PathCommand::Close
98     };
99
100     Point pts[5] = {
101         {100, 100},
102         {200, 200},
103         {10, 10},
104         {20, 20},
105         {30, 30}
106     };
107
108     REQUIRE(shape->appendPath(cmds, 0, pts, 5) == Result::InvalidArguments);
109     REQUIRE(shape->appendPath(cmds, 5, pts, 0) == Result::InvalidArguments);
110     REQUIRE(shape->appendPath(cmds, 5, pts, 5) == Result::Success);
111
112     const PathCommand* cmds2;
113     REQUIRE(shape->pathCommands(&cmds2) == 5);
114
115     const Point* pts2;
116     REQUIRE(shape->pathCoords(&pts2) == 5);
117
118     for (int i = 0; i < 5; ++i) {
119         REQUIRE(cmds2[i] == cmds[i]);
120         REQUIRE(pts[i].x == pts2[i].x);
121         REQUIRE(pts[i].y == pts2[i].y);
122     }
123
124     shape->reset();
125     REQUIRE(shape->pathCommands(nullptr) == 0);
126     REQUIRE(shape->pathCoords(nullptr) == 0);
127 }
128
129 TEST_CASE("Stroking", "[tvgShape]")
130 {
131     auto shape = Shape::gen();
132     REQUIRE(shape);
133
134     //Stroke Width
135     REQUIRE(shape->stroke(0) == Result::Success);
136     REQUIRE(shape->strokeWidth() == 0);
137     REQUIRE(shape->stroke(300) == Result::Success);
138     REQUIRE(shape->strokeWidth() == 300);
139
140     //Stroke Color
141     uint8_t r, g, b, a;
142     REQUIRE(shape->stroke(0, 50, 100, 200) == Result::Success);
143     REQUIRE(shape->strokeColor(nullptr, nullptr, &b, nullptr) == Result::Success);
144     REQUIRE(b == 100);
145     REQUIRE(shape->strokeColor(&r, &g, &b, &a) == Result::Success);
146     REQUIRE(r == 0);
147     REQUIRE(g == 50);
148     REQUIRE(b == 100);
149     REQUIRE(a == 200);
150     REQUIRE(shape->strokeColor(nullptr, nullptr, nullptr, nullptr) == Result::Success);
151
152     //Stroke Dash
153     float dashPattern[3] = {0, 1.5f, 2.22f};
154     REQUIRE(shape->stroke(dashPattern, 3) == Result::InvalidArguments);
155
156     float dashPattern2[3] = {1.0f, 1.5f, 2.22f};
157     REQUIRE(shape->stroke(dashPattern2, 3) == Result::Success);
158
159     const float* dashPattern3;
160     REQUIRE(shape->strokeDash(nullptr) == 3);
161     REQUIRE(shape->strokeDash(&dashPattern3) == 3);
162     REQUIRE(dashPattern3[0] == 1.0f);
163     REQUIRE(dashPattern3[1] == 1.5f);
164     REQUIRE(dashPattern3[2] == 2.22f);
165
166     REQUIRE(shape->stroke(nullptr, 0) == Result::Success);
167
168     //Stroke Cap
169     REQUIRE(shape->strokeCap() == StrokeCap::Square);
170     REQUIRE(shape->stroke(StrokeCap::Round) == Result::Success);
171     REQUIRE(shape->stroke(StrokeCap::Butt) == Result::Success);
172     REQUIRE(shape->strokeCap() == StrokeCap::Butt);
173
174     //Stroke Join
175     REQUIRE(shape->strokeJoin() == StrokeJoin::Bevel);
176     REQUIRE(shape->stroke(StrokeJoin::Miter) == Result::Success);
177     REQUIRE(shape->stroke(StrokeJoin::Round) == Result::Success);
178     REQUIRE(shape->strokeJoin() == StrokeJoin::Round);
179 }
180
181 TEST_CASE("Shape Filling", "[tvgShape]")
182 {
183     auto shape = Shape::gen();
184     REQUIRE(shape);
185
186     //Fill Color
187     uint8_t r, g, b, a;
188     REQUIRE(shape->fill(255, 100, 50, 5) == Result::Success);
189     REQUIRE(shape->fillColor(&r, nullptr, &b, nullptr) == Result::Success);
190     REQUIRE(r == 255);
191     REQUIRE(b == 50);
192     REQUIRE(shape->fillColor(&r, &g, &b, &a) == Result::Success);
193     REQUIRE(g == 100);
194     REQUIRE(a == 5);
195
196     //Fill Rule
197     REQUIRE(shape->fillRule() == FillRule::Winding);
198     REQUIRE(shape->fill(FillRule::EvenOdd) == Result::Success);
199     REQUIRE(shape->fillRule() == FillRule::EvenOdd);
200 }