feat: add binary operator factory

This commit is contained in:
jfmcarreira committed 2024-03-12 23:08:34 +00:00
1 parent 0631eaa5fc
commit affe74097e
4 files changed
+96

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
#ifndef CALCULATOR_CHALLENGE_BINARYOPERATORFACTORY_HPP
#define CALCULATOR_CHALLENGE_BINARYOPERATORFACTORY_HPP
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include "Operand.hpp"
template<typename T>
class BinaryOperatorFactory
{
using Operator = BinaryOperatorType<T>;
public:
BinaryOperatorFactory() noexcept;
BinaryOperatorFactory(const BinaryOperatorFactory&) noexcept = delete;
BinaryOperatorFactory(BinaryOperatorFactory&&) noexcept = delete;
auto operator=(const BinaryOperatorFactory&) noexcept -> BinaryOperatorFactory& = delete;
auto operator=(BinaryOperatorFactory&&) noexcept -> BinaryOperatorFactory& = delete;
~BinaryOperatorFactory() noexcept = default;
auto make(std::string_view text) noexcept -> std::optional<Operator>;
private:
std::unordered_map<std::string, Operator> _binary_operations;
};
#include "BinaryOperatorFactory.tcc"
#endif
+34
View File
@@ -0,0 +1,34 @@
#include "BinaryOperatorFactory.hpp"
template<typename T>
BinaryOperatorFactory<T>::BinaryOperatorFactory() noexcept
{
_binary_operations["+"] = [](auto lhs, auto rhs) { return lhs + rhs; };
_binary_operations["-"] = [](auto lhs, auto rhs) { return lhs - rhs; };
_binary_operations["*"] = [](auto lhs, auto rhs) { return lhs * rhs; };
_binary_operations["/"] = [](auto lhs, auto rhs) { return lhs / rhs; };
}
template<typename T>
auto BinaryOperatorFactory<T>::make(std::string_view text) noexcept -> std::optional<Operator>
{
auto sanitize = [](std::string_view text) noexcept -> std::string {
constexpr char kSpace{' '};
std::string_view view{text};
auto leading_spaces = view.find_first_not_of(kSpace);
if (leading_spaces == std::string::npos) return "";
auto sub_view = view.substr(leading_spaces);
auto trailling_spaces = sub_view.find_first_of(kSpace);
if (trailling_spaces == std::string::npos) return std::string{sub_view};
return std::string{sub_view.substr(0, trailling_spaces)};
};
auto found_op = _binary_operations.find(sanitize(text));
if (found_op == _binary_operations.end()) return std::nullopt;
return found_op->second;
}
+29
View File
@@ -0,0 +1,29 @@
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>
#include "BinaryOperatorFactory.hpp"
TEMPLATE_TEST_CASE("BinaryOperatorFactory", "[calculator-challenge]", std::int64_t, double)
{
constexpr TestType kLeftOperand{20};
constexpr TestType kRightOperand{2};
BinaryOperatorFactory<TestType> factory;
auto check_factory = [&](std::string_view str, TestType result) {
auto operation = factory.make(str);
if (!operation.has_value()) {
return false;
}
return (*operation)(kLeftOperand, kRightOperand) == result;
};
CHECK(check_factory("+", kLeftOperand + kRightOperand));
CHECK(check_factory(" + ", kLeftOperand + kRightOperand));
CHECK(check_factory(" +", kLeftOperand + kRightOperand));
CHECK(check_factory("+ ", kLeftOperand + kRightOperand));
CHECK(check_factory("-", kLeftOperand - kRightOperand));
CHECK(check_factory("*", kLeftOperand * kRightOperand));
CHECK(check_factory("/", kLeftOperand / kRightOperand));
}
+1
View File
@@ -43,6 +43,7 @@ target_link_libraries(calculator-challege PRIVATE fmt::fmt)
add_executable(calculator-challege-tests
CalculatorTests.cpp
BinaryOperatorFactoryTests.cpp
${CALCULATOR_CHALLENGE_SOURCES}
)
target_link_libraries(calculator-challege-tests PRIVATE Catch2::Catch2WithMain)