Imported Ord method, and added a simple test

This commit is contained in:
Leonetienne 2022-02-27 14:05:51 +01:00
parent 2958c2c72b
commit f776f48e21
5 changed files with 18029 additions and 1 deletions

View File

@ -1,12 +1,30 @@
#ifndef GENERALUTILITY_GENERALUTILITY_H
#define GENERALUTILITY_GENERALUTILITY_H
#include <algorithm>
#include <utility>
class GeneralUtility {
public:
template <typename T_Type, class T_Container>
static int Ord(const T_Type& item, const T_Container& set);
private:
// No instanciation! >:(
GeneralUtility();
};
template<typename T_Type, class T_Container>
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 {
return c == item;
});
// No item found
if (result == set.end())
return -1;
else
return result - set.begin();
}
#endif //GENERALUTILITY_GENERALUTILITY_H

17
Test/CMakeLists.txt Normal file
View File

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.16)
project(Test)
set(CMAKE_CXX_STANDARD 17)
include_directories(../Src)
link_directories(../Src/cmake-build-debug)
add_executable(Test
Catch2.h
main.cpp
Ord.cpp
)
target_link_libraries(Test GeneralUtility)

17965
Test/Catch2.h Normal file

File diff suppressed because it is too large Load Diff

26
Test/Ord.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <GeneralUtility.h>
#include "Catch2.h"
#include <string>
// Tests that the Ord method works with characters in a string
TEST_CASE(__FILE__"/WorksWithCharsInString", "[Ord]")
{
const std::string set = "0123456789abcdef";
REQUIRE(GeneralUtility::Ord('0', set) == 0);
REQUIRE(GeneralUtility::Ord('1', set) == 1);
REQUIRE(GeneralUtility::Ord('2', set) == 2);
REQUIRE(GeneralUtility::Ord('3', set) == 3);
REQUIRE(GeneralUtility::Ord('4', set) == 4);
REQUIRE(GeneralUtility::Ord('5', set) == 5);
REQUIRE(GeneralUtility::Ord('6', set) == 6);
REQUIRE(GeneralUtility::Ord('7', set) == 7);
REQUIRE(GeneralUtility::Ord('8', set) == 8);
REQUIRE(GeneralUtility::Ord('9', set) == 9);
REQUIRE(GeneralUtility::Ord('a', set) == 10);
REQUIRE(GeneralUtility::Ord('b', set) == 11);
REQUIRE(GeneralUtility::Ord('c', set) == 12);
REQUIRE(GeneralUtility::Ord('d', set) == 13);
REQUIRE(GeneralUtility::Ord('e', set) == 14);
REQUIRE(GeneralUtility::Ord('f', set) == 15);
}

2
Test/main.cpp Normal file
View File

@ -0,0 +1,2 @@
#define CATCH_CONFIG_MAIN
#include "Catch2.h"