Tests for Ord method
This commit is contained in:
parent
f776f48e21
commit
0886aa0a68
@ -9,14 +9,14 @@ public:
|
||||
template <typename T_Type, class T_Container>
|
||||
static int Ord(const T_Type& item, const T_Container& set);
|
||||
private:
|
||||
// No instanciation! >:(
|
||||
// No instantiation! >:(
|
||||
GeneralUtility();
|
||||
};
|
||||
|
||||
template<typename T_Type, class T_Container>
|
||||
int GeneralUtility::Ord(const T_Type &item, const T_Container& set) {
|
||||
int GeneralUtility::Ord(const T_Type& item, const T_Container& set) {
|
||||
const auto result =
|
||||
std::find_if(set.begin(), set.end(), [item](const char c) -> bool {
|
||||
std::find_if(set.begin(), set.end(), [item](const T_Type& c) -> bool {
|
||||
return c == item;
|
||||
});
|
||||
|
||||
|
35
Test/Ord.cpp
35
Test/Ord.cpp
@ -1,6 +1,7 @@
|
||||
#include <GeneralUtility.h>
|
||||
#include "Catch2.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Tests that the Ord method works with characters in a string
|
||||
TEST_CASE(__FILE__"/WorksWithCharsInString", "[Ord]")
|
||||
@ -24,3 +25,37 @@ TEST_CASE(__FILE__"/WorksWithCharsInString", "[Ord]")
|
||||
REQUIRE(GeneralUtility::Ord('e', set) == 14);
|
||||
REQUIRE(GeneralUtility::Ord('f', set) == 15);
|
||||
}
|
||||
|
||||
// Tests that, if an object is not found, -1 is returned
|
||||
TEST_CASE(__FILE__"/ReturnsNeg1IfNotFound", "[Ord]")
|
||||
{
|
||||
const std::string set = "0123456789abcdef";
|
||||
|
||||
REQUIRE(GeneralUtility::Ord('z', set) == -1);
|
||||
}
|
||||
|
||||
// Tests that Ord works with vectors <string>
|
||||
TEST_CASE(__FILE__"/WorksWithVector_String", "[Ord]")
|
||||
{
|
||||
const std::vector<std::string> vec = { "Apple", "Banana", "Tomato", "Olives" };
|
||||
|
||||
REQUIRE(GeneralUtility::Ord(std::string("Apple"), vec) == 0);
|
||||
REQUIRE(GeneralUtility::Ord(std::string("Banana"), vec) == 1);
|
||||
REQUIRE(GeneralUtility::Ord(std::string("Tomato"), vec) == 2);
|
||||
REQUIRE(GeneralUtility::Ord(std::string("Olives"), vec) == 3);
|
||||
|
||||
INFO("Now testing that unknown is -1");
|
||||
REQUIRE(GeneralUtility::Ord(std::string("Pepper"), vec) == -1);
|
||||
}
|
||||
|
||||
// Tests that Ord works with vectors <int>
|
||||
TEST_CASE(__FILE__"/WorksWithVector_Int", "[Ord]")
|
||||
{
|
||||
const std::vector<int> vec = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
for (std::size_t i = 0 ; i < 10; i++)
|
||||
REQUIRE(GeneralUtility::Ord((int)i, vec) == i);
|
||||
|
||||
INFO("Now testing that unknown is -1");
|
||||
REQUIRE(GeneralUtility::Ord((int)99, vec) == -1);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user