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 20using namespace testing; 21using namespace testing::ext; 22 23namespace OHOS { 24namespace Rosen { 25using namespace TransformHelper; 26class WmMathTest : public testing::Test { 27public: 28 static void SetUpTestCase(); 29 static void TearDownTestCase(); 30 virtual void SetUp() override; 31 virtual void TearDown() override; 32}; 33 34void WmMathTest::SetUpTestCase() 35{ 36} 37 38void WmMathTest::TearDownTestCase() 39{ 40} 41 42void WmMathTest::SetUp() 43{ 44} 45 46void WmMathTest::TearDown() 47{ 48} 49 50namespace { 51/** 52 * @tc.name: MathHalper 53 * @tc.desc: MathHalper test 54 * @tc.type: FUNC 55 */ 56HWTEST_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 81/** 82 * @tc.name: TransformMatrix 83 * @tc.desc: Create transform matrix 84 * Get scale component from transform matrix 85 * Get translation component from transform matrix 86 * @tc.type: FUNC 87 */ 88HWTEST_F(WmMathTest, TransformMatrix, Function | SmallTest | Level2) 89{ 90 Vector3 scale(1.5f, 0.7f, 2.2f), translation(100.f, 132.f, 20.f); 91 Matrix4 transformMat = CreateScale(scale.x_, scale.y_, scale.z_); 92 float theta = 2.34f; 93 transformMat *= CreateRotationY(theta); 94 transformMat *= CreateTranslation(translation); 95 Vector3 scaleComp = transformMat.GetScale(); 96 Vector3 translationComp = transformMat.GetTranslation(); 97 ASSERT_EQ(true, MathHelper::NearZero((scale - scaleComp).Length())); 98 ASSERT_EQ(true, MathHelper::NearZero((translation - translationComp).Length())); 99} 100/** 101 * @tc.name: TransformWithPerspDiv 102 * @tc.desc: Create transform matrix 103 * Get scale component from transform matrix 104 * Get translation component from transform matrix 105 * @tc.type: FUNC 106 */ 107HWTEST_F(WmMathTest, TransformWithPerspDiv, Function | SmallTest | Level2) 108{ 109 Vector3 vec(1.0, 1.0, 1.0); 110 Matrix4 mat = Matrix4::Identity; 111 auto result = TransformWithPerspDiv(vec, mat, 0.5); 112 auto expect = vec * 2; 113 ASSERT_EQ(expect.x_, result.x_); 114 ASSERT_EQ(expect.y_, result.y_); 115 ASSERT_EQ(expect.z_, result.z_); 116 117 result = TransformWithPerspDiv(vec, mat, 0); 118 ASSERT_EQ(vec.x_, result.x_); 119 ASSERT_EQ(vec.y_, result.y_); 120 ASSERT_EQ(vec.z_, result.z_); 121} 122 123/** 124 * @tc.name: Invert 125 * @tc.desc: 126 * @tc.type: FUNC 127 */ 128HWTEST_F(WmMathTest, Invert, Function | SmallTest | Level2) 129{ 130 Matrix4 mat; 131 mat.mat_[0][0] = 0.f; 132 mat.mat_[1][0] = -1.0f; 133 mat.Invert(); 134 ASSERT_EQ(false, MathHelper::NearZero(0.f - mat.mat_[1][0])); 135} 136 137/** 138 * @tc.name: Invert02 139 * @tc.desc: 140 * @tc.type: FUNC 141 */ 142HWTEST_F(WmMathTest, Invert02, Function | SmallTest | Level2) 143{ 144 Matrix4 mat; 145 mat.mat_[0][0] = 10.0f; 146 mat.mat_[1][0] = -1.0f; 147 mat.Invert(); 148 ASSERT_EQ(false, MathHelper::NearZero(0.f - mat.mat_[1][0])); 149} 150 151/** 152 * @tc.name: Invert02 153 * @tc.desc: 154 * @tc.type: FUNC 155 */ 156HWTEST_F(WmMathTest, Invert03, Function | SmallTest | Level2) 157{ 158 Matrix4 mat; 159 mat.mat_[0][0] = 0.f; 160 mat.mat_[1][0] = 0.f; 161 mat.Invert(); 162 ASSERT_EQ(false, MathHelper::NearZero(0.f - mat.mat_[1][0])); 163} 164 165/** 166 * @tc.name: Invert02 167 * @tc.desc: 168 * @tc.type: FUNC 169 */ 170HWTEST_F(WmMathTest, Invert04, Function | SmallTest | Level2) 171{ 172 Matrix4 mat; 173 mat.mat_[0][0] = 10.0f; 174 mat.mat_[1][0] = 0.f; 175 mat.Invert(); 176 ASSERT_EQ(false, MathHelper::NearZero(0.f - mat.mat_[1][0])); 177} 178} 179} // namespace Rosen 180} // namespace OHOS