• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "alarm_test.h"
32 
33 time_t g_time = 18880; /* 18880, common data for test, no special meaning */
34 size_t g_zero = 0;
35 
36 LITE_TEST_SUIT(TIME, TimeUtilsTest, TimeUtilsTestSuite);
37 
TimeUtilsTestSuiteSetUp(void)38 static BOOL TimeUtilsTestSuiteSetUp(void)
39 {
40     return TRUE;
41 }
42 
TimeUtilsTestSuiteTearDown(void)43 static BOOL TimeUtilsTestSuiteTearDown(void)
44 {
45     return TRUE;
46 }
47 
48 /**
49 * @tc.number     SUB_KERNEL_TIME_API_MKTIME_0100
50 * @tc.name       test mktime api
51 * @tc.desc       [C- SOFTWARE -0200]
52 */
53 LITE_TEST_CASE(TimeUtilsTestSuite, testMktime, Function | MediumTest | Level2)
54 {
55     struct tm *localTime = NULL;
56     struct tm timeptr = {0};
57     struct timeval tv;
58     struct timezone tz;
59 
60     int ret = gettimeofday(&tv, &tz);
61     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
62     long sysTimezone = (long)(-tz.tz_minuteswest) * SECS_PER_MIN;
63 
64     INIT_TM(timeptr, 2000, 6, 9, 10, 10, 0, 7); /* 2000, 6, 9, 10, 7, common data for test, no special meaning */
65     time_t timeRet = mktime(&timeptr);
66     ICUNIT_ASSERT_EQUAL(sysTimezone, timeptr.__tm_gmtoff, sysTimezone);
67     ICUNIT_ASSERT_EQUAL(timeRet, 963137400 - timeptr.__tm_gmtoff, timeRet); /* 963137400, common data for test, no special meaning */
68     localTime = localtime(&g_time);
69     ICUNIT_ASSERT_NOT_EQUAL(localTime, NULL, localTime);
70     time_t timep = mktime(localTime);
71     ICUNIT_ASSERT_EQUAL(timep, 18880, timep); /* 18880, common data for test, no special meaning */
72     return 0;
73 }
74 
75 RUN_TEST_SUITE(TimeUtilsTestSuite);
76 
TimeUtilsTest(void)77 void TimeUtilsTest(void)
78 {
79     RUN_ONE_TESTCASE(testMktime);
80 }
81