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