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
18 changes: 18 additions & 0 deletions components/cobs/example/main/cobs_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,24 @@ void test_edge_cases(espp::Logger &logger) {
decoded.size(), large_packet.size(), encoded.size());
}
}

// Test 5: Packet of exactly 255 bytes with no zeros in the data
{
std::vector<uint8_t> block_len_packet(255, 0x42);

std::vector<uint8_t> encoded = Cobs::encode_packet(block_len_packet);
std::vector<uint8_t> decoded = Cobs::decode_packet(encoded);

bool success = (decoded.size() == block_len_packet.size()) &&
(std::memcmp(decoded.data(), block_len_packet.data(), block_len_packet.size()) == 0);
if (success) {
logger.info("Test 5: PASS - Packet of exactly 255 bytes with no zeros");
} else {
logger.error(
"Test 5: FAIL - Packet of exactly 255 bytes with no zeros (decoded: {}, expected: {}, encoded: {})",
decoded.size(), block_len_packet.size(), encoded.size());
}
}
}

extern "C" void app_main(void) {
Expand Down
3 changes: 1 addition & 2 deletions components/cobs/src/cobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ size_t Cobs::encode_packet(std::span<const uint8_t> data, std::span<uint8_t> out
*codep = code;
code = 1;
codep = encode;
if (!byte)
++encode;
++encode;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the loop ends because of no more data, we still want to ready the next encoding position for the delimiter to be added.

}
}
*codep = code; // Write final code value
Expand Down
Loading