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 #include "test_macros.h"
     21 
     22 template <class UIntType, UIntType Min, UIntType Max>
     23 class rand1
     24 {
     25 public:
     26     // types
     27     typedef UIntType result_type;
     28 
     29 private:
     30     result_type x_;
     31 
     32     static_assert(Min < Max, "rand1 invalid parameters");
     33 public:
     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 
     61 void
     62 test1()
     63 {
     64    typedef std::knuth_b E;
     65 
     66     E e;
     67     assert(e() == 152607844u);
     68 }
     69 
     70 void
     71 test2()
     72 {
     73     typedef rand1<unsigned long long, 0, 0xFFFFFFFFFFFFFFFFull> E0;
     74     typedef std::shuffle_order_engine<E0, 101> E;
     75     E e;
     76     e.discard(400);
     77     assert(e() == 501);
     78 }
     79 
     80 void
     81 test3()
     82 {
     83     typedef rand1<unsigned long long, 0, 0xFFFFFFFFFFFFFFFFull> E0;
     84     typedef std::shuffle_order_engine<E0, 100> E;
     85     E e;
     86     e.discard(400);
     87     assert(e() == 500);
     88 }
     89 
     90 int main()
     91 {
     92     test1();
     93     test2();
     94     test3();
     95 }
     96