1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <random> 11 12 // template<class RealType = double> 13 // class piecewise_constant_distribution 14 15 // template<class InputIterator> 16 // param_type(InputIteratorB firstB, InputIteratorB lastB, 17 // InputIteratorW firstW); 18 19 #include <random> 20 #include <cassert> 21 22 int main() 23 { 24 { 25 typedef std::piecewise_constant_distribution<> D; 26 typedef D::param_type P; 27 double b[] = {10}; 28 double p[] = {12}; 29 P pa(b, b, p); 30 std::vector<double> iv = pa.intervals(); 31 assert(iv.size() == 2); 32 assert(iv[0] == 0); 33 assert(iv[1] == 1); 34 std::vector<double> dn = pa.densities(); 35 assert(dn.size() == 1); 36 assert(dn[0] == 1); 37 } 38 { 39 typedef std::piecewise_constant_distribution<> D; 40 typedef D::param_type P; 41 double b[] = {10}; 42 double p[] = {12}; 43 P pa(b, b+1, p); 44 std::vector<double> iv = pa.intervals(); 45 assert(iv.size() == 2); 46 assert(iv[0] == 0); 47 assert(iv[1] == 1); 48 std::vector<double> dn = pa.densities(); 49 assert(dn.size() == 1); 50 assert(dn[0] == 1); 51 } 52 { 53 typedef std::piecewise_constant_distribution<> D; 54 typedef D::param_type P; 55 double b[] = {10, 15}; 56 double p[] = {12}; 57 P pa(b, b+2, p); 58 std::vector<double> iv = pa.intervals(); 59 assert(iv.size() == 2); 60 assert(iv[0] == 10); 61 assert(iv[1] == 15); 62 std::vector<double> dn = pa.densities(); 63 assert(dn.size() == 1); 64 assert(dn[0] == 1/5.); 65 } 66 { 67 typedef std::piecewise_constant_distribution<> D; 68 typedef D::param_type P; 69 double b[] = {10, 15, 16}; 70 double p[] = {.25, .75}; 71 P pa(b, b+3, p); 72 std::vector<double> iv = pa.intervals(); 73 assert(iv.size() == 3); 74 assert(iv[0] == 10); 75 assert(iv[1] == 15); 76 assert(iv[2] == 16); 77 std::vector<double> dn = pa.densities(); 78 assert(dn.size() == 2); 79 assert(dn[0] == .25/5.); 80 assert(dn[1] == .75); 81 } 82 { 83 typedef std::piecewise_constant_distribution<> D; 84 typedef D::param_type P; 85 double b[] = {10, 14, 16, 17}; 86 double p[] = {25, 62.5, 12.5}; 87 P pa(b, b+4, p); 88 std::vector<double> iv = pa.intervals(); 89 assert(iv.size() == 4); 90 assert(iv[0] == 10); 91 assert(iv[1] == 14); 92 assert(iv[2] == 16); 93 assert(iv[3] == 17); 94 std::vector<double> dn = pa.densities(); 95 assert(dn.size() == 3); 96 assert(dn[0] == .0625); 97 assert(dn[1] == .3125); 98 assert(dn[2] == .125); 99 } 100 } 101