root: test: Random: assert multiple types

- Add test cases for multiple types
- Rewrite to include new interface/impl
- Fix assertion test to not exceed error boundary
This commit is contained in:
2024-07-23 18:04:19 -07:00
parent 879eb6f785
commit 480b42e273
2 changed files with 60 additions and 18 deletions

View File

@@ -53,7 +53,8 @@ struct RandomInterface
t_random generate_impl() t_random generate_impl()
{ {
static_assert( static_assert(
std::is_same_v<t_random, uint32_t>, "Random interface has changed"); ::docker_finance::internal::type::is_real_integral<t_random>::value,
"Random interface has changed");
return t_random{std::numeric_limits<t_random>::max()}; return t_random{std::numeric_limits<t_random>::max()};
} }

View File

@@ -60,9 +60,24 @@ struct RandomInterface : public RandomInterfaceFixture<tests::RandomInterface>
{ {
}; };
TEST_F(RandomInterface, generate) TEST_F(RandomInterface, generate_uint16_t)
{ {
ASSERT_EQ(random.generate(), std::numeric_limits<uint32_t>::max()); ASSERT_EQ(random.generate<uint16_t>(), std::numeric_limits<uint16_t>::max());
}
TEST_F(RandomInterface, generate_uint32_t)
{
ASSERT_EQ(random.generate<uint32_t>(), std::numeric_limits<uint32_t>::max());
}
TEST_F(RandomInterface, generate_int16_t)
{
ASSERT_EQ(random.generate<int16_t>(), std::numeric_limits<int16_t>::max());
}
TEST_F(RandomInterface, generate_int32_t)
{
ASSERT_EQ(random.generate<int32_t>(), std::numeric_limits<int32_t>::max());
} }
//! \brief Generic random generator fixture //! \brief Generic random generator fixture
@@ -71,22 +86,22 @@ template <typename t_impl>
struct RandomFixture : public ::testing::Test, protected t_impl struct RandomFixture : public ::testing::Test, protected t_impl
{ {
protected: protected:
template <typename t_random>
void generate() void generate()
{ {
// NOTE: Why auto? Because no accidental type casting or under/overflow // NOTE: Why auto? Because no accidental type casting or under/overflow
auto one = t_impl::random.generate(); auto one = t_impl::random.template generate<t_random>();
static_assert(std::is_same_v<decltype(one), uint32_t>); static_assert(std::is_same_v<decltype(one), t_random>);
auto two = t_impl::random.generate(); auto two = t_impl::random.template generate<t_random>();
static_assert(std::is_same_v<decltype(two), uint32_t>); static_assert(std::is_same_v<decltype(two), t_random>);
ASSERT_LE(one, std::numeric_limits<uint32_t>::max()); // 0xFFFFFFFFul // NOTE: t_random limits are implementation-specific
ASSERT_GE(one, 0); ASSERT_NEAR(0, one, std::numeric_limits<t_random>::max());
ASSERT_NEAR(0, two, std::numeric_limits<t_random>::max());
ASSERT_LE(two, std::numeric_limits<uint32_t>::max());
ASSERT_GE(two, 0);
// Exceedingly rare (tests the obvious, but is not an accurate entropy test) // Exceedingly rare (tests the obvious, but is not an accurate entropy test)
// If fails, re-run tests to confirm
ASSERT_NE(one, two); ASSERT_NE(one, two);
}; };
}; };
@@ -101,9 +116,14 @@ struct RandomBotan : public RandomFixture<tests::RandomBotan_Impl>
{ {
}; };
TEST_F(RandomBotan, generate) TEST_F(RandomBotan, generate_uint16_t)
{ {
ASSERT_NO_THROW(generate()); ASSERT_NO_THROW(generate<uint16_t>());
}
TEST_F(RandomBotan, generate_uint32_t)
{
ASSERT_NO_THROW(generate<uint32_t>());
} }
// //
@@ -116,9 +136,24 @@ struct RandomCryptoPP : public RandomFixture<tests::RandomCryptoPP_Impl>
{ {
}; };
TEST_F(RandomCryptoPP, generate) TEST_F(RandomCryptoPP, generate_uint16_t)
{ {
ASSERT_NO_THROW(generate()); ASSERT_NO_THROW(generate<uint16_t>());
}
TEST_F(RandomCryptoPP, generate_uint32_t)
{
ASSERT_NO_THROW(generate<uint32_t>());
}
TEST_F(RandomCryptoPP, generate_int16_t)
{
ASSERT_NO_THROW(generate<int16_t>());
}
TEST_F(RandomCryptoPP, generate_int32_t)
{
ASSERT_NO_THROW(generate<int32_t>());
} }
// //
@@ -131,10 +166,16 @@ struct RandomLibsodium : public RandomFixture<tests::RandomLibsodium_Impl>
{ {
}; };
TEST_F(RandomLibsodium, generate) TEST_F(RandomLibsodium, generate_uint16_t)
{ {
ASSERT_NO_THROW(generate()); ASSERT_NO_THROW(generate<uint16_t>());
} }
TEST_F(RandomLibsodium, generate_uint32_t)
{
ASSERT_NO_THROW(generate<uint32_t>());
}
} // namespace unit } // namespace unit
} // namespace tests } // namespace tests
} // namespace docker_finance } // namespace docker_finance