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 | // <random> |
10 | |
11 | // template <class UIntType, UIntType a, UIntType c, UIntType m> |
12 | // class linear_congruential_engine; |
13 | |
14 | // linear_congruential_engine(const linear_congruential_engine&); |
15 | |
16 | #include <random> |
17 | #include <cassert> |
18 | |
19 | #include "test_macros.h" |
20 | |
21 | template <class T, T a, T c, T m> |
22 | void |
23 | test1() |
24 | { |
25 | typedef std::linear_congruential_engine<T, a, c, m> E; |
26 | E e1; |
27 | E e2 = e1; |
28 | assert(e1 == e2); |
29 | (void)e1(); |
30 | (void)e2(); |
31 | assert(e1 == e2); |
32 | } |
33 | |
34 | template <class T> |
35 | void |
36 | test() |
37 | { |
38 | const int W = sizeof(T) * CHAR_BIT; |
39 | const T M(static_cast<T>(-1)); |
40 | const T A(static_cast<T>((static_cast<T>(1) << (W / 2)) - 1)); |
41 | |
42 | // Cases where m = 0 |
43 | test1<T, 0, 0, 0>(); |
44 | test1<T, A, 0, 0>(); |
45 | test1<T, 0, 1, 0>(); |
46 | test1<T, A, 1, 0>(); |
47 | |
48 | // Cases where m = 2^n for n < w |
49 | test1<T, 0, 0, 256>(); |
50 | test1<T, 5, 0, 256>(); |
51 | test1<T, 0, 1, 256>(); |
52 | test1<T, 5, 1, 256>(); |
53 | |
54 | // Cases where m is odd and a = 0 |
55 | test1<T, 0, 0, M>(); |
56 | test1<T, 0, M - 2, M>(); |
57 | test1<T, 0, M - 1, M>(); |
58 | |
59 | // Cases where m is odd and m % a <= m / a (Schrage) |
60 | test1<T, A, 0, M>(); |
61 | test1<T, A, M - 2, M>(); |
62 | test1<T, A, M - 1, M>(); |
63 | } |
64 | |
65 | template <class T> |
66 | void test_ext() { |
67 | const T M(static_cast<T>(-1)); |
68 | |
69 | // Cases where m is odd and m % a > m / a |
70 | test1<T, M - 2, 0, M>(); |
71 | test1<T, M - 2, M - 2, M>(); |
72 | test1<T, M - 2, M - 1, M>(); |
73 | test1<T, M - 1, 0, M>(); |
74 | test1<T, M - 1, M - 2, M>(); |
75 | test1<T, M - 1, M - 1, M>(); |
76 | } |
77 | |
78 | int main(int, char**) |
79 | { |
80 | test<unsigned short>(); |
81 | test_ext<unsigned short>(); |
82 | test<unsigned int>(); |
83 | test_ext<unsigned int>(); |
84 | test<unsigned long>(); |
85 | test_ext<unsigned long>(); |
86 | test<unsigned long long>(); |
87 | // This isn't implemented on platforms without __int128 |
88 | #ifndef _LIBCPP_HAS_NO_INT128 |
89 | test_ext<unsigned long long>(); |
90 | #endif |
91 | |
92 | return 0; |
93 | } |
94 | |