Skip to content

Commit

Permalink
Os/Random: New Random[Range]() API
Browse files Browse the repository at this point in the history
Currently POSIX-only.
  • Loading branch information
leha-bot committed Sep 13, 2024
1 parent 4f42715 commit e094458
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/FlameIDE/Os/Headers.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set (HEADER_LIST
${CMAKE_CURRENT_SOURCE_DIR}/Constants.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Library.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Random.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Types.hpp
)
25 changes: 25 additions & 0 deletions include/FlameIDE/Os/Random.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef FLAMEIDE_OS_POSIX_RANDOM_HPP
#define FLAMEIDE_OS_POSIX_RANDOM_HPP

#include <FlameIDE/Common/Traits/Numbers.hpp>
#include <FlameIDE/Common/Traits/Functional.hpp>

namespace flame_ide
{namespace os
{

flame_ide::primitive_types::ssize_t RandomRange(void *buf, flame_ide::primitive_types::size_t size);

/// @brief Get next random value from POSIX getrandom(2).
/// It fills all bytes of returned value.
/// @tparam T Integral type used for returned value (default is int).
template <typename T = flame_ide::primitive_types::int_t>
typename EnableType<IsIntegralValue<T>, T>::Type Random()
{
T result{};
return RandomRange(&result, sizeof(T)) == sizeof(T) ? result : 0;
}

}}

#endif // FLAMEIDE_OS_POSIX_RANDOM_HPP
18 changes: 18 additions & 0 deletions src/Os/Posix/Random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "FlameIDE/Common/PrimitiveTypes.hpp"
#include <FlameIDE/Os/Random.hpp>

#include <android/api-level.h>
#define __ANDROID_API__ 34
#include <sys/random.h>

namespace flame_ide
{namespace os
{


flame_ide::primitive_types::ssize_t RandomRange(void *buf, flame_ide::primitive_types::size_t size)
{
return getrandom(buf, size, 0);
}

}}
1 change: 1 addition & 0 deletions src/Os/Posix/Sources.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
set (SOURCE_LIST
${CMAKE_CURRENT_SOURCE_DIR}/LibraryFunctions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Random.cpp
)
36 changes: 36 additions & 0 deletions src/Os/Tests/RandomTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <FlameIDE/../../src/Os/Tests/RandomTest.hpp>

#include <FlameIDE/Os/Random.hpp>

namespace flame_ide
{namespace os
{namespace tests
{

RandomTest::RandomTest() : ::AbstractTest("Random")
{}

RandomTest::~RandomTest() = default;

int RandomTest::vStart()
{
constexpr size_t BUF_SIZE = 15;

char buf[BUF_SIZE];
if (flame_ide::os::RandomRange(buf, BUF_SIZE) != BUF_SIZE)
{
std::cout << "OS-dependent random failed" << std::endl;
return FAILED;
}
else
{
std::cout << "First byte of random buf: " << static_cast<int>(buf[0]) << std::endl;
}

std::cout << "Random() test (int): " << flame_ide::os::Random() << std::endl;
std::cout << "Random() test (short): " << flame_ide::os::Random<short>() << std::endl;
return SUCCESS;
}

}}} // namespace flame_ide::os::tests

23 changes: 23 additions & 0 deletions src/Os/Tests/RandomTest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef FLAMEIDE_SRC_OS_TESTS_RANDOMTEST_HPP
#define FLAMEIDE_SRC_OS_TESTS_RANDOMTEST_HPP

#include <tests/Test.hpp>

namespace flame_ide
{namespace os
{namespace tests
{

class RandomTest: public ::AbstractTest
{
public:
RandomTest();
virtual ~RandomTest();

private:
virtual int vStart();
};

}}} // namespace flame_ide::os::tests

#endif // FLAMEIDE_SRC_OS_TESTS_RANDOMTEST_HPP
2 changes: 2 additions & 0 deletions src/Os/Tests/Sources.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
set (SOURCE_LIST
${CMAKE_CURRENT_SOURCE_DIR}/LibraryTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/LibraryTest.hpp
${CMAKE_CURRENT_SOURCE_DIR}/RandomTest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/RandomTest.hpp
${CMAKE_CURRENT_SOURCE_DIR}/TestAggregator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TestAggregator.hpp
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
Expand Down

0 comments on commit e094458

Please sign in to comment.