From da3d4646e7361cada7ff4735d9c39d7a04de1c7d Mon Sep 17 00:00:00 2001
From: Steven Murray <Steven.Murray@cern.ch>
Date: Mon, 7 Sep 2015 18:22:41 +0200
Subject: [PATCH] Moved getAdler32 from ByteArray to Utils

---
 common/Utils.cpp                  | 10 ++++++++++
 common/Utils.hpp                  | 10 ++++++++++
 common/UtilsTest.cpp              | 15 +++++++++++++++
 common/checksum/ByteArray.cpp     |  9 ---------
 common/checksum/ByteArray.hpp     |  5 -----
 common/checksum/ByteArrayTest.cpp | 13 -------------
 6 files changed, 35 insertions(+), 27 deletions(-)

diff --git a/common/Utils.cpp b/common/Utils.cpp
index ba6368a5a6..e153643989 100644
--- a/common/Utils.cpp
+++ b/common/Utils.cpp
@@ -29,6 +29,7 @@
 #include <strings.h>
 #include <sys/types.h>
 #include <uuid/uuid.h>
+#include <zlib.h>
 
 using cta::exception::Exception;
 
@@ -472,3 +473,12 @@ bool cta::Utils::isValidUInt(const std::string &str)
 
   return true;
 }
+
+//------------------------------------------------------------------------------
+// getAdler32
+//------------------------------------------------------------------------------
+uint32_t cta::Utils::getAdler32(const char *buf, const uint32_t len)
+  throw() {
+  const uint32_t checksum = adler32(0L, Z_NULL, 0);
+  return adler32(checksum, (const Bytef*)buf, len);
+}
diff --git a/common/Utils.hpp b/common/Utils.hpp
index 7d3bade4af..b5c1504f46 100644
--- a/common/Utils.hpp
+++ b/common/Utils.hpp
@@ -20,6 +20,7 @@
 
 #include <list>
 #include <sstream>
+#include <stdint.h>
 #include <string>
 #include <unistd.h>
 #include <vector>
@@ -207,6 +208,15 @@ public:
    */
   static bool isValidUInt(const std::string &str) throw();
 
+  /**
+   * Returns the alder32 checksum of the specified buffer.
+   *
+   * @param buf The buffer.
+   * @param len The length of the buffer in bytes.
+   * @return the alder32 checksum of the specified buffer.
+   */
+  static uint32_t getAdler32(const char *buf, const uint32_t len) throw();
+
 }; // class Utils
 
 } // namespace cta
diff --git a/common/UtilsTest.cpp b/common/UtilsTest.cpp
index f1f8bb7184..44e027e2fc 100644
--- a/common/UtilsTest.cpp
+++ b/common/UtilsTest.cpp
@@ -452,4 +452,19 @@ TEST_F(cta_UtilsTest, isValidUInt_not_a_number) {
   ASSERT_FALSE(Utils::isValidUInt("one"));
 }
 
+TEST_F(cta_UtilsTest, adler32_empty_buf) {
+  using namespace cta;
+
+  // The adler32 of an empty buffer is 1
+  ASSERT_EQ((uint32_t)1, Utils::getAdler32(NULL, 0));
+}
+
+TEST_F(cta_UtilsTest, adler32_buf_of_character_1) {
+  using namespace cta;
+
+  const char buf = '1';
+  ASSERT_EQ((uint32_t)0x320032, Utils::getAdler32(&buf, 1));
+}
+
+
 } // namespace unitTests
diff --git a/common/checksum/ByteArray.cpp b/common/checksum/ByteArray.cpp
index 7451825991..69562f7e21 100644
--- a/common/checksum/ByteArray.cpp
+++ b/common/checksum/ByteArray.cpp
@@ -19,7 +19,6 @@
 #include "common/checksum/ByteArray.hpp"
 
 #include <ostream>
-#include <zlib.h>
 
 //------------------------------------------------------------------------------
 // constructor
@@ -130,14 +129,6 @@ const uint8_t *cta::ByteArray::getBytes() const throw() {
   return m_bytes;
 }
 
-//------------------------------------------------------------------------------
-// getAdler32
-//------------------------------------------------------------------------------
-uint32_t cta::ByteArray::getAdler32() const {
-  const uint32_t checksum = adler32(0L, Z_NULL, 0);
-  return adler32(checksum, m_bytes, m_size);
-}
-
 //------------------------------------------------------------------------------
 // operator<<
 //------------------------------------------------------------------------------
diff --git a/common/checksum/ByteArray.hpp b/common/checksum/ByteArray.hpp
index 4dfd3aa361..c297323a3c 100644
--- a/common/checksum/ByteArray.hpp
+++ b/common/checksum/ByteArray.hpp
@@ -118,11 +118,6 @@ public:
    */
   const uint8_t *getBytes() const throw();
 
-  /**
-   * Returns the adler 32 checksum of the array of bytes.
-   */
-  uint32_t getAdler32() const;
-
 private:
 
   /**
diff --git a/common/checksum/ByteArrayTest.cpp b/common/checksum/ByteArrayTest.cpp
index f898130e1e..c84949be58 100644
--- a/common/checksum/ByteArrayTest.cpp
+++ b/common/checksum/ByteArrayTest.cpp
@@ -206,17 +206,4 @@ TEST_F(cta_ByteArrayTest, equality_operator_ne) {
   ASSERT_NE(byteArray1, byteArray2);
 }
 
-TEST_F(cta_ByteArrayTest, adler32_empty_array) {
-  // The adler32 of an empty buffer is 1
-  cta::ByteArray byteArray;
-  ASSERT_EQ((uint32_t)1, byteArray.getAdler32());
-}
-
-TEST_F(cta_ByteArrayTest, adler32_array_of_character_1) {
-  // The adler32 of an empty buffer is 1
-  const uint8_t c = '1';
-  cta::ByteArray byteArray(1, &c);
-  ASSERT_EQ((uint32_t)0x320032, byteArray.getAdler32());
-}
-
 } // namespace unitTests
-- 
GitLab