From 21e93b7e503a7d6cce96c9a3c0c266ef2b4f37b7 Mon Sep 17 00:00:00 2001 From: Steven Murray <Steven.Murray@cern.ch> Date: Mon, 7 Sep 2015 19:17:26 +0200 Subject: [PATCH] Added constructor cta::ByteArray::ByteArray(const uint32_t value) --- common/checksum/ByteArray.cpp | 17 ++++++++++++++++- common/checksum/ByteArray.hpp | 9 +++++++++ common/checksum/ByteArrayTest.cpp | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/common/checksum/ByteArray.cpp b/common/checksum/ByteArray.cpp index 69562f7e21..2f7aa67f37 100644 --- a/common/checksum/ByteArray.cpp +++ b/common/checksum/ByteArray.cpp @@ -19,6 +19,7 @@ #include "common/checksum/ByteArray.hpp" #include <ostream> +#include <strings.h> //------------------------------------------------------------------------------ // constructor @@ -51,7 +52,21 @@ cta::ByteArray::ByteArray(const std::string &bytes) { } //------------------------------------------------------------------------------ -// constructor +// copy constructor +//------------------------------------------------------------------------------ +cta::ByteArray::ByteArray(const uint32_t value) { + m_size = sizeof value; + m_bytes = new uint8_t[sizeof value]; + bzero(m_bytes, sizeof m_bytes); + + m_bytes[0] = value & 0xFF; + m_bytes[1] = value >> 8 & 0xFF; + m_bytes[2] = value >> 16 & 0xFF; + m_bytes[3] = value >> 24 & 0xFF; +} + +//------------------------------------------------------------------------------ +// copy constructor //------------------------------------------------------------------------------ cta::ByteArray::ByteArray(const ByteArray &other) { m_size = other.m_size; diff --git a/common/checksum/ByteArray.hpp b/common/checksum/ByteArray.hpp index c297323a3c..d4600543b5 100644 --- a/common/checksum/ByteArray.hpp +++ b/common/checksum/ByteArray.hpp @@ -69,6 +69,15 @@ public: */ ByteArray(const std::string &bytes); + /** + * Constructor. + * + * Copies the specified uint32_t into this object in little endian byte order. + * + * @param value The value. + */ + explicit ByteArray(const uint32_t value); + /** * Copy constructor. */ diff --git a/common/checksum/ByteArrayTest.cpp b/common/checksum/ByteArrayTest.cpp index c84949be58..4658e1ce1c 100644 --- a/common/checksum/ByteArrayTest.cpp +++ b/common/checksum/ByteArrayTest.cpp @@ -81,6 +81,20 @@ TEST_F(cta_ByteArrayTest, string_constructor) { ASSERT_EQ((uint8_t)'o', byteArray.getBytes()[4]); } +TEST_F(cta_ByteArrayTest, uint32_t_constructor) { + using namespace cta; + + const uint32_t value = 0x10203040; + const ByteArray byteArray(value); + + // uint32_t should be stored in little endian byte order + ASSERT_EQ((uint32_t)4, byteArray.getSize()); + ASSERT_EQ((uint8_t)0x40, byteArray.getBytes()[0]); + ASSERT_EQ((uint8_t)0x30, byteArray.getBytes()[1]); + ASSERT_EQ((uint8_t)0x20, byteArray.getBytes()[2]); + ASSERT_EQ((uint8_t)0x10, byteArray.getBytes()[3]); +} + TEST_F(cta_ByteArrayTest, copy_constructor) { using namespace cta; -- GitLab