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 #ifndef UTILS_UTILS_H
17 #define UTILS_UTILS_H
18 #include <cmath>
19 #include "common_utilities_hpp.h"
20 namespace OHOS::uitest {
NearEqual(const double left,const double right,const double epsilon)21 inline bool NearEqual(const double left, const double right, const double epsilon)
22 {
23 return (std::abs(left - right) <= epsilon);
24 }
25
26 template<typename T>
27 constexpr bool NearEqual(const T& left, const T& right);
28
29 template<>
30 inline bool NearEqual<float>(const float& left, const float& right)
31 {
32 constexpr double epsilon = 0.001f;
33 return NearEqual(left, right, epsilon);
34 }
35
36 template<>
37 inline bool NearEqual<double>(const double& left, const double& right)
38 {
39 constexpr double epsilon = 0.00001f;
40 return NearEqual(left, right, epsilon);
41 }
42
43 template<typename T>
NearEqual(const T & left,const T & right)44 constexpr bool NearEqual(const T& left, const T& right)
45 {
46 return left == right;
47 }
48
NearZero(const double value,const double epsilon)49 inline bool NearZero(const double value, const double epsilon)
50 {
51 return NearEqual(value, 0.0, epsilon);
52 }
53
NearEqual(const double left,const double right)54 inline bool NearEqual(const double left, const double right)
55 {
56 constexpr double epsilon = 0.001f;
57 return NearEqual(left, right, epsilon);
58 }
59
NearZero(const double left)60 inline bool NearZero(const double left)
61 {
62 constexpr double epsilon = 0.001f;
63 return NearZero(left, epsilon);
64 }
65
LessOrEqual(double left,double right)66 inline bool LessOrEqual(double left, double right)
67 {
68 constexpr double epsilon = 0.001f;
69 return (left - right) < epsilon;
70 }
71
LessNotEqual(double left,double right)72 inline bool LessNotEqual(double left, double right)
73 {
74 constexpr double epsilon = -0.001f;
75 return (left - right) < epsilon;
76 }
77
GreatOrEqual(double left,double right)78 inline bool GreatOrEqual(double left, double right)
79 {
80 constexpr double epsilon = -0.001f;
81 return (left - right) > epsilon;
82 }
83
GreatNotEqual(double left,double right)84 inline bool GreatNotEqual(double left, double right)
85 {
86 constexpr double epsilon = 0.001f;
87 return (left - right) > epsilon;
88 }
89 } // namespace OHOS::uitest
90 #endif // UTILS_UTILS_H