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 // See https://llvm.org/PR20183
10 // XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx10.{{9|10|11}}
12 // The behavior of std::random_device changed on Apple platforms with
13 // https://llvm.org/D116045.
14 // XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}}
16 // UNSUPPORTED: no-random-device
20 // class random_device;
22 // explicit random_device(const string& token = implementation-defined); // before C++20
23 // random_device() : random_device(implementation-defined) {} // C++20
24 // explicit random_device(const string& token); // C++20
26 // For the following ctors, the standard states: "The semantics and default
27 // value of the token parameter are implementation-defined". Implementations
28 // therefore aren't required to accept any string, but the default shouldn't
33 #include <system_error>
40 #include "test_macros.h"
41 #if TEST_STD_VER >= 11
42 #include "test_convertible.h"
45 void check_random_device_valid(const std::string
&token
) {
46 std::random_device
r(token
);
49 void check_random_device_invalid(const std::string
&token
) {
50 #ifndef TEST_HAS_NO_EXCEPTIONS
52 std::random_device
r(token
);
54 } catch (const std::system_error
&) {
61 int main(int, char**) {
65 // Check the validity of various tokens
67 #if defined(_LIBCPP_USING_ARC4_RANDOM)
68 check_random_device_valid("/dev/urandom");
69 check_random_device_valid("/dev/random");
70 check_random_device_valid("/dev/null");
71 check_random_device_valid("/dev/nonexistent");
72 check_random_device_valid("wrong file");
73 #elif defined(_LIBCPP_USING_DEV_RANDOM)
74 check_random_device_valid("/dev/urandom");
75 check_random_device_valid("/dev/random");
76 check_random_device_valid("/dev/null");
77 check_random_device_invalid("/dev/nonexistent");
78 check_random_device_invalid("wrong file");
80 check_random_device_valid("/dev/urandom");
81 check_random_device_invalid("/dev/random");
82 check_random_device_invalid("/dev/null");
83 check_random_device_invalid("/dev/nonexistent");
84 check_random_device_invalid("wrong file");
89 // Test that random_device(const string&) properly handles getting
90 // a file descriptor with the value '0'. Do this by closing the standard
91 // streams so that the descriptor '0' is available.
94 ec
= close(STDIN_FILENO
);
96 ec
= close(STDOUT_FILENO
);
98 ec
= close(STDERR_FILENO
);
100 std::random_device r
;
102 #endif // !defined(_WIN32)
104 #if TEST_STD_VER >= 11
105 static_assert(test_convertible
<std::random_device
>(), "");