tests: kernel context: rewrite test to use ztest correctly

The test was using ztest incorrectly exposing everything as one single
test function. We now have multiple tests that can be tracked back to
features and requirements.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
Anas Nashif 2018-05-25 13:28:12 -04:00
commit c2e9eef8b0
3 changed files with 266 additions and 301 deletions

View file

@ -1,2 +1,5 @@
CONFIG_IRQ_OFFLOAD=y
CONFIG_ZTEST=y
#CONFIG_MAIN_STACK_SIZE=1024
#CONFIG_ZTEST_STACKSIZE=2048
#CONFIG_IDLE_STACK_SIZE=512

View file

@ -8,17 +8,16 @@
* @brief test context and thread APIs
*
* This module tests the following CPU and thread related routines:
* k_thread_create, k_yield(), k_is_in_isr(),
* k_thread_create(), k_yield(), k_is_in_isr(),
* k_current_get(), k_cpu_idle(), k_cpu_atomic_idle(),
* irq_lock(), irq_unlock(),
* irq_offload(), irq_enable(), irq_disable(),
*/
#include <tc_util.h>
#include <ztest.h>
#include <kernel_structs.h>
#include <arch/cpu.h>
#include <irq_offload.h>
#include <ztest.h>
/*
* Include board.h from platform to get IRQ number.
@ -28,7 +27,8 @@
#include <board.h>
#endif
#define THREAD_STACKSIZE (384 + CONFIG_TEST_EXTRA_STACKSIZE)
#define THREAD_STACKSIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
#define THREAD_STACKSIZE2 (384 + CONFIG_TEST_EXTRA_STACKSIZE)
#define THREAD_PRIORITY 4
#define THREAD_SELF_CMD 0
@ -101,6 +101,7 @@ typedef struct {
};
} ISR_INFO;
typedef int (*disable_int_func) (int);
typedef void (*enable_int_func) (int);
@ -109,21 +110,21 @@ static struct k_timer timer;
static struct k_sem reply_timeout;
struct k_fifo timeout_order_fifo;
static int thread_detected_error;
static int thread_evidence;
static K_THREAD_STACK_DEFINE(thread_stack1, THREAD_STACKSIZE);
static K_THREAD_STACK_DEFINE(thread_stack2, THREAD_STACKSIZE);
static K_THREAD_STACK_DEFINE(thread_stack3, THREAD_STACKSIZE);
static struct k_thread thread_data1;
static struct k_thread thread_data2;
static struct k_thread thread_data3;
static ISR_INFO isr_info;
/**
*
* @brief Handler to perform various actions from within an ISR context
*
* This routine is the ISR handler for isr_handler_trigger(). It performs
* This routine is the ISR handler for isr_handler_trigger(). It performs
* the command requested in <isr_info.command>.
*
* @return N/A
@ -169,30 +170,59 @@ static void isr_handler_trigger(void)
*
* This routine initializes the kernel objects used in this module's tests.
*
* @return TC_PASS
*/
static int kernel_init_objects(void)
static void kernel_init_objects(void)
{
k_sem_init(&sem_thread, 0, UINT_MAX);
k_sem_init(&reply_timeout, 0, UINT_MAX);
k_timer_init(&timer, NULL, NULL);
k_fifo_init(&timeout_order_fifo);
}
return TC_PASS;
/**
* @brief A wrapper for irq_lock()
*
* @return irq_lock() return value
*/
int irq_lock_wrapper(int unused)
{
ARG_UNUSED(unused);
return irq_lock();
}
/**
* @brief A wrapper for irq_unlock()
*
* @return N/A
*/
void irq_unlock_wrapper(int imask)
{
irq_unlock(imask);
}
/**
* @brief A wrapper for irq_disable()
*
* @return @a irq
*/
int irq_disable_wrapper(int irq)
{
irq_disable(irq);
return irq;
}
/**
* @brief A wrapper for irq_enable()
*
* @return N/A
*/
void irq_enable_wrapper(int irq)
{
irq_enable(irq);
}
#ifdef HAS_POWERSAVE_INSTRUCTION
/**
*
* @brief Test the k_cpu_idle() routine
*
* This tests the k_cpu_idle() routine. The first thing it does is align to
* a tick boundary. The only source of interrupts while the test is running is
* expected to be the tick clock timer which should wake the CPU. Thus after
* each call to k_cpu_idle(), the tick count should be one higher.
*
*/
static void test_kernel_cpu_idle(int atomic)
static void _test_kernel_cpu_idle(int atomic)
{
int tms, tms2;; /* current time in millisecond */
int i; /* loop variable */
@ -218,68 +248,52 @@ static void test_kernel_cpu_idle(int atomic)
tms += sys_clock_us_per_tick / USEC_PER_MSEC;
tms2 = k_uptime_get_32();
zassert_false(tms2 < tms, "Bad ms per tick value computed,"
"got %d which is less than %d\n",
tms2, tms);
"got %d which is less than %d\n",
tms2, tms);
}
}
/**
*
* @brief Test the k_cpu_idle() routine
*
* This tests the k_cpu_idle() routine. The first thing it does is align to
* a tick boundary. The only source of interrupts while the test is running is
* expected to be the tick clock timer which should wake the CPU. Thus after
* each call to k_cpu_idle(), the tick count should be one higher.
*
* @see k_cpu_idle()
*/
#ifndef CONFIG_ARM
static void test_kernel_cpu_idle_atomic(void)
{
_test_kernel_cpu_idle(1);
}
#else
static void test_kernel_cpu_idle_atomic(void)
{
ztest_test_skip();
}
#endif
/**
*
* @brief A wrapper for irq_lock()
*
* @return irq_lock() return value
*/
int irq_lock_wrapper(int unused)
static void test_kernel_cpu_idle(void)
{
ARG_UNUSED(unused);
return irq_lock();
_test_kernel_cpu_idle(0);
}
/**
*
* @brief A wrapper for irq_unlock()
*
* @return N/A
*/
void irq_unlock_wrapper(int imask)
#else
static void test_kernel_cpu_idle(void)
{
irq_unlock(imask);
ztest_test_skip();
}
/**
* @brief A wrapper for irq_disable()
*
* @return @a irq
*/
int irq_disable_wrapper(int irq)
static void test_kernel_cpu_idle_atomic(void)
{
irq_disable(irq);
return irq;
ztest_test_skip();
}
#endif
/**
*
* @brief A wrapper for irq_enable()
*
* @return N/A
*/
void irq_enable_wrapper(int irq)
{
irq_enable(irq);
}
/**
*
* @brief Test routines for disabling and enabling ints
*
* This routine tests the routines for disabling and enabling interrupts.
* These include irq_lock() and irq_unlock(), irq_disable() and irq_enable().
*
*/
static void test_kernel_interrupts(disable_int_func disable_int,
enable_int_func enable_int, int irq)
static void _test_kernel_interrupts(disable_int_func disable_int,
enable_int_func enable_int, int irq)
{
unsigned long long count = 0;
unsigned long long i = 0;
@ -305,7 +319,7 @@ static void test_kernel_interrupts(disable_int_func disable_int,
/*
* Inflate <count> so that when we loop later, many ticks should have
* elapsed during the loop. This later loop will not exactly match the
* elapsed during the loop. This later loop will not exactly match the
* previous loop, but it should be close enough in structure that when
* combined with the inflated count, many ticks will have passed.
*/
@ -327,11 +341,10 @@ static void test_kernel_interrupts(disable_int_func disable_int,
* Re-enable interrupts before returning (for both success and failure
* cases).
*/
enable_int(imask);
zassert_equal(tick2, tick,
"tick advanced with interrupts locked");
"tick advanced with interrupts locked");
/* Now repeat with interrupts unlocked. */
for (i = 0; i < count; i++) {
@ -343,7 +356,40 @@ static void test_kernel_interrupts(disable_int_func disable_int,
tick2 = _tick_get_32();
zassert_not_equal(tick, tick2,
"tick didn't advance as expected");
"tick didn't advance as expected");
}
/**
*
* @brief Test routines for disabling and enabling interrupts
*
* This routine tests the routines for disabling and enabling interrupts.
* These include irq_lock() and irq_unlock(), irq_disable() and irq_enable().
*
* @see irq_lock(), irq_unlock(), irq_disable(), irq_enable()
*/
static void test_kernel_interrupts(void)
{
_test_kernel_interrupts(irq_lock_wrapper, irq_unlock_wrapper, -1);
}
/**
*
* @brief Test routines for disabling and enabling interrupts (disable timer)
*
* This routine tests the routines for disabling and enabling interrupts.
* These include irq_lock() and irq_unlock(), irq_disable() and irq_enable().
*
* @see irq_lock(), irq_unlock(), irq_disable(), irq_enable()
*/
static void test_kernel_timer_interrupts(void)
{
#ifdef TICK_IRQ
/* Disable interrupts coming from the timer. */
_test_kernel_interrupts(irq_disable_wrapper, irq_enable_wrapper, TICK_IRQ);
#else
ztest_test_skip();
#endif
}
/**
@ -355,12 +401,15 @@ static void test_kernel_interrupts(disable_int_func disable_int,
* interrupted a preemptible thread). Checking those routines with cooperative
* threads are done elsewhere.
*
* @see k_current_get(), k_is_in_isr()
*/
static void test_kernel_ctx_task(void)
static void test_kernel_ctx_thread(void)
{
k_tid_t self_thread_id;
TC_PRINT("Testing k_current_get() from an ISR and task\n");
k_thread_priority_set(k_current_get(), 0);
TC_PRINT("Testing k_current_get() from an ISR and thread\n");
self_thread_id = k_current_get();
isr_info.command = THREAD_SELF_CMD;
@ -371,7 +420,7 @@ static void test_kernel_ctx_task(void)
zassert_false(isr_info.error, "ISR detected an error");
zassert_equal(isr_info.data, (void *)self_thread_id,
"ISR context ID mismatch");
"ISR context ID mismatch");
TC_PRINT("Testing k_is_in_isr() from an ISR\n");
isr_info.command = EXEC_CTX_TYPE_CMD;
@ -381,83 +430,60 @@ static void test_kernel_ctx_task(void)
zassert_false(isr_info.error, "ISR detected an error");
zassert_equal(isr_info.value, K_ISR,
"isr_info.value was not K_ISR");
"isr_info.value was not K_ISR");
TC_PRINT("Testing k_is_in_isr() from a preemptible thread\n");
zassert_false(k_is_in_isr(), "Should not be in ISR context");
zassert_false(_current->base.prio < 0,
"Current thread should have preemptible priority");
"Current thread should have preemptible priority: %d",
_current->base.prio);
}
/**
*
* @brief Test the various context/thread routines from a cooperative thread
*
* This routines tests the k_current_get and
* k_is_in_isr() routines from both a thread and an ISR (that interrupted a
* cooperative thread). Checking those routines with preemptible threads are
* done elsewhere.
* This routines tests the k_current_get() and k_is_in_isr() routines from both
* a thread and an ISR (that interrupted a cooperative thread). Checking those
* routines with preemptible threads are done elsewhere.
*
* This routine may set @a thread_detected_error to the following values:
* 1 - if thread ID matches that of the task
* 2 - if thread ID taken during ISR does not match that of the thread
* 3 - k_is_in_isr() when called from an ISR is false
* 4 - k_is_in_isr() when called from a thread is true
* 5 - if thread is not a cooperative thread
*
* @return TC_PASS on success
* @return TC_FAIL on failure
* @see k_current_get(), k_is_in_isr()
*/
static int test_kernel_thread(k_tid_t task_thread_id)
static void _test_kernel_thread(k_tid_t _thread_id)
{
k_tid_t self_thread_id;
self_thread_id = k_current_get();
if (self_thread_id == task_thread_id) {
thread_detected_error = 1;
return TC_FAIL;
}
zassert_true((self_thread_id != _thread_id), "thread id matches parent thread");
isr_info.command = THREAD_SELF_CMD;
isr_info.error = 0;
isr_handler_trigger();
if (isr_info.error || isr_info.data != (void *)self_thread_id) {
/*
* Either the ISR detected an error, or the ISR context ID
* does not match the interrupted thread's thread ID.
*/
thread_detected_error = 2;
return TC_FAIL;
}
/*
* Either the ISR detected an error, or the ISR context ID
* does not match the interrupted thread's ID.
*/
zassert_false((isr_info.error || (isr_info.data != (void *)self_thread_id)),
"Thread ID taken during ISR != calling thread");
isr_info.command = EXEC_CTX_TYPE_CMD;
isr_info.error = 0;
isr_handler_trigger();
if (isr_info.error || (isr_info.value != K_ISR)) {
thread_detected_error = 3;
return TC_FAIL;
}
zassert_false((isr_info.error || (isr_info.value != K_ISR)),
"k_is_in_isr() when called from an ISR is false");
if (k_is_in_isr()) {
thread_detected_error = 4;
return TC_FAIL;
}
zassert_false(k_is_in_isr(), "k_is_in_isr() when called from a thread is true");
if (_current->base.prio >= 0) {
thread_detected_error = 5;
return TC_FAIL;
}
return TC_PASS;
zassert_false((_current->base.prio >= 0),
"thread is not a cooperative thread");
}
/**
*
* @brief Entry point to the thread's helper
*
* This routine is the entry point to the thread's helper thread. It is used to
* This routine is the entry point to the thread's helper thread. It is used to
* help test the behavior of the k_yield() routine.
*
* @param arg1 unused
@ -479,7 +505,6 @@ static void thread_helper(void *arg1, void *arg2, void *arg3)
* This thread starts off at a higher priority than thread_entry().
* Thus, it should execute immediately.
*/
thread_evidence++;
/* Test that helper will yield to a thread of equal priority */
@ -488,41 +513,34 @@ static void thread_helper(void *arg1, void *arg2, void *arg3)
/* Lower priority to that of thread_entry() */
k_thread_priority_set(self_thread_id, self_thread_id->base.prio + 1);
k_yield(); /* Yield to thread of equal priority */
k_yield(); /* Yield to thread of equal priority */
thread_evidence++;
/* <thread_evidence> should now be 2 */
/* thread_evidence should now be 2 */
}
/**
* @brief Entry point to thread started by another thread
*
* @brief Test the k_yield() routine
*
* This routine tests the k_yield() routine. It starts another thread
* (thus also testing k_thread_create() and checks that behavior of
* k_yield() against the cases of there being a higher priority thread,
* a lower priority thread, and another thread of equal priority.
*
* On error, it may set @a thread_detected_error to one of the following values:
* 10 - helper thread ran prematurely
* 11 - k_yield() did not yield to a higher priority thread
* 12 - k_yield() did not yield to an equal priority thread
* 13 - k_yield() yielded to a lower priority thread
*
* @return TC_PASS on success
* @return TC_FAIL on failure
* This routine is the entry point to the thread started by the thread.
*/
static int test_k_yield(void)
static void k_yield_entry(void *arg0, void *arg1, void *arg2)
{
k_tid_t self_thread_id;
/*
* Start a thread of higher priority. Note that since the new thread is
* being started from a thread, it will not automatically switch to the
* thread as it would if done from a task.
*/
ARG_UNUSED(arg0);
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
thread_evidence++; /* Prove that the thread has run */
k_sem_take(&sem_thread, K_FOREVER);
/*
* Start a thread of higher priority. Note that since the new thread is
* being started from a thread, it will not automatically switch to the
* thread as it would if done from another thread.
*/
self_thread_id = k_current_get();
thread_evidence = 0;
@ -530,96 +548,57 @@ static int test_k_yield(void)
thread_helper, NULL, NULL, NULL,
K_PRIO_COOP(THREAD_PRIORITY - 1), 0, 0);
if (thread_evidence != 0) {
/* ERROR! Helper created at higher */
thread_detected_error = 10; /* priority ran prematurely. */
return TC_FAIL;
}
zassert_equal(thread_evidence, 0,
"Helper created at higher priority ran prematurely.");
/*
* Test that the thread will yield to the higher priority helper.
* thread_evidence is still 0.
*/
k_yield();
if (thread_evidence == 0) {
/* ERROR! Did not yield to higher */
thread_detected_error = 11; /* priority thread. */
return TC_FAIL;
}
zassert_not_equal(thread_evidence, 0,
"k_yield() did not yield to a higher priority thread: %d",
thread_evidence);
if (thread_evidence > 1) {
/* ERROR! Helper did not yield to */
thread_detected_error = 12; /* equal priority thread. */
return TC_FAIL;
}
zassert_false((thread_evidence > 1),
"k_yield() did not yield to an equal priority thread: %d",
thread_evidence);
/*
* Raise the priority of thread_entry(). Calling k_yield() should
* Raise the priority of thread_entry(). Calling k_yield() should
* not result in switching to the helper.
*/
k_thread_priority_set(self_thread_id, self_thread_id->base.prio - 1);
k_yield();
if (thread_evidence != 1) {
/* ERROR! Context switched to a lower */
thread_detected_error = 13; /* priority thread! */
return TC_FAIL;
}
zassert_equal(thread_evidence, 1,
"k_yield() yielded to a lower priority thread");
/*
* Block on sem_thread. This will allow the helper thread to
* complete. The main task will wake this thread.
* Block on sem_thread. This will allow the helper thread to
* complete. The main thread will wake this thread.
*/
k_sem_take(&sem_thread, K_FOREVER);
return TC_PASS;
}
/**
* @brief Entry point to thread started by the task
*
* This routine is the entry point to the thread started by the task.
*
* @param task_thread_id thread ID of the spawning task
* @param arg1 unused
* @param arg2 unused
*
* @return N/A
*/
static void thread_entry(void *task_thread_id, void *arg1, void *arg2)
static void kernel_thread_entry(void *_thread_id, void *arg1, void *arg2)
{
int rv;
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
thread_evidence++; /* Prove to the task that the thread has run */
thread_evidence++; /* Prove that the thread has run */
k_sem_take(&sem_thread, K_FOREVER);
rv = test_kernel_thread((k_tid_t) task_thread_id);
if (rv != TC_PASS) {
return;
}
_test_kernel_thread((k_tid_t) _thread_id);
/* Allow the task to print any messages before the next test runs */
k_sem_take(&sem_thread, K_FOREVER);
rv = test_k_yield();
if (rv != TC_PASS) {
return;
}
}
/*
* Timeout tests
* @brief Timeout tests
*
* Test the k_sleep() API, as well as the k_thread_create() ones.
*/
struct timeout_order {
void *link_in_fifo;
s32_t timeout;
@ -639,11 +618,11 @@ struct timeout_order timeouts[] = {
#define NUM_TIMEOUT_THREADS ARRAY_SIZE(timeouts)
static K_THREAD_STACK_ARRAY_DEFINE(timeout_stacks, NUM_TIMEOUT_THREADS,
THREAD_STACKSIZE);
THREAD_STACKSIZE2);
static struct k_thread timeout_threads[NUM_TIMEOUT_THREADS];
/* a thread busy waits, then reports through a fifo */
static void test_busy_wait(void *mseconds, void *arg2, void *arg3)
/* a thread busy waits */
static void busy_wait_thread(void *mseconds, void *arg2, void *arg3)
{
u32_t usecs;
@ -665,7 +644,7 @@ static void test_busy_wait(void *mseconds, void *arg2, void *arg3)
*
* Consequently, the best we can do for now to test busy waiting is
* to invoke the API and verify that it returns. (If it takes way
* too long, or never returns, the main test task may be able to
* too long, or never returns, the main test thread may be able to
* time out and report an error.)
*/
@ -673,7 +652,7 @@ static void test_busy_wait(void *mseconds, void *arg2, void *arg3)
}
/* a thread sleeps and times out, then reports through a fifo */
static void test_thread_sleep(void *delta, void *arg2, void *arg3)
static void thread_sleep(void *delta, void *arg2, void *arg3)
{
s64_t timestamp;
int timeout = (int)delta;
@ -709,46 +688,58 @@ static void delayed_thread(void *num, void *arg2, void *arg3)
k_fifo_put(&timeout_order_fifo, timeout);
}
static void test_timeout(void)
/**
* @brief Test timouts
*
* @see k_busy_wait(), k_sleep()
*/
static void test_busy_wait(void)
{
s32_t timeout;
int rv;
timeout = 20; /* in ms */
k_thread_create(&timeout_threads[0], timeout_stacks[0],
THREAD_STACKSIZE2, busy_wait_thread,
(void *)(intptr_t) timeout, NULL,
NULL, K_PRIO_COOP(THREAD_PRIORITY), 0, 0);
rv = k_sem_take(&reply_timeout, timeout * 2);
zassert_false(rv, " *** thread timed out waiting for " "k_busy_wait()");
}
/**
* @brief Test timouts
*
* @see k_sleep()
*/
static void test_k_sleep(void)
{
struct timeout_order *data;
s32_t timeout;
int rv;
int i;
/* test k_busy_wait() */
TC_PRINT("Testing k_busy_wait()\n");
timeout = 20; /* in ms */
k_thread_create(&timeout_threads[0], timeout_stacks[0],
THREAD_STACKSIZE, test_busy_wait,
(void *)(intptr_t) timeout, NULL,
NULL, K_PRIO_COOP(THREAD_PRIORITY), 0, 0);
rv = k_sem_take(&reply_timeout, timeout * 2);
zassert_false(rv, " *** task timed out waiting for " "k_busy_wait()");
/* test k_sleep() */
TC_PRINT("Testing k_sleep()\n");
timeout = 50;
k_thread_create(&timeout_threads[0], timeout_stacks[0],
THREAD_STACKSIZE, test_thread_sleep,
THREAD_STACKSIZE2, thread_sleep,
(void *)(intptr_t) timeout, NULL,
NULL, K_PRIO_COOP(THREAD_PRIORITY), 0, 0);
rv = k_sem_take(&reply_timeout, timeout * 2);
zassert_equal(rv, 0, " *** task timed out waiting for thread on "
"k_sleep().");
zassert_equal(rv, 0, " *** thread timed out waiting for thread on "
"k_sleep().");
/* test k_thread_create() without cancellation */
TC_PRINT("Testing k_thread_create() without cancellation\n");
for (i = 0; i < NUM_TIMEOUT_THREADS; i++) {
k_thread_create(&timeout_threads[i], timeout_stacks[i],
THREAD_STACKSIZE,
THREAD_STACKSIZE2,
delayed_thread,
(void *)i,
NULL, NULL,
@ -760,8 +751,8 @@ static void test_timeout(void)
" delayed thread");
zassert_equal(data->timeout_order, i,
" *** wrong delayed thread ran (got %d, "
"expected %d)\n", data->timeout_order, i);
" *** wrong delayed thread ran (got %d, "
"expected %d)\n", data->timeout_order, i);
TC_PRINT(" got thread (q order: %d, t/o: %d) as expected\n",
data->q_order, data->timeout);
@ -785,7 +776,7 @@ static void test_timeout(void)
k_tid_t id;
id = k_thread_create(&timeout_threads[i], timeout_stacks[i],
THREAD_STACKSIZE, delayed_thread,
THREAD_STACKSIZE2, delayed_thread,
(void *)i, NULL, NULL,
K_PRIO_COOP(5), 0, timeouts[i].timeout);
@ -807,7 +798,7 @@ static void test_timeout(void)
}
if (j < NUM_TIMEOUT_THREADS) {
k_thread_cancel(delayed_threads[j]);
k_thread_abort(delayed_threads[j]);
++next_cancellation;
continue;
}
@ -819,8 +810,8 @@ static void test_timeout(void)
" delayed thread");
zassert_equal(data->timeout_order, i,
" *** wrong delayed thread ran (got %d, "
"expected %d)\n", data->timeout_order, i);
" *** wrong delayed thread ran (got %d, "
"expected %d)\n", data->timeout_order, i);
TC_PRINT(" got (q order: %d, t/o: %d, t/o order %d) "
"as expected\n", data->q_order, data->timeout,
@ -828,8 +819,8 @@ static void test_timeout(void)
}
zassert_equal(num_cancellations, next_cancellation,
" *** wrong number of cancellations (expected %d, "
"got %d\n", num_cancellations, next_cancellation);
" *** wrong number of cancellations (expected %d, "
"got %d\n", num_cancellations, next_cancellation);
/* ensure no more thread fire */
data = k_fifo_get(&timeout_order_fifo, 750);
@ -838,91 +829,61 @@ static void test_timeout(void)
}
/**
* @brief Entry point to timer tests
*
* This is the entry point to the CPU and thread tests.
* @brief Test the k_yield() routine
*
* @return N/A
* Tests the k_yield() routine. It starts another thread
* (thus also testing k_thread_create()) and checks that behavior of
* k_yield() against the a higher priority thread,
* a lower priority thread, and another thread of equal priority.
*
* @see k_yield()
*/
void test_context(void)
void test_k_yield(void)
{
int rv; /* return value from tests */
thread_detected_error = 0;
thread_evidence = 0;
/*
* Main thread(test_main) priority is 0 but ztest thread runs at
* priority -1. To run the test smoothly make both main and ztest
* threads run at same priority level.
*/
k_thread_priority_set(k_current_get(), 0);
TC_PRINT("Initializing kernel objects\n");
rv = kernel_init_objects();
zassert_equal(rv, TC_PASS, "failure kernel objects");
TC_PRINT("Testing interrupt locking and unlocking\n");
TC_START("test_kernel_interrupts");
test_kernel_interrupts(irq_lock_wrapper, irq_unlock_wrapper, -1);
#ifdef TICK_IRQ
/* Disable interrupts coming from the timer. */
TC_PRINT("Testing irq_disable() and irq_enable()\n");
test_kernel_interrupts(irq_disable_wrapper, irq_enable_wrapper,
TICK_IRQ);
#else
TC_PRINT("Test of irq_disable() and irq_enable() skipped "
"(TICK_IRQ not defined in this platform)\n");
#endif
TC_PRINT("Testing some kernel context routines\n");
test_kernel_ctx_task();
TC_PRINT("Spawning a thread from a task\n");
thread_evidence = 0;
k_sem_init(&sem_thread, 0, UINT_MAX);
k_thread_create(&thread_data1, thread_stack1, THREAD_STACKSIZE,
thread_entry, k_current_get(), NULL,
k_yield_entry, NULL, NULL,
NULL, K_PRIO_COOP(THREAD_PRIORITY), 0, 0);
zassert_equal(thread_evidence, 1,
" - thread did not execute as expected!");
/*
* The thread ran, now wake it so it can test k_current_get and
* k_is_in_isr.
*/
TC_PRINT("Thread to test k_current_get() and " "k_is_in_isr()\n");
k_sem_give(&sem_thread);
zassert_equal(thread_detected_error, 0,
" - failure detected in thread; "
"thread_detected_error = %d\n", thread_detected_error);
TC_PRINT("Thread to test k_yield()\n");
k_sem_give(&sem_thread);
zassert_equal(thread_detected_error, 0,
" - failure detected in thread; "
"thread_detected_error = %d\n", thread_detected_error);
"Thread did not execute as expected!: %d", thread_evidence);
k_sem_give(&sem_thread);
k_sem_give(&sem_thread);
k_sem_give(&sem_thread);
}
test_timeout();
#ifdef HAS_POWERSAVE_INSTRUCTION
TC_PRINT("Testing k_cpu_idle()\n");
test_kernel_cpu_idle(0);
#ifndef CONFIG_ARM
TC_PRINT("Testing k_cpu_atomic_idle()\n");
test_kernel_cpu_idle(1);
#endif
#endif
void test_kernel_thread(void)
{
k_thread_create(&thread_data3, thread_stack3, THREAD_STACKSIZE,
kernel_thread_entry, NULL, NULL,
NULL, K_PRIO_COOP(THREAD_PRIORITY), 0, 0);
}
/*test case main entry*/
void test_main(void)
{
ztest_test_suite(context, ztest_unit_test(test_context));
kernel_init_objects();
ztest_test_suite(context,
ztest_unit_test(test_kernel_interrupts),
ztest_unit_test(test_kernel_timer_interrupts),
ztest_unit_test(test_kernel_ctx_thread),
ztest_unit_test(test_busy_wait),
ztest_unit_test(test_k_sleep),
ztest_unit_test(test_kernel_cpu_idle_atomic),
ztest_unit_test(test_kernel_cpu_idle),
ztest_unit_test(test_k_yield),
ztest_unit_test(test_kernel_thread)
);
ztest_run_test_suite(context);
}

View file

@ -1,3 +1,4 @@
tests:
kernel.common:
tags: core bat_commit
min_ram: 16