Skip to content

Commit

Permalink
Move ComputeMajorityVote() to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickKa committed Aug 25, 2024
1 parent efbe584 commit 3c573d7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 49 deletions.
6 changes: 0 additions & 6 deletions Sts1CobcSw/FramSections/PersistentVariables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ class PersistentVariables

static RODOS::Semaphore semaphore;
};


// TODO: This should probably be defined somewhere else
template<typename T>
[[nodiscard]] static auto ComputeMajorityVote(T const & value0, T const & value1, T const & value2)
-> std::optional<T>;
}


Expand Down
16 changes: 1 addition & 15 deletions Sts1CobcSw/FramSections/PersistentVariables.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


#include <Sts1CobcSw/FramSections/PersistentVariables.hpp>
#include <Sts1CobcSw/Utility/ErrorDetectionAndCorrection.hpp>
#include <Sts1CobcSw/Utility/Span.hpp>


Expand Down Expand Up @@ -191,19 +192,4 @@ std::tuple<typename PersistentVariableInfos::ValueType...> PersistentVariables<
parentSection2,
PersistentVariableInfos...>::cache2 =
std::tuple(typename PersistentVariableInfos::ValueType{}...);


template<typename T>
auto ComputeMajorityVote(T const & value0, T const & value1, T const & value2) -> std::optional<T>
{
if(value0 == value1 or value0 == value2)
{
return value0;
}
if(value1 == value2)
{
return value1;
}
return std::nullopt;
}
}
25 changes: 25 additions & 0 deletions Sts1CobcSw/Utility/ErrorDetectionAndCorrection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once


#include <optional>


namespace sts1cobcsw
{
// I won't make a separate .ipp file just for this simple function
template<typename T>
[[nodiscard]] constexpr auto ComputeMajorityVote(T const & value0,
T const & value1,
T const & value2) -> std::optional<T>
{
if(value0 == value1 or value0 == value2)
{
return value0;
}
if(value1 == value2)
{
return value1;
}
return std::nullopt;
}
}
6 changes: 6 additions & 0 deletions Tests/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# --- 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)

add_program(ErrorDetectionAndCorrection ErrorDetectionAndCorrection.test.cpp)
target_link_libraries(
Sts1CobcSwTests_ErrorDetectionAndCorrection PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Utility
)

add_program(FlatArray FlatArray.test.cpp)
target_link_libraries(Sts1CobcSwTests_FlatArray PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Utility)

Expand Down
30 changes: 30 additions & 0 deletions Tests/UnitTests/ErrorDetectionAndCorrection.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <Sts1CobcSw/Utility/ErrorDetectionAndCorrection.hpp>

#include <catch2/catch_test_macros.hpp>

#include <optional>


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());
}
31 changes: 3 additions & 28 deletions Tests/UnitTests/PersistentVariables.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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>);


// UnitTestWithRodos.hpp
// TODO: Move this to UnitTestWithRodos.hpp

std::uint32_t printfMask = 0;

Expand Down Expand Up @@ -77,7 +77,7 @@ class UnitTestThread : public RODOS::StaticThread<>
} unitTestThread;


// UnitTestWithRodos.cpp
// TODO: Move this to UnitTestWithRodos.cpp

auto Require(bool condition, std::source_location location) -> void
{
Expand All @@ -90,7 +90,7 @@ auto Require(bool condition, std::source_location location) -> void
}


// PersistentVariables.test.cpp
// TODO: This should stay here in PersistentVariables.test.cpp

auto RunUnitTest() -> void
{
Expand Down Expand Up @@ -215,28 +215,3 @@ auto RunUnitTest() -> void
}
}
}


// 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 3c573d7

Please sign in to comment.