1 /*
2 * Copyright (c) 2022 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 <gtest/gtest.h>
17
18 #include "wm_math.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace Rosen {
25 using namespace TransformHelper;
26 class WmMathTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 virtual void SetUp() override;
31 virtual void TearDown() override;
32 };
33
SetUpTestCase()34 void WmMathTest::SetUpTestCase()
35 {
36 }
37
TearDownTestCase()38 void WmMathTest::TearDownTestCase()
39 {
40 }
41
SetUp()42 void WmMathTest::SetUp()
43 {
44 }
45
TearDown()46 void WmMathTest::TearDown()
47 {
48 }
49
50 namespace {
51 /**
52 * @tc.name: MathHalper
53 * @tc.desc: MathHalper test
54 * @tc.type: FUNC
55 */
56 HWTEST_F(WmMathTest, MathHalper, Function | SmallTest | Level2)
57 {
58 {
59 const float t = 0.5f;
60 ASSERT_EQ(true, MathHelper::NearZero(0));
61 ASSERT_EQ(true, MathHelper::NearZero(t * MathHelper::NAG_ZERO));
62 ASSERT_EQ(true, MathHelper::NearZero(t * MathHelper::POS_ZERO));
63 }
64 {
65 float radians = MathHelper::PI, degrees = 180.f;
66 ASSERT_EQ(true, MathHelper::NearZero(MathHelper::ToDegrees(radians) - degrees));
67 ASSERT_EQ(true, MathHelper::NearZero(MathHelper::ToRadians(degrees) - radians));
68 }
69 {
70 int a = 1, b = 2, c = 3, d = 4;
71 ASSERT_EQ(true, MathHelper::Max(a, b, c) == c);
72 ASSERT_EQ(true, MathHelper::Min(a, b, c) == a);
73 ASSERT_EQ(true, MathHelper::Max(a, b, c, d) == d);
74 ASSERT_EQ(true, MathHelper::Min(a, b, c, d) == a);
75 ASSERT_EQ(true, MathHelper::Clamp(a, b, c) == b);
76 ASSERT_EQ(true, MathHelper::Clamp(b, a, c) == b);
77 ASSERT_EQ(true, MathHelper::Clamp(c, a, b) == b);
78 }
79 {
80 float a = 0.0001f, b = -0.0001f;
81 EXPECT_TRUE(MathHelper::NearZero(MathHelper::NonZero(a) - MathHelper::POS_ZERO));
82 EXPECT_TRUE(MathHelper::NearZero(MathHelper::NonZero(b) - MathHelper::NAG_ZERO));
83 }
84 {
85 float a = 2.5f;
86 EXPECT_EQ(MathHelper::Floor(a), int32_t(2));
87 EXPECT_EQ(MathHelper::Ceil(a), int32_t(3));
88 }
89 }
90
91 /**
92 * @tc.name: TransformMatrix
93 * @tc.desc: Create transform matrix
94 * Get scale component from transform matrix
95 * Get translation component from transform matrix
96 * @tc.type: FUNC
97 */
98 HWTEST_F(WmMathTest, TransformMatrix, Function | SmallTest | Level2)
99 {
100 Vector3 scale(1.5f, 0.7f, 2.2f), translation(100.f, 132.f, 20.f);
101 Matrix4 transformMat = CreateScale(scale.x_, scale.y_, scale.z_);
102 float theta = 2.34f;
103 transformMat *= CreateRotationY(theta);
104 transformMat *= CreateTranslation(translation);
105 Vector3 scaleComp = transformMat.GetScale();
106 Vector3 translationComp = transformMat.GetTranslation();
107 ASSERT_EQ(true, MathHelper::NearZero((scale - scaleComp).Length()));
108 ASSERT_EQ(true, MathHelper::NearZero((translation - translationComp).Length()));
109 }
110
111 /**
112 * @tc.name: TransformWithPerspDiv
113 * @tc.desc: Create transform matrix
114 * Get scale component from transform matrix
115 * Get translation component from transform matrix
116 * @tc.type: FUNC
117 */
118 HWTEST_F(WmMathTest, TransformWithPerspDiv, Function | SmallTest | Level2)
119 {
120 Vector3 vec(1.0, 1.0, 1.0);
121 Matrix4 mat = Matrix4::Identity;
122 auto result = TransformWithPerspDiv(vec, mat, 0.5);
123 auto expect = vec * 2;
124 ASSERT_EQ(expect.x_, result.x_);
125 ASSERT_EQ(expect.y_, result.y_);
126 ASSERT_EQ(expect.z_, result.z_);
127
128 result = TransformWithPerspDiv(vec, mat, 0);
129 ASSERT_EQ(vec.x_, result.x_);
130 ASSERT_EQ(vec.y_, result.y_);
131 ASSERT_EQ(vec.z_, result.z_);
132 }
133
134 /**
135 * @tc.name: Invert
136 * @tc.desc:
137 * @tc.type: FUNC
138 */
139 HWTEST_F(WmMathTest, Invert, Function | SmallTest | Level2)
140 {
141 Matrix4 mat;
142 mat.mat_[0][0] = 0.f;
143 mat.mat_[1][0] = -1.0f;
144 mat.Invert();
145 ASSERT_EQ(false, MathHelper::NearZero(0.f - mat.mat_[1][0]));
146 }
147
148 /**
149 * @tc.name: Invert02
150 * @tc.desc:
151 * @tc.type: FUNC
152 */
153 HWTEST_F(WmMathTest, Invert02, Function | SmallTest | Level2)
154 {
155 Matrix4 mat;
156 mat.mat_[0][0] = 10.0f;
157 mat.mat_[1][0] = -1.0f;
158 mat.Invert();
159 ASSERT_EQ(false, MathHelper::NearZero(0.f - mat.mat_[1][0]));
160 }
161
162 /**
163 * @tc.name: Invert02
164 * @tc.desc:
165 * @tc.type: FUNC
166 */
167 HWTEST_F(WmMathTest, Invert03, Function | SmallTest | Level2)
168 {
169 Matrix4 mat;
170 mat.mat_[0][0] = 0.f;
171 mat.mat_[1][0] = 0.f;
172 mat.Invert();
173 ASSERT_EQ(false, MathHelper::NearZero(0.f - mat.mat_[1][0]));
174 }
175
176 /**
177 * @tc.name: Invert02
178 * @tc.desc:
179 * @tc.type: FUNC
180 */
181 HWTEST_F(WmMathTest, Invert04, Function | SmallTest | Level2)
182 {
183 Matrix4 mat;
184 mat.mat_[0][0] = 10.0f;
185 mat.mat_[1][0] = 0.f;
186 mat.Invert();
187 ASSERT_EQ(false, MathHelper::NearZero(0.f - mat.mat_[1][0]));
188 }
189 }
190 } // namespace Rosen
191 } // namespace OHOS