diff options
author | Yuval Peress <peress@google.com> | 2022-03-14 13:35:52 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2022-03-18 06:12:33 +0000 |
commit | d6ba5787e7f02e4e9e61a7cf439b8307ba2f9c90 (patch) | |
tree | 8849aa86b74c1767df3802dd0f3d985ce8683e5f | |
parent | b47f8e1c229a74b126b6c8ccca7cd10ceb5f36ef (diff) | |
download | chrome-ec-d6ba5787e7f02e4e9e61a7cf439b8307ba2f9c90.tar.gz |
zephyr: add tests to math library to fill in the gaps
Get the coverage of the math_util.c file to 100%
BRANCH=none
BUG=b:224582527
TEST=zmake test test-math_float && zmake test test-math_fixed
Signed-off-by: Yuval Peress <peress@google.com>
Change-Id: Iae45f38b56c4bd2573e9981af59edbda0c72e04a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3530112
Reviewed-by: Wai-Hong Tam <waihong@google.com>
-rw-r--r-- | zephyr/test/math/BUILD.py | 11 | ||||
-rw-r--r-- | zephyr/test/math/CMakeLists.txt | 23 | ||||
-rw-r--r-- | zephyr/test/math/fixed_point.conf | 5 | ||||
-rw-r--r-- | zephyr/test/math/floating_point.conf | 5 | ||||
-rw-r--r-- | zephyr/test/math/prj.conf | 12 | ||||
-rw-r--r-- | zephyr/test/math/src/fixed_point_int_sqrtf.c | 19 | ||||
-rw-r--r-- | zephyr/test/math/src/mask.c | 20 | ||||
-rw-r--r-- | zephyr/test/math/src/math_util.c | 41 | ||||
-rw-r--r-- | zephyr/test/math/src/suite.c | 8 | ||||
-rw-r--r-- | zephyr/test/math/src/vector.c | 29 |
10 files changed, 173 insertions, 0 deletions
diff --git a/zephyr/test/math/BUILD.py b/zephyr/test/math/BUILD.py new file mode 100644 index 0000000000..eb901b78c3 --- /dev/null +++ b/zephyr/test/math/BUILD.py @@ -0,0 +1,11 @@ +# Copyright 2022 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. + +register_host_test( + "math_fixed", kconfig_files=[here / "prj.conf", here / "fixed_point.conf"] +) +# TODO (b/224582527) uncomment when FPU is enabled in POSIX arch +# register_host_test("math_float", +# kconfig_files=[here / "prj.conf", +# here / "floating_point.conf"]) diff --git a/zephyr/test/math/CMakeLists.txt b/zephyr/test/math/CMakeLists.txt new file mode 100644 index 0000000000..e90ce4cf8c --- /dev/null +++ b/zephyr/test/math/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright 2022 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(math) + +zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include") +zephyr_include_directories("${PLATFORM_EC}/include") + +target_sources(app PRIVATE ${PLATFORM_EC}/common/math_util.c) + +target_sources( + app PRIVATE + src/suite.c + src/math_util.c + src/vector.c + src/mask.c +) + +# Fixed point specific tests +target_sources_ifndef(CONFIG_FPU app PRIVATE src/fixed_point_int_sqrtf.c) diff --git a/zephyr/test/math/fixed_point.conf b/zephyr/test/math/fixed_point.conf new file mode 100644 index 0000000000..5274cb2287 --- /dev/null +++ b/zephyr/test/math/fixed_point.conf @@ -0,0 +1,5 @@ +# Copyright 2022 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_FPU=n diff --git a/zephyr/test/math/floating_point.conf b/zephyr/test/math/floating_point.conf new file mode 100644 index 0000000000..ce8f17011d --- /dev/null +++ b/zephyr/test/math/floating_point.conf @@ -0,0 +1,5 @@ +# Copyright 2022 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_FPU=y diff --git a/zephyr/test/math/prj.conf b/zephyr/test/math/prj.conf new file mode 100644 index 0000000000..d1592a2932 --- /dev/null +++ b/zephyr/test/math/prj.conf @@ -0,0 +1,12 @@ +# Copyright 2022 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_ZTEST_ASSERT_VERBOSE=1 +CONFIG_ZTEST_NEW_API=y +CONFIG_ASSERT=y + +CONFIG_PLATFORM_EC=y +CONFIG_CROS_EC=y +CONFIG_PLATFORM_EC_MATH_UTIL=y diff --git a/zephyr/test/math/src/fixed_point_int_sqrtf.c b/zephyr/test/math/src/fixed_point_int_sqrtf.c new file mode 100644 index 0000000000..d8360ec189 --- /dev/null +++ b/zephyr/test/math/src/fixed_point_int_sqrtf.c @@ -0,0 +1,19 @@ +/* Copyright 2022 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 <ztest.h> + +#include "math.h" +#include "math_util.h" + +ZTEST_USER(math, int_sqrtf_negative) +{ + zassert_equal(int_sqrtf(-100), 0, NULL); +} + +ZTEST_USER(math, int_sqrtf_overflow) +{ + zassert_equal(int_sqrtf(INT64_MAX), INT32_MAX, NULL); +} diff --git a/zephyr/test/math/src/mask.c b/zephyr/test/math/src/mask.c new file mode 100644 index 0000000000..9ced211a88 --- /dev/null +++ b/zephyr/test/math/src/mask.c @@ -0,0 +1,20 @@ +/* Copyright 2022 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 <inttypes.h> +#include <ztest.h> + +#include "math.h" +#include "math_util.h" + +ZTEST_USER(math, bitmask_uint64) +{ + zassert_equal(bitmask_uint64(-1), 0, NULL); + zassert_equal(bitmask_uint64(64), 0, NULL); + zassert_equal(bitmask_uint64(1), UINT64_C(1) << 1, NULL); + zassert_equal(bitmask_uint64(15), UINT64_C(1) << 15, NULL); + zassert_equal(bitmask_uint64(35), UINT64_C(1) << 35, NULL); + zassert_equal(bitmask_uint64(60), UINT64_C(1) << 60, NULL); +} diff --git a/zephyr/test/math/src/math_util.c b/zephyr/test/math/src/math_util.c new file mode 100644 index 0000000000..901c3a6cc6 --- /dev/null +++ b/zephyr/test/math/src/math_util.c @@ -0,0 +1,41 @@ +/* Copyright 2022 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 <ztest.h> + +#include "math.h" +#include "math_util.h" + +ZTEST_USER(math, arc_cos__x_below_range) +{ + fp_t result = arc_cos(FLOAT_TO_FP(-1.1)); + + zassert_within(result, FLOAT_TO_FP(180.0), FLOAT_TO_FP(1.0), + "arc_cos(-1.1) was %d", FP_TO_INT(result)); +} + +ZTEST_USER(math, arc_cos__x_above_range) +{ + fp_t result = arc_cos(FLOAT_TO_FP(1.1)); + + zassert_within(result, FLOAT_TO_FP(0), FLOAT_TO_FP(1.0), + "arc_cos(1.1) was %d", FP_TO_INT(result)); +} + +ZTEST_USER(math, int_sqrtf) +{ + zassert_equal(int_sqrtf(0), 0, NULL); + zassert_equal(int_sqrtf(15), 3, NULL); + zassert_equal(int_sqrtf(25), 5, NULL); + zassert_equal(int_sqrtf(1111088889), 33333, NULL); + zassert_equal(int_sqrtf(123456789), 11111, NULL); + zassert_equal(int_sqrtf(1000000000000000005), 1000000000, NULL); +} + +ZTEST_USER(math, fp_sqrtf) +{ + zassert_within(fp_sqrtf(FLOAT_TO_FP(15)), FLOAT_TO_FP(3.872983), + FLOAT_TO_FP(0.001), NULL); +} diff --git a/zephyr/test/math/src/suite.c b/zephyr/test/math/src/suite.c new file mode 100644 index 0000000000..75b8e84bde --- /dev/null +++ b/zephyr/test/math/src/suite.c @@ -0,0 +1,8 @@ +/* Copyright 2022 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 <ztest.h> + +ZTEST_SUITE(math, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/math/src/vector.c b/zephyr/test/math/src/vector.c new file mode 100644 index 0000000000..2e8ca52c5d --- /dev/null +++ b/zephyr/test/math/src/vector.c @@ -0,0 +1,29 @@ +/* Copyright 2022 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 <ztest.h> + +#include "math.h" +#include "math_util.h" + +ZTEST_USER(math, cosine_of_angle_diff__zero_magnitude_vector) +{ + intv3_t v0 = { 0, 0, 0 }; + intv3_t v1 = { 1, 1, 1 }; + + zassert_equal(cosine_of_angle_diff(v0, v1), 0, NULL); + zassert_equal(cosine_of_angle_diff(v1, v0), 0, NULL); +} + +ZTEST_USER(math, rotate_inv__null_matrix) +{ + intv3_t v = { 1, 2, 3 }; + intv3_t r = { 4, 5, 6 }; + + rotate_inv(v, NULL, r); + zassert_equal(v[0], r[0], NULL); + zassert_equal(v[1], r[1], NULL); + zassert_equal(v[2], r[2], NULL); +} |