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
20 #include <cassert>
21 #include <vector>
22
23 #include "test_macros.h"
24
fw(double x)25 double fw(double x)
26 {
27 return 2*x;
28 }
29
main(int,char **)30 int main(int, char**)
31 {
32 {
33 typedef std::piecewise_linear_distribution<> D;
34 typedef D::param_type P;
35 P pa(0, 0, 1, fw);
36 std::vector<double> iv = pa.intervals();
37 assert(iv.size() == 2);
38 assert(iv[0] == 0);
39 assert(iv[1] == 1);
40 std::vector<double> dn = pa.densities();
41 assert(dn.size() == 2);
42 assert(dn[0] == 0);
43 assert(dn[1] == 2);
44 }
45 {
46 typedef std::piecewise_linear_distribution<> D;
47 typedef D::param_type P;
48 P pa(1, 10, 12, fw);
49 std::vector<double> iv = pa.intervals();
50 assert(iv.size() == 2);
51 assert(iv[0] == 10);
52 assert(iv[1] == 12);
53 std::vector<double> dn = pa.densities();
54 assert(dn.size() == 2);
55 assert(dn[0] == 20./44);
56 assert(dn[1] == 24./44);
57 }
58 {
59 typedef std::piecewise_linear_distribution<> D;
60 typedef D::param_type P;
61 P pa(2, 6, 14, fw);
62 std::vector<double> iv = pa.intervals();
63 assert(iv.size() == 3);
64 assert(iv[0] == 6);
65 assert(iv[1] == 10);
66 assert(iv[2] == 14);
67 std::vector<double> dn = pa.densities();
68 assert(dn.size() == 3);
69 assert(dn[0] == 0.075);
70 assert(dn[1] == 0.125);
71 assert(dn[2] == 0.175);
72 }
73
74 return 0;
75 }
76