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 IntType = int>
12// class discrete_distribution
13
14// template<class InputIterator>
15// discrete_distribution(InputIterator firstW, InputIterator lastW);
16
17#include <random>
18
19#include <cassert>
20#include <vector>
21
22#include "test_macros.h"
23
24int main(int, char**)
25{
26 {
27 typedef std::discrete_distribution<> D;
28 double p0[] = {1};
29 D d(p0, p0);
30 std::vector<double> p = d.probabilities();
31 assert(p.size() == 1);
32 assert(p[0] == 1);
33 }
34 {
35 typedef std::discrete_distribution<> D;
36 double p0[] = {10};
37 D d(p0, p0+1);
38 std::vector<double> p = d.probabilities();
39 assert(p.size() == 1);
40 assert(p[0] == 1);
41 }
42 {
43 typedef std::discrete_distribution<> D;
44 double p0[] = {10, 30};
45 D d(p0, p0+2);
46 std::vector<double> p = d.probabilities();
47 assert(p.size() == 2);
48 assert(p[0] == 0.25);
49 assert(p[1] == 0.75);
50 }
51 {
52 typedef std::discrete_distribution<> D;
53 double p0[] = {30, 10};
54 D d(p0, p0+2);
55 std::vector<double> p = d.probabilities();
56 assert(p.size() == 2);
57 assert(p[0] == 0.75);
58 assert(p[1] == 0.25);
59 }
60 {
61 typedef std::discrete_distribution<> D;
62 double p0[] = {30, 0, 10};
63 D d(p0, p0+3);
64 std::vector<double> p = d.probabilities();
65 assert(p.size() == 3);
66 assert(p[0] == 0.75);
67 assert(p[1] == 0);
68 assert(p[2] == 0.25);
69 }
70 {
71 typedef std::discrete_distribution<> D;
72 double p0[] = {0, 30, 10};
73 D d(p0, p0+3);
74 std::vector<double> p = d.probabilities();
75 assert(p.size() == 3);
76 assert(p[0] == 0);
77 assert(p[1] == 0.75);
78 assert(p[2] == 0.25);
79 }
80 {
81 typedef std::discrete_distribution<> D;
82 double p0[] = {0, 0, 10};
83 D d(p0, p0+3);
84 std::vector<double> p = d.probabilities();
85 assert(p.size() == 3);
86 assert(p[0] == 0);
87 assert(p[1] == 0);
88 assert(p[2] == 1);
89 }
90
91 return 0;
92}
93

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