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

deterministic stress test #52

Merged
merged 7 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SRCS := tests/cobs_encode_max_c.c \
tests/test_cobs_decode.cc \
tests/test_cobs_decode_inc.cc \
tests/test_cobs_decode_inplace.cc \
tests/test_many_random_payloads.cc \
tests/test_paper_figures.cc \
tests/test_wikipedia.cc \
tests/unittest_main.cc
Expand Down
1 change: 1 addition & 0 deletions make-win.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cl.exe /W4 /WX /MP /EHsc /std:c++20 ^
tests/test_cobs_encode.cc ^
tests/test_cobs_encode_inc.cc ^
tests/test_cobs_encode_inplace.cc ^
tests/test_many_random_payloads.cc ^
tests/test_paper_figures.cc ^
tests/test_wikipedia.cc ^
tests/unittest_main.cc ^
Expand Down
61 changes: 61 additions & 0 deletions tests/test_many_random_payloads.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "../cobs.h"
#include "byte_vec.h"

#include "doctest.h"

#include <algorithm>
#include <atomic>
#include <cstring>
#include <random>
#include <thread>

using std::atomic;
using std::generate;
using std::max;
using std::mt19937;
using std::none_of;
using std::thread;
using std::vector;

auto constexpr LEN{ 4u * 1024u * 1024u };

namespace {
atomic<int> s_iterations{ 250u };
}

TEST_CASE("many random payloads") {
auto const thread_proc{ [](unsigned seed) {
mt19937 mt{ seed }; // deterministic
byte_vec_t src(LEN), dec(LEN), enc(COBS_ENCODE_MAX(LEN));

while (--s_iterations > 0) {
generate(src.begin(), src.end(), [&]() { return byte_t(mt()); });
memset(src.data() + 1000, 0xAA, 256 * 10); // nonzero run

size_t enc_len{ 0u };
REQUIRE(cobs_encode(src.data(), src.size(), enc.data(), enc.size(), &enc_len) ==
COBS_RET_SUCCESS);

REQUIRE(enc_len >= LEN);
REQUIRE(enc_len <= COBS_ENCODE_MAX(LEN));
REQUIRE(none_of(enc.data(), enc.data() + enc_len - 1, [](byte_t b) { return !b; }));
REQUIRE(enc[enc_len - 1] == 0);

size_t dec_len{ 0u };
REQUIRE(cobs_decode(enc.data(), enc_len, dec.data(), dec.size(), &dec_len) ==
COBS_RET_SUCCESS);

REQUIRE(dec_len == LEN);
REQUIRE(src == dec);
}
} };

vector<thread> threads;
for (auto i{ 0u }, n{ max(1u, thread::hardware_concurrency() - 1) }; i < n; ++i) {
threads.emplace_back(thread_proc, i);
}

for (auto& t : threads) {
t.join();
}
}