From 7553b4688b6535fad286a466760c4518a3e1f084 Mon Sep 17 00:00:00 2001 From: jing wang Date: Thu, 17 Nov 2016 10:39:42 +0800 Subject: [PATCH] tests: add mutex api test cases which use unified kernel add unified kernel mutext test cases which covere basic mutex apis under different conditions - timeout, no_wait, forever Change-Id: Iaab5bba80a6eebd89bfe675352d67b27024ad7bb Signed-off-by: jing wang Signed-off-by: Anas Nashif --- tests/kernel/mutex/mutex_api/Makefile | 4 + tests/kernel/mutex/mutex_api/prj.conf | 2 + tests/kernel/mutex/mutex_api/src/Makefile | 3 + tests/kernel/mutex/mutex_api/src/main.c | 43 +++++ .../mutex/mutex_api/src/test_mutex_apis.c | 164 ++++++++++++++++++ tests/kernel/mutex/mutex_api/testcase.ini | 2 + 6 files changed, 218 insertions(+) create mode 100644 tests/kernel/mutex/mutex_api/Makefile create mode 100644 tests/kernel/mutex/mutex_api/prj.conf create mode 100644 tests/kernel/mutex/mutex_api/src/Makefile create mode 100644 tests/kernel/mutex/mutex_api/src/main.c create mode 100644 tests/kernel/mutex/mutex_api/src/test_mutex_apis.c create mode 100644 tests/kernel/mutex/mutex_api/testcase.ini diff --git a/tests/kernel/mutex/mutex_api/Makefile b/tests/kernel/mutex/mutex_api/Makefile new file mode 100644 index 00000000000..4de50f93d4d --- /dev/null +++ b/tests/kernel/mutex/mutex_api/Makefile @@ -0,0 +1,4 @@ +BOARD ?= qemu_x86 +CONF_FILE = prj.conf + +include ${ZEPHYR_BASE}/Makefile.inc diff --git a/tests/kernel/mutex/mutex_api/prj.conf b/tests/kernel/mutex/mutex_api/prj.conf new file mode 100644 index 00000000000..9a75212e89d --- /dev/null +++ b/tests/kernel/mutex/mutex_api/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_IRQ_OFFLOAD=y diff --git a/tests/kernel/mutex/mutex_api/src/Makefile b/tests/kernel/mutex/mutex_api/src/Makefile new file mode 100644 index 00000000000..126afc00f41 --- /dev/null +++ b/tests/kernel/mutex/mutex_api/src/Makefile @@ -0,0 +1,3 @@ +include $(ZEPHYR_BASE)/tests/Makefile.test + +obj-y = main.o test_mutex_apis.o diff --git a/tests/kernel/mutex/mutex_api/src/main.c b/tests/kernel/mutex/mutex_api/src/main.c new file mode 100644 index 00000000000..6ebc34f0e49 --- /dev/null +++ b/tests/kernel/mutex/mutex_api/src/main.c @@ -0,0 +1,43 @@ +/* + * 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_sema + * @{ + * @defgroup t_sema_api test_sema_api + * @} + */ + + +#include +extern void test_mutex_lock_unlock(void); +extern void test_mutex_reent_lock_forever(void); +extern void test_mutex_reent_lock_no_wait(void); +extern void test_mutex_reent_lock_timeout_fail(void); +extern void test_mutex_reent_lock_timeout_pass(void); + +/*test case main entry*/ +void test_main(void *p1, void *p2, void *p3) +{ + ztest_test_suite(test_mutex_api, + ztest_unit_test(test_mutex_lock_unlock), + ztest_unit_test(test_mutex_reent_lock_forever), + ztest_unit_test(test_mutex_reent_lock_no_wait), + ztest_unit_test(test_mutex_reent_lock_timeout_fail), + ztest_unit_test(test_mutex_reent_lock_timeout_pass) + ); + ztest_run_test_suite(test_mutex_api); +} diff --git a/tests/kernel/mutex/mutex_api/src/test_mutex_apis.c b/tests/kernel/mutex/mutex_api/src/test_mutex_apis.c new file mode 100644 index 00000000000..7116f1b1234 --- /dev/null +++ b/tests/kernel/mutex/mutex_api/src/test_mutex_apis.c @@ -0,0 +1,164 @@ +/* + * 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_mutex_api + * @{ + * @defgroup t_mutex_lock test_mutex_lock + * @brief TestPurpose: verify zephyr mutex lock/unlock apis under different + * condition + * - API coverage + * -# k_mutex_init K_MUTEX_DEFINE + * -# k_mutex_lock [FOREVER NO_WAIT TIMEOUT] + * -# k_mutex_unlock + * @} + */ + +#include +#include + +#define TIMEOUT 500 +#define STACK_SIZE 512 + +/**TESTPOINT: init via K_MUTEX_DEFINE*/ +K_MUTEX_DEFINE(kmutex); +static struct k_mutex mutex; + +static char __noinit __stack tstack[STACK_SIZE]; + +static void tThread_entry_lock_forever(void *p1, void *p2, void *p3) +{ + assert_false(k_mutex_lock((struct k_mutex *)p1, K_FOREVER) == 0, + "access locked resource from spawn thread"); + /* should not hit here */ +} + +static void tThread_entry_lock_no_wait(void *p1, void *p2, void *p3) +{ + assert_true(k_mutex_lock((struct k_mutex *)p1, K_NO_WAIT) != 0, NULL); + TC_PRINT("bypass locked resource from spawn thread\n"); +} + +static void tThread_entry_lock_timeout_fail(void *p1, void *p2, void *p3) +{ + assert_true(k_mutex_lock((struct k_mutex *)p1, TIMEOUT - 100) != 0, NULL); + TC_PRINT("bypass locked resource from spawn thread\n"); +} + +static void tThread_entry_lock_timeout_pass(void *p1, void *p2, void *p3) +{ + assert_true(k_mutex_lock((struct k_mutex *)p1, TIMEOUT + 100) == 0, NULL); + TC_PRINT("access resource from spawn thread\n"); + k_mutex_unlock((struct k_mutex *)p1); +} + +static void tmutex_test_lock(struct k_mutex *pmutex, + void (*entry_fn)(void *, void *, void *)) +{ + k_mutex_init(pmutex); + k_tid_t tid = k_thread_spawn(tstack, STACK_SIZE, + entry_fn, pmutex, NULL, NULL, + K_PRIO_PREEMPT(0), 0, 0); + k_mutex_lock(pmutex, K_FOREVER); + TC_PRINT("access resource from main thread\n"); + + /* wait for spawn thread to take action */ + k_sleep(TIMEOUT); + + /* teardown */ + k_thread_abort(tid); +} + +static void tmutex_test_lock_timeout(struct k_mutex *pmutex, + void (*entry_fn)(void *, void *, void *)) +{ + /**TESTPOINT: test k_mutex_init mutex*/ + k_mutex_init(pmutex); + k_tid_t tid = k_thread_spawn(tstack, STACK_SIZE, + entry_fn, pmutex, NULL, NULL, + K_PRIO_PREEMPT(0), 0, 0); + k_mutex_lock(pmutex, K_FOREVER); + TC_PRINT("access resource from main thread\n"); + + /* wait for spawn thread to take action */ + k_sleep(TIMEOUT); + k_mutex_unlock(pmutex); + k_sleep(TIMEOUT); + + /* teardown */ + k_thread_abort(tid); +} + +static void tmutex_test_lock_unlock(struct k_mutex *pmutex) +{ + k_mutex_init(pmutex); + assert_true(k_mutex_lock(pmutex, K_FOREVER) == 0, + "fail to lock K_FOREVER"); + k_mutex_unlock(pmutex); + assert_true(k_mutex_lock(pmutex, K_NO_WAIT) == 0, + "fail to lock K_NO_WAIT"); + k_mutex_unlock(pmutex); + assert_true(k_mutex_lock(pmutex, TIMEOUT) == 0, + "fail to lock TIMEOUT"); + k_mutex_unlock(pmutex); +} + +/*test cases*/ +void test_mutex_reent_lock_forever(void) +{ + /**TESTPOINT: test k_mutex_init mutex*/ + k_mutex_init(&mutex); + tmutex_test_lock(&mutex, tThread_entry_lock_forever); + + /**TESTPOINT: test K_MUTEX_DEFINE mutex*/ + tmutex_test_lock(&kmutex, tThread_entry_lock_forever); +} + +void test_mutex_reent_lock_no_wait(void) +{ + /**TESTPOINT: test k_mutex_init mutex*/ + tmutex_test_lock(&mutex, tThread_entry_lock_no_wait); + + /**TESTPOINT: test K_MUTEX_DEFINE mutex*/ + tmutex_test_lock(&kmutex, tThread_entry_lock_no_wait); +} + +void test_mutex_reent_lock_timeout_fail(void) +{ + /**TESTPOINT: test k_mutex_init mutex*/ + tmutex_test_lock_timeout(&mutex, tThread_entry_lock_timeout_fail); + + /**TESTPOINT: test K_MUTEX_DEFINE mutex*/ + tmutex_test_lock_timeout(&kmutex, tThread_entry_lock_no_wait); +} + +void test_mutex_reent_lock_timeout_pass(void) +{ + /**TESTPOINT: test k_mutex_init mutex*/ + tmutex_test_lock_timeout(&mutex, tThread_entry_lock_timeout_pass); + + /**TESTPOINT: test K_MUTEX_DEFINE mutex*/ + tmutex_test_lock_timeout(&kmutex, tThread_entry_lock_no_wait); +} + +void test_mutex_lock_unlock(void) +{ + /**TESTPOINT: test k_mutex_init mutex*/ + tmutex_test_lock_unlock(&mutex); + + /**TESTPOINT: test K_MUTEX_DEFINE mutex*/ + tmutex_test_lock_unlock(&kmutex); +} diff --git a/tests/kernel/mutex/mutex_api/testcase.ini b/tests/kernel/mutex/mutex_api/testcase.ini new file mode 100644 index 00000000000..58c4d1a1747 --- /dev/null +++ b/tests/kernel/mutex/mutex_api/testcase.ini @@ -0,0 +1,2 @@ +[test] +tags = kernel