-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: add tests for clear_and_insert.
Also change how tests are specified in the build system.
- Loading branch information
Showing
2 changed files
with
59 additions
and
8 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,49 @@ | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include "util-bits.h" | ||
|
||
TEST_CASE("clear_and_insert unsigned basic", "[bits-test]") | ||
{ | ||
uint32_t reg = UINT32_MAX; | ||
clear_and_insert(reg, 0x49U, 0x7f00); | ||
CHECK(reg == 0xffffc9ff); | ||
} | ||
|
||
TEST_CASE("clear_and_insert signed basic", "[bits-test]") | ||
{ | ||
uint32_t reg; | ||
|
||
/* using negative value */ | ||
reg = UINT32_MAX; | ||
clear_and_insert(reg, -0x2a70, 0x00ffff00); /* equivalent to 0xd590 */ | ||
CHECK(reg == 0xffd590ff); | ||
|
||
/* using cast to unsigned */ | ||
reg = UINT32_MAX; | ||
clear_and_insert(reg, (uint16_t)-0x2a70, 0x00ffff00); | ||
CHECK(reg == 0xffd590ff); | ||
} | ||
|
||
const uint32_t range_mask = 0x1ff000; | ||
|
||
TEST_CASE("clear_and_insert unsigned range", "[bits-test]") | ||
{ | ||
uint32_t reg = 0; | ||
CHECK_THROWS_AS(clear_and_insert(reg, 1000U, range_mask), std::runtime_error); | ||
CHECK_THROWS_AS(clear_and_insert(reg, -1000, range_mask), std::runtime_error); | ||
|
||
CHECK_NOTHROW(clear_and_insert(reg, 511U, range_mask)); | ||
CHECK_NOTHROW(clear_and_insert(reg, 255U, range_mask)); | ||
CHECK_NOTHROW(clear_and_insert(reg, 0U, range_mask)); | ||
} | ||
|
||
TEST_CASE("clear_and_insert signed range", "[bits-test]") | ||
{ | ||
uint32_t reg = 0; | ||
CHECK_THROWS_AS(clear_and_insert(reg, 1000, range_mask), std::runtime_error); | ||
CHECK_THROWS_AS(clear_and_insert(reg, -1000, range_mask), std::runtime_error); | ||
|
||
CHECK_NOTHROW(clear_and_insert(reg, 255, range_mask)); | ||
CHECK_NOTHROW(clear_and_insert(reg, 0, range_mask)); | ||
CHECK_NOTHROW(clear_and_insert(reg, -256, range_mask)); | ||
} |
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