summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Fagerburg <pfagerburg@google.com>2021-01-22 09:56:01 -0700
committerCommit Bot <commit-bot@chromium.org>2021-01-22 23:13:46 +0000
commit3a00c91522337c6bc79271d5b0fddd2852459c37 (patch)
treec5153bce0342afb3348345d3320092f9f19c56fb
parent5215f4c6dceef2d259f27d2732b2fab3e18ab5c7 (diff)
downloadchrome-ec-3a00c91522337c6bc79271d5b0fddd2852459c37.tar.gz
test: port accel_cal to Ztest
BUG=b:172240633 BRANCH=none TEST=build for both EC and Ztest: `TEST_LIST_HOST=accel_cal make runhosttests` `zmake configure --test -B build/accel_cal zephyr/test/accel_cal` Signed-off-by: Paul Fagerburg <pfagerburg@google.com> Change-Id: Ie787403b36d775d4185cafad8ca134561a97794b Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2645198 Tested-by: Paul Fagerburg <pfagerburg@chromium.org> Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Commit-Queue: Paul Fagerburg <pfagerburg@chromium.org>
-rw-r--r--docs/ztest.md27
-rw-r--r--test/accel_cal.c54
-rw-r--r--zephyr/test/accel_cal/CMakeLists.txt24
-rw-r--r--zephyr/test/accel_cal/prj.conf24
-rw-r--r--zephyr/test/accel_cal/shimmed_test_tasks.h6
-rw-r--r--zephyr/test/accel_cal/zmake.yaml10
6 files changed, 111 insertions, 34 deletions
diff --git a/docs/ztest.md b/docs/ztest.md
index 6c67699aa9..9c020c5d58 100644
--- a/docs/ztest.md
+++ b/docs/ztest.md
@@ -7,9 +7,10 @@ Zephyr's Ztest framework. All of the work is done in `src/platform/ec`.
See [Test Framework - Zephyr Project Documentation](https://docs.zephyrproject.org/1.12.0/subsystems/test/ztest.html#quick-start-unit-testing) for details about Zephyr's Ztest framework.
-See [chromium:2492527](https://crrev.com/c/2492527) and
-[chromium:2634401](https://crrev.com/c/2634401) for examples of
-porting an EC unit test to the Ztest API.
+
+For examples of porting an EC unit test to the Ztest API, see:
+* [base32](https://crrev.com/c/2492527) and [improvements](https://crrev.com/c/2634401)
+* [accel_cal](https://crrev.com/c/2645198)
## Determine source files being tested
@@ -55,8 +56,9 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
target_sources(app PRIVATE ${PLATFORM_EC}/test/base32.c)
```
-### Modify test source code
+## Modify test source code
+### Test cases
In the unit test, replace `run_test` with `TEST_MAIN()`. This will allow both
platform/ec tests and Ztests to share the same entry point.
@@ -81,8 +83,10 @@ TEST_MAIN()
Each function that is called by `ztest_unit_test` needs to be declared using
`DECLARE_EC_TEST`. Keep the `return EC_SUCCESS;` at the end
-of the test function.
+of the test function. Note that for the EC build, `TEST_MAIN` will call
+`test_reset` before running the test cases, and `test_print_result` after.
+### Assert macros
Change the `TEST_ASSERT` macros to `zassert` macros. There are plans to
automate this process, but for now, it's a manual process involving some
intelligent find-and-replace.
@@ -99,7 +103,7 @@ intelligent find-and-replace.
* `TEST_BITS_CLEARED(a, bits)` to `zassert_true(a & (int)bits == 0, "%u, 0", a & (int)bits)`
* `TEST_ASSERT_ARRAY_EQ(s, d, n)` to `zassert_mem_equal(s, d, b, NULL)`
* `TEST_CHECK(n)` to `zassert_true(n, NULL)`
-* `TEST_NEAR(a, b, epsilon, fmt)` to `zassert_true(fabs(a-b) < epsilon, "%f, %f, %f", a, b, epsilon)`
+* `TEST_NEAR(a, b, epsilon, fmt)` to `zassert_within(a, b, epsilon, fmt, a)`
* Currently, every usage of `TEST_NEAR` involves floating point values
* `TEST_ASSERT_ABS_LESS(n, t)` to `zassert_true(abs(n) < t, "%d, %d", n, t)`
* Currently, every usage of `TEST_ASSERT_ANS_LESS` involves signed integers.
@@ -117,6 +121,17 @@ Refer to
[test: Allow EC unit test to use Ztest API](https://crrev.com/c/2492527) for
the changes to the base32.c source code.
+### Tasklist
+
+For any test that has a corresponding `${TESTNAME}.tasklist`, add the file
+`shimmed_test_tasks.h` in the zephyr test directory, and in that file,
+`#include` the tasklist file. See [accel_cal](https://crrev.com/c/2645198)
+for an example.
+
+Add `CONFIG_HAS_TEST_TASKS=y` to the `prj.conf` file, as well as the appropriate
+`CONFIG_PLATFORM_EC` defines to include or exclude code that the unit under
+test uses.
+
## Build and run
Use `zmake` to build and run the test:
diff --git a/test/accel_cal.c b/test/accel_cal.c
index 1fa657b36c..274ea3c380 100644
--- a/test/accel_cal.c
+++ b/test/accel_cal.c
@@ -37,7 +37,7 @@ static bool accumulate(float x, float y, float z, float temperature)
| accel_cal_accumulate(&cal, 1000 * MSEC, x, y, z, temperature);
}
-static int test_calibrated_correctly_with_kasa(void)
+DECLARE_EC_TEST(test_calibrated_correctly_with_kasa)
{
bool has_bias;
@@ -50,15 +50,15 @@ static int test_calibrated_correctly_with_kasa(void)
accumulate(0.7171f, 0.7171f, 0.7171f, 21.0f);
has_bias = accumulate(-0.6971f, -0.6971f, -0.6971f, 21.0f);
- TEST_EQ(has_bias, true, "%d");
- TEST_NEAR(cal.bias[X], 0.01f, 0.0001f, "%f");
- TEST_NEAR(cal.bias[Y], 0.01f, 0.0001f, "%f");
- TEST_NEAR(cal.bias[Z], 0.01f, 0.0001f, "%f");
+ zassert_true(has_bias, NULL);
+ zassert_within(cal.bias[X], 0.01f, 0.0001f, "%f", cal.bias[X]);
+ zassert_within(cal.bias[Y], 0.01f, 0.0001f, "%f", cal.bias[Y]);
+ zassert_within(cal.bias[Z], 0.01f, 0.0001f, "%f", cal.bias[Z]);
return EC_SUCCESS;
}
-static int test_calibrated_correctly_with_newton(void)
+DECLARE_EC_TEST(test_calibrated_correctly_with_newton)
{
bool has_bias = false;
struct kasa_fit kasa;
@@ -78,30 +78,30 @@ static int test_calibrated_correctly_with_newton(void)
kasa_reset(&kasa);
for (i = 0; i < ARRAY_SIZE(data); i += 3) {
- TEST_EQ(has_bias, false, "%d");
+ zassert_false(has_bias, NULL);
kasa_accumulate(&kasa, data[i], data[i + 1], data[i + 2]);
has_bias = accumulate(data[i], data[i + 1], data[i + 2], 21.0f);
}
kasa_compute(&kasa, kasa_bias, &kasa_radius);
- TEST_EQ(has_bias, true, "%d");
+ zassert_true(has_bias, NULL);
/* Check that the bias is right */
- TEST_NEAR(cal.bias[X], 0.01f, 0.001f, "%f");
- TEST_NEAR(cal.bias[Y], 0.01f, 0.001f, "%f");
- TEST_NEAR(cal.bias[Z], 0.01f, 0.001f, "%f");
+ zassert_within(cal.bias[X], 0.01f, 0.001f, "%f", cal.bias[X]);
+ zassert_within(cal.bias[Y], 0.01f, 0.001f, "%f", cal.bias[Y]);
+ zassert_within(cal.bias[Z], 0.01f, 0.001f, "%f", cal.bias[Z]);
/* Demonstrate that we got a better bias compared to kasa */
- TEST_LT(sqrtf(powf(cal.bias[X] - 0.01f, 2.0f) +
- powf(cal.bias[Y] - 0.01f, 2.0f) +
- powf(cal.bias[Z] - 0.01f, 2.0f)),
- sqrtf(powf(kasa_bias[X] - 0.01f, 2.0f) +
- powf(kasa_bias[Y] - 0.01f, 2.0f) +
- powf(kasa_bias[Z] - 0.01f, 2.0f)),
- "%f");
+ zassert_true(sqrtf(powf(cal.bias[X] - 0.01f, 2.0f) +
+ powf(cal.bias[Y] - 0.01f, 2.0f) +
+ powf(cal.bias[Z] - 0.01f, 2.0f)) <
+ sqrtf(powf(kasa_bias[X] - 0.01f, 2.0f) +
+ powf(kasa_bias[Y] - 0.01f, 2.0f) +
+ powf(kasa_bias[Z] - 0.01f, 2.0f)),
+ NULL);
return EC_SUCCESS;
}
-static int test_temperature_gates(void)
+DECLARE_EC_TEST(test_temperature_gates)
{
bool has_bias;
@@ -114,7 +114,7 @@ static int test_temperature_gates(void)
accumulate(0.7171f, 0.7171f, 0.7171f, 21.0f);
has_bias = accumulate(-0.6971f, -0.6971f, -0.6971f, 31.0f);
- TEST_EQ(has_bias, false, "%d");
+ zassert_false(has_bias, NULL);
return EC_SUCCESS;
}
@@ -125,13 +125,11 @@ void before_test(void)
accel_cal_reset(&cal);
}
-void run_test(int argc, char **argv)
+TEST_MAIN()
{
- test_reset();
-
- RUN_TEST(test_calibrated_correctly_with_kasa);
- RUN_TEST(test_calibrated_correctly_with_newton);
- RUN_TEST(test_temperature_gates);
-
- test_print_result();
+ ztest_test_suite(test_accel_cal,
+ ztest_unit_test(test_calibrated_correctly_with_kasa),
+ ztest_unit_test(test_calibrated_correctly_with_newton),
+ ztest_unit_test(test_temperature_gates));
+ ztest_run_test_suite(test_accel_cal);
}
diff --git a/zephyr/test/accel_cal/CMakeLists.txt b/zephyr/test/accel_cal/CMakeLists.txt
new file mode 100644
index 0000000000..14fd70e01a
--- /dev/null
+++ b/zephyr/test/accel_cal/CMakeLists.txt
@@ -0,0 +1,24 @@
+# Copyright 2021 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.
+
+cmake_minimum_required(VERSION 3.13.1)
+find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
+project(accel_cal)
+
+# Ensure that we get the definitions from test_config.h
+zephyr_compile_definitions("TEST_ACCEL_CAL")
+
+# Include the local test directory for shimmed_test_tasks.h
+zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
+
+# Include test file and unit under test
+target_sources(app PRIVATE
+ "${PLATFORM_EC}/test/accel_cal.c"
+ "${PLATFORM_EC}/common/accel_cal.c"
+ "${PLATFORM_EC}/common/kasa.c"
+ "${PLATFORM_EC}/common/mat44.c"
+ "${PLATFORM_EC}/common/math_util.c"
+ "${PLATFORM_EC}/common/newton_fit.c"
+ "${PLATFORM_EC}/common/stillness_detector.c"
+ "${PLATFORM_EC}/common/vec3.c")
diff --git a/zephyr/test/accel_cal/prj.conf b/zephyr/test/accel_cal/prj.conf
new file mode 100644
index 0000000000..648eb63dd7
--- /dev/null
+++ b/zephyr/test/accel_cal/prj.conf
@@ -0,0 +1,24 @@
+# Copyright 2021 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.
+
+CONFIG_ZTEST=y
+CONFIG_HAS_TEST_TASKS=y
+CONFIG_POLL=y
+
+CONFIG_PLATFORM_EC=y
+CONFIG_PLATFORM_EC_I2C=n
+CONFIG_PLATFORM_EC_KEYBOARD=n
+CONFIG_PLATFORM_EC_HOSTCMD=n
+CONFIG_PLATFORM_EC_TIMER=n
+
+CONFIG_CROS_EC=y
+# Define necessary program memory locations. These are meaning less though
+CONFIG_CROS_EC_PROGRAM_MEMORY_BASE=0x10090000
+CONFIG_CROS_EC_RAM_BASE=0x200c0000
+CONFIG_CROS_EC_DATA_RAM_SIZE=0x00010000
+CONFIG_CROS_EC_RAM_SIZE=0x0000f800
+CONFIG_CROS_EC_RO_MEM_OFF=0x0
+CONFIG_CROS_EC_RO_SIZE=0xb000
+CONFIG_CROS_EC_RW_MEM_OFF=0xb000
+CONFIG_CROS_EC_RW_SIZE=0x75000
diff --git a/zephyr/test/accel_cal/shimmed_test_tasks.h b/zephyr/test/accel_cal/shimmed_test_tasks.h
new file mode 100644
index 0000000000..ff221a5ba3
--- /dev/null
+++ b/zephyr/test/accel_cal/shimmed_test_tasks.h
@@ -0,0 +1,6 @@
+/* Copyright 2021 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.
+ */
+
+#include "accel_cal.tasklist"
diff --git a/zephyr/test/accel_cal/zmake.yaml b/zephyr/test/accel_cal/zmake.yaml
new file mode 100644
index 0000000000..d4b8654312
--- /dev/null
+++ b/zephyr/test/accel_cal/zmake.yaml
@@ -0,0 +1,10 @@
+# Copyright 2021 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.
+
+board: native_posix
+supported-zephyr-versions:
+ - v2.4
+toolchain: llvm
+output-type: elf
+is-test: true