-
Notifications
You must be signed in to change notification settings - Fork 9
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
Implement subscription API functions #46
Changes from all commits
df1108b
af82f15
7428526
0da9548
02c9a33
118a746
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/// This software is distributed under the terms of the MIT License. | ||
/// Copyright (C) OpenCyphal Development Team <opencyphal.org> | ||
/// Copyright Amazon.com Inc. or its affiliates. | ||
/// SPDX-License-Identifier: MIT | ||
|
||
#include <unity.h> | ||
|
||
namespace | ||
{ | ||
|
||
// Here be dragons. | ||
|
||
} // namespace | ||
|
||
void setUp() {} | ||
|
||
void tearDown() {} | ||
|
||
int main() | ||
{ | ||
UNITY_BEGIN(); | ||
// TODO | ||
return UNITY_END(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/// This software is distributed under the terms of the MIT License. | ||
/// Copyright (C) OpenCyphal Development Team <opencyphal.org> | ||
/// Copyright Amazon.com Inc. or its affiliates. | ||
/// SPDX-License-Identifier: MIT | ||
|
||
#include <udpard.h> | ||
#include "helpers.h" | ||
#include "hexdump.hpp" | ||
#include <unity.h> | ||
#include <iostream> | ||
#include <array> | ||
#include <algorithm> | ||
|
||
namespace | ||
{ | ||
void testGather() | ||
{ | ||
const std::string_view payload = | ||
"It's very simple. The attacker must first transform themselves into life forms that can survive in a " | ||
"low-dimensional universe. For instance, a four-dimensional species can transform itself into " | ||
"three-dimensional creatures, or a three-dimensional species can transform itself into two-dimensional life. " | ||
"After the entire civilization has entered a lower dimension, they can initiate a dimensional strike against " | ||
"the enemy without concern for the consequences."; | ||
|
||
std::array<UdpardFragment, 4> frags{{}}; | ||
frags.at(0).next = &frags.at(1); | ||
frags.at(1).next = &frags.at(2); | ||
frags.at(2).next = &frags.at(3); | ||
frags.at(3).next = nullptr; | ||
|
||
frags.at(0).view.data = payload.data(); | ||
frags.at(0).view.size = 100; | ||
|
||
frags.at(1).view.data = payload.data() + frags.at(0).view.size; | ||
frags.at(1).view.size = 100; | ||
|
||
frags.at(2).view.data = payload.data() + frags.at(1).view.size + frags.at(0).view.size; | ||
frags.at(2).view.size = 0; // Edge case. | ||
|
||
frags.at(3).view.data = payload.data() + frags.at(2).view.size + frags.at(1).view.size + frags.at(0).view.size; | ||
frags.at(3).view.size = payload.size() - frags.at(2).view.size - frags.at(1).view.size - frags.at(0).view.size; | ||
|
||
std::array<std::uint8_t, 1024> mono{}; | ||
|
||
// Copy full size payload. | ||
std::generate(mono.begin(), mono.end(), [] { return std::rand() % 256; }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To generate random test bits use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, I overlooked the seeding part. However, I think it would be unwise to use the C++ RNG utilities here because this is a C library and most of the test code is written in C, so in the interest of consistency and compatibility with future tests written in C, it is desirable to use I implemented seeding in the last commit. |
||
TEST_ASSERT_EQUAL(payload.size(), udpardGather(frags.at(0), mono.size(), mono.data())); | ||
TEST_ASSERT_EQUAL_MEMORY(payload.data(), mono.data(), payload.size()); | ||
|
||
// Truncation mid-fragment. | ||
std::generate(mono.begin(), mono.end(), [] { return std::rand() % 256; }); | ||
TEST_ASSERT_EQUAL(150, udpardGather(frags.at(0), 150, mono.data())); | ||
TEST_ASSERT_EQUAL_MEMORY(payload.data(), mono.data(), 150); | ||
|
||
// Truncation at the fragment boundary. | ||
std::generate(mono.begin(), mono.end(), [] { return std::rand() % 256; }); | ||
TEST_ASSERT_EQUAL(200, udpardGather(frags.at(0), 200, mono.data())); | ||
TEST_ASSERT_EQUAL_MEMORY(payload.data(), mono.data(), 200); | ||
|
||
// Empty destination. | ||
mono.fill(0xA5); | ||
TEST_ASSERT_EQUAL(0, udpardGather(frags.at(0), 0, mono.data())); | ||
TEST_ASSERT_EQUAL(0, std::count_if(mono.begin(), mono.end(), [](const auto x) { return x != 0xA5; })); | ||
|
||
// Edge cases. | ||
TEST_ASSERT_EQUAL(0, udpardGather(frags.at(0), 0, nullptr)); | ||
TEST_ASSERT_EQUAL(0, udpardGather(frags.at(0), 100, nullptr)); | ||
} | ||
} // namespace | ||
|
||
void setUp() | ||
{ | ||
seedRandomNumberGenerator(); // Re-seed the RNG for each test to avoid coupling. | ||
} | ||
|
||
void tearDown() {} | ||
|
||
int main() | ||
{ | ||
UNITY_BEGIN(); | ||
RUN_TEST(testGather); | ||
return UNITY_END(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we define byte_t as unsigned char? I forgot if this was a local typedef or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is internal:
https://github.com/OpenCyphal-Garage/libudpard/blob/c92f8df4c3be2a3c0e6df501d97576a2df8e8080/libudpard/udpard.c#L33