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