Jelajahi Sumber

Extended Float class

- implemented ISerializable interface
- extended tests for Float class
pcmattulat 4 tahun lalu
induk
melakukan
1b302b02c1
3 mengubah file dengan 61 tambahan dan 2 penghapusan
  1. 22 0
      source/boxing/Float.cpp
  2. 8 1
      source/boxing/Float.hpp
  3. 31 1
      test/cases/boxing/FloatTest.cpp

+ 22 - 0
source/boxing/Float.cpp

@@ -160,6 +160,17 @@ void ls_std::Float::operator--() {
   this->value -= 1.0f;
 }
 
+ls_std::byte_field ls_std::Float::marshal()
+{
+  ls_std::byte_field data {};
+
+  if(this->serializable != nullptr) {
+    data = this->serializable->marshal();
+  }
+
+  return data;
+}
+
 void ls_std::Float::parse(std::string _parseText) {
   this->value = std::stof(_parseText);
 }
@@ -168,6 +179,13 @@ std::string ls_std::Float::toString() {
   return std::to_string(this->value);
 }
 
+void ls_std::Float::unmarshal(const ls_std::byte_field& _data)
+{
+  if(this->serializable != nullptr) {
+    this->serializable->unmarshal(_data);
+  }
+}
+
 float ls_std::Float::getEpsilon() {
   return this->epsilon;
 }
@@ -179,3 +197,7 @@ float ls_std::Float::getValue() {
 void ls_std::Float::setEpsilon(float _epsilon) {
   this->epsilon = _epsilon;
 }
+
+void ls_std::Float::setSerializable(std::shared_ptr<ISerializable> _serializable) {
+  this->serializable = std::move(_serializable);
+}

+ 8 - 1
source/boxing/Float.hpp

@@ -10,11 +10,13 @@
 #ifndef LS_STD_FLOAT_HPP
 #define LS_STD_FLOAT_HPP
 
+#include <memory>
 #include "../base/Class.hpp"
 #include "IBoxing.hpp"
+#include "../serialization/ISerializable.hpp"
 
 namespace ls_std {
-  class Float : public Class, IBoxing {
+  class Float : public Class, public IBoxing, public ISerializable {
     public:
 
       Float();
@@ -74,17 +76,22 @@ 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
 
       float getEpsilon();
       float getValue();
       void setEpsilon(float _epsilon);
+      void setSerializable(std::shared_ptr<ISerializable> _serializable);
 
     private:
 
+      std::shared_ptr<ISerializable> serializable {};
+
       float value {};
       float epsilon {};
   };

+ 31 - 1
test/cases/boxing/FloatTest.cpp

@@ -3,12 +3,14 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2020-08-14
+ * Changed:         2020-09-04
  *
  * */
 
 #include <gtest/gtest.h>
 #include "../../../source/boxing/Float.hpp"
+#include "../../../source/serialization/boxing/SerializableJSONFloat.hpp"
+#include "../../../source/boxing/String.hpp"
 
 namespace {
   class FloatTest : public ::testing::Test {
@@ -268,6 +270,21 @@ namespace {
 
   // implementation
 
+  TEST_F(FloatTest, marshal)
+  {
+    std::shared_ptr<ls_std::Float> x = std::make_shared<ls_std::Float>(3.14159f);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONFloat>(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.13149f;
+    jsonString = x->marshal();
+    ASSERT_TRUE(jsonString.contains(R"({"value":17.1314)"));
+  }
+
   TEST_F(FloatTest, parse)
   {
     ls_std::Float x {};
@@ -285,6 +302,19 @@ namespace {
     ASSERT_TRUE(x.toString().find("13.1543") != std::string::npos);
   }
 
+  TEST_F(FloatTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Float> x = std::make_shared<ls_std::Float>(3.14159f);
+
+    ASSERT_FLOAT_EQ(3.14159f, *x);
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONFloat>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+    x->unmarshal(R"({"value":17.4132})");
+
+    ASSERT_FLOAT_EQ(17.4132, *x);
+  }
+
   // additional functionality
 
   TEST_F(FloatTest, getEpsilon)