Skip to content

Commit

Permalink
tests: lib: Add sys_util_expr test
Browse files Browse the repository at this point in the history
Add tests for EXPR utilities

Signed-off-by: TOKITA Hiroshi <[email protected]>
  • Loading branch information
soburi committed Jan 19, 2025
1 parent 2158675 commit 8b93c98
Show file tree
Hide file tree
Showing 10 changed files with 664 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/lib/sys_util_expr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(sys_util_expr)

target_sources(app PRIVATE
src/main.c
src/logical.c
src/shift.c
src/compare.c
src/add_sub.c
src/multiply.c
src/division.c
)
1 change: 1 addition & 0 deletions tests/lib/sys_util_expr/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_ZTEST=y
72 changes: 72 additions & 0 deletions tests/lib/sys_util_expr/src/add_sub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2025 TOKITA Hiroshi
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/ztest.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/util_expr.h>

#pragma GCC diagnostic ignored "-Wshift-count-overflow"
#pragma GCC diagnostic ignored "-Woverflow"

#define Z_EXPR_HEX_0xFFFFFFFE \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 0
#define Z_EXPR_HEX_0x7FFFFFFF \
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1
#define Z_EXPR_HEX_0xFFFFFFFF \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1

/**
* @brief Verify that the behavior of EXPR_ADD is equivalent to uint32 addition in C.
*/
ZTEST(sys_util_expr_add_sub, test_add)
{
zassert_equal(0x0 + 0x0, EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0x0), EXPR_HEX(0x0))));
zassert_equal(0x0 + 0x1, EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0x0), EXPR_HEX(0x1))));
zassert_equal(0x1 + 0x0, EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0x1), EXPR_HEX(0x0))));
zassert_equal(0x1 + 0x1, EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0x1), EXPR_HEX(0x1))));
zassert_equal(0x2 + 0x1, EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0x2), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE + 0x1,
EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE + 0x2,
EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF + 0x0,
EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x0))));
zassert_equal(0xFFFFFFFF + 0x1,
EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFF + 0x2,
EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF + 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_ADD(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0xFFFFFFFF))));
}

/**
* @brief Verify that the behavior of EXPR_SUB is equivalent to uint32_t subtraction in C.
*/
ZTEST(sys_util_expr_add_sub, test_sub)
{
zassert_equal(0x0 - 0x0, EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0x0), EXPR_HEX(0x0))));
zassert_equal(0x0 - 0x1, EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0x0), EXPR_HEX(0x1))));
zassert_equal(0x1 - 0x0, EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0x1), EXPR_HEX(0x0))));
zassert_equal(0x1 - 0x1, EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0x1), EXPR_HEX(0x1))));
zassert_equal(0x2 - 0x1, EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0x2), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE - 0x1,
EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE - 0x2,
EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF - 0x0,
EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x0))));
zassert_equal(0xFFFFFFFF - 0x1,
EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFF - 0x2,
EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF - 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_SUB(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0xFFFFFFFF))));
}

ZTEST_SUITE(sys_util_expr_add_sub, NULL, NULL, NULL, NULL, NULL);
159 changes: 159 additions & 0 deletions tests/lib/sys_util_expr/src/compare.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright (c) 2025 TOKITA Hiroshi
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/ztest.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/util_expr.h>

#pragma GCC diagnostic ignored "-Wshift-count-overflow"
#pragma GCC diagnostic ignored "-Woverflow"

#define Z_EXPR_HEX_0xFFFFFFFE \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 0
#define Z_EXPR_HEX_0x7FFFFFFF \
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1
#define Z_EXPR_HEX_0xFFFFFFFF \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1

/**
* @brief Verify that the behavior of EXPR_EQ is equivalent to == in C.
*/
ZTEST(sys_util_expr_compare, test_eq)
{
zassert_equal(0x0 == 0x0, EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0x0), EXPR_HEX(0x0))));
zassert_equal(0x0 == 0x1, EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0x0), EXPR_HEX(0x1))));
zassert_equal(0x1 == 0x0, EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0x1), EXPR_HEX(0x0))));
zassert_equal(0x1 == 0x1, EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0x1), EXPR_HEX(0x1))));
zassert_equal(0x2 == 0x1, EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0x2), EXPR_HEX(0x1))));
zassert_equal(0x2 == 0x2, EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0x2), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFE == 0x1,
EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE == 0x2,
EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF == 0x0,
EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x0))));
zassert_equal(0xFFFFFFFF == 0x1,
EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFF == 0x2,
EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0x0 == 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0x0), EXPR_HEX(0xFFFFFFFF))));
zassert_equal(0xFFFFFFFF == 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_EQ(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0xFFFFFFFF))));
}

/**
* @brief Verify that the behavior of EXPR_GT is equivalent to > in C.
*/
ZTEST(sys_util_expr_compare, test_gt)
{
zassert_equal(0x0 > 0x0, EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0x0), EXPR_HEX(0x0))));
zassert_equal(0x0 > 0x1, EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0x0), EXPR_HEX(0x1))));
zassert_equal(0x1 > 0x0, EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0x1), EXPR_HEX(0x0))));
zassert_equal(0x1 > 0x1, EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0x1), EXPR_HEX(0x1))));
zassert_equal(0x2 > 0x1, EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0x2), EXPR_HEX(0x1))));
zassert_equal(0x2 > 0x2, EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0x2), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFE > 0x1,
EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE > 0x2,
EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF > 0x0,
EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x0))));
zassert_equal(0xFFFFFFFF > 0x1,
EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFF > 0x2,
EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0x0 > 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0x0), EXPR_HEX(0xFFFFFFFF))));
zassert_equal(0xFFFFFFFF > 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_GT(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0xFFFFFFFF))));
}

/**
* @brief Verify that the behavior of EXPR_LT is equivalent to < in C.
*/
ZTEST(sys_util_expr_compare, test_lt)
{
zassert_equal(0x0 < 0x0, EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0x0), EXPR_HEX(0x0))));
zassert_equal(0x0 < 0x1, EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0x0), EXPR_HEX(0x1))));
zassert_equal(0x1 < 0x0, EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0x1), EXPR_HEX(0x0))));
zassert_equal(0x1 < 0x1, EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0x1), EXPR_HEX(0x1))));
zassert_equal(0x2 < 0x1, EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0x2), EXPR_HEX(0x1))));
zassert_equal(0x2 < 0x2, EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0x2), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFE < 0x1,
EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE < 0x2,
EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF < 0x0,
EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x0))));
zassert_equal(0xFFFFFFFF < 0x1,
EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFF < 0x2,
EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0x0 < 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0x0), EXPR_HEX(0xFFFFFFFF))));
zassert_equal(0xFFFFFFFF < 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_LT(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0xFFFFFFFF))));
}

/**
* @brief Verify that the behavior of EXPR_GTE is equivalent to >= in C.
*/
ZTEST(sys_util_expr_compare, test_gte)
{
zassert_equal(0x0 >= 0x0, EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0x0), EXPR_HEX(0x0))));
zassert_equal(0x0 >= 0x1, EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0x0), EXPR_HEX(0x1))));
zassert_equal(0x1 >= 0x0, EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0x1), EXPR_HEX(0x0))));
zassert_equal(0x1 >= 0x1, EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0x1), EXPR_HEX(0x1))));
zassert_equal(0x2 >= 0x1, EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0x2), EXPR_HEX(0x1))));
zassert_equal(0x2 >= 0x2, EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0x2), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFE >= 0x1,
EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE >= 0x2,
EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF >= 0x0,
EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x0))));
zassert_equal(0xFFFFFFFF >= 0x1,
EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFF >= 0x2,
EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0x0 >= 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0x0), EXPR_HEX(0xFFFFFFFF))));
zassert_equal(0xFFFFFFFF >= 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_GTE(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0xFFFFFFFF))));
}

/**
* @brief Verify that the behavior of EXPR_LTE is equivalent to <= in C.
*/
ZTEST(sys_util_expr_compare, test_lte)
{
zassert_equal(0x0 <= 0x0, EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0x0), EXPR_HEX(0x0))));
zassert_equal(0x0 <= 0x1, EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0x0), EXPR_HEX(0x1))));
zassert_equal(0x1 <= 0x0, EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0x1), EXPR_HEX(0x0))));
zassert_equal(0x1 <= 0x1, EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0x1), EXPR_HEX(0x1))));
zassert_equal(0x2 <= 0x1, EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0x2), EXPR_HEX(0x1))));
zassert_equal(0x2 <= 0x2, EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0x2), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFE <= 0x1,
EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE <= 0x2,
EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF <= 0x0,
EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x0))));
zassert_equal(0xFFFFFFFF <= 0x1,
EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFF <= 0x2,
EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0x0 <= 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0x0), EXPR_HEX(0xFFFFFFFF))));
zassert_equal(0xFFFFFFFF <= 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_LTE(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0xFFFFFFFF))));
}

ZTEST_SUITE(sys_util_expr_compare, NULL, NULL, NULL, NULL, NULL);
80 changes: 80 additions & 0 deletions tests/lib/sys_util_expr/src/division.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2025 TOKITA Hiroshi
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/ztest.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/util_expr.h>

#pragma GCC diagnostic ignored "-Wshift-count-overflow"
#pragma GCC diagnostic ignored "-Woverflow"

#define Z_EXPR_HEX_0xFFFFFFFE \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 0
#define Z_EXPR_HEX_0x7FFFFFFF \
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1
#define Z_EXPR_HEX_0xFFFFFFFF \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1

/**
* @brief Verify that the behavior of EXPR_DIV is equivalent to uint32_t division in C.
*/
ZTEST(sys_util_expr_division, test_div)
{
zassert_equal(0x0 / 0x1, EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0x0), EXPR_HEX(0x1))));
zassert_equal(0x1 / 0x1, EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0x1), EXPR_HEX(0x1))));
zassert_equal(0x2 / 0x1, EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0x2), EXPR_HEX(0x1))));
zassert_equal(0x2 / 0x2, EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0x2), EXPR_HEX(0x2))));
zassert_equal(0x3 / 0x2, EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0x3), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFE / 0x1,
EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE / 0x2,
EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x2))));
zassert_equal(0x7FFFFFFF / 0x1,
EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0x7FFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0x7FFFFFFF / 0x2,
EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0x7FFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF / 0x1,
EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFF / 0x2,
EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF / 0x7,
EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x7))));
zassert_equal(0xFFFFFFFF / 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_DIV(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0xFFFFFFFF))));
}

/**
* @brief Verify that the behavior of EXPR_MOD is equivalent to uint32_t modulo in C.
*/
ZTEST(sys_util_expr_division, test_mod)
{
zassert_equal(0x0 % 0x1, EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0x0), EXPR_HEX(0x1))));
zassert_equal(0x1 % 0x1, EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0x1), EXPR_HEX(0x1))));
zassert_equal(0x2 % 0x1, EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0x2), EXPR_HEX(0x1))));
zassert_equal(0x2 % 0x2, EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0x2), EXPR_HEX(0x2))));
zassert_equal(0x3 % 0x2, EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0x3), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFE % 0x1,
EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFE % 0x2,
EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0xFFFFFFFE), EXPR_HEX(0x2))));
zassert_equal(0x7FFFFFFF % 0x1,
EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0x7FFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0x7FFFFFFF % 0x2,
EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0x7FFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF % 0x1,
EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x1))));
zassert_equal(0xFFFFFFFF % 0x2,
EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x2))));
zassert_equal(0xFFFFFFFF % 0x7,
EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0x7))));
zassert_equal(0xFFFFFFFF % 0xFFFFFFFF,
EXPR_HEX_ENCODE(EXPR_MOD(EXPR_HEX(0xFFFFFFFF), EXPR_HEX(0xFFFFFFFF))));
}

ZTEST_SUITE(sys_util_expr_division, NULL, NULL, NULL, NULL, NULL);
Loading

0 comments on commit 8b93c98

Please sign in to comment.