Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
RubBra committed Sep 9, 2024
1 parent a69eb8c commit 330aa47
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/mwr/utils/ihex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ static inline u8 ihex_byte(const string& line, size_t off) {
template <typename T>
static inline T vec_to(const std::vector<u8>& vec) {
constexpr size_t num_bytes = sizeof(T);
MWR_ERROR_ON(num_bytes > vec.size(),
"reading beyond given vector: %lu > %lu", num_bytes,
vec.size());
MWR_ERROR_ON(num_bytes > vec.size(), "reading beyond given vector");
T result = 0;
for (size_t i = 0; i < num_bytes; ++i)
result = (result << 8) | vec[i];
Expand Down Expand Up @@ -125,6 +123,7 @@ ihex::ihex(const string& filename): m_start_addr(), m_records() {
seg_offset = vec_to<u16>(ihex_rec.data) << 4;
break;
case IHEX_START_SEG:
case IHEX_START_LIN_ADDR:
m_start_addr = vec_to<u32>(ihex_rec.data);
break;
case IHEX_EX_LIN_ADDR:
Expand All @@ -134,7 +133,9 @@ ihex::ihex(const string& filename): m_start_addr(), m_records() {
break;
}
}
MWR_REPORT_ON(m_records.size() == 0, "File '%s' does not seem to be in intel hex format", filename.c_str());
MWR_REPORT_ON(m_records.size() == 0,
"File '%s' does not seem to be in intel hex format",
filename.c_str());
}

} // namespace mwr
7 changes: 7 additions & 0 deletions test/resources/sample.ihex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:04000005000000CD2A //start address = 0x000000CD
:10010000214601360121470136007EFE09D2190140
:100110002146017E17C20001FF5F16002148011928
:020000040800F2//extended linear address by 0x800 << 16
:10012000194E79234623965778239EDA3F01B2CAA7
:100130003F0156702B5E712B722B732146013421C7
:00000001FF//eof
1 change: 1 addition & 0 deletions test/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ util_test(modules)
util_test(options)
util_test(socket)
util_test(srec)
util_test(ihex)
util_test(terminal)
util_test(uimage)
44 changes: 44 additions & 0 deletions test/utils/ihex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/******************************************************************************
* *
* Copyright (C) 2024 MachineWare GmbH *
* All Rights Reserved *
* *
* This is work is licensed under the terms described in the LICENSE file *
* found in the root directory of this source tree. *
* *
******************************************************************************/

#include "testing.h"

#include "mwr.h"

using namespace mwr;

const vector<u8> V1 = { 0x21, 0x46, 0x01, 0x36, 0x01, 0x21, 0x47, 0x01,
0x36, 0x00, 0x7E, 0xFE, 0x09, 0xD2, 0x19, 0x01 };

const vector<u8> V2 = { 0x21, 0x46, 0x01, 0x7E, 0x17, 0xC2, 0x00, 0x01,
0xFF, 0x5F, 0x16, 0x00, 0x21, 0x48, 0x01, 0x19 };

const vector<u8> V3 = { 0x19, 0x4E, 0x79, 0x23, 0x46, 0x23, 0x96, 0x57,
0x78, 0x23, 0x9E, 0xDA, 0x3F, 0x01, 0xB2, 0xCA };

const vector<u8> V4 = { 0x3F, 0x01, 0x56, 0x70, 0x2B, 0x5E, 0x71, 0x2B,
0x72, 0x2B, 0x73, 0x21, 0x46, 0x01, 0x34, 0x21 };

TEST(ihex, load) {
ihex reader(get_resource_path("sample.ihex"));

EXPECT_EQ(reader.start_addr(), 0xcd);
ASSERT_EQ(reader.records().size(), 4);

EXPECT_EQ(reader.records()[0].addr, 0x0100);
EXPECT_EQ(reader.records()[1].addr, 0x0110);
EXPECT_EQ(reader.records()[2].addr, 0x0120 | 0x800 << 16);
EXPECT_EQ(reader.records()[3].addr, 0x0130 | 0x800 << 16);

EXPECT_EQ(reader.records()[0].data, V1);
EXPECT_EQ(reader.records()[1].data, V2);
EXPECT_EQ(reader.records()[2].data, V3);
EXPECT_EQ(reader.records()[3].data, V4);
}

0 comments on commit 330aa47

Please sign in to comment.