• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <gtest/gtest.h>
17 
18 #include "geomagnetic_field.h"
19 #include "sensor_algorithm.h"
20 #include "sensors_errors.h"
21 
22 namespace OHOS {
23 namespace Sensors {
24 using namespace testing::ext;
25 using namespace OHOS::HiviewDFX;
26 
27 namespace {
28 constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorAlgorithmTest" };
29 constexpr int32_t QUATERNION_LENGTH = 4;
30 constexpr int32_t ROTATION_VECTOR_LENGTH = 3;
31 constexpr int32_t THREE_DIMENSIONAL_MATRIX_LENGTH = 9;
32 }  // namespace
33 
34 class SensorAlgorithmTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp();
39     void TearDown();
40 };
41 
SetUpTestCase()42 void SensorAlgorithmTest::SetUpTestCase() {}
43 
TearDownTestCase()44 void SensorAlgorithmTest::TearDownTestCase() {}
45 
SetUp()46 void SensorAlgorithmTest::SetUp() {}
47 
TearDown()48 void SensorAlgorithmTest::TearDown() {}
49 
50 SensorAlgorithm sensorAlgorithm;
51 
52 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_001, TestSize.Level1)
53 {
54     SEN_HILOGI("SensorAlgorithmTest_001 in");
55     std::vector<float> rotationVector = {0.52, -0.336, -0.251};
56     std::vector<float> quaternion(QUATERNION_LENGTH);
57     int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion);
58     ASSERT_EQ(ret, OHOS::Sensors::SUCCESS);
59     ASSERT_EQ(quaternion.size(), QUATERNION_LENGTH);
60     std::vector<float> result = {0.7441122531890869, 0.5199999809265137, -0.335999995470047, -0.25099998712539673};
61     for (size_t i = 0; i < QUATERNION_LENGTH; ++i) {
62         ASSERT_EQ(quaternion[i], result[i]);
63     }
64 }
65 
66 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_002, TestSize.Level1)
67 {
68     SEN_HILOGI("SensorAlgorithmTest_002 in");
69     std::vector<float> rotationVector = {};
70     std::vector<float> quaternion(QUATERNION_LENGTH);
71     int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion);
72     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
73 }
74 
75 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_003, TestSize.Level1)
76 {
77     SEN_HILOGI("SensorAlgorithmTest_003 in");
78     std::vector<float> rotationVector = {0.52, -0.336, -0.251};
79     std::vector<float> quaternion(ROTATION_VECTOR_LENGTH - 1);
80     int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion);
81     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
82 }
83 
84 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_004, TestSize.Level1)
85 {
86     SEN_HILOGI("SensorAlgorithmTest_004 in");
87     std::vector<float> inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5};
88     std::vector<float> outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
89     int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, 2, outRotationMatrix);
90     ASSERT_EQ(ret, OHOS::Sensors::SUCCESS);
91     std::vector<float> result =
92         {1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000, 1.500000};
93     ASSERT_EQ(outRotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH);
94     for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) {
95         ASSERT_EQ(outRotationMatrix[i], result[i]);
96     }
97 }
98 
99 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_005, TestSize.Level1)
100 {
101     SEN_HILOGI("SensorAlgorithmTest_005 in");
102     std::vector<float> inRotationMatrix(3);
103     std::vector<float> outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
104     int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, 2, outRotationMatrix);
105     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
106 }
107 
108 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_006, TestSize.Level1)
109 {
110     SEN_HILOGI("SensorAlgorithmTest_006 in");
111     std::vector<float> inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5};
112     std::vector<float> outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
113     int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, 1, -1, outRotationMatrix);
114     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
115 }
116 
117 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_007, TestSize.Level1)
118 {
119     SEN_HILOGI("SensorAlgorithmTest_007 in");
120     std::vector<float> inRotationMatrix = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5};
121     std::vector<float> outRotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
122     int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationMatrix, -1, 1, outRotationMatrix);
123     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
124 }
125 
126 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_008, TestSize.Level1)
127 {
128     float altitude = -1.0;
129     int32_t ret = sensorAlgorithm.GetAltitude(5.0, 0.0, &altitude);
130     ASSERT_EQ(ret, OHOS::Sensors::SUCCESS);
131     ASSERT_EQ(altitude, 44330.0);
132 }
133 
134 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_009, TestSize.Level1)
135 {
136     int32_t ret = sensorAlgorithm.GetAltitude(5.0, 0.0, nullptr);
137     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
138 }
139 
140 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_010, TestSize.Level1)
141 {
142     float geomagneticDip = -1.0;
143     std::vector<float> inclinationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
144     int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip);
145     ASSERT_EQ(ret, OHOS::Sensors::SUCCESS);
146     ASSERT_EQ(geomagneticDip, 0.8760581016540527);
147 }
148 
149 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_011, TestSize.Level1)
150 {
151     std::vector<float> inclinationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
152     int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, nullptr);
153     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
154 }
155 
156 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_012, TestSize.Level1)
157 {
158     std::vector<float> inclinationMatrix(3);
159     float geomagneticDip = -1.0;
160     int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip);
161     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
162 }
163 
164 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_013, TestSize.Level1)
165 {
166     std::vector<float> currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38,
167         1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38};
168     std::vector<float> preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38,
169         1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38};
170     std::vector<float> angleChange(ROTATION_VECTOR_LENGTH);
171     int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange);
172     ASSERT_EQ(ret, OHOS::Sensors::SUCCESS);
173     ASSERT_EQ(angleChange.size(), ROTATION_VECTOR_LENGTH);
174     std::vector<float> result = {0.0, -0.0, -0.0};
175     for (size_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) {
176         ASSERT_EQ(angleChange[i], result[i]);
177     }
178 }
179 
180 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_014, TestSize.Level1)
181 {
182     std::vector<float> currotationMatrix(3);
183     std::vector<float> preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38,
184         1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38};
185     std::vector<float> angleChange(ROTATION_VECTOR_LENGTH);
186     int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange);
187     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
188 }
189 
190 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_015, TestSize.Level1)
191 {
192     std::vector<float> currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38,
193         1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38};
194     std::vector<float> preRotationMatrix(ROTATION_VECTOR_LENGTH);
195     std::vector<float> angleChange(ROTATION_VECTOR_LENGTH);
196     int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange);
197     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
198 }
199 
200 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_016, TestSize.Level1)
201 {
202     std::vector<float> currotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38,
203         1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38};
204     std::vector<float> preRotationMatrix = {1.17549e-38, 1.17549e-38, 1.17549e-38,
205         1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38};
206     std::vector<float> angleChange(ROTATION_VECTOR_LENGTH - 1);
207     int32_t ret = sensorAlgorithm.GetAngleModify(currotationMatrix, preRotationMatrix, angleChange);
208     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
209 }
210 
211 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_017, TestSize.Level1)
212 {
213     std::vector<float> rotationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
214     std::vector<float> rotationAngle(ROTATION_VECTOR_LENGTH);
215     int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle);
216     ASSERT_EQ(ret, OHOS::Sensors::SUCCESS);
217     std::vector<float> result = {0.38050639629364014, -0.9783217310905457, -0.6610431671142578};
218     ASSERT_EQ(rotationAngle.size(), ROTATION_VECTOR_LENGTH);
219     for (size_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) {
220         ASSERT_EQ(rotationAngle[i], result[i]);
221     }
222 }
223 
224 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_018, TestSize.Level1)
225 {
226     std::vector<float> rotationMatrix(5);
227     std::vector<float> rotationAngle(ROTATION_VECTOR_LENGTH);
228     int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle);
229     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
230 }
231 
232 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_019, TestSize.Level1)
233 {
234     std::vector<float> rotationMatrix = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
235     std::vector<float> rotationAngle(ROTATION_VECTOR_LENGTH - 1);
236     int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle);
237     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
238 }
239 
240 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_020, TestSize.Level1)
241 {
242     std::vector<float> rotationVector = {0.0, 0.0, 0.0};
243     std::vector<float> rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
244     int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix);
245     ASSERT_EQ(ret, OHOS::Sensors::SUCCESS);
246     std::vector<float> result = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
247     ASSERT_EQ(rotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH);
248     for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) {
249         ASSERT_EQ(rotationMatrix[i], result[i]);
250     }
251 }
252 
253 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_021, TestSize.Level1)
254 {
255     std::vector<float> rotationVector(ROTATION_VECTOR_LENGTH - 1);
256     std::vector<float> rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
257     int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix);
258     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
259 }
260 
261 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_022, TestSize.Level1)
262 {
263     std::vector<float> rotationVector = {0.0, 0.0, 0.0};
264     std::vector<float> rotationMatrix(ROTATION_VECTOR_LENGTH - 1);
265     int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix);
266     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
267 }
268 
269 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_023, TestSize.Level1)
270 {
271     std::vector<float> gravity = {9.0, 9.0, 9.0};
272     std::vector<float> geomagnetic = {30.0, 25.0, 41.0};
273     std::vector<float> rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
274     std::vector<float> inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
275     int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix);
276     ASSERT_EQ(ret, OHOS::Sensors::SUCCESS);
277     ASSERT_EQ(rotationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH);
278     ASSERT_EQ(inclinationMatrix.size(), THREE_DIMENSIONAL_MATRIX_LENGTH);
279     std::vector<float> rotationMatrixResult =
280         {-0.7980074882507324, 0.5486301183700562, 0.24937734007835388, -0.17277367413043976,
281         -0.6047078967094421, 0.7774815559387207, 0.5773502588272095, 0.5773502588272095, 0.5773502588272095};
282     std::vector<float> inclinationMatrixResult = {1.0, 0.0, 0.0, 0.0, 0.20444221794605255,
283         0.9788785576820374, 0, -0.9788785576820374, 0.20444221794605255};
284     for (size_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) {
285         ASSERT_EQ(rotationMatrix[i], rotationMatrixResult[i]);
286         ASSERT_EQ(inclinationMatrix[i], inclinationMatrixResult[i]);
287     }
288 }
289 
290 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_024, TestSize.Level1)
291 {
292     std::vector<float> gravity(ROTATION_VECTOR_LENGTH - 1);
293     std::vector<float> geomagnetic = {30.0, 25.0, 41.0};
294     std::vector<float> rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
295     std::vector<float> inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
296     int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix);
297     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
298 }
299 
300 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_025, TestSize.Level1)
301 {
302     std::vector<float> gravity = {9.0, 9.0, 9.0};
303     std::vector<float> geomagnetic(ROTATION_VECTOR_LENGTH - 1);
304     std::vector<float> rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
305     std::vector<float> inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
306     int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix);
307     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
308 }
309 
310 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_026, TestSize.Level1)
311 {
312     std::vector<float> gravity = {9.0, 9.0, 9.0};
313     std::vector<float> geomagnetic = {30.0, 25.0, 41.0};
314     std::vector<float> rotationMatrix(ROTATION_VECTOR_LENGTH - 1);
315     std::vector<float> inclinationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
316     int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix);
317     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
318 }
319 
320 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_027, TestSize.Level1)
321 {
322     std::vector<float> gravity = {9.0, 9.0, 9.0};
323     std::vector<float> geomagnetic = {30.0, 25.0, 41.0};
324     std::vector<float> rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
325     std::vector<float> inclinationMatrix(ROTATION_VECTOR_LENGTH - 1);
326     int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotationMatrix, inclinationMatrix);
327     ASSERT_EQ(ret, OHOS::Sensors::PARAMETER_ERROR);
328 }
329 
330 HWTEST_F(SensorAlgorithmTest, SensorAlgorithmTest_028, TestSize.Level1)
331 {
332     GeomagneticField geomagneticField(80.0, 0.0, 0.0, 1580486400000);
333     ASSERT_EQ(geomagneticField.ObtainX(), 6570.3935546875);
334     ASSERT_EQ(geomagneticField.ObtainY(), -146.3289337158203);
335     ASSERT_EQ(geomagneticField.ObtainZ(), 54606.0078125);
336     ASSERT_EQ(geomagneticField.ObtainGeomagneticDip(), 83.13726043701172);
337     ASSERT_EQ(geomagneticField.ObtainDeflectionAngle(), -1.2758207321166992);
338     ASSERT_EQ(geomagneticField.ObtainLevelIntensity(), 6572.02294921875);
339     ASSERT_EQ(geomagneticField.ObtainTotalIntensity(), 55000.0703125);
340 }
341 }  // namespace Sensors
342 }  // namespace OHOS
343