Browse Source

Add IndexOutOfBoundsException class

This exception can be thrown in case an element is being tried to access, although the index being used for accessing the element is out of bounds.
Patrick-Christopher Mattulat 2 years ago
parent
commit
b5e460699f

+ 2 - 0
CMakeLists.txt

@@ -138,6 +138,7 @@ set(SOURCE_FILES_CORE
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/IllegalArgumentException.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/IllegalArithmeticOperationException.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/IncompleteJsonException.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/IndexOutOfBoundsException.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/NullPointerException.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/exception/WrongCallException.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/core/interface/IBoxing.cpp
@@ -226,6 +227,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/IllegalArgumentExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/IllegalArithmeticOperationExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/IncompleteJsonExceptionTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/IndexOutOfBoundsExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/NullPointerExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/WrongCallExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/ClassTest.cpp

+ 35 - 0
include/ls-std/core/exception/IndexOutOfBoundsException.hpp

@@ -0,0 +1,35 @@
+/*
+* 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_EXCEPTION_HPP
+#define LS_STD_INDEX_OUT_OF_BOUNDS_EXCEPTION_HPP
+
+#include <exception>
+#include <ls-std/os/dynamic-goal.hpp>
+#include <string>
+
+namespace ls::std::core
+{
+  class LS_STD_DYNAMIC_GOAL IndexOutOfBoundsException : public ::std::exception
+  {
+    public:
+
+      IndexOutOfBoundsException();
+      explicit IndexOutOfBoundsException(::std::string _message);
+      ~IndexOutOfBoundsException() override;
+
+      [[nodiscard]] const char *what() const noexcept override;
+
+    private:
+
+      ::std::string message{};
+  };
+}
+
+#endif

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

@@ -22,6 +22,7 @@
 #include <ls-std/core/exception/IllegalArgumentException.hpp>
 #include <ls-std/core/exception/IllegalArithmeticOperationException.hpp>
 #include <ls-std/core/exception/IncompleteJsonException.hpp>
+#include <ls-std/core/exception/IndexOutOfBoundsException.hpp>
 #include <ls-std/core/exception/NullPointerException.hpp>
 #include <ls-std/core/exception/WrongCallException.hpp>
 

+ 34 - 0
source/ls-std/core/exception/IndexOutOfBoundsException.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/exception/ExceptionMessage.hpp>
+#include <ls-std/core/exception/IndexOutOfBoundsException.hpp>
+
+ls::std::core::IndexOutOfBoundsException::IndexOutOfBoundsException() = default;
+
+ls::std::core::IndexOutOfBoundsException::IndexOutOfBoundsException(::std::string _message) : message(::std::move(_message))
+{}
+
+ls::std::core::IndexOutOfBoundsException::~IndexOutOfBoundsException() = default;
+
+const char *ls::std::core::IndexOutOfBoundsException::what() const noexcept
+{
+  ::std::string concatenatedMessage = "IndexOutOfBoundsException thrown - ";
+
+  if (this->message.empty())
+  {
+    concatenatedMessage = concatenatedMessage + "provided index is out of bounds!";
+  }
+  else
+  {
+    concatenatedMessage = concatenatedMessage + this->message;
+  }
+
+  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
+}

+ 67 - 0
test/cases/core/exception/IndexOutOfBoundsExceptionTest.cpp

@@ -0,0 +1,67 @@
+/*
+* 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;
+
+namespace
+{
+  class IndexOutOfBoundsExceptionTest : public ::testing::Test
+  {
+    protected:
+
+      IndexOutOfBoundsExceptionTest() = default;
+      ~IndexOutOfBoundsExceptionTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(IndexOutOfBoundsExceptionTest, constructor)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            throw IndexOutOfBoundsException{};
+          }
+          catch (const IndexOutOfBoundsException &_exception)
+          {
+            ::std::string message = _exception.what();
+            EXPECT_STREQ("IndexOutOfBoundsException thrown - provided index is out of bounds!", message.c_str());
+            throw;
+          }
+        },
+        IndexOutOfBoundsException);
+  }
+
+  TEST_F(IndexOutOfBoundsExceptionTest, constructor_dedicated_message)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            throw IndexOutOfBoundsException{"index 3 is out of bounds!"};
+          }
+          catch (const IndexOutOfBoundsException &_exception)
+          {
+            ::std::string message = _exception.what();
+            EXPECT_STREQ("IndexOutOfBoundsException thrown - index 3 is out of bounds!", message.c_str());
+            throw;
+          }
+        },
+        IndexOutOfBoundsException);
+  }
+}