1 /*
2 * Copyright (C) 2024 HiHope Open Source Organization.
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 <iostream>
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <sys/times.h>
20 #include "securec.h"
21 #include <ctime>
22 using namespace testing::ext;
23 class HatsTimesApiTest : public testing::Test {
24 public:
25 static void SetUpTestCase();
26 static void TearDownTestCase();
27 void SetUp();
28 void TearDown();
29 private:
30 };
SetUp()31 void HatsTimesApiTest::SetUp()
32 {
33 }
TearDown()34 void HatsTimesApiTest::TearDown()
35 {
36 }
SetUpTestCase()37 void HatsTimesApiTest::SetUpTestCase()
38 {
39 }
TearDownTestCase()40 void HatsTimesApiTest::TearDownTestCase()
41 {
42 }
43
44 /*
45 * @tc.number : SUB_KERNEL_SYSCALL_TIMES_0100
46 * @tc.name : TimesBasicSuccess_0001
47 * @tc.desc : Test the times basic functionality.
48 * @tc.size : MediumTest
49 * @tc.type : Function
50 * @tc.level : Level 1
51 */
52 HWTEST_F(HatsTimesApiTest, TimesBasicSuccess_0001, Function | MediumTest | Level1)
53 {
54 struct tms tms1, tms2;
55 clock_t startTime = 0;
56 int counts = 30000000;
57 int syscounts = 100000;
58 double threshold = 1.0;
59 double duration = 0;
60 clock_t init = 0;
61 clock_t start = times(&tms1);
62 EXPECT_GE(start, init);
63 clock_t end = 0;
64 volatile int j = 0;
65 startTime = clock();
66 do {
67 for (volatile int i = 0; i < counts; i++) {
68 j++;
69 }
70 for (int k = 0; k < syscounts; k++) {
71 end = times(&tms2);
72 }
73
74 if (tms2.tms_utime > tms1.tms_utime && tms2.tms_stime > tms1.tms_stime) {
75 break;
76 }
77 } while ((duration = static_cast<double>(clock() - startTime) / CLOCKS_PER_SEC) < threshold);
78 EXPECT_GT(end, start);
79 EXPECT_GT(tms2.tms_utime, tms1.tms_utime);
80 EXPECT_GT(tms2.tms_stime, tms1.tms_stime);
81 }