| 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 RealType = double> |
| 14 | // class piecewise_constant_distribution |
| 15 | |
| 16 | // param_type(initializer_list<result_type> bl, UnaryOperation fw); |
| 17 | |
| 18 | #include <random> |
| 19 | |
| 20 | #include <cassert> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | |
| 25 | double f(double x) |
| 26 | { |
| 27 | return x*2; |
| 28 | } |
| 29 | |
| 30 | int main(int, char**) |
| 31 | { |
| 32 | { |
| 33 | typedef std::piecewise_constant_distribution<> D; |
| 34 | typedef D::param_type P; |
| 35 | P pa({}, f); |
| 36 | std::vector<double> iv = pa.intervals(); |
| 37 | assert(iv.size() == 2); |
| 38 | assert(iv[0] == 0); |
| 39 | assert(iv[1] == 1); |
| 40 | std::vector<double> dn = pa.densities(); |
| 41 | assert(dn.size() == 1); |
| 42 | assert(dn[0] == 1); |
| 43 | } |
| 44 | { |
| 45 | typedef std::piecewise_constant_distribution<> D; |
| 46 | typedef D::param_type P; |
| 47 | P pa({12}, f); |
| 48 | std::vector<double> iv = pa.intervals(); |
| 49 | assert(iv.size() == 2); |
| 50 | assert(iv[0] == 0); |
| 51 | assert(iv[1] == 1); |
| 52 | std::vector<double> dn = pa.densities(); |
| 53 | assert(dn.size() == 1); |
| 54 | assert(dn[0] == 1); |
| 55 | } |
| 56 | { |
| 57 | typedef std::piecewise_constant_distribution<> D; |
| 58 | typedef D::param_type P; |
| 59 | P pa({12, 14}, f); |
| 60 | std::vector<double> iv = pa.intervals(); |
| 61 | assert(iv.size() == 2); |
| 62 | assert(iv[0] == 12); |
| 63 | assert(iv[1] == 14); |
| 64 | std::vector<double> dn = pa.densities(); |
| 65 | assert(dn.size() == 1); |
| 66 | assert(dn[0] == 0.5); |
| 67 | } |
| 68 | { |
| 69 | typedef std::piecewise_constant_distribution<> D; |
| 70 | typedef D::param_type P; |
| 71 | P pa({5.5, 7.5, 11.5}, f); |
| 72 | std::vector<double> iv = pa.intervals(); |
| 73 | assert(iv.size() == 3); |
| 74 | assert(iv[0] == 5.5); |
| 75 | assert(iv[1] == 7.5); |
| 76 | assert(iv[2] == 11.5); |
| 77 | std::vector<double> dn = pa.densities(); |
| 78 | assert(dn.size() == 2); |
| 79 | assert(dn[0] == 0.203125); |
| 80 | assert(dn[1] == 0.1484375); |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |