Skip to content

Commit

Permalink
Extend test coverage and update existing tests to match the new behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kirienko committed Apr 21, 2023
1 parent 45cd82c commit 5f5ca48
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions tests/test_private_rx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ TEST_CASE("rxSessionUpdate")
REQUIRE(ins.getAllocator().getTotalAllocatedAmount() == 16);
ins.getAllocator().deallocate(transfer.payload);

// Restart by TID timeout, not the first frame.
// TID timeout does not occur until SOT; see https://github.com/OpenCyphal/libcanard/issues/212.
frame.timestamp_usec = 30'000'000;
frame.transfer_id = 12; // Goes back.
frame.start_of_transfer = false;
Expand All @@ -507,18 +507,34 @@ TEST_CASE("rxSessionUpdate")
frame.payload = reinterpret_cast<const uint8_t*>("\x0A\x0A\x0A\x0A\x0A\x0A\x0A");
REQUIRE(0 == update(2, 1'000'000, 16));
REQUIRE(rxs.transfer_timestamp_usec == 20'000'100); // No change.
REQUIRE(rxs.payload_size == 0);
REQUIRE(rxs.payload == nullptr);
REQUIRE(rxs.calculated_crc == 0xFFFF);
REQUIRE(rxs.transfer_id == 13U);
REQUIRE(rxs.toggle);
REQUIRE(rxs.redundant_transport_index == 2);
REQUIRE(rxs.transfer_id == 14U); // No change.
REQUIRE(rxs.toggle); // No change.
REQUIRE(rxs.redundant_transport_index == 0); // No change.
REQUIRE(ins.getAllocator().getNumAllocatedFragments() == 0);
REQUIRE(ins.getAllocator().getTotalAllocatedAmount() == 0);

// Restart by TID timeout. This may only occur when SOT is set.
frame.timestamp_usec = 30'000'000;
frame.transfer_id = 12; // Goes back.
frame.start_of_transfer = true;
frame.end_of_transfer = false;
frame.toggle = true;
frame.payload_size = 7;
frame.payload = reinterpret_cast<const uint8_t*>("\x0A\x0A\x0A\x0A\x0A\x0A\x0A");
REQUIRE(0 == update(2, 1'000'000, 16));
REQUIRE(rxs.transfer_timestamp_usec == 30'000'000); // Updated from the frame.
REQUIRE(rxs.payload_size == 7); // From the frame.
REQUIRE(rxs.payload != nullptr);
REQUIRE(rxs.calculated_crc == 0x23C7);
REQUIRE(rxs.transfer_id == 12U); // Updated from the frame.
REQUIRE(!rxs.toggle); // In anticipation of the next frame.
REQUIRE(rxs.redundant_transport_index == 2); // Updated from the update.
REQUIRE(ins.getAllocator().getNumAllocatedFragments() == 1);
REQUIRE(ins.getAllocator().getTotalAllocatedAmount() == 16);

// Restart by TID mismatch.
frame.timestamp_usec = 20'000'200; // Goes back.
frame.transfer_id = 11; // Goes back.
frame.transfer_id = 10; // Goes back.
frame.start_of_transfer = true;
frame.end_of_transfer = false;
frame.toggle = true;
Expand All @@ -529,15 +545,15 @@ TEST_CASE("rxSessionUpdate")
REQUIRE(rxs.payload_size == 7);
REQUIRE(0 == std::memcmp(rxs.payload, "\x0B\x0B\x0B\x0B\x0B\x0B\x0B", 7));
REQUIRE(rxs.calculated_crc == crc("\x0B\x0B\x0B\x0B\x0B\x0B\x0B"));
REQUIRE(rxs.transfer_id == 11U);
REQUIRE(rxs.transfer_id == 10U);
REQUIRE(!rxs.toggle);
REQUIRE(rxs.redundant_transport_index == 2);
REQUIRE(ins.getAllocator().getNumAllocatedFragments() == 1);
REQUIRE(ins.getAllocator().getTotalAllocatedAmount() == 16);

// Duplicate start rejected (toggle mismatch).
frame.timestamp_usec = 20'000'300;
frame.transfer_id = 11;
frame.transfer_id = 10;
frame.start_of_transfer = true;
frame.end_of_transfer = true;
frame.toggle = true;
Expand All @@ -548,15 +564,15 @@ TEST_CASE("rxSessionUpdate")
REQUIRE(rxs.payload_size == 7);
REQUIRE(0 == std::memcmp(rxs.payload, "\x0B\x0B\x0B\x0B\x0B\x0B\x0B", 7));
REQUIRE(rxs.calculated_crc == crc("\x0B\x0B\x0B\x0B\x0B\x0B\x0B"));
REQUIRE(rxs.transfer_id == 11U);
REQUIRE(rxs.transfer_id == 10U);
REQUIRE(!rxs.toggle);
REQUIRE(rxs.redundant_transport_index == 2);
REQUIRE(ins.getAllocator().getNumAllocatedFragments() == 1);
REQUIRE(ins.getAllocator().getTotalAllocatedAmount() == 16);

// Continue & finalize.
frame.timestamp_usec = 20'000'400;
frame.transfer_id = 11;
frame.transfer_id = 10;
frame.start_of_transfer = false;
frame.end_of_transfer = true;
frame.toggle = false;
Expand All @@ -567,15 +583,15 @@ TEST_CASE("rxSessionUpdate")
REQUIRE(rxs.payload_size == 0);
REQUIRE(rxs.payload == nullptr);
REQUIRE(rxs.calculated_crc == 0xFFFF);
REQUIRE(rxs.transfer_id == 12U);
REQUIRE(rxs.transfer_id == 11U);
REQUIRE(rxs.toggle);
REQUIRE(rxs.redundant_transport_index == 2);
REQUIRE(transfer.timestamp_usec == 20'000'200);
REQUIRE(transfer.metadata.priority == CanardPrioritySlow);
REQUIRE(transfer.metadata.transfer_kind == CanardTransferKindMessage);
REQUIRE(transfer.metadata.port_id == 2'222);
REQUIRE(transfer.metadata.remote_node_id == 55);
REQUIRE(transfer.metadata.transfer_id == 11);
REQUIRE(transfer.metadata.transfer_id == 10);
REQUIRE(transfer.payload_size == 10);
REQUIRE(0 == std::memcmp(transfer.payload, "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0D\x0D\x0D", 10));
REQUIRE(ins.getAllocator().getNumAllocatedFragments() == 1);
Expand Down

0 comments on commit 5f5ca48

Please sign in to comment.