diff options
author | Paul Fagerburg <pfagerburg@google.com> | 2020-12-28 12:24:34 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-12-31 00:48:30 +0000 |
commit | 660dce8ec1e7a6545fbcd8832b9c415f22a2905f (patch) | |
tree | 6f350fb22e8c1931d71b4af562276656fbc558eb | |
parent | 4db453bdf95a7d38aae7c24aef2b1ca86833ae93 (diff) | |
download | chrome-ec-660dce8ec1e7a6545fbcd8832b9c415f22a2905f.tar.gz |
zephyr: improve shimming for unit tests
* Move the Ztest-related definitions out of test_util and into a
shim version of that header.
* Add definitions for EC unit tests declaring additional tasks so
the task shim will pick them up correctly.
* Copy the prng functions into the shimmed test_util.
BUG=None
BRANCH=None
TEST=`make clobber ; make buildall -j ; make runhosttests`
Then build the unit tests that have been ported to Ztest,
e.g. `zmake configure --test -B build/base32 zephyr/test/base32`
for `base32`, `crc`, `hooks`, `i2c`,and `tasks`
Signed-off-by: Paul Fagerburg <pfagerburg@google.com>
Change-Id: Ia2873a4c2028fcb6960e80f683189def2f3a228d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2605231
Tested-by: Paul Fagerburg <pfagerburg@chromium.org>
Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
Commit-Queue: Paul Fagerburg <pfagerburg@chromium.org>
Auto-Submit: Paul Fagerburg <pfagerburg@chromium.org>
-rw-r--r-- | include/test_util.h | 23 | ||||
-rw-r--r-- | zephyr/shim/include/shimmed_task_id.h | 21 | ||||
-rw-r--r-- | zephyr/shim/include/test_util.h | 34 | ||||
-rw-r--r-- | zephyr/shim/src/tasks.c | 7 | ||||
-rw-r--r-- | zephyr/shim/src/test_util.c | 20 |
5 files changed, 81 insertions, 24 deletions
diff --git a/include/test_util.h b/include/test_util.h index d33691a626..ef2ec0a48a 100644 --- a/include/test_util.h +++ b/include/test_util.h @@ -8,24 +8,6 @@ #ifndef __CROS_EC_TEST_UTIL_H #define __CROS_EC_TEST_UTIL_H -#ifdef CONFIG_ZTEST - -#include <ztest.h> - -/* - * We need these macros so that a test can be built for either Ztest or the - * EC test framework. - * - * Ztest unit tests are void and do not return a value. In the EC framework, - * if none of the assertions fail, the test is supposed to return EC_SUCCESS, - * so just define that as empty and `return EC_SUCCESS;` will get pre-processed - * into `return ;` - */ -#define EC_TEST_RETURN void -#define EC_SUCCESS - -#else /* CONFIG_ZTEST */ - #include "common.h" #include "console.h" #include "stack_trace.h" @@ -329,6 +311,9 @@ int test_attach_i2c(const int port, const uint16_t addr_flags); */ #define EC_TEST_RETURN int +/* An EC task only has one void parameter */ +#define TASK_PARAMS void *p1 + /* * Map the Ztest assertions onto EC assertions. There are two significant * issues here. @@ -354,6 +339,4 @@ int test_attach_i2c(const int port, const uint16_t addr_flags); #define zassert_mem_equal(buf, exp, size, msg, ...) \ TEST_ASSERT_ARRAY_EQ(buf, exp, size) -#endif /* CONFIG_ZTEST */ - #endif /* __CROS_EC_TEST_UTIL_H */ diff --git a/zephyr/shim/include/shimmed_task_id.h b/zephyr/shim/include/shimmed_task_id.h index fd23ad47de..29b6d675b6 100644 --- a/zephyr/shim/include/shimmed_task_id.h +++ b/zephyr/shim/include/shimmed_task_id.h @@ -39,17 +39,29 @@ typedef uint8_t task_id_t; CONFIG_TASK_KEYSCAN_STACK_SIZE)), ()) #elif defined(CONFIG_HAS_TEST_TASKS) #include "shimmed_test_tasks.h" -#ifndef CROS_EC_TASK_LIST -#error "shimmed_test_tasks.h should define CROS_EC_TASK_LIST" -#endif /* !CROS_EC_TASK_LIST */ +/* + * There are two different ways to define a task list (because historical + * reasons). Applications use CROS_EC_TASK_LIST to define their tasks, while + * unit tests that need additional tasks use CONFIG_TEST_TASK_LIST. For + * shimming a unit test, define CROS_EC_TASk_LIST as whatever + * CONFIG_TEST_TASK_LIST expands to. + */ +#if defined(CONFIG_TEST_TASK_LIST) && !defined(CROS_EC_TASK_LIST) +#define CROS_EC_TASK_LIST CONFIG_TEST_TASK_LIST +#endif /* CONFIG_TEST_TASK_LIST && !CROS_EC_TASK_LIST */ #endif /* !CONFIG_ZTEST */ #ifndef CROS_EC_TASK_LIST #define CROS_EC_TASK_LIST #endif /* CROS_EC_TASK_LIST */ -/* Define the task_ids globally for all shimmed platform/ec code to use */ +/* + * Define the task_ids globally for all shimmed platform/ec code to use. + * Note that unit test task lists use TASK_TEST, which we can just alias + * into a regular CROS_EC_TASK. + */ #define CROS_EC_TASK(name, ...) TASK_ID_##name, +#define TASK_TEST(name, ...) CROS_EC_TASK(name) enum { TASK_ID_IDLE = -1, /* We don't shim the idle task */ CROS_EC_TASK_LIST @@ -57,5 +69,6 @@ enum { TASK_ID_INVALID = 0xff, /* Unable to find the task */ }; #undef CROS_EC_TASK +#undef TASK_TEST #endif /* __CROS_EC_SHIMMED_TASK_ID_H */ diff --git a/zephyr/shim/include/test_util.h b/zephyr/shim/include/test_util.h new file mode 100644 index 0000000000..cc28bcf697 --- /dev/null +++ b/zephyr/shim/include/test_util.h @@ -0,0 +1,34 @@ +/* Copyright 2020 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Various utility for unit testing */ + +#ifndef __CROS_EC_TEST_UTIL_H +#define __CROS_EC_TEST_UTIL_H + +#include <ztest.h> +#include "ec_tasks.h" + +/* + * We need these macros so that a test can be built for either Ztest or the + * EC test framework. + * + * Ztest unit tests are void and do not return a value. In the EC framework, + * if none of the assertions fail, the test is supposed to return EC_SUCCESS, + * so just define that as empty and `return EC_SUCCESS;` will get pre-processed + * into `return ;` + */ +#define EC_TEST_RETURN void +#define EC_SUCCESS +#define test_pass ztest_test_pass + +/* Zephyr threads have three void pointers as parameters */ +#define TASK_PARAMS void *p1, void *p2, void *p3 + +uint32_t prng(uint32_t seed); + +uint32_t prng_no_seed(void); + +#endif /* __CROS_EC_TEST_UTIL_H */ diff --git a/zephyr/shim/src/tasks.c b/zephyr/shim/src/tasks.c index 45450076ee..8ce84adce6 100644 --- a/zephyr/shim/src/tasks.c +++ b/zephyr/shim/src/tasks.c @@ -17,13 +17,17 @@ BUILD_ASSERT(CONFIG_NUM_PREEMPT_PRIORITIES + 1 >= TASK_ID_COUNT, /* Declare all task stacks here */ #define CROS_EC_TASK(name, e, p, size) \ K_THREAD_STACK_DEFINE(name##_STACK, size); +#define TASK_TEST(name, e, p, size) CROS_EC_TASK(name, e, p, size) CROS_EC_TASK_LIST #undef CROS_EC_TASK +#undef TASK_TEST /* Forward declare all task entry point functions */ #define CROS_EC_TASK(name, entry, ...) void entry(void *p); +#define TASK_TEST(name, entry, ...) CROS_EC_TASK(name, entry) CROS_EC_TASK_LIST #undef CROS_EC_TASK +#undef TASK_TEST /** Context for each CROS EC task that is run in its own zephyr thread */ struct task_ctx { @@ -55,8 +59,11 @@ struct task_ctx { .stack_size = _size, \ .name = #_name, \ }, +#define TASK_TEST(_name, _entry, _parameter, _size) \ + CROS_EC_TASK(_name, _entry, _parameter, _size) static struct task_ctx shimmed_tasks[] = { CROS_EC_TASK_LIST }; #undef CROS_EC_TASK +#undef TASK_TEST task_id_t task_get_current(void) { diff --git a/zephyr/shim/src/test_util.c b/zephyr/shim/src/test_util.c new file mode 100644 index 0000000000..28be596043 --- /dev/null +++ b/zephyr/shim/src/test_util.c @@ -0,0 +1,20 @@ +/* Copyright 2020 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Test utilities. + */ + +#include "test_util.h" + +/* Linear congruential pseudo random number generator */ +uint32_t prng(uint32_t seed) +{ + return 22695477 * seed + 1; +} + +uint32_t prng_no_seed(void) +{ + static uint32_t seed = 0x1234abcd; + return seed = prng(seed); +} |