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

utils: Re-add uniform test + docstring #3282

Merged
merged 1 commit into from
Oct 30, 2019
Merged
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
8 changes: 8 additions & 0 deletions src/utils/include/utils/uniform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
namespace Utils {
/**
* @brief Uniformly map unsigned integer to double.
*
* This maps a unsigned integer to the double interval
* (0., 1.], where 0. is mapped to the smallest representable
* value larger than 0., and the maximal integer value is
* mapped to 1.
*
* @param in Unsigned integer value
* @return Mapped floating point value.
*/
constexpr inline double uniform(uint64_t in) {
auto constexpr const max = std::numeric_limits<uint64_t>::max();
Expand Down
1 change: 1 addition & 0 deletions src/utils/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ unit_test(NAME rotation_matrix_test SRC rotation_matrix_test.cpp DEPENDS utils)
unit_test(NAME quaternion_test SRC quaternion_test.cpp DEPENDS utils)
unit_test(NAME mask_test SRC mask_test.cpp DEPENDS utils)
unit_test(NAME type_traits_test SRC type_traits_test.cpp DEPENDS utils)
unit_test(NAME uniform_test SRC uniform_test.cpp DEPENDS utils)

unit_test(NAME gather_buffer_test SRC gather_buffer_test.cpp DEPENDS utils Boost::mpi MPI::MPI_CXX NUM_PROC 4)
unit_test(NAME scatter_buffer_test SRC scatter_buffer_test.cpp DEPENDS utils Boost::mpi MPI::MPI_CXX NUM_PROC 4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
#include <utils/uniform.hpp>

BOOST_AUTO_TEST_CASE(limits) {
/* 0 maps to epsilon */
BOOST_CHECK_SMALL(Utils::uniform(0ul),
std::numeric_limits<double>::epsilon());
/* numeric_limits<>::max() maps to 1. */
BOOST_CHECK_EQUAL(1., Utils::uniform(std::numeric_limits<uint64_t>::max()));
/* linearity */
BOOST_CHECK_EQUAL(Utils::uniform(0ul) - Utils::uniform(5ul),
Utils::uniform(10000ul) - Utils::uniform(10005ul));
}