Home | History | Annotate | Download | only in rand.adapt.shuf
      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 k>
     13 // class shuffle_order_engine
     14 
     15 // result_type operator()();
     16 
     17 #include <random>
     18 #include <cassert>
     19 
     20 template <class UIntType, UIntType Min, UIntType Max>
     21 class rand1
     22 {
     23 public:
     24     // types
     25     typedef UIntType result_type;
     26 
     27 private:
     28     result_type x_;
     29 
     30     static_assert(Min < Max, "rand1 invalid parameters");
     31 public:
     32 
     33 #ifdef _LIBCPP_HAS_NO_CONSTEXPR
     34     // Workaround for lack of constexpr in C++03
     35     static const result_type _Min = Min;
     36     static const result_type _Max = Max;
     37 #endif
     38 
     39     static _LIBCPP_CONSTEXPR result_type min() {return Min;}
     40     static _LIBCPP_CONSTEXPR result_type max() {return Max;}
     41 
     42     explicit rand1(result_type sd = Min) : x_(sd)
     43     {
     44         if (x_ > Max)
     45             x_ = Max;
     46     }
     47 
     48     result_type operator()()
     49     {
     50         result_type r = x_;
     51         if (x_ < Max)
     52             ++x_;
     53         else
     54             x_ = Min;
     55         return r;
     56     }
     57 };
     58 
     59 void
     60 test1()
     61 {
     62    typedef std::knuth_b E;
     63 
     64     E e;
     65     assert(e() == 152607844u);
     66 }
     67 
     68 void
     69 test2()
     70 {
     71     typedef rand1<unsigned long long, 0, 0xFFFFFFFFFFFFFFFFull> E0;
     72     typedef std::shuffle_order_engine<E0, 101> E;
     73     E e;
     74     e.discard(400);
     75     assert(e() == 501);
     76 }
     77 
     78 void
     79 test3()
     80 {
     81     typedef rand1<unsigned long long, 0, 0xFFFFFFFFFFFFFFFFull> E0;
     82     typedef std::shuffle_order_engine<E0, 100> E;
     83     E e;
     84     e.discard(400);
     85     assert(e() == 500);
     86 }
     87 
     88 int main()
     89 {
     90     test1();
     91     test2();
     92     test3();
     93 }
     94