Browse Source

Extended Double class

- implemented IStorable interface
- added tests for Double class
pcmattulat 4 years ago
parent
commit
4245ec8efc
3 changed files with 60 additions and 1 deletions
  1. 25 0
      source/boxing/Double.cpp
  2. 6 1
      source/boxing/Double.hpp
  3. 29 0
      test/cases/boxing/DoubleTest.cpp

+ 25 - 0
source/boxing/Double.cpp

@@ -160,6 +160,17 @@ void ls_std::Double::operator--() {
   this->value -= 1.0f;
 }
 
+ls_std::byte_field ls_std::Double::load() {
+  ls_std::byte_field data {};
+
+  if(this->storable != nullptr && this->serializable != nullptr) {
+    data = this->storable->load();
+    this->serializable->unmarshal(data);
+  }
+
+  return data;
+}
+
 ls_std::byte_field ls_std::Double::marshal() {
   ls_std::byte_field data {};
 
@@ -174,6 +185,16 @@ void ls_std::Double::parse(std::string _parseText) {
   this->value = std::stod(_parseText);
 }
 
+void ls_std::Double::save(const ls_std::byte_field &_data) {
+  if(this->serializable != nullptr) {
+    if(_data.empty()) {
+      this->storable->save(this->serializable->marshal());
+    } else {
+      this->storable->save(_data);
+    }
+  }
+}
+
 std::string ls_std::Double::toString() {
   return std::to_string(this->value);
 }
@@ -199,3 +220,7 @@ void ls_std::Double::setEpsilon(double _epsilon) {
 void ls_std::Double::setSerializable(std::shared_ptr<ISerializable> _serializable) {
   this->serializable = std::move(_serializable);
 }
+
+void ls_std::Double::setStorable(std::shared_ptr<IStorable> _storable) {
+  this->storable = std::move(_storable);
+}

+ 6 - 1
source/boxing/Double.hpp

@@ -14,9 +14,10 @@
 #include "../base/Class.hpp"
 #include "IBoxing.hpp"
 #include "../serialization/ISerializable.hpp"
+#include "../io/IStorable.hpp"
 
 namespace ls_std {
-  class Double : public Class, public IBoxing, public ISerializable {
+  class Double : public Class, public IBoxing, public ISerializable, public IStorable {
     public:
 
       Double();
@@ -76,8 +77,10 @@ namespace ls_std {
 
       // implementation
 
+      ls_std::byte_field load() override;
       ls_std::byte_field marshal() override;
       void parse(std::string _parseText) override;
+      void save(const ls_std::byte_field& _data) override;
       std::string toString() override;
       void unmarshal(const ls_std::byte_field& _data) override;
 
@@ -87,11 +90,13 @@ namespace ls_std {
       double getValue();
       void setEpsilon(double _epsilon);
       void setSerializable(std::shared_ptr<ISerializable> _serializable);
+      void setStorable(std::shared_ptr<IStorable> _storable);
 
     private:
 
       double epsilon {};
       std::shared_ptr<ISerializable> serializable {};
+      std::shared_ptr<IStorable> storable {};
       double value {};
   };
 }

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

@@ -11,6 +11,10 @@
 #include "../../../source/boxing/Double.hpp"
 #include "../../../source/serialization/boxing/SerializableJSONDouble.hpp"
 #include "../../../source/boxing/String.hpp"
+#include "../../TestHelper.hpp"
+#include "../../../source/io/File.hpp"
+#include "../../../source/io/FileWriter.hpp"
+#include "../../../source/io/StorableFile.hpp"
 
 namespace {
   class DoubleTest : public ::testing::Test {
@@ -270,6 +274,31 @@ namespace {
 
   // implementation
 
+  TEST_F(DoubleTest, load)
+  {
+    // preparation
+
+    std::shared_ptr<ls_std::Double> x = std::make_shared<ls_std::Double>();
+    std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_double.json";
+    ls_std::File file {path};
+    file.createNewFile();
+    ls_std::FileWriter writer {file};
+    writer.write(R"({"value":3.14159})");
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONDouble>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    auto storable = std::make_shared<ls_std::StorableFile>(path);
+    x->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
+
+    // check
+
+    x->load();
+    ASSERT_DOUBLE_EQ(3.14159, *x);
+
+    file.remove();
+  }
+
   TEST_F(DoubleTest, marshal)
   {
     std::shared_ptr<ls_std::Double> x = std::make_shared<ls_std::Double>(3.14159);