68 lines
1.5 KiB
C++
68 lines
1.5 KiB
C++
//
|
|
// Created by erki on 10/12/22.
|
|
//
|
|
|
|
#include <catch2/catch.hpp>
|
|
|
|
#include "utility_bytes.hpp"
|
|
|
|
TEST_CASE("zeroInitialized creates an appropriate struct.")
|
|
{
|
|
struct TestStruct
|
|
{
|
|
int a;
|
|
char b;
|
|
int c;
|
|
};
|
|
|
|
SECTION("Return by value variant returns a struct where all members are 0.")
|
|
{
|
|
const TestStruct test = Utility::zeroInitialized<TestStruct>();
|
|
REQUIRE(test.a == 0);
|
|
REQUIRE(test.b == 0);
|
|
REQUIRE(test.c == 0);
|
|
}
|
|
|
|
SECTION("Pass by reference variant initializes the struct to all 0's.")
|
|
{
|
|
TestStruct test;
|
|
Utility::zeroInitialized(test);
|
|
|
|
REQUIRE(test.a == 0);
|
|
REQUIRE(test.b == 0);
|
|
REQUIRE(test.c == 0);
|
|
}
|
|
|
|
SECTION("Pass by pointer variant initializes the struct to all 0's.")
|
|
{
|
|
TestStruct test;
|
|
Utility::zeroInitialized(&test);
|
|
|
|
REQUIRE(test.a == 0);
|
|
REQUIRE(test.b == 0);
|
|
REQUIRE(test.c == 0);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("arrayToType constructs a type appropriately.")
|
|
{
|
|
const std::uint32_t initial_value = 0x12345678;
|
|
|
|
SECTION("Using C-arrays the operation works appropriately.")
|
|
{
|
|
std::uint8_t raw_data[4] = {0};
|
|
std::memcpy(raw_data, &initial_value, 4);
|
|
|
|
const std::uint32_t output = Utility::arrayToType<std::uint32_t, 4>(raw_data);
|
|
REQUIRE(output == initial_value);
|
|
}
|
|
|
|
SECTION("Using C-arrays the operation works appropriately.")
|
|
{
|
|
std::array<std::uint8_t, 4> raw_data = {0, 0, 0, 0};
|
|
std::memcpy(raw_data.data(), &initial_value, 4);
|
|
|
|
const std::uint32_t output = Utility::arrayToType<std::uint32_t>(raw_data);
|
|
REQUIRE(output == initial_value);
|
|
}
|
|
} |