Browse Source

Added Double boxing class

- added Double boxing class to wrap primitive double datatype
- added tests for Double boxing class
pcmattulat 4 years ago
parent
commit
57fbe1857e
4 changed files with 588 additions and 2 deletions
  1. 6 2
      CMakeLists.txt
  2. 181 0
      source/boxing/Double.cpp
  3. 93 0
      source/boxing/Double.hpp
  4. 308 0
      test/cases/boxing/DoubleTest.cpp

+ 6 - 2
CMakeLists.txt

@@ -42,12 +42,16 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/Boolean.hpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/Boolean.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/exception/IllegalArgumentException.hpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/Float.hpp source/boxing/Float.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/Float.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/Float.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/Double.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/Double.cpp)
 
 set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/BooleanTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/FloatTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/FloatTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/DoubleTest.cpp)
 
 ##########################################################
 # Build

+ 181 - 0
source/boxing/Double.cpp

@@ -0,0 +1,181 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-14
+ * Changed:         2020-08-14
+ *
+ * */
+
+#include <cmath>
+#include "Double.hpp"
+
+ls_std::Double::Double() : Class("Double"),
+epsilon(0.00000001)
+{}
+
+ls_std::Double::Double(double _value) : Class("Double"),
+epsilon(0.00000001),
+value(_value)
+{}
+
+ls_std::Double::operator double() const {
+  return this->value;
+}
+
+ls_std::Double & ls_std::Double::operator=(double _value) {
+  this->value = _value;
+  return *this;
+}
+
+double ls_std::Double::operator-() const {
+  return -this->value;
+}
+
+double ls_std::Double::operator+(const Double &_double) const {
+  return this->value + _double;
+}
+
+double ls_std::Double::operator+(double _value) const {
+  return this->value + _value;
+}
+
+double ls_std::Double::operator*(const Double &_double) const {
+  return this->value * _double;
+}
+
+double ls_std::Double::operator*(double _value) const {
+  return this->value * _value;
+}
+
+double ls_std::Double::operator-(const Double &_double) const {
+  return this->value - _double;
+}
+
+double ls_std::Double::operator-(double _value) const {
+  return this->value - _value;
+}
+
+double ls_std::Double::operator/(const Double &_double) const {
+  return this->value / _double;
+}
+
+double ls_std::Double::operator/(double _value) const {
+  return this->value / _value;
+}
+
+ls_std::Double & ls_std::Double::operator+=(const Double &_double) {
+  this->value += _double;
+  return *this;
+}
+
+ls_std::Double & ls_std::Double::operator+=(double _value) {
+  this->value += _value;
+  return *this;
+}
+
+ls_std::Double & ls_std::Double::operator-=(const Double &_double) {
+  this->value -= _double;
+  return *this;
+}
+
+ls_std::Double & ls_std::Double::operator-=(double _value) {
+  this->value -= _value;
+  return *this;
+}
+
+ls_std::Double & ls_std::Double::operator*=(const Double &_double) {
+  this->value *= _double;
+  return *this;
+}
+
+ls_std::Double & ls_std::Double::operator*=(double _value) {
+  this->value *= _value;
+  return *this;
+}
+
+ls_std::Double & ls_std::Double::operator/=(const Double &_double) {
+  this->value /= _double;
+  return *this;
+}
+
+ls_std::Double & ls_std::Double::operator/=(double _value) {
+  this->value /= _value;
+  return *this;
+}
+
+bool ls_std::Double::operator==(const Double &_double) const {
+  return std::fabs(this->value - _double) < this->epsilon;
+}
+
+bool ls_std::Double::operator==(double _value) const {
+  return std::fabs(this->value - _value) < this->epsilon;
+}
+
+bool ls_std::Double::operator!=(const Double &_double) const {
+  return std::fabs(this->value - _double) >= this->epsilon;
+}
+
+bool ls_std::Double::operator!=(double _value) const {
+  return std::fabs(this->value - _value) >= this->epsilon;
+}
+
+bool ls_std::Double::operator>(const Double &_double) const {
+  return this->value > _double;
+}
+
+bool ls_std::Double::operator>(double _value) const {
+  return this->value > _value;
+}
+
+bool ls_std::Double::operator>=(const Double &_double) const {
+  return this->value >= _double;
+}
+
+bool ls_std::Double::operator>=(double _value) const {
+  return this->value >= _value;
+}
+
+bool ls_std::Double::operator<(const Double &_double) const {
+  return this->value < _double;
+}
+
+bool ls_std::Double::operator<(double _value) const {
+  return this->value < _value;
+}
+
+bool ls_std::Double::operator<=(const Double &_double) const {
+  return this->value <= _double;
+}
+
+bool ls_std::Double::operator<=(double _value) const {
+  return this->value <= _value;
+}
+
+void ls_std::Double::operator++() {
+  this->value += 1.0f;
+}
+
+void ls_std::Double::operator--() {
+  this->value -= 1.0f;
+}
+
+void ls_std::Double::parse(std::string parseText) {
+  this->value = std::stod(parseText);
+}
+
+std::string ls_std::Double::toString() {
+  return std::to_string(this->value);
+}
+
+double ls_std::Double::getEpsilon() {
+  return this->epsilon;
+}
+
+double ls_std::Double::getValue() {
+  return this->value;
+}
+
+void ls_std::Double::setEpsilon(double _epsilon) {
+  this->epsilon = _epsilon;
+}

+ 93 - 0
source/boxing/Double.hpp

@@ -0,0 +1,93 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-14
+ * Changed:         2020-08-14
+ *
+ * */
+
+#ifndef DOUBLE_HPP
+#define DOUBLE_HPP
+
+#include "../base/Class.hpp"
+#include "IBoxing.hpp"
+
+namespace ls_std {
+  class Double : public Class, IBoxing {
+    public:
+
+      Double();
+      explicit Double(double _value);
+      ~Double() = default;
+
+      // conversion operator
+
+      operator double() const; // do not make explicit!
+
+      // assignment operators
+
+      Double& operator=(double _value);
+
+      // arithmetic operators
+
+      double operator-() const;
+      double operator+(const Double& _double) const;
+      double operator+(double _value) const;
+      double operator*(const Double& _double) const;
+      double operator*(double _value) const;
+      double operator-(const Double& _double) const;
+      double operator-(double _value) const;
+      double operator/(const Double& _double) const;
+      double operator/(double _value) const;
+
+      // compound operators
+
+      Double& operator+=(const Double& _double);
+      Double& operator+=(double _value);
+      Double& operator-=(const Double& _double);
+      Double& operator-=(double _value);
+      Double& operator*=(const Double& _double);
+      Double& operator*=(double _value);
+      Double& operator/=(const Double& _double);
+      Double& operator/=(double _value);
+
+      // comparison operators
+
+      bool operator==(const Double& _double) const;
+      bool operator==(double _value) const;
+      bool operator!=(const Double& _double) const;
+      bool operator!=(double _value) const;
+      bool operator>(const Double& _double) const;
+      bool operator>(double _value) const;
+      bool operator>=(const Double& _double) const;
+      bool operator>=(double _value) const;
+      bool operator<(const Double& _double) const;
+      bool operator<(double _value) const;
+      bool operator<=(const Double& _double) const;
+      bool operator<=(double _value) const;
+
+      // increment / decrement operator
+
+      void operator++();
+      void operator--();
+
+      // implementation
+
+      void parse(std::string parseText) override;
+      std::string toString() override;
+
+      // additional functionality
+
+      double getEpsilon();
+      double getValue();
+      void setEpsilon(double _epsilon);
+
+    private:
+
+      double value {};
+      double epsilon {};
+  };
+}
+
+#endif

+ 308 - 0
test/cases/boxing/DoubleTest.cpp

@@ -0,0 +1,308 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-14
+ * Changed:         2020-08-14
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../../source/boxing/Double.hpp"
+
+namespace {
+  class DoubleTest : public ::testing::Test {
+    protected:
+
+      DoubleTest() = default;
+      ~DoubleTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  // assignment operators
+
+  TEST_F(DoubleTest, operatorAssignment)
+  {
+    ls_std::Double x {13.023};
+    ASSERT_EQ(13.023, x);
+
+    x = 44.22;
+    ASSERT_EQ(44.22, x);
+
+    ls_std::Double y {3.0};
+    x = y;
+    ASSERT_EQ(3.0, x);
+  }
+
+  // arithmetic operators
+
+  TEST_F(DoubleTest, operatorHyphen)
+  {
+    ls_std::Double x {3.25};
+    ASSERT_DOUBLE_EQ(-3.25, -x);
+  }
+
+  TEST_F(DoubleTest, operatorAddition)
+  {
+    ls_std::Double x {3.1415};
+    ls_std::Double y {2.223};
+    ls_std::Double z {x + y};
+
+    ASSERT_DOUBLE_EQ(5.3645, z);
+    ASSERT_DOUBLE_EQ(5.3645, x + 2.223);
+  }
+
+  TEST_F(DoubleTest, operatorMultiplication)
+  {
+    ls_std::Double x {3.14};
+    ls_std::Double y {2.22};
+    ls_std::Double z {x * y};
+
+    ASSERT_DOUBLE_EQ(6.9708, z);
+    ASSERT_DOUBLE_EQ(6.9708, x * 2.22);
+  }
+
+  TEST_F(DoubleTest, operatorSubstraction)
+  {
+    ls_std::Double x {3.1415};
+    ls_std::Double y {2.225};
+    ls_std::Double z {x - y};
+
+    ASSERT_DOUBLE_EQ(0.9165, z);
+    ASSERT_DOUBLE_EQ(0.9165, x - 2.225);
+  }
+
+  TEST_F(DoubleTest, operatorDivision)
+  {
+    ls_std::Double x {2.25};
+    ls_std::Double y {0.5};
+    ls_std::Double z {x / y};
+
+    ASSERT_DOUBLE_EQ(4.5, z);
+    ASSERT_DOUBLE_EQ(4.5, x / 0.5);
+  }
+
+  // compound operators
+
+  TEST_F(DoubleTest, operatorAddEqual)
+  {
+    ls_std::Double x {2.25000000};
+    ls_std::Double y {-0.39000000};
+    ASSERT_DOUBLE_EQ(2.25000000, x);
+
+    x += 3.14000000;
+    ASSERT_DOUBLE_EQ(5.39000000, x);
+
+    x += y;
+    ASSERT_DOUBLE_EQ(5.0000000, x);
+  }
+
+  TEST_F(DoubleTest, operatorSubEqual)
+  {
+    ls_std::Double x {2.25};
+    ls_std::Double y {-0.04};
+    ASSERT_DOUBLE_EQ(2.25, x);
+
+    x -= 1.14;
+    ASSERT_DOUBLE_EQ(1.11, x);
+
+    x -= y;
+    ASSERT_DOUBLE_EQ(1.15, x);
+  }
+
+  TEST_F(DoubleTest, operatorMulEqual)
+  {
+    ls_std::Double x {2.25000000};
+    ls_std::Double y {0.04000000};
+    ASSERT_DOUBLE_EQ(2.25000000, x);
+
+    x *= 1.14000000;
+    ASSERT_DOUBLE_EQ(2.56500000, x);
+
+    x *= y;
+    ASSERT_DOUBLE_EQ(0.102600000, x);
+  }
+
+  TEST_F(DoubleTest, operatorDivEqual)
+  {
+    ls_std::Double x {2.25};
+    ls_std::Double y {1.5};
+    ASSERT_DOUBLE_EQ(2.25, x);
+
+    x /= 0.05;
+    ASSERT_DOUBLE_EQ(45.0, x);
+
+    x /= y;
+    ASSERT_DOUBLE_EQ(30.0, x);
+  }
+
+  // comparison operators
+
+  TEST_F(DoubleTest, operatorEqual)
+  {
+    ls_std::Double x {3.14159};
+    ls_std::Double y {3.14159};
+
+    ASSERT_TRUE(x == y);
+    ASSERT_TRUE(y == x);
+    ASSERT_TRUE(x == 3.14159);
+    ASSERT_TRUE(3.14159 == x);
+  }
+
+  TEST_F(DoubleTest, operatorNotEqual)
+  {
+    ls_std::Double x {3.1415};
+    ls_std::Double y {3.1414};
+
+    ASSERT_TRUE(x != y);
+    ASSERT_TRUE(y != x);
+    ASSERT_TRUE(x != 3.1414);
+    ASSERT_TRUE(3.1414 != x);
+  }
+
+  TEST_F(DoubleTest, operatorGreaterThan)
+  {
+    ls_std::Double x {3.1415};
+    ls_std::Double y {3.1414};
+
+    ASSERT_TRUE(x > y);
+    ASSERT_TRUE(x > 3.1414);
+  }
+
+  TEST_F(DoubleTest, operatorGreaterThanNegative)
+  {
+    ls_std::Double x {3.1414};
+    ls_std::Double y {3.1414};
+
+    ASSERT_FALSE(x > y);
+    ASSERT_FALSE(x > 3.1414);
+  }
+
+  TEST_F(DoubleTest, operatorGreaterThanEqual)
+  {
+    ls_std::Double x {3.1414};
+    ls_std::Double y {3.1414};
+    ls_std::Double z {3.1415};
+
+    ASSERT_TRUE(x >= y);
+    ASSERT_TRUE(z >= y);
+    ASSERT_TRUE(x >= 3.1414);
+    ASSERT_TRUE(z >= 3.1414);
+  }
+
+  TEST_F(DoubleTest, operatorGreaterThanEqualNegative)
+  {
+    ls_std::Double x {3.1414};
+    ls_std::Double y {3.1415};
+
+    ASSERT_FALSE(x >= y);
+    ASSERT_FALSE(x >= 3.1415);
+  }
+
+  TEST_F(DoubleTest, operatorLessThan)
+  {
+    ls_std::Double x {3.1413};
+    ls_std::Double y {3.1414};
+
+    ASSERT_TRUE(x < y);
+    ASSERT_TRUE(x < 3.1414);
+  }
+
+  TEST_F(DoubleTest, operatorLessThanNegative)
+  {
+    ls_std::Double x {3.1414};
+    ls_std::Double y {3.1414};
+
+    ASSERT_FALSE(x < y);
+    ASSERT_FALSE(x < 3.1414);
+  }
+
+  TEST_F(DoubleTest, operatorLessThanEqual)
+  {
+    ls_std::Double x {3.1414};
+    ls_std::Double y {3.1414};
+    ls_std::Double z {3.1415};
+
+    ASSERT_TRUE(x <= y);
+    ASSERT_TRUE(x <= z);
+    ASSERT_TRUE(x <= 3.1414);
+    ASSERT_TRUE(x <= 3.1415);
+  }
+
+  TEST_F(DoubleTest, operatorLessThanEqualNegative)
+  {
+    ls_std::Double x {3.1415};
+    ls_std::Double y {3.1414};
+
+    ASSERT_FALSE(x <= y);
+    ASSERT_FALSE(x <= 3.1414);
+  }
+
+  // increment / decrement operator
+
+  TEST_F(DoubleTest, operatorIncrement)
+  {
+    ls_std::Double x {3.1415};
+    ASSERT_DOUBLE_EQ(3.1415, x);
+
+    ++x;
+    ASSERT_DOUBLE_EQ(4.1415, x);
+
+    ++x;
+    ASSERT_DOUBLE_EQ(5.1415, x);
+  }
+
+  TEST_F(DoubleTest, operatorDecrement)
+  {
+    ls_std::Double x {3.1415};
+    ASSERT_DOUBLE_EQ(3.1415, x);
+
+    --x;
+    ASSERT_DOUBLE_EQ(2.1415, x);
+
+    --x;
+    ASSERT_DOUBLE_EQ(1.1415, x);
+  }
+
+  // implementation
+
+  TEST_F(DoubleTest, parse)
+  {
+    ls_std::Double x {};
+
+    x.parse("3.1415");
+    ASSERT_DOUBLE_EQ(3.1415, x);
+
+    x.parse("-2.1415");
+    ASSERT_DOUBLE_EQ(-2.1415, x);
+  }
+
+  TEST_F(DoubleTest, toString)
+  {
+    ls_std::Double x {13.1543};
+    ASSERT_TRUE(x.toString().find("13.1543") != std::string::npos);
+  }
+
+  // additional functionality
+
+  TEST_F(DoubleTest, getEpsilon)
+  {
+    ls_std::Double x {};
+    ASSERT_DOUBLE_EQ(0.00000001, x.getEpsilon());
+  }
+
+  TEST_F(DoubleTest, getValue)
+  {
+    ls_std::Double x {3.1415};
+    ASSERT_DOUBLE_EQ(3.1415, x.getValue());
+  }
+
+  TEST_F(DoubleTest, setEpsilon)
+  {
+    ls_std::Double x {};
+    x.setEpsilon(0.01);
+    ASSERT_DOUBLE_EQ(0.01, x.getEpsilon());
+  }
+}