Home | History | Annotate | Download | only in rand.dist.samp.plinear
      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 RealType = double>
     13 // class piecewise_linear_distribution
     14 
     15 // piecewise_linear_distribution(initializer_list<result_type> bl,
     16 //                                 UnaryOperation fw);
     17 
     18 #include <iostream>
     19 
     20 #include <random>
     21 #include <cassert>
     22 
     23 double f(double x)
     24 {
     25     return x*2;
     26 }
     27 
     28 int main()
     29 {
     30 #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
     31     {
     32         typedef std::piecewise_linear_distribution<> D;
     33         D d({}, f);
     34         std::vector<double> iv = d.intervals();
     35         assert(iv.size() == 2);
     36         assert(iv[0] == 0);
     37         assert(iv[1] == 1);
     38         std::vector<double> dn = d.densities();
     39         assert(dn.size() == 2);
     40         assert(dn[0] == 1);
     41         assert(dn[1] == 1);
     42     }
     43     {
     44         typedef std::piecewise_linear_distribution<> D;
     45         D d({12}, f);
     46         std::vector<double> iv = d.intervals();
     47         assert(iv.size() == 2);
     48         assert(iv[0] == 0);
     49         assert(iv[1] == 1);
     50         std::vector<double> dn = d.densities();
     51         assert(dn.size() == 2);
     52         assert(dn[0] == 1);
     53         assert(dn[1] == 1);
     54     }
     55     {
     56         typedef std::piecewise_linear_distribution<> D;
     57         D d({10, 12}, f);
     58         std::vector<double> iv = d.intervals();
     59         assert(iv.size() == 2);
     60         assert(iv[0] == 10);
     61         assert(iv[1] == 12);
     62         std::vector<double> dn = d.densities();
     63         assert(dn.size() == 2);
     64         assert(dn[0] == 20./44);
     65         assert(dn[1] == 24./44);
     66     }
     67     {
     68         typedef std::piecewise_linear_distribution<> D;
     69         D d({6, 10, 14}, f);
     70         std::vector<double> iv = d.intervals();
     71         assert(iv.size() == 3);
     72         assert(iv[0] == 6);
     73         assert(iv[1] == 10);
     74         assert(iv[2] == 14);
     75         std::vector<double> dn = d.densities();
     76         assert(dn.size() == 3);
     77         assert(dn[0] == 0.075);
     78         assert(dn[1] == 0.125);
     79         assert(dn[2] == 0.175);
     80     }
     81 #endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
     82 }
     83