1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <random>
10 
11 // template<class RealType = double>
12 // class piecewise_linear_distribution
13 
14 // template<class UnaryOperation>
15 //     param_type(size_t nw, double xmin, double xmax,
16 //                           UnaryOperation fw);
17 
18 #include <random>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 
fw(double x)23 double fw(double x)
24 {
25     return 2*x;
26 }
27 
main(int,char **)28 int main(int, char**)
29 {
30     {
31         typedef std::piecewise_linear_distribution<> D;
32         typedef D::param_type P;
33         P pa(0, 0, 1, fw);
34         std::vector<double> iv = pa.intervals();
35         assert(iv.size() == 2);
36         assert(iv[0] == 0);
37         assert(iv[1] == 1);
38         std::vector<double> dn = pa.densities();
39         assert(dn.size() == 2);
40         assert(dn[0] == 0);
41         assert(dn[1] == 2);
42     }
43     {
44         typedef std::piecewise_linear_distribution<> D;
45         typedef D::param_type P;
46         P pa(1, 10, 12, fw);
47         std::vector<double> iv = pa.intervals();
48         assert(iv.size() == 2);
49         assert(iv[0] == 10);
50         assert(iv[1] == 12);
51         std::vector<double> dn = pa.densities();
52         assert(dn.size() == 2);
53         assert(dn[0] == 20./44);
54         assert(dn[1] == 24./44);
55     }
56     {
57         typedef std::piecewise_linear_distribution<> D;
58         typedef D::param_type P;
59         P pa(2, 6, 14, fw);
60         std::vector<double> iv = pa.intervals();
61         assert(iv.size() == 3);
62         assert(iv[0] == 6);
63         assert(iv[1] == 10);
64         assert(iv[2] == 14);
65         std::vector<double> dn = pa.densities();
66         assert(dn.size() == 3);
67         assert(dn[0] == 0.075);
68         assert(dn[1] == 0.125);
69         assert(dn[2] == 0.175);
70     }
71 
72   return 0;
73 }
74