From 1e993c8c65d19d1fddd546931e793452d47ff7f8 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 11 Sep 2023 17:34:40 +0200 Subject: [PATCH] math: port unit tests to XCode Signed-off-by: Simon Rozman --- mac/UnitTests.xcodeproj/project.pbxproj | 6 ++++ mac/UnitTests/common.hpp | 18 ++++++++++++ mac/UnitTests/main.cpp | 17 ++++++++--- mac/UnitTests/math.hpp | 39 +++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 mac/UnitTests/common.hpp create mode 100644 mac/UnitTests/math.hpp diff --git a/mac/UnitTests.xcodeproj/project.pbxproj b/mac/UnitTests.xcodeproj/project.pbxproj index dbe98c2dd..1fa59934e 100644 --- a/mac/UnitTests.xcodeproj/project.pbxproj +++ b/mac/UnitTests.xcodeproj/project.pbxproj @@ -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 = ""; }; + F48105AE2AAF64C7004DE682 /* common.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = common.hpp; sourceTree = ""; }; 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 = ""; }; /* 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; }; diff --git a/mac/UnitTests/common.hpp b/mac/UnitTests/common.hpp new file mode 100644 index 000000000..12a0b9d08 --- /dev/null +++ b/mac/UnitTests/common.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +template +void are_equal(const T& a, const T& b) +{ + if (!(a == b)) + throw std::runtime_error("values are not equal"); +} + +template +void expect_exception(F functor) +{ + try { functor(); } + catch (const E&) { return; } + throw std::runtime_error("exception expected"); +} diff --git a/mac/UnitTests/main.cpp b/mac/UnitTests/main.cpp index 1bad0d9b3..63a8a242a 100644 --- a/mac/UnitTests/main.cpp +++ b/mac/UnitTests/main.cpp @@ -1,7 +1,16 @@ +#include "math.hpp" #include -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; + } } diff --git a/mac/UnitTests/math.hpp b/mac/UnitTests/math.hpp new file mode 100644 index 000000000..06c32bf4a --- /dev/null +++ b/mac/UnitTests/math.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include "common.hpp" +#include + +using namespace std; + +namespace UnitTests +{ + class math + { + public: + static void mul() + { + are_equal(10, stdex::mul(2, 5)); + are_equal(10, stdex::mul(5, 2)); + are_equal(0, stdex::mul(0, 10)); + are_equal(0, stdex::mul(10, 0)); + are_equal(0, stdex::mul(SIZE_MAX, 0)); + are_equal(0, stdex::mul(0, SIZE_MAX)); + are_equal(SIZE_MAX, stdex::mul(SIZE_MAX, 1)); + are_equal(SIZE_MAX, stdex::mul(1, SIZE_MAX)); + expect_exception([] { stdex::mul(SIZE_MAX, 2); }); + expect_exception([] { stdex::mul(2, SIZE_MAX); }); + } + + static void add() + { + are_equal(7, stdex::add(2, 5)); + are_equal(7, stdex::add(5, 2)); + are_equal(10, stdex::add(0, 10)); + are_equal(10, stdex::add(10, 0)); + are_equal(SIZE_MAX, stdex::add(SIZE_MAX, 0)); + are_equal(SIZE_MAX, stdex::add(0, SIZE_MAX)); + expect_exception([] { stdex::add(SIZE_MAX, 1); }); + expect_exception([] { stdex::add(1, SIZE_MAX); }); + } + }; +}