Debian Codegraph @

libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/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_linear_distribution
13
14 // template<class InputIterator>
15 // piecewise_linear_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_linear_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() == 2);
43 assert(dn[0] == 1);
44 assert(dn[1] == 1);
45 }
46 {
47 typedef std::piecewise_linear_distribution<> D;
48 double b[] = {10};
49 double p[] = {12};
50 D d(b, b+1, p);
51 std::vector<double> iv = d.intervals();
52 assert(iv.size() == 2);
53 assert(iv[0] == 0);
54 assert(iv[1] == 1);
55 std::vector<double> dn = d.densities();
56 assert(dn.size() == 2);
57 assert(dn[0] == 1);
58 assert(dn[1] == 1);
59 }
60 {
61 typedef std::piecewise_linear_distribution<> D;
62 double b[] = {10, 15};
63 double p[] = {20, 20};
64 D d(b, b+2, p);
65 std::vector<double> iv = d.intervals();
66 assert(iv.size() == 2);
67 assert(iv[0] == 10);
68 assert(iv[1] == 15);
69 std::vector<double> dn = d.densities();
70 assert(dn.size() == 2);
71 assert(dn[0] == 1/5.);
72 assert(dn[1] == 1/5.);
73 }
74 {
75 typedef std::piecewise_linear_distribution<> D;
76 double b[] = {10, 15, 16};
77 double p[] = {.25, .75, .25};
78 D d(b, b+3, p);
79 std::vector<double> iv = d.intervals();
80 assert(iv.size() == 3);
81 assert(iv[0] == 10);
82 assert(iv[1] == 15);
83 assert(iv[2] == 16);
84 std::vector<double> dn = d.densities();
85 assert(dn.size() == 3);
86 assert(dn[0] == .25/3);
87 assert(dn[1] == .75/3);
88 assert(dn[2] == .25/3);
89 }
90 {
91 typedef std::piecewise_linear_distribution<> D;
92 double b[] = {10, 14, 16, 17};
93 double p[] = {0, 1, 1, 0};
94 D d(b, b+4, p);
95 std::vector<double> iv = d.intervals();
96 assert(iv.size() == 4);
97 assert(iv[0] == 10);
98 assert(iv[1] == 14);
99 assert(iv[2] == 16);
100 assert(iv[3] == 17);
101 std::vector<double> dn = d.densities();
102 assert(dn.size() == 4);
103 assert(dn[0] == 0);
104 assert(dn[1] == 1/4.5);
105 assert(dn[2] == 1/4.5);
106 assert(dn[3] == 0);
107 }
108
109 return 0;
110 }