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 k> |
12 | // class shuffle_order_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 | |
40 | static TEST_CONSTEXPR result_type min() {return Min;} |
41 | static TEST_CONSTEXPR result_type max() {return Max;} |
42 | |
43 | explicit rand1(result_type sd = Min) : x_(sd) |
44 | { |
45 | if (x_ > Max) |
46 | x_ = Max; |
47 | } |
48 | |
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 |
61 | test1() |
62 | { |
63 | typedef std::knuth_b E; |
64 | |
65 | E e; |
66 | assert(e() == 152607844u); |
67 | } |
68 | |
69 | void |
70 | test2() |
71 | { |
72 | typedef rand1<unsigned long long, 0, 0xFFFFFFFFFFFFFFFFull> E0; |
73 | typedef std::shuffle_order_engine<E0, 101> E; |
74 | E e; |
75 | e.discard(400); |
76 | assert(e() == 501); |
77 | } |
78 | |
79 | void |
80 | test3() |
81 | { |
82 | typedef rand1<unsigned long long, 0, 0xFFFFFFFFFFFFFFFFull> E0; |
83 | typedef std::shuffle_order_engine<E0, 100> E; |
84 | E e; |
85 | e.discard(400); |
86 | assert(e() == 500); |
87 | } |
88 | |
89 | int main(int, char**) |
90 | { |
91 | test1(); |
92 | test2(); |
93 | test3(); |
94 | |
95 | return 0; |
96 | } |
97 | |