From f1b147aed10ee195e4585e0d0ebc74b54f0feea1 Mon Sep 17 00:00:00 2001 From: zwx1232718 Date: Tue, 13 Jun 2023 16:41:52 +0800 Subject: [PATCH] =?UTF-8?q?test:=E6=B5=8B=E8=AF=95=E7=94=A8=E5=8A=9B?= =?UTF-8?q?=E8=A1=A5=E9=BD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zwx1232718 --- testsuites/BUILD.gn | 6 +- testsuites/include/osTest.h | 1 + testsuites/src/osTest.c | 7 + testsuites/unittest/xts/BUILD.gn | 35 ++ testsuites/unittest/xts/ipc/BUILD.gn | 46 ++ .../unittest/xts/ipc/cond/PthreadCondTest.c | 193 ++++++++ .../unittest/xts/ipc/mutex/PthreadMutexTest.c | 151 ++++++ .../unittest/xts/ipc/semaphone/sem_test.c | 348 ++++++++++++++ .../unittest/xts/ipc/semaphone/semabn_test.c | 191 ++++++++ .../unittest/xts/ipc/semaphone/semxts_test.c | 428 ++++++++++++++++++ testsuites/unittest/xts/ipc/xts_ipc.c | 43 ++ testsuites/unittest/xts/ipc/xts_ipc.h | 65 +++ testsuites/unittest/xts/xts_test.c | 36 ++ testsuites/unittest/xts/xts_test.h | 54 +++ 14 files changed, 1603 insertions(+), 1 deletion(-) create mode 100644 testsuites/unittest/xts/BUILD.gn create mode 100644 testsuites/unittest/xts/ipc/BUILD.gn create mode 100644 testsuites/unittest/xts/ipc/cond/PthreadCondTest.c create mode 100644 testsuites/unittest/xts/ipc/mutex/PthreadMutexTest.c create mode 100644 testsuites/unittest/xts/ipc/semaphone/sem_test.c create mode 100644 testsuites/unittest/xts/ipc/semaphone/semabn_test.c create mode 100644 testsuites/unittest/xts/ipc/semaphone/semxts_test.c create mode 100644 testsuites/unittest/xts/ipc/xts_ipc.c create mode 100644 testsuites/unittest/xts/ipc/xts_ipc.h create mode 100644 testsuites/unittest/xts/xts_test.c create mode 100644 testsuites/unittest/xts/xts_test.h diff --git a/testsuites/BUILD.gn b/testsuites/BUILD.gn index b3e34749..9c088101 100644 --- a/testsuites/BUILD.gn +++ b/testsuites/BUILD.gn @@ -48,7 +48,10 @@ kernel_module("test_init") { "src/osTest.c", ] - include_dirs = [ "unittest/posix/src" ] + include_dirs = [ + "unittest/posix/src", + "unittest/xts", + ] configs += [ ":include" ] } @@ -69,6 +72,7 @@ group("testsuites") { "sample/kernel/task:test_task", "sample/posix:test_posix", "unittest/posix:posix_test", + "unittest/xts:xts_test", ] if (defined(LOSCFG_DYNLINK)) { deps += [ "sample/kernel/dynlink:test_dynlink" ] diff --git a/testsuites/include/osTest.h b/testsuites/include/osTest.h index 78a4ddea..93199aa0 100644 --- a/testsuites/include/osTest.h +++ b/testsuites/include/osTest.h @@ -96,6 +96,7 @@ extern "C" { #define LOS_KERNEL_LMK_TEST 0 #define LOS_KERNEL_SIGNAL_TEST 0 +#define LOS_XTS_TEST 1 #define LOS_POSIX_TEST 1 #define LOS_CMSIS_TEST 1 #define LOS_CMSIS2_CORE_TASK_TEST 0 diff --git a/testsuites/src/osTest.c b/testsuites/src/osTest.c index cd214d66..0c4e3342 100644 --- a/testsuites/src/osTest.c +++ b/testsuites/src/osTest.c @@ -38,6 +38,9 @@ #if (LOS_POSIX_TEST == 1) #include "posix_test.h" #endif +#if (LOS_XTS_TEST == 1) +#include "xts_test.h" +#endif UINT32 volatile g_testCount; UINT32 g_testTskHandle; @@ -246,6 +249,10 @@ VOID TestTaskEntry(VOID) CmsisFuncTestSuite(); #endif +#if(LOS_XTS_TEST == 1) + XtsTestSuite(); +#endif + /* The log is used for testing entrance guard, please do not make any changes. */ PRINTF("\nfailed count:%d, success count:%d\n", g_failResult, g_passResult); PRINTF("--- Test End ---\n"); diff --git a/testsuites/unittest/xts/BUILD.gn b/testsuites/unittest/xts/BUILD.gn new file mode 100644 index 00000000..688315e3 --- /dev/null +++ b/testsuites/unittest/xts/BUILD.gn @@ -0,0 +1,35 @@ +# Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//kernel/liteos_m/liteos.gni") + +static_library("xts_test") { + sources = [ "xts_test.c" ] + deps = [ "ipc:ipc_test" ] + configs += [ "$LITEOSTOPDIR/testsuites:include" ] +} diff --git a/testsuites/unittest/xts/ipc/BUILD.gn b/testsuites/unittest/xts/ipc/BUILD.gn new file mode 100644 index 00000000..c2cd9005 --- /dev/null +++ b/testsuites/unittest/xts/ipc/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//kernel/liteos_m/liteos.gni") + +static_library("ipc_test") { + sources = [ + "cond/PthreadCondTest.c", + "mutex/PthreadMutexTest.c", + "semaphone/sem_test.c", + "semaphone/semabn_test.c", + "semaphone/semxts_test.c", + "xts_ipc.c", + ] + + include_dirs = [ + ".", + "$LITEOSTOPDIR/testsuites/include", + "$LITEOSTOPDIR/testsuites/unittest/xts", + ] +} diff --git a/testsuites/unittest/xts/ipc/cond/PthreadCondTest.c b/testsuites/unittest/xts/ipc/cond/PthreadCondTest.c new file mode 100644 index 00000000..e1f011ca --- /dev/null +++ b/testsuites/unittest/xts/ipc/cond/PthreadCondTest.c @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "xts_ipc.h" + +pthread_mutex_t g_mtx3 = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t g_cond3 = PTHREAD_COND_INITIALIZER; + +LITE_TEST_SUIT(FUTEX, PthreadCondApiTest, PthreadCondApiTestSuite); + +static BOOL PthreadCondApiTestSuiteSetUp(void) +{ + return TRUE; +} + +static BOOL PthreadCondApiTestSuiteTearDown(void) +{ + printf("+-------------------------------------------+\n"); + return TRUE; +} + +/** + * @tc.number SUB_KERNEL_PTHREAD_COND_INIT_0100 + * @tc.name pthread_cond_init initializes condition variables + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PthreadCondApiTestSuite, testPthreadCondInit, Function | MediumTest | Level2) +{ + int ret; + pthread_condattr_t condAttr; + + ret = pthread_condattr_init(&condAttr); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + + pthread_cond_t cond1; + ret = pthread_cond_init(&cond1, &condAttr); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + + pthread_cond_t cond2; + ret = pthread_cond_init(&cond2, NULL); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); +} + +/** + * @tc.number SUB_KERNEL_PTHREAD_COND_DESTROY_0100 + * @tc.name pthread_cond_destroy destroy condition variables + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PthreadCondApiTestSuite, testPthreadCondDestroy, Function | MediumTest | Level3) +{ + int ret; + pthread_condattr_t condAttr; + ret = pthread_condattr_init(&condAttr); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + + pthread_cond_t cond1; + ret = pthread_cond_init(&cond1, &condAttr); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_cond_destroy(&cond1); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + + pthread_cond_t cond2; + ret = pthread_cond_init(&cond2, NULL); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_cond_destroy(&cond2); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + + pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER; + ret =pthread_cond_destroy(&cond3); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); +} + +/** + * @tc.number SUB_KERNEL_PTHREAD_CONDATTR_INIT_0100 + * @tc.name Init and destroy operations + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PthreadCondApiTestSuite, testPthreadCondattrInit, Function | MediumTest | Level2) +{ + int ret; + pthread_condattr_t condAttr; + + ret = pthread_condattr_init(&condAttr); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret =pthread_condattr_destroy(&condAttr); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_condattr_init(&condAttr); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); +} + +// pthread_cond_broadcast +void *ThreadPthreadCondBroadcast1(void *arg) +{ + int ret; + int *testIntP = (int *)arg; + usleep(20); /* 20, common data for test, no special meaning */ + ret = pthread_mutex_lock(&g_mtx3); + ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT); + *testIntP = TEST_INTP_SIZE; + ret = pthread_cond_broadcast(&g_cond3); + ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT); + ret = pthread_mutex_unlock(&g_mtx3); + ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT); + return arg; +EXIT: + return NULL; +} + +// pthread_cond_wait +void *ThreadPthreadCondBroadcast2(void *arg) +{ + int ret; + int *testIntP = (int *)arg; + ret = pthread_mutex_lock(&g_mtx3); + ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT); + ret = pthread_cond_wait(&g_cond3, &g_mtx3); + ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT); + (*testIntP)++; + ret = pthread_mutex_unlock(&g_mtx3); + ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT); + return arg; +EXIT: + return NULL; +} + +/** + * @tc.number SUB_KERNEL_PTHREAD_COND_BROADCAST_0100 + * @tc.name Use pthread_cond_broadcast to release conditional semaphore + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PthreadCondApiTestSuite, testPthreadCondBroadcast, Function | MediumTest | Level3) +{ + int ret; + pthread_t tid[3]; /* 3, common data for test, no special meaning */ + int testInt = 0; + + ret = pthread_create(&tid[0], NULL, ThreadPthreadCondBroadcast1, (void*)&testInt); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_create(&tid[1], NULL, ThreadPthreadCondBroadcast2, (void*)&testInt); /* 1, common data for test, no special meaning */ + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_create(&tid[2], NULL, ThreadPthreadCondBroadcast2, (void*)&testInt); /* 2, common data for test, no special meaning */ + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + + usleep(100); /* 100, common data for test, no special meaning */ + int index = (int)(sizeof(tid) / sizeof(tid[0])); + for (int i = 0; i < index; i++) { + ret = pthread_join(tid[i], NULL); + ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); + } +EXIT: + ret = pthread_cond_destroy(&g_cond3); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_mutex_destroy(&g_mtx3); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ICUNIT_ASSERT_EQUAL(testInt, TEST_SEEK_SIZE, testInt); +} + +RUN_TEST_SUITE(PthreadCondApiTestSuite); + +void PosixFutexCondTest(void) +{ + RUN_ONE_TESTCASE(testPthreadCondInit); + RUN_ONE_TESTCASE(testPthreadCondDestroy); + RUN_ONE_TESTCASE(testPthreadCondattrInit); + RUN_ONE_TESTCASE(testPthreadCondBroadcast); +} \ No newline at end of file diff --git a/testsuites/unittest/xts/ipc/mutex/PthreadMutexTest.c b/testsuites/unittest/xts/ipc/mutex/PthreadMutexTest.c new file mode 100644 index 00000000..1c0df2a4 --- /dev/null +++ b/testsuites/unittest/xts/ipc/mutex/PthreadMutexTest.c @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "xts_ipc.h" + +LITE_TEST_SUIT(FUTEX, PthreadMutexApiTest, PthreadMutexApiTestSuite); + +static BOOL PthreadMutexApiTestSuiteSetUp(void) +{ + return TRUE; +} + +static BOOL PthreadMutexApiTestSuiteTearDown(void) +{ + printf("+-------------------------------------------+\n"); + return TRUE; +} + +/********************************************* Test case dividing line ***********************************************/ + +struct PthreadMutexCond { + int loopNum; + int countMax; + int count; + int top; + int bottom; + pthread_cond_t notfull; + pthread_cond_t notempty; + pthread_mutex_t mutex; +}; + +struct PthreadMutexCond g_st1; +void G_St1Init(void) +{ + memset_s(&g_st1, sizeof(struct PthreadMutexCond), 0, sizeof(struct PthreadMutexCond)); + g_st1.loopNum = 10; /* 10, common data for test, no special meaning */ + g_st1.countMax = 5; /* 5, common data for test, no special meaning */ + g_st1.count = 0; + g_st1.top = 0; + g_st1.bottom = 0; + g_st1.notfull = (pthread_cond_t)PTHREAD_COND_INITIALIZER; + g_st1.notempty = (pthread_cond_t)PTHREAD_COND_INITIALIZER; + g_st1.mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; +} + +void *PthreadProduce(void *arg) +{ + int ret; + for (int i = 0; i < g_st1.loopNum; i++) { + ret = pthread_mutex_lock(&g_st1.mutex); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + // check full + if ((g_st1.top + 1) % g_st1.countMax == g_st1.bottom) { /* 1, common data for test, no special meaning */ + ret = pthread_cond_wait(&g_st1.notempty, &g_st1.mutex); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + } + // Produce + g_st1.top = (g_st1.top + 1) % g_st1.countMax; /* 1, common data for test, no special meaning */ + g_st1.count++; + + ret = pthread_cond_signal(&g_st1.notempty); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_mutex_unlock(&g_st1.mutex); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + usleep(10); /* 10, common data for test, no special meaning */ + } + return arg; +} + +void *PthreadConsume(void *arg) +{ + int ret; + for (int i = 0; i < g_st1.loopNum; i++) { + ret = pthread_mutex_lock(&g_st1.mutex); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + // check empty + if (g_st1.top == g_st1.bottom) { + ret = pthread_cond_wait(&g_st1.notempty, &g_st1.mutex); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + } + // Consume + g_st1.bottom = (g_st1.bottom + 1) % g_st1.countMax; + g_st1.count--; + + ret = pthread_cond_signal(&g_st1.notempty); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_mutex_unlock(&g_st1.mutex); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + + usleep(10); /* 10, common data for test, no special meaning */ + } + return arg; +} + +/** + * @tc.number SUB_KERNEL_FUTEX_MUTEX_ALL_0300 + * @tc.name test pthread_mutex with condition variable, produce and consume + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PthreadMutexApiTestSuite, testPthreadMutexCond, Function | MediumTest | Level3) +{ + int ret; + pthread_t tid[2]; /* 2, common data for test, no special meaning */ + + g_st1.count = 0; + memset_s(tid, sizeof(tid), POSIX_IPC_NO_ERROR, sizeof(tid)); + + ret = pthread_create(&tid[0], NULL, PthreadProduce, NULL); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_create(&tid[1], NULL, PthreadConsume, NULL); /* 1, common data for test, no special meaning */ + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_join(tid[0], NULL); + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ret = pthread_join(tid[1], NULL); /* 1, common data for test, no special meaning */ + ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret); + ICUNIT_ASSERT_EQUAL(g_st1.count, POSIX_IPC_NO_ERROR, g_st1.count); +} + +RUN_TEST_SUITE(PthreadMutexApiTestSuite); + +void PosixFutexMutexTest(void) +{ + RUN_ONE_TESTCASE(testPthreadMutexCond); +} diff --git a/testsuites/unittest/xts/ipc/semaphone/sem_test.c b/testsuites/unittest/xts/ipc/semaphone/sem_test.c new file mode 100644 index 00000000..2d1e1bcd --- /dev/null +++ b/testsuites/unittest/xts/ipc/semaphone/sem_test.c @@ -0,0 +1,348 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "xts_ipc.h" + +static int g_semTestStep = 0; + +LITE_TEST_SUIT(IPC, SemApi, IpcSemApiTestSuite); + +static BOOL IpcSemApiTestSuiteSetUp(void) +{ + return TRUE; +} + +static BOOL IpcSemApiTestSuiteTearDown(void) +{ + printf("+-------------------------------------------+\n"); + return TRUE; +} + +LITE_TEST_CASE(IpcSemApiTestSuite, testSemInit0100, Function | MediumTest | Level2) +{ + int ret; + sem_t sem; + int testValue[3] = {0, 1, 10}; /* 3, 1, 10 common data for test, no special meaning */ + + int index = (int)(sizeof(testValue) / sizeof(int)); + for (int i = 0; i < index; i++) { + + ret = sem_init((sem_t *)&sem, 0, testValue[0]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + } +} + +LITE_TEST_CASE(IpcSemApiTestSuite, testSemPost0100, Function | MediumTest | Level2) +{ + int ret; + sem_t sem; + + ret = sem_init((sem_t *)&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +LITE_TEST_CASE(IpcSemApiTestSuite, testSemWait0100, Function | MediumTest | Level2) +{ + int ret; + sem_t sem; + + ret = sem_init((sem_t *)&sem, 0, 3); /* 3, common data for test, no special meaning */ + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_wait(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_wait(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_wait(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +void *ThreadChat(void *arg) +{ + int ret; + sem_t *sem = (sem_t *)arg; + + ICUNIT_ASSERT_EQUAL(g_semTestStep, 0, g_semTestStep); + + g_semTestStep = 1; /* 1, common data for test, no special meaning */ + ret = sem_wait(sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + g_semTestStep = 2; /* 2, common data for test, no special meaning */ + return NULL; +} + +LITE_TEST_CASE(IpcSemApiTestSuite, testThreadChat0100, Function | MediumTest | Level3) +{ + pthread_t tid; + sem_t sem; + int ret; + struct timespec req; + g_semTestStep = 0; + + ret = sem_init((sem_t *)&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_create(&tid, NULL, ThreadChat, (void *)&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + req.tv_sec = 0; + req.tv_nsec = TEN_CONT * NANO_MS; + ret = nanosleep(&req, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(g_semTestStep, 1, g_semTestStep); /* 1, common data for test, no special meaning */ + + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = nanosleep(&req, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(g_semTestStep, 2, g_semTestStep); /* 2, common data for test, no special meaning */ + + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(g_semTestStep, 2, g_semTestStep); /* 2, common data for test, no special meaning */ + + ret = pthread_join(tid, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +void *ThreadNThreadWait1(void *arg) +{ + int ret; + sem_t *sem = (sem_t *)arg; + struct timespec req; + + req.tv_sec = 0; + req.tv_nsec = HUNDRED_CONT * NANO_MS; + + ret = nanosleep(&req, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + g_semTestStep = 1; /* 1, common data for test, no special meaning */ + ret = sem_wait(sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + g_semTestStep = 2; /* 2, common data for test, no special meaning */ + return NULL; +} + +void *ThreadNThreadWait2(void *arg) +{ + int ret; + sem_t *sem = (sem_t *)arg; + struct timespec req; + + req.tv_sec = 0; + req.tv_nsec = 300 * NANO_MS; /* 300, common data for test, no special meaning */ + ret = nanosleep(&req, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + g_semTestStep = 3; /* 3, common data for test, no special meaning */ + + req.tv_nsec = 200 * NANO_MS; /* 200, common data for test, no special meaning */ + ret = nanosleep(&req, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ret = sem_wait(sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + g_semTestStep = 4; /* 4, common data for test, no special meaning */ + return NULL; +} + +LITE_TEST_CASE(IpcSemApiTestSuite, testThreadChat0400, Function | MediumTest | Level4) +{ + pthread_t tid1; + pthread_t tid2; + sem_t sem; + int ret; + struct timespec req; + + req.tv_sec = 0; + req.tv_nsec = 200 * NANO_MS; /* 200, common data for test, no special meaning */ + g_semTestStep = 0; + + ret = sem_init((sem_t *)&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_create(&tid1, NULL, ThreadNThreadWait1, (void *)&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_create(&tid2, NULL, ThreadNThreadWait2, (void *)&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = nanosleep(&req, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(g_semTestStep, 1, g_semTestStep); /* 1, common data for test, no special meaning */ + + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + req.tv_nsec = 20 * NANO_MS; /* 20, common data for test, no special meaning */ + ret = nanosleep(&req, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(g_semTestStep, 2, g_semTestStep); /* 2, common data for test, no special meaning */ + + req.tv_nsec = 200 * NANO_MS; /* 200, common data for test, no special meaning */ + ret = nanosleep(&req, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(g_semTestStep, 3, g_semTestStep); /* 3, common data for test, no special meaning */ + + ret = sem_post(&sem); + req.tv_nsec = 20 * NANO_MS; /* 20, common data for test, no special meaning */ + ret = nanosleep(&req, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(g_semTestStep, 3, g_semTestStep); /* 3, common data for test, no special meaning */ + + ret = pthread_join(tid1, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_join(tid2, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ICUNIT_ASSERT_EQUAL(g_semTestStep, 4, g_semTestStep); /* 4, common data for test, no special meaning */ + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +LITE_TEST_CASE(IpcSemApiTestSuite, testSemInitAbnormal0200, Function | MediumTest | Level3) +{ + int ret; + sem_t sem; + unsigned int gtSemMax = (unsigned int)SEM_VALUE_MAX + 1; /* 1, common data for test, no special meaning */ + + ret = sem_init(&sem, 0, SEM_VALUE_MAX); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_init(&sem, 0, gtSemMax); + ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno); + + ret = sem_init(&sem, 0, 1); /* 1, common data for test, no special meaning */ + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +LITE_TEST_CASE(IpcSemApiTestSuite, testSemPostAbnormal, Function | MediumTest | Level3) +{ + int ret; + sem_t sem; + + ret = sem_init(&sem, 0, SEM_VALUE_MAX); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +LITE_TEST_CASE(IpcSemApiTestSuite, testSemTimedWaitAbnormalA, Function | MediumTest | Level3) +{ + int ret; + struct timespec ts; + sem_t sem; + + ret = sem_init(&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ts.tv_sec = 0; + ts.tv_nsec = -2; /* -2, common data for test, no special meaning */ + ret = sem_timedwait(&sem, &ts); + ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +LITE_TEST_CASE(IpcSemApiTestSuite, testSemTimedWaitAbnormalB, Function | MediumTest | Level3) +{ + int ret; + struct timespec ts; + sem_t sem; + + ret = sem_init(&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ts.tv_sec = time(NULL); + ts.tv_nsec = NANO_S; + ret = sem_timedwait(&sem, &ts); + ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +RUN_TEST_SUITE(IpcSemApiTestSuite); + +void PosixSemFuncTest() +{ + RUN_ONE_TESTCASE(testSemInit0100); + RUN_ONE_TESTCASE(testSemPost0100); + RUN_ONE_TESTCASE(testSemWait0100); + RUN_ONE_TESTCASE(testThreadChat0100); + RUN_ONE_TESTCASE(testThreadChat0400); + RUN_ONE_TESTCASE(testSemInitAbnormal0200); + RUN_ONE_TESTCASE(testSemPostAbnormal); + RUN_ONE_TESTCASE(testSemTimedWaitAbnormalA); + RUN_ONE_TESTCASE(testSemTimedWaitAbnormalB); +} \ No newline at end of file diff --git a/testsuites/unittest/xts/ipc/semaphone/semabn_test.c b/testsuites/unittest/xts/ipc/semaphone/semabn_test.c new file mode 100644 index 00000000..e55d67c5 --- /dev/null +++ b/testsuites/unittest/xts/ipc/semaphone/semabn_test.c @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "xts_ipc.h" + +LITE_TEST_SUIT(IPC, SemAbnormalTestSuite, SemAbnormalTestSuite); + +static BOOL SemAbnormalTestSuiteSetUp(void) +{ + return TRUE; +} + +static BOOL SemAbnormalTestSuiteTearDown(void) +{ + printf("+-------------------------------------------+\n"); + return TRUE; +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_INIT_0200 + * @tc.name Use sem_init initialized value when value is SEM_VALUE_MAX + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(SemAbnormalTestSuite, testSemInitAbnormalSemvaluemax, Function | MediumTest | Level3) +{ + int ret; + sem_t sem; + int semValue = 0; + + ret = sem_init(&sem, 0, SEM_VALUE_MAX); + ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ICUNIT_ASSERT_EQUAL(SEM_VALUE_MAX, semValue, SEM_VALUE_MAX); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_INIT_0210 + * @tc.name Use sem_init initialized value when value is greater than SEM_VALUE_MAX + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(SemAbnormalTestSuite, testSemInitAbnormalGtsemvaluemax, Function | MediumTest | Level3) +{ + int ret; + sem_t sem; + unsigned int gtSemMax = (unsigned int)SEM_VALUE_MAX + 1; /* 1, common data for test, no special meaning */ + + ret = sem_init(&sem, 0, gtSemMax); + ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT); /* -1, common data for test, no special meaning */ + ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno); +EXIT: + return; +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_INIT_0220 + * @tc.name Use sem_init initialized value twice + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(SemAbnormalTestSuite, testSemInitAbnormalInitTwice, Function | MediumTest | Level3) +{ + int ret; + sem_t sem; + + ret = sem_init(&sem, 0, 1); /* 1, common data for test, no special meaning */ + ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_POST_0200 + * @tc.name sem_post increases the semaphore count near the maximum value + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(SemAbnormalTestSuite, testSemPostAbnormal, Function | MediumTest | Level3) +{ + int ret; + sem_t sem; + + ret = sem_init(&sem, 0, SEM_VALUE_MAX); + ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ret = sem_post(&sem); + ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT); + ICUNIT_GOTO_EQUAL(errno, EOVERFLOW, errno, EXIT); /* -1, common data for test, no special meaning */ + +EXIT: + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_0200 + * @tc.name sem_timedwait get semaphore, wait time abnormal, tv_nsec less than 0 + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(SemAbnormalTestSuite, testSemTimedwaitAbnormalA, Function | MediumTest | Level3) +{ + int ret; + struct timespec ts = {0}; + sem_t sem; + int semValue = 0; + + ret = sem_init(&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ts.tv_sec = time(NULL); + ts.tv_nsec = -2; /* -2, common data for test, no special meaning */ + ret = sem_timedwait(&sem, &ts); + ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT); /* -1, common data for test, no special meaning */ + ICUNIT_GOTO_EQUAL(errno, EINVAL, errno, EXIT); + +EXIT: + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_0300 + * @tc.name sem_timedwait get semaphore, wait time abnormal + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(SemAbnormalTestSuite, testSemTimedwaitAbnormalB, Function | MediumTest | Level3) +{ + int ret; + struct timespec ts = {0}; + sem_t sem; + int semValue = 0; + + ret = sem_init(&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ts.tv_sec = time(NULL); + ts.tv_nsec = KERNEL_NS_PER_SECOND; + ret = sem_timedwait(&sem, &ts); + ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT); /* -1, common data for test, no special meaning */ + ICUNIT_GOTO_EQUAL(errno, EINVAL, errno, EXIT); +EXIT: + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +RUN_TEST_SUITE(SemAbnormalTestSuite); + +void PosixSemAbnFuncTest() +{ + RUN_ONE_TESTCASE(testSemInitAbnormalSemvaluemax); + RUN_ONE_TESTCASE(testSemInitAbnormalInitTwice); + RUN_ONE_TESTCASE(testSemTimedwaitAbnormalA); + RUN_ONE_TESTCASE(testSemTimedwaitAbnormalB); +} diff --git a/testsuites/unittest/xts/ipc/semaphone/semxts_test.c b/testsuites/unittest/xts/ipc/semaphone/semxts_test.c new file mode 100644 index 00000000..36100bbf --- /dev/null +++ b/testsuites/unittest/xts/ipc/semaphone/semxts_test.c @@ -0,0 +1,428 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "xts_ipc.h" + +LITE_TEST_SUIT(IPC, SemApi, IpcSemApiXtsTestSuite); + +static BOOL IpcSemApiXtsTestSuiteSetUp(void) +{ + return TRUE; +} + +static BOOL IpcSemApiXtsTestSuiteTearDown(void) +{ + printf("+-------------------------------------------+\n"); + return TRUE; +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_INIT_0100 + * @tc.name Use sem_init initialize the semaphore with 0 + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemInitA, Function | MediumTest | Level2) +{ + int ret; + sem_t sem; + int semValue = 0; + int testValue = 0; + + ret = sem_init(&sem, 0, testValue); + ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ICUNIT_ASSERT_EQUAL(semValue, testValue, semValue); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_INIT_0110 + * @tc.name Use sem_init initialize the semaphore with 1 + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemInitB, Function | MediumTest | Level2) +{ + int ret; + sem_t sem; + int semValue = 0; + int testValue = 1; /* 1, common data for test, no special meaning */ + + ret = sem_init(&sem, 0, testValue); + ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ICUNIT_ASSERT_EQUAL(semValue, testValue, semValue); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_INIT_0120 + * @tc.name Use sem_init initialize the semaphore with 100 + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemInitC, Function | MediumTest | Level2) +{ + int ret; + sem_t sem; + int semValue = 0; + int testValue = 10; /* 10, common data for test, no special meaning */ + + ret = sem_init(&sem, 0, testValue); + ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ICUNIT_ASSERT_EQUAL(semValue, testValue, semValue); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_POST_0100 + * @tc.name sem_post increases the semaphore count + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemPost, Function | MediumTest | Level2) +{ + int ret; + sem_t sem; + int semValue = 0; + + ret = sem_init(&sem, 0, 0); + ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */ + + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 3, semValue); /* 3, common data for test, no special meaning */ + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_WAIT_0100 + * @tc.name sem_wait get semaphore + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemWait, Function | MediumTest | Level2) +{ + int ret; + sem_t sem; + int semValue = 0; + + ret = sem_init(&sem, 0, 3); /* 3, common data for test, no special meaning */ + ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ + + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 3, semValue); /* 3, common data for test, no special meaning */ + + ret = sem_wait(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 2, semValue); /* 2, common data for test, no special meaning */ + + ret = sem_wait(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_wait(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_DESTROY_0100 + * @tc.name check sem_destroy function + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemdestroy, Function | MediumTest | Level3) +{ + int ret; + sem_t sem; + + ret = sem_init(&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +void *ThreadChat_F01(void *arg) +{ + int ret; + sem_t *sem = (sem_t*)arg; + int semValue = 0; + + ret = sem_getvalue(sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + ret = sem_wait(sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + return NULL; +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_CHAT_0100 + * @tc.name Inter-thread communication, check sem_wait function + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(IpcSemApiXtsTestSuite, testThreadChat, Function | MediumTest | Level3) +{ + pthread_t tid; + sem_t sem; + int reInt; + int semValue = 0; + + reInt = sem_init(&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + + reInt = pthread_create(&tid, NULL, ThreadChat_F01, (void*)&sem); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + + usleep(20); /* 20, common data for test, no special meaning */ + reInt = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + + reInt = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + reInt = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + + usleep(20); /* 20, common data for test, no special meaning */ + reInt = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */ + + reInt = pthread_join(tid, NULL); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + reInt = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); +} + +void *ThreadSemTimedWait(void *arg) +{ + int ret; + struct timespec ts = {0}; + sem_t *sem = (sem_t*)arg; + int semValue = 0; + + ret = sem_getvalue(sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec = ts.tv_sec + (ts.tv_nsec + KERNEL_100MS_BY_NS) / KERNEL_NS_PER_SECOND; + ts.tv_nsec = (ts.tv_nsec + KERNEL_100MS_BY_NS) % KERNEL_NS_PER_SECOND; + usleep(50); /* 50, common data for test, no special meaning */ + ret = sem_timedwait(sem, &ts); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ret = sem_getvalue(sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + usleep(100); /* 100, common data for test, no special meaning */ + ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */ + return NULL; +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_CHAT_0300 + * @tc.name Inter-thread communication, check sem_timedwait function + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(IpcSemApiXtsTestSuite, testThreadSemTimedWait, Function | MediumTest | Level3) +{ + int ret; + pthread_t tid; + sem_t sem; + int reInt; + int semValue = 0; + + reInt = sem_init(&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + + reInt = pthread_create(&tid, NULL, ThreadSemTimedWait, (void*)&sem); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + + usleep(10); /* 10, common data for test, no special meaning */ + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + reInt = pthread_join(tid, NULL); + ret = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */ + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); +} + +void *ThreadNThreadWait_F01(void *arg) +{ + int ret; + sem_t *sem = (sem_t*)arg; + int semValue = 0; + + usleep(100); /* 100, common data for test, no special meaning */ + ret = sem_getvalue(sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + ret = sem_wait(sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + return NULL; +} + +void *ThreadNThreadWait_F02(void *arg) +{ + int ret; + sem_t *sem = (sem_t*)arg; + int semValue = 0; + + ret = sem_getvalue(sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + + usleep(300); /* 300, common data for test, no special meaning */ + ret = sem_getvalue(sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + + usleep(200); /* 200, common data for test, no special meaning */ + ret = sem_getvalue(sem, &semValue); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */ + ret = sem_wait(sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + return NULL; +} + +/** + * @tc.number SUB_KERNEL_IPC_SEM_CHAT_0400 + * @tc.name N threads wait, main thread post + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(IpcSemApiXtsTestSuite, testNThreadWait, Function | MediumTest | Level4) +{ + pthread_t tid1; + pthread_t tid2; + sem_t sem; + int reInt; + int semValue = 0; + + reInt = sem_init(&sem, 0, 0); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + + reInt = pthread_create(&tid1, NULL, ThreadNThreadWait_F01, (void*)&sem); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + + reInt = pthread_create(&tid2, NULL, ThreadNThreadWait_F02, (void*)&sem); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + + usleep(200); /* 200, common data for test, no special meaning */ + reInt = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + reInt = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + + usleep(20); /* 20, common data for test, no special meaning */ + reInt = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + + usleep(200); /* 200, common data for test, no special meaning */ + reInt = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + ICUNIT_ASSERT_EQUAL(semValue, 0, semValue); + reInt = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + + usleep(20); /* 20, common data for test, no special meaning */ + reInt = sem_getvalue(&sem, &semValue); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */ + + reInt = pthread_join(tid1, NULL); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + reInt = pthread_join(tid2, NULL); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); + reInt = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(reInt, 0, reInt); +} + +RUN_TEST_SUITE(IpcSemApiXtsTestSuite); + +void PosixSemXtsFuncTest() +{ + RUN_ONE_TESTCASE(testSemInitA); + RUN_ONE_TESTCASE(testSemInitB); + RUN_ONE_TESTCASE(testSemInitC); + RUN_ONE_TESTCASE(testSemPost); + RUN_ONE_TESTCASE(testSemWait); + RUN_ONE_TESTCASE(testSemdestroy); + RUN_ONE_TESTCASE(testThreadChat); +} diff --git a/testsuites/unittest/xts/ipc/xts_ipc.c b/testsuites/unittest/xts/ipc/xts_ipc.c new file mode 100644 index 00000000..7e3bc406 --- /dev/null +++ b/testsuites/unittest/xts/ipc/xts_ipc.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "xts_test.h" + +void IpcSemApiTest(void) +{ + PosixFutexCondTest(); + PosixFutexMutexTest(); + + PosixSemFuncTest(); + PosixSemAbnFuncTest(); + PosixSemXtsFuncTest(); + + return; +} \ No newline at end of file diff --git a/testsuites/unittest/xts/ipc/xts_ipc.h b/testsuites/unittest/xts/ipc/xts_ipc.h new file mode 100644 index 00000000..03f88937 --- /dev/null +++ b/testsuites/unittest/xts/ipc/xts_ipc.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef XTS_IPC_H +#define XTS_IPC_H + +#include "xts_test.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define NANO_S 1000000000 +#define NANO_MS 1000000 + +#define TEN_CONT 10 +#define FIFTY_CONT 50 +#define HUNDRED_CONT 100 + +#define TEST_INTP_SIZE 10 /* 10, common data for test, no special meaning */ +#define TEST_SEEK_SIZE 12 /* 12, common data for test, no special meaning */ +#define POSIX_IPC_NO_ERROR 0 /* 0, common data for test, no special meaning */ + +#define KERNEL_NS_PER_SECOND 1000000000 +#define SEM_VALUE_MAX 0xFFFE + +#define KERNEL_NS_PER_SECOND 1000000000 +#define KERNEL_100MS_BY_NS 100000000 +#define RUN_ONE_TESTCASE(caseName) ADD_TEST_CASE(caseName) +#define AUTO_RUN_ONE_TESTCASEFUNC(func) UnityDefaultTestRun(func, __FILE__, __LINE__) +#endif \ No newline at end of file diff --git a/testsuites/unittest/xts/xts_test.c b/testsuites/unittest/xts/xts_test.c new file mode 100644 index 00000000..edfeb2e6 --- /dev/null +++ b/testsuites/unittest/xts/xts_test.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "xts_test.h" + +void XtsTestSuite(void) +{ + IpcSemApiTest(); +} diff --git a/testsuites/unittest/xts/xts_test.h b/testsuites/unittest/xts/xts_test.h new file mode 100644 index 00000000..f5478c3e --- /dev/null +++ b/testsuites/unittest/xts/xts_test.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _XTS_TEST_H +#define _XTS_TEST_H + +#include +#include "iCunit.h" + +#define TEST_STR(func) ItLos##func +#define TEST_TO_STR(x) #x +#define TEST_HEAD_TO_STR(x) TEST_TO_STR(x) +#define ADD_TEST_CASE(func) \ + TEST_ADD_CASE(TEST_HEAD_TO_STR(TEST_STR(func)), func, TEST_LOS, TEST_TASK, TEST_LEVEL0, TEST_FUNCTION) + +#define LITE_TEST_SUIT(subsystem, module, testsuit) +#define LITE_TEST_CASE(module, function, flag) static int function(void) +#define RUN_TEST_SUITE(testsuit) + +#define TEST_ASSERT_EQUAL_FLOAT(expected, actual) \ + ICUNIT_ASSERT_EQUAL(((expected) == (actual)) || (isnan(expected) && isnan(actual)), TRUE, 0) + +void XtsTestSuite(void); + +extern void IpcSemApiTest(void); + +#endif -- Gitee