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