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#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_constant_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() == 1);
39 assert(dn[0] == 1);
40 }
41 {
42 typedef std::piecewise_constant_distribution<> D;
43 double b[] = {10};
44 double p[] = {12};
45 D d(b, b+1, p);
46 std::vector<double> iv = d.intervals();
47 assert(iv.size() == 2);
48 assert(iv[0] == 0);
49 assert(iv[1] == 1);
50 std::vector<double> dn = d.densities();
51 assert(dn.size() == 1);
52 assert(dn[0] == 1);
53 }
54 {
55 typedef std::piecewise_constant_distribution<> D;
56 double b[] = {10, 15};
57 double p[] = {12};
58 D d(b, b+2, p);
59 std::vector<double> iv = d.intervals();
60 assert(iv.size() == 2);
61 assert(iv[0] == 10);
62 assert(iv[1] == 15);
63 std::vector<double> dn = d.densities();
64 assert(dn.size() == 1);
65 assert(dn[0] == 1/5.);
66 }
67 {
68 typedef std::piecewise_constant_distribution<> D;
69 double b[] = {10, 15, 16};
70 double p[] = {.25, .75};
71 D d(b, b+3, p);
72 std::vector<double> iv = d.intervals();
73 assert(iv.size() == 3);
74 assert(iv[0] == 10);
75 assert(iv[1] == 15);
76 assert(iv[2] == 16);
77 std::vector<double> dn = d.densities();
78 assert(dn.size() == 2);
79 assert(dn[0] == .25/5.);
80 assert(dn[1] == .75);
81 }
82 {
83 typedef std::piecewise_constant_distribution<> D;
84 double b[] = {10, 14, 16, 17};
85 double p[] = {25, 62.5, 12.5};
86 D d(b, b+4, p);
87 std::vector<double> iv = d.intervals();
88 assert(iv.size() == 4);
89 assert(iv[0] == 10);
90 assert(iv[1] == 14);
91 assert(iv[2] == 16);
92 assert(iv[3] == 17);
93 std::vector<double> dn = d.densities();
94 assert(dn.size() == 3);
95 assert(dn[0] == .0625);
96 assert(dn[1] == .3125);
97 assert(dn[2] == .125);
98 }
99
100 return 0;
101}
102

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