| 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 RealType = double> |
| 12 | // class piecewise_linear_distribution |
| 13 | |
| 14 | // template<class UnaryOperation> |
| 15 | // piecewise_linear_distribution(size_t nw, result_type xmin, |
| 16 | // result_type xmax, UnaryOperation fw); |
| 17 | |
| 18 | #include <random> |
| 19 | |
| 20 | #include <cassert> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | |
| 25 | double fw(double x) |
| 26 | { |
| 27 | return 2*x; |
| 28 | } |
| 29 | |
| 30 | int main(int, char**) |
| 31 | { |
| 32 | { |
| 33 | typedef std::piecewise_linear_distribution<> D; |
| 34 | D d(0, 0, 1, fw); |
| 35 | std::vector<double> iv = d.intervals(); |
| 36 | assert(iv.size() == 2); |
| 37 | assert(iv[0] == 0); |
| 38 | assert(iv[1] == 1); |
| 39 | std::vector<double> dn = d.densities(); |
| 40 | assert(dn.size() == 2); |
| 41 | assert(dn[0] == 0); |
| 42 | assert(dn[1] == 2); |
| 43 | } |
| 44 | { |
| 45 | typedef std::piecewise_linear_distribution<> D; |
| 46 | D d(1, 10, 12, fw); |
| 47 | std::vector<double> iv = d.intervals(); |
| 48 | assert(iv.size() == 2); |
| 49 | assert(iv[0] == 10); |
| 50 | assert(iv[1] == 12); |
| 51 | std::vector<double> dn = d.densities(); |
| 52 | assert(dn.size() == 2); |
| 53 | assert(dn[0] == 20./44); |
| 54 | assert(dn[1] == 24./44); |
| 55 | } |
| 56 | { |
| 57 | typedef std::piecewise_linear_distribution<> D; |
| 58 | D d(2, 6, 14, fw); |
| 59 | std::vector<double> iv = d.intervals(); |
| 60 | assert(iv.size() == 3); |
| 61 | assert(iv[0] == 6); |
| 62 | assert(iv[1] == 10); |
| 63 | assert(iv[2] == 14); |
| 64 | std::vector<double> dn = d.densities(); |
| 65 | assert(dn.size() == 3); |
| 66 | assert(dn[0] == 0.075); |
| 67 | assert(dn[1] == 0.125); |
| 68 | assert(dn[2] == 0.175); |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |