1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // The behavior of std::random_device changed on Apple platforms with
10 // https://llvm.org/D116045 (d202c76441e1).
11 // XFAIL: target={{.+}}-apple-{{.+}} && using-built-library-before-llvm-14
13 // UNSUPPORTED: no-random-device
17 // class random_device;
19 // explicit random_device(const string& token = implementation-defined); // before C++20
20 // random_device() : random_device(implementation-defined) {} // C++20
21 // explicit random_device(const string& token); // C++20
23 // For the following ctors, the standard states: "The semantics and default
24 // value of the token parameter are implementation-defined". Implementations
25 // therefore aren't required to accept any string, but the default shouldn't
30 #include <system_error>
37 #include "test_macros.h"
38 #if TEST_STD_VER >= 11
39 #include "test_convertible.h"
42 void check_random_device_valid(const std::string
&token
) {
43 std::random_device
r(token
);
46 void check_random_device_invalid(const std::string
&token
) {
47 #ifndef TEST_HAS_NO_EXCEPTIONS
49 std::random_device
r(token
);
51 } catch (const std::system_error
&) {
58 int main(int, char**) {
63 // Check the validity of various tokens
65 #if defined(_LIBCPP_USING_ARC4_RANDOM)
66 check_random_device_valid("/dev/urandom");
67 check_random_device_valid("/dev/random");
68 check_random_device_valid("/dev/null");
69 check_random_device_valid("/dev/nonexistent");
70 check_random_device_valid("wrong file");
71 #elif defined(_LIBCPP_USING_DEV_RANDOM)
72 check_random_device_valid("/dev/urandom");
73 check_random_device_valid("/dev/random");
74 check_random_device_valid("/dev/null");
75 check_random_device_invalid("/dev/nonexistent");
76 check_random_device_invalid("wrong file");
78 check_random_device_valid("/dev/urandom");
79 check_random_device_invalid("/dev/random");
80 check_random_device_invalid("/dev/null");
81 check_random_device_invalid("/dev/nonexistent");
82 check_random_device_invalid("wrong file");
87 // Test that random_device(const string&) properly handles getting
88 // a file descriptor with the value '0'. Do this by closing the standard
89 // streams so that the descriptor '0' is available.
92 ec
= close(STDIN_FILENO
);
94 ec
= close(STDOUT_FILENO
);
96 ec
= close(STDERR_FILENO
);
100 #endif // !defined(_WIN32)
102 #if TEST_STD_VER >= 11
103 static_assert(test_convertible
<std::random_device
>(), "");