Debian Codegraph @

libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.discrete/param_ctor_func.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 IntType = int>
12 // class discrete_distribution
13
14 // template<class UnaryOperation>
15 // param_type(size_t nw, double xmin, double xmax,
16 // UnaryOperation fw);
17
18 // There is a bogus diagnostic about a too large allocation
19 // ADDITIONAL_COMPILE_FLAGS(gcc): -Wno-alloc-size-larger-than
20
21 #include <random>
22
23 #include <cassert>
24 #include <vector>
25
26 #include "test_macros.h"
27
28 double fw(double x)
29 {
30 return x+1;
31 }
32
33 int main(int, char**)
34 {
35 {
36 typedef std::discrete_distribution<> D;
37 typedef D::param_type P;
38 P pa(0, 0, 1, fw);
39 std::vector<double> p = pa.probabilities();
40 assert(p.size() == 1);
41 assert(p[0] == 1);
42 }
43 {
44 typedef std::discrete_distribution<> D;
45 typedef D::param_type P;
46 P pa(1, 0, 1, fw);
47 std::vector<double> p = pa.probabilities();
48 assert(p.size() == 1);
49 assert(p[0] == 1);
50 }
51 {
52 typedef std::discrete_distribution<> D;
53 typedef D::param_type P;
54 P pa(2, 0.5, 1.5, fw);
55 std::vector<double> p = pa.probabilities();
56 assert(p.size() == 2);
57 assert(p[0] == .4375);
58 assert(p[1] == .5625);
59 }
60 {
61 typedef std::discrete_distribution<> D;
62 typedef D::param_type P;
63 P pa(4, 0, 2, fw);
64 std::vector<double> p = pa.probabilities();
65 assert(p.size() == 4);
66 assert(p[0] == .15625);
67 assert(p[1] == .21875);
68 assert(p[2] == .28125);
69 }
70
71 return 0;
72 }