Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_46254178
rt-thread
提交
141e6a03
R
rt-thread
项目概览
weixin_46254178
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
141e6a03
编写于
8月 12, 2021
作者:
T
tyustli
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[add] add semaphore testcase src code and Kconfig
上级
a6df4391
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
567 addition
and
0 deletion
+567
-0
examples/utest/testcases/kernel/Kconfig
examples/utest/testcases/kernel/Kconfig
+5
-0
examples/utest/testcases/kernel/SConscript
examples/utest/testcases/kernel/SConscript
+3
-0
examples/utest/testcases/kernel/semaphore_tc.c
examples/utest/testcases/kernel/semaphore_tc.c
+559
-0
未找到文件。
examples/utest/testcases/kernel/Kconfig
浏览文件 @
141e6a03
...
...
@@ -5,4 +5,9 @@ config UTEST_MEMHEAP_TC
default y
depends on RT_USING_MEMHEAP
config UTEST_SEMAPHORE_TC
bool "semaphore test"
default n
depends on RT_USING_SEMAPHORE
endmenu
examples/utest/testcases/kernel/SConscript
浏览文件 @
141e6a03
...
...
@@ -8,6 +8,9 @@ src = Split('''
if
GetDepend
([
'UTEST_MEMHEAP_TC'
]):
src
+=
[
'memheap_tc.c'
]
if
GetDepend
([
'UTEST_SEMAPHORE_TC'
]):
src
+=
[
'semaphore_tc.c'
]
CPPPATH
=
[
cwd
]
group
=
DefineGroup
(
'utestcases'
,
src
,
depend
=
[],
CPPPATH
=
CPPPATH
)
...
...
examples/utest/testcases/kernel/semaphore_tc.c
0 → 100644
浏览文件 @
141e6a03
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-08-12 luckyzjq the first version
*/
#include <rtthread.h>
#include <stdlib.h>
#include "utest.h"
static
struct
rt_semaphore
static_semaphore
;
#ifdef RT_USING_HEAP
static
rt_sem_t
dynamic_semaphore
;
#endif
/* RT_USING_HEAP */
static
void
test_static_semaphore_init
(
void
)
{
rt_err_t
result
;
int
rand_num
=
rand
()
%
0x10000
;
for
(
int
i
=
0
;
i
<
rand_num
;
i
++
)
{
result
=
rt_sem_init
(
&
static_semaphore
,
"static_sem"
,
i
,
RT_IPC_FLAG_PRIO
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
break
;
}
rt_sem_detach
(
&
static_semaphore
);
result
=
rt_sem_init
(
&
static_semaphore
,
"static_sem"
,
i
,
RT_IPC_FLAG_FIFO
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
break
;
}
rt_sem_detach
(
&
static_semaphore
);
}
uassert_true
(
RT_TRUE
);
}
static
void
test_static_semaphore_detach
(
void
)
{
rt_err_t
result
;
int
rand_num
=
rand
()
%
0x10000
;
for
(
int
i
=
0
;
i
<
rand_num
;
i
++
)
{
result
=
rt_sem_init
(
&
static_semaphore
,
"static_sem"
,
i
,
RT_IPC_FLAG_PRIO
);
if
(
RT_EOK
!=
result
)
{
break
;
}
result
=
rt_sem_detach
(
&
static_semaphore
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
break
;
}
result
=
rt_sem_init
(
&
static_semaphore
,
"static_sem"
,
i
,
RT_IPC_FLAG_FIFO
);
if
(
RT_EOK
!=
result
)
{
break
;
}
result
=
rt_sem_detach
(
&
static_semaphore
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
break
;
}
}
uassert_true
(
RT_TRUE
);
}
static
void
test_static_semaphore_take
(
void
)
{
rt_err_t
result
;
result
=
rt_sem_init
(
&
static_semaphore
,
"static_sem"
,
1
,
RT_IPC_FLAG_PRIO
);
if
(
RT_EOK
==
result
)
{
/* first take */
result
=
rt_sem_take
(
&
static_semaphore
,
RT_WAITING_FOREVER
);
if
(
RT_EOK
!=
result
)
uassert_true
(
RT_FALSE
);
/* second take */
result
=
rt_sem_take
(
&
static_semaphore
,
100
);
if
(
-
RT_ETIMEOUT
!=
result
)
uassert_true
(
RT_FALSE
);
}
else
{
return
;
}
rt_sem_detach
(
&
static_semaphore
);
uassert_true
(
RT_TRUE
);
return
;
}
static
void
test_static_semaphore_trytake
(
void
)
{
rt_err_t
result
;
result
=
rt_sem_init
(
&
static_semaphore
,
"static_sem"
,
1
,
RT_IPC_FLAG_PRIO
);
if
(
RT_EOK
==
result
)
{
/* first take */
result
=
rt_sem_trytake
(
&
static_semaphore
);
if
(
RT_EOK
!=
result
)
uassert_true
(
RT_FALSE
);
/* second take */
result
=
rt_sem_trytake
(
&
static_semaphore
);
if
(
-
RT_ETIMEOUT
!=
result
)
uassert_true
(
RT_FALSE
);
}
else
{
return
;
}
rt_sem_detach
(
&
static_semaphore
);
uassert_true
(
RT_TRUE
);
return
;
}
static
void
test_static_semaphore_release
(
void
)
{
rt_err_t
result
;
result
=
rt_sem_init
(
&
static_semaphore
,
"static_sem"
,
0
,
RT_IPC_FLAG_PRIO
);
if
(
RT_EOK
==
result
)
{
/* first take */
result
=
rt_sem_take
(
&
static_semaphore
,
100
);
if
(
-
RT_ETIMEOUT
!=
result
)
uassert_true
(
RT_FALSE
);
/* release */
result
=
rt_sem_release
(
&
static_semaphore
);
if
(
RT_EOK
!=
result
)
uassert_true
(
RT_FALSE
);
/* second take */
result
=
rt_sem_take
(
&
static_semaphore
,
RT_WAITING_FOREVER
);
if
(
RT_EOK
!=
result
)
uassert_true
(
RT_FALSE
);
}
else
{
return
;
}
rt_sem_detach
(
&
static_semaphore
);
uassert_true
(
RT_TRUE
);
return
;
}
static
void
test_static_semaphore_control
(
void
)
{
rt_err_t
result
;
int
value
=
0
;
value
=
rand
()
%
100
;
result
=
rt_sem_init
(
&
static_semaphore
,
"static_sem"
,
1
,
RT_IPC_FLAG_PRIO
);
if
(
RT_EOK
==
result
)
{
result
=
rt_sem_control
(
&
static_semaphore
,
RT_IPC_CMD_RESET
,
&
value
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
}
}
else
{
return
;
}
for
(
int
i
=
0
;
i
<
value
;
i
++
)
{
result
=
rt_sem_take
(
&
static_semaphore
,
10
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
}
}
rt_sem_detach
(
&
static_semaphore
);
uassert_true
(
RT_TRUE
);
}
static
void
static_release_isr_hardware_callback
(
void
*
param
)
{
rt_err_t
result
;
result
=
rt_sem_release
(
&
static_semaphore
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
}
}
static
void
static_release_isr_software_callback
(
void
*
param
)
{
rt_err_t
result
;
result
=
rt_sem_release
(
&
static_semaphore
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
}
}
static
void
test_static_semaphore_release_isr
(
void
)
{
rt_err_t
result
;
rt_timer_t
hardware_timer
;
rt_timer_t
software_timer
;
/* create timer */
hardware_timer
=
rt_timer_create
(
"release_isr"
,
static_release_isr_hardware_callback
,
RT_NULL
,
100
,
RT_TIMER_FLAG_HARD_TIMER
|
RT_TIMER_FLAG_ONE_SHOT
);
software_timer
=
rt_timer_create
(
"release_isr"
,
static_release_isr_software_callback
,
RT_NULL
,
100
,
RT_TIMER_FLAG_SOFT_TIMER
|
RT_TIMER_FLAG_ONE_SHOT
);
/* start tiemr */
if
(
hardware_timer
)
rt_timer_start
(
hardware_timer
);
if
(
software_timer
)
rt_timer_start
(
software_timer
);
result
=
rt_sem_init
(
&
static_semaphore
,
"static_sem"
,
0
,
RT_IPC_FLAG_PRIO
);
if
(
RT_EOK
==
result
)
{
for
(
int
i
=
0
;
i
<
2
;
i
++
)
{
result
=
rt_sem_take
(
&
static_semaphore
,
1000
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
}
}
}
else
{
return
;
}
rt_sem_detach
(
&
static_semaphore
);
rt_timer_delete
(
hardware_timer
);
rt_timer_delete
(
software_timer
);
uassert_true
(
RT_TRUE
);
}
#ifdef RT_USING_HEAP
static
void
test_dynamic_semaphore_create
(
void
)
{
rt_err_t
result
;
int
rand_num
=
rand
()
%
0x10000
;
for
(
int
i
=
0
;
i
<
rand_num
;
i
++
)
{
dynamic_semaphore
=
rt_sem_create
(
"static_sem"
,
i
,
RT_IPC_FLAG_PRIO
);
if
(
RT_NULL
==
dynamic_semaphore
)
{
uassert_true
(
RT_FALSE
);
break
;
}
rt_sem_delete
(
dynamic_semaphore
);
dynamic_semaphore
=
rt_sem_create
(
"static_sem"
,
i
,
RT_IPC_FLAG_FIFO
);
if
(
RT_NULL
==
dynamic_semaphore
)
{
uassert_true
(
RT_FALSE
);
break
;
}
rt_sem_delete
(
dynamic_semaphore
);
}
uassert_true
(
RT_TRUE
);
}
static
void
test_dynamic_semaphore_delete
(
void
)
{
rt_err_t
result
;
int
rand_num
=
rand
()
%
0x10000
;
for
(
int
i
=
0
;
i
<
rand_num
;
i
++
)
{
dynamic_semaphore
=
rt_sem_create
(
"static_sem"
,
i
,
RT_IPC_FLAG_PRIO
);
if
(
RT_NULL
==
dynamic_semaphore
)
{
break
;
}
result
=
rt_sem_delete
(
dynamic_semaphore
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
break
;
}
dynamic_semaphore
=
rt_sem_create
(
"static_sem"
,
i
,
RT_IPC_FLAG_FIFO
);
if
(
RT_NULL
==
dynamic_semaphore
)
{
break
;
}
result
=
rt_sem_delete
(
dynamic_semaphore
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
break
;
}
}
uassert_true
(
RT_TRUE
);
}
static
void
test_dynamic_semaphore_take
(
void
)
{
rt_err_t
result
;
dynamic_semaphore
=
rt_sem_create
(
"static_sem"
,
1
,
RT_IPC_FLAG_PRIO
);
if
(
RT_NULL
!=
dynamic_semaphore
)
{
/* first take */
result
=
rt_sem_take
(
dynamic_semaphore
,
RT_WAITING_FOREVER
);
if
(
RT_EOK
!=
result
)
uassert_true
(
RT_FALSE
);
/* second take */
result
=
rt_sem_take
(
dynamic_semaphore
,
100
);
if
(
-
RT_ETIMEOUT
!=
result
)
uassert_true
(
RT_FALSE
);
}
else
{
return
;
}
rt_sem_delete
(
dynamic_semaphore
);
uassert_true
(
RT_TRUE
);
return
;
}
static
void
test_dynamic_semaphore_trytake
(
void
)
{
rt_err_t
result
;
dynamic_semaphore
=
rt_sem_create
(
"static_sem"
,
1
,
RT_IPC_FLAG_PRIO
);
if
(
RT_NULL
!=
dynamic_semaphore
)
{
/* first take */
result
=
rt_sem_trytake
(
dynamic_semaphore
);
if
(
RT_EOK
!=
result
)
uassert_true
(
RT_FALSE
);
/* second take */
result
=
rt_sem_trytake
(
dynamic_semaphore
);
if
(
-
RT_ETIMEOUT
!=
result
)
uassert_true
(
RT_FALSE
);
}
else
{
return
;
}
rt_sem_delete
(
dynamic_semaphore
);
uassert_true
(
RT_TRUE
);
return
;
}
static
void
test_dynamic_semaphore_release
(
void
)
{
rt_err_t
result
;
dynamic_semaphore
=
rt_sem_create
(
"static_sem"
,
0
,
RT_IPC_FLAG_PRIO
);
if
(
RT_NULL
!=
dynamic_semaphore
)
{
/* first take */
result
=
rt_sem_take
(
dynamic_semaphore
,
100
);
if
(
-
RT_ETIMEOUT
!=
result
)
uassert_true
(
RT_FALSE
);
/* release */
result
=
rt_sem_release
(
dynamic_semaphore
);
if
(
RT_EOK
!=
result
)
uassert_true
(
RT_FALSE
);
/* second take */
result
=
rt_sem_take
(
dynamic_semaphore
,
RT_WAITING_FOREVER
);
if
(
RT_EOK
!=
result
)
uassert_true
(
RT_FALSE
);
}
else
{
return
;
}
rt_sem_delete
(
dynamic_semaphore
);
uassert_true
(
RT_TRUE
);
return
;
}
static
void
test_dynamic_semaphore_control
(
void
)
{
rt_err_t
result
;
int
value
=
0
;
value
=
rand
()
%
100
;
dynamic_semaphore
=
rt_sem_create
(
"static_sem"
,
1
,
RT_IPC_FLAG_PRIO
);
if
(
RT_NULL
!=
dynamic_semaphore
)
{
result
=
rt_sem_control
(
dynamic_semaphore
,
RT_IPC_CMD_RESET
,
&
value
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
}
}
else
{
return
;
}
for
(
int
i
=
0
;
i
<
value
;
i
++
)
{
result
=
rt_sem_take
(
dynamic_semaphore
,
10
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
}
}
rt_sem_delete
(
dynamic_semaphore
);
uassert_true
(
RT_TRUE
);
}
static
void
dynamic_release_isr_hardware_callback
(
void
*
param
)
{
rt_err_t
result
;
result
=
rt_sem_release
(
dynamic_semaphore
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
}
}
static
void
dynamic_release_isr_software_callback
(
void
*
param
)
{
rt_err_t
result
;
result
=
rt_sem_release
(
dynamic_semaphore
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
}
}
static
void
test_dynamic_semaphore_release_isr
(
void
)
{
rt_err_t
result
;
rt_timer_t
hardware_timer
;
rt_timer_t
software_timer
;
/* create timer */
hardware_timer
=
rt_timer_create
(
"release_isr"
,
dynamic_release_isr_hardware_callback
,
RT_NULL
,
100
,
RT_TIMER_FLAG_HARD_TIMER
|
RT_TIMER_FLAG_ONE_SHOT
);
software_timer
=
rt_timer_create
(
"release_isr"
,
dynamic_release_isr_software_callback
,
RT_NULL
,
100
,
RT_TIMER_FLAG_SOFT_TIMER
|
RT_TIMER_FLAG_ONE_SHOT
);
/* start tiemr */
if
(
hardware_timer
)
rt_timer_start
(
hardware_timer
);
if
(
software_timer
)
rt_timer_start
(
software_timer
);
dynamic_semaphore
=
rt_sem_create
(
"static_sem"
,
0
,
RT_IPC_FLAG_PRIO
);
if
(
RT_NULL
!=
dynamic_semaphore
)
{
for
(
int
i
=
0
;
i
<
2
;
i
++
)
{
result
=
rt_sem_take
(
dynamic_semaphore
,
1000
);
if
(
RT_EOK
!=
result
)
{
uassert_true
(
RT_FALSE
);
}
}
}
else
{
return
;
}
rt_sem_delete
(
dynamic_semaphore
);
rt_timer_delete
(
hardware_timer
);
rt_timer_delete
(
software_timer
);
uassert_true
(
RT_TRUE
);
}
#endif
/* RT_USING_HEAP */
static
rt_err_t
utest_tc_init
(
void
)
{
#ifdef RT_USING_HEAP
dynamic_semaphore
=
RT_NULL
;
#endif
/* RT_USING_HEAP */
return
RT_EOK
;
}
static
rt_err_t
utest_tc_cleanup
(
void
)
{
#ifdef RT_USING_HEAP
dynamic_semaphore
=
RT_NULL
;
#endif
/* RT_USING_HEAP */
return
RT_EOK
;
}
static
void
testcase
(
void
)
{
UTEST_UNIT_RUN
(
test_static_semaphore_init
);
UTEST_UNIT_RUN
(
test_static_semaphore_take
);
UTEST_UNIT_RUN
(
test_static_semaphore_release
);
UTEST_UNIT_RUN
(
test_static_semaphore_detach
);
UTEST_UNIT_RUN
(
test_static_semaphore_trytake
);
UTEST_UNIT_RUN
(
test_static_semaphore_control
);
UTEST_UNIT_RUN
(
test_static_semaphore_release_isr
);
#ifdef RT_USING_HEAP
UTEST_UNIT_RUN
(
test_dynamic_semaphore_create
);
UTEST_UNIT_RUN
(
test_dynamic_semaphore_take
);
UTEST_UNIT_RUN
(
test_dynamic_semaphore_release
);
UTEST_UNIT_RUN
(
test_dynamic_semaphore_delete
);
UTEST_UNIT_RUN
(
test_dynamic_semaphore_trytake
);
UTEST_UNIT_RUN
(
test_dynamic_semaphore_control
);
UTEST_UNIT_RUN
(
test_dynamic_semaphore_release_isr
);
#endif
/* RT_USING_HEAP */
}
UTEST_TC_EXPORT
(
testcase
,
"testcases.kernel.semaphore_tc"
,
utest_tc_init
,
utest_tc_cleanup
,
1000
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录