Commit 14f87d50 authored by Steven Murray's avatar Steven Murray
Browse files

Added constructor cta::ByteArray::ByteArray(const uint32_t value)

parent ced96bfb
......@@ -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;
......
......@@ -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.
*/
......
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment