diff --git a/tests/kernel/msgq/msgq_api/Makefile b/tests/kernel/msgq/msgq_api/Makefile new file mode 100644 index 00000000000..4de50f93d4d --- /dev/null +++ b/tests/kernel/msgq/msgq_api/Makefile @@ -0,0 +1,4 @@ +BOARD ?= qemu_x86 +CONF_FILE = prj.conf + +include ${ZEPHYR_BASE}/Makefile.inc diff --git a/tests/kernel/msgq/msgq_api/prj.conf b/tests/kernel/msgq/msgq_api/prj.conf new file mode 100644 index 00000000000..9a75212e89d --- /dev/null +++ b/tests/kernel/msgq/msgq_api/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_IRQ_OFFLOAD=y diff --git a/tests/kernel/msgq/msgq_api/src/Makefile b/tests/kernel/msgq/msgq_api/src/Makefile new file mode 100644 index 00000000000..772c9504142 --- /dev/null +++ b/tests/kernel/msgq/msgq_api/src/Makefile @@ -0,0 +1,3 @@ +include $(ZEPHYR_BASE)/tests/Makefile.test + +obj-y = main.o test_msgq_contexts.o test_msgq_fail.o test_msgq_purge.o diff --git a/tests/kernel/msgq/msgq_api/src/main.c b/tests/kernel/msgq/msgq_api/src/main.c new file mode 100644 index 00000000000..d223a5f0ea5 --- /dev/null +++ b/tests/kernel/msgq/msgq_api/src/main.c @@ -0,0 +1,41 @@ +/* + * 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_kernel_msgq + * @{ + * @defgroup t_msgq_api test_msgq_api + * @} + */ + +#include +extern void test_msgq_thread(void); +extern void test_msgq_isr(void); +extern void test_msgq_put_fail(void); +extern void test_msgq_get_fail(void); +extern void test_msgq_purge_when_put(void); + +/*test case main entry*/ +void test_main(void *p1, void *p2, void *p3) +{ + ztest_test_suite(test_msgq_api, + ztest_unit_test(test_msgq_thread), + ztest_unit_test(test_msgq_isr), + ztest_unit_test(test_msgq_put_fail), + ztest_unit_test(test_msgq_get_fail), + ztest_unit_test(test_msgq_purge_when_put)); + ztest_run_test_suite(test_msgq_api); +} diff --git a/tests/kernel/msgq/msgq_api/src/test_msgq.h b/tests/kernel/msgq/msgq_api/src/test_msgq.h new file mode 100644 index 00000000000..69d212e5331 --- /dev/null +++ b/tests/kernel/msgq/msgq_api/src/test_msgq.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2015-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. + */ + +#ifndef __TEST_MSGQ_H__ +#define __TEST_MSGQ_H__ + +#include +#include +#include + +#define TIMEOUT 100 +#define STACK_SIZE 512 +#define MSG_SIZE 4 +#define MSGQ_LEN 2 +#define MSG0 0xABCD +#define MSG1 0x1234 +#endif /* __TEST_MSGQ_H__ */ diff --git a/tests/kernel/msgq/msgq_api/src/test_msgq_contexts.c b/tests/kernel/msgq/msgq_api/src/test_msgq_contexts.c new file mode 100644 index 00000000000..647d2119215 --- /dev/null +++ b/tests/kernel/msgq/msgq_api/src/test_msgq_contexts.c @@ -0,0 +1,130 @@ +/* + * 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_kernel_msgq + * @{ + * @defgroup t_msgq_context test_msgq_context + * @brief TestPurpose: verify zephyr msgq apis across contexts + * @} + */ + +#include "test_msgq.h" + +/**TESTPOINT: init via K_MSGQ_DEFINE*/ +K_MSGQ_DEFINE(kmsgq, MSG_SIZE, MSGQ_LEN, 4); + +static char __noinit __stack tstack[STACK_SIZE]; +static char __aligned(4) tbuffer[MSG_SIZE * MSGQ_LEN]; +static uint32_t data[MSGQ_LEN] = { MSG0, MSG1 }; +static struct k_sem end_sema; + +static void put_msgq(struct k_msgq *pmsgq) +{ + int ret; + + for (int i = 0; i < MSGQ_LEN; i++) { + ret = k_msgq_put(pmsgq, (void *)&data[i], K_NO_WAIT); + assert_false(ret, NULL); + /**TESTPOINT: msgq free get*/ + assert_equal(k_msgq_num_free_get(pmsgq), MSGQ_LEN - 1 - i, NULL); + /**TESTPOINT: msgq used get*/ + assert_equal(k_msgq_num_used_get(pmsgq), i + 1, NULL); + } +} + +static void get_msgq(struct k_msgq *pmsgq) +{ + uint32_t rx_data; + int ret; + + for (int i = 0; i < MSGQ_LEN; i++) { + ret = k_msgq_get(pmsgq, &rx_data, K_FOREVER); + assert_false(ret, NULL); + assert_equal(rx_data, data[i], NULL); + /**TESTPOINT: msgq free get*/ + assert_equal(k_msgq_num_free_get(pmsgq), i + 1, NULL); + /**TESTPOINT: msgq used get*/ + assert_equal(k_msgq_num_used_get(pmsgq), MSGQ_LEN - 1 - i, NULL); + } +} + +static void purge_msgq(struct k_msgq *pmsgq) +{ + k_msgq_purge(pmsgq); + assert_equal(k_msgq_num_free_get(pmsgq), MSGQ_LEN, NULL); + assert_equal(k_msgq_num_used_get(pmsgq), 0, NULL); +} + +static void tIsr_entry(void *p) +{ + put_msgq((struct k_msgq *)p); +} + +static void tThread_entry(void *p1, void *p2, void *p3) +{ + get_msgq((struct k_msgq *)p1); + k_sem_give(&end_sema); +} + +static void msgq_thread(struct k_msgq *pmsgq) +{ + k_sem_init(&end_sema, 0, 1); + /**TESTPOINT: thread-thread data passing via message queue*/ + k_tid_t tid = k_thread_spawn(tstack, STACK_SIZE, + tThread_entry, pmsgq, NULL, NULL, + K_PRIO_PREEMPT(0), 0, 0); + put_msgq(pmsgq); + k_sem_take(&end_sema, K_FOREVER); + k_thread_abort(tid); + + /**TESTPOINT: msgq purge*/ + purge_msgq(pmsgq); +} + +static void msgq_isr(struct k_msgq *pmsgq) +{ + /**TESTPOINT: thread-isr data passing via message queue*/ + irq_offload(tIsr_entry, pmsgq); + get_msgq(pmsgq); + + /**TESTPOINT: msgq purge*/ + purge_msgq(pmsgq); +} + + +/*test cases*/ +void test_msgq_thread(void) +{ + struct k_msgq msgq; + + /**TESTPOINT: init via k_msgq_init*/ + k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN); + + msgq_thread(&msgq); + msgq_thread(&kmsgq); +} + +void test_msgq_isr(void) +{ + struct k_msgq msgq; + + /**TESTPOINT: init via k_msgq_init*/ + k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN); + + msgq_isr(&msgq); + msgq_isr(&kmsgq); +} diff --git a/tests/kernel/msgq/msgq_api/src/test_msgq_fail.c b/tests/kernel/msgq/msgq_api/src/test_msgq_fail.c new file mode 100644 index 00000000000..9f2e494e3d6 --- /dev/null +++ b/tests/kernel/msgq/msgq_api/src/test_msgq_fail.c @@ -0,0 +1,66 @@ +/* + * 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_msgq_api + * @{ + * @defgroup t_msgq_fail test_msgq_fail + * @brief TestPurpose: verify zephyr msgq return code under negative tests + * @} + */ + + +#include "test_msgq.h" + +static char __aligned(4) tbuffer[MSG_SIZE * MSGQ_LEN]; +static uint32_t data[MSGQ_LEN] = { MSG0, MSG1 }; + +/*test cases*/ +void test_msgq_put_fail(void *p1, void *p2, void *p3) +{ + struct k_msgq msgq; + + k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN); + + int ret = k_msgq_put(&msgq, (void *)&data[0], K_NO_WAIT); + + assert_false(ret, NULL); + ret = k_msgq_put(&msgq, (void *)&data[0], K_NO_WAIT); + assert_false(ret, NULL); + /**TESTPOINT: msgq put returns -ENOMSG*/ + ret = k_msgq_put(&msgq, (void *)&data[1], K_NO_WAIT); + assert_equal(ret, -ENOMSG, NULL); + /**TESTPOINT: msgq put returns -EAGAIN*/ + ret = k_msgq_put(&msgq, (void *)&data[0], TIMEOUT); + assert_equal(ret, -EAGAIN, NULL); + + k_msgq_purge(&msgq); +} + +void test_msgq_get_fail(void *p1, void *p2, void *p3) +{ + struct k_msgq msgq; + uint32_t rx_data; + + k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN); + /**TESTPOINT: msgq get returns -ENOMSG*/ + int ret = k_msgq_get(&msgq, &rx_data, K_NO_WAIT); + + assert_equal(ret, -ENOMSG, NULL); + /**TESTPOINT: msgq get returns -EAGAIN*/ + ret = k_msgq_get(&msgq, &rx_data, TIMEOUT); + assert_equal(ret, -EAGAIN, NULL); +} diff --git a/tests/kernel/msgq/msgq_api/src/test_msgq_purge.c b/tests/kernel/msgq/msgq_api/src/test_msgq_purge.c new file mode 100644 index 00000000000..5ad450abf01 --- /dev/null +++ b/tests/kernel/msgq/msgq_api/src/test_msgq_purge.c @@ -0,0 +1,66 @@ +/* + * 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_msgq_api + * @{ + * @defgroup t_msgq_purge test_msgq_purge + * @brief TestPurpose: verify zephyr msgq purge under different scenario + * @} + */ + + +#include "test_msgq.h" + +static char __noinit __stack tstack[STACK_SIZE]; +static char __aligned(4) tbuffer[MSG_SIZE * MSGQ_LEN]; +static uint32_t data[MSGQ_LEN] = { MSG0, MSG1 }; + +static void tThread_entry(void *p1, void *p2, void *p3) +{ + int ret = k_msgq_put((struct k_msgq *)p1, (void *)&data[0], TIMEOUT); + + assert_equal(ret, -ENOMSG, NULL); +} + +/*test cases*/ +void test_msgq_purge_when_put(void) +{ + struct k_msgq msgq; + int ret; + + k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN); + + /*fill the queue to full*/ + for (int i = 0; i < MSGQ_LEN; i++) { + ret = k_msgq_put(&msgq, (void *)&data[i], K_NO_WAIT); + } + /*create another thread waiting to put msg*/ + k_tid_t tid = k_thread_spawn(tstack, STACK_SIZE, + tThread_entry, &msgq, NULL, NULL, + K_PRIO_PREEMPT(0), 0, 0); + k_sleep(TIMEOUT >> 1); + /**TESTPOINT: msgq purge while another thread waiting to put msg*/ + k_msgq_purge(&msgq); + k_sleep(TIMEOUT >> 1); + k_thread_abort(tid); + + /*verify msg put after purge*/ + for (int i = 0; i < MSGQ_LEN; i++) { + ret = k_msgq_put(&msgq, (void *)&data[i], K_NO_WAIT); + assert_false(ret, NULL); + } +} diff --git a/tests/kernel/msgq/msgq_api/testcase.ini b/tests/kernel/msgq/msgq_api/testcase.ini new file mode 100644 index 00000000000..58c4d1a1747 --- /dev/null +++ b/tests/kernel/msgq/msgq_api/testcase.ini @@ -0,0 +1,2 @@ +[test] +tags = kernel