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 <gtest/gtest.h>
17
18#include <meta/base/time_span.h>
19
20#include "src/test_runner.h"
21#include "src/test_utils.h"
22
23using namespace testing::ext;
24
25META_BEGIN_NAMESPACE()
26
27class TimeSpanTest : public testing::Test {
28public:
29    static void SetUpTestSuite()
30    {
31        SetTest();
32    }
33    static void TearDownTestSuite()
34    {
35        ResetTest();
36    }
37    void SetUp() override {}
38    void TearDown() override {}
39};
40
41using namespace META_NS::TimeSpanLiterals;
42
43/**
44 * @tc.name: TimeSpanTest
45 * @tc.desc: test InitMethods function
46 * @tc.type: FUNC
47 * @tc.require: I7DMS1
48 */
49HWTEST_F(TimeSpanTest, InitMethods, TestSize.Level1)
50{
51    constexpr auto v1 = TimeSpan::Milliseconds(2000);
52    constexpr auto v2 = TimeSpan::Microseconds(2000000);
53    constexpr auto v3 = TimeSpan::Seconds(2);
54    constexpr TimeSpan v4 = 2_s;
55    constexpr auto v5 = 2000_ms;
56    constexpr auto v6 = 2000000_us;
57
58    EXPECT_EQ(v1, v2);
59    EXPECT_EQ(v2, v3);
60    EXPECT_EQ(v4, v5);
61    EXPECT_EQ(v5, v6);
62    EXPECT_EQ(v6, v1);
63}
64
65/**
66 * @tc.name: TimeSpanTest
67 * @tc.desc: test Addition function
68 * @tc.type: FUNC
69 * @tc.require: I7DMS1
70 */
71HWTEST_F(TimeSpanTest, Addition, TestSize.Level1)
72{
73    constexpr auto v1 = TimeSpan::Milliseconds(400);
74    constexpr auto v2 = TimeSpan::Microseconds(2000);
75
76    constexpr auto v3 = v1 + v2;
77
78    EXPECT_EQ(TimeSpan::Milliseconds(402), v3);
79
80    EXPECT_EQ(TimeSpan::Milliseconds(400), v1);
81    EXPECT_EQ(TimeSpan::Microseconds(2000), v2);
82}
83
84/**
85 * @tc.name: TimeSpanTest
86 * @tc.desc: test Subtraction function
87 * @tc.type: FUNC
88 * @tc.require: I7DMS1
89 */
90HWTEST_F(TimeSpanTest, Subtraction, TestSize.Level1)
91{
92    constexpr auto v1 = TimeSpan::Milliseconds(400);
93    constexpr auto v2 = TimeSpan::Microseconds(2000);
94
95    constexpr auto v3 = v1 - v2;
96
97    EXPECT_EQ(TimeSpan::Milliseconds(398), v3);
98
99    EXPECT_EQ(TimeSpan::Milliseconds(400), v1);
100    EXPECT_EQ(TimeSpan::Microseconds(2000), v2);
101}
102
103/**
104 * @tc.name: TimeSpanTest
105 * @tc.desc: test Negation function
106 * @tc.type: FUNC
107 * @tc.require: I7DMS1
108 */
109HWTEST_F(TimeSpanTest, Negation, TestSize.Level1)
110{
111    constexpr auto v1 = -TimeSpan::Milliseconds(200);
112    constexpr auto v2 = -200_ms;
113
114    EXPECT_EQ(v1, v2);
115    EXPECT_EQ(v1, TimeSpan::Milliseconds(-200));
116    EXPECT_EQ(v2, TimeSpan::Milliseconds(-200));
117}
118
119/**
120 * @tc.name: TimeSpanTest
121 * @tc.desc: test Infinities function
122 * @tc.type: FUNC
123 * @tc.require: I7DMS1
124 */
125HWTEST_F(TimeSpanTest, Infinities, TestSize.Level1)
126{
127    EXPECT_EQ(TimeSpan::Infinite(), TimeSpan::Infinite() + TimeSpan::Infinite());
128    EXPECT_EQ(-TimeSpan::Infinite(), -TimeSpan::Infinite() - TimeSpan::Infinite());
129    EXPECT_EQ(TimeSpan::Infinite(), 3 * TimeSpan::Infinite());
130    EXPECT_EQ(-TimeSpan::Infinite(), -1 * TimeSpan::Infinite());
131    EXPECT_EQ(TimeSpan::Infinite(), -1 * -TimeSpan::Infinite());
132    EXPECT_EQ(TimeSpan::Zero(), 0 * TimeSpan::Infinite());
133    EXPECT_EQ(TimeSpan::Zero(), 0 * -TimeSpan::Infinite());
134
135    EXPECT_EQ(TimeSpan::Infinite(), TimeSpan::Infinite() + TimeSpan::Microseconds(10));
136    EXPECT_EQ(TimeSpan::Infinite(), TimeSpan::Infinite() - TimeSpan::Microseconds(10));
137    EXPECT_EQ(-TimeSpan::Infinite(), -TimeSpan::Infinite() + TimeSpan::Microseconds(10));
138    EXPECT_EQ(-TimeSpan::Infinite(), -TimeSpan::Infinite() - TimeSpan::Microseconds(10));
139
140    auto v1 = TimeSpan::Infinite();
141    v1 += TimeSpan::Milliseconds(10);
142
143    EXPECT_EQ(TimeSpan::Infinite(), v1);
144
145    auto v2 = TimeSpan::Infinite();
146    v2 -= TimeSpan::Milliseconds(10);
147
148    EXPECT_EQ(TimeSpan::Infinite(), v2);
149}
150
151/**
152 * @tc.name: TimeSpanTest
153 * @tc.desc: test TimeIsRoundedToMicroseconds function
154 * @tc.type: FUNC
155 * @tc.require: I7DMS1
156 */
157HWTEST_F(TimeSpanTest, TimeIsRoundedToMicroseconds, TestSize.Level1)
158{
159    EXPECT_EQ(TimeSpan::Microseconds(1), TimeSpan::Seconds(1e-6));
160    EXPECT_EQ(TimeSpan::Microseconds(0), TimeSpan::Seconds(0.4e-6));
161    EXPECT_EQ(TimeSpan::Microseconds(1), TimeSpan::Seconds(0.5e-6));
162
163    auto timeSpan = TimeSpan::Microseconds(1);
164    timeSpan.SetSeconds(0.4e-6);
165    EXPECT_EQ(TimeSpan::Microseconds(0), timeSpan);
166
167    timeSpan.SetSeconds(0.5e-6);
168    EXPECT_EQ(TimeSpan::Microseconds(1), timeSpan);
169}
170
171/**
172 * @tc.name: TimeSpanTest
173 * @tc.desc: test MultiplicationResultIsRoundedToMicroseconds function
174 * @tc.type: FUNC
175 * @tc.require: I7DMS1
176 */
177HWTEST_F(TimeSpanTest, MultiplicationResultIsRoundedToMicroseconds, TestSize.Level1)
178{
179    EXPECT_EQ(TimeSpan::Microseconds(1), TimeSpan::Microseconds(1) * 0.5f);
180    EXPECT_EQ(TimeSpan::Microseconds(0), TimeSpan::Microseconds(1) * 0.1f);
181}
182
183/**
184 * @tc.name: TimeSpanTest
185 * @tc.desc: test MultiplicationResultIsRoundedToMicroseconds function
186 * @tc.type: FUNC
187 * @tc.require: I7DMS1
188 */
189HWTEST_F(TimeSpanTest, DivisionResultIsRoundedToMicroseconds, TestSize.Level1)
190{
191    EXPECT_EQ(TimeSpan::Microseconds(1), TimeSpan::Microseconds(1) / 2);
192    EXPECT_EQ(TimeSpan::Microseconds(0), TimeSpan::Microseconds(1) / 10);
193    EXPECT_EQ(TimeSpan::Microseconds(1), TimeSpan::Microseconds(1) / 2.f);
194    EXPECT_EQ(TimeSpan::Microseconds(0), TimeSpan::Microseconds(1) / 10.f);
195}
196
197META_END_NAMESPACE()