Skip to content

Commit

Permalink
util: add tests for clear_and_insert.
Browse files Browse the repository at this point in the history
Also change how tests are specified in the build system.
  • Loading branch information
ericonr committed Jan 5, 2024
1 parent f3728d4 commit b37ac17
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
49 changes: 49 additions & 0 deletions util/tests/bits-test.cc
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));
}
18 changes: 10 additions & 8 deletions util/tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ test_util_lib = static_library('test-util', test_util_src, dependencies: [utilit
copy_test = executable('copy-test', 'copy-test.cc', link_with: [test_util_lib], dependencies: [thread_dep, utilities])
test('copy-test', copy_test)

list_of_keys_test = executable('list-of-keys-test', 'list-of-keys-test.cc', dependencies: [utilities, catch2])
test('list-of-keys-test', list_of_keys_test)

fixed_test = executable('fixed-test', 'fixed-test.cc', dependencies: [utilities, catch2])
test('fixed-test', fixed_test)

decoders_test = executable('decoders-test', 'decoders-test.cc', dependencies: [utilities, catch2])
test('decoders_test', decoders_test)
tests = [
'bits-test',
'decoders-test',
'fixed-test',
'list-of-keys-test',
]
foreach test_name : tests
exe = executable(test_name, test_name + '.cc', dependencies: [utilities, catch2])
test(test_name, exe)
endforeach

0 comments on commit b37ac17

Please sign in to comment.