summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2022-09-29 15:57:08 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-10-07 17:38:41 +0000
commite43858632c11ad587ec31a4582acc5aa124bde44 (patch)
tree07612e17f010dca8bd8d3c06b5df8236805da00f
parentf2d0e133ba06196e1bf4b1cbce9b1c8ec8354cf6 (diff)
downloadchrome-ec-e43858632c11ad587ec31a4582acc5aa124bde44.tar.gz
zephyr: tests: Test remaining functions in `common/util.c`
Add tests for reverse(), parse_offset_size(), wait_for_ready(), binary_from_bits(), and ternary_from_bits() BRANCH=None BUG=b:249823196 TEST=./twister Signed-off-by: Tristan Honscheid <honscheid@google.com> Change-Id: If90c33fdbc9bd25dc52a3aa3f5141c90de4b7296 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3925111 Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com> Reviewed-by: Al Semjonovs <asemjonovs@google.com>
-rw-r--r--zephyr/test/drivers/default/CMakeLists.txt1
-rw-r--r--zephyr/test/drivers/default/src/util.c97
2 files changed, 98 insertions, 0 deletions
diff --git a/zephyr/test/drivers/default/CMakeLists.txt b/zephyr/test/drivers/default/CMakeLists.txt
index 90beb4bc35..ff4eadde02 100644
--- a/zephyr/test/drivers/default/CMakeLists.txt
+++ b/zephyr/test/drivers/default/CMakeLists.txt
@@ -81,6 +81,7 @@ target_sources(app PRIVATE
src/thermistor.c
src/uart_hostcmd.c
src/usb_mux.c
+ src/util.c
src/vboot_hash.c
src/virtual_battery.c
src/vstore.c
diff --git a/zephyr/test/drivers/default/src/util.c b/zephyr/test/drivers/default/src/util.c
new file mode 100644
index 0000000000..32f989bb0f
--- /dev/null
+++ b/zephyr/test/drivers/default/src/util.c
@@ -0,0 +1,97 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include <zephyr/ztest.h>
+#include <zephyr/fff.h>
+
+#include "util.h"
+#include "test/drivers/test_state.h"
+
+ZTEST(util, reverse)
+{
+ uint8_t input[] = { 0, 1, 2, 3, 4 };
+ uint8_t expected[] = { 4, 3, 2, 1, 0 };
+
+ reverse(input, sizeof(input));
+
+ zassert_mem_equal(input, expected, sizeof(input), NULL);
+}
+
+ZTEST(util, parse_offset_size__normal)
+{
+ const char *argv[] = { "cmd", "123", "456" };
+ int argc = ARRAY_SIZE(argv);
+ int offset = 0, size = 0;
+
+ /* shift=1 is the position of the "123" CLI arg */
+ zassert_ok(parse_offset_size(argc, argv, 1, &offset, &size), NULL);
+ zassert_equal(123, offset, NULL);
+ zassert_equal(456, size, NULL);
+}
+
+ZTEST(util, parse_offset_size__invalid_param1)
+{
+ const char *argv[] = { "cmd", "xyz" /* <- bad */, "456" };
+ int argc = ARRAY_SIZE(argv);
+ int offset = 0, size = 0;
+
+ /* shift=1 is the position of the "123" CLI arg */
+ zassert_equal(EC_ERROR_PARAM1,
+ parse_offset_size(argc, argv, 1, &offset, &size), NULL);
+}
+
+ZTEST(util, parse_offset_size__invalid_param2)
+{
+ const char *argv[] = { "cmd", "123", "xyz" /* <- bad */ };
+ int argc = ARRAY_SIZE(argv);
+ int offset = 0, size = 0;
+
+ /* shift=1 is the position of the "123" CLI arg */
+ zassert_equal(EC_ERROR_PARAM2,
+ parse_offset_size(argc, argv, 1, &offset, &size), NULL);
+}
+
+ZTEST(util, wait_for_ready)
+{
+ uint32_t reg;
+
+ /* These calls should immediately exit. If not, the test will fail by
+ * virtue of timing out.
+ */
+ reg = 1;
+ wait_for_ready(&reg, 0, 1);
+
+ reg = 0;
+ wait_for_ready(&reg, 1, 1);
+}
+
+ZTEST(util, binary_from_bits)
+{
+ int input[] = {
+ 0,
+ 1,
+ 0,
+ 1,
+ };
+
+ zassert_equal(0xA, binary_from_bits(input, ARRAY_SIZE(input)), NULL);
+ zassert_equal(0, binary_from_bits(NULL, 0), NULL);
+}
+
+ZTEST(util, ternary_from_bits)
+{
+ int input[] = {
+ 0,
+ 1,
+ 2,
+ 3,
+ };
+
+ /* Base 3 digits: 0*(3^0) + 1*(3^1) + 2*(3^2) + 3*(3^3) = 102 */
+
+ zassert_equal(102, ternary_from_bits(input, ARRAY_SIZE(input)), NULL);
+ zassert_equal(0, ternary_from_bits(NULL, 0), NULL);
+}
+
+ZTEST_SUITE(util, drivers_predicate_post_main, NULL, NULL, NULL, NULL);