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 <jing.j.wang@intel.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
jing wang 2016-11-17 10:39:42 +08:00 committed by Anas Nashif
commit 7553b4688b
6 changed files with 218 additions and 0 deletions

View file

@ -0,0 +1,4 @@
BOARD ?= qemu_x86
CONF_FILE = prj.conf
include ${ZEPHYR_BASE}/Makefile.inc

View file

@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_IRQ_OFFLOAD=y

View file

@ -0,0 +1,3 @@
include $(ZEPHYR_BASE)/tests/Makefile.test
obj-y = main.o test_mutex_apis.o

View file

@ -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 <ztest.h>
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);
}

View file

@ -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 <ztest.h>
#include <irq_offload.h>
#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);
}

View file

@ -0,0 +1,2 @@
[test]
tags = kernel