diff options
| author | Lim-LinYuanbo <[email protected]> | 2021-09-08 22:45:31 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-09-08 22:45:31 +0800 |
| commit | b404f69f851817c62697074ffb363294b91585c6 (patch) | |
| tree | f5f31152993843a5e07559a6b7b0caddcc9ebd0a /examples | |
| parent | 49fcf9f4cd436d30d2df8c4e24b751194c32f43b (diff) | |
| parent | 7fa1c819ee852937705d948a58f0218c422ea6b1 (diff) | |
Merge branch 'master' into master
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/file/listdir.c | 2 | ||||
| -rw-r--r-- | examples/file/readspeed.c | 4 | ||||
| -rw-r--r-- | examples/file/readwrite.c | 4 | ||||
| -rw-r--r-- | examples/file/writespeed.c | 4 | ||||
| -rw-r--r-- | examples/kernel/tc_comm.c | 1 | ||||
| -rw-r--r-- | examples/libc/termios_test.c | 4 | ||||
| -rw-r--r-- | examples/test/hwtimer_test.c | 2 | ||||
| -rw-r--r-- | examples/utest/configs/kernel/ipc.conf | 9 | ||||
| -rw-r--r-- | examples/utest/configs/kernel/irq.conf | 2 | ||||
| -rw-r--r-- | examples/utest/configs/kernel/timer.conf | 4 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/Kconfig | 26 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/SConscript | 18 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/event_tc.c | 344 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/irq_tc.c | 78 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/messagequeue_tc.c | 192 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/semaphore_tc.c | 559 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/signal_tc.c | 199 | ||||
| -rw-r--r-- | examples/utest/testcases/kernel/timer_tc.c | 520 |
18 files changed, 1957 insertions, 15 deletions
diff --git a/examples/file/listdir.c b/examples/file/listdir.c index 06c746a642..9649347734 100644 --- a/examples/file/listdir.c +++ b/examples/file/listdir.c @@ -67,7 +67,6 @@ void list_dir(const char* path) #include <finsh.h> FINSH_FUNCTION_EXPORT(list_dir, list directory); -#ifdef FINSH_USING_MSH static void cmd_list_dir(int argc, char *argv[]) { char* filename; @@ -84,5 +83,4 @@ static void cmd_list_dir(int argc, char *argv[]) list_dir(filename); } MSH_CMD_EXPORT_ALIAS(cmd_list_dir, list_dir, list directory); -#endif /* FINSH_USING_MSH */ #endif /* RT_USING_FINSH */ diff --git a/examples/file/readspeed.c b/examples/file/readspeed.c index ffc19635cf..5f4cad3190 100644 --- a/examples/file/readspeed.c +++ b/examples/file/readspeed.c @@ -58,7 +58,6 @@ void readspeed(const char* filename, int block_size) #include <finsh.h> FINSH_FUNCTION_EXPORT(readspeed, perform file read test); -#ifdef FINSH_USING_MSH static void cmd_readspeed(int argc, char *argv[]) { char* filename; @@ -82,6 +81,5 @@ static void cmd_readspeed(int argc, char *argv[]) } readspeed(filename, block_size); } -FINSH_FUNCTION_EXPORT_ALIAS(cmd_readspeed, __cmd_readspeed, test file system read speed); -#endif /* FINSH_USING_MSH */ +MSH_CMD_EXPORT_ALIAS(cmd_readspeed, readspeed, test file system read speed); #endif /* RT_USING_FINSH */ diff --git a/examples/file/readwrite.c b/examples/file/readwrite.c index 56f3b7e16c..d1dd5bc038 100644 --- a/examples/file/readwrite.c +++ b/examples/file/readwrite.c @@ -147,7 +147,6 @@ __exit: /* export to finsh */ FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test); -#ifdef FINSH_USING_MSH static void cmd_readwrite(int argc, char *argv[]) { char* filename; @@ -163,6 +162,5 @@ static void cmd_readwrite(int argc, char *argv[]) } readwrite(filename); } -FINSH_FUNCTION_EXPORT_ALIAS(cmd_readwrite, __cmd_readwrite, perform file read and write test); -#endif /* FINSH_USING_MSH */ +MSH_CMD_EXPORT_ALIAS(cmd_readwrite, readwrite, perform file read and write test); #endif /* RT_USING_FINSH */ diff --git a/examples/file/writespeed.c b/examples/file/writespeed.c index 680761583c..f76027d28b 100644 --- a/examples/file/writespeed.c +++ b/examples/file/writespeed.c @@ -66,7 +66,6 @@ void writespeed(const char* filename, int total_length, int block_size) #include <finsh.h> FINSH_FUNCTION_EXPORT(writespeed, perform file write test); -#ifdef FINSH_USING_MSH static void cmd_writespeed(int argc, char *argv[]) { char* filename; @@ -93,6 +92,5 @@ static void cmd_writespeed(int argc, char *argv[]) } writespeed(filename, length, block_size); } -FINSH_FUNCTION_EXPORT_ALIAS(cmd_writespeed, __cmd_writespeed, test file system write speed); -#endif /* FINSH_USING_MSH */ +MSH_CMD_EXPORT_ALIAS(cmd_writespeed, writespeed, test file system write speed); #endif /* RT_USING_FINSH */ diff --git a/examples/kernel/tc_comm.c b/examples/kernel/tc_comm.c index b98c97d9b3..0a43ca3855 100644 --- a/examples/kernel/tc_comm.c +++ b/examples/kernel/tc_comm.c @@ -25,7 +25,6 @@ static const char* _tc_current; static void (*_tc_cleanup)(void) = RT_NULL; static rt_uint32_t _tc_scale = 1; -FINSH_VAR_EXPORT(_tc_scale, finsh_type_int, the testcase timer timeout scale) static rt_uint32_t _tc_loop; diff --git a/examples/libc/termios_test.c b/examples/libc/termios_test.c index f65bff1a9f..f3a6ce3a1c 100644 --- a/examples/libc/termios_test.c +++ b/examples/libc/termios_test.c @@ -356,8 +356,8 @@ int termios_test(int argc, char **argv) #ifdef RT_USING_FINSH #include <finsh.h> - #ifdef FINSH_USING_MSH + #ifdef RT_USING_FINSH MSH_CMD_EXPORT_ALIAS(termios_test, termtest, e.g: termtest /dev/uart4 115200); - #endif /* FINSH_USING_MSH */ + #endif /* RT_USING_FINSH */ #endif /* RT_USING_FINSH */ diff --git a/examples/test/hwtimer_test.c b/examples/test/hwtimer_test.c index 15d7aa830f..08b0c5acaf 100644 --- a/examples/test/hwtimer_test.c +++ b/examples/test/hwtimer_test.c @@ -105,7 +105,7 @@ EXIT: return err; } -#ifdef FINSH_USING_MSH +#ifdef RT_USING_FINSH MSH_CMD_EXPORT(hwtimer, "Test hardware timer"); #endif #endif /* RT_USING_HWTIMER */ diff --git a/examples/utest/configs/kernel/ipc.conf b/examples/utest/configs/kernel/ipc.conf new file mode 100644 index 0000000000..f8e687e633 --- /dev/null +++ b/examples/utest/configs/kernel/ipc.conf @@ -0,0 +1,9 @@ +CONFIG_UTEST_SEMAPHORE_TC=y +CONFIG_UTEST_EVENT_TC=y +CONFIG_UTEST_MESSAGEQUEUE_TC=y +CONFIG_UTEST_SIGNAL_TC=y +# dependencies +CONFIG_RT_USING_SEMAPHORE=y +CONFIG_RT_USING_EVENT=y +CONFIG_RT_USING_MESSAGEQUEUE=y +CONFIG_RT_USING_SIGNALS=y diff --git a/examples/utest/configs/kernel/irq.conf b/examples/utest/configs/kernel/irq.conf new file mode 100644 index 0000000000..83fe485638 --- /dev/null +++ b/examples/utest/configs/kernel/irq.conf @@ -0,0 +1,2 @@ +CONFIG_UTEST_IRQ_TC=y +# dependencies diff --git a/examples/utest/configs/kernel/timer.conf b/examples/utest/configs/kernel/timer.conf new file mode 100644 index 0000000000..20696f065f --- /dev/null +++ b/examples/utest/configs/kernel/timer.conf @@ -0,0 +1,4 @@ +CONFIG_UTEST_TIMER_TC=y + +# dependencies +CONFIG_RT_USING_TIMER_SOFT=y diff --git a/examples/utest/testcases/kernel/Kconfig b/examples/utest/testcases/kernel/Kconfig index 14334af16f..f4673aebb9 100644 --- a/examples/utest/testcases/kernel/Kconfig +++ b/examples/utest/testcases/kernel/Kconfig @@ -5,4 +5,30 @@ config UTEST_MEMHEAP_TC default y depends on RT_USING_MEMHEAP +config UTEST_IRQ_TC + bool "IRQ test" + default n + +config UTEST_SEMAPHORE_TC + bool "semaphore test" + default n + depends on RT_USING_SEMAPHORE + +config UTEST_EVENT_TC + bool "event test" + default n + depends on RT_USING_EVENT + +config UTEST_TIMER_TC + bool "timer test" + default n + +config UTEST_MESSAGEQUEUE_TC + bool "message queue test" + default n + +config UTEST_SIGNAL_TC + bool "signal test" + default n + endmenu diff --git a/examples/utest/testcases/kernel/SConscript b/examples/utest/testcases/kernel/SConscript index 9411cdd9a0..ca34db6fed 100644 --- a/examples/utest/testcases/kernel/SConscript +++ b/examples/utest/testcases/kernel/SConscript @@ -8,6 +8,24 @@ src = Split(''' if GetDepend(['UTEST_MEMHEAP_TC']): src += ['memheap_tc.c'] +if GetDepend(['UTEST_IRQ_TC']): + src += ['irq_tc.c'] + +if GetDepend(['UTEST_SEMAPHORE_TC']): + src += ['semaphore_tc.c'] + +if GetDepend(['UTEST_EVENT_TC']): + src += ['event_tc.c'] + +if GetDepend(['UTEST_TIMER_TC']): + src += ['timer_tc.c'] + +if GetDepend(['UTEST_MESSAGEQUEUE_TC']): + src += ['messagequeue_tc.c'] + +if GetDepend(['UTEST_SIGNAL_TC']): + src += ['signal_tc.c'] + CPPPATH = [cwd] group = DefineGroup('utestcases', src, depend = [], CPPPATH = CPPPATH) diff --git a/examples/utest/testcases/kernel/event_tc.c b/examples/utest/testcases/kernel/event_tc.c new file mode 100644 index 0000000000..9847529214 --- /dev/null +++ b/examples/utest/testcases/kernel/event_tc.c @@ -0,0 +1,344 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-15 liukang the first version + */ + +#include <rtthread.h> +#include "utest.h" +#include <stdlib.h> + +#define EVENT_FLAG3 (1 << 3) +#define EVENT_FLAG5 (1 << 5) + +static struct rt_event static_event = {0}; +#ifdef RT_USING_HEAP +static rt_event_t dynamic_event = RT_NULL; +static rt_uint32_t dynamic_event_recv_thread_finish = 0, dynamic_event_send_thread_finish = 0; + +ALIGN(RT_ALIGN_SIZE) +static char thread3_stack[1024]; +static struct rt_thread thread3; + +ALIGN(RT_ALIGN_SIZE) +static char thread4_stack[1024]; +static struct rt_thread thread4; +#endif /* RT_USING_HEAP */ + +static rt_uint32_t recv_event_times1 = 0, recv_event_times2 = 0; +static rt_uint32_t static_event_recv_thread_finish = 0, static_event_send_thread_finish = 0; + +ALIGN(RT_ALIGN_SIZE) +static char thread1_stack[1024]; +static struct rt_thread thread1; + +ALIGN(RT_ALIGN_SIZE) +static char thread2_stack[1024]; +static struct rt_thread thread2; + +#define THREAD_PRIORITY 9 +#define THREAD_TIMESLICE 5 + +static void test_event_init(void) +{ + rt_err_t result; + + result = rt_event_init(&static_event, "event", RT_IPC_FLAG_PRIO); + if (result != RT_EOK) + { + uassert_false(1); + } + result = rt_event_detach(&static_event); + if (result != RT_EOK) + { + uassert_false(1); + } + + result = rt_event_init(&static_event, "event", RT_IPC_FLAG_FIFO); + if (result != RT_EOK) + { + uassert_false(1); + } + result = rt_event_detach(&static_event); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void test_event_detach(void) +{ + rt_err_t result = RT_EOK; + + result = rt_event_init(&static_event, "event", RT_IPC_FLAG_PRIO); + if (result != RT_EOK) + { + uassert_false(1); + } + + result = rt_event_detach(&static_event); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void thread1_recv_static_event(void *param) +{ + rt_uint32_t e; + + if (rt_event_recv(&static_event, (EVENT_FLAG3 | EVENT_FLAG5), + RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &e) != RT_EOK) + { + return; + } + + recv_event_times1 = e; + + rt_thread_mdelay(50); + + if (rt_event_recv(&static_event, (EVENT_FLAG3 | EVENT_FLAG5), + RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &e) != RT_EOK) + { + return; + } + recv_event_times2 = e; + + static_event_recv_thread_finish = 1; +} + +static void thread2_send_static_event(void *param) +{ + rt_event_send(&static_event, EVENT_FLAG3); + rt_thread_mdelay(10); + + rt_event_send(&static_event, EVENT_FLAG5); + rt_thread_mdelay(10); + + rt_event_send(&static_event, EVENT_FLAG3); + + static_event_send_thread_finish = 1; +} + + +static void test_static_event_send_recv(void) +{ + rt_err_t result = RT_EOK; + + result = rt_event_init(&static_event, "event", RT_IPC_FLAG_PRIO); + if (result != RT_EOK) + { + uassert_false(1); + } + + rt_thread_init(&thread1, + "thread1", + thread1_recv_static_event, + RT_NULL, + &thread1_stack[0], + sizeof(thread1_stack), + THREAD_PRIORITY - 1, THREAD_TIMESLICE); + rt_thread_startup(&thread1); + + rt_thread_init(&thread2, + "thread2", + thread2_send_static_event, + RT_NULL, + &thread2_stack[0], + sizeof(thread2_stack), + THREAD_PRIORITY, THREAD_TIMESLICE); + rt_thread_startup(&thread2); + + while (static_event_recv_thread_finish != 1 || static_event_send_thread_finish != 1) + { + rt_thread_delay(1); + } + + if (recv_event_times1 == EVENT_FLAG3 && recv_event_times2 == (EVENT_FLAG3 | EVENT_FLAG5)) + { + if (rt_event_detach(&static_event) != RT_EOK) + { + uassert_false(1); + } + uassert_true(1); + } + else + { + if (rt_event_detach(&static_event) != RT_EOK) + { + uassert_false(1); + } + uassert_false(1); + } + + return; +} + +#ifdef RT_USING_HEAP +static void test_event_create(void) +{ + rt_err_t result = RT_EOK; + + dynamic_event = rt_event_create("dynamic_event", RT_IPC_FLAG_FIFO); + if (dynamic_event == RT_NULL) + { + uassert_false(1); + } + + result = rt_event_delete(dynamic_event); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void test_event_delete(void) +{ + rt_err_t result; + + dynamic_event = rt_event_create("dynamic_event", RT_IPC_FLAG_FIFO); + if (dynamic_event == RT_NULL) + { + uassert_false(1); + } + + result = rt_event_delete(dynamic_event); + if (result != RT_EOK) + { + uassert_false(1); + } + + uassert_true(1); +} + +static void thread3_recv_dynamic_event(void *param) +{ + rt_uint32_t e; + + if (rt_event_recv(dynamic_event, (EVENT_FLAG3 | EVENT_FLAG5), + RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &e) != RT_EOK) + { + return; + } + + recv_event_times1 = e; + + rt_thread_mdelay(50); + + if (rt_event_recv(dynamic_event, (EVENT_FLAG3 | EVENT_FLAG5), + RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &e) != RT_EOK) + { + return; + } + recv_event_times2 = e; + + dynamic_event_recv_thread_finish = 1; +} + +static void thread4_send_dynamic_event(void *param) +{ + rt_event_send(dynamic_event, EVENT_FLAG3); + rt_thread_mdelay(10); + + rt_event_send(dynamic_event, EVENT_FLAG5); + rt_thread_mdelay(10); + + rt_event_send(dynamic_event, EVENT_FLAG3); + + dynamic_event_send_thread_finish = 1; +} + +static void test_dynamic_event_send_recv(void) +{ + dynamic_event = rt_event_create("dynamic_event", RT_IPC_FLAG_PRIO); + if (dynamic_event == RT_NULL) + { + uassert_false(1); + } + + rt_thread_init(&thread3, + "thread3", + thread3_recv_dynamic_event, + RT_NULL, + &thread3_stack[0], + sizeof(thread3_stack), + THREAD_PRIORITY - 1, THREAD_TIMESLICE); + rt_thread_startup(&thread3); + + rt_thread_init(&thread4, + "thread4", + thread4_send_dynamic_event, + RT_NULL, + &thread4_stack[0], + sizeof(thread4_stack), + THREAD_PRIORITY, THREAD_TIMESLICE); + rt_thread_startup(&thread4); + + while (dynamic_event_recv_thread_finish != 1 || dynamic_event_send_thread_finish != 1) + { + rt_thread_delay(1); + } + + if (recv_event_times1 == EVENT_FLAG3 && recv_event_times2 == (EVENT_FLAG3 | EVENT_FLAG5)) + { + if (rt_event_delete(dynamic_event) != RT_EOK) + { + uassert_false(1); + } + uassert_true(1); + } + else + { + if (rt_event_delete(dynamic_event) != RT_EOK) + { + uassert_false(1); + } + uassert_false(1); + } + + return; +} +#endif + +static rt_err_t utest_tc_init(void) +{ + static_event_recv_thread_finish = 0; + static_event_send_thread_finish = 0; +#ifdef RT_USING_HEAP + dynamic_event_recv_thread_finish = 0; + dynamic_event_send_thread_finish = 0; +#endif + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ + UTEST_UNIT_RUN(test_event_init); + UTEST_UNIT_RUN(test_event_detach); + UTEST_UNIT_RUN(test_static_event_send_recv); +#ifdef RT_USING_HEAP + UTEST_UNIT_RUN(test_event_create); + UTEST_UNIT_RUN(test_event_delete); + UTEST_UNIT_RUN(test_dynamic_event_send_recv); +#endif +} +UTEST_TC_EXPORT(testcase, "src.ipc.event_tc", utest_tc_init, utest_tc_cleanup, 60); diff --git a/examples/utest/testcases/kernel/irq_tc.c b/examples/utest/testcases/kernel/irq_tc.c new file mode 100644 index 0000000000..86b6c144f1 --- /dev/null +++ b/examples/utest/testcases/kernel/irq_tc.c @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-15 supperthomas add irq_test + */ + +#include <rtthread.h> +#include "utest.h" +#include "rthw.h" + +#define UTEST_NAME "irq_tc" +static uint32_t irq_count = 0; +static uint32_t max_get_nest_count = 0; + +static void irq_callback() +{ + if(rt_interrupt_get_nest() > max_get_nest_count) + { + max_get_nest_count = rt_interrupt_get_nest(); + } + irq_count ++; +} + +static void irq_test(void) +{ + irq_count = 0; + rt_interrupt_enter_sethook(irq_callback); + rt_interrupt_leave_sethook(irq_callback); + rt_thread_mdelay(2); + LOG_D("%s test irq_test! irq_count %d max_get_nest_count %d\n", UTEST_NAME, irq_count, max_get_nest_count); + uassert_int_not_equal(0, irq_count); + uassert_int_not_equal(0, max_get_nest_count); + rt_interrupt_enter_sethook(RT_NULL); + rt_interrupt_leave_sethook(RT_NULL); + LOG_D("irq_test OK!\n"); +} + +static rt_err_t utest_tc_init(void) +{ + irq_count = 0; + max_get_nest_count = 0; + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void interrupt_test(void) +{ + rt_base_t level; + uint32_t i = 1000; + + rt_interrupt_enter_sethook(irq_callback); + rt_interrupt_leave_sethook(irq_callback); + irq_count = 0; + level = rt_hw_interrupt_disable(); + while(i) + { + i --; + } + uassert_int_equal(0, irq_count); + rt_hw_interrupt_enable(level); + rt_interrupt_enter_sethook(RT_NULL); + rt_interrupt_leave_sethook(RT_NULL); + +} +static void testcase(void) +{ + UTEST_UNIT_RUN(irq_test); + UTEST_UNIT_RUN(interrupt_test); +} +UTEST_TC_EXPORT(testcase, "testcases.kernel.irq_tc", utest_tc_init, utest_tc_cleanup, 10); diff --git a/examples/utest/testcases/kernel/messagequeue_tc.c b/examples/utest/testcases/kernel/messagequeue_tc.c new file mode 100644 index 0000000000..335c591ab6 --- /dev/null +++ b/examples/utest/testcases/kernel/messagequeue_tc.c @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-28 Sherman the first version + */ + +#include <rtthread.h> +#include "utest.h" + +#define MSG_SIZE 4 +#define MAX_MSGS 5 + +static struct rt_messagequeue static_mq; +static rt_uint8_t mq_buf[(MSG_SIZE + 4) * MAX_MSGS]; + +static struct rt_thread mq_send_thread; +static struct rt_thread mq_recv_thread; +static rt_uint8_t mq_send_stack[512]; +static rt_uint8_t mq_recv_stack[512]; + +static struct rt_event finish_e; +#define MQSEND_FINISH 0x01 +#define MQRECV_FINIHS 0x02 + +#ifdef RT_USING_HEAP +static rt_mq_t dynamic_mq; +#endif /* RT_USING_HEAP */ + +static void test_mq_init(void) +{ + rt_err_t ret; + ret = rt_mq_init(&static_mq,"testmq1", mq_buf, MSG_SIZE, sizeof(mq_buf), RT_IPC_FLAG_FIFO); + uassert_true(ret == RT_EOK); +} + +static void test_mq_create(void) +{ +#ifdef RT_USING_HEAP + dynamic_mq = rt_mq_create("testmq2", MSG_SIZE, MAX_MSGS, RT_IPC_FLAG_FIFO); + uassert_true(dynamic_mq != RT_NULL); +#endif /* RT_USING_HEAP */ +} + +static void mq_send_case(rt_mq_t testmq) +{ + rt_uint32_t send_buf[MAX_MSGS+1] = {0}; + rt_err_t ret = RT_EOK; + + for (int var = 0; var < MAX_MSGS; ++var) + { + send_buf[var] = var + 1; + ret = rt_mq_send_wait(testmq, &send_buf[var], sizeof(send_buf[0]), RT_WAITING_FOREVER); + uassert_true(ret == RT_EOK); + } + send_buf[MAX_MSGS] = MAX_MSGS + 1; + ret = rt_mq_send(testmq, &send_buf[MAX_MSGS], sizeof(send_buf[0])); + uassert_true(ret == -RT_EFULL); + + ret = rt_mq_send_wait(testmq, &send_buf[MAX_MSGS], sizeof(send_buf[0]), RT_WAITING_FOREVER); + uassert_true(ret == RT_EOK); + + while (testmq->entry != 0) + { + rt_thread_delay(100); + } + + ret = rt_mq_send(testmq, &send_buf[1], sizeof(send_buf[0])); + uassert_true(ret == RT_EOK); + + ret = rt_mq_send(testmq, &send_buf[2], sizeof(send_buf[0])); + uassert_true(ret == RT_EOK); + + ret = rt_mq_urgent(testmq, &send_buf[0], sizeof(send_buf[0])); + uassert_true(ret == RT_EOK); + + while (testmq->entry != 0) + { + rt_thread_delay(100); + } + + ret = rt_mq_send(testmq, &send_buf[1], sizeof(send_buf[0])); + uassert_true(ret == RT_EOK); + ret = rt_mq_control(testmq, RT_IPC_CMD_RESET, RT_NULL); + uassert_true(ret == RT_EOK); + uassert_true(testmq->entry == 0); +} + +static void mq_send_entry(void *param) +{ + mq_send_case(&static_mq); + +#ifdef RT_USING_HEAP + if(dynamic_mq != RT_NULL) + { + mq_send_case(dynamic_mq); + } +#endif /* RT_USING_HEAP */ + + rt_event_send(&finish_e, MQSEND_FINISH); +} + +static void mq_recv_case(rt_mq_t testmq) +{ + rt_uint32_t recv_buf[MAX_MSGS+1] = {0}; + rt_err_t ret = RT_EOK; + + for (int var = 0; var < MAX_MSGS + 1; ++var) + { + ret = rt_mq_recv(testmq, &recv_buf[var], sizeof(recv_buf[0]), RT_WAITING_FOREVER); + uassert_true(ret == RT_EOK); + uassert_true(recv_buf[var] == (var + 1)); + } + + for (int var = 0; var < 3; ++var) + { + ret = rt_mq_recv(testmq, &recv_buf[var], sizeof(recv_buf[0]), RT_WAITING_FOREVER); + uassert_true(ret == RT_EOK); + uassert_true(recv_buf[var] == (var + 1)); + } +} + +static void mq_recv_entry(void *param) +{ + mq_recv_case(&static_mq); + +#ifdef RT_USING_HEAP + if(dynamic_mq != RT_NULL) + { + mq_recv_case(dynamic_mq); + } +#endif /* RT_USING_HEAP */ + rt_event_send(&finish_e, MQRECV_FINIHS); +} + +static void test_mq_testcase(void) +{ + rt_thread_startup(&mq_send_thread); + rt_thread_startup(&mq_recv_thread); + + rt_event_recv(&finish_e, MQSEND_FINISH | MQRECV_FINIHS, RT_EVENT_FLAG_AND, RT_WAITING_FOREVER, RT_NULL); +} + +static void test_mq_detach(void) +{ + rt_err_t ret = rt_mq_detach(&static_mq); + uassert_true(ret == RT_EOK); +} + +static void test_mq_delete(void) +{ +#ifdef RT_USING_HEAP + rt_err_t ret = rt_mq_delete(dynamic_mq); + uassert_true(ret == RT_EOK); +#endif /* RT_USING_HEAP */ +} + +static rt_err_t utest_tc_init(void) +{ + rt_err_t ret ; + ret = rt_thread_init(&mq_send_thread, "mq_send", mq_send_entry, RT_NULL, mq_send_stack, sizeof(mq_send_stack), 22, 20); + if(ret != RT_EOK) + return -RT_ERROR; + + ret = rt_thread_init(&mq_recv_thread, "mq_recv", mq_recv_entry, RT_NULL, mq_recv_stack, sizeof(mq_recv_stack), 23, 20); + if(ret != RT_EOK) + return -RT_ERROR; + + ret = rt_event_init(&finish_e, "finish", RT_IPC_FLAG_FIFO); + if(ret != RT_EOK) + return -RT_ERROR; + + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ + UTEST_UNIT_RUN(test_mq_init); + UTEST_UNIT_RUN(test_mq_create); + UTEST_UNIT_RUN(test_mq_testcase); + UTEST_UNIT_RUN(test_mq_detach); + UTEST_UNIT_RUN(test_mq_delete); +} +UTEST_TC_EXPORT(testcase, "testcases.kernel.messagequeue_tc", utest_tc_init, utest_tc_cleanup, 1000); diff --git a/examples/utest/testcases/kernel/semaphore_tc.c b/examples/utest/testcases/kernel/semaphore_tc.c new file mode 100644 index 0000000000..2992831d87 --- /dev/null +++ b/examples/utest/testcases/kernel/semaphore_tc.c @@ -0,0 +1,559 @@ +/* + * 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); diff --git a/examples/utest/testcases/kernel/signal_tc.c b/examples/utest/testcases/kernel/signal_tc.c new file mode 100644 index 0000000000..40c08f7402 --- /dev/null +++ b/examples/utest/testcases/kernel/signal_tc.c @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-08-12 flybreak the first version + * + * case 1:rt_signal_install, install all available signal + * case 2:rt_signal_install, install illegal signal + * case 3:rt_signal_mask/unmask, one thread self, install and unmask, then kill, should received. + * case 4:rt_signal_mask/unmask, one thread self, install and unmask and mask, then kill, should can't received. + * case 5:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: kill, should received. + * case 6:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: sleep 2s then kill, should can't received. + * case 7:rt_signal_kill, kill legal thread, return 0; + * case 8:rt_signal_kill, kill illegal thread, return failed (unused); + * case 9:rt_signal_kill, kill illegal signo, return -RT_EINVAL; + * + */ + +#include <rtthread.h> +#include "utest.h" + +int recive_sig = 0; + +void sig_handle_default(int signo) +{ + recive_sig = signo; +} + +static void rt_signal_install_test(void) +{ + int signo; + rt_sighandler_t result; + + /* case 1:rt_signal_install, install all available signal. */ + for (signo = 0; signo < RT_SIG_MAX; signo++) + { + result = rt_signal_install(signo, sig_handle_default); + uassert_true(result != SIG_ERR); + } + /* case 2:rt_signal_install, install illegal signal. */ + result = rt_signal_install(signo, sig_handle_default); + uassert_true(result == SIG_ERR); + + return; +} + +static void rt_signal_mask_test(void) +{ + int signo; + rt_sighandler_t result; + + /* case 3:rt_signal_mask/unmask, one thread self, install and unmask, then kill, should received. */ + for (signo = 0; signo < RT_SIG_MAX; signo++) + { + recive_sig = -1; + result = rt_signal_install(signo, sig_handle_default); + uassert_true(result != SIG_ERR); + rt_signal_unmask(signo); + uassert_int_equal(rt_thread_kill(rt_thread_self(), signo), RT_EOK); + rt_thread_mdelay(1); + uassert_int_equal(recive_sig, signo); + } + + return; +} + +static void rt_signal_unmask_test(void) +{ + int signo; + rt_sighandler_t result; + + /* case 4:rt_signal_mask/unmask, one thread self, install and unmask and mask, then kill, should can't received. */ + for (signo = 0; signo < RT_SIG_MAX; signo++) + { + recive_sig = -1; + result = rt_signal_install(signo, sig_handle_default); + uassert_true(result != SIG_ERR); + rt_signal_unmask(signo); + rt_signal_mask(signo); + uassert_int_equal(rt_thread_kill(rt_thread_self(), signo), RT_EOK); + rt_thread_mdelay(1); + uassert_int_not_equal(recive_sig, signo); + } + + return; +} + +static void rt_signal_kill_test(void) +{ + int signo; + rt_sighandler_t result; + + /* case 7:rt_signal_kill, kill legal thread, return 0; */ + for (signo = 0; signo < RT_SIG_MAX; signo++) + { + recive_sig = -1; + result = rt_signal_install(signo, sig_handle_default); + uassert_true(result != SIG_ERR); + rt_signal_unmask(signo); + uassert_int_equal(rt_thread_kill(rt_thread_self(), signo), RT_EOK); + rt_thread_mdelay(1); + uassert_int_equal(recive_sig, signo); + } + /* case 8:rt_signal_kill, kill illegal thread, return failed; */ +// uassert_true(rt_thread_kill((rt_thread_t)-1, signo) == -RT_ERROR); + + /* case 9:rt_signal_kill, kill illegal signo, return -RT_EINVAL; */ + uassert_true(rt_thread_kill(rt_thread_self(), -1) == -RT_EINVAL); + + return; +} + +void rt_signal_wait_thread(void *parm) +{ + sigset_t selectset; + siginfo_t recive_si; + + rt_signal_install(SIGUSR1, sig_handle_default); + rt_signal_unmask(SIGUSR1); + + sigemptyset(&selectset); + sigaddset(&selectset, SIGUSR1); + + /* case 5:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: kill, should received. */ + if (rt_signal_wait(&selectset, &recive_si, RT_TICK_PER_SECOND) != RT_EOK) + { + return; + } + + recive_sig = recive_si.si_signo; +} + +static void rt_signal_wait_test(void) +{ + rt_thread_t t1; + + recive_sig = -1; + t1 = rt_thread_create("sig_t1", rt_signal_wait_thread, 0, 4096, 14, 10); + if (t1) + { + rt_thread_startup(t1); + } + + rt_thread_mdelay(1); + /* case 5:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: kill, should received. */ + uassert_int_equal(rt_thread_kill(t1, SIGUSR1), RT_EOK); + rt_thread_mdelay(1); + uassert_int_equal(recive_sig, SIGUSR1); + + return; +} + +static void rt_signal_wait_test2(void) +{ + rt_thread_t t1; + + recive_sig = -1; + t1 = rt_thread_create("sig_t1", rt_signal_wait_thread, 0, 4096, 14, 10); + if (t1) + { + rt_thread_startup(t1); + } + + /* case 6:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: sleep 2s then kill, should can't received. */ + rt_thread_mdelay(2000); + uassert_int_equal(rt_thread_kill(t1, SIGUSR1), RT_EOK); + rt_thread_mdelay(1); + uassert_int_not_equal(recive_sig, SIGUSR1); + + return; +} + +static rt_err_t utest_tc_init(void) +{ + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + return RT_EOK; +} + +static void testcase(void) +{ +#ifdef RT_USING_HEAP + UTEST_UNIT_RUN(rt_signal_install_test); + UTEST_UNIT_RUN(rt_signal_mask_test); + UTEST_UNIT_RUN(rt_signal_unmask_test); + UTEST_UNIT_RUN(rt_signal_kill_test); + UTEST_UNIT_RUN(rt_signal_wait_test); + UTEST_UNIT_RUN(rt_signal_wait_test2); +#endif /* RT_USING_HEAP */ +} +UTEST_TC_EXPORT(testcase, "testcases.kernel.signal_tc", utest_tc_init, utest_tc_cleanup, 1000); + +/*********************** end of file ****************************/ diff --git a/examples/utest/testcases/kernel/timer_tc.c b/examples/utest/testcases/kernel/timer_tc.c new file mode 100644 index 0000000000..e7fffcb617 --- /dev/null +++ b/examples/utest/testcases/kernel/timer_tc.c @@ -0,0 +1,520 @@ +/* + * 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 rt_uint8_t timer_flag_oneshot[] = { + RT_TIMER_FLAG_ONE_SHOT, + RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER, + RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER, +}; + +static rt_uint8_t timer_flag_periodic[] = { + RT_TIMER_FLAG_PERIODIC, + RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_HARD_TIMER, + RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER, +}; + +typedef struct test_timer_struct +{ + struct rt_timer static_timer; /* static timer handler */ + rt_timer_t dynamic_timer; /* dynamic timer pointer */ + rt_tick_t expect_tick; /* expect tick */ + rt_uint8_t test_flag; /* timer callback done flag */ +} timer_struct; +static timer_struct timer; + +#define test_static_timer_start test_static_timer_init +#define test_static_timer_stop test_static_timer_init +#define test_static_timer_detach test_static_timer_init + +static void static_timer_oneshot(void *param) +{ + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + return; +} +static void static_timer_periodic(void *param) +{ + rt_err_t result; + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + /* periodic timer can stop */ + result = rt_timer_stop(&timer_call->static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + return; +} + +static void test_static_timer_init(void) +{ + rt_err_t result; + int rand_num = rand() % 10; + + /* one shot timer test */ + for (int time_out = 0; time_out < rand_num; time_out++) + { + for (int i = 0; i < sizeof(timer_flag_oneshot); i++) + { + rt_timer_init(&timer.static_timer, + "static_timer", + static_timer_oneshot, + &timer, + time_out, + timer_flag_oneshot[i]); + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + time_out; + + /* start timer */ + result = rt_timer_start(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* wait for timerout */ + rt_thread_delay(time_out + 1); + + /* detach timer */ + result = rt_timer_detach(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + } + } + + /* periodic timer test */ + for (int time_out = 0; time_out < rand_num; time_out++) + { + for (int i = 0; i < sizeof(timer_flag_periodic); i++) + { + rt_timer_init(&timer.static_timer, + "static_timer", + static_timer_periodic, + &timer, + time_out, + timer_flag_periodic[i]); + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + time_out; + + /* start timer */ + result = rt_timer_start(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* wait for timerout */ + rt_thread_delay(time_out + 1); + + /* detach timer */ + result = rt_timer_detach(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + } + } + + timer.test_flag = RT_FALSE; + uassert_true(RT_TRUE); + + return; +} + +static void static_timer_control(void *param) +{ + rt_err_t result; + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + /* periodic timer can stop */ + result = rt_timer_stop(&timer_call->static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + return; +} + +static void test_static_timer_control(void) +{ + rt_err_t result; + int rand_num = rand() % 10; + int set_data; + int get_data; + + rt_timer_init(&timer.static_timer, + "static_timer", + static_timer_control, + &timer, + 5, + RT_TIMER_FLAG_PERIODIC); + + /* test set data */ + set_data = rand_num; + result = rt_timer_control(&timer.static_timer, RT_TIMER_CTRL_SET_TIME, &set_data); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + /* test get data */ + result = rt_timer_control(&timer.static_timer, RT_TIMER_CTRL_GET_TIME, &get_data); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + /* a set of test */ + if (set_data != get_data) + { + uassert_true(RT_FALSE); + } + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + set_data; + + /* start timer */ + result = rt_timer_start(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + rt_thread_delay(set_data + 1); + + /* detach timer */ + result = rt_timer_detach(&timer.static_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + + timer.test_flag = RT_FALSE; + uassert_true(RT_TRUE); +} + +#ifdef RT_USING_HEAP + +#define test_dynamic_timer_start test_dynamic_timer_create +#define test_dynamic_timer_stop test_dynamic_timer_create +#define test_dynamic_timer_delete test_dynamic_timer_create + +static void dynamic_timer_oneshot(void *param) +{ + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + return; +} +static void dynamic_timer_periodic(void *param) +{ + rt_err_t result; + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + /* periodic timer can stop */ + result = rt_timer_stop(timer_call->dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + return; +} + +static void test_dynamic_timer_create(void) +{ + rt_err_t result; + int rand_num = rand() % 10; + + /* one shot timer test */ + for (int time_out = 0; time_out < rand_num; time_out++) + { + for (int i = 0; i < sizeof(timer_flag_oneshot); i++) + { + timer.dynamic_timer = rt_timer_create("dynamic_timer", + dynamic_timer_oneshot, + &timer, + time_out, + timer_flag_oneshot[i]); + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + time_out; + + /* start timer */ + result = rt_timer_start(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* wait for timerout */ + rt_thread_delay(time_out + 1); + + /* detach timer */ + result = rt_timer_delete(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + } + } + + /* periodic timer test */ + for (int time_out = 0; time_out < rand_num; time_out++) + { + for (int i = 0; i < sizeof(timer_flag_periodic); i++) + { + timer.dynamic_timer = rt_timer_create("dynamic_timer", + dynamic_timer_periodic, + &timer, + time_out, + timer_flag_periodic[i]); + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + time_out; + + /* start timer */ + result = rt_timer_start(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + /* wait for timerout */ + rt_thread_delay(time_out + 1); + + /* detach timer */ + result = rt_timer_delete(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + } + } + + timer.test_flag = RT_FALSE; + uassert_true(RT_TRUE); + + return; +} + +static void dynamic_timer_control(void *param) +{ + rt_err_t result; + timer_struct *timer_call; + timer_call = (timer_struct *)param; + timer_call->test_flag = RT_TRUE; + + /* check expect tick */ + if (rt_tick_get() - timer_call->expect_tick > 1) + { + uassert_true(RT_FALSE); + } + + /* periodic timer can stop */ + result = rt_timer_stop(timer_call->dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + return; +} + +static void test_dynamic_timer_control(void) +{ + rt_err_t result; + int rand_num = rand() % 10; + int set_data; + int get_data; + + timer.dynamic_timer = rt_timer_create("dynamic_timer", + dynamic_timer_control, + &timer, + 5, + RT_TIMER_FLAG_PERIODIC); + + /* test set data */ + set_data = rand_num; + result = rt_timer_control(timer.dynamic_timer, RT_TIMER_CTRL_SET_TIME, &set_data); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + /* test get data */ + result = rt_timer_control(timer.dynamic_timer, RT_TIMER_CTRL_GET_TIME, &get_data); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + } + + /* a set of test */ + if (set_data != get_data) + { + uassert_true(RT_FALSE); + } + + /* calc expect tick */ + timer.expect_tick = rt_tick_get() + set_data; + + /* start timer */ + result = rt_timer_start(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + rt_thread_delay(set_data + 1); + + /* detach timer */ + result = rt_timer_delete(timer.dynamic_timer); + if (RT_EOK != result) + { + uassert_true(RT_FALSE); + return; + } + + if (timer.test_flag != RT_TRUE) + { + uassert_true(RT_FALSE); + return; + } + + timer.test_flag = RT_FALSE; + uassert_true(RT_TRUE); +} + +#endif /* RT_USING_HEAP */ + +static rt_err_t utest_tc_init(void) +{ + timer.dynamic_timer = RT_NULL; + timer.test_flag = RT_FALSE; + + return RT_EOK; +} + +static rt_err_t utest_tc_cleanup(void) +{ + timer.dynamic_timer = RT_NULL; + timer.test_flag = RT_FALSE; + + return RT_EOK; +} + +static void testcase(void) +{ + UTEST_UNIT_RUN(test_static_timer_init); + UTEST_UNIT_RUN(test_static_timer_start); + UTEST_UNIT_RUN(test_static_timer_stop); + UTEST_UNIT_RUN(test_static_timer_detach); + UTEST_UNIT_RUN(test_static_timer_control); +#ifdef RT_USING_HEAP + UTEST_UNIT_RUN(test_dynamic_timer_create); + UTEST_UNIT_RUN(test_dynamic_timer_start); + UTEST_UNIT_RUN(test_dynamic_timer_stop); + UTEST_UNIT_RUN(test_dynamic_timer_delete); + UTEST_UNIT_RUN(test_dynamic_timer_control); +#endif /* RT_USING_HEAP */ +} +UTEST_TC_EXPORT(testcase, "testcases.kernel.timer_tc", utest_tc_init, utest_tc_cleanup, 1000); + +/*********************** end of file ****************************/ |
