xref: /llvm-project/libcxx/test/std/numerics/rand/rand.device/eval.pass.cpp (revision a7f9895cc18995549c7facb96e72718da282a864)
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: no-random-device
10 
11 // <random>
12 
13 // class random_device;
14 
15 // result_type operator()();
16 
17 #include <random>
18 #include <cassert>
19 #include <system_error>
20 
21 #include "test_macros.h"
22 
23 int main(int, char**)
24 {
25     {
26         std::random_device r;
27         std::random_device::result_type e = r();
28         ((void)e); // Prevent unused warning
29     }
30 
31     // When using the `/dev/urandom` implementation, make sure that we throw
32     // an exception when we hit EOF while reading the custom-provided file.
33 #if !defined(TEST_HAS_NO_EXCEPTIONS) && defined(_LIBCPP_USING_DEV_RANDOM)
34     {
35         std::random_device r("/dev/null");
36         try {
37             (void)r();
38             LIBCPP_ASSERT(false);
39         } catch (const std::system_error&) {
40         }
41     }
42 #endif
43 
44   return 0;
45 }
46