• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include <random>
20 #include <thread>
21 
22 #include "libpandabase/utils/utils.h"
23 #include "runtime/include/mem/panda_string.h"
24 #include "runtime/include/runtime.h"
25 #include "runtime/include/time_utils.h"
26 
27 #ifndef PANDA_NIGHTLY_TEST_ON
28 constexpr size_t ITERATION = 64;
29 #else
30 constexpr size_t ITERATION = 1024;
31 #endif
32 
33 namespace ark::time::test {
34 
35 class TimeTest : public testing::Test {
36 public:
TimeTest()37     TimeTest()
38     {
39         RuntimeOptions options;
40         options.SetShouldLoadBootPandaFiles(false);
41         options.SetShouldInitializeIntrinsics(false);
42         Runtime::Create(options);
43         thread_ = ark::MTManagedThread::GetCurrent();
44         thread_->ManagedCodeBegin();
45     }
46 
~TimeTest()47     ~TimeTest() override
48     {
49         thread_->ManagedCodeEnd();
50         Runtime::Destroy();
51     }
52 
53     NO_COPY_SEMANTIC(TimeTest);
54     NO_MOVE_SEMANTIC(TimeTest);
55 
56 private:
57     ark::MTManagedThread *thread_;
58 };
59 
TEST_F(TimeTest,TimerTest)60 TEST_F(TimeTest, TimerTest)
61 {
62     uint64_t duration = 0;
63     {
64         Timer timer(&duration);
65         // NOLINTNEXTLINE(readability-magic-numbers)
66         std::this_thread::sleep_for(std::chrono::nanoseconds(10_I));
67     }
68     ASSERT_GT(duration, 0);
69 
70     uint64_t lastDuration = duration;
71     {
72         Timer timer(&duration);
73         // NOLINTNEXTLINE(readability-magic-numbers)
74         std::this_thread::sleep_for(std::chrono::nanoseconds(10_I));
75     }
76     ASSERT_GT(duration, lastDuration);
77 
78     {
79         Timer timer(&duration, true);
80         ASSERT_EQ(duration, 0);
81     }
82     // There is some nondeterminism in sleep, moreover with small values
83     // and check (duration_ < last_duration_) may fail
84     ASSERT_GT(duration, 0);
85 }
86 
TEST_F(TimeTest,CurrentTimeStringTest)87 TEST_F(TimeTest, CurrentTimeStringTest)
88 {
89     constexpr auto PATTERN =
90         "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}";
91     for (size_t i = 0; i < ITERATION; i++) {
92         auto date = GetCurrentTimeString();
93         ASSERT_EQ(date.size(), 19U);
94         ASSERT_THAT(date.c_str(), ::testing::MatchesRegex(PATTERN));
95         // NOLINTNEXTLINE(readability-magic-numbers)
96         std::this_thread::sleep_for(std::chrono::milliseconds(10_I));
97     }
98 }
99 
100 }  // namespace ark::time::test
101