Skip to content

Commit

Permalink
unit tests #sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Dec 4, 2024
1 parent 18ea9fe commit 2d834f8
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 7 deletions.
7 changes: 4 additions & 3 deletions libcanard/canard.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,14 @@ CANARD_PRIVATE int32_t txPushMultiFrame(struct CanardTxQueue* const que,

CANARD_PRIVATE void txPopAndFreeTransfer(struct CanardTxQueue* const que,
const struct CanardInstance* const ins,
struct CanardTxQueueItem* tx_item,
struct CanardTxQueueItem* const tx_item,
const bool drop_whole_transfer)
{
struct CanardTxQueueItem* curr_tx_item = tx_item;
struct CanardTxQueueItem* tx_item_to_free = NULL;
while (NULL != (tx_item_to_free = canardTxPop(que, tx_item)))
while (NULL != (tx_item_to_free = canardTxPop(que, curr_tx_item)))
{
tx_item = tx_item_to_free->next_in_transfer;
curr_tx_item = tx_item_to_free->next_in_transfer;
canardTxFree(que, ins, tx_item_to_free);

if (!drop_whole_transfer)
Expand Down
30 changes: 27 additions & 3 deletions tests/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
#include <array>
#include <atomic>
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <limits>
#include <mutex>
#include <numeric>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>

Expand Down Expand Up @@ -87,7 +91,7 @@ class TestAllocator
std::uint8_t* p = nullptr;
if ((amount > 0U) && ((getTotalAllocatedAmount() + amount) <= ceiling_))
{
const auto amount_with_canaries = amount + canary_.size() * 2U;
const auto amount_with_canaries = amount + (canary_.size() * 2U);
// Clang-tidy complains about manual memory management. Suppressed because we need it for testing purposes.
p = static_cast<std::uint8_t*>(std::malloc(amount_with_canaries)); // NOLINT
if (p == nullptr)
Expand Down Expand Up @@ -127,7 +131,7 @@ class TestAllocator
std::to_string(reinterpret_cast<std::uint64_t>(user_pointer)));
}
std::generate_n(p - canary_.size(), // Damage the memory to make sure it's not used after deallocation.
amount + canary_.size() * 2U,
amount + (canary_.size() * 2U),
[]() { return getRandomNatural<std::uint8_t>(256U); });
std::free(p - canary_.size()); // NOLINT we require manual memory management here.
allocated_.erase(it);
Expand Down Expand Up @@ -345,6 +349,26 @@ class TxQueue

void freeItem(Instance& ins, CanardTxQueueItem* const item) { canardTxFree(&que_, &ins.getInstance(), item); }

using FrameHandler = std::function<std::int8_t(const CanardMicrosecond, CanardMutableFrame&)>;

[[nodiscard]] auto poll(Instance& ins, const CanardMicrosecond now_usec, FrameHandler frame_handler)
{
if (!frame_handler)
{
return canardTxPoll(&que_, &ins.getInstance(), now_usec, nullptr, nullptr);
}

return canardTxPoll(&que_,
&ins.getInstance(),
now_usec,
&frame_handler,
[](auto* user_reference, const auto deadline_usec, auto* frame) -> std::int8_t {
//
const auto* const handler_ptr = static_cast<FrameHandler*>(user_reference);
return (*handler_ptr)(deadline_usec, *frame);
});
}

[[nodiscard]] auto getSize() const
{
std::size_t out = 0;
Expand Down
Loading

0 comments on commit 2d834f8

Please sign in to comment.