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

Add API for Random numbers (OS-dependent) (#14) #15

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

randomRange()


/// @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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random()

{
T result{};
return RandomRange(&result, sizeof(T)) == sizeof(T) ? result : 0;
}

}}
Copy link
Member

Choose a reason for hiding this comment

The 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
16 changes: 16 additions & 0 deletions src/Os/Posix/Random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "FlameIDE/Common/PrimitiveTypes.hpp"
Copy link
Member

@kachsheev kachsheev Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use <>. And I think this string can be removed.

#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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use flame_ide::Types from Numbers.hpp

{
return getrandom(buf, size, 0);
}

}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment to the end of namespaces

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
Loading