From 204a6a27dfb407b94e017e69a267927972011c2c Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Mon, 17 Apr 2023 19:35:46 -0700 Subject: [PATCH] Implements ranks and tests. --- sbf-cpp/Ranks.cpp | 37 ++++++++++ sbf-cpp/Ranks.h | 23 ++++-- sbf-cpp/Ranks_test.cpp | 157 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+), 6 deletions(-) diff --git a/sbf-cpp/Ranks.cpp b/sbf-cpp/Ranks.cpp index e69de29..0c752cd 100644 --- a/sbf-cpp/Ranks.cpp +++ b/sbf-cpp/Ranks.cpp @@ -0,0 +1,37 @@ +#include "Ranks.h" +#include +#include +#include + +namespace SBF { +using std::string; +using std::vector; + +std::ostream& operator<<(std::ostream& os, const RankType& rank) { + os << "Rank: {id: " << rank.id + << ", label: \"" << rank.label << "\"}"; + return os; +} + +bool operator==(const RankType& left, const RankType& right) { + return left.id == right.id && left.label == right.label; +} + +bool operator!=(const RankType& left, const RankType& right) { + return !(left == right); +} + +RankType GetRank(int rankId) { + if (rankId > 0 && rankId <= kRanksCount) { + return kRanks[rankId]; + } + return kRankUnknown; +} + +void FillRanks(std::vector& ranks) { + ranks.clear(); + for (int id = 1; id <= kRanksCount; id++) { + ranks.push_back(GetRank(id)); + } +} +} // End namespace SBF diff --git a/sbf-cpp/Ranks.h b/sbf-cpp/Ranks.h index ede6b21..cfd118a 100644 --- a/sbf-cpp/Ranks.h +++ b/sbf-cpp/Ranks.h @@ -22,18 +22,29 @@ namespace SBF { const int kRankTertiaryId = 3; const std::string kRankTertiaryLabel = "Tertiary"; const int kRanksCount = 3; + struct RankType { int id; std::string label; }; + + const RankType kRankPrimary = {kRankPrimaryId, kRankPrimaryLabel}; + const RankType kRankSecondary = {kRankSecondaryId, kRankSecondaryLabel}; + const RankType kRankTertiary = {kRankTertiaryId, kRankTertiaryLabel}; + const RankType kRankUnknown = {0, ""}; + const RankType kRanks[] { - {0, ""}, - {kRankPrimaryId, kRankPrimaryLabel}, - {kRankSecondaryId, kRankSecondaryLabel}, - {kRankTertiaryId, kRankTertiaryLabel}, + kRankUnknown, + kRankPrimary, + kRankSecondary, + kRankTertiary, }; - const RankType& GetRank(int rankId); - void FillRanks(std::vector ranks); + + std::ostream& operator<<(std::ostream& os, const RankType& abilityGroup); + bool operator==(const RankType& left, const RankType& right); + bool operator!=(const RankType& left, const RankType& right); + RankType GetRank(int rankId); + void FillRanks(std::vector& ranks); } // End namespace SBF /** @}*/ #endif // !defined RANKS_H__ diff --git a/sbf-cpp/Ranks_test.cpp b/sbf-cpp/Ranks_test.cpp index 2bd9ccf..3230054 100644 --- a/sbf-cpp/Ranks_test.cpp +++ b/sbf-cpp/Ranks_test.cpp @@ -1,10 +1,167 @@ #include "Ranks.h" #include "test.h" +#include +#include +#include +#include + using namespace SBF; using namespace Test; +using namespace std; + +namespace Test::Ranks { +TestResults test_RankType_operator_extract(); +TestResults test_RankType_operator_equal_to(); +TestResults test_RankType_operator_not_equal_to(); +TestResults test_GetRank(); +TestResults test_FillRanks(); +} // End namespace Test::Ranks +using namespace Test::Ranks; TestResults main_test_Ranks(int argc, char** argv) { TestResults results; + results += test_RankType_operator_extract(); + results += test_RankType_operator_equal_to(); + results += test_RankType_operator_not_equal_to(); + results += test_GetRank(); + results += test_FillRanks(); + return results; } + +namespace Test::Ranks { +TestResults test_GetRank() { + return execute_suite(make_test_suite( + "SBF::GetRank", + GetRank, + vector>({ + make_test( + "should get {0, \"\"} for invalid rank id 0", + RankType({0, ""}), + make_tuple(0)), + make_test( + "should get {1, \"Primary\"} for rank id 1", + RankType({1, "Primary"}), + make_tuple(1)), + make_test( + "should get {2, \"Secondary\"} for rank id 2", + RankType({2, "Secondary"}), + make_tuple(2)), + make_test( + "should get {3, \"Tertiary\"} for rank id 3", + RankType({3, "Tertiary"}), + make_tuple(3)), + make_test( + "should get {0, \"\"} for invalid rank id 6", + RankType({0, ""}), + make_tuple(6)), + }) + )); +} + +TestResults test_FillRanks() { + auto fnToTest = []()->string { + ostringstream error_message; + vector expected = { + kRankPrimary, + kRankSecondary, + kRankTertiary, + }; + vector actual = {{-5, "This should be removed."}}; + FillRanks(actual); + compare(error_message, expected, actual); + string error = error_message.str(); + if (error.size() > 0) { + return error; + } + return "no errors"; + }; + return execute_suite(make_test_suite( + "SBF::FillRanks", + fnToTest, + vector>({ + make_test( + "should fill ranks", + "no errors", + make_tuple()), + }) + )); +} + +TestResults test_RankType_operator_extract() { + auto fnToTest = [](const RankType& rank)->string { + ostringstream os; + os << rank; + return os.str(); + }; + return execute_suite(make_test_suite( + "SBF::RankType::operator<<", + fnToTest, + vector>({ + make_test( + "should print an invalid rank", + "Rank: {id: 0, label: \"\"}", + make_tuple((RankType){0, ""})), + make_test( + "should print primary", + "Rank: {id: 1, label: \"Primary\"}", + make_tuple(kRankPrimary)), + make_test( + "should print secondary", + "Rank: {id: 2, label: \"Secondary\"}", + make_tuple(kRankSecondary)), + make_test( + "should print tertiary", + "Rank: {id: 3, label: \"Tertiary\"}", + make_tuple(kRankTertiary)), + }) + )); +} + +TestResults test_RankType_operator_equal_to() { + return execute_suite(make_test_suite( + "SBF::RankType::operator==", + [](const RankType& left, const RankType& right) { + return left == right; + }, + vector>({ + make_test( + "should return true when comparing an rank to itself", + true, + make_tuple(kRankPrimary, kRankPrimary)), + make_test( + "should return true when comparing two different instances created with the same values", + true, + make_tuple(kRankSecondary, RankType({kRankSecondaryId, kRankSecondaryLabel}))), + make_test( + "should return false when comparing two different rank gropus", + false, + make_tuple(kRankPrimary, kRankTertiary)), + }) + )); +} + +TestResults test_RankType_operator_not_equal_to() { + return execute_suite(make_test_suite( + "SBF::RankType::operator!=", + [](const RankType& left, const RankType& right) { + return left != right; + }, + vector>({ + make_test( + "should return false when comparing an rank to itself", + false, + make_tuple(kRankPrimary, kRankPrimary)), + make_test( + "should return false when comparing two different instances created with the same values", + false, + make_tuple(kRankSecondary, RankType({kRankSecondaryId, kRankSecondaryLabel}))), + make_test( + "should return true when comparing two different rank gropus", + true, + make_tuple(kRankSecondary, kRankTertiary)), + }) + )); +} +}