Add Utility/Rand module
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
e9b633e46c
commit
8d721ccaaa
@ -14,7 +14,8 @@ add_executable(tests
|
||||
ringbuffer.cpp
|
||||
packet.cpp
|
||||
parser.cpp
|
||||
button.cpp)
|
||||
button.cpp
|
||||
rand.cpp)
|
||||
|
||||
target_link_libraries(tests
|
||||
PUBLIC
|
||||
|
||||
62
Tests/rand.cpp
Normal file
62
Tests/rand.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
//
|
||||
// Created by erki on 29.04.21.
|
||||
//
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include "utility_rand.hpp"
|
||||
|
||||
TEST_CASE("rand32 is deterministic.", "[utility],[rand]")
|
||||
{
|
||||
const std::uint32_t original_state = 34;
|
||||
std::uint32_t state = original_state;
|
||||
std::vector<std::uint32_t> generated;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
generated.push_back(Peripherals::rand32(state));
|
||||
}
|
||||
|
||||
std::uint32_t new_state = original_state;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
REQUIRE(generated[i] == Peripherals::rand32(new_state));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("rand64 is deterministic.", "[utility],[rand]")
|
||||
{
|
||||
const std::uint64_t original_state = 10e4;
|
||||
std::uint64_t state = original_state;
|
||||
std::vector<std::uint64_t> generated;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
generated.push_back(Peripherals::rand64(state));
|
||||
}
|
||||
|
||||
std::uint64_t new_state = original_state;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
REQUIRE(generated[i] == Peripherals::rand64(new_state));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("rand and srand are deterministic", "[utility],[rand]")
|
||||
{
|
||||
const std::uint32_t original_state = 34;
|
||||
|
||||
Peripherals::srand(original_state);
|
||||
std::vector<std::uint32_t> generated;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
generated.push_back(Peripherals::rand());
|
||||
}
|
||||
|
||||
Peripherals::srand(original_state);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
REQUIRE(generated[i] == Peripherals::rand());
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@ cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
|
||||
|
||||
add_library(utility STATIC
|
||||
Src/utility_logging.cpp
|
||||
Src/utility_rand.cpp
|
||||
${additional_sources}
|
||||
)
|
||||
|
||||
|
||||
40
Utility/Inc/utility_rand.hpp
Normal file
40
Utility/Inc/utility_rand.hpp
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// Created by erki on 29.04.21.
|
||||
//
|
||||
|
||||
#ifndef SKULLC_UTILITY_RAND_HPP
|
||||
#define SKULLC_UTILITY_RAND_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace Peripherals
|
||||
{
|
||||
|
||||
inline std::uint32_t rand32(std::uint32_t& state)
|
||||
{
|
||||
std::uint32_t x = state;
|
||||
x ^= x << 13;
|
||||
x ^= x >> 17;
|
||||
x ^= x << 5;
|
||||
state = x;
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
inline std::uint64_t rand64(std::uint64_t& state)
|
||||
{
|
||||
std::uint64_t x = state;
|
||||
x ^= x << 13;
|
||||
x ^= x >> 7;
|
||||
x ^= x << 17;
|
||||
state = x;
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
void srand(const std::uint32_t& seed);
|
||||
std::uint32_t rand();
|
||||
|
||||
}
|
||||
|
||||
#endif //SKULLC_UTILITY_RAND_HPP
|
||||
27
Utility/Src/utility_rand.cpp
Normal file
27
Utility/Src/utility_rand.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by erki on 29.04.21.
|
||||
//
|
||||
|
||||
#include "utility_rand.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
std::uint32_t rand_state = 0;
|
||||
|
||||
}
|
||||
|
||||
namespace Peripherals
|
||||
{
|
||||
|
||||
void srand(const std::uint32_t& seed)
|
||||
{
|
||||
rand_state = seed;
|
||||
}
|
||||
|
||||
std::uint32_t rand()
|
||||
{
|
||||
return rand32(rand_state);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user