Skip to content

Commit

Permalink
deterministic stress test (#52)
Browse files Browse the repository at this point in the history
* deterministic stress test

* ensure exact iteration count

* tidy

* golf

* golf

* golf

* tidy up
  • Loading branch information
charlesnicholson authored Dec 30, 2023
1 parent 3b7a47f commit 1b78336
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
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();
}
}

0 comments on commit 1b78336

Please sign in to comment.