1 /*
2  * Copyright (C) 2024 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 <iomanip>
17 #include <sstream>
18 
19 #include <base/containers/string.h>
20 
21 #include "test_utils.h"
22 
23 namespace {
24     // The width of the output field
25     const int TWO = 2;
26 
27     // Insert '-' separator at a specific location
28     const int THREE = 3;
29     const int FIVE = 5;
30     const int SEVEN = 7;
31     const int NINE = 9;
32 
33     // Floating-point precision
34     const int SIX = 6;
35 }
36 
37 namespace BASE_NS {
operator <<(std::ostream& os, const BASE_NS::Uid& uid)38 std::ostream& operator<<(std::ostream& os, const BASE_NS::Uid& uid)
39 {
40     std::stringstream ss;
41     ss << "{" << std::setfill('0') << std::setw(TWO) << std::hex;
42     for (size_t i = 0; i < sizeof(uid.data); i++) {
43         ss << static_cast<uint16_t>(uid.data[i]);
44         if (i == THREE || i == FIVE || i == SEVEN || i == NINE) {
45             ss << "-";
46         }
47     }
48     ss << "}";
49     return os << ss.str();
50 }
51 
operator <<(std::ostream& os, const BASE_NS::string& s)52 std::ostream& operator<<(std::ostream& os, const BASE_NS::string& s)
53 {
54     return os << s.c_str();
55 }
56 
operator <<(std::ostream& os, const BASE_NS::string_view& s)57 std::ostream& operator<<(std::ostream& os, const BASE_NS::string_view& s)
58 {
59     return os << s.data();
60 }
61 
62 namespace Math {
operator >(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs)63 bool operator>(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs)
64 {
65     return ((lhs.x > rhs.x) && (lhs.y > rhs.y));
66 }
67 
operator <(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs)68 bool operator<(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs)
69 {
70     return ((lhs.x < rhs.x) && (lhs.y < rhs.y));
71 }
72 
operator >(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs)73 bool operator>(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs)
74 {
75     return ((lhs.x > rhs.x) && (lhs.y > rhs.y));
76 }
77 
operator <(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs)78 bool operator<(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs)
79 {
80     return ((lhs.x < rhs.x) && (lhs.y < rhs.y));
81 }
82 
operator >=(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs)83 bool operator>=(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs)
84 {
85     return ((lhs.x >= rhs.x) && (lhs.y >= rhs.y));
86 }
87 
operator <=(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs)88 bool operator<=(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs)
89 {
90     return ((lhs.x <= rhs.x) && (lhs.y <= rhs.y));
91 }
92 
operator >=(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs)93 bool operator>=(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs)
94 {
95     return ((lhs.x >= rhs.x) && (lhs.y >= rhs.y));
96 }
97 
operator <=(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs)98 bool operator<=(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs)
99 {
100     return ((lhs.x <= rhs.x) && (lhs.y <= rhs.y));
101 }
102 
AreEqual(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs, float epsilon)103 bool AreEqual(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs, float epsilon)
104 {
105     return BASE_NS::Math::abs(lhs.x - rhs.x) < epsilon && BASE_NS::Math::abs(lhs.y - rhs.y) < epsilon &&
106            BASE_NS::Math::abs(lhs.z - rhs.z) < epsilon;
107 }
108 
AreNear(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs)109 bool AreNear(const BASE_NS::Math::Vec3& lhs, const BASE_NS::Math::Vec3& rhs)
110 {
111     return AreEqual(lhs, rhs, 0.01f);
112 }
113 
AreEqual(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs, float epsilon)114 bool AreEqual(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs, float epsilon)
115 {
116     return BASE_NS::Math::abs(lhs.x - rhs.x) < epsilon && BASE_NS::Math::abs(lhs.y - rhs.y) < epsilon;
117 }
118 
AreNear(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs)119 bool AreNear(const BASE_NS::Math::Vec2& lhs, const BASE_NS::Math::Vec2& rhs)
120 {
121     return AreEqual(lhs, rhs, 0.01f);
122 }
123 
operator <<(std::ostream& os, const BASE_NS::Math::Vec3& vec)124 std::ostream& operator<<(std::ostream& os, const BASE_NS::Math::Vec3& vec)
125 {
126     std::stringstream ss;
127     ss << std::setprecision(SIX) << "BASE_NS::Math::Vec3 {" << vec.x << ", " << vec.y << ", " << vec.z << "}";
128     return os << ss.str();
129 }
operator <<(std::ostream& os, const BASE_NS::Math::Vec2& vec)130 std::ostream& operator<<(std::ostream& os, const BASE_NS::Math::Vec2& vec)
131 {
132     std::stringstream ss;
133     ss << std::setprecision(SIX) << "BASE_NS::Math::Vec2 {" << vec.x << ", " << vec.y << "}";
134     return os << ss.str();
135 }
136 
137 } // namespace Math
138 } // namespace BASE_NS
139 
140 namespace META_NS {
141 
operator <<(std::ostream& os, const META_NS::TimeSpan& ts)142 std::ostream& operator<<(std::ostream& os, const META_NS::TimeSpan& ts)
143 {
144     return os << ts.ToMicroseconds() << "us";
145 }
146 
147 } // namespace META_NS