summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorWai-Hong Tam <waihong@google.com>2021-06-15 15:43:06 -0700
committerCommit Bot <commit-bot@chromium.org>2021-06-16 04:30:15 +0000
commit504216c9b3df0af440ce86571136bb7dc005fc02 (patch)
treec0e1400644b9005a4593ac73f42dff99ad8fee3b /test
parentce149d30f144c952fe4ebf617b1e30ed0b0b3c8e (diff)
downloadchrome-ec-504216c9b3df0af440ce86571136bb7dc005fc02.tar.gz
util: Add function to convert binary first base3 number
Add an util function to convert a ternary bit array (each element is either 0, 1, or 2) to a non-standard ternary number system where the first 2^n natural numbers are represented as they would be in a binary system (without any Z digits) and the following 3^n-2^n numbers use the remaining ternary representations in the normal ternary system order (skipping the values that were already used up). This function is useful for converting BOARd ID, which is initially used a binary and later decided to switch to tri-state after some revisions have already been built. BRANCH=Trogdor BUG=b:190250108 TEST=make runhosttests Change-Id: I853a04f3b28eb54c61855251dc5232c7e6994fef Signed-off-by: Wai-Hong Tam <waihong@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2964389 Reviewed-by: Julius Werner <jwerner@chromium.org>
Diffstat (limited to 'test')
-rw-r--r--test/utils.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/test/utils.c b/test/utils.c
index 82316a53cc..9402e92740 100644
--- a/test/utils.c
+++ b/test/utils.c
@@ -481,6 +481,30 @@ test_static int test_alignment_log2(void)
return EC_SUCCESS;
}
+test_static int test_binary_first_base3_from_bits(void)
+{
+ int n0[] = {0, 0, 0}; /* LSB first */
+ int n7[] = {1, 1, 1};
+ int n8[] = {2, 0, 0};
+ int n9[] = {2, 1, 0};
+ int n10[] = {0, 2, 0};
+ int n11[] = {1, 2, 0};
+ int n18[] = {0, 0, 2};
+ int n26[] = {2, 2, 2};
+ int n38[] = {1, 2, 0, 1};
+
+ TEST_EQ(binary_first_base3_from_bits(n0, ARRAY_SIZE(n0)), 0, "%d");
+ TEST_EQ(binary_first_base3_from_bits(n7, ARRAY_SIZE(n7)), 7, "%d");
+ TEST_EQ(binary_first_base3_from_bits(n8, ARRAY_SIZE(n8)), 8, "%d");
+ TEST_EQ(binary_first_base3_from_bits(n9, ARRAY_SIZE(n9)), 9, "%d");
+ TEST_EQ(binary_first_base3_from_bits(n10, ARRAY_SIZE(n10)), 10, "%d");
+ TEST_EQ(binary_first_base3_from_bits(n11, ARRAY_SIZE(n11)), 11, "%d");
+ TEST_EQ(binary_first_base3_from_bits(n18, ARRAY_SIZE(n18)), 18, "%d");
+ TEST_EQ(binary_first_base3_from_bits(n26, ARRAY_SIZE(n26)), 26, "%d");
+ TEST_EQ(binary_first_base3_from_bits(n38, ARRAY_SIZE(n38)), 38, "%d");
+ return EC_SUCCESS;
+}
+
void run_test(int argc, char **argv)
{
test_reset();
@@ -502,6 +526,7 @@ void run_test(int argc, char **argv)
RUN_TEST(test_is_aligned);
RUN_TEST(test_safe_memcmp);
RUN_TEST(test_alignment_log2);
+ RUN_TEST(test_binary_first_base3_from_bits);
test_print_result();
}