Skip to content
Snippets Groups Projects
Commit 909af644 authored by Steven Murray's avatar Steven Murray
Browse files

Added comparison operators to ByteArray

parent cce80246
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,8 @@
#include "common/ByteArray.hpp"
#include <ostream>
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
......@@ -85,6 +87,34 @@ cta::ByteArray &cta::ByteArray::operator=(const ByteArray &rhs) {
return *this;
}
//------------------------------------------------------------------------------
// operator==
//------------------------------------------------------------------------------
bool cta::ByteArray::operator==(const ByteArray &rhs) const {
if(this == &rhs) {
return true;
}
if(m_size != rhs.m_size) {
return false;
}
for(uint32_t i = 0; i < m_size; i++) {
if(m_bytes[i] != rhs.m_bytes[i]) {
return false;
}
}
return true;
}
//------------------------------------------------------------------------------
// operator!=
//------------------------------------------------------------------------------
bool cta::ByteArray::operator!=(const ByteArray &rhs) const {
return !operator==(rhs);
}
//------------------------------------------------------------------------------
// getSize
//------------------------------------------------------------------------------
......@@ -98,3 +128,20 @@ uint32_t cta::ByteArray::getSize() const throw() {
const uint8_t *cta::ByteArray::getBytes() const throw() {
return m_bytes;
}
//------------------------------------------------------------------------------
// operator<<
//------------------------------------------------------------------------------
std::ostream &operator<<(std::ostream &os, const cta::ByteArray &obj) {
os << "{";
const auto size = obj.getSize();
const auto bytes = obj.getBytes();
for(uint32_t i = 0; i < size; i++) {
os << bytes[i];
}
os << "}";
return os;
}
......@@ -86,6 +86,22 @@ public:
*/
ByteArray &operator=(const ByteArray &rhs);
/**
* Returns true if the specified right-hand side is equal to this object.
*
* @param rhs The object on the right-hand side of the == operator.
* @return True if the specified right-hand side is equal to this object.
*/
bool operator==(const ByteArray &rhs) const;
/**
* Returns true if the specified right-hand side is not euqal to this object.
*
* @param rhs The object on the right-hand side of the != operator.
* @return True if the specified right-hand side is not equal to this object.
*/
bool operator!=(const ByteArray &rhs) const;
/**
* Returns the size of the array in bytes.
*
......@@ -101,7 +117,7 @@ public:
* @return The contents of the array of bytes or NULL if the array is empty.
*/
const uint8_t *getBytes() const throw();
private:
/**
......@@ -117,3 +133,8 @@ private:
}; // class ByteArray
} // namespace cta
/**
* Output stream operator for the cta::ByteArray class.
*/
std::ostream &operator<<(std::ostream &os, const cta::ByteArray &obj);
......@@ -146,4 +146,64 @@ TEST_F(cta_ByteArrayTest, assignment_operator) {
ASSERT_NE(byteArray1.getBytes(), byteArray2.getBytes());
}
TEST_F(cta_ByteArrayTest, equality_operator_eq) {
using namespace cta;
const uint32_t arraySize1 = 4;
const uint8_t bytes1[4] = {10, 20, 30, 40};
const ByteArray byteArray1(arraySize1, bytes1);
ASSERT_EQ(arraySize1, byteArray1.getSize());
ASSERT_EQ((uint8_t)10, byteArray1.getBytes()[0]);
ASSERT_EQ((uint8_t)20, byteArray1.getBytes()[1]);
ASSERT_EQ((uint8_t)30, byteArray1.getBytes()[2]);
ASSERT_EQ((uint8_t)40, byteArray1.getBytes()[3]);
const uint32_t arraySize2 = 4;
const uint8_t bytes2[4] = {10, 20, 30, 40};
ByteArray byteArray2(arraySize2, bytes2);
ASSERT_EQ(arraySize2, byteArray2.getSize());
ASSERT_EQ((uint8_t)10, byteArray2.getBytes()[0]);
ASSERT_EQ((uint8_t)20, byteArray2.getBytes()[1]);
ASSERT_EQ((uint8_t)30, byteArray2.getBytes()[2]);
ASSERT_EQ((uint8_t)40, byteArray2.getBytes()[3]);
ASSERT_EQ(byteArray1, byteArray2);
}
TEST_F(cta_ByteArrayTest, equality_operator_ne) {
using namespace cta;
const uint32_t arraySize1 = 4;
const uint8_t bytes1[4] = {10,20,30,40};
const ByteArray byteArray1(arraySize1, bytes1);
ASSERT_EQ(arraySize1, byteArray1.getSize());
ASSERT_EQ((uint8_t)10, byteArray1.getBytes()[0]);
ASSERT_EQ((uint8_t)20, byteArray1.getBytes()[1]);
ASSERT_EQ((uint8_t)30, byteArray1.getBytes()[2]);
ASSERT_EQ((uint8_t)40, byteArray1.getBytes()[3]);
const uint32_t arraySize2 = 10;
const uint8_t bytes2[10] = {10, 9, 8, 7 ,6 , 5, 4, 3, 2, 1};
ByteArray byteArray2(arraySize2, bytes2);
ASSERT_EQ(arraySize2, byteArray2.getSize());
ASSERT_EQ((uint8_t)10, byteArray2.getBytes()[0]);
ASSERT_EQ( (uint8_t)9, byteArray2.getBytes()[1]);
ASSERT_EQ( (uint8_t)8, byteArray2.getBytes()[2]);
ASSERT_EQ( (uint8_t)7, byteArray2.getBytes()[3]);
ASSERT_EQ( (uint8_t)6, byteArray2.getBytes()[4]);
ASSERT_EQ( (uint8_t)5, byteArray2.getBytes()[5]);
ASSERT_EQ( (uint8_t)4, byteArray2.getBytes()[6]);
ASSERT_EQ( (uint8_t)3, byteArray2.getBytes()[7]);
ASSERT_EQ( (uint8_t)2, byteArray2.getBytes()[8]);
ASSERT_EQ( (uint8_t)1, byteArray2.getBytes()[9]);
ASSERT_NE(byteArray1, byteArray2);
}
} // namespace unitTests
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment