-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
ComputeMajorityVote()
to a separate file
- Loading branch information
Showing
6 changed files
with
65 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters