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

RNG refactoring #2459

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
add test cases for new rng interface
Signed-off-by: DNKpp <DNKpp2011@gmail.com>
DNKpp committed Feb 17, 2021
commit 35bd5211b82697b37d204e5d9f56795a38a115d6
11 changes: 8 additions & 3 deletions src/rand.h
Original file line number Diff line number Diff line change
@@ -56,13 +56,18 @@ class SequencedRNGWrapper :
public AbstractRNGWrapper
{
public:
template <class... TArgs, typename = std::enable_if_t<std::is_convertible_v<std::common_type_t<TArgs...>, result_type>>>
explicit SequencedRNGWrapper(TArgs ... args) noexcept :
explicit SequencedRNGWrapper(std::vector<result_type> seq) noexcept :
AbstractRNGWrapper{},
m_Sequence{ static_cast<result_type>(args)... }
m_Sequence{ std::move(seq) }
{
assert(!std::empty(m_Sequence) && "Empty sequence is not allowed.");
}

template <class... TArgs, typename = std::enable_if_t<std::is_convertible_v<std::common_type_t<TArgs...>, result_type>>>
explicit SequencedRNGWrapper(TArgs ... args) noexcept :
SequencedRNGWrapper{ std::vector<result_type>{ static_cast<result_type>(args)... } }
{
}

result_type operator()() override;

127 changes: 60 additions & 67 deletions tests/rand.cpp
Original file line number Diff line number Diff line change
@@ -19,73 +19,66 @@ TEST_CASE("GetRandomNumber") {
testGetRandomNumber(-5, -2);
}

//TEST_CASE("Lock") {
// REQUIRE_FALSE(Rand::GetRandomLocked().first);
//
// Rand::LockRandom(55);
//
// REQUIRE(Rand::GetRandomLocked().first);
// REQUIRE_EQ(Rand::GetRandomLocked().second, 55);
//
// Rand::UnlockRandom();
//
// REQUIRE_FALSE(Rand::GetRandomLocked().first);
//}

//TEST_CASE("LockGuardNest") {
// REQUIRE_FALSE(Rand::GetRandomLocked().first);
// {
// Rand::LockGuard fg(32);
// REQUIRE(fg.Enabled());
//
// REQUIRE(Rand::GetRandomLocked().first);
// REQUIRE_EQ(Rand::GetRandomLocked().second, 32);
//
// {
// Rand::LockGuard fg(0, false);
// REQUIRE(fg.Enabled());
//
// REQUIRE_FALSE(Rand::GetRandomLocked().first);
// }
//
// REQUIRE(Rand::GetRandomLocked().first);
// REQUIRE_EQ(Rand::GetRandomLocked().second, 32);
// }
// REQUIRE_FALSE(Rand::GetRandomLocked().first);
//}
//
//TEST_CASE("LockGuardRelease") {
// REQUIRE_FALSE(Rand::GetRandomLocked().first);
//
// Rand::LockGuard fg(-16);
// REQUIRE(fg.Enabled());
//
// REQUIRE(Rand::GetRandomLocked().first);
// REQUIRE_EQ(Rand::GetRandomLocked().second, -16);
//
// fg.Release();
//
// REQUIRE_FALSE(Rand::GetRandomLocked().first);
// REQUIRE_FALSE(fg.Enabled());
//}
//
//TEST_CASE("LockGuardDismiss") {
// Rand::LockGuard fg(INT32_MAX);
// REQUIRE(fg.Enabled());
//
// REQUIRE(Rand::GetRandomLocked().first);
// REQUIRE_EQ(Rand::GetRandomLocked().second, INT32_MAX);
//
// fg.Dismiss();
//
// REQUIRE(Rand::GetRandomLocked().first);
// REQUIRE_EQ(Rand::GetRandomLocked().second, INT32_MAX);
//
// Rand::UnlockRandom();
//}
static void testSequenceGenerator(const std::vector<std::uint32_t>& seq, Rand::SequencedRNGWrapper& rng, std::size_t iterations = 1)
{
for (std::size_t i = 0; i < iterations; ++i)
{
for (auto value : seq)
{
REQUIRE(rng() == value);
}
}
}

TEST_CASE("Single-Element Sequence") {

std::vector<std::uint32_t> seq{ 42 };
Rand::SequencedRNGWrapper seqRNG(seq);

testSequenceGenerator(seq, seqRNG, 3);
}

TEST_CASE("Multi-Element Sequence") {

std::vector<std::uint32_t> seq{ 42, 1337, 42, 3, 1, 3 };
Rand::SequencedRNGWrapper seqRNG(seq);

testSequenceGenerator(seq, seqRNG, 3);
}

TEST_CASE("GetRNG") {

REQUIRE(&Rand::GetRNG() != nullptr);
}

TEST_CASE("ExchangeRNG") {

auto* preRNGPtr = &Rand::GetRNG();
std::vector<std::uint32_t> seq{ 42, 1337, 42, 3, 1, 3 };
auto seqRNG = std::make_unique<Rand::SequencedRNGWrapper>(seq);
auto* seqRNGPtr = seqRNG.get();
auto prevRNG = Rand::ExchangeRNG(std::move(seqRNG));

REQUIRE(preRNGPtr == prevRNG.get());

REQUIRE(seqRNGPtr == &Rand::GetRNG());
}

TEST_CASE("ScopedRNGExchange") {

auto* preRNGPtr = &Rand::GetRNG();

{
auto scoped = Rand::test::makeScopedRNGExchange<Rand::SequencedRNGWrapper>(1);

REQUIRE(preRNGPtr != &Rand::GetRNG());
}

REQUIRE(preRNGPtr == &Rand::GetRNG());
}

static void testGetRandomNumberFixed(int32_t a, int32_t b, int32_t fix, int32_t result) {
auto scoped = Rand::test::makeScopedRNGExchange<Rand::SequencedRNGWrapper>(static_cast<std::uint32_t>(fix));
auto scoped = Rand::test::makeScopedRNGExchange<Rand::SequencedRNGWrapper>(fix);
for (int i = 0; i < 10; ++i) {
auto x = Rand::GetRandomNumber(a, b);
REQUIRE_EQ(x, result);
@@ -96,8 +89,8 @@ TEST_CASE("GetRandomNumberFixed") {
testGetRandomNumberFixed(-10, 12, 5, 5);
testGetRandomNumberFixed(-10, 12, 12, 12);
testGetRandomNumberFixed(-10, 12, 55, 12);
testGetRandomNumberFixed(-10, 12, INT32_MIN, -10);
testGetRandomNumberFixed(-10, 12, INT32_MAX, 12);
testGetRandomNumberFixed(-10, 12, std::numeric_limits<std::int32_t>::min(), -10);
testGetRandomNumberFixed(-10, 12, std::numeric_limits<std::int32_t>::max(), 12);
}

TEST_SUITE_END();