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
main(int,char **)21 int 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() == 25214903918);
30 assert(e1() == 205774354444503);
31 assert(e1() == 158051849450892);
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 shrage's algorithm
41 typedef std::linear_congruential_engine<T, (1ull<<2), 0, (1ull<<63) + 1> E2;
42 E2 e2;
43 // make sure shrage's algorithm is used (it would be 0s otherwise)
44 assert(e2() == 4);
45 assert(e2() == 16);
46 assert(e2() == 64);
47 // make sure result is in bounds
48 assert(e2() < (1ull<<48) + 1);
49 assert(e2() < (1ull<<48) + 1);
50 assert(e2() < (1ull<<48) + 1);
51 assert(e2() < (1ull<<48) + 1);
52 assert(e2() < (1ull<<48) + 1);
53
54 // m will not overflow so we should not use shrage's algorithm
55 typedef std::linear_congruential_engine<T, 1ull, 1, (1ull<<48)> E3;
56 E3 e3;
57 // make sure the correct algorithm was used
58 assert(e3() == 2);
59 assert(e3() == 3);
60 assert(e3() == 4);
61 // make sure result is in bounds
62 assert(e3() < (1ull<<48));
63 assert(e3() < (1ull<<48));
64 assert(e3() < (1ull<<48));
65 assert(e3() < (1ull<<48));
66 assert(e2() < (1ull<<48));
67
68 return 0;
69 }