Просмотр исходного кода

Add test for Base64 class (incomplete)

Patrick-Christopher Mattulat 3 лет назад
Родитель
Сommit
040f1938bf

+ 2 - 1
CMakeLists.txt

@@ -155,7 +155,8 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/io/xml/XmlParserTestWrapper.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/base/LibraryVersionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/logging/LogLevelTest.cpp
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/StandardOutputWriterTest.cpp)
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/StandardOutputWriterTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/encoding/base64/Base64Test.cpp)
 endif ()
 
 ##########################################################

+ 7 - 2
include/ls_std/encoding/base64/Base64.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-01-03
- * Changed:         2022-01-03
+ * Changed:         2022-01-08
  *
  * */
 
@@ -11,6 +11,7 @@
 #define LS_STD_BASE64_HPP
 
 #include <ls_std/encoding/IEncoding.hpp>
+#include <bitset>
 #include <vector>
 
 namespace ls_std
@@ -22,6 +23,8 @@ namespace ls_std
       Base64() = default;
       ~Base64() = default;
 
+      // implementation
+
       std::string encode(const std::string &_sequence) override;
       std::string decode(const std::string &_sequence) override;
 
@@ -39,7 +42,9 @@ namespace ls_std
         '4','5','6','7','8','9','+','/'
       };
 
-      static std::string _getSubSequence(const std::string& _sequence, size_t index);
+      static std::bitset<24> _getBitSequenceFromSequence(const std::string &basicString);
+      static std::string _getEncodingFromSubSequence(const std::string& basicString);
+      static std::string _getNextByteTriple(const std::string& _sequence, size_t index);
   };
 }
 

+ 12 - 9
include/ls_std/ls_std.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-10-29
- * Changed:         2021-09-18
+ * Changed:         2022-01-08
  *
  * */
 
@@ -27,6 +27,15 @@
 #include "boxing/Long.hpp"
 #include "boxing/String.hpp"
 
+#include "encoding/base64/Base64.hpp"
+#include "encoding/IEncoding.hpp"
+
+#include "event/Event.hpp"
+#include "event/EventTypes.hpp"
+#include "event/EventHandler.hpp"
+#include "event/IEventSubscriber.hpp"
+#include "event/EventManager.hpp"
+
 #include "exception/EventNotHandledException.hpp"
 #include "exception/EventNotSubscribedException.hpp"
 #include "exception/FileNotFoundException.hpp"
@@ -36,6 +45,8 @@
 #include "exception/IncompleteJsonException.hpp"
 #include "exception/NullPointerException.hpp"
 
+#include "factory/IFactory.hpp"
+
 #include "io/logging/LogLevel.hpp"
 #include "io/logging/LogLevelValue.hpp"
 #include "io/logging/Logger.hpp"
@@ -84,12 +95,4 @@
 #include "utils/WindowsUtils.hpp"
 #endif
 
-#include "event/Event.hpp"
-#include "event/EventTypes.hpp"
-#include "event/EventHandler.hpp"
-#include "event/IEventSubscriber.hpp"
-#include "event/EventManager.hpp"
-
-#include "factory/IFactory.hpp"
-
 #endif

+ 27 - 6
source/ls_std/encoding/base64/Base64.cpp

@@ -3,11 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-01-03
- * Changed:         2022-01-03
+ * Changed:         2022-01-08
  *
  * */
 
 #include <ls_std/encoding/base64/Base64.hpp>
+#include <bitset>
+#include <sstream>
 
 std::string ls_std::Base64::encode(const std::string &_sequence)
 {
@@ -15,8 +17,8 @@ std::string ls_std::Base64::encode(const std::string &_sequence)
 
   for(size_t index = 0 ; index < _sequence.size() ; index += 3)
   {
-    subSequence = ls_std::Base64::_getSubSequence(_sequence, index);
-    encodedString +=
+    subSequence = ls_std::Base64::_getNextByteTriple(_sequence, index);
+    encodedString += ls_std::Base64::_getEncodingFromSubSequence(subSequence);
   }
 
   return encodedString;
@@ -27,19 +29,38 @@ std::string ls_std::Base64::decode(const std::string &_sequence)
   return "";
 }
 
-std::string ls_std::Base64::_getSubSequence(const std::string &_sequence, size_t index)
+std::bitset<24> ls_std::Base64::_getBitSequenceFromSequence(const std::string &basicString)
+{
+  uint32_t bits{};
+  std::stringstream stringStream{basicString};
+  stringStream >> bits;
+  std::bitset<32> bitSequenceBuffer{bits};
+  std::bitset<24> bitSequence{};
+
+  return bitSequence;
+}
+
+std::string ls_std::Base64::_getEncodingFromSubSequence(const std::string& basicString)
+{
+  std::string encodingString{};
+  std::bitset<24> bitSequence = ls_std::Base64::_getBitSequenceFromSequence(basicString);
+
+  return encodingString;
+}
+
+std::string ls_std::Base64::_getNextByteTriple(const std::string &_sequence, size_t index)
 {
   std::string subSequence{_sequence[index]};
   size_t size = _sequence.size();
 
   if((index + 1) < size)
   {
-    subSequence += (_sequence[size+1]);
+    subSequence += (_sequence[index+1]);
   }
 
   if((index + 2) < size)
   {
-    subSequence += (_sequence[size+2]);
+    subSequence += (_sequence[index+2]);
   }
 
   return subSequence;

+ 36 - 0
test/cases/encoding/base64/Base64Test.cpp

@@ -0,0 +1,36 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2022-01-08
+ * Changed:         2022-01-08
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include <ls_std/ls_std.hpp>
+
+namespace
+{
+  class Base64Test : public ::testing::Test
+  {
+    protected:
+
+      Base64Test() = default;
+      ~Base64Test() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(Base64Test, encode)
+  {
+    ls_std::Base64 base64{};
+    std::string sequence = "abc";
+
+    ASSERT_STREQ("YWJj", base64.encode(sequence).c_str());
+  }
+}