97 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <random>10 11// template<class Engine, size_t k>12// class shuffle_order_engine13 14// result_type operator()();15 16#include <random>17#include <cassert>18 19#include "test_macros.h"20 21template <class UIntType, UIntType Min, UIntType Max>22class rand123{24public:25 // types26 typedef UIntType result_type;27 28private:29 result_type x_;30 31 static_assert(Min < Max, "rand1 invalid parameters");32public:33 34#if TEST_STD_VER < 11 && defined(_LIBCPP_VERSION)35 // Workaround for lack of constexpr in C++0336 static const result_type _Min = Min;37 static const result_type _Max = Max;38#endif39 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 else55 x_ = Min;56 return r;57 }58};59 60void61test1()62{63 typedef std::knuth_b E;64 65 E e;66 assert(e() == 152607844u);67}68 69void70test2()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 79void80test3()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 89int main(int, char**)90{91 test1();92 test2();93 test3();94 95 return 0;96}97