Browse Source

Implement POSIX system time functionality

Patrick-Christopher Mattulat 1 năm trước cách đây
mục cha
commit
4f08e50366

+ 9 - 1
CMakeLists.txt

@@ -218,6 +218,10 @@ set(SOURCE_FILES_IO
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/StorableFile.cpp)
 
 set(SOURCE_FILES_TIME
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/time/system-time/IPosixClock.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/time/system-time/PosixClock.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/time/system-time/SystemTime.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/time/system-time/SystemTimeParameter.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/time/Date.cpp)
 
 ####################################################################################################################
@@ -335,7 +339,10 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/JsonTest.cpp)
 
     set(UNIT_TEST_FILES_TIME
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/time/DateTest.cpp)
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/time/system-time/SystemTimeParameterTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/time/system-time/SystemTimeTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/time/DateTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/time/system-time/MockPosixClock.cpp)
 endif ()
 
 ####################################################################################################################
@@ -669,6 +676,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
     message("${MODULE_NAME_TIME}: Linking libraries for unit test application...")
     target_link_libraries(${MODULE_NAME_TIME}-unit-test
             gtest
+            gmock
             gtest_main
             "${MODULE_NAME_TIME}"
             "${MODULE_NAME_CORE}")

+ 6 - 2
include/ls-std/ls-std-time.hpp

@@ -3,14 +3,18 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-05-16
- * Changed:         2023-03-07
+ * Changed:         2023-03-15
  *
  * */
 
 #ifndef LS_STD_LS_STD_TIME_HPP
 #define LS_STD_LS_STD_TIME_HPP
 
-#include <ls-std/time/Date.hpp>
+#include <ls-std/time/system-time/IPosixClock.hpp>
+#include <ls-std/time/system-time/PosixClock.hpp>
 #include <ls-std/time/system-time/SystemTime.hpp>
+#include <ls-std/time/system-time/SystemTimeParameter.hpp>
+
+#include <ls-std/time/Date.hpp>
 
 #endif

+ 29 - 0
include/ls-std/time/system-time/IPosixClock.hpp

@@ -0,0 +1,29 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#ifndef LS_STD_I_POSIX_CLOCK_HPP
+#define LS_STD_I_POSIX_CLOCK_HPP
+
+#include <cstdint>
+#include <ls-std/os/dynamic-goal.hpp>
+
+namespace ls::std::time
+{
+  class LS_STD_DYNAMIC_GOAL IPosixClock
+  {
+    public:
+
+      IPosixClock();
+      virtual ~IPosixClock();
+
+      virtual bool setTime(uint32_t _timeStamp) = 0;
+  };
+}
+
+#endif

+ 30 - 0
include/ls-std/time/system-time/PosixClock.hpp

@@ -0,0 +1,30 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#ifndef LS_STD_POSIX_CLOCK_HPP
+#define LS_STD_POSIX_CLOCK_HPP
+
+#include "IPosixClock.hpp"
+#include <cstdint>
+#include <ls-std/os/dynamic-goal.hpp>
+
+namespace ls::std::time
+{
+  class LS_STD_DYNAMIC_GOAL PosixClock : public ls::std::time::IPosixClock
+  {
+    public:
+
+      PosixClock();
+      ~PosixClock() noexcept override;
+
+      [[nodiscard]] bool setTime(uint32_t _timeStamp) override;
+  };
+}
+
+#endif

+ 12 - 2
include/ls-std/time/system-time/SystemTime.hpp

@@ -3,15 +3,18 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-03-07
-* Changed:         2023-03-07
+* Changed:         2023-03-15
 *
 * */
 
 #ifndef LS_STD_SYSTEM_TIME_HPP
 #define LS_STD_SYSTEM_TIME_HPP
 
+#include "SystemTimeParameter.hpp"
+#include <cstdint>
 #include <ls-std/core/Class.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
+#include <memory>
 
 namespace ls::std::time
 {
@@ -19,10 +22,17 @@ namespace ls::std::time
   {
     public:
 
+      explicit SystemTime(const ::std::shared_ptr<ls::std::time::SystemTimeParameter> &_parameter);
       SystemTime();
       ~SystemTime() noexcept override;
 
-      bool set(uint64_t _timeStamp);
+      [[nodiscard]] bool set(uint32_t _timeStamp);
+
+    private:
+
+      ::std::shared_ptr<ls::std::time::SystemTimeParameter> parameter{};
+
+      void _generateParameter();
   };
 }
 

+ 35 - 0
include/ls-std/time/system-time/SystemTimeParameter.hpp

@@ -0,0 +1,35 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#ifndef LS_STD_SYSTEM_TIME_PARAMETER_HPP
+#define LS_STD_SYSTEM_TIME_PARAMETER_HPP
+
+#include "IPosixClock.hpp"
+#include <ls-std/os/dynamic-goal.hpp>
+#include <memory>
+
+namespace ls::std::time
+{
+  class LS_STD_DYNAMIC_GOAL SystemTimeParameter
+  {
+    public:
+
+      SystemTimeParameter();
+      ~SystemTimeParameter();
+
+      [[nodiscard]] ::std::shared_ptr<ls::std::time::IPosixClock> getPosixClock();
+      void setPosixClock(const ::std::shared_ptr<ls::std::time::IPosixClock> &_posixClock);
+
+    private:
+
+      ::std::shared_ptr<ls::std::time::IPosixClock> posixClock{};
+  };
+}
+
+#endif

+ 16 - 0
source/ls-std/time/system-time/IPosixClock.cpp

@@ -0,0 +1,16 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#include <ls-std/time/system-time/IPosixClock.hpp>
+
+using ls::std::time::IPosixClock;
+
+IPosixClock::IPosixClock() = default;
+
+IPosixClock::~IPosixClock() = default;

+ 23 - 0
source/ls-std/time/system-time/PosixClock.cpp

@@ -0,0 +1,23 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#include <ctime>
+#include <ls-std/time/system-time/PosixClock.hpp>
+
+using ls::std::time::PosixClock;
+
+PosixClock::PosixClock() = default;
+
+PosixClock::~PosixClock() noexcept = default;
+
+bool PosixClock::setTime(uint32_t _timeStamp)
+{
+  timespec timespec{_timeStamp, 0};
+  return clock_settime(CLOCK_REALTIME, &timespec) == 0;
+}

+ 47 - 0
source/ls-std/time/system-time/SystemTime.cpp

@@ -0,0 +1,47 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#include <ls-std/core/Class.hpp>
+#include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
+#include <ls-std/time/system-time/PosixClock.hpp>
+#include <ls-std/time/system-time/SystemTime.hpp>
+#include <ls-std/time/system-time/SystemTimeParameter.hpp>
+#include <memory>
+
+using ls::std::core::Class;
+using ls::std::core::NullPointerArgumentEvaluator;
+using ls::std::time::PosixClock;
+using ls::std::time::SystemTime;
+using ls::std::time::SystemTimeParameter;
+using std::make_shared;
+using std::shared_ptr;
+
+SystemTime::SystemTime(const shared_ptr<SystemTimeParameter> &_parameter) : SystemTime()
+{
+  NullPointerArgumentEvaluator{_parameter}.evaluate();
+  this->parameter = _parameter;
+}
+
+SystemTime::SystemTime() : Class("SystemTime")
+{
+  this->_generateParameter();
+}
+
+SystemTime::~SystemTime() noexcept = default;
+
+bool SystemTime::set(uint32_t _timeStamp)
+{
+  return this->parameter->getPosixClock()->setTime(_timeStamp);
+}
+
+void SystemTime::_generateParameter()
+{
+  this->parameter = make_shared<SystemTimeParameter>();
+  this->parameter->setPosixClock(make_shared<PosixClock>());
+}

+ 30 - 0
source/ls-std/time/system-time/SystemTimeParameter.cpp

@@ -0,0 +1,30 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#include <ls-std/time/system-time/IPosixClock.hpp>
+#include <ls-std/time/system-time/SystemTimeParameter.hpp>
+#include <memory>
+
+using ls::std::time::IPosixClock;
+using ls::std::time::SystemTimeParameter;
+using std::shared_ptr;
+
+SystemTimeParameter::SystemTimeParameter() = default;
+
+SystemTimeParameter::~SystemTimeParameter() = default;
+
+shared_ptr<IPosixClock> SystemTimeParameter::getPosixClock()
+{
+  return this->posixClock;
+}
+
+void SystemTimeParameter::setPosixClock(const shared_ptr<IPosixClock> &_posixClock)
+{
+  this->posixClock = _posixClock;
+}

+ 1 - 2
test/cases/io/evaluator/FileExistenceEvaluatorTest.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-21
-* Changed:         2023-02-23
+* Changed:         2023-03-15
 *
 * */
 
@@ -46,7 +46,6 @@ namespace
           {
             string actual = _exception.what();
             string expected = _exception.getName() + " thrown - \"" + GetParam() + "\" does not exist!";
-            ;
 
             ASSERT_STREQ(expected.c_str(), actual.c_str());
             throw;

+ 43 - 0
test/cases/time/system-time/SystemTimeParameterTest.cpp

@@ -0,0 +1,43 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-time.hpp>
+#include <memory>
+
+using ls::std::time::PosixClock;
+using ls::std::time::SystemTimeParameter;
+using std::make_shared;
+using std::shared_ptr;
+using testing::Test;
+
+namespace
+{
+  class SystemTimeParameterTest : public Test
+  {
+    public:
+
+      SystemTimeParameterTest() = default;
+      ~SystemTimeParameterTest() override = default;
+  };
+
+  TEST_F(SystemTimeParameterTest, getPosixClock)
+  {
+    ASSERT_TRUE(SystemTimeParameter{}.getPosixClock() == nullptr);
+  }
+
+  TEST_F(SystemTimeParameterTest, setPosixClock)
+  {
+    SystemTimeParameter parameter{};
+    shared_ptr<PosixClock> posixClock = make_shared<PosixClock>();
+    parameter.setPosixClock(posixClock);
+
+    ASSERT_TRUE(parameter.getPosixClock() == posixClock);
+  }
+}

+ 71 - 0
test/cases/time/system-time/SystemTimeTest.cpp

@@ -0,0 +1,71 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#include <cstdint>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <ls-std-time-test.hpp>
+#include <ls-std/ls-std-core.hpp>
+#include <ls-std/ls-std-time.hpp>
+#include <memory>
+
+using ls::std::core::IllegalArgumentException;
+using ls::std::time::SystemTime;
+using ls::std::time::SystemTimeParameter;
+using std::make_shared;
+using std::shared_ptr;
+using test::time::MockPosixClock;
+using testing::AtLeast;
+using testing::Return;
+using testing::Test;
+
+namespace
+{
+  class SystemTimeTest : public Test
+  {
+    public:
+
+      SystemTimeTest() = default;
+      ~SystemTimeTest() override = default;
+  };
+
+  TEST_F(SystemTimeTest, constructor)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            SystemTime systemTime{nullptr};
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
+  TEST_F(SystemTimeTest, getClassName)
+  {
+    ASSERT_STREQ("SystemTime", SystemTime{}.getClassName().c_str());
+  }
+
+  TEST_F(SystemTimeTest, setTime)
+  {
+    shared_ptr<SystemTimeParameter> parameter = make_shared<SystemTimeParameter>();
+    shared_ptr<MockPosixClock> posixClock = make_shared<MockPosixClock>();
+    parameter->setPosixClock(posixClock);
+    uint32_t birthday = 612694333;
+
+    EXPECT_CALL(*posixClock, setTime(birthday)).Times(AtLeast(1));
+    ON_CALL(*posixClock, setTime(birthday)).WillByDefault(Return(true));
+
+    ASSERT_TRUE(SystemTime{parameter}.set(birthday));
+  }
+}

+ 16 - 0
test/classes/time/system-time/MockPosixClock.cpp

@@ -0,0 +1,16 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#include "MockPosixClock.hpp"
+
+using test::time::MockPosixClock;
+
+MockPosixClock::MockPosixClock() = default;
+
+MockPosixClock::~MockPosixClock() noexcept = default;

+ 29 - 0
test/classes/time/system-time/MockPosixClock.hpp

@@ -0,0 +1,29 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#ifndef LS_STD_MOCK_POSIX_CLOCK_HPP
+#define LS_STD_MOCK_POSIX_CLOCK_HPP
+
+#include <gmock/gmock.h>
+#include <ls-std/time/system-time/IPosixClock.hpp>
+
+namespace test::time
+{
+  class MockPosixClock : public ls::std::time::IPosixClock
+  {
+    public:
+
+      MockPosixClock();
+      ~MockPosixClock() noexcept override;
+
+      MOCK_METHOD(bool, setTime, (uint32_t _timeStamp), (override));
+  };
+}
+
+#endif

+ 15 - 0
test/ls-std-time-test.hpp

@@ -0,0 +1,15 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-03-15
+* Changed:         2023-03-15
+*
+* */
+
+#ifndef LS_STD_LS_STD_TIME_TEST_HPP
+#define LS_STD_LS_STD_TIME_TEST_HPP
+
+#include <classes/time/system-time/MockPosixClock.hpp>
+
+#endif