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
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_constant_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() == 1);
42 assert(dn[0] == 1);
43 }
44 {
45 typedef std::piecewise_constant_distribution<> D;
46 typedef D::param_type P;
47 P pa(1, 10, 12, fw);
48 std::vector<double> iv = pa.intervals();
49 assert(iv.size() == 2);
50 assert(iv[0] == 10);
51 assert(iv[1] == 12);
52 std::vector<double> dn = pa.densities();
53 assert(dn.size() == 1);
54 assert(dn[0] == 0.5);
55 }
56 {
57 typedef std::piecewise_constant_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() == 2);
67 assert(dn[0] == 0.1);
68 assert(dn[1] == 0.15);
69 }
70
71 return 0;
72 }
73