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 // 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_linear_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() == 2);
39 assert(dn[0] == 0);
40 assert(dn[1] == 2);
41 }
42 {
43 typedef std::piecewise_linear_distribution<> D;
44 typedef D::param_type P;
45 P pa(1, 10, 12, fw);
46 std::vector<double> iv = pa.intervals();
47 assert(iv.size() == 2);
48 assert(iv[0] == 10);
49 assert(iv[1] == 12);
50 std::vector<double> dn = pa.densities();
51 assert(dn.size() == 2);
52 assert(dn[0] == 20./44);
53 assert(dn[1] == 24./44);
54 }
55 {
56 typedef std::piecewise_linear_distribution<> D;
57 typedef D::param_type P;
58 P pa(2, 6, 14, fw);
59 std::vector<double> iv = pa.intervals();
60 assert(iv.size() == 3);
61 assert(iv[0] == 6);
62 assert(iv[1] == 10);
63 assert(iv[2] == 14);
64 std::vector<double> dn = pa.densities();
65 assert(dn.size() == 3);
66 assert(dn[0] == 0.075);
67 assert(dn[1] == 0.125);
68 assert(dn[2] == 0.175);
69 }
70 }
71