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#include <random>
20
21#include <cassert>
22#include <vector>
23
24#include "test_macros.h"
25
26int main(int, char**)
27{
28 {
29 typedef std::piecewise_linear_distribution<> D;
30 double b[] = {10};
31 double p[] = {12};
32 D d(b, b, p);
33 std::vector<double> iv = d.intervals();
34 assert(iv.size() == 2);
35 assert(iv[0] == 0);
36 assert(iv[1] == 1);
37 std::vector<double> dn = d.densities();
38 assert(dn.size() == 2);
39 assert(dn[0] == 1);
40 assert(dn[1] == 1);
41 }
42 {
43 typedef std::piecewise_linear_distribution<> D;
44 double b[] = {10};
45 double p[] = {12};
46 D d(b, b+1, p);
47 std::vector<double> iv = d.intervals();
48 assert(iv.size() == 2);
49 assert(iv[0] == 0);
50 assert(iv[1] == 1);
51 std::vector<double> dn = d.densities();
52 assert(dn.size() == 2);
53 assert(dn[0] == 1);
54 assert(dn[1] == 1);
55 }
56 {
57 typedef std::piecewise_linear_distribution<> D;
58 double b[] = {10, 15};
59 double p[] = {20, 20};
60 D d(b, b+2, p);
61 std::vector<double> iv = d.intervals();
62 assert(iv.size() == 2);
63 assert(iv[0] == 10);
64 assert(iv[1] == 15);
65 std::vector<double> dn = d.densities();
66 assert(dn.size() == 2);
67 assert(dn[0] == 1/5.);
68 assert(dn[1] == 1/5.);
69 }
70 {
71 typedef std::piecewise_linear_distribution<> D;
72 double b[] = {10, 15, 16};
73 double p[] = {.25, .75, .25};
74 D d(b, b+3, p);
75 std::vector<double> iv = d.intervals();
76 assert(iv.size() == 3);
77 assert(iv[0] == 10);
78 assert(iv[1] == 15);
79 assert(iv[2] == 16);
80 std::vector<double> dn = d.densities();
81 assert(dn.size() == 3);
82 assert(dn[0] == .25/3);
83 assert(dn[1] == .75/3);
84 assert(dn[2] == .25/3);
85 }
86 {
87 typedef std::piecewise_linear_distribution<> D;
88 double b[] = {10, 14, 16, 17};
89 double p[] = {0, 1, 1, 0};
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() == 4);
99 assert(dn[0] == 0);
100 assert(dn[1] == 1/4.5);
101 assert(dn[2] == 1/4.5);
102 assert(dn[3] == 0);
103 }
104
105 return 0;
106}
107

source code of libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/ctor_iterator.pass.cpp