Selaa lähdekoodia

Extended Double class

- implemented ISerializable interface
- extended tests for Double class
pcmattulat 4 vuotta sitten
vanhempi
commit
b1651c6a01
3 muutettua tiedostoa jossa 59 lisäystä ja 3 poistoa
  1. 21 1
      source/boxing/Double.cpp
  2. 8 2
      source/boxing/Double.hpp
  3. 30 0
      test/cases/boxing/DoubleTest.cpp

+ 21 - 1
source/boxing/Double.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2020-08-16
+ * Changed:         2020-09-04
  *
  * */
 
@@ -160,6 +160,16 @@ void ls_std::Double::operator--() {
   this->value -= 1.0f;
 }
 
+ls_std::byte_field ls_std::Double::marshal() {
+  ls_std::byte_field data {};
+
+  if(this->serializable != nullptr) {
+    data = this->serializable->marshal();
+  }
+
+  return data;
+}
+
 void ls_std::Double::parse(std::string _parseText) {
   this->value = std::stod(_parseText);
 }
@@ -168,6 +178,12 @@ std::string ls_std::Double::toString() {
   return std::to_string(this->value);
 }
 
+void ls_std::Double::unmarshal(const ls_std::byte_field &_data) {
+  if(this->serializable != nullptr) {
+    this->serializable->unmarshal(_data);
+  }
+}
+
 double ls_std::Double::getEpsilon() {
   return this->epsilon;
 }
@@ -179,3 +195,7 @@ double ls_std::Double::getValue() {
 void ls_std::Double::setEpsilon(double _epsilon) {
   this->epsilon = _epsilon;
 }
+
+void ls_std::Double::setSerializable(std::shared_ptr<ISerializable> _serializable) {
+  this->serializable = std::move(_serializable);
+}

+ 8 - 2
source/boxing/Double.hpp

@@ -10,11 +10,13 @@
 #ifndef LS_STD_DOUBLE_HPP
 #define LS_STD_DOUBLE_HPP
 
+#include <memory>
 #include "../base/Class.hpp"
 #include "IBoxing.hpp"
+#include "../serialization/ISerializable.hpp"
 
 namespace ls_std {
-  class Double : public Class, IBoxing {
+  class Double : public Class, public IBoxing, public ISerializable {
     public:
 
       Double();
@@ -74,19 +76,23 @@ namespace ls_std {
 
       // implementation
 
+      ls_std::byte_field marshal() override;
       void parse(std::string _parseText) override;
       std::string toString() override;
+      void unmarshal(const ls_std::byte_field& _data) override;
 
       // additional functionality
 
       double getEpsilon();
       double getValue();
       void setEpsilon(double _epsilon);
+      void setSerializable(std::shared_ptr<ISerializable> _serializable);
 
     private:
 
-      double value {};
       double epsilon {};
+      std::shared_ptr<ISerializable> serializable {};
+      double value {};
   };
 }
 

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

@@ -9,6 +9,8 @@
 
 #include <gtest/gtest.h>
 #include "../../../source/boxing/Double.hpp"
+#include "../../../source/serialization/boxing/SerializableJSONDouble.hpp"
+#include "../../../source/boxing/String.hpp"
 
 namespace {
   class DoubleTest : public ::testing::Test {
@@ -268,6 +270,21 @@ namespace {
 
   // implementation
 
+  TEST_F(DoubleTest, marshal)
+  {
+    std::shared_ptr<ls_std::Double> x = std::make_shared<ls_std::Double>(3.14159);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONDouble>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+    ls_std::String jsonString {x->marshal()};
+
+    ASSERT_TRUE(jsonString.contains(R"({"value":3.14159)"));
+
+    *x = 17.13149;
+    jsonString = x->marshal();
+    ASSERT_TRUE(jsonString.contains(R"({"value":17.1314)"));
+  }
+
   TEST_F(DoubleTest, parse)
   {
     ls_std::Double x {};
@@ -285,6 +302,19 @@ namespace {
     ASSERT_TRUE(x.toString().find("13.1543") != std::string::npos);
   }
 
+  TEST_F(DoubleTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Double> x = std::make_shared<ls_std::Double>(3.14159);
+
+    ASSERT_DOUBLE_EQ(3.14159f, *x);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONDouble>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+    x->unmarshal(R"({"value":17.4132})");
+
+    ASSERT_DOUBLE_EQ(17.4132, *x);
+  }
+
   // additional functionality
 
   TEST_F(DoubleTest, getEpsilon)