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