Debian Codegraph @

libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/ctor_iterator.pass.cpp

raw
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 InputIterator>
15 // piecewise_constant_distribution(InputIteratorB firstB,
16 // InputIteratorB lastB,
17 // InputIteratorW firstW);
18
19 // XFAIL: FROZEN-CXX03-HEADERS-FIXME
20
21 #include <random>
22
23 #include <cassert>
24 #include <vector>
25
26 #include "test_iterators.h"
27 #include "test_macros.h"
28
29 int main(int, char**)
30 {
31 {
32 typedef std::piecewise_constant_distribution<> D;
33 typedef cpp17_input_iterator<const double*> InIt;
34 double b[] = {10};
35 double p[] = {12};
36 D d((InIt(b)), (InIt(b)), (InIt(p)));
37 std::vector<double> iv = d.intervals();
38 assert(iv.size() == 2);
39 assert(iv[0] == 0);
40 assert(iv[1] == 1);
41 std::vector<double> dn = d.densities();
42 assert(dn.size() == 1);
43 assert(dn[0] == 1);
44 }
45 {
46 typedef std::piecewise_constant_distribution<> D;
47 double b[] = {10};
48 double p[] = {12};
49 D d(b, b+1, p);
50 std::vector<double> iv = d.intervals();
51 assert(iv.size() == 2);
52 assert(iv[0] == 0);
53 assert(iv[1] == 1);
54 std::vector<double> dn = d.densities();
55 assert(dn.size() == 1);
56 assert(dn[0] == 1);
57 }
58 {
59 typedef std::piecewise_constant_distribution<> D;
60 double b[] = {10, 15};
61 double p[] = {12};
62 D d(b, b+2, p);
63 std::vector<double> iv = d.intervals();
64 assert(iv.size() == 2);
65 assert(iv[0] == 10);
66 assert(iv[1] == 15);
67 std::vector<double> dn = d.densities();
68 assert(dn.size() == 1);
69 assert(dn[0] == 1/5.);
70 }
71 {
72 typedef std::piecewise_constant_distribution<> D;
73 double b[] = {10, 15, 16};
74 double p[] = {.25, .75};
75 D d(b, b+3, p);
76 std::vector<double> iv = d.intervals();
77 assert(iv.size() == 3);
78 assert(iv[0] == 10);
79 assert(iv[1] == 15);
80 assert(iv[2] == 16);
81 std::vector<double> dn = d.densities();
82 assert(dn.size() == 2);
83 assert(dn[0] == .25/5.);
84 assert(dn[1] == .75);
85 }
86 {
87 typedef std::piecewise_constant_distribution<> D;
88 double b[] = {10, 14, 16, 17};
89 double p[] = {25, 62.5, 12.5};
90 D d(b, b+4, p);
91 std::vector<double> iv = d.intervals();
92 assert(iv.size() == 4);
93 assert(iv[0] == 10);
94 assert(iv[1] == 14);
95 assert(iv[2] == 16);
96 assert(iv[3] == 17);
97 std::vector<double> dn = d.densities();
98 assert(dn.size() == 3);
99 assert(dn[0] == .0625);
100 assert(dn[1] == .3125);
101 assert(dn[2] == .125);
102 }
103
104 return 0;
105 }