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 "xts_ipc.h"
323d8536b4Sopenharmony_ci
333d8536b4Sopenharmony_cistatic int g_semTestStep = 0;
343d8536b4Sopenharmony_ci
353d8536b4Sopenharmony_ciLITE_TEST_SUIT(IPC, SemApi, IpcSemApiTestSuite);
363d8536b4Sopenharmony_ci
373d8536b4Sopenharmony_cistatic BOOL IpcSemApiTestSuiteSetUp(void)
383d8536b4Sopenharmony_ci{
393d8536b4Sopenharmony_ci    return TRUE;
403d8536b4Sopenharmony_ci}
413d8536b4Sopenharmony_ci
423d8536b4Sopenharmony_cistatic BOOL IpcSemApiTestSuiteTearDown(void)
433d8536b4Sopenharmony_ci{
443d8536b4Sopenharmony_ci    return TRUE;
453d8536b4Sopenharmony_ci}
463d8536b4Sopenharmony_ci
473d8536b4Sopenharmony_ciLITE_TEST_CASE(IpcSemApiTestSuite, testSemInit0100, Function | MediumTest | Level2)
483d8536b4Sopenharmony_ci{
493d8536b4Sopenharmony_ci    int ret;
503d8536b4Sopenharmony_ci    sem_t sem;
513d8536b4Sopenharmony_ci    int testValue[3] = {0, 1, 10}; /* 3, 1, 10 common data for test, no special meaning */
523d8536b4Sopenharmony_ci
533d8536b4Sopenharmony_ci    int index = (int)(sizeof(testValue) / sizeof(int));
543d8536b4Sopenharmony_ci    for (int i = 0; i < index; i++) {
553d8536b4Sopenharmony_ci
563d8536b4Sopenharmony_ci        ret = sem_init((sem_t *)&sem, 0, testValue[0]);
573d8536b4Sopenharmony_ci        ICUNIT_ASSERT_EQUAL(ret, 0, ret);
583d8536b4Sopenharmony_ci
593d8536b4Sopenharmony_ci        ret = sem_destroy(&sem);
603d8536b4Sopenharmony_ci        ICUNIT_ASSERT_EQUAL(ret, 0, ret);
613d8536b4Sopenharmony_ci    }
623d8536b4Sopenharmony_ci    return 0;
633d8536b4Sopenharmony_ci}
643d8536b4Sopenharmony_ci
653d8536b4Sopenharmony_ciLITE_TEST_CASE(IpcSemApiTestSuite, testSemPost0100, Function | MediumTest | Level2)
663d8536b4Sopenharmony_ci{
673d8536b4Sopenharmony_ci    int ret;
683d8536b4Sopenharmony_ci    sem_t sem;
693d8536b4Sopenharmony_ci
703d8536b4Sopenharmony_ci    ret = sem_init((sem_t *)&sem, 0, 0);
713d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
723d8536b4Sopenharmony_ci
733d8536b4Sopenharmony_ci    ret = sem_post(&sem);
743d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
753d8536b4Sopenharmony_ci
763d8536b4Sopenharmony_ci    ret = sem_post(&sem);
773d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
783d8536b4Sopenharmony_ci
793d8536b4Sopenharmony_ci    ret = sem_post(&sem);
803d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
813d8536b4Sopenharmony_ci
823d8536b4Sopenharmony_ci    ret = sem_destroy(&sem);
833d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
843d8536b4Sopenharmony_ci    return 0;
853d8536b4Sopenharmony_ci}
863d8536b4Sopenharmony_ci
873d8536b4Sopenharmony_ciLITE_TEST_CASE(IpcSemApiTestSuite, testSemWait0100, Function | MediumTest | Level2)
883d8536b4Sopenharmony_ci{
893d8536b4Sopenharmony_ci    int ret;
903d8536b4Sopenharmony_ci    sem_t sem;
913d8536b4Sopenharmony_ci
923d8536b4Sopenharmony_ci    ret = sem_init((sem_t *)&sem, 0, 3); /* 3, common data for test, no special meaning */
933d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
943d8536b4Sopenharmony_ci
953d8536b4Sopenharmony_ci    ret = sem_wait(&sem);
963d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
973d8536b4Sopenharmony_ci
983d8536b4Sopenharmony_ci    ret = sem_wait(&sem);
993d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1003d8536b4Sopenharmony_ci
1013d8536b4Sopenharmony_ci    ret = sem_wait(&sem);
1023d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1033d8536b4Sopenharmony_ci
1043d8536b4Sopenharmony_ci    ret = sem_destroy(&sem);
1053d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1063d8536b4Sopenharmony_ci    return 0;
1073d8536b4Sopenharmony_ci}
1083d8536b4Sopenharmony_ci
1093d8536b4Sopenharmony_civoid *ThreadChat(void *arg)
1103d8536b4Sopenharmony_ci{
1113d8536b4Sopenharmony_ci    int ret;
1123d8536b4Sopenharmony_ci    sem_t *sem = (sem_t *)arg;
1133d8536b4Sopenharmony_ci
1143d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(g_semTestStep, 0, g_semTestStep);
1153d8536b4Sopenharmony_ci
1163d8536b4Sopenharmony_ci    g_semTestStep = 1; /* 1, common data for test, no special meaning */
1173d8536b4Sopenharmony_ci    ret = sem_wait(sem);
1183d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1193d8536b4Sopenharmony_ci
1203d8536b4Sopenharmony_ci    g_semTestStep = 2; /* 2, common data for test, no special meaning */
1213d8536b4Sopenharmony_ci    return NULL;
1223d8536b4Sopenharmony_ci}
1233d8536b4Sopenharmony_ci
1243d8536b4Sopenharmony_ciLITE_TEST_CASE(IpcSemApiTestSuite, testThreadChat0100, Function | MediumTest | Level3)
1253d8536b4Sopenharmony_ci{
1263d8536b4Sopenharmony_ci    pthread_t tid;
1273d8536b4Sopenharmony_ci    sem_t sem;
1283d8536b4Sopenharmony_ci    int ret;
1293d8536b4Sopenharmony_ci    struct timespec req;
1303d8536b4Sopenharmony_ci    g_semTestStep = 0;
1313d8536b4Sopenharmony_ci
1323d8536b4Sopenharmony_ci    ret = sem_init((sem_t *)&sem, 0, 0);
1333d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1343d8536b4Sopenharmony_ci
1353d8536b4Sopenharmony_ci    ret = pthread_create(&tid, NULL, ThreadChat, (void *)&sem);
1363d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1373d8536b4Sopenharmony_ci
1383d8536b4Sopenharmony_ci    req.tv_sec = 0;
1393d8536b4Sopenharmony_ci    req.tv_nsec = TEN_CONT * NANO_MS;
1403d8536b4Sopenharmony_ci    ret = nanosleep(&req, NULL);
1413d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1423d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(g_semTestStep, 1, g_semTestStep); /* 1, common data for test, no special meaning */
1433d8536b4Sopenharmony_ci
1443d8536b4Sopenharmony_ci    ret = sem_post(&sem);
1453d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1463d8536b4Sopenharmony_ci
1473d8536b4Sopenharmony_ci    ret = nanosleep(&req, NULL);
1483d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1493d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(g_semTestStep, 2, g_semTestStep); /* 2, common data for test, no special meaning */
1503d8536b4Sopenharmony_ci
1513d8536b4Sopenharmony_ci    ret = sem_post(&sem);
1523d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(g_semTestStep, 2, g_semTestStep); /* 2, common data for test, no special meaning */
1533d8536b4Sopenharmony_ci
1543d8536b4Sopenharmony_ci    ret = pthread_join(tid, NULL);
1553d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1563d8536b4Sopenharmony_ci
1573d8536b4Sopenharmony_ci    ret = sem_destroy(&sem);
1583d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1593d8536b4Sopenharmony_ci    return 0;
1603d8536b4Sopenharmony_ci}
1613d8536b4Sopenharmony_ci
1623d8536b4Sopenharmony_civoid *ThreadNThreadWait1(void *arg)
1633d8536b4Sopenharmony_ci{
1643d8536b4Sopenharmony_ci    int ret;
1653d8536b4Sopenharmony_ci    sem_t *sem = (sem_t *)arg;
1663d8536b4Sopenharmony_ci    struct timespec req;
1673d8536b4Sopenharmony_ci
1683d8536b4Sopenharmony_ci    req.tv_sec = 0;
1693d8536b4Sopenharmony_ci    req.tv_nsec = HUNDRED_CONT * NANO_MS;
1703d8536b4Sopenharmony_ci
1713d8536b4Sopenharmony_ci    ret = nanosleep(&req, NULL);
1723d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1733d8536b4Sopenharmony_ci    g_semTestStep = 1; /* 1, common data for test, no special meaning */
1743d8536b4Sopenharmony_ci    ret = sem_wait(sem);
1753d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1763d8536b4Sopenharmony_ci
1773d8536b4Sopenharmony_ci    g_semTestStep = 2; /* 2, common data for test, no special meaning */
1783d8536b4Sopenharmony_ci    return NULL;
1793d8536b4Sopenharmony_ci}
1803d8536b4Sopenharmony_ci
1813d8536b4Sopenharmony_civoid *ThreadNThreadWait2(void *arg)
1823d8536b4Sopenharmony_ci{
1833d8536b4Sopenharmony_ci    int ret;
1843d8536b4Sopenharmony_ci    sem_t *sem = (sem_t *)arg;
1853d8536b4Sopenharmony_ci    struct timespec req;
1863d8536b4Sopenharmony_ci
1873d8536b4Sopenharmony_ci    req.tv_sec = 0;
1883d8536b4Sopenharmony_ci    req.tv_nsec = 300 * NANO_MS; /* 300, common data for test, no special meaning */
1893d8536b4Sopenharmony_ci    ret = nanosleep(&req, NULL);
1903d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1913d8536b4Sopenharmony_ci    g_semTestStep = 3; /* 3, common data for test, no special meaning */
1923d8536b4Sopenharmony_ci
1933d8536b4Sopenharmony_ci    req.tv_nsec = 200 * NANO_MS; /* 200, common data for test, no special meaning */
1943d8536b4Sopenharmony_ci    ret = nanosleep(&req, NULL);
1953d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1963d8536b4Sopenharmony_ci    ret = sem_wait(sem);
1973d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1983d8536b4Sopenharmony_ci
1993d8536b4Sopenharmony_ci    g_semTestStep = 4; /* 4, common data for test, no special meaning */
2003d8536b4Sopenharmony_ci    return NULL;
2013d8536b4Sopenharmony_ci}
2023d8536b4Sopenharmony_ci
2033d8536b4Sopenharmony_ciLITE_TEST_CASE(IpcSemApiTestSuite, testThreadChat0400, Function | MediumTest | Level4)
2043d8536b4Sopenharmony_ci{
2053d8536b4Sopenharmony_ci    pthread_t tid1;
2063d8536b4Sopenharmony_ci    pthread_t tid2;
2073d8536b4Sopenharmony_ci    sem_t sem;
2083d8536b4Sopenharmony_ci    int ret;
2093d8536b4Sopenharmony_ci    struct timespec req;
2103d8536b4Sopenharmony_ci
2113d8536b4Sopenharmony_ci    req.tv_sec = 0;
2123d8536b4Sopenharmony_ci    req.tv_nsec = 200 * NANO_MS; /* 200, common data for test, no special meaning */
2133d8536b4Sopenharmony_ci    g_semTestStep = 0;
2143d8536b4Sopenharmony_ci
2153d8536b4Sopenharmony_ci    ret = sem_init((sem_t *)&sem, 0, 0);
2163d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2173d8536b4Sopenharmony_ci
2183d8536b4Sopenharmony_ci    ret = pthread_create(&tid1, NULL, ThreadNThreadWait1, (void *)&sem);
2193d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2203d8536b4Sopenharmony_ci
2213d8536b4Sopenharmony_ci    ret = pthread_create(&tid2, NULL, ThreadNThreadWait2, (void *)&sem);
2223d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2233d8536b4Sopenharmony_ci
2243d8536b4Sopenharmony_ci    ret = nanosleep(&req, NULL);
2253d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2263d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(g_semTestStep, 1, g_semTestStep); /* 1, common data for test, no special meaning */
2273d8536b4Sopenharmony_ci
2283d8536b4Sopenharmony_ci    ret = sem_post(&sem);
2293d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2303d8536b4Sopenharmony_ci
2313d8536b4Sopenharmony_ci    req.tv_nsec = 20 * NANO_MS; /* 20, common data for test, no special meaning */
2323d8536b4Sopenharmony_ci    ret = nanosleep(&req, NULL);
2333d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2343d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(g_semTestStep, 2, g_semTestStep); /* 2, common data for test, no special meaning */
2353d8536b4Sopenharmony_ci
2363d8536b4Sopenharmony_ci    req.tv_nsec = 200 * NANO_MS; /* 200, common data for test, no special meaning */
2373d8536b4Sopenharmony_ci    ret = nanosleep(&req, NULL);
2383d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2393d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(g_semTestStep, 3, g_semTestStep); /* 3, common data for test, no special meaning */
2403d8536b4Sopenharmony_ci
2413d8536b4Sopenharmony_ci    ret = sem_post(&sem);
2423d8536b4Sopenharmony_ci    req.tv_nsec = 20 * NANO_MS; /* 20, common data for test, no special meaning */
2433d8536b4Sopenharmony_ci    ret = nanosleep(&req, NULL);
2443d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2453d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(g_semTestStep, 3, g_semTestStep); /* 3, common data for test, no special meaning */
2463d8536b4Sopenharmony_ci
2473d8536b4Sopenharmony_ci    ret = pthread_join(tid1, NULL);
2483d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2493d8536b4Sopenharmony_ci
2503d8536b4Sopenharmony_ci    ret = pthread_join(tid2, NULL);
2513d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2523d8536b4Sopenharmony_ci
2533d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(g_semTestStep, 4, g_semTestStep); /* 4, common data for test, no special meaning */
2543d8536b4Sopenharmony_ci
2553d8536b4Sopenharmony_ci    ret = sem_destroy(&sem);
2563d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2573d8536b4Sopenharmony_ci    return 0;
2583d8536b4Sopenharmony_ci}
2593d8536b4Sopenharmony_ci
2603d8536b4Sopenharmony_ciLITE_TEST_CASE(IpcSemApiTestSuite, testSemInitAbnormal0200, Function | MediumTest | Level3)
2613d8536b4Sopenharmony_ci{
2623d8536b4Sopenharmony_ci    int ret;
2633d8536b4Sopenharmony_ci    sem_t sem;
2643d8536b4Sopenharmony_ci    unsigned int gtSemMax = (unsigned int)SEM_VALUE_MAX + 1; /* 1, common data for test, no special meaning */
2653d8536b4Sopenharmony_ci
2663d8536b4Sopenharmony_ci    ret = sem_init(&sem, 0, SEM_VALUE_MAX);
2673d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2683d8536b4Sopenharmony_ci
2693d8536b4Sopenharmony_ci    ret = sem_destroy(&sem);
2703d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2713d8536b4Sopenharmony_ci
2723d8536b4Sopenharmony_ci    ret = sem_init(&sem, 0, gtSemMax);
2733d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
2743d8536b4Sopenharmony_ci
2753d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
2763d8536b4Sopenharmony_ci
2773d8536b4Sopenharmony_ci    ret = sem_init(&sem, 0, 1); /* 1, common data for test, no special meaning */
2783d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2793d8536b4Sopenharmony_ci
2803d8536b4Sopenharmony_ci    ret = sem_destroy(&sem);
2813d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2823d8536b4Sopenharmony_ci    return 0;
2833d8536b4Sopenharmony_ci}
2843d8536b4Sopenharmony_ci
2853d8536b4Sopenharmony_ciLITE_TEST_CASE(IpcSemApiTestSuite, testSemPostAbnormal, Function | MediumTest | Level3)
2863d8536b4Sopenharmony_ci{
2873d8536b4Sopenharmony_ci    int ret;
2883d8536b4Sopenharmony_ci    sem_t sem;
2893d8536b4Sopenharmony_ci
2903d8536b4Sopenharmony_ci    ret = sem_init(&sem, 0, SEM_VALUE_MAX);
2913d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2923d8536b4Sopenharmony_ci
2933d8536b4Sopenharmony_ci    ret = sem_post(&sem);
2943d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2953d8536b4Sopenharmony_ci
2963d8536b4Sopenharmony_ci    ret = sem_destroy(&sem);
2973d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
2983d8536b4Sopenharmony_ci    return 0;
2993d8536b4Sopenharmony_ci}
3003d8536b4Sopenharmony_ci
3013d8536b4Sopenharmony_ciLITE_TEST_CASE(IpcSemApiTestSuite, testSemTimedWaitAbnormalA, Function | MediumTest | Level3)
3023d8536b4Sopenharmony_ci{
3033d8536b4Sopenharmony_ci    int ret;
3043d8536b4Sopenharmony_ci    struct timespec ts;
3053d8536b4Sopenharmony_ci    sem_t sem;
3063d8536b4Sopenharmony_ci
3073d8536b4Sopenharmony_ci    ret = sem_init(&sem, 0, 0);
3083d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
3093d8536b4Sopenharmony_ci
3103d8536b4Sopenharmony_ci    ts.tv_sec = 0;
3113d8536b4Sopenharmony_ci    ts.tv_nsec = -2; /* -2, common data for test, no special meaning */
3123d8536b4Sopenharmony_ci    ret = sem_timedwait(&sem, &ts);
3133d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
3143d8536b4Sopenharmony_ci
3153d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
3163d8536b4Sopenharmony_ci
3173d8536b4Sopenharmony_ci    ret = sem_destroy(&sem);
3183d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
3193d8536b4Sopenharmony_ci    return 0;
3203d8536b4Sopenharmony_ci}
3213d8536b4Sopenharmony_ci
3223d8536b4Sopenharmony_ciLITE_TEST_CASE(IpcSemApiTestSuite, testSemTimedWaitAbnormalB, Function | MediumTest | Level3)
3233d8536b4Sopenharmony_ci{
3243d8536b4Sopenharmony_ci    int ret;
3253d8536b4Sopenharmony_ci    struct timespec ts;
3263d8536b4Sopenharmony_ci    sem_t sem;
3273d8536b4Sopenharmony_ci
3283d8536b4Sopenharmony_ci    ret = sem_init(&sem, 0, 0);
3293d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
3303d8536b4Sopenharmony_ci
3313d8536b4Sopenharmony_ci    ts.tv_sec = time(NULL);
3323d8536b4Sopenharmony_ci    ts.tv_nsec = NANO_S;
3333d8536b4Sopenharmony_ci    ret = sem_timedwait(&sem, &ts);
3343d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
3353d8536b4Sopenharmony_ci
3363d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
3373d8536b4Sopenharmony_ci
3383d8536b4Sopenharmony_ci    ret = sem_destroy(&sem);
3393d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
3403d8536b4Sopenharmony_ci    return 0;
3413d8536b4Sopenharmony_ci}
3423d8536b4Sopenharmony_ci
3433d8536b4Sopenharmony_ciRUN_TEST_SUITE(IpcSemApiTestSuite);
3443d8536b4Sopenharmony_ci
3453d8536b4Sopenharmony_civoid PosixSemFuncTest()
3463d8536b4Sopenharmony_ci{
3473d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testSemInit0100);
3483d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testSemPost0100);
3493d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testSemWait0100);
3503d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testThreadChat0100);
3513d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testThreadChat0400);
3523d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testSemInitAbnormal0200);
3533d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testSemPostAbnormal);
3543d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testSemTimedWaitAbnormalA);
3553d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testSemTimedWaitAbnormalB);
3563d8536b4Sopenharmony_ci}