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_constant_distribution
14 
15 // template<class UnaryOperation>
16 //     param_type(size_t nw, double xmin, double xmax,
17 //                           UnaryOperation fw);
18 
19 #include <random>
20 #include <cassert>
21 
fw(double x)22 double fw(double x)
23 {
24     return 2*x;
25 }
26 
main()27 int main()
28 {
29     {
30         typedef std::piecewise_constant_distribution<> D;
31         typedef D::param_type P;
32         P pa(0, 0, 1, fw);
33         std::vector<double> iv = pa.intervals();
34         assert(iv.size() == 2);
35         assert(iv[0] == 0);
36         assert(iv[1] == 1);
37         std::vector<double> dn = pa.densities();
38         assert(dn.size() == 1);
39         assert(dn[0] == 1);
40     }
41     {
42         typedef std::piecewise_constant_distribution<> D;
43         typedef D::param_type P;
44         P pa(1, 10, 12, fw);
45         std::vector<double> iv = pa.intervals();
46         assert(iv.size() == 2);
47         assert(iv[0] == 10);
48         assert(iv[1] == 12);
49         std::vector<double> dn = pa.densities();
50         assert(dn.size() == 1);
51         assert(dn[0] == 0.5);
52     }
53     {
54         typedef std::piecewise_constant_distribution<> D;
55         typedef D::param_type P;
56         P pa(2, 6, 14, fw);
57         std::vector<double> iv = pa.intervals();
58         assert(iv.size() == 3);
59         assert(iv[0] == 6);
60         assert(iv[1] == 10);
61         assert(iv[2] == 14);
62         std::vector<double> dn = pa.densities();
63         assert(dn.size() == 2);
64         assert(dn[0] == 0.1);
65         assert(dn[1] == 0.15);
66     }
67 }
68