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