Skip to content

Commit

Permalink
[libc++] Refactor the tests for std::random_device
Browse files Browse the repository at this point in the history
That will make it easier to change the behavior of the arc4random()
based implementation. Note that in particular, the eval.pass.cpp test
used to work with non "/dev/random" based implementations because we'd
throw an exception upon constructing the random_device. This patch makes
the intent of the test clearer.
  • Loading branch information
ldionne committed Jan 10, 2022
1 parent 914fffc commit 84654f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
36 changes: 10 additions & 26 deletions libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp
Expand Up @@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//

// See https://llvm.org/PR20183
//
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}

// UNSUPPORTED: libcpp-has-no-random-device
Expand Down Expand Up @@ -38,15 +37,6 @@
#include "test_convertible.h"
#endif

bool is_valid_random_device(const std::string &token) {
#if defined(_LIBCPP_USING_DEV_RANDOM)
// Not an exhaustive list: they're the only tokens that are tested below.
return token == "/dev/urandom" || token == "/dev/random";
#else
return token == "/dev/urandom";
#endif
}

void check_random_device_valid(const std::string &token) {
std::random_device r(token);
}
Expand All @@ -67,24 +57,18 @@ int main(int, char**) {
{
std::random_device r;
}
// Check the validity of various tokens
{
std::string token = "wrong file";
check_random_device_invalid(token);
}
{
std::string token = "/dev/urandom";
if (is_valid_random_device(token))
check_random_device_valid(token);
else
check_random_device_invalid(token);
}
{
std::string token = "/dev/random";
if (is_valid_random_device(token))
check_random_device_valid(token);
else
check_random_device_invalid(token);
check_random_device_invalid("wrong file");
check_random_device_invalid("/dev/whatever");
check_random_device_valid("/dev/urandom");
#if defined(_LIBCPP_USING_DEV_RANDOM)
check_random_device_valid("/dev/random");
#else
check_random_device_invalid("/dev/random");
#endif
}

#if !defined(_WIN32)
// Test that random_device(const string&) properly handles getting
// a file descriptor with the value '0'. Do this by closing the standard
Expand Down
16 changes: 8 additions & 8 deletions libcxx/test/std/numerics/rand/rand.device/eval.pass.cpp
Expand Up @@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//

// See https://llvm.org/PR20183
//
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}

// UNSUPPORTED: libcpp-has-no-random-device
Expand All @@ -32,15 +31,16 @@ int main(int, char**)
((void)e); // Prevent unused warning
}

#ifndef TEST_HAS_NO_EXCEPTIONS
try
// When using the `/dev/urandom` implementation, make sure that we throw
// an exception when we hit EOF while reading the custom-provided file.
#if !defined(TEST_HAS_NO_EXCEPTIONS) && defined(_LIBCPP_USING_DEV_RANDOM)
{
std::random_device r("/dev/null");
(void)r();
LIBCPP_ASSERT(false);
}
catch (const std::system_error&)
{
try {
(void)r();
LIBCPP_ASSERT(false);
} catch (const std::system_error&) {
}
}
#endif

Expand Down

0 comments on commit 84654f2

Please sign in to comment.