Browse Source

Add IndexOutOfBoundsEvaluator class

This evaluator will throw an IndexOutOfBoundsException in case a given index is out of bounds based on a given size.
Patrick-Christopher Mattulat 2 years ago
parent
commit
07d4ce7271

+ 2 - 0
CMakeLists.txt

@@ -128,6 +128,7 @@ set(SOURCE_FILES_BOXING
 
 set(SOURCE_FILES_CORE
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/evaluator/EmptyStringArgumentEvaluator.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/evaluator/IndexOutOfBoundsEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/evaluator/NullPointerArgumentEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/evaluator/NullPointerEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/EventNotHandledException.cpp
@@ -217,6 +218,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
 
     set(TEST_FILES_CORE
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/evaluator/EmptyStringArgumentEvaluatorTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/evaluator/IndexOutOfBoundsEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/evaluator/NullPointerArgumentEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/evaluator/NullPointerEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/EventNotHandledExceptionTest.cpp

+ 36 - 0
include/ls-std/core/evaluator/IndexOutOfBoundsEvaluator.hpp

@@ -0,0 +1,36 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-10
+* Changed:         2023-02-10
+*
+* */
+
+#ifndef LS_STD_INDEX_OUT_OF_BOUNDS_EVALUATOR_EVALUATOR_HPP
+#define LS_STD_INDEX_OUT_OF_BOUNDS_EVALUATOR_EVALUATOR_HPP
+
+#include <ls-std/core/interface/IEvaluator.hpp>
+#include <string>
+
+namespace ls::std::core
+{
+  class IndexOutOfBoundsEvaluator : public ls::std::core::interface_type::IEvaluator
+  {
+    public:
+
+      explicit IndexOutOfBoundsEvaluator(size_t _index, size_t _size);
+      explicit IndexOutOfBoundsEvaluator(size_t _index, size_t _size, ::std::string _message);
+      ~IndexOutOfBoundsEvaluator() override;
+
+      void evaluate() override;
+
+    private:
+
+      size_t index{};
+      ::std::string message{};
+      size_t size{};
+  };
+}
+
+#endif

+ 2 - 2
include/ls-std/core/evaluator/NullPointerEvaluator.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-08
-* Changed:         2023-02-08
+* Changed:         2023-02-10
 *
 * */
 
@@ -16,7 +16,7 @@
 
 namespace ls::std::core
 {
-  class NullPointerEvaluator : public ls::std::core::interface_type::IEvaluator
+  class NullPointerEvaluator : public ls::std::core::interface_type::IEvaluator // TODO: add missing dynamic goal
   {
     public:
 

+ 1 - 0
include/ls-std/ls-std-core.hpp

@@ -11,6 +11,7 @@
 #define LS_STD_LS_STD_CORE_HPP
 
 #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
+#include <ls-std/core/evaluator/IndexOutOfBoundsEvaluator.hpp>
 #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
 #include <ls-std/core/evaluator/NullPointerEvaluator.hpp>
 

+ 34 - 0
source/ls-std/core/evaluator/IndexOutOfBoundsEvaluator.cpp

@@ -0,0 +1,34 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-10
+* Changed:         2023-02-10
+*
+* */
+
+#include <ls-std/core/evaluator/IndexOutOfBoundsEvaluator.hpp>
+#include <ls-std/core/exception/IndexOutOfBoundsException.hpp>
+
+ls::std::core::IndexOutOfBoundsEvaluator::IndexOutOfBoundsEvaluator(size_t _index, size_t _size) : index(_index), size(_size)
+{}
+
+ls::std::core::IndexOutOfBoundsEvaluator::IndexOutOfBoundsEvaluator(size_t _index, size_t _size, ::std::string _message) : index(_index), size(_size), message(::std::move(_message))
+{}
+
+ls::std::core::IndexOutOfBoundsEvaluator::~IndexOutOfBoundsEvaluator() = default;
+
+void ls::std::core::IndexOutOfBoundsEvaluator::evaluate()
+{
+  if (this->index >= this->size)
+  {
+    if (this->message.empty())
+    {
+      throw ls::std::core::IndexOutOfBoundsException{};
+    }
+    else
+    {
+      throw ls::std::core::IndexOutOfBoundsException{this->message};
+    }
+  }
+}

+ 68 - 0
test/cases/core/evaluator/IndexOutOfBoundsEvaluatorTest.cpp

@@ -0,0 +1,68 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-10
+* Changed:         2023-02-10
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-core.hpp>
+#include <string>
+
+using namespace ls::std::core;
+using namespace ::std;
+
+namespace
+{
+  class IndexOutOfBoundsEvaluatorTest : public ::testing::Test
+  {
+    protected:
+
+      IndexOutOfBoundsEvaluatorTest() = default;
+      ~IndexOutOfBoundsEvaluatorTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(IndexOutOfBoundsEvaluatorTest, evaluate)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            IndexOutOfBoundsEvaluator(3, 2).evaluate();
+          }
+          catch (const IndexOutOfBoundsException &_exception)
+          {
+            string message = _exception.what();
+            ASSERT_STREQ("IndexOutOfBoundsException thrown - provided index is out of bounds!", message.c_str());
+            throw;
+          }
+        },
+        IndexOutOfBoundsException);
+  }
+
+  TEST_F(IndexOutOfBoundsEvaluatorTest, evaluate_dedicated_message)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            IndexOutOfBoundsEvaluator(3, 2, "index 3 is not in range of the containers size, which is 2!").evaluate();
+          }
+          catch (const IndexOutOfBoundsException &_exception)
+          {
+            string message = _exception.what();
+            ASSERT_STREQ("IndexOutOfBoundsException thrown - index 3 is not in range of the containers size, which is 2!", message.c_str());
+            throw;
+          }
+        },
+        IndexOutOfBoundsException);
+  }
+}