-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for EXPR utilities Signed-off-by: TOKITA Hiroshi <[email protected]>
- Loading branch information
Showing
10 changed files
with
664 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CONFIG_ZTEST=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.