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_constant_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_constant_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() == 1);
40 assert(dn[0] == 1);
41 }
42 {
43 typedef std::piecewise_constant_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() == 1);
52 assert(dn[0] == 0.5);
53 }
54 {
55 typedef std::piecewise_constant_distribution<> D;
56 typedef D::param_type P;
57 P pa(2, 6, 14, fw);
58 std::vector<double> iv = pa.intervals();
59 assert(iv.size() == 3);
60 assert(iv[0] == 6);
61 assert(iv[1] == 10);
62 assert(iv[2] == 14);
63 std::vector<double> dn = pa.densities();
64 assert(dn.size() == 2);
65 assert(dn[0] == 0.1);
66 assert(dn[1] == 0.15);
67 }
68
69 return 0;
70 }
71