tests: add tests for internal utilities

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2023-09-24 17:36:38 +05:30
parent 560bd5bfa1
commit 5fcc75bc9a
12 changed files with 160 additions and 32 deletions

View File

@@ -1,7 +1,7 @@
#include "bus.hh"
#include <catch2/catch_test_macros.hpp>
#define TAG "bus"
static constexpr auto TAG = "[bus]";
using namespace matar;
@@ -41,5 +41,3 @@ TEST_CASE_METHOD(BusFixture, "Word", TAG) {
CHECK(bus.read_halfword(100724276) == 0x491D);
CHECK(bus.read_byte(100724276) == 0x1D);
}
#undef TAG

View File

@@ -32,7 +32,7 @@ class CpuFixture {
};
};
#define TAG "arm execution"
static constexpr auto TAG = "[arm][execution]";
using namespace arm;
@@ -1051,5 +1051,3 @@ TEST_CASE_METHOD(CpuFixture, "Data Processing", TAG) {
CHECK(cpu.spsr.raw() == cpu.cpsr.raw());
}
}
#undef TAG

View File

@@ -1,7 +1,7 @@
#include "cpu/arm/instruction.hh"
#include <catch2/catch_test_macros.hpp>
#define TAG "disassembler"
static constexpr auto TAG = "[arm][disassembly]";
using namespace matar;
using namespace arm;
@@ -467,5 +467,3 @@ TEST_CASE("Software Interrupt", TAG) {
CHECK(instruction.condition == Condition::EQ);
CHECK(instruction.disassemble() == "SWIEQ");
}
#undef TAG

View File

@@ -10,6 +10,7 @@ tests_sources = files(
)
subdir('cpu')
subdir('util')
catch2 = dependency('catch2', version: '>=3.4.0', static: true)
catch2_tests = executable(

106
tests/util/bits.cc Normal file
View File

@@ -0,0 +1,106 @@
#include "util/bits.hh"
#include <catch2/catch_test_macros.hpp>
static constexpr auto TAG = "[util][bits]";
TEST_CASE("8 bits", TAG) {
uint8_t num = 45;
CHECK(get_bit(num, 0));
CHECK(!get_bit(num, 1));
CHECK(get_bit(num, 5));
CHECK(!get_bit(num, 6));
CHECK(!get_bit(num, 7));
set_bit(num, 6);
CHECK(get_bit(num, 6));
rst_bit(num, 6);
CHECK(!get_bit(num, 6));
chg_bit(num, 5, false);
CHECK(!get_bit(num, 5));
chg_bit(num, 5, true);
CHECK(get_bit(num, 5));
// 0b0110
CHECK(bit_range(num, 1, 4) == 6);
}
TEST_CASE("16 bits", TAG) {
uint16_t num = 34587;
CHECK(get_bit(num, 0));
CHECK(get_bit(num, 1));
CHECK(!get_bit(num, 5));
CHECK(!get_bit(num, 14));
CHECK(get_bit(num, 15));
set_bit(num, 14);
CHECK(get_bit(num, 14));
rst_bit(num, 14);
CHECK(!get_bit(num, 14));
chg_bit(num, 5, true);
CHECK(get_bit(num, 5));
// num = 45
chg_bit(num, 5, false);
CHECK(!get_bit(num, 5));
// 0b1000110
CHECK(bit_range(num, 2, 8) == 70);
}
TEST_CASE("32 bits", TAG) {
uint32_t num = 3194142523;
CHECK(get_bit(num, 0));
CHECK(get_bit(num, 1));
CHECK(get_bit(num, 12));
CHECK(get_bit(num, 29));
CHECK(!get_bit(num, 30));
CHECK(get_bit(num, 31));
set_bit(num, 30);
CHECK(get_bit(num, 30));
rst_bit(num, 30);
CHECK(!get_bit(num, 30));
chg_bit(num, 12, false);
CHECK(!get_bit(num, 12));
chg_bit(num, 12, true);
CHECK(get_bit(num, 12));
// 0b10011000101011111100111
CHECK(bit_range(num, 3, 25) == 5003239);
}
TEST_CASE("64 bits", TAG) {
uint64_t num = 58943208889991935;
CHECK(get_bit(num, 0));
CHECK(get_bit(num, 1));
CHECK(!get_bit(num, 10));
CHECK(get_bit(num, 55));
CHECK(!get_bit(num, 60));
set_bit(num, 63);
CHECK(get_bit(num, 63));
rst_bit(num, 63);
CHECK(!get_bit(num, 63));
chg_bit(num, 10, true);
CHECK(get_bit(num, 10));
chg_bit(num, 10, false);
CHECK(!get_bit(num, 10));
// 0b011010001
CHECK(bit_range(num, 39, 47) == 209);
}

21
tests/util/crypto.cc Normal file
View File

@@ -0,0 +1,21 @@
#include "util/crypto.hh"
#include <catch2/catch_test_macros.hpp>
static constexpr auto TAG = "[util][crypto]";
TEST_CASE("sha256 matar", TAG) {
std::array<uint8_t, 5> data = { 'm', 'a', 't', 'a', 'r' };
CHECK(crypto::sha256(data) ==
"3b02a908fd5743c0e868675bb6ae77d2a62b3b5f7637413238e2a1e0e94b6a53");
}
TEST_CASE("sha256 forgis", TAG) {
std::array<uint8_t, 32> data = { 'i', ' ', 'p', 'u', 't', ' ', 't', 'h',
'e', ' ', 'n', 'e', 'w', ' ', 'f', 'o',
'r', 'g', 'i', 's', ' ', 'o', 'n', ' ',
't', 'h', 'e', ' ', 'j', 'e', 'e', 'p' };
CHECK(crypto::sha256(data) ==
"cfddca2ce2673f355518cbe2df2a8522693c54723a469e8b36a4f68b90d2b759");
}

4
tests/util/meson.build Normal file
View File

@@ -0,0 +1,4 @@
tests_sources += files(
'bits.cc',
'crypto.cc'
)