Skip to content

Commit

Permalink
chore: eliminate [in/out] parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
zfields committed Sep 8, 2023
1 parent 425c532 commit a64be28
Show file tree
Hide file tree
Showing 9 changed files with 434 additions and 233 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ target_compile_options(
-Wextra
-Wpedantic
-Werror
-Og
-ggdb
)
target_include_directories(
note_c
Expand Down
186 changes: 128 additions & 58 deletions n_helpers.c

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions note.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,22 @@ void NoteMD5HashToString(unsigned char *hash, char *strbuf, unsigned long buflen

// High-level helper functions that are both useful and serve to show developers
// how to call the API
#define NOTE_C_BINARY_RX_ALL 0
const char * NoteBinaryDataDecodedLength(uint32_t *len);
const char * NoteBinaryDataEncodedLength(uint32_t *len);
const char * NoteBinaryDataReset(void);
const char * NoteBinaryDecode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen);
uint8_t *outBuf, uint32_t outLen,
uint32_t *decLen);
const char * NoteBinaryEncode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen);
uint8_t *outBuf, uint32_t outLen,
uint32_t *encLen);
uint32_t NoteBinaryMaxDecodedLength(uint32_t bufferSize);
uint32_t NoteBinaryMaxEncodedLength(uint32_t unencodedLength);
const char * NoteBinaryReceive(uint8_t *buffer, uint32_t bufLen,
uint32_t decodedOffset, uint32_t *decodedLen);
uint32_t *dataLen);
const char * NoteBinaryReceiveRange(uint8_t *buffer, uint32_t bufLen,
uint32_t decodedOffset,
uint32_t decodedLen);
const char * NoteBinaryTransmit(uint8_t *unencodedData, uint32_t unencodedLen,
uint32_t bufLen, uint32_t notecardOffset);
uint32_t NoteSetSTSecs(uint32_t secs);
Expand Down
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ macro(add_test TEST_NAME)
PRIVATE note_c
PRIVATE Catch2::Catch2WithMain
)
target_compile_options(${TEST_NAME}
PRIVATE -Og
PRIVATE -ggdb
)

list(APPEND TEST_TARGETS ${TEST_NAME})

Expand Down Expand Up @@ -152,6 +156,7 @@ add_test(NoteBinaryEncode_test)
add_test(NoteBinaryMaxEncodedLength_test)
add_test(NoteBinaryMaxDecodedLength_test)
add_test(NoteBinaryReceive_test)
add_test(NoteBinaryReceiveRange_test)
add_test(NoteBinaryTransmit_test)

if(NOTE_C_COVERAGE)
Expand Down
22 changes: 11 additions & 11 deletions test/src/NoteBinaryDecode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,44 @@ DEFINE_FFF_GLOBALS
FAKE_VALUE_FUNC(uint32_t, cobsDecode, uint8_t *, uint32_t, uint8_t, uint8_t *)

uint8_t inBuf[12];
uint32_t inLen;
const uint32_t inLen = sizeof(inBuf);
uint8_t outBuf[10];
uint32_t outLen;
const uint32_t outLen = sizeof(outBuf);
uint32_t decLen;

namespace
{

SCENARIO("NoteBinaryDecode")
{
RESET_FAKE(cobsDecode);
uint32_t inLen = sizeof(inBuf);
uint32_t outLen = sizeof(outBuf);
decLen = 0;

GIVEN("Bad parameters are supplied") {
WHEN("inBuf is NULL") {
const char *err = NoteBinaryDecode(NULL, inLen, outBuf, &outLen);
const char *err = NoteBinaryDecode(NULL, inLen, outBuf, outLen, &decLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outBuf is NULL") {
const char *err = NoteBinaryDecode(inBuf, inLen, NULL, &outLen);
const char *err = NoteBinaryDecode(inBuf, inLen, NULL, outLen, &decLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outLen is NULL") {
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, NULL);
WHEN("decLen is NULL") {
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, outLen, NULL);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outLen is less than the size required for the worst-case decoding") {
uint32_t badOutLen = (cobsGuaranteedFit(inLen) - 1);
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, &badOutLen);
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, badOutLen, &decLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -70,7 +70,7 @@ SCENARIO("NoteBinaryDecode")
GIVEN("Parameters are in order") {
const uint32_t EXPECTED_RESULT = 79;
cobsDecode_fake.return_val = EXPECTED_RESULT;
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, &outLen);
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, outLen, &decLen);

THEN("cobsDecode is invoked") {
CHECK(cobsDecode_fake.call_count > 0);
Expand All @@ -84,7 +84,7 @@ SCENARIO("NoteBinaryDecode")
}

THEN("The result is returned without modification") {
CHECK(EXPECTED_RESULT == outLen);
CHECK(EXPECTED_RESULT == decLen);
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions test/src/NoteBinaryEncode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,44 @@ DEFINE_FFF_GLOBALS
FAKE_VALUE_FUNC(uint32_t, cobsEncode, uint8_t *, uint32_t, uint8_t, uint8_t *)

uint8_t inBuf[10] = "Hi there!";
uint32_t inLen;
uint32_t inLen = strlen((const char *)inBuf);
uint8_t outBuf[12];
uint32_t outLen;
uint32_t outLen = sizeof(outBuf);
uint32_t encLen;

namespace
{

SCENARIO("NoteBinaryEncode")
{
RESET_FAKE(cobsEncode);
uint32_t inLen = strlen((const char *)inBuf);
uint32_t outLen = sizeof(outBuf);
encLen = 0;

GIVEN("Bad parameters are supplied") {
WHEN("inBuf is NULL") {
const char *err = NoteBinaryEncode(NULL, inLen, outBuf, &outLen);
const char *err = NoteBinaryEncode(NULL, inLen, outBuf, outLen, &encLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outBuf is NULL") {
const char *err = NoteBinaryEncode(inBuf, inLen, NULL, &outLen);
const char *err = NoteBinaryEncode(inBuf, inLen, NULL, outLen, &encLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outLen is NULL") {
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, NULL);
WHEN("encLen is NULL") {
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, outLen, NULL);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outLen is less than the size required for in place encoding") {
uint32_t badOutLen = (cobsEncodedLength(inBuf, inLen) - 1);
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, &badOutLen);
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, badOutLen, &encLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -72,7 +72,7 @@ SCENARIO("NoteBinaryEncode")
GIVEN("Parameters are in order") {
const uint32_t EXPECTED_RESULT = 79;
cobsEncode_fake.return_val = EXPECTED_RESULT;
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, &outLen);
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, outLen, &encLen);

THEN("cobsEncode is invoked") {
CHECK(cobsEncode_fake.call_count > 0);
Expand All @@ -86,7 +86,7 @@ SCENARIO("NoteBinaryEncode")
}

THEN("The result is returned without modification") {
CHECK(EXPECTED_RESULT == outLen);
CHECK(EXPECTED_RESULT == encLen);
}
}
}
Expand Down
Loading

0 comments on commit a64be28

Please sign in to comment.