| 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 InputIterator> |
| 15 | // param_type(InputIteratorB firstB, InputIteratorB lastB, |
| 16 | // InputIteratorW firstW); |
| 17 | |
| 18 | #include <random> |
| 19 | |
| 20 | #include <cassert> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | |
| 25 | int main(int, char**) |
| 26 | { |
| 27 | { |
| 28 | typedef std::piecewise_linear_distribution<> D; |
| 29 | typedef D::param_type P; |
| 30 | double b[] = {10}; |
| 31 | double p[] = {12}; |
| 32 | P pa(b, b, p); |
| 33 | std::vector<double> iv = pa.intervals(); |
| 34 | assert(iv.size() == 2); |
| 35 | assert(iv[0] == 0); |
| 36 | assert(iv[1] == 1); |
| 37 | std::vector<double> dn = pa.densities(); |
| 38 | assert(dn.size() == 2); |
| 39 | assert(dn[0] == 1); |
| 40 | assert(dn[1] == 1); |
| 41 | } |
| 42 | { |
| 43 | typedef std::piecewise_linear_distribution<> D; |
| 44 | typedef D::param_type P; |
| 45 | double b[] = {10}; |
| 46 | double p[] = {12}; |
| 47 | P pa(b, b+1, p); |
| 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() == 2); |
| 54 | assert(dn[0] == 1); |
| 55 | assert(dn[1] == 1); |
| 56 | } |
| 57 | { |
| 58 | typedef std::piecewise_linear_distribution<> D; |
| 59 | typedef D::param_type P; |
| 60 | double b[] = {10, 15}; |
| 61 | double p[] = {12, 12}; |
| 62 | P pa(b, b+2, p); |
| 63 | std::vector<double> iv = pa.intervals(); |
| 64 | assert(iv.size() == 2); |
| 65 | assert(iv[0] == 10); |
| 66 | assert(iv[1] == 15); |
| 67 | std::vector<double> dn = pa.densities(); |
| 68 | assert(dn.size() == 2); |
| 69 | assert(dn[0] == 1/5.); |
| 70 | assert(dn[1] == 1/5.); |
| 71 | } |
| 72 | { |
| 73 | typedef std::piecewise_linear_distribution<> D; |
| 74 | typedef D::param_type P; |
| 75 | double b[] = {10, 15, 16}; |
| 76 | double p[] = {.25, .75, .25}; |
| 77 | P pa(b, b+3, p); |
| 78 | std::vector<double> iv = pa.intervals(); |
| 79 | assert(iv.size() == 3); |
| 80 | assert(iv[0] == 10); |
| 81 | assert(iv[1] == 15); |
| 82 | assert(iv[2] == 16); |
| 83 | std::vector<double> dn = pa.densities(); |
| 84 | assert(dn.size() == 3); |
| 85 | assert(dn[0] == .25/3); |
| 86 | assert(dn[1] == .75/3); |
| 87 | assert(dn[2] == .25/3); |
| 88 | } |
| 89 | { |
| 90 | typedef std::piecewise_linear_distribution<> D; |
| 91 | typedef D::param_type P; |
| 92 | double b[] = {10, 14, 16, 17}; |
| 93 | double p[] = {0, 1, 1, 0}; |
| 94 | P pa(b, b+4, p); |
| 95 | std::vector<double> iv = pa.intervals(); |
| 96 | assert(iv.size() == 4); |
| 97 | assert(iv[0] == 10); |
| 98 | assert(iv[1] == 14); |
| 99 | assert(iv[2] == 16); |
| 100 | assert(iv[3] == 17); |
| 101 | std::vector<double> dn = pa.densities(); |
| 102 | assert(dn.size() == 4); |
| 103 | assert(dn[0] == 0); |
| 104 | assert(dn[1] == 1/4.5); |
| 105 | assert(dn[2] == 1/4.5); |
| 106 | assert(dn[3] == 0); |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |