Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions test/read_api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,34 @@ TEST_CASE("Write complicated, nested data format, then read it")
ulog_cpp::AccessException);
}

TEST_CASE("Read string values that fill the whole field or are empty")
{
std::vector<uint8_t> written_data;
TestWriter writer([&](const uint8_t* data, int length) {
const int prev_size = written_data.size();
written_data.resize(written_data.size() + length);
memcpy(written_data.data() + prev_size, data, length);
});

// ULog strings carry no terminating zero, and PX4 logs contain empty ones (char[0])
writer.fileHeader(ulog_cpp::FileHeader{});
writer.messageInfo(ulog_cpp::MessageInfo{"empty", ""});
writer.messageInfo(ulog_cpp::MessageInfo{"full", "hello"});
writer.headerComplete();
CHECK_EQ(writer.num_errors, 0);

const auto data_container =
std::make_shared<ulog_cpp::DataContainer>(ulog_cpp::DataContainer::StorageConfig::FullLog);
ulog_cpp::Reader reader{data_container};
reader.readChunk(written_data.data(), written_data.size());
CHECK(data_container->parsingErrors().empty());
CHECK_FALSE(data_container->hadFatalError());

const auto& info = data_container->messageInfo();
CHECK_EQ(info.at("empty").field().arrayLength(), 0);
CHECK_EQ(info.at("empty").value().as<std::string>(), "");
CHECK_EQ(info.at("full").field().arrayLength(), 5);
CHECK_EQ(info.at("full").value().as<std::string>(), "hello");
}

TEST_SUITE_END();
2 changes: 1 addition & 1 deletion ulog_cpp/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static inline ReturnType staticCastEnsureUnsignedChar(const OriginType& v)
inline int strnlen(const uint8_t* ptr, int max_len)
{
int len = 0;
while ((*ptr != 0) && (len < max_len)) {
while ((len < max_len) && (*ptr != 0)) {
ptr++;
len++;
}
Expand Down
Loading