-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add API for Random numbers (OS-dependent) (#14) #15
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
) |
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
T result{}; | ||
return RandomRange(&result, sizeof(T)) == sizeof(T) ? result : 0; | ||
} | ||
|
||
}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comment to the end of namespaces |
||
|
||
#endif // FLAMEIDE_OS_POSIX_RANDOM_HPP |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "FlameIDE/Common/PrimitiveTypes.hpp" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
#include <FlameIDE/Os/Random.hpp> | ||
|
||
#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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
{ | ||
return getrandom(buf, size, 0); | ||
} | ||
|
||
}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comment to the end of namespaces |
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 | ||
) |
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 | ||
|
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
randomRange()