Skip to content

Commit

Permalink
Part of theheraldproject#89. Added tests for newer datatype methods
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Fowler <adam@adamfowler.org>
  • Loading branch information
adamfowleruk committed Nov 13, 2021
1 parent 0a73a8f commit 2f40078
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
81 changes: 81 additions & 0 deletions herald-tests/data-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,25 @@ TEST_CASE("datatypes-data-ctor-fromhexstring", "[datatypes][data][ctor][fromhexs
}
}

TEST_CASE("datatypes-data-ctor-fromhexstring-trimmed", "[datatypes][data][ctor][fromhexstring]") {
SECTION("datatypes-data-ctor-fromhexstring-trimmed") {
const std::string hex = "8010ff0ffcc";
herald::datatype::Data d = herald::datatype::Data::fromHexEncodedString(hex);
const std::string finalhex = d.hexEncodedString();
INFO("Data: fromHexEncodedString: from: " << hex << ", to: " << finalhex);

REQUIRE(d.size() == 6);
REQUIRE(d.at(0) == std::byte(8));
REQUIRE(d.at(1) == std::byte(1));
REQUIRE(std::size_t(std::uint8_t(0x0f)) == std::size_t(15));
// REQUIRE(std::uint8_t(0x0f) == std::uint8_t('f'));
REQUIRE(d.at(2) == std::byte(15));
REQUIRE(d.at(3) == std::byte(240));
REQUIRE(d.at(4) == std::byte(255));
REQUIRE(d.at(5) == std::byte(204));
}
}

// TODO uppercase fex encoding test
// TODO mixedcase fex encoding test
// TODO hex string with odd number of chars (leading 0 removed)
Expand Down Expand Up @@ -253,6 +272,15 @@ TEST_CASE("datatypes-data-append", "[datatypes][data][append]") {
REQUIRE(d.at(14) == std::byte(0x42));
REQUIRE(d.at(13) == std::byte(0x40));
REQUIRE(r64 == u64);

bool r8notok = d.uint8(64,r8);
bool r16notok = d.uint16(63,r16);
bool r32notok = d.uint32(61,r32);
bool r64notok = d.uint64(57,r64);
REQUIRE(!r8notok);
REQUIRE(!r16notok);
REQUIRE(!r32notok);
REQUIRE(!r64notok);
}
}

Expand All @@ -271,6 +299,39 @@ TEST_CASE("datatypes-data-append-data", "[datatypes][data][append-data]") {
}
}

TEST_CASE("datatypes-data-append-data-offset", "[datatypes][data][append-data][offset]") {
SECTION("datatypes-data-append-data-offset") {
herald::datatype::Data d{std::byte('a'),6}; // 6
herald::datatype::Data dFrom{std::byte(8),12}; // 12

d.append(dFrom,4,8);

REQUIRE(d.size() == 14);

REQUIRE(d.at(5) == std::byte('a'));

std::uint8_t b;
bool ok = d.uint8(11, b);
REQUIRE(ok);
REQUIRE(std::uint8_t(8) == b);
}
}

TEST_CASE("datatypes-data-assign-data", "[datatypes][data][assign-data]") {
SECTION("datatypes-data-assign-data") {
herald::datatype::Data d{std::byte('a'),6}; // 6
herald::datatype::Data dFrom{std::byte(8),12}; // 12

d.assign(dFrom);

REQUIRE(d.size() == 12);
std::uint8_t b;
bool ok = d.uint8(11, b);
REQUIRE(ok);
REQUIRE(std::uint8_t(8) == b);
}
}

TEST_CASE("datatypes-data-reversed", "[datatypes][data][reversed]") {
SECTION("datatypes-data-reversed") {
const uint8_t bytes[] = {0,1,2,3};
Expand Down Expand Up @@ -576,6 +637,21 @@ TEST_CASE("datatypes-data-append-data-reversed-mthd-valid", "[datatypes][data][a
}


TEST_CASE("datatypes-data-append-data-reversed-offset-toolarge", "[datatypes][data][append][data][reversed][offset][toolarge]") {
SECTION("datatypes-data-append-data-reversed-offset-toolarge") {
uint8_t initial[] = {0,1,2,3,4,5,6,7};
herald::datatype::Data d(initial, 8);
uint8_t byteArray[] = {0,1,2,3,4,5,6,7};
herald::datatype::Data more(byteArray,8);

d.appendReversed(more,more.size(),1);
REQUIRE(d.size() == 8);
REQUIRE(d.at(0) == std::byte(0));
REQUIRE(d.at(7) == std::byte(7));
}
}



TEST_CASE("datatypes-data-equals", "[datatypes][data][equals]") {
SECTION("datatypes-data-equals") {
Expand All @@ -586,6 +662,8 @@ TEST_CASE("datatypes-data-equals", "[datatypes][data][equals]") {
herald::datatype::Data d2(byteArray,8);
uint8_t byteArray2[] = {4,4,4,4,4,4,4,4};
herald::datatype::Data d3(byteArray2,8);
uint8_t byteArray3[] = {4,4,4,4,4,4,4,4,5};
herald::datatype::Data d4(byteArray3,9);

REQUIRE(d1.size() == d2.size());
REQUIRE(d1.at(0) == d2.at(0));
Expand All @@ -600,5 +678,8 @@ TEST_CASE("datatypes-data-equals", "[datatypes][data][equals]") {
REQUIRE(d2 != d3);
REQUIRE(d3 != d1);

REQUIRE(d4 != d3);
REQUIRE(!(d4 == d3));
REQUIRE(((d4 > d3) & (d3 < d4)) | ((d3 > d4) & (d4 < d3)));
}
}
30 changes: 30 additions & 0 deletions herald-tests/memoryarena-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ TEST_CASE("memoryarena-set","[memoryarena][set]") {
}
}

TEST_CASE("memoryarena-set-uninitialised","[memoryarena][set][uninitialised") {
SECTION("memoryarena-set-uninitialised") {
DummyLoggingSink dls;
DummyBluetoothStateManager dbsm;
herald::DefaultPlatformType dpt;
herald::Context ctx(dpt,dls,dbsm); // default context include

herald::util::ByteArrayPrinter bap(ctx);

herald::datatype::MemoryArena<96,10> arena;
herald::datatype::MemoryArenaEntry notInitialised;

REQUIRE(0 == notInitialised.byteLength);
REQUIRE(arena.pagesFree() == 10);

arena.set(notInitialised, 2, 'a');

REQUIRE(0 == notInitialised.byteLength);
REQUIRE(arena.pagesFree() == 10);

REQUIRE('\0' == arena.get(notInitialised,2));
}
}

TEST_CASE("memoryarena-reserve","[memoryarena][reserve]") {
SECTION("memoryarena-reserve") {
herald::datatype::MemoryArena<2048,10> arena;
Expand Down Expand Up @@ -143,5 +167,11 @@ TEST_CASE("memoryarena-entry-rawlocation","[memoryarena][entry][rawlocation]") {
arena.rawCopy(buffer,offsetIdx * 16);
bap.print(buffer, offsetIdx * 16);
}

// Now try copy into a buffer bigger than our arena
std::array<unsigned char,72> largeBuffer;
largeBuffer[71] = ((unsigned char)8);
arena.rawCopy(largeBuffer,0);
REQUIRE(largeBuffer[71] == ((unsigned char)0));
}
}
2 changes: 1 addition & 1 deletion herald/include/herald/datatype/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class DataRef {
/// \brief Appends the specified DataRef to this one, but in its reverse order
void appendReversed(const DataRef& rawData, std::size_t offset, std::size_t length)
{
if (offset > rawData.size()) {
if (offset >= rawData.size()) {
return; // append nothing - out of range
}
std::size_t checkedLength = length;
Expand Down
2 changes: 1 addition & 1 deletion herald/include/herald/datatype/memory_arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class MemoryArena {
}
}
// we may have ran out of bytes in the arena, so just return empty bytes in the destination array
for (;pos < len;++pos) {
for (;pos < toPopulate.size();++pos) {
toPopulate[pos] = std::numeric_limits<unsigned char>::min();
}
}
Expand Down

0 comments on commit 2f40078

Please sign in to comment.