math: add
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
3516c546ca
commit
501183ca3e
@ -116,6 +116,7 @@
|
|||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="ios.cpp" />
|
<ClCompile Include="ios.cpp" />
|
||||||
|
<ClCompile Include="math.cpp" />
|
||||||
<ClCompile Include="parser.cpp" />
|
<ClCompile Include="parser.cpp" />
|
||||||
<ClCompile Include="pch.cpp">
|
<ClCompile Include="pch.cpp">
|
||||||
<PrecompiledHeader>Create</PrecompiledHeader>
|
<PrecompiledHeader>Create</PrecompiledHeader>
|
||||||
|
@ -27,6 +27,9 @@
|
|||||||
<ClCompile Include="ios.cpp">
|
<ClCompile Include="ios.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="math.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="pch.h">
|
<ClInclude Include="pch.h">
|
||||||
|
42
UnitTests/math.cpp
Normal file
42
UnitTests/math.cpp
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
SPDX-License-Identifier: MIT
|
||||||
|
Copyright © 2023 Amebis
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "pch.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
|
|
||||||
|
namespace UnitTests
|
||||||
|
{
|
||||||
|
TEST_CLASS(math)
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TEST_METHOD(mul)
|
||||||
|
{
|
||||||
|
Assert::AreEqual<size_t>(10, stdex::mul(2, 5));
|
||||||
|
Assert::AreEqual<size_t>(10, stdex::mul(5, 2));
|
||||||
|
Assert::AreEqual<size_t>(0, stdex::mul(0, 10));
|
||||||
|
Assert::AreEqual<size_t>(0, stdex::mul(10, 0));
|
||||||
|
Assert::AreEqual<size_t>(0, stdex::mul(SIZE_MAX, 0));
|
||||||
|
Assert::AreEqual<size_t>(0, stdex::mul(0, SIZE_MAX));
|
||||||
|
Assert::AreEqual<size_t>(SIZE_MAX, stdex::mul(SIZE_MAX, 1));
|
||||||
|
Assert::AreEqual<size_t>(SIZE_MAX, stdex::mul(1, SIZE_MAX));
|
||||||
|
Assert::ExpectException<std::invalid_argument>([] { stdex::mul(SIZE_MAX, 2); });
|
||||||
|
Assert::ExpectException<std::invalid_argument>([] { stdex::mul(2, SIZE_MAX); });
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(add)
|
||||||
|
{
|
||||||
|
Assert::AreEqual<size_t>(7, stdex::add(2, 5));
|
||||||
|
Assert::AreEqual<size_t>(7, stdex::add(5, 2));
|
||||||
|
Assert::AreEqual<size_t>(10, stdex::add(0, 10));
|
||||||
|
Assert::AreEqual<size_t>(10, stdex::add(10, 0));
|
||||||
|
Assert::AreEqual<size_t>(SIZE_MAX, stdex::add(SIZE_MAX, 0));
|
||||||
|
Assert::AreEqual<size_t>(SIZE_MAX, stdex::add(0, SIZE_MAX));
|
||||||
|
Assert::ExpectException<std::invalid_argument>([] { stdex::add(SIZE_MAX, 1); });
|
||||||
|
Assert::ExpectException<std::invalid_argument>([] { stdex::add(1, SIZE_MAX); });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -16,6 +16,7 @@
|
|||||||
#include <stdex/interval.hpp>
|
#include <stdex/interval.hpp>
|
||||||
#include <stdex/ios.hpp>
|
#include <stdex/ios.hpp>
|
||||||
#include <stdex/mapping.hpp>
|
#include <stdex/mapping.hpp>
|
||||||
|
#include <stdex/math.hpp>
|
||||||
#include <stdex/parser.hpp>
|
#include <stdex/parser.hpp>
|
||||||
#include <stdex/progress.hpp>
|
#include <stdex/progress.hpp>
|
||||||
#include <stdex/sal.hpp>
|
#include <stdex/sal.hpp>
|
||||||
|
49
include/stdex/math.hpp
Normal file
49
include/stdex/math.hpp
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
SPDX-License-Identifier: MIT
|
||||||
|
Copyright © 2023 Amebis
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "sal.hpp"
|
||||||
|
#include "system.hpp"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace stdex
|
||||||
|
{
|
||||||
|
inline size_t mul(size_t a, size_t b)
|
||||||
|
{
|
||||||
|
#if _MSC_VER >= 1300
|
||||||
|
SIZE_T result;
|
||||||
|
if (SUCCEEDED(SIZETMult(a, b, &result)))
|
||||||
|
return result;
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
if (a == 0)
|
||||||
|
return 0;
|
||||||
|
if (b <= SIZE_MAX / a)
|
||||||
|
return a * b;
|
||||||
|
#else
|
||||||
|
size_t result;
|
||||||
|
if (!__builtin_mul_overflow(a, b, &result))
|
||||||
|
return result;
|
||||||
|
#endif
|
||||||
|
throw std::invalid_argument("multiply overflow");
|
||||||
|
}
|
||||||
|
|
||||||
|
inline size_t add(size_t a, size_t b)
|
||||||
|
{
|
||||||
|
#if _MSC_VER >= 1300
|
||||||
|
SIZE_T result;
|
||||||
|
if (SUCCEEDED(SIZETAdd(a, b, &result)))
|
||||||
|
return result;
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
if (a <= SIZE_MAX - b)
|
||||||
|
return a + b;
|
||||||
|
#else
|
||||||
|
size_t result;
|
||||||
|
if (!__builtin_add_overflow(a, b, &result))
|
||||||
|
return result;
|
||||||
|
#endif
|
||||||
|
throw std::invalid_argument("add overflow");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user