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 Engine, size_t w, class UIntType>
12 // class independent_bits_engine
13 
14 // result_type operator()();
15 
16 #include <random>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 template <class UIntType, UIntType Min, UIntType Max>
22 class rand1
23 {
24 public:
25     // types
26     typedef UIntType result_type;
27 
28 private:
29     result_type x_;
30 
31     static_assert(Min < Max, "rand1 invalid parameters");
32 public:
33 
34 #if TEST_STD_VER < 11 && defined(_LIBCPP_VERSION)
35     // Workaround for lack of constexpr in C++03
36     static const result_type _Min = Min;
37     static const result_type _Max = Max;
38 #endif
39 
min()40     static TEST_CONSTEXPR result_type min() {return Min;}
max()41     static TEST_CONSTEXPR result_type max() {return Max;}
42 
rand1(result_type sd=Min)43     explicit rand1(result_type sd = Min) : x_(sd)
44     {
45         if (x_ > Max)
46             x_ = Max;
47     }
48 
operator ()()49     result_type operator()()
50     {
51         result_type r = x_;
52         if (x_ < Max)
53             ++x_;
54         else
55             x_ = Min;
56         return r;
57     }
58 };
59 
60 void
test1()61 test1()
62 {
63    typedef std::independent_bits_engine<rand1<unsigned, 0, 10>, 16, unsigned> E;
64 
65     E e;
66     assert(e() == 6958);
67 }
68 
69 void
test2()70 test2()
71 {
72     typedef std::independent_bits_engine<rand1<unsigned, 0, 100>, 16, unsigned> E;
73 
74     E e;
75     assert(e() == 66);
76 }
77 
78 void
test3()79 test3()
80 {
81     typedef std::independent_bits_engine<rand1<unsigned, 0, 0xFFFFFFFF>, 32, unsigned> E;
82 
83     E e(5);
84     assert(e() == 5);
85 }
86 
87 void
test4()88 test4()
89 {
90     typedef std::independent_bits_engine<rand1<unsigned, 0, 0xFFFFFFFF>, 7, unsigned> E;
91 
92     E e(129);
93     assert(e() == 1);
94 }
95 
96 void
test5()97 test5()
98 {
99     typedef std::independent_bits_engine<rand1<unsigned, 2, 3>, 1, unsigned> E;
100 
101     E e(6);
102     assert(e() == 1);
103 }
104 
105 void
test6()106 test6()
107 {
108     typedef std::independent_bits_engine<rand1<unsigned, 2, 3>, 11, unsigned> E;
109 
110     E e(6);
111     assert(e() == 1365);
112 }
113 
114 void
test7()115 test7()
116 {
117     typedef std::independent_bits_engine<rand1<unsigned, 2, 3>, 32, unsigned> E;
118 
119     E e(6);
120     assert(e() == 2863311530u);
121 }
122 
123 void
test8()124 test8()
125 {
126     typedef std::independent_bits_engine<std::mt19937, 64, unsigned long long> E;
127 
128     E e(6);
129     assert(e() == 16470362623952407241ull);
130 }
131 
main(int,char **)132 int main(int, char**)
133 {
134     test1();
135     test2();
136     test3();
137     test4();
138     test5();
139     test6();
140     test7();
141     test8();
142 
143   return 0;
144 }
145