Skip to content

Commit

Permalink
Use a Rodos thread instead of Catch in PersistentVariables.test.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickKa committed Aug 25, 2024
1 parent 89874d6 commit 666251c
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 85 deletions.
34 changes: 22 additions & 12 deletions Tests/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# --- Tests with Catch2 ---

# TODO: Implement something like add_test_program() which adds the suffix "Test" to the output name
add_program(Dummy Dummy.test.cpp)
target_link_libraries(Sts1CobcSwTests_Dummy PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Dummy)
Expand Down Expand Up @@ -39,13 +41,6 @@ target_link_libraries(
Sts1CobcSw_Utility
)

add_program(PersistentVariables PersistentVariables.test.cpp)
target_link_libraries(
Sts1CobcSwTests_PersistentVariables
PRIVATE Catch2::Catch2WithMain Sts1CobcSw_FramSections Sts1CobcSw_Periphery Sts1CobcSw_Serial
Sts1CobcSw_Utility
)

add_program(Section Section.test.cpp)
target_link_libraries(
Sts1CobcSwTests_Section PRIVATE Catch2::Catch2WithMain Sts1CobcSw_FramSections
Expand Down Expand Up @@ -76,12 +71,27 @@ target_link_libraries(
)

get_property(
all_unit_test_targets DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY BUILDSYSTEM_TARGETS
all_catch2_unit_test_targets DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY BUILDSYSTEM_TARGETS
)
add_custom_target(AllUnitTests) # Must be defined after getting all unit test targets
add_dependencies(AllUnitTests ${all_unit_test_targets})

include(Catch)
foreach(target IN LISTS all_unit_test_targets)
foreach(target IN LISTS all_catch2_unit_test_targets)
catch_discover_tests(${target})
endforeach()

# --- Tests with Rodos ---

add_program(PersistentVariables PersistentVariables.test.cpp)
target_link_libraries(
Sts1CobcSwTests_PersistentVariables
PRIVATE rodos::rodos Sts1CobcSw_FramSections Sts1CobcSw_Periphery Sts1CobcSw_Serial
Sts1CobcSw_Utility
)
add_test(NAME PersistentVariables COMMAND Sts1CobcSwTests_PersistentVariables)

# --- All unit tests ---

get_property(
all_unit_test_targets DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY BUILDSYSTEM_TARGETS
)
add_custom_target(AllUnitTests) # Must be defined after getting all unit test targets
add_dependencies(AllUnitTests ${all_unit_test_targets})
190 changes: 117 additions & 73 deletions Tests/UnitTests/PersistentVariables.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#include <Sts1CobcSw/Serial/Byte.hpp>
#include <Sts1CobcSw/Utility/StringLiteral.hpp>

#include <catch2/catch_test_macros.hpp>
#include <strong_type/type.hpp>

#include <rodos_no_using_namespace.h>

#include <array>
#include <cstdint>
#include <optional>
#include <string>
#include <cstdlib>
#include <source_location>
#include <type_traits>


Expand All @@ -23,12 +24,6 @@ using sts1cobcsw::fram::Address;
using sts1cobcsw::fram::Size;


TEST_CASE("All static_asserts passed")
{
REQUIRE(true);
}


constexpr auto section0 = Section<Address(0), Size(100)>();
constexpr auto section1 = Section<Address(100), Size(100)>();
constexpr auto section2 = Section<Address(200), Size(100)>();
Expand All @@ -49,33 +44,57 @@ static_assert(std::is_same_v<decltype(pvs.Load<"activeFwImage">()), std::uint8_t
static_assert(std::is_same_v<decltype(pvs.Load<"somethingElse">()), std::int16_t>);


TEST_CASE("Majority vote")
// UnitTestWithRodos.hpp

std::uint32_t printfMask = 0;


auto RunUnitTest() -> void;
auto Require(bool condition, std::source_location location = std::source_location::current())
-> void;


class UnitTestThread : public RODOS::StaticThread<>
{
using sts1cobcsw::ComputeMajorityVote;
public:
UnitTestThread() : StaticThread("UnitTestThread")
{
}


auto voteResult = ComputeMajorityVote(173, 173, 173);
CHECK(voteResult.has_value());
CHECK(voteResult.value() == 173); // NOLINT(*unchecked-optional-access)
private:
void init() override
{
}

voteResult = ComputeMajorityVote(-2, 173, 173);
CHECK(voteResult.has_value());
CHECK(voteResult.value() == 173); // NOLINT(*unchecked-optional-access)
void run() override
{
printfMask = 1;
RunUnitTest();
RODOS::isShuttingDown = true;
std::exit(EXIT_SUCCESS); // NOLINT(concurrency-mt-unsafe)
}
} unitTestThread;

voteResult = ComputeMajorityVote(173, -2, 173);
CHECK(voteResult.has_value());
CHECK(voteResult.value() == 173); // NOLINT(*unchecked-optional-access)

voteResult = ComputeMajorityVote(173, 173, -2);
CHECK(voteResult.has_value());
CHECK(voteResult.value() == 173); // NOLINT(*unchecked-optional-access)
// UnitTestWithRodos.cpp

voteResult = ComputeMajorityVote(17, 173, -2);
CHECK(not voteResult.has_value());
auto Require(bool condition, std::source_location location) -> void
{
if(not condition)
{
RODOS::PRINTF("%s:%d: FAILED\n", location.file_name(), location.line());
RODOS::isShuttingDown = true;
std::exit(EXIT_FAILURE); // NOLINT(concurrency-mt-unsafe)
}
}


TEST_CASE("Load() and Store()")
// PersistentVariables.test.cpp

auto RunUnitTest() -> void
{
// Test case: Load() and Store()
using sts1cobcsw::fram::Address;
using sts1cobcsw::fram::framIsWorking;
using sts1cobcsw::fram::ram::memory;
Expand All @@ -89,110 +108,135 @@ TEST_CASE("Load() and Store()")

sts1cobcsw::fram::ram::SetAllDoFunctions();

SECTION("FRAM is working")
// Section: FRAM is working
{
memory.fill(0x00_b);
framIsWorking = true;

SECTION("You load what you store")
// Section: You load what you store
{
pvs.Store<"nResets">(0x12345678);
pvs.Store<"activeFwImage">(42);
pvs.Store<"somethingElse">(-2);
CHECK(pvs.Load<"nResets">() == 0x12345678);
CHECK(pvs.Load<"activeFwImage">() == 42);
CHECK(pvs.Load<"somethingElse">() == -2);
Require(pvs.Load<"nResets">() == 0x12345678);
Require(pvs.Load<"activeFwImage">() == 42);
Require(pvs.Load<"somethingElse">() == -2);
}

SECTION("Store() writes to memory")
// Section: Store() writes to memory
{
pvs.Store<"nResets">(0x12345678);
pvs.Store<"activeFwImage">(42);
pvs.Store<"somethingElse">(-2);
CHECK(memory[nResetsAddress0] == 0x78_b);
CHECK(memory[nResetsAddress0 + 1] == 0x56_b);
CHECK(memory[nResetsAddress0 + 2] == 0x34_b);
CHECK(memory[nResetsAddress0 + 3] == 0x12_b);
CHECK(memory[activeFwImageAddress0] == 42_b);
CHECK(memory[somethingElseAddress0] == 0xFE_b);
CHECK(memory[somethingElseAddress0 + 1] == 0xFF_b);
Require(memory[nResetsAddress0] == 0x78_b);
Require(memory[nResetsAddress0 + 1] == 0x56_b);
Require(memory[nResetsAddress0 + 2] == 0x34_b);
Require(memory[nResetsAddress0 + 3] == 0x12_b);
Require(memory[activeFwImageAddress0] == 42_b);
Require(memory[somethingElseAddress0] == 0xFE_b);
Require(memory[somethingElseAddress0 + 1] == 0xFF_b);
}

SECTION("Load() reads from memory")
// Section: Load() reads from memory
{
memory[activeFwImageAddress0] = 17_b;
memory[activeFwImageAddress1] = 17_b;
memory[activeFwImageAddress2] = 17_b;
CHECK(pvs.Load<"activeFwImage">() == 17);
Require(pvs.Load<"activeFwImage">() == 17);
}

SECTION("Load() returns majority and repairs memory")
// Section: Load() returns majority and repairs memory
{
memory[activeFwImageAddress0] = 42_b;
memory[activeFwImageAddress1] = 17_b;
memory[activeFwImageAddress2] = 17_b;
CHECK(pvs.Load<"activeFwImage">() == 17);
CHECK(memory[activeFwImageAddress0] == 17_b);
CHECK(memory[activeFwImageAddress1] == 17_b);
CHECK(memory[activeFwImageAddress2] == 17_b);
Require(pvs.Load<"activeFwImage">() == 17);
Require(memory[activeFwImageAddress0] == 17_b);
Require(memory[activeFwImageAddress1] == 17_b);
Require(memory[activeFwImageAddress2] == 17_b);

memory[activeFwImageAddress1] = 42_b;
CHECK(pvs.Load<"activeFwImage">() == 17);
CHECK(memory[activeFwImageAddress0] == 17_b);
CHECK(memory[activeFwImageAddress1] == 17_b);
CHECK(memory[activeFwImageAddress2] == 17_b);
Require(pvs.Load<"activeFwImage">() == 17);
Require(memory[activeFwImageAddress0] == 17_b);
Require(memory[activeFwImageAddress1] == 17_b);
Require(memory[activeFwImageAddress2] == 17_b);

memory[activeFwImageAddress2] = 42_b;
CHECK(pvs.Load<"activeFwImage">() == 17);
CHECK(memory[activeFwImageAddress0] == 17_b);
CHECK(memory[activeFwImageAddress1] == 17_b);
CHECK(memory[activeFwImageAddress2] == 17_b);
Require(pvs.Load<"activeFwImage">() == 17);
Require(memory[activeFwImageAddress0] == 17_b);
Require(memory[activeFwImageAddress1] == 17_b);
Require(memory[activeFwImageAddress2] == 17_b);

memory[activeFwImageAddress0] = 42_b;
memory[activeFwImageAddress2] = 42_b;
CHECK(pvs.Load<"activeFwImage">() == 42);
CHECK(memory[activeFwImageAddress0] == 42_b);
CHECK(memory[activeFwImageAddress1] == 42_b);
CHECK(memory[activeFwImageAddress2] == 42_b);
Require(pvs.Load<"activeFwImage">() == 42);
Require(memory[activeFwImageAddress0] == 42_b);
Require(memory[activeFwImageAddress1] == 42_b);
Require(memory[activeFwImageAddress2] == 42_b);
}
}

SECTION("FRAM is not working")
// Section: FRAM is not working
{
memory.fill(0x00_b);
framIsWorking = false;

SECTION("You load what you store")
// Section: You load what you store
{
pvs.Store<"nResets">(0xABCDABCD);
pvs.Store<"activeFwImage">(111);
pvs.Store<"somethingElse">(-55);
CHECK(pvs.Load<"nResets">() == 0xABCDABCD);
CHECK(pvs.Load<"activeFwImage">() == 111);
CHECK(pvs.Load<"somethingElse">() == -55);
Require(pvs.Load<"nResets">() == 0xABCDABCD);
Require(pvs.Load<"activeFwImage">() == 111);
Require(pvs.Load<"somethingElse">() == -55);
}

SECTION("Store() does not write to memory")
// Section: Store() does not write to memory
{
pvs.Store<"nResets">(0xABCDABCD);
pvs.Store<"activeFwImage">(111);
pvs.Store<"somethingElse">(-55);
CHECK(memory[nResetsAddress0] == 0x00_b);
CHECK(memory[nResetsAddress0 + 1] == 0x00_b);
CHECK(memory[nResetsAddress0 + 2] == 0x00_b);
CHECK(memory[nResetsAddress0 + 3] == 0x00_b);
CHECK(memory[activeFwImageAddress0] == 00_b);
CHECK(memory[somethingElseAddress0] == 0x00_b);
CHECK(memory[somethingElseAddress0 + 1] == 0x00_b);
Require(memory[nResetsAddress0] == 0x00_b);
Require(memory[nResetsAddress0 + 1] == 0x00_b);
Require(memory[nResetsAddress0 + 2] == 0x00_b);
Require(memory[nResetsAddress0 + 3] == 0x00_b);
Require(memory[activeFwImageAddress0] == 00_b);
Require(memory[somethingElseAddress0] == 0x00_b);
Require(memory[somethingElseAddress0 + 1] == 0x00_b);
}

SECTION("Load() does not read from memory")
// Section: Load() does not read from memory
{
pvs.Store<"activeFwImage">(0);
memory[activeFwImageAddress0] = 17_b;
memory[activeFwImageAddress1] = 17_b;
memory[activeFwImageAddress2] = 17_b;
CHECK(pvs.Load<"activeFwImage">() == 0);
Require(pvs.Load<"activeFwImage">() == 0);
}
}
}


// TEST_CASE("Majority vote")
// {
// using sts1cobcsw::ComputeMajorityVote;

// auto voteResult = ComputeMajorityVote(173, 173, 173);
// CHECK(voteResult.has_value());
// CHECK(voteResult.value() == 173); // NOLINT(*unchecked-optional-access)

// voteResult = ComputeMajorityVote(-2, 173, 173);
// CHECK(voteResult.has_value());
// CHECK(voteResult.value() == 173); // NOLINT(*unchecked-optional-access)

// voteResult = ComputeMajorityVote(173, -2, 173);
// CHECK(voteResult.has_value());
// CHECK(voteResult.value() == 173); // NOLINT(*unchecked-optional-access)

// voteResult = ComputeMajorityVote(173, 173, -2);
// CHECK(voteResult.has_value());
// CHECK(voteResult.value() == 173); // NOLINT(*unchecked-optional-access)

// voteResult = ComputeMajorityVote(17, 173, -2);
// CHECK(not voteResult.has_value());
// }

0 comments on commit 666251c

Please sign in to comment.