diff --git a/tests/kernel/stack/stack_api/Makefile b/tests/kernel/stack/stack_api/Makefile new file mode 100644 index 00000000000..4de50f93d4d --- /dev/null +++ b/tests/kernel/stack/stack_api/Makefile @@ -0,0 +1,4 @@ +BOARD ?= qemu_x86 +CONF_FILE = prj.conf + +include ${ZEPHYR_BASE}/Makefile.inc diff --git a/tests/kernel/stack/stack_api/prj.conf b/tests/kernel/stack/stack_api/prj.conf new file mode 100644 index 00000000000..9a75212e89d --- /dev/null +++ b/tests/kernel/stack/stack_api/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_IRQ_OFFLOAD=y diff --git a/tests/kernel/stack/stack_api/src/Makefile b/tests/kernel/stack/stack_api/src/Makefile new file mode 100644 index 00000000000..876771e9f25 --- /dev/null +++ b/tests/kernel/stack/stack_api/src/Makefile @@ -0,0 +1,3 @@ +include $(ZEPHYR_BASE)/tests/Makefile.test + +obj-y = main.o test_stack_contexts.o test_stack_fail.o diff --git a/tests/kernel/stack/stack_api/src/main.c b/tests/kernel/stack/stack_api/src/main.c new file mode 100644 index 00000000000..20747f0b014 --- /dev/null +++ b/tests/kernel/stack/stack_api/src/main.c @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup t_stack + * @{ + * @defgroup t_stack_api test_stack_api + * @} + */ + +#include +extern void test_stack_thread2thread(void); +extern void test_stack_thread2isr(void); +extern void test_stack_pop_fail(void); + +/*test case main entry*/ +void test_main(void *p1, void *p2, void *p3) +{ + ztest_test_suite(test_stack_api, + ztest_unit_test(test_stack_thread2thread), + ztest_unit_test(test_stack_thread2isr), + ztest_unit_test(test_stack_pop_fail)); + ztest_run_test_suite(test_stack_api); +} diff --git a/tests/kernel/stack/stack_api/src/test_stack_contexts.c b/tests/kernel/stack/stack_api/src/test_stack_contexts.c new file mode 100644 index 00000000000..31331c40f69 --- /dev/null +++ b/tests/kernel/stack/stack_api/src/test_stack_contexts.c @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup t_stack_api + * @{ + * @defgroup t_stack_api_basic test_stack_api_basic + * @brief TestPurpose: verify zephyr stack apis under different context + * - API coverage + * -# k_stack_init K_STACK_DEFINE + * -# k_stack_push + * -# k_stack_pop + * @} + */ + +#include +#include + +#define STACK_SIZE 512 +#define STACK_LEN 2 + +/**TESTPOINT: init via K_STACK_DEFINE*/ +K_STACK_DEFINE(kstack, STACK_LEN); +static struct k_stack stack; + +static char __noinit __stack threadstack[STACK_SIZE]; +static uint32_t data[STACK_LEN] = { 0xABCD, 0x1234 }; +static struct k_sem end_sema; + +static void tstack_push(struct k_stack *pstack) +{ + for (int i = 0; i < STACK_LEN; i++) { + /**TESTPOINT: stack push*/ + k_stack_push(pstack, data[i]); + } +} + +static void tstack_pop(struct k_stack *pstack) +{ + uint32_t rx_data; + + for (int i = STACK_LEN - 1; i >= 0; i--) { + /**TESTPOINT: stack pop*/ + assert_false(k_stack_pop(pstack, &rx_data, K_NO_WAIT), NULL); + assert_equal(rx_data, data[i], NULL); + } +} + +/*entry of contexts*/ +static void tIsr_entry_push(void *p) +{ + tstack_push((struct k_stack *)p); +} + +static void tIsr_entry_pop(void *p) +{ + tstack_pop((struct k_stack *)p); +} + +static void tThread_entry(void *p1, void *p2, void *p3) +{ + tstack_pop((struct k_stack *)p1); + k_sem_give(&end_sema); + tstack_push((struct k_stack *)p1); + k_sem_give(&end_sema); +} + +static void tstack_thread_thread(struct k_stack *pstack) +{ + k_sem_init(&end_sema, 0, 1); + /**TESTPOINT: thread-thread data passing via stack*/ + k_tid_t tid = k_thread_spawn(threadstack, STACK_SIZE, + tThread_entry, pstack, NULL, NULL, + K_PRIO_PREEMPT(0), 0, 0); + tstack_push(pstack); + k_sem_take(&end_sema, K_FOREVER); + + k_sem_take(&end_sema, K_FOREVER); + tstack_pop(pstack); + + /* clear the spawn thread to avoid side effect */ + k_thread_abort(tid); +} + +static void tstack_thread_isr(struct k_stack *pstack) +{ + k_sem_init(&end_sema, 0, 1); + /**TESTPOINT: thread-isr data passing via stack*/ + irq_offload(tIsr_entry_push, pstack); + tstack_pop(pstack); + + tstack_push(pstack); + irq_offload(tIsr_entry_pop, pstack); +} + +/*test cases*/ +void test_stack_thread2thread(void) +{ + /**TESTPOINT: test k_stack_init stack*/ + k_stack_init(&stack, data, STACK_LEN); + tstack_thread_thread(&stack); + + /**TESTPOINT: test K_STACK_INIT stack*/ + tstack_thread_thread(&kstack); +} + +void test_stack_thread2isr(void) +{ + /**TESTPOINT: test k_stack_init stack*/ + k_stack_init(&stack, data, STACK_LEN); + tstack_thread_isr(&stack); + + /**TESTPOINT: test K_STACK_INIT stack*/ + tstack_thread_isr(&kstack); +} diff --git a/tests/kernel/stack/stack_api/src/test_stack_fail.c b/tests/kernel/stack/stack_api/src/test_stack_fail.c new file mode 100644 index 00000000000..b17ff5e63db --- /dev/null +++ b/tests/kernel/stack/stack_api/src/test_stack_fail.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup t_stack_api + * @{ + * @defgroup t_stack_fail_pop test_stack_fail_pop + * @brief TestPurpose: verify zephyr stack pop when empty + * - API coverage + * -# k_stack_pop + * @} + */ + +#include +#include + +#define TIMEOUT 100 +#define STACK_LEN 2 + +static uint32_t data[STACK_LEN]; + +/*test cases*/ +void test_stack_pop_fail(void *p1, void *p2, void *p3) +{ + struct k_stack stack; + uint32_t rx_data; + + k_stack_init(&stack, data, STACK_LEN); + /**TESTPOINT: stack pop returns -EBUSY*/ + assert_equal(k_stack_pop(&stack, &rx_data, K_NO_WAIT), -EBUSY, NULL); + /**TESTPOINT: stack pop returns -EAGAIN*/ + assert_equal(k_stack_pop(&stack, &rx_data, TIMEOUT), -EAGAIN, NULL); +} diff --git a/tests/kernel/stack/stack_api/testcase.ini b/tests/kernel/stack/stack_api/testcase.ini new file mode 100644 index 00000000000..58c4d1a1747 --- /dev/null +++ b/tests/kernel/stack/stack_api/testcase.ini @@ -0,0 +1,2 @@ +[test] +tags = kernel