-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
conanfile.txt - is partial CMakeLists.txt - is partial
- Loading branch information
1 parent
41b19a7
commit 62f129e
Showing
9 changed files
with
190 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
cmake_minimum_required(VERSION 3.15) | ||
project(Platform.Ranges) | ||
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) | ||
conan_basic_setup(TARGETS) | ||
|
||
add_library(${PROJECT_NAME}.Library INTERFACE) | ||
target_include_directories(${PROJECT_NAME}.Library INTERFACE ${PROJECT_NAME}) | ||
target_link_libraries(${PROJECT_NAME}.Library INTERFACE CONAN_PKG::platform.delegates) | ||
target_link_libraries(${PROJECT_NAME}.Library INTERFACE CONAN_PKG::platform.converters) | ||
target_link_libraries(${PROJECT_NAME}.Library INTERFACE CONAN_PKG::platform.exceptions) | ||
|
||
add_executable(${PROJECT_NAME}.Tests ${PROJECT_NAME}.Tests/AllTests.cpp) | ||
target_link_libraries(${PROJECT_NAME}.Tests PRIVATE ${PROJECT_NAME}.Library) | ||
target_link_libraries(${PROJECT_NAME}.Tests PRIVATE CONAN_PKG::gtest) | ||
set_target_properties(${PROJECT_NAME}.Tests PROPERTIES CXX_STANDARD 20) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,12 @@ | ||
#include "pch.h" | ||
#include "pch.h" // )) | ||
|
||
#include <Platform.Ranges.h> | ||
|
||
#include <locale> | ||
#include <codecvt> | ||
#include <string> | ||
|
||
#include "CppUnitTest.h" | ||
#include <gtest/gtest.h> | ||
|
||
namespace Microsoft::VisualStudio::CppUnitTestFramework | ||
{ | ||
template<> static std::wstring ToString<Platform::Ranges::Range<std::int32_t>>(const struct Platform::Ranges::Range<std::int32_t> &t) | ||
{ | ||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; | ||
auto string = Platform::Converters::To<std::string>(t); | ||
return converter.from_bytes(string); | ||
} | ||
} | ||
|
||
using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||
#include "EnsureExtensionsTests.cpp" | ||
#include "RangeTests.cpp" | ||
#include "RangeTests.cpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
namespace Platform::Ranges::Tests | ||
{ | ||
TEST_CLASS(EnsureExtensionsTests) | ||
TEST(EnsureExtensionsTests, MaximumArgumentIsGreaterOrEqualToMinimumExceptionTest) | ||
{ | ||
public: TEST_METHOD(MaximumArgumentIsGreaterOrEqualToMinimumExceptionTest) { Assert::ExpectException<std::invalid_argument>([&]()-> auto { return Platform::Ranges::EnsureExtensions::MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::Ensure::Always, 2, 1); }); } | ||
|
||
public: TEST_METHOD(ArgumentInRangeExceptionTest) { Assert::ExpectException<std::invalid_argument>([&]()-> auto { return Platform::Ranges::EnsureExtensions::ArgumentInRange(Platform::Exceptions::Ensure::Always, 5, {6, 7}); }); } | ||
EXPECT_THROW(Always::MaximumArgumentIsGreaterOrEqualToMinimum(2, 1), std::logic_error); | ||
}; | ||
} | ||
|
||
TEST(EnsureExtensionsTests, ArgumentInRangeExceptionTest) | ||
{ | ||
EXPECT_THROW(Always::ArgumentInRange(5, {6, 7}), std::logic_error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,46 @@ | ||
namespace Platform::Ranges::Tests | ||
{ | ||
TEST_CLASS(RangeTests) | ||
TEST(RangesTests, ConstructorsTest) | ||
{ | ||
public: TEST_METHOD(ConstructorsTest) | ||
{ | ||
auto range1 = Range<std::int32_t>(1, 3); | ||
Assert::AreEqual(1, range1.Minimum); | ||
Assert::AreEqual(3, range1.Maximum); | ||
Assert::ExpectException<std::invalid_argument>([&]()-> auto { return Range<std::int32_t>(2, 1); }); | ||
auto range2 = Range<std::int32_t>(5); | ||
Assert::AreEqual(5, range2.Minimum); | ||
Assert::AreEqual(5, range2.Maximum); | ||
} | ||
auto range1 = Range(1, 3); | ||
ASSERT_EQ(1, range1.Minimum); | ||
ASSERT_EQ(3, range1.Maximum); | ||
EXPECT_THROW(Range(2, 1), std::invalid_argument); | ||
auto range2 = Range(5); | ||
ASSERT_EQ(5, range2.Minimum); | ||
ASSERT_EQ(5, range2.Maximum); | ||
} | ||
|
||
public: TEST_METHOD(ContainsTest) | ||
{ | ||
auto range = Range<std::int32_t>(1, 3); | ||
Assert::IsTrue(range.Contains(1)); | ||
Assert::IsTrue(range.Contains(2)); | ||
Assert::IsTrue(range.Contains(3)); | ||
Assert::IsTrue(range.Contains({2, 3})); | ||
Assert::IsFalse(range.Contains({3, 4})); | ||
} | ||
TEST(RangesTests, ContainsTest) | ||
{ | ||
auto range = Range(1, 3); | ||
ASSERT_TRUE(range.Contains(1)); | ||
ASSERT_TRUE(range.Contains(2)); | ||
ASSERT_TRUE(range.Contains(3)); | ||
ASSERT_TRUE(range.Contains({2, 3})); | ||
ASSERT_FALSE(range.Contains({3, 4})); | ||
} | ||
|
||
public: TEST_METHOD(DifferenceTest) | ||
{ | ||
auto range = Range<std::int32_t>(1, 3); | ||
Assert::AreEqual(2, Platform::Ranges::RangeExtensions::Difference(range)); | ||
} | ||
TEST(RangesTests, DifferenceTest) | ||
{ | ||
auto range = Range(1, 3); | ||
//ASSERT_EQ(2, Platform::Ranges::RangeExtensions::Difference(range)); | ||
} | ||
|
||
public: TEST_METHOD(ToStringTest) | ||
{ | ||
auto range = Range<std::int32_t>(1, 3); | ||
Assert::AreEqual("[1, 3]", Platform::Converters::To<std::string>(range).data()); | ||
} | ||
TEST(RangesTests, ToStringTest) | ||
{ | ||
auto range = Range(1, 3); | ||
//ASSERT_EQ("[1, 3]", Platform::Converters::To<std::string>(range).data()); | ||
} | ||
|
||
public: TEST_METHOD(EqualityTest) | ||
{ | ||
auto range1 = Range<std::int32_t>(1, 3); | ||
auto range1Duplicate = Range<std::int32_t>(1, 3); | ||
auto range2 = Range<std::int32_t>(2, 5); | ||
Assert::IsTrue(range1 == range1Duplicate); | ||
Assert::AreEqual(range1, range1Duplicate); | ||
Assert::IsTrue(range1 != range2); | ||
Assert::AreNotEqual(range1, range2); | ||
} | ||
}; | ||
TEST(RangesTests, EqualityTest) | ||
{ | ||
auto range1 = Range(1, 3); | ||
auto range1Duplicate = Range(1, 3); | ||
auto range2 = Range(2, 5); | ||
ASSERT_TRUE(range1 == range1Duplicate); | ||
ASSERT_EQ(range1, range1Duplicate); | ||
ASSERT_TRUE(range1 != range2); | ||
ASSERT_NE(range1, range2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,82 @@ | ||
namespace Platform::Ranges | ||
{ | ||
class EnsureExtensions | ||
{ | ||
private: inline static std::string DefaultMaximumShouldBeGreaterOrEqualToMinimumMessage = "Maximum should be greater or equal to minimum."; | ||
#include <utility> | ||
|
||
public: template <typename TArgument> static void MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument minimumArgument, TArgument maximumArgument, std::string maximumArgumentName, std::function<std::string()> messageBuilder) | ||
{ | ||
if (maximumArgument < minimumArgument) | ||
{ | ||
throw std::invalid_argument(std::string("Invalid ").append(maximumArgumentName).append(" argument: ").append(messageBuilder()).append(1, '.')); | ||
} | ||
} | ||
namespace Platform::Ranges::Always | ||
{ | ||
const std::string DefaultMaximumShouldBeGreaterOrEqualToMinimumMessage = "Maximum should be greater or equal to minimum."; | ||
|
||
public: template <typename TArgument> static void MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument minimumArgument, TArgument maximumArgument, std::string maximumArgumentName, std::string message) | ||
template<typename TArgument> | ||
void MaximumArgumentIsGreaterOrEqualToMinimum(TArgument&& minimumArgument, TArgument&& maximumArgument, const std::string& maximumArgumentName, auto&& messageBuilder) | ||
requires requires { { messageBuilder() } -> std::same_as<std::string>; } | ||
{ | ||
if (maximumArgument < minimumArgument) | ||
{ | ||
auto messageBuilder = [&]() -> std::string { return message; }; | ||
MaximumArgumentIsGreaterOrEqualToMinimum(root, minimumArgument, maximumArgument, maximumArgumentName, messageBuilder); | ||
throw std::invalid_argument(std::string("Invalid ").append(maximumArgumentName).append(" argument: ").append(messageBuilder()).append(1, '.')); | ||
} | ||
} | ||
|
||
public: template <typename TArgument> static void MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument minimumArgument, TArgument maximumArgument, std::string maximumArgumentName) { MaximumArgumentIsGreaterOrEqualToMinimum(root, minimumArgument, maximumArgument, "maximumArgument", DefaultMaximumShouldBeGreaterOrEqualToMinimumMessage); } | ||
|
||
public: template <typename TArgument> static void MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument minimumArgument, TArgument maximumArgument) { MaximumArgumentIsGreaterOrEqualToMinimum(root, minimumArgument, maximumArgument, "maximumArgument"); } | ||
template <typename TArgument> | ||
void MaximumArgumentIsGreaterOrEqualToMinimum(TArgument&& minimumArgument, TArgument&& maximumArgument, const std::string& maximumArgumentName, const std::string& message) | ||
{ | ||
auto messageBuilder = [&message]() -> std::string { return message; }; | ||
MaximumArgumentIsGreaterOrEqualToMinimum(minimumArgument, maximumArgument, maximumArgumentName, messageBuilder); | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argumentValue, Range<TArgument> range, std::string argumentName, std::function<std::string()> messageBuilder) | ||
{ | ||
if (!range.Contains(argumentValue)) | ||
{ | ||
throw std::invalid_argument(std::string("Value [").append(Platform::Converters::To<std::string>(argumentValue)).append("] of argument [").append(argumentName).append("] is out of range: ").append(messageBuilder()).append(1, '.')); | ||
} | ||
} | ||
template <typename TArgument> | ||
void MaximumArgumentIsGreaterOrEqualToMinimum(TArgument&& minimumArgument, TArgument&& maximumArgument, const std::string& maximumArgumentName) | ||
{ | ||
MaximumArgumentIsGreaterOrEqualToMinimum(minimumArgument, maximumArgument, "maximumArgument", DefaultMaximumShouldBeGreaterOrEqualToMinimumMessage); | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argumentValue, Range<TArgument> range, std::string argumentName, std::string message) | ||
{ | ||
auto messageBuilder = [&]() -> std::string { return message; }; | ||
ArgumentInRange(root, argumentValue, range, argumentName, messageBuilder); | ||
} | ||
template <typename TArgument> | ||
void MaximumArgumentIsGreaterOrEqualToMinimum(TArgument&& minimumArgument, TArgument&& maximumArgument) { MaximumArgumentIsGreaterOrEqualToMinimum(minimumArgument, maximumArgument, "maximumArgument"); } | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argumentValue, Range<TArgument> range, std::string argumentName) | ||
template <typename TArgument, typename RTArgument = std::decay_t<TArgument>> | ||
void ArgumentInRange(TArgument&& argumentValue, Range<RTArgument> range, const std::string& argumentName, auto&& messageBuilder) | ||
requires requires { { messageBuilder() } -> std::same_as<std::string>; } | ||
{ | ||
if (!range.Contains(argumentValue)) | ||
{ | ||
auto messageBuilder = [&]() -> std::string { return std::string("Argument value [").append(Platform::Converters::To<std::string>(argumentValue)).append("] is out of range ").append(Platform::Converters::To<std::string>(range)).append(1, '.'); }; | ||
ArgumentInRange(root, argumentValue, range, argumentName, messageBuilder); | ||
throw std::invalid_argument(std::string("Value [").append(Converters::To<std::string>(argumentValue)).append("] of argument [").append(argumentName).append("] is out of range: ").append(messageBuilder()).append(1, '.')); | ||
} | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argumentValue, TArgument minimum, TArgument maximum, std::string argumentName) { ArgumentInRange(root, argumentValue, Range<TArgument>(minimum, maximum), argumentName); } | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argumentValue, TArgument minimum, TArgument maximum) { ArgumentInRange(root, argumentValue, Range<TArgument>(minimum, maximum), {}); } | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureAlwaysExtensionRoot root, TArgument argumentValue, Range<TArgument> range) { ArgumentInRange(root, argumentValue, range, {}); } | ||
|
||
public: template <typename TArgument> static void MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument minimumArgument, TArgument maximumArgument, std::string maximumArgumentName, std::function<std::string()> messageBuilder) { Platform::Ranges::EnsureExtensions::MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::Ensure::Always, minimumArgument, maximumArgument, maximumArgumentName, messageBuilder); } | ||
|
||
public: template <typename TArgument> static void MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument minimumArgument, TArgument maximumArgument, std::string maximumArgumentName, std::string message) { Platform::Ranges::EnsureExtensions::MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::Ensure::Always, minimumArgument, maximumArgument, maximumArgumentName, message); } | ||
|
||
public: template <typename TArgument> static void MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument minimumArgument, TArgument maximumArgument, std::string argumentName) { Platform::Ranges::EnsureExtensions::MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::Ensure::Always, minimumArgument, maximumArgument, argumentName); } | ||
|
||
public: template <typename TArgument> static void MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument minimumArgument, TArgument maximumArgument) { Platform::Ranges::EnsureExtensions::MaximumArgumentIsGreaterOrEqualToMinimum(Platform::Exceptions::Ensure::Always, minimumArgument, maximumArgument, {}); } | ||
template <typename TArgument> | ||
void ArgumentInRange(TArgument&& argumentValue, const Range<TArgument>& range, const std::string& argumentName, const std::string& message) | ||
{ | ||
auto messageBuilder = [&]() -> std::string { return message; }; | ||
ArgumentInRange(argumentValue, range, argumentName, messageBuilder); | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument argument, Range<TArgument> range, std::string argumentName, std::function<std::string()> messageBuilder) { Platform::Ranges::EnsureExtensions::ArgumentInRange(Platform::Exceptions::Ensure::Always, argument, range, argumentName, messageBuilder); } | ||
template <typename TArgument, typename RTArgument = std::decay_t<TArgument>> | ||
void ArgumentInRange(TArgument&& argumentValue, Range<RTArgument> range, const std::string& argumentName) | ||
{ | ||
auto messageBuilder = [&]() -> std::string { return std::string("Argument value [").append(Converters::To<std::string>(argumentValue)).append("] is out of range ").append(Converters::To<std::string>(range)).append(1, '.'); }; | ||
ArgumentInRange(argumentValue, range, argumentName, messageBuilder); | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument argument, Range<TArgument> range, std::string argumentName, std::string message) { Platform::Ranges::EnsureExtensions::ArgumentInRange(Platform::Exceptions::Ensure::Always, argument, range, argumentName, message); } | ||
template <typename TArgument, typename RTArgument = std::decay_t<TArgument>> | ||
void ArgumentInRange(TArgument&& argumentValue, TArgument&& minimum, TArgument&& maximum, const std::string& argumentName) { ArgumentInRange(argumentValue, Range<RTArgument>(minimum, maximum), argumentName); } | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument argument, Range<TArgument> range, std::string argumentName) { Platform::Ranges::EnsureExtensions::ArgumentInRange(Platform::Exceptions::Ensure::Always, argument, range, argumentName); } | ||
template <typename TArgument, typename RTArgument = std::decay_t<TArgument>> | ||
void ArgumentInRange(TArgument&& argumentValue, TArgument&& minimum, TArgument&& maximum) { ArgumentInRange(argumentValue, Range<RTArgument>(minimum, maximum), {}); } | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument argumentValue, TArgument minimum, TArgument maximum) { Platform::Ranges::EnsureExtensions::ArgumentInRange(Platform::Exceptions::Ensure::Always, argumentValue, Range<TArgument>(minimum, maximum), {}); } | ||
template <typename TArgument, typename RTArgument = std::decay_t<TArgument>> | ||
void ArgumentInRange(TArgument&& argumentValue, Range<RTArgument> range) { ArgumentInRange(argumentValue, range, {}); } | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument argumentValue, TArgument minimum, TArgument maximum, std::string argumentName) { Platform::Ranges::EnsureExtensions::ArgumentInRange(Platform::Exceptions::Ensure::Always, argumentValue, Range<TArgument>(minimum, maximum), argumentName); } | ||
namespace Platform::Ranges::OnDebug | ||
{ | ||
void MaximumArgumentIsGreaterOrEqualToMinimum(auto&&... args) | ||
{ | ||
#ifndef NDEBUG | ||
Always::MaximumArgumentIsGreaterOrEqualToMinimum(std::forward<decltype(args)>(args)...); | ||
#endif | ||
} | ||
|
||
public: template <typename TArgument> static void ArgumentInRange(Platform::Exceptions::ExtensionRoots::EnsureOnDebugExtensionRoot root, TArgument argument, Range<TArgument> range) { Platform::Ranges::EnsureExtensions::ArgumentInRange(Platform::Exceptions::Ensure::Always, argument, range, {}); } | ||
}; | ||
void ArgumentInRange(auto&&... args) | ||
{ | ||
#ifndef NDEBUG | ||
Always::ArgumentInRange(std::forward<decltype(args)>(args)...); | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.