• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <sys/timeb.h>
3 using namespace testing::ext;
4 
5 class TimeFtimeTest : public testing::Test {
SetUp()6     void SetUp() override {}
TearDown()7     void TearDown() override {}
8 };
9 constexpr int NUM = 1000000;
10 /**
11  * @tc.name: ftime_001
12  * @tc.desc: Test the functionality of the ftime function and compares the system time obtained from
13  *           std::chrono::system_clock::now() with the time obtained from ftime function.
14  * @tc.type: FUNC
15  **/
16 HWTEST_F(TimeFtimeTest, ftime_001, TestSize.Level1)
17 {
18     struct timeb tmb;
19     int result = ftime(&tmb);
20     EXPECT_EQ(0, result);
21     ftime(&tmb);
22     auto currentTime = std::chrono::system_clock::now();
23     auto duration = currentTime.time_since_epoch();
24 
25     auto sec = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
26     auto usec = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % NUM;
27 
28     long secDiff = sec - tmb.time;
29     long usecDiff = usec - tmb.millitm;
30     if (usecDiff < 0) {
31         --secDiff;
32         usecDiff += NUM;
33     }
34     long maxSecDiff = 0;
35     EXPECT_LE(secDiff, maxSecDiff);
36 }