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// result_type operator()();
15
16#include <random>
17#include <cassert>
18
19#include "test_macros.h"
20
21int main(int, char**)
22{
23 typedef unsigned long long T;
24
25 // m might overflow, but the overflow is OK so it shouldn't use Schrage's algorithm
26 typedef std::linear_congruential_engine<T, 25214903917ull, 1, (1ull << 48)> E1;
27 E1 e1;
28 // make sure the right algorithm was used
29 assert(e1() == 25214903918ull);
30 assert(e1() == 205774354444503ull);
31 assert(e1() == 158051849450892ull);
32 // make sure result is in bounds
33 assert(e1() < (1ull << 48));
34 assert(e1() < (1ull << 48));
35 assert(e1() < (1ull << 48));
36 assert(e1() < (1ull << 48));
37 assert(e1() < (1ull << 48));
38
39 // m might overflow. The overflow is not OK and result will be in bounds
40 // so we should use Schrage's algorithm
41 typedef std::linear_congruential_engine<T, (1ull << 32), 0, (1ull << 63) + 1ull> E2;
42 E2 e2;
43 // make sure Schrage's algorithm is used (it would be 0s after the first otherwise)
44 assert(e2() == (1ull << 32));
45 assert(e2() == (1ull << 63) - 1ull);
46 assert(e2() == (1ull << 63) - 0x1ffffffffull);
47 // make sure result is in bounds
48 assert(e2() < (1ull << 63) + 1);
49 assert(e2() < (1ull << 63) + 1);
50 assert(e2() < (1ull << 63) + 1);
51 assert(e2() < (1ull << 63) + 1);
52 assert(e2() < (1ull << 63) + 1);
53
54 // m might overflow. The overflow is not OK and result will be in bounds
55 // so we should use Schrage's algorithm. m is even
56 typedef std::linear_congruential_engine<T, 0x18000001ull, 0x12347ull, (3ull << 56)> E3;
57 E3 e3;
58 // make sure Schrage's algorithm is used
59 assert(e3() == 0x18012348ull);
60 assert(e3() == 0x2401b4ed802468full);
61 assert(e3() == 0x18051ec400369d6ull);
62 // make sure result is in bounds
63 assert(e3() < (3ull << 56));
64 assert(e3() < (3ull << 56));
65 assert(e3() < (3ull << 56));
66 assert(e3() < (3ull << 56));
67 assert(e3() < (3ull << 56));
68
69 // 32-bit case:
70 // m might overflow. The overflow is not OK, result will be in bounds,
71 // and Schrage's algorithm is incompatible here. Need to use 64 bit arithmetic.
72 typedef std::linear_congruential_engine<unsigned, 0x10009u, 0u, 0x7fffffffu> E4;
73 E4 e4;
74 // make sure enough precision is used
75 assert(e4() == 0x10009u);
76 assert(e4() == 0x120053u);
77 assert(e4() == 0xf5030fu);
78 // make sure result is in bounds
79 assert(e4() < 0x7fffffffu);
80 assert(e4() < 0x7fffffffu);
81 assert(e4() < 0x7fffffffu);
82 assert(e4() < 0x7fffffffu);
83 assert(e4() < 0x7fffffffu);
84
85#ifndef _LIBCPP_HAS_NO_INT128
86 // m might overflow. The overflow is not OK, result will be in bounds,
87 // and Schrage's algorithm is incompatible here. Need to use 128 bit arithmetic.
88 typedef std::linear_congruential_engine<T, 0x100000001ull, 0ull, (1ull << 61) - 1ull> E5;
89 E5 e5;
90 // make sure enough precision is used
91 assert(e5() == 0x100000001ull);
92 assert(e5() == 0x200000009ull);
93 assert(e5() == 0xb00000019ull);
94 // make sure result is in bounds
95 assert(e5() < (1ull << 61) - 1ull);
96 assert(e5() < (1ull << 61) - 1ull);
97 assert(e5() < (1ull << 61) - 1ull);
98 assert(e5() < (1ull << 61) - 1ull);
99 assert(e5() < (1ull << 61) - 1ull);
100#endif
101
102 // m will not overflow so we should not use Schrage's algorithm
103 typedef std::linear_congruential_engine<T, 1ull, 1, (1ull << 48)> E6;
104 E6 e6;
105 // make sure the correct algorithm was used
106 assert(e6() == 2ull);
107 assert(e6() == 3ull);
108 assert(e6() == 4ull);
109 // make sure result is in bounds
110 assert(e6() < (1ull << 48));
111 assert(e6() < (1ull << 48));
112 assert(e6() < (1ull << 48));
113 assert(e6() < (1ull << 48));
114 assert(e6() < (1ull << 48));
115
116 return 0;
117}
118

source code of libcxx/test/std/numerics/rand/rand.eng/rand.eng.lcong/alg.pass.cpp