13d8536b4Sopenharmony_ci/*
23d8536b4Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
33d8536b4Sopenharmony_ci *
43d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
53d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met:
63d8536b4Sopenharmony_ci *
73d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
83d8536b4Sopenharmony_ci *    conditions and the following disclaimer.
93d8536b4Sopenharmony_ci *
103d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
113d8536b4Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
123d8536b4Sopenharmony_ci *    provided with the distribution.
133d8536b4Sopenharmony_ci *
143d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
153d8536b4Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
163d8536b4Sopenharmony_ci *    permission.
173d8536b4Sopenharmony_ci *
183d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
193d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
203d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
213d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
223d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
233d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
243d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
253d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
263d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
273d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
283d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293d8536b4Sopenharmony_ci */
303d8536b4Sopenharmony_ci
313d8536b4Sopenharmony_ci#include "alarm_test.h"
323d8536b4Sopenharmony_ci
333d8536b4Sopenharmony_ciLITE_TEST_SUIT(TIME, TimeClockTimeTest, TimeClockTimeTestSuite);
343d8536b4Sopenharmony_ci
353d8536b4Sopenharmony_cistatic BOOL TimeClockTimeTestSuiteSetUp(void)
363d8536b4Sopenharmony_ci{
373d8536b4Sopenharmony_ci    return TRUE;
383d8536b4Sopenharmony_ci}
393d8536b4Sopenharmony_ci
403d8536b4Sopenharmony_cistatic BOOL TimeClockTimeTestSuiteTearDown(void)
413d8536b4Sopenharmony_ci{
423d8536b4Sopenharmony_ci    return TRUE;
433d8536b4Sopenharmony_ci}
443d8536b4Sopenharmony_ci
453d8536b4Sopenharmony_ci/**
463d8536b4Sopenharmony_ci * @tc.number SUB_KERNEL_TIME_API_CLOCK_GETTIME_0100
473d8536b4Sopenharmony_ci * @tc.name   test all supported clockid of clock_gettime
483d8536b4Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
493d8536b4Sopenharmony_ci */
503d8536b4Sopenharmony_ciLITE_TEST_CASE(TimeClockTimeTestSuite, testClockGettimeAll, Function | MediumTest | Level1)
513d8536b4Sopenharmony_ci{
523d8536b4Sopenharmony_ci    clockid_t cid = CLOCK_REALTIME;
533d8536b4Sopenharmony_ci    struct timespec time1 = {0, 0};
543d8536b4Sopenharmony_ci    int rt = clock_gettime(cid, &time1);
553d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(rt, 0, rt);
563d8536b4Sopenharmony_ci    return 0;
573d8536b4Sopenharmony_ci}
583d8536b4Sopenharmony_ci
593d8536b4Sopenharmony_ci/**
603d8536b4Sopenharmony_ci * @tc.number SUB_KERNEL_TIME_API_CLOCK_SETTIME_0100
613d8536b4Sopenharmony_ci * @tc.name   test clock_settime basic
623d8536b4Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
633d8536b4Sopenharmony_ci */
643d8536b4Sopenharmony_ciLITE_TEST_CASE(TimeClockTimeTestSuite, testClockSettime, Function | MediumTest | Level1)
653d8536b4Sopenharmony_ci{
663d8536b4Sopenharmony_ci    struct timespec time1 = {0, 0};
673d8536b4Sopenharmony_ci    sleep(1); /* 1, common data for test, no special meaning */
683d8536b4Sopenharmony_ci    int rt = clock_gettime(CLOCK_REALTIME, &time1);
693d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(rt, 0, rt);
703d8536b4Sopenharmony_ci    time_t sec = time1.tv_sec;
713d8536b4Sopenharmony_ci    time1.tv_sec -= 1; /* 1, common data for test, no special meaning */
723d8536b4Sopenharmony_ci    time1.tv_nsec = 1; /* 1, common data for test, no special meaning */
733d8536b4Sopenharmony_ci    rt = clock_settime(CLOCK_REALTIME, &time1);
743d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(rt, 0, rt);
753d8536b4Sopenharmony_ci    sleep(1); /* 1, common data for test, no special meaning */
763d8536b4Sopenharmony_ci    rt = clock_gettime(CLOCK_REALTIME, &time1);
773d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(rt, 0, rt);
783d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(time1.tv_sec, sec, time1.tv_sec);
793d8536b4Sopenharmony_ci    return 0;
803d8536b4Sopenharmony_ci}
813d8536b4Sopenharmony_ci
823d8536b4Sopenharmony_ci/**
833d8536b4Sopenharmony_ci* @tc.number     SUB_KERNEL_TIME_API_GETTIMEOFDAY_0100
843d8536b4Sopenharmony_ci* @tc.name       test gettimeofday api
853d8536b4Sopenharmony_ci* @tc.desc       [C- SOFTWARE -0200]
863d8536b4Sopenharmony_ci*/
873d8536b4Sopenharmony_ciLITE_TEST_CASE(TimeClockTimeTestSuite, testGettimeofday, Function | MediumTest | Level1)
883d8536b4Sopenharmony_ci{
893d8536b4Sopenharmony_ci    int ret;
903d8536b4Sopenharmony_ci    int sleepSec = 1; /* 1, common data for test, no special meaning */
913d8536b4Sopenharmony_ci    struct timeval tValStart = {0};
923d8536b4Sopenharmony_ci    struct timeval tValEnd = {0};
933d8536b4Sopenharmony_ci    struct timezone tZone;
943d8536b4Sopenharmony_ci
953d8536b4Sopenharmony_ci    int ret1 = gettimeofday(&tValStart, &tZone);
963d8536b4Sopenharmony_ci    sleep(sleepSec);
973d8536b4Sopenharmony_ci    int ret2 = gettimeofday(&tValEnd, &tZone);
983d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret1, 0, ret1);
993d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret2, 0, ret2);
1003d8536b4Sopenharmony_ci
1013d8536b4Sopenharmony_ci    ret = (int)(tValEnd.tv_sec - tValStart.tv_sec);
1023d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, sleepSec, ret);
1033d8536b4Sopenharmony_ci    ICUNIT_ASSERT_WITHIN_EQUAL(ret, sleepSec, INT_MAX, ret);
1043d8536b4Sopenharmony_ci    ret = (int)(tValEnd.tv_sec - tValStart.tv_sec);
1053d8536b4Sopenharmony_ci    ICUNIT_ASSERT_WITHIN_EQUAL(ret, INT_MIN, sleepSec + 1, ret); /* 1, common data for test, no special meaning */
1063d8536b4Sopenharmony_ci    return 0;
1073d8536b4Sopenharmony_ci}
1083d8536b4Sopenharmony_ci
1093d8536b4Sopenharmony_ci/**
1103d8536b4Sopenharmony_ci* @tc.number     SUB_KERNEL_TIME_API_SETTIMEOFDAY_0100
1113d8536b4Sopenharmony_ci* @tc.name       test settimeofday api
1123d8536b4Sopenharmony_ci* @tc.desc       [C- SOFTWARE -0200]
1133d8536b4Sopenharmony_ci*/
1143d8536b4Sopenharmony_ciLITE_TEST_CASE(TimeClockTimeTestSuite, testSettimeofday, Function | MediumTest | Level1)
1153d8536b4Sopenharmony_ci{
1163d8536b4Sopenharmony_ci    int ret;
1173d8536b4Sopenharmony_ci    int setSec = 100; /* 100, common data for test, no special meaning */
1183d8536b4Sopenharmony_ci    int sleepSec = 2; /* 2, common data for test, no special meaning */
1193d8536b4Sopenharmony_ci    struct timeval tValStart = {0};
1203d8536b4Sopenharmony_ci    struct timeval tValEnd = {0};
1213d8536b4Sopenharmony_ci    struct timeval set = {.tv_sec = setSec, .tv_usec = 0};
1223d8536b4Sopenharmony_ci
1233d8536b4Sopenharmony_ci    int ret1 = settimeofday(&set, NULL);
1243d8536b4Sopenharmony_ci    int ret2 = gettimeofday(&tValStart, NULL);
1253d8536b4Sopenharmony_ci    sleep(sleepSec);
1263d8536b4Sopenharmony_ci    int ret3 = gettimeofday(&tValEnd, NULL);
1273d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret1, 0, ret1);
1283d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret2, 0, ret2);
1293d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret3, 0, ret3);
1303d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(tValStart.tv_sec, setSec, tValStart.tv_sec);
1313d8536b4Sopenharmony_ci
1323d8536b4Sopenharmony_ci    ret = (int)(tValEnd.tv_sec - tValStart.tv_sec);
1333d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, sleepSec, ret);
1343d8536b4Sopenharmony_ci    ICUNIT_ASSERT_WITHIN_EQUAL(ret, sleepSec, INT_MAX, ret);
1353d8536b4Sopenharmony_ci    ret = (int)(tValEnd.tv_sec - tValStart.tv_sec);
1363d8536b4Sopenharmony_ci    ICUNIT_ASSERT_WITHIN_EQUAL(ret, INT_MIN, sleepSec + 1, ret); /* 1, common data for test, no special meaning */
1373d8536b4Sopenharmony_ci    return 0;
1383d8536b4Sopenharmony_ci}
1393d8536b4Sopenharmony_ci
1403d8536b4Sopenharmony_ciRUN_TEST_SUITE(TimeClockTimeTestSuite);
1413d8536b4Sopenharmony_ci
1423d8536b4Sopenharmony_civoid ClockTimeTest(void)
1433d8536b4Sopenharmony_ci{
1443d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testClockGettimeAll);
1453d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testClockSettime);
1463d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testGettimeofday);
1473d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testSettimeofday);
1483d8536b4Sopenharmony_ci}
149