Debian Codegraph @

libcxx/test/std/numerics/rand/rand.eng/rand.eng.lcong/values.pass.cpp

raw
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 // public:
15 // engine characteristics
16 // static constexpr result_type multiplier = a;
17 // static constexpr result_type increment = c;
18 // static constexpr result_type modulus = m;
19 // static constexpr result_type min() { return c == 0u ? 1u: 0u;}
20 // static constexpr result_type max() { return m - 1u;}
21 // static constexpr result_type default_seed = 1u;
22
23 #include <random>
24 #include <type_traits>
25 #include <cassert>
26
27 #include "test_macros.h"
28
29 template <class T>
30 void where(const T &) {}
31
32 template <class T, T a, T c, T m>
33 void
34 test1()
35 {
36 typedef std::linear_congruential_engine<T, a, c, m> LCE;
37 typedef typename LCE::result_type result_type;
38 static_assert((LCE::multiplier == a), "");
39 static_assert((LCE::increment == c), "");
40 static_assert((LCE::modulus == m), "");
41 #if TEST_STD_VER >= 11
42 static_assert((LCE::min() == (c == 0u ? 1u: 0u)), "");
43 #else
44 assert((LCE::min() == (c == 0u ? 1u: 0u)));
45 #endif
46
47 TEST_DIAGNOSTIC_PUSH
48 TEST_MSVC_DIAGNOSTIC_IGNORED(4310) // cast truncates constant value
49
50 #if TEST_STD_VER >= 11
51 static_assert((LCE::max() == result_type(m - 1u)), "");
52 #else
53 assert((LCE::max() == result_type(m - 1u)));
54 #endif
55
56 TEST_DIAGNOSTIC_POP
57
58 static_assert((LCE::default_seed == 1), "");
59 where(LCE::multiplier);
60 where(LCE::increment);
61 where(LCE::modulus);
62 where(LCE::default_seed);
63 }
64
65 template <class T>
66 void
67 test()
68 {
69 const int W = sizeof(T) * CHAR_BIT;
70 const T M(static_cast<T>(-1));
71 const T A(static_cast<T>((static_cast<T>(1) << (W / 2)) - 1));
72
73 // Cases where m = 0
74 test1<T, 0, 0, 0>();
75 test1<T, A, 0, 0>();
76 test1<T, 0, 1, 0>();
77 test1<T, A, 1, 0>();
78
79 // Cases where m = 2^n for n < w
80 test1<T, 0, 0, 256>();
81 test1<T, 5, 0, 256>();
82 test1<T, 0, 1, 256>();
83 test1<T, 5, 1, 256>();
84
85 // Cases where m is odd and a = 0
86 test1<T, 0, 0, M>();
87 test1<T, 0, M - 2, M>();
88 test1<T, 0, M - 1, M>();
89
90 // Cases where m is odd and m % a <= m / a (Schrage)
91 test1<T, A, 0, M>();
92 test1<T, A, M - 2, M>();
93 test1<T, A, M - 1, M>();
94 }
95
96 template <class T>
97 void test_ext() {
98 const T M(static_cast<T>(-1));
99
100 // Cases where m is odd and m % a > m / a
101 test1<T, M - 2, 0, M>();
102 test1<T, M - 2, M - 2, M>();
103 test1<T, M - 2, M - 1, M>();
104 test1<T, M - 1, 0, M>();
105 test1<T, M - 1, M - 2, M>();
106 test1<T, M - 1, M - 1, M>();
107 }
108
109 int main(int, char**)
110 {
111 test<unsigned short>();
112 test_ext<unsigned short>();
113 test<unsigned int>();
114 test_ext<unsigned int>();
115 test<unsigned long>();
116 test_ext<unsigned long>();
117 test<unsigned long long>();
118 // This isn't implemented on platforms without __int128
119 #ifndef _LIBCPP_HAS_NO_INT128
120 test_ext<unsigned long long>();
121 #endif
122
123 return 0;
124 }