| 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 | // discrete_distribution(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 | D d(0, 0, 1, fw); |
| 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 | D d(1, 0, 1, fw); |
| 45 | std::vector<double> p = d.probabilities(); |
| 46 | assert(p.size() == 1); |
| 47 | assert(p[0] == 1); |
| 48 | } |
| 49 | { |
| 50 | typedef std::discrete_distribution<> D; |
| 51 | D d(2, 0.5, 1.5, fw); |
| 52 | std::vector<double> p = d.probabilities(); |
| 53 | assert(p.size() == 2); |
| 54 | assert(p[0] == .4375); |
| 55 | assert(p[1] == .5625); |
| 56 | } |
| 57 | { |
| 58 | typedef std::discrete_distribution<> D; |
| 59 | D d(4, 0, 2, fw); |
| 60 | std::vector<double> p = d.probabilities(); |
| 61 | assert(p.size() == 4); |
| 62 | assert(p[0] == .15625); |
| 63 | assert(p[1] == .21875); |
| 64 | assert(p[2] == .28125); |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |