Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit tests for longer encodings #23

Merged
merged 1 commit into from
May 2, 2021
Merged
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
unit tests for longer encodings
  • Loading branch information
charlesnicholson committed May 2, 2021
commit f4cc256a44864f20056992f9d620043fc06a1f72
65 changes: 59 additions & 6 deletions tests/test_cobs_encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,71 @@ TEST_CASE("Simple encodings", "[cobs_encode]") {
}
}

TEST_CASE("0xFF single code-block case", "[cobs_encode]") {
byte_vec_t dec(254, 0x01);
byte_vec_t enc( cobs_encode_max(static_cast< unsigned >(dec.size())) );
namespace {
byte_vec_t encode(byte_vec_t const &decoded) {
byte_vec_t enc(cobs_encode_max(static_cast< unsigned >(decoded.size())));
unsigned enc_len;
REQUIRE( cobs_encode(dec.data(),
static_cast< unsigned >(dec.size()),
REQUIRE( cobs_encode(decoded.data(),
static_cast< unsigned >(decoded.size()),
enc.data(),
static_cast< unsigned >(enc.size()),
&enc_len) == COBS_RET_SUCCESS );
return byte_vec_t(enc.begin(), enc.begin() + enc_len);
}
}

TEST_CASE("0xFF single code-block case", "[cobs_encode]") {
byte_vec_t expected(255, 0x01);
expected[0] = 0xFF;
expected.push_back(0x00);
REQUIRE ( enc == expected );
REQUIRE( encode(byte_vec_t(254, 0x01)) == expected );
}

TEST_CASE("Longer payloads", "[cobs_encode]") {
SECTION("255 non-zero bytes") {
byte_vec_t expected{0xFF};
expected.insert(std::end(expected), 254, 0x01);
expected.push_back(0x02); // code: distance to next block
expected.push_back(0x01);
expected.push_back(0x00);
REQUIRE( encode(byte_vec_t(255, 0x01)) == expected );
}

SECTION("255 zero bytes") {
byte_vec_t expected(256, 0x01);
expected.push_back(0x00);
REQUIRE( encode(byte_vec_t(255, 0x00)) == expected );
}

SECTION("1024 non-zero bytes") {
byte_vec_t expected;
for (auto i = 0u; i < 1024 / 254; ++i) {
expected.push_back(0xFF);
expected.insert(std::end(expected), 254, '!');
}
expected.push_back((1024 % 254) + 1);
expected.insert(std::end(expected), (1024 % 254), '!');
expected.push_back(0x00);
REQUIRE( encode(byte_vec_t(1024, '!')) == expected );
}

SECTION("1024 zero bytes") {
byte_vec_t expected(1025, 0x01);
expected.push_back(0x00);
REQUIRE( encode(byte_vec_t(1024, 0x00)) == expected );
}

SECTION("1024 every other byte is zero") {
byte_vec_t dec;
for (auto i = 0u; i < 1024; ++i) {
dec.push_back(i & 1);
}

byte_vec_t expected;
for (auto i = 0u; i <= 1024; ++i) {
expected.push_back((i & 1) ? 2 : 1);
}
expected.push_back(0x00);
REQUIRE( encode(dec) == expected );
}
}