fix: POSIX 测试用例后续补充

【背景】
kernel_litos_m 中需要补充 POSIX 相关的测试用例

【修改方案】
从 A 核中移植了一部分的POSIX 相关的测试用例
到 sample/posix 目录下

【影响】
对现有的产品编译不会有影响

Signed-off-by: yinjiaming <yinjiaming@huawei.com>
Change-Id: I65adfd00faec185bcad3c1e38e305ce9e8cb9346
This commit is contained in:
yinjiaming 2022-10-27 10:26:29 +08:00
parent 972d765c6e
commit ad4ef11410
17 changed files with 1605 additions and 1 deletions

View File

@ -65,6 +65,19 @@ static_library("test_posix") {
"mutex/It_posix_mutex_034.c",
"mutex/It_posix_mutex_035.c",
"mutex/It_posix_mutex_036.c",
"mutex/It_posix_mutex_037.c",
"mutex/It_posix_mutex_038.c",
"mutex/It_posix_mutex_039.c",
"mutex/It_posix_mutex_040.c",
"mutex/It_posix_mutex_041.c",
"mutex/It_posix_mutex_042.c",
"mutex/It_posix_mutex_043.c",
"mutex/It_posix_mutex_044.c",
"mutex/It_posix_mutex_045.c",
"mutex/It_posix_mutex_046.c",
"mutex/It_posix_mutex_047.c",
"mutex/It_posix_mutex_048.c",
"mutex/It_posix_mutex_049.c",
"pthread/It_posix_pthread.c",
"pthread/It_posix_pthread_001.c",
"pthread/It_posix_pthread_002.c",

View File

@ -115,4 +115,19 @@ VOID ItSuitePosixMutex(void)
ItPosixMux034();
ItPosixMux035();
ItPosixMux036();
ItPosixMux037();
ItPosixMux038();
ItPosixMux039();
ItPosixMux040();
ItPosixMux041();
ItPosixMux042();
ItPosixMux043();
ItPosixMux044();
ItPosixMux045();
ItPosixMux046();
ItPosixMux047();
ItPosixMux048();
#ifndef LOSCFG_KERNEL_SMP
ItPosixMux049();
#endif
}

View File

@ -56,6 +56,10 @@
}
#endif
#define MUTEX_TEST_DEFAULT_PRIO LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO
#define MUTEX_TEST_HIGH_PRIO (MUTEX_TEST_DEFAULT_PRIO - 6)
#define MUTEX_TEST_LOW_PRIO (MUTEX_TEST_DEFAULT_PRIO + 5)
UINT32 PosixPthreadDestroy(pthread_attr_t *attr, pthread_t thread);
UINT32 PosixPthreadInit(pthread_attr_t *attr, int pri);
void TestExtraTaskDelay(UINT32 tick);
@ -96,6 +100,19 @@ VOID ItPosixMux033(void);
VOID ItPosixMux034(void);
VOID ItPosixMux035(void);
VOID ItPosixMux036(void);
VOID ItPosixMux037(void);
VOID ItPosixMux038(void);
VOID ItPosixMux039(void);
VOID ItPosixMux040(void);
VOID ItPosixMux041(void);
VOID ItPosixMux042(void);
VOID ItPosixMux043(void);
VOID ItPosixMux044(void);
VOID ItPosixMux045(void);
VOID ItPosixMux046(void);
VOID ItPosixMux047(void);
VOID ItPosixMux048(void);
VOID ItPosixMux049(void);
VOID ItSuitePosixMutex(void);

View File

@ -119,7 +119,7 @@ static UINT32 Testcase(VOID)
}
/**
* @tc.name: ItPosixMux035
* @tc.name: ItPosixMux036
* @tc.desc: Test interface pthread_mutex_unlock
* @tc.type: FUNC
* @tc.require: issueI5WZI6

View File

@ -0,0 +1,140 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static pthread_mutex_t g_mutex038;
static sem_t g_sem038;
static void *TaskF01(void *arg)
{
int ret;
ret = pthread_mutex_trylock(&g_mutex038);
ICUNIT_GOTO_NOT_EQUAL(ret, 0, ret, EXIT);
ret = sem_post(&g_sem038);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_mutex_lock(&g_mutex038);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = sem_post(&g_sem038);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_mutex_unlock(&g_mutex038);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
EXIT:
return NULL;
}
static UINT32 Testcase(VOID)
{
int ret;
int i;
pthread_mutexattr_t ma;
pthread_t child;
ret = sem_init(&g_sem038, 0, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutexattr_init(&ma);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_init(&g_mutex038, &ma);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutexattr_destroy(&ma);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_lock(&g_mutex038);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_lock(&g_mutex038);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_unlock(&g_mutex038);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_create(&child, NULL, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_wait(&g_sem038);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_unlock(&g_mutex038);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_wait(&g_sem038);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_unlock(&g_mutex038);
ICUNIT_ASSERT_NOT_EQUAL(1, 0, ret);
ret = pthread_join(child, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
for (i = 0; i < 50; i++) { // 50, The loop frequency.
ret = pthread_mutex_lock(&g_mutex038);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
for (i = 0; i < 50; i++) { // 50, The loop frequency.
ret = pthread_mutex_unlock(&g_mutex038);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
ret = pthread_mutex_unlock(&g_mutex038);
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
ret = pthread_mutex_destroy(&g_mutex038);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&g_sem038);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux037
* @tc.desc: Test interface sem_wait
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux037(void)
{
TEST_ADD_CASE("ItPosixMux037", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,102 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
#define THREAD_NUM 6
#define LOOPS 3
static pthread_mutex_t g_mutex040 = PTHREAD_MUTEX_INITIALIZER;
static g_value;
static void *TaskF01(void *parm)
{
int i, tmp;
int rc;
/* Loopd M times to acquire the mutex, increase the value,
and then release the mutex. */
for (i = 0; i < LOOPS; ++i) {
rc = pthread_mutex_lock(&g_mutex040);
if (rc != 0) {
return (void *)(LOS_NOK);
}
tmp = g_value + 1;
usleep(1000); // 1000, delay the increasement operation.
g_value = tmp;
rc = pthread_mutex_unlock(&g_mutex040);
if (rc != 0) {
return (void *)(LOS_NOK);
}
sleep(1);
}
return (void *)(LOS_OK);
}
static UINT32 Testcase(VOID)
{
int i, rc;
pthread_t threads[THREAD_NUM];
g_value = 0;
/* Create threads */
for (i = 0; i < THREAD_NUM; ++i) {
rc = pthread_create(&threads[i], NULL, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(rc, 0, rc);
}
/* Wait to join all threads */
for (i = 0; i < THREAD_NUM; ++i) {
rc = pthread_join(threads[i], NULL);
ICUNIT_ASSERT_EQUAL(rc, 0, rc);
}
pthread_mutex_destroy(&g_mutex040);
/* Check if the final value is as expected */
ICUNIT_ASSERT_EQUAL(g_value, (THREAD_NUM) * LOOPS, LOS_NOK);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux038
* @tc.desc: Test interface pthread_mutex_lock
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux038(void)
{
TEST_ADD_CASE("ItPosixMux038", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static pthread_mutex_t g_mutex056;
static VOID *TaskF01(void *argument)
{
int ret;
ret = pthread_mutex_trylock(&g_mutex056);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
ret = pthread_mutex_unlock(&g_mutex056);
g_testCount++;
EXIT:
return NULL;
}
static UINT32 Testcase(VOID)
{
int ret;
pthread_mutexattr_t mta;
pthread_attr_t attr;
pthread_t newTh;
g_testCount = 0;
ret = pthread_mutexattr_init(&mta);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_init(&g_mutex056, &mta);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr, MUTEX_TEST_HIGH_PRIO);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_create(&newTh, &attr, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_testCount, 1, g_testCount);
ret = pthread_mutex_destroy(&g_mutex056);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutexattr_destroy(&mta);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(newTh, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux039
* @tc.desc: Test interface pthread_mutex_trylock
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux039(void)
{
TEST_ADD_CASE("ItPosixMux039", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,104 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static pthread_mutex_t g_mutex066;
static void *TaskF01(void *argv)
{
UINT32 ret;
g_testCount++;
ret = pthread_mutex_trylock(&g_mutex066);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
ret = pthread_mutex_trylock(&g_mutex066);
ICUNIT_TRACK_EQUAL(ret, EBUSY, ret);
LOS_TaskDelay(2); // 2, set delay time.
ret = pthread_mutex_unlock(&g_mutex066);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
ret = pthread_mutex_unlock(&g_mutex066);
ICUNIT_TRACK_NOT_EQUAL(ret, 0, ret);
g_testCount++;
return NULL;
}
static UINT32 Testcase(VOID)
{
UINT32 ret;
pthread_t newTh;
pthread_attr_t attr;
g_testCount = 0;
ret = pthread_mutex_init(&g_mutex066, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr, MUTEX_TEST_HIGH_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh, &attr, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_testCount, 1, g_testCount);
ret = pthread_mutex_destroy(&g_mutex066);
ICUNIT_ASSERT_EQUAL(ret, EBUSY, ret);
LOS_TaskDelay(3); // 3, set delay time.
ICUNIT_ASSERT_EQUAL(g_testCount, 2, g_testCount); // 2, Assert that g_testCount.
ret = pthread_mutex_destroy(&g_mutex066);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr, newTh);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux040
* @tc.desc: Test interface pthread_mutex_trylock
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux040(void)
{
TEST_ADD_CASE("ItPosixMux040", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,134 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static pthread_mutex_t g_mutex068;
static VOID *TaskF01(void *argv)
{
UINT32 ret;
g_testCount++;
ret = pthread_mutex_lock(&g_mutex068);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
LOS_TaskDelay(30); // 30, set delay time.
ret = pthread_mutex_unlock(&g_mutex068);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
LOS_TaskDelay(100); // 100, set delay time.
return NULL;
}
static VOID *TaskF02(void *argv)
{
UINT32 ret;
g_testCount++;
ret = pthread_mutex_lock(&g_mutex068);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
LOS_TaskDelay(30); // 30, set delay time.
ret = pthread_mutex_unlock(&g_mutex068);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
LOS_TaskDelay(100); // 100, set delay time.
return NULL;
}
static UINT32 Testcase(VOID)
{
UINT32 ret;
pthread_t newTh;
pthread_attr_t attr;
pthread_t newTh2;
pthread_attr_t attr2;
ret = pthread_mutex_init(&g_mutex068, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_testCount = 0;
LOS_TaskLock();
ret = PosixPthreadInit(&attr, MUTEX_TEST_DEFAULT_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh, &attr, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr2, MUTEX_TEST_HIGH_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh2, &attr2, TaskF02, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
LOS_TaskUnlock();
ICUNIT_ASSERT_EQUAL(g_testCount, 3, g_testCount); // 3, Assert that g_testCount.
LOS_TaskDelay(40); // 40, set delay time.
ICUNIT_ASSERT_EQUAL(g_testCount, 4, g_testCount); // 4, Assert that g_testCount.
LOS_TaskDelay(30); // 30, set delay time.
ret = pthread_mutex_destroy(&g_mutex068);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr, newTh);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr2, newTh2);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux041
* @tc.desc: Test interface pthread_mutex_unlock
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux041(void)
{
TEST_ADD_CASE("ItPosixMux041", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,127 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static pthread_mutex_t g_mutex072;
static VOID *TaskF01(void *arg)
{
UINT32 ret;
ret = pthread_mutex_lock(&g_mutex072);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
ICUNIT_TRACK_EQUAL(g_testCount, 0, g_testCount);
g_testCount++;
ret = pthread_mutex_unlock(&g_mutex072);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
LOS_TaskDelay(6); // 6, set delay time.
ICUNIT_TRACK_EQUAL(g_testCount, 4, g_testCount); // 4, Here, assert that g_testCount is equal to 4.
g_testCount++;
return LOS_OK;
}
static VOID *TaskF02(void *arg)
{
UINT32 ret;
ICUNIT_TRACK_EQUAL(g_testCount, 1, g_testCount);
g_testCount++;
ret = pthread_mutex_lock(&g_mutex072);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
ICUNIT_TRACK_EQUAL(g_testCount, 2, g_testCount); // 2, Here, assert that g_testCount is equal to 2.
g_testCount++;
ret = pthread_mutex_unlock(&g_mutex072);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
ICUNIT_TRACK_EQUAL(g_testCount, 3, g_testCount); // 3, Here, assert that g_testCount is equal to 3.
g_testCount++;
return LOS_OK;
}
static UINT32 Testcase(VOID)
{
UINT32 ret;
pthread_t newTh;
pthread_attr_t attr;
pthread_t newTh2;
pthread_attr_t attr2;
ret = pthread_mutex_init(&g_mutex072, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_testCount = 0;
ret = PosixPthreadInit(&attr, MUTEX_TEST_DEFAULT_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh, &attr, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr2, MUTEX_TEST_HIGH_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh2, &attr2, TaskF02, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
LOS_TaskDelay(8); // 8, set delay time.
ICUNIT_ASSERT_EQUAL(g_testCount, 5, g_testCount); // 5, Assert that g_testCount.
ret = pthread_mutex_destroy(&g_mutex072);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr, newTh);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr2, newTh2);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux042
* @tc.desc: Test interface pthread_mutex_lock
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux042(void)
{
TEST_ADD_CASE("ItPosixMux042", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,136 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static pthread_mutex_t g_mutex079;
static sem_t g_sem079;
static int g_pshared084;
static VOID *TaskF01(void *arg)
{
UINT32 ret;
ret = pthread_mutex_trylock(&g_mutex079);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
ret = sem_wait(&g_sem079);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
ret = pthread_mutex_unlock(&g_mutex079);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
return NULL;
}
static VOID *TaskF02(void *arg)
{
UINT32 ret;
ret = sem_post(&g_sem079);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
return NULL;
}
static VOID *TaskF03(void *arg)
{
UINT32 ret;
ret = pthread_mutex_lock(&g_mutex079);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
ret = pthread_mutex_unlock(&g_mutex079);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
return NULL;
}
static UINT32 Testcase(VOID)
{
UINT32 ret;
pthread_t newTh;
pthread_attr_t attr;
pthread_t newTh2;
pthread_attr_t attr2;
pthread_t newTh3;
pthread_attr_t attr3;
ret = pthread_mutex_init(&g_mutex079, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_init(&g_sem079, g_pshared084, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr, MUTEX_TEST_HIGH_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh, &attr, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr2, MUTEX_TEST_HIGH_PRIO - 1);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh2, &attr2, TaskF02, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr3, MUTEX_TEST_HIGH_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh3, &attr3, TaskF03, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_destroy(&g_mutex079);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&g_sem079);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr, newTh);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr2, newTh2);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr3, newTh3);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux043
* @tc.desc: Test interface pthread_mutex_lock
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux043(void)
{
TEST_ADD_CASE("ItPosixMux043", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static UINT32 Testcase(VOID)
{
pthread_mutexattr_t mta;
pthread_mutex_t mutex1 = TEST_MUTEX_INIT;
pthread_mutex_t mutex2 = TEST_MUTEX_INIT;
int rc;
/* Initialize a mutex attributes object */
rc = pthread_mutexattr_init(&mta);
ICUNIT_ASSERT_EQUAL(rc, 0, rc);
/* Initialize mutex1 with the default mutex attributes */
rc = pthread_mutex_init(&mutex1, &mta);
ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT3);
/* Initialize mutex2 with NULL attributes */
rc = pthread_mutex_init(&mutex2, NULL);
ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT2);
/* Destroy the mutex attributes object */
rc = pthread_mutexattr_destroy(&mta);
ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT1);
/* Destroy mutex1 */
rc = pthread_mutex_destroy(&mutex1);
ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT1);
/* Destroy mutex2 */
rc = pthread_mutex_destroy(&mutex2);
ICUNIT_ASSERT_EQUAL(rc, 0, rc);
return LOS_OK;
EXIT1:
pthread_mutex_destroy(&mutex2);
EXIT2:
pthread_mutex_destroy(&mutex1);
EXIT3:
pthread_mutexattr_destroy(&mta);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux044
* @tc.desc: Test interface pthread_mutexattr_destroy
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux044(void)
{
TEST_ADD_CASE("ItPosixMux044", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,103 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static pthread_mutex_t g_mutex046;
static UINT32 g_nID;
static void *TaskF01(void *arg)
{
int ret;
ret = pthread_mutex_trylock(&g_mutex046);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
g_nID = OsCurrTaskGet()->taskID;
LOS_TaskSuspend(OsCurrTaskGet()->taskID);
ret = pthread_mutex_unlock(&g_mutex046);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
EXIT:
return NULL;
}
static UINT32 Testcase(VOID)
{
int ret;
pthread_t newTh;
pthread_attr_t attr;
ret = pthread_mutex_init(&g_mutex046, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_trylock(&g_mutex046);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_trylock(&g_mutex046);
ICUNIT_ASSERT_EQUAL(ret, EBUSY, ret);
ret = pthread_mutex_unlock(&g_mutex046);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr, MUTEX_TEST_HIGH_PRIO);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_create(&newTh, &attr, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_mutex_unlock(&g_mutex046);
ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
LOS_TaskResume(g_nID);
ret = pthread_mutex_unlock(&g_mutex046);
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
ret = pthread_mutex_destroy(&g_mutex046);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(newTh, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux045
* @tc.desc: Test interface pthread_mutex_trylock
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux045(void)
{
TEST_ADD_CASE("ItPosixMux045", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static UINT32 Testcase(VOID)
{
int rc;
pthread_mutex_t mutex;
rc = pthread_mutex_init(&mutex, NULL);
ICUNIT_ASSERT_EQUAL(rc, 0, rc);
rc = pthread_mutex_lock(&mutex);
ICUNIT_ASSERT_EQUAL(rc, 0, rc);
rc = pthread_mutex_trylock(&mutex);
ICUNIT_ASSERT_EQUAL(rc, EBUSY, rc);
rc = pthread_mutex_unlock(&mutex);
ICUNIT_ASSERT_EQUAL(rc, 0, rc);
rc = pthread_mutex_destroy(&mutex);
ICUNIT_ASSERT_EQUAL(rc, 0, rc);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux046
* @tc.desc: Test interface pthread_mutex_trylock
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux046(void)
{
TEST_ADD_CASE("ItPosixMux046", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static pthread_mutex_t g_mutex069;
static VOID *TaskF01(void *argv)
{
UINT32 ret;
ret = pthread_mutex_lock(&g_mutex069);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
LOS_TaskDelay(10); // 10, set delay time.
ret = pthread_mutex_unlock(&g_mutex069);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
ret = pthread_mutex_trylock(&g_mutex069);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
LOS_TaskDelay(10); // 10, set delay time.
ret = pthread_mutex_unlock(&g_mutex069);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
return NULL;
}
static UINT32 Testcase(VOID)
{
UINT32 ret;
pthread_t newTh;
pthread_attr_t attr;
ret = pthread_mutex_init(&g_mutex069, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_testCount = 0;
ret = PosixPthreadInit(&attr, MUTEX_TEST_HIGH_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh, &attr, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_testCount, 0, g_testCount);
ret = pthread_mutex_unlock(&g_mutex069);
ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
LOS_TaskDelay(15); // 15, set delay time.
ICUNIT_ASSERT_EQUAL(g_testCount, 1, g_testCount);
ret = pthread_mutex_unlock(&g_mutex069);
ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
LOS_TaskDelay(10); // 10, set delay time.
ret = pthread_mutex_unlock(&g_mutex069);
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_testCount, 2, g_testCount); // 2, Assert that g_testCount.
ret = pthread_mutex_destroy(&g_mutex069);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr, newTh);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux047
* @tc.desc: Test interface pthread_mutex_unlock
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux047(void)
{
TEST_ADD_CASE("ItPosixMux047", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,174 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static pthread_mutex_t g_mutex074;
static sem_t g_sem074;
static int g_pshared074;
static VOID *TaskF01(void *arg)
{
UINT32 ret;
ret = pthread_mutex_lock(&g_mutex074);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 1, g_testCount);
LOS_TaskDelay(5); // 5, set delay time.
ret = sem_wait(&g_sem074);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 5, g_testCount); // 5, Here, assert that g_testCount is equal to 5.
ret = sem_post(&g_sem074);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 6, g_testCount); // 6, Here, assert that g_testCount is equal to 6.
ret = pthread_mutex_unlock(&g_mutex074);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
return NULL;
}
static VOID *TaskF02(void *arg)
{
UINT32 ret;
LOS_TaskDelay(1);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 2, g_testCount); // 2, Here, assert that g_testCount is equal to 2.
ret = pthread_mutex_lock(&g_mutex074);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 7, g_testCount); // 7, Here, assert that g_testCount is equal to 7.
ret = pthread_mutex_unlock(&g_mutex074);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
return NULL;
}
static VOID *TaskF03(void *arg)
{
UINT32 ret;
LOS_TaskDelay(2); // 2, set delay time.
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 3, g_testCount); // 3, Here, assert that g_testCount is equal to 3.
ret = sem_wait(&g_sem074);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 4, g_testCount); // 4, Here, assert that g_testCount is equal to 4.
LOS_TaskDelay(4); // 4, set delay time.
ret = sem_post(&g_sem074);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 8, g_testCount); // 8, Here, assert that g_testCount is equal to 8.
return NULL;
}
static UINT32 Testcase(VOID)
{
UINT32 ret;
pthread_t newTh;
pthread_attr_t attr;
pthread_t newTh2;
pthread_attr_t attr2;
pthread_t newTh3;
pthread_attr_t attr3;
g_testCount = 0;
ret = pthread_mutex_init(&g_mutex074, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_init(&g_sem074, g_pshared074, 2); // 2, The initial number of available semaphores.
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr, MUTEX_TEST_LOW_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh, &attr, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr2, MUTEX_TEST_HIGH_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh2, &attr2, TaskF02, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr3, MUTEX_TEST_DEFAULT_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh3, &attr3, TaskF03, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
LOS_TaskDelay(10); // 10, set delay time.
ret = pthread_mutex_destroy(&g_mutex074);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&g_sem074);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr, newTh);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr2, newTh2);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr3, newTh3);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux048
* @tc.desc: Test interface sem_post
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux048(void)
{
TEST_ADD_CASE("ItPosixMux048", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}

View File

@ -0,0 +1,173 @@
/*
* Copyright (c) 2022 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 "It_posix_mutex.h"
static pthread_mutex_t g_mutex075;
static sem_t g_sem075;
static int g_pshared075;
static VOID *TaskF01(void *arg)
{
UINT32 ret;
ret = pthread_mutex_lock(&g_mutex075);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 3, g_testCount); // 3, Here, assert that g_testCount is equal to 3.
ret = sem_wait(&g_sem075);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 6, g_testCount); // 6, Here, assert that g_testCount is equal to 6.
ret = sem_post(&g_sem075);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 7, g_testCount); // 7, Here, assert that g_testCount is equal to 7.
ret = pthread_mutex_unlock(&g_mutex075);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
return NULL;
}
static VOID *TaskF02(void *arg)
{
UINT32 ret;
LOS_TaskDelay(2); // 2, set delay time.
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 4, g_testCount); // 4, Here, assert that g_testCount is equal to 4.
ret = pthread_mutex_lock(&g_mutex075);
ICUNIT_TRACK_EQUAL(ret, 0, ret); //
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 8, g_testCount); // 8, Here, assert that g_testCount is equal to 8.
ret = pthread_mutex_unlock(&g_mutex075);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
return NULL;
}
static VOID *TaskF03(void *arg)
{
UINT32 ret;
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 1, g_testCount);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 2, g_testCount); // 2, Here, assert that g_testCount is equal to 2.
LOS_TaskDelay(3); // 3, set delay time.
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 5, g_testCount); // 5, Here, assert that g_testCount is equal to 5.
ret = sem_post(&g_sem075);
ICUNIT_TRACK_EQUAL(ret, 0, ret);
LOS_TaskDelay(1);
g_testCount++;
ICUNIT_TRACK_EQUAL(g_testCount, 9, g_testCount); // 9, Here, assert the g_testCount.
return NULL;
}
static UINT32 Testcase(VOID)
{
UINT32 ret;
pthread_t newTh;
pthread_attr_t attr;
pthread_t newTh2;
pthread_attr_t attr2;
pthread_t newTh3;
pthread_attr_t attr3;
g_testCount = 0;
ret = pthread_mutex_init(&g_mutex075, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_init(&g_sem075, g_pshared075, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr, MUTEX_TEST_DEFAULT_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh, &attr, TaskF03, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr2, MUTEX_TEST_LOW_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh2, &attr2, TaskF01, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadInit(&attr3, MUTEX_TEST_HIGH_PRIO);
ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
ret = pthread_create(&newTh3, &attr3, TaskF02, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
LOS_TaskDelay(5); // 5, set delay time.
ret = pthread_mutex_destroy(&g_mutex075);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&g_sem075);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr, newTh);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr2, newTh2);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = PosixPthreadDestroy(&attr3, newTh3);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return LOS_OK;
}
/**
* @tc.name: ItPosixMux049
* @tc.desc: Test interface sem_wait
* @tc.type: FUNC
* @tc.require: issueI5YAEX
*/
VOID ItPosixMux049(void)
{
TEST_ADD_CASE("ItPosixMux049", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
}