math: port unit tests to XCode

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2023-09-11 17:34:40 +02:00
parent b326f819ab
commit 1e993c8c65
4 changed files with 76 additions and 4 deletions

View File

@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
F48105AD2AAF5FD4004DE682 /* math.hpp in Sources */ = {isa = PBXBuildFile; fileRef = F48105AC2AAF5FD4004DE682 /* math.hpp */; };
F4B7FBE02AAF49BC00C6BE9F /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4B7FBDF2AAF49BC00C6BE9F /* main.cpp */; };
/* End PBXBuildFile section */
@ -23,6 +24,8 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
F48105AC2AAF5FD4004DE682 /* math.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = math.hpp; sourceTree = "<group>"; };
F48105AE2AAF64C7004DE682 /* common.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = common.hpp; sourceTree = "<group>"; };
F4B7FBDC2AAF49BC00C6BE9F /* UnitTests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = UnitTests; sourceTree = BUILT_PRODUCTS_DIR; };
F4B7FBDF2AAF49BC00C6BE9F /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -58,6 +61,8 @@
F4B7FBDE2AAF49BC00C6BE9F /* UnitTests */ = {
isa = PBXGroup;
children = (
F48105AE2AAF64C7004DE682 /* common.hpp */,
F48105AC2AAF5FD4004DE682 /* math.hpp */,
F4B7FBDF2AAF49BC00C6BE9F /* main.cpp */,
);
path = UnitTests;
@ -121,6 +126,7 @@
buildActionMask = 2147483647;
files = (
F4B7FBE02AAF49BC00C6BE9F /* main.cpp in Sources */,
F48105AD2AAF5FD4004DE682 /* math.hpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

18
mac/UnitTests/common.hpp Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <stdexcept>
template <class T>
void are_equal(const T& a, const T& b)
{
if (!(a == b))
throw std::runtime_error("values are not equal");
}
template <class E, typename F>
void expect_exception(F functor)
{
try { functor(); }
catch (const E&) { return; }
throw std::runtime_error("exception expected");
}

View File

@ -1,7 +1,16 @@
#include "math.hpp"
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
int main(int argc, const char * argv[])
{
try {
UnitTests::math::mul();
UnitTests::math::add();
std::cout << "PASS\n";
return 0;
}
catch (const std::exception& ex) {
std::cerr << ex.what() << " FAIL\n";
return 1;
}
}

39
mac/UnitTests/math.hpp Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#include "common.hpp"
#include <stdex/math.hpp>
using namespace std;
namespace UnitTests
{
class math
{
public:
static void mul()
{
are_equal<size_t>(10, stdex::mul(2, 5));
are_equal<size_t>(10, stdex::mul(5, 2));
are_equal<size_t>(0, stdex::mul(0, 10));
are_equal<size_t>(0, stdex::mul(10, 0));
are_equal<size_t>(0, stdex::mul(SIZE_MAX, 0));
are_equal<size_t>(0, stdex::mul(0, SIZE_MAX));
are_equal<size_t>(SIZE_MAX, stdex::mul(SIZE_MAX, 1));
are_equal<size_t>(SIZE_MAX, stdex::mul(1, SIZE_MAX));
expect_exception<std::invalid_argument>([] { stdex::mul(SIZE_MAX, 2); });
expect_exception<std::invalid_argument>([] { stdex::mul(2, SIZE_MAX); });
}
static void add()
{
are_equal<size_t>(7, stdex::add(2, 5));
are_equal<size_t>(7, stdex::add(5, 2));
are_equal<size_t>(10, stdex::add(0, 10));
are_equal<size_t>(10, stdex::add(10, 0));
are_equal<size_t>(SIZE_MAX, stdex::add(SIZE_MAX, 0));
are_equal<size_t>(SIZE_MAX, stdex::add(0, SIZE_MAX));
expect_exception<std::invalid_argument>([] { stdex::add(SIZE_MAX, 1); });
expect_exception<std::invalid_argument>([] { stdex::add(1, SIZE_MAX); });
}
};
}