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