From 5430540d57b86c7dcc4a29e4ceb4b24c01a23b34 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Tue, 25 May 2010 00:27:34 +0000 Subject: [PATCH] [rand.dist.samp.plinear]. This means we've got a fully tested and functional ! 489 tests over 48 sections are passing. :-) The only thing still on my plate in this area is to back-port some of this technology to random_shuffle/shuffle in . That will involve shuffling header bits around ( depepends on ), but it won't entail that much development (compared to what has been required for ). git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@104575 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/random | 377 +++++++++++++++++- .../rand.dist.samp.plinear/assign.pass.cpp | 36 ++ .../rand.dist.samp.plinear/copy.pass.cpp | 34 ++ .../ctor_default.pass.cpp | 34 ++ .../rand.dist.samp.plinear/ctor_func.pass.cpp | 69 ++++ .../ctor_init_func.pass.cpp | 82 ++++ .../ctor_iterator.pass.cpp | 101 +++++ .../ctor_param.pass.cpp | 42 ++ .../rand.dist.samp.plinear/eq.pass.cpp | 47 +++ .../rand.dist.samp.plinear/eval.pass.cpp | 341 ++++++++++++++++ .../eval_param.pass.cpp | 90 +++++ .../rand.dist.samp.plinear/get_param.pass.cpp | 32 ++ .../rand.dist.samp.plinear/io.pass.cpp | 44 ++ .../rand.dist.samp.plinear/max.pass.cpp | 30 ++ .../rand.dist.samp.plinear/min.pass.cpp | 30 ++ .../param_assign.pass.cpp | 34 ++ .../param_copy.pass.cpp | 33 ++ .../param_ctor_default.pass.cpp | 35 ++ .../param_ctor_func.pass.cpp | 70 ++++ .../param_ctor_init_func.pass.cpp | 83 ++++ .../param_ctor_iterator.pass.cpp | 105 +++++ .../rand.dist.samp.plinear/param_eq.pass.cpp | 41 ++ .../param_types.pass.cpp | 28 ++ .../rand.dist.samp.plinear/set_param.pass.cpp | 32 ++ .../rand.dist.samp.plinear/types.pass.cpp | 32 ++ 25 files changed, 1880 insertions(+), 2 deletions(-) create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/assign.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/copy.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_default.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_func.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_init_func.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_iterator.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_param.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eq.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval_param.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/get_param.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/io.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/max.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/min.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_assign.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_copy.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_default.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_func.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_init_func.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_iterator.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_eq.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_types.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/set_param.pass.cpp create mode 100644 test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/types.pass.cpp diff --git a/include/random b/include/random index 1a680ff5..adefa8b9 100644 --- a/include/random +++ b/include/random @@ -1552,7 +1552,82 @@ class piecewise_constant_distribution }; template - class piecewise_linear_distribution; +class piecewise_linear_distribution +{ + // types + typedef RealType result_type; + + class param_type + { + public: + typedef piecewise_linear_distribution distribution_type; + + param_type(); + template + param_type(InputIteratorB firstB, InputIteratorB lastB, + InputIteratorW firstW); + template + param_type(initializer_list bl, UnaryOperation fw); + template + param_type(size_t nw, result_type xmin, result_type xmax, + UnaryOperation fw); + + vector intervals() const; + vector densities() const; + + friend bool operator==(const param_type& x, const param_type& y); + friend bool operator!=(const param_type& x, const param_type& y); + }; + + // constructor and reset functions + piecewise_linear_distribution(); + template + piecewise_linear_distribution(InputIteratorB firstB, + InputIteratorB lastB, + InputIteratorW firstW); + + template + piecewise_linear_distribution(initializer_list bl, + UnaryOperation fw); + + template + piecewise_linear_distribution(size_t nw, result_type xmin, + result_type xmax, UnaryOperation fw); + + explicit piecewise_linear_distribution(const param_type& parm); + void reset(); + + // generating functions + template result_type operator()(URNG& g); + template result_type operator()(URNG& g, const param_type& parm); + + // property functions + vector intervals() const; + vector densities() const; + + param_type param() const; + void param(const param_type& parm); + + result_type min() const; + result_type max() const; + + friend bool operator==(const piecewise_linear_distribution& x, + const piecewise_linear_distribution& y); + friend bool operator!=(const piecewise_linear_distribution& x, + const piecewise_linear_distribution& y); + + template + friend + basic_ostream& + operator<<(basic_ostream& os, + const piecewise_linear_distribution& x); + + template + friend + basic_istream& + operator>>(basic_istream& is, + piecewise_linear_distribution& x); +}; } // std */ @@ -5772,7 +5847,8 @@ piecewise_constant_distribution<_RealType>::param_type::__init() template piecewise_constant_distribution<_RealType>::param_type::param_type() : __b_(2), - __densities_(1, 1.0) + __densities_(1, 1.0), + __areas_(1, 0.0) { __b_[1] = 1; } @@ -5789,6 +5865,7 @@ piecewise_constant_distribution<_RealType>::param_type::param_type( __b_[0] = 0; __b_[1] = 1; __densities_.assign(1, 1.0); + __areas_.assign(1, 0.0); } else { @@ -5811,6 +5888,7 @@ piecewise_constant_distribution<_RealType>::param_type::param_type( __b_[0] = 0; __b_[1] = 1; __densities_.assign(1, 1.0); + __areas_.assign(1, 0.0); } else { @@ -5910,6 +5988,301 @@ operator>>(basic_istream<_CharT, _Traits>& __is, return __is; } +// piecewise_linear_distribution + +template +class piecewise_linear_distribution +{ +public: + // types + typedef _RealType result_type; + + class param_type + { + typedef typename common_type::type __area_type; + vector __b_; + vector __densities_; + vector<__area_type> __areas_; + public: + typedef piecewise_linear_distribution distribution_type; + + param_type(); + template + param_type(_InputIteratorB __fB, _InputIteratorB __lB, + _InputIteratorW __fW); + template + param_type(initializer_list __bl, _UnaryOperation __fw); + template + param_type(size_t __nw, result_type __xmin, result_type __xmax, + _UnaryOperation __fw); + + vector intervals() const {return __b_;} + vector densities() const {return __densities_;} + + friend bool operator==(const param_type& __x, const param_type& __y) + {return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;} + friend bool operator!=(const param_type& __x, const param_type& __y) + {return !(__x == __y);} + + private: + void __init(); + + friend class piecewise_linear_distribution; + + template + friend + basic_ostream<_CharT, _Traits>& + operator<<(basic_ostream<_CharT, _Traits>& __os, + const piecewise_linear_distribution<_RT>& __x); + + template + friend + basic_istream<_CharT, _Traits>& + operator>>(basic_istream<_CharT, _Traits>& __is, + piecewise_linear_distribution<_RT>& __x); + }; + +private: + param_type __p_; + +public: + // constructor and reset functions + piecewise_linear_distribution() {} + template + piecewise_linear_distribution(_InputIteratorB __fB, + _InputIteratorB __lB, + _InputIteratorW __fW) + : __p_(__fB, __lB, __fW) {} + + template + piecewise_linear_distribution(initializer_list __bl, + _UnaryOperation __fw) + : __p_(__bl, __fw) {} + + template + piecewise_linear_distribution(size_t __nw, result_type __xmin, + result_type __xmax, _UnaryOperation __fw) + : __p_(__nw, __xmin, __xmax, __fw) {} + + explicit piecewise_linear_distribution(const param_type& __p) + : __p_(__p) {} + + void reset() {} + + // generating functions + template result_type operator()(_URNG& __g) + {return (*this)(__g, __p_);} + template result_type operator()(_URNG& __g, const param_type& __p); + + // property functions + vector intervals() const {return __p_.intervals();} + vector densities() const {return __p_.densities();} + + param_type param() const {return __p_;} + void param(const param_type& __p) {__p_ = __p;} + + result_type min() const {return __p_.__b_.front();} + result_type max() const {return __p_.__b_.back();} + + friend bool operator==(const piecewise_linear_distribution& __x, + const piecewise_linear_distribution& __y) + {return __x.__p_ == __y.__p_;} + friend bool operator!=(const piecewise_linear_distribution& __x, + const piecewise_linear_distribution& __y) + {return !(__x == __y);} + + template + friend + basic_ostream<_CharT, _Traits>& + operator<<(basic_ostream<_CharT, _Traits>& __os, + const piecewise_linear_distribution<_RT>& __x); + + template + friend + basic_istream<_CharT, _Traits>& + operator>>(basic_istream<_CharT, _Traits>& __is, + piecewise_linear_distribution<_RT>& __x); +}; + +template +void +piecewise_linear_distribution<_RealType>::param_type::__init() +{ + __areas_.assign(__densities_.size() - 1, __area_type()); + __area_type _S = 0; + for (size_t __i = 0; __i < __areas_.size(); ++__i) + { + __areas_[__i] = (__densities_[__i+1] + __densities_[__i]) * + (__b_[__i+1] - __b_[__i]) * .5; + _S += __areas_[__i]; + } + for (size_t __i = __areas_.size(); __i > 1;) + { + --__i; + __areas_[__i] = __areas_[__i-1] / _S; + } + __areas_[0] = 0; + for (size_t __i = 1; __i < __areas_.size(); ++__i) + __areas_[__i] += __areas_[__i-1]; + for (size_t __i = 0; __i < __densities_.size(); ++__i) + __densities_[__i] /= _S; +} + +template +piecewise_linear_distribution<_RealType>::param_type::param_type() + : __b_(2), + __densities_(2, 1.0), + __areas_(1, 0.0) +{ + __b_[1] = 1; +} + +template +template +piecewise_linear_distribution<_RealType>::param_type::param_type( + _InputIteratorB __fB, _InputIteratorB __lB, _InputIteratorW __fW) + : __b_(__fB, __lB) +{ + if (__b_.size() < 2) + { + __b_.resize(2); + __b_[0] = 0; + __b_[1] = 1; + __densities_.assign(2, 1.0); + __areas_.assign(1, 0.0); + } + else + { + __densities_.reserve(__b_.size()); + for (size_t __i = 0; __i < __b_.size(); ++__i, ++__fW) + __densities_.push_back(*__fW); + __init(); + } +} + +template +template +piecewise_linear_distribution<_RealType>::param_type::param_type( + initializer_list __bl, _UnaryOperation __fw) + : __b_(__bl.begin(), __bl.end()) +{ + if (__b_.size() < 2) + { + __b_.resize(2); + __b_[0] = 0; + __b_[1] = 1; + __densities_.assign(2, 1.0); + __areas_.assign(1, 0.0); + } + else + { + __densities_.reserve(__b_.size()); + for (size_t __i = 0; __i < __b_.size(); ++__i) + __densities_.push_back(__fw(__b_[__i])); + __init(); + } +} + +template +template +piecewise_linear_distribution<_RealType>::param_type::param_type( + size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw) + : __b_(__nw == 0 ? 2 : __nw + 1) +{ + size_t __n = __b_.size() - 1; + result_type __d = (__xmax - __xmin) / __n; + __densities_.reserve(__b_.size()); + for (size_t __i = 0; __i < __n; ++__i) + { + __b_[__i] = __xmin + __i * __d; + __densities_.push_back(__fw(__b_[__i])); + } + __b_[__n] = __xmax; + __densities_.push_back(__fw(__b_[__n])); + __init(); +} + +template +template +_RealType +piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) +{ + typedef uniform_real_distribution _Gen; + result_type __u = _Gen()(__g); + ptrdiff_t __k = _STD::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), + static_cast(__u)) - __p.__areas_.begin() - 1; + __u -= __p.__areas_[__k]; + const double __dk = __p.__densities_[__k]; + const double __dk1 = __p.__densities_[__k+1]; + const double __deltad = __dk1 - __dk; + const result_type __bk = __p.__b_[__k]; + if (__deltad == 0) + return static_cast(__u / __dk + __bk); + const result_type __bk1 = __p.__b_[__k+1]; + const result_type __deltab = __bk1 - __bk; + return static_cast((__bk * __dk1 - __bk1 * __dk + + _STD::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) / + __deltad); +} + +template +basic_ostream<_CharT, _Traits>& +operator<<(basic_ostream<_CharT, _Traits>& __os, + const piecewise_linear_distribution<_RT>& __x) +{ + __save_flags<_CharT, _Traits> _(__os); + __os.flags(ios_base::dec | ios_base::left | ios_base::fixed | + ios_base::scientific); + _CharT __sp = __os.widen(' '); + __os.fill(__sp); + size_t __n = __x.__p_.__b_.size(); + __os << __n; + for (size_t __i = 0; __i < __n; ++__i) + __os << __sp << __x.__p_.__b_[__i]; + __n = __x.__p_.__densities_.size(); + __os << __sp << __n; + for (size_t __i = 0; __i < __n; ++__i) + __os << __sp << __x.__p_.__densities_[__i]; + __n = __x.__p_.__areas_.size(); + __os << __sp << __n; + for (size_t __i = 0; __i < __n; ++__i) + __os << __sp << __x.__p_.__areas_[__i]; + return __os; +} + +template +basic_istream<_CharT, _Traits>& +operator>>(basic_istream<_CharT, _Traits>& __is, + piecewise_linear_distribution<_RT>& __x) +{ + typedef piecewise_linear_distribution<_RT> _Eng; + typedef typename _Eng::result_type result_type; + typedef typename _Eng::param_type param_type; + typedef typename param_type::__area_type __area_type; + __save_flags<_CharT, _Traits> _(__is); + __is.flags(ios_base::dec | ios_base::skipws); + size_t __n; + __is >> __n; + vector __b(__n); + for (size_t __i = 0; __i < __n; ++__i) + __is >> __b[__i]; + __is >> __n; + vector __densities(__n); + for (size_t __i = 0; __i < __n; ++__i) + __is >> __densities[__i]; + __is >> __n; + vector<__area_type> __areas(__n); + for (size_t __i = 0; __i < __n; ++__i) + __is >> __areas[__i]; + if (!__is.fail()) + { + swap(__x.__p_.__b_, __b); + swap(__x.__p_.__densities_, __densities); + swap(__x.__p_.__areas_, __areas); + } + return __is; +} + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_RANDOM diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/assign.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/assign.pass.cpp new file mode 100644 index 00000000..77b69234 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/assign.pass.cpp @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// piecewise_linear_distribution& operator=(const piecewise_linear_distribution&); + +#include +#include + +void +test1() +{ + typedef std::piecewise_linear_distribution<> D; + double p[] = {2, 4, 1, 8, 3}; + double b[] = {2, 4, 5, 8, 9}; + D d1(b, b+5, p); + D d2; + assert(d1 != d2); + d2 = d1; + assert(d1 == d2); +} + +int main() +{ + test1(); +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/copy.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/copy.pass.cpp new file mode 100644 index 00000000..3bc165f6 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/copy.pass.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// piecewise_linear_distribution(const piecewise_linear_distribution&); + +#include +#include + +void +test1() +{ + typedef std::piecewise_linear_distribution<> D; + double p[] = {2, 4, 1, 8, 2}; + double b[] = {2, 4, 5, 8, 9}; + D d1(b, b+5, p); + D d2 = d1; + assert(d1 == d2); +} + +int main() +{ + test1(); +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_default.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_default.pass.cpp new file mode 100644 index 00000000..ca0b775e --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_default.pass.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// piecewise_linear_distribution(initializer_list wl); + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + D d; + std::vector iv = d.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = d.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1); + assert(dn[1] == 1); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_func.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_func.pass.cpp new file mode 100644 index 00000000..fed6f565 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_func.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// template +// piecewise_linear_distribution(size_t nw, result_type xmin, +// result_type xmax, UnaryOperation fw); + +#include + +#include +#include + +double fw(double x) +{ + return 2*x; +} + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + D d(0, 0, 1, fw); + std::vector iv = d.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = d.densities(); + assert(dn.size() == 2); + assert(dn[0] == 0); + assert(dn[1] == 2); + } + { + typedef std::piecewise_linear_distribution<> D; + D d(1, 10, 12, fw); + std::vector iv = d.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 10); + assert(iv[1] == 12); + std::vector dn = d.densities(); + assert(dn.size() == 2); + assert(dn[0] == 20./44); + assert(dn[1] == 24./44); + } + { + typedef std::piecewise_linear_distribution<> D; + D d(2, 6, 14, fw); + std::vector iv = d.intervals(); + assert(iv.size() == 3); + assert(iv[0] == 6); + assert(iv[1] == 10); + assert(iv[2] == 14); + std::vector dn = d.densities(); + assert(dn.size() == 3); + assert(dn[0] == 0.075); + assert(dn[1] == 0.125); + assert(dn[2] == 0.175); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_init_func.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_init_func.pass.cpp new file mode 100644 index 00000000..77aab01e --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_init_func.pass.cpp @@ -0,0 +1,82 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// piecewise_linear_distribution(initializer_list bl, +// UnaryOperation fw); + +#include + +#include +#include + +double f(double x) +{ + return x*2; +} + +int main() +{ +#ifdef _LIBCPP_MOVE + { + typedef std::piecewise_linear_distribution<> D; + D d({}, f); + std::vector iv = d.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = d.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1); + assert(dn[1] == 1); + } + { + typedef std::piecewise_linear_distribution<> D; + D d({12}, f); + std::vector iv = d.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = d.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1); + assert(dn[1] == 1); + } + { + typedef std::piecewise_linear_distribution<> D; + D d({10, 12}, f); + std::vector iv = d.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 10); + assert(iv[1] == 12); + std::vector dn = d.densities(); + assert(dn.size() == 2); + assert(dn[0] == 20./44); + assert(dn[1] == 24./44); + } + { + typedef std::piecewise_linear_distribution<> D; + D d({6, 10, 14}, f); + std::vector iv = d.intervals(); + assert(iv.size() == 3); + assert(iv[0] == 6); + assert(iv[1] == 10); + assert(iv[2] == 14); + std::vector dn = d.densities(); + assert(dn.size() == 3); + assert(dn[0] == 0.075); + assert(dn[1] == 0.125); + assert(dn[2] == 0.175); + } +#endif +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_iterator.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_iterator.pass.cpp new file mode 100644 index 00000000..89ba21a9 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_iterator.pass.cpp @@ -0,0 +1,101 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// template +// piecewise_linear_distribution(InputIteratorB firstB, +// InputIteratorB lastB, +// InputIteratorW firstW); + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + double b[] = {10}; + double p[] = {12}; + D d(b, b, p); + std::vector iv = d.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = d.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1); + assert(dn[1] == 1); + } + { + typedef std::piecewise_linear_distribution<> D; + double b[] = {10}; + double p[] = {12}; + D d(b, b+1, p); + std::vector iv = d.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = d.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1); + assert(dn[1] == 1); + } + { + typedef std::piecewise_linear_distribution<> D; + double b[] = {10, 15}; + double p[] = {20, 20}; + D d(b, b+2, p); + std::vector iv = d.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 10); + assert(iv[1] == 15); + std::vector dn = d.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1/5.); + assert(dn[1] == 1/5.); + } + { + typedef std::piecewise_linear_distribution<> D; + double b[] = {10, 15, 16}; + double p[] = {.25, .75, .25}; + D d(b, b+3, p); + std::vector iv = d.intervals(); + assert(iv.size() == 3); + assert(iv[0] == 10); + assert(iv[1] == 15); + assert(iv[2] == 16); + std::vector dn = d.densities(); + assert(dn.size() == 3); + assert(dn[0] == .25/3); + assert(dn[1] == .75/3); + assert(dn[2] == .25/3); + } + { + typedef std::piecewise_linear_distribution<> D; + double b[] = {10, 14, 16, 17}; + double p[] = {0, 1, 1, 0}; + D d(b, b+4, p); + std::vector iv = d.intervals(); + assert(iv.size() == 4); + assert(iv[0] == 10); + assert(iv[1] == 14); + assert(iv[2] == 16); + assert(iv[3] == 17); + std::vector dn = d.densities(); + assert(dn.size() == 4); + assert(dn[0] == 0); + assert(dn[1] == 1/4.5); + assert(dn[2] == 1/4.5); + assert(dn[3] == 0); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_param.pass.cpp new file mode 100644 index 00000000..1eb49262 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/ctor_param.pass.cpp @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// explicit piecewise_linear_distribution(const param_type& parm); + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 0}; + P pa(b, b+4, p); + D d(pa); + std::vector iv = d.intervals(); + assert(iv.size() == 4); + assert(iv[0] == 10); + assert(iv[1] == 14); + assert(iv[2] == 16); + assert(iv[3] == 17); + std::vector dn = d.densities(); + assert(dn.size() == 4); + assert(dn[0] == 25/256.25); + assert(dn[1] == 62.5/256.25); + assert(dn[2] == 12.5/256.25); + assert(dn[3] == 0); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eq.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eq.pass.cpp new file mode 100644 index 00000000..b394cd8a --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eq.pass.cpp @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// bool operator=(const piecewise_linear_distribution& x, +// const piecewise_linear_distribution& y); +// bool operator!(const piecewise_linear_distribution& x, +// const piecewise_linear_distribution& y); + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + D d1; + D d2; + assert(d1 == d2); + } + { + typedef std::piecewise_linear_distribution<> D; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 1}; + D d1(b, b+4, p); + D d2(b, b+4, p); + assert(d1 == d2); + } + { + typedef std::piecewise_linear_distribution<> D; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 0}; + D d1(b, b+4, p); + D d2; + assert(d1 != d2); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval.pass.cpp new file mode 100644 index 00000000..ce884a88 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval.pass.cpp @@ -0,0 +1,341 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// template result_type operator()(_URNG& g); + +#include + +#include +#include +#include +#include +#include + +template +inline +T +sqr(T x) +{ + return x*x; +} + +double +f(double x, double a, double m, double b, double c) +{ + return a + m*(sqr(x) - sqr(b))/2 + c*(x-b); +} + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + typedef std::mt19937_64 G; + G g; + double b[] = {10, 14, 16, 17}; + double p[] = {0, 1, 1, 0}; + const size_t Np = sizeof(p) / sizeof(p[0]) - 1; + D d(b, b+Np+1, p); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v < d.max()); + u.push_back(v); + } + std::sort(u.begin(), u.end()); + int kp = -1; + double a; + double m; + double bk; + double c; + std::vector areas(Np); + double S = 0; + for (int i = 0; i < areas.size(); ++i) + { + areas[i] = (p[i]+p[i+1])*(b[i+1]-b[i])/2; + S += areas[i]; + } + for (int i = 0; i < areas.size(); ++i) + areas[i] /= S; + for (int i = 0; i < Np+1; ++i) + p[i] /= S; + for (int i = 0; i < N; ++i) + { + int k = std::lower_bound(b, b+Np+1, u[i]) - b - 1; + if (k != kp) + { + a = 0; + for (int j = 0; j < k; ++j) + a += areas[j]; + m = (p[k+1] - p[k]) / (b[k+1] - b[k]); + bk = b[k]; + c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]); + kp = k; + } + assert(std::abs(f(u[i], a, m, bk, c) - double(i)/N) < .001); + } + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + typedef std::mt19937_64 G; + G g; + double b[] = {10, 14, 16, 17}; + double p[] = {0, 0, 1, 0}; + const size_t Np = sizeof(p) / sizeof(p[0]) - 1; + D d(b, b+Np+1, p); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v < d.max()); + u.push_back(v); + } + std::sort(u.begin(), u.end()); + int kp = -1; + double a; + double m; + double bk; + double c; + std::vector areas(Np); + double S = 0; + for (int i = 0; i < areas.size(); ++i) + { + areas[i] = (p[i]+p[i+1])*(b[i+1]-b[i])/2; + S += areas[i]; + } + for (int i = 0; i < areas.size(); ++i) + areas[i] /= S; + for (int i = 0; i < Np+1; ++i) + p[i] /= S; + for (int i = 0; i < N; ++i) + { + int k = std::lower_bound(b, b+Np+1, u[i]) - b - 1; + if (k != kp) + { + a = 0; + for (int j = 0; j < k; ++j) + a += areas[j]; + m = (p[k+1] - p[k]) / (b[k+1] - b[k]); + bk = b[k]; + c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]); + kp = k; + } + assert(std::abs(f(u[i], a, m, bk, c) - double(i)/N) < .001); + } + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + typedef std::mt19937_64 G; + G g; + double b[] = {10, 14, 16, 17}; + double p[] = {1, 0, 0, 0}; + const size_t Np = sizeof(p) / sizeof(p[0]) - 1; + D d(b, b+Np+1, p); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v < d.max()); + u.push_back(v); + } + std::sort(u.begin(), u.end()); + int kp = -1; + double a; + double m; + double bk; + double c; + std::vector areas(Np); + double S = 0; + for (int i = 0; i < areas.size(); ++i) + { + areas[i] = (p[i]+p[i+1])*(b[i+1]-b[i])/2; + S += areas[i]; + } + for (int i = 0; i < areas.size(); ++i) + areas[i] /= S; + for (int i = 0; i < Np+1; ++i) + p[i] /= S; + for (int i = 0; i < N; ++i) + { + int k = std::lower_bound(b, b+Np+1, u[i]) - b - 1; + if (k != kp) + { + a = 0; + for (int j = 0; j < k; ++j) + a += areas[j]; + m = (p[k+1] - p[k]) / (b[k+1] - b[k]); + bk = b[k]; + c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]); + kp = k; + } + assert(std::abs(f(u[i], a, m, bk, c) - double(i)/N) < .001); + } + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + typedef std::mt19937_64 G; + G g; + double b[] = {10, 14, 16}; + double p[] = {0, 1, 0}; + const size_t Np = sizeof(p) / sizeof(p[0]) - 1; + D d(b, b+Np+1, p); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v < d.max()); + u.push_back(v); + } + std::sort(u.begin(), u.end()); + int kp = -1; + double a; + double m; + double bk; + double c; + std::vector areas(Np); + double S = 0; + for (int i = 0; i < areas.size(); ++i) + { + areas[i] = (p[i]+p[i+1])*(b[i+1]-b[i])/2; + S += areas[i]; + } + for (int i = 0; i < areas.size(); ++i) + areas[i] /= S; + for (int i = 0; i < Np+1; ++i) + p[i] /= S; + for (int i = 0; i < N; ++i) + { + int k = std::lower_bound(b, b+Np+1, u[i]) - b - 1; + if (k != kp) + { + a = 0; + for (int j = 0; j < k; ++j) + a += areas[j]; + m = (p[k+1] - p[k]) / (b[k+1] - b[k]); + bk = b[k]; + c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]); + kp = k; + } + assert(std::abs(f(u[i], a, m, bk, c) - double(i)/N) < .001); + } + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + typedef std::mt19937_64 G; + G g; + double b[] = {10, 14}; + double p[] = {1, 1}; + const size_t Np = sizeof(p) / sizeof(p[0]) - 1; + D d(b, b+Np+1, p); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v < d.max()); + u.push_back(v); + } + std::sort(u.begin(), u.end()); + int kp = -1; + double a; + double m; + double bk; + double c; + std::vector areas(Np); + double S = 0; + for (int i = 0; i < areas.size(); ++i) + { + areas[i] = (p[i]+p[i+1])*(b[i+1]-b[i])/2; + S += areas[i]; + } + for (int i = 0; i < areas.size(); ++i) + areas[i] /= S; + for (int i = 0; i < Np+1; ++i) + p[i] /= S; + for (int i = 0; i < N; ++i) + { + int k = std::lower_bound(b, b+Np+1, u[i]) - b - 1; + if (k != kp) + { + a = 0; + for (int j = 0; j < k; ++j) + a += areas[j]; + m = (p[k+1] - p[k]) / (b[k+1] - b[k]); + bk = b[k]; + c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]); + kp = k; + } + assert(std::abs(f(u[i], a, m, bk, c) - double(i)/N) < .001); + } + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + typedef std::mt19937_64 G; + G g; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 0}; + const size_t Np = sizeof(p) / sizeof(p[0]) - 1; + D d(b, b+Np+1, p); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v < d.max()); + u.push_back(v); + } + std::sort(u.begin(), u.end()); + int kp = -1; + double a; + double m; + double bk; + double c; + std::vector areas(Np); + double S = 0; + for (int i = 0; i < areas.size(); ++i) + { + areas[i] = (p[i]+p[i+1])*(b[i+1]-b[i])/2; + S += areas[i]; + } + for (int i = 0; i < areas.size(); ++i) + areas[i] /= S; + for (int i = 0; i < Np+1; ++i) + p[i] /= S; + for (int i = 0; i < N; ++i) + { + int k = std::lower_bound(b, b+Np+1, u[i]) - b - 1; + if (k != kp) + { + a = 0; + for (int j = 0; j < k; ++j) + a += areas[j]; + m = (p[k+1] - p[k]) / (b[k+1] - b[k]); + bk = b[k]; + c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]); + kp = k; + } + assert(std::abs(f(u[i], a, m, bk, c) - double(i)/N) < .001); + } + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval_param.pass.cpp new file mode 100644 index 00000000..b024b878 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval_param.pass.cpp @@ -0,0 +1,90 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// template result_type operator()(_URNG& g, const param_type& parm); + +#include +#include +#include +#include +#include + +template +inline +T +sqr(T x) +{ + return x*x; +} + +double +f(double x, double a, double m, double b, double c) +{ + return a + m*(sqr(x) - sqr(b))/2 + c*(x-b); +} + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + typedef std::mt19937_64 G; + G g; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 0}; + const size_t Np = sizeof(p) / sizeof(p[0]) - 1; + D d; + P pa(b, b+Np+1, p); + const int N = 1000000; + std::vector u; + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g, pa); + assert(10 <= v && v < 17); + u.push_back(v); + } + std::sort(u.begin(), u.end()); + int kp = -1; + double a; + double m; + double bk; + double c; + std::vector areas(Np); + double S = 0; + for (int i = 0; i < areas.size(); ++i) + { + areas[i] = (p[i]+p[i+1])*(b[i+1]-b[i])/2; + S += areas[i]; + } + for (int i = 0; i < areas.size(); ++i) + areas[i] /= S; + for (int i = 0; i < Np+1; ++i) + p[i] /= S; + for (int i = 0; i < N; ++i) + { + int k = std::lower_bound(b, b+Np+1, u[i]) - b - 1; + if (k != kp) + { + a = 0; + for (int j = 0; j < k; ++j) + a += areas[j]; + m = (p[k+1] - p[k]) / (b[k+1] - b[k]); + bk = b[k]; + c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]); + kp = k; + } + assert(std::abs(f(u[i], a, m, bk, c) - double(i)/N) < .001); + } + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/get_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/get_param.pass.cpp new file mode 100644 index 00000000..b04b8512 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/get_param.pass.cpp @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// param_type param() const; + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 10}; + const size_t Np = sizeof(p) / sizeof(p[0]); + P pa(b, b+Np+1, p); + D d(pa); + assert(d.param() == pa); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/io.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/io.pass.cpp new file mode 100644 index 00000000..d1640626 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/io.pass.cpp @@ -0,0 +1,44 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// template +// basic_ostream& +// operator<<(basic_ostream& os, +// const piecewise_linear_distribution& x); +// +// template +// basic_istream& +// operator>>(basic_istream& is, +// piecewise_linear_distribution& x); + +#include +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 25}; + const size_t Np = sizeof(p) / sizeof(p[0]); + D d1(b, b+Np+1, p); + std::ostringstream os; + os << d1; + std::istringstream is(os.str()); + D d2; + is >> d2; + assert(d1 == d2); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/max.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/max.pass.cpp new file mode 100644 index 00000000..351b94d5 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/max.pass.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// result_type max() const; + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 5}; + const size_t Np = sizeof(p) / sizeof(p[0]); + D d(b, b+Np, p); + assert(d.max() == 17); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/min.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/min.pass.cpp new file mode 100644 index 00000000..df0835f8 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/min.pass.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// result_type min() const; + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 0}; + const size_t Np = sizeof(p) / sizeof(p[0]); + D d(b, b+Np, p); + assert(d.min() == 10); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_assign.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_assign.pass.cpp new file mode 100644 index 00000000..baa9d216 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_assign.pass.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution +// { +// class param_type; + +#include +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 2}; + const size_t Np = sizeof(p) / sizeof(p[0]); + P p0(b, b+Np, p); + P p1; + p1 = p0; + assert(p1 == p0); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_copy.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_copy.pass.cpp new file mode 100644 index 00000000..56e2fcd3 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_copy.pass.cpp @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution +// { +// class param_type; + +#include +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 5}; + const size_t Np = sizeof(p) / sizeof(p[0]); + P p0(b, b+Np, p); + P p1 = p0; + assert(p1 == p0); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_default.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_default.pass.cpp new file mode 100644 index 00000000..74177134 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_default.pass.cpp @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// param_type(); + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + P pa; + std::vector iv = pa.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = pa.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1); + assert(dn[1] == 1); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_func.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_func.pass.cpp new file mode 100644 index 00000000..73c86160 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_func.pass.cpp @@ -0,0 +1,70 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// template +// param_type(size_t nw, double xmin, double xmax, +// UnaryOperation fw); + +#include +#include + +double fw(double x) +{ + return 2*x; +} + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + P pa(0, 0, 1, fw); + std::vector iv = pa.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = pa.densities(); + assert(dn.size() == 2); + assert(dn[0] == 0); + assert(dn[1] == 2); + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + P pa(1, 10, 12, fw); + std::vector iv = pa.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 10); + assert(iv[1] == 12); + std::vector dn = pa.densities(); + assert(dn.size() == 2); + assert(dn[0] == 20./44); + assert(dn[1] == 24./44); + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + P pa(2, 6, 14, fw); + std::vector iv = pa.intervals(); + assert(iv.size() == 3); + assert(iv[0] == 6); + assert(iv[1] == 10); + assert(iv[2] == 14); + std::vector dn = pa.densities(); + assert(dn.size() == 3); + assert(dn[0] == 0.075); + assert(dn[1] == 0.125); + assert(dn[2] == 0.175); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_init_func.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_init_func.pass.cpp new file mode 100644 index 00000000..1b01b05e --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_init_func.pass.cpp @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// param_type(initializer_list bl, UnaryOperation fw); + +#include +#include + +double f(double x) +{ + return x*2; +} + +int main() +{ +#ifdef _LIBCPP_MOVE + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + P pa({}, f); + std::vector iv = pa.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = pa.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1); + assert(dn[1] == 1); + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + P pa({12}, f); + std::vector iv = pa.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = pa.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1); + assert(dn[1] == 1); + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + P pa({10, 12}, f); + std::vector iv = pa.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 10); + assert(iv[1] == 12); + std::vector dn = pa.densities(); + assert(dn.size() == 2); + assert(dn[0] == 20./44); + assert(dn[1] == 24./44); + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + P pa({6, 10, 14}, f); + std::vector iv = pa.intervals(); + assert(iv.size() == 3); + assert(iv[0] == 6); + assert(iv[1] == 10); + assert(iv[2] == 14); + std::vector dn = pa.densities(); + assert(dn.size() == 3); + assert(dn[0] == 0.075); + assert(dn[1] == 0.125); + assert(dn[2] == 0.175); + } +#endif +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_iterator.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_iterator.pass.cpp new file mode 100644 index 00000000..5be45b87 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_ctor_iterator.pass.cpp @@ -0,0 +1,105 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// template +// param_type(InputIteratorB firstB, InputIteratorB lastB, +// InputIteratorW firstW); + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10}; + double p[] = {12}; + P pa(b, b, p); + std::vector iv = pa.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = pa.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1); + assert(dn[1] == 1); + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10}; + double p[] = {12}; + P pa(b, b+1, p); + std::vector iv = pa.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 0); + assert(iv[1] == 1); + std::vector dn = pa.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1); + assert(dn[1] == 1); + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10, 15}; + double p[] = {12, 12}; + P pa(b, b+2, p); + std::vector iv = pa.intervals(); + assert(iv.size() == 2); + assert(iv[0] == 10); + assert(iv[1] == 15); + std::vector dn = pa.densities(); + assert(dn.size() == 2); + assert(dn[0] == 1/5.); + assert(dn[1] == 1/5.); + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10, 15, 16}; + double p[] = {.25, .75, .25}; + P pa(b, b+3, p); + std::vector iv = pa.intervals(); + assert(iv.size() == 3); + assert(iv[0] == 10); + assert(iv[1] == 15); + assert(iv[2] == 16); + std::vector dn = pa.densities(); + assert(dn.size() == 3); + assert(dn[0] == .25/3); + assert(dn[1] == .75/3); + assert(dn[2] == .25/3); + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10, 14, 16, 17}; + double p[] = {0, 1, 1, 0}; + P pa(b, b+4, p); + std::vector iv = pa.intervals(); + assert(iv.size() == 4); + assert(iv[0] == 10); + assert(iv[1] == 14); + assert(iv[2] == 16); + assert(iv[3] == 17); + std::vector dn = pa.densities(); + assert(dn.size() == 4); + assert(dn[0] == 0); + assert(dn[1] == 1/4.5); + assert(dn[2] == 1/4.5); + assert(dn[3] == 0); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_eq.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_eq.pass.cpp new file mode 100644 index 00000000..28fb0f8c --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_eq.pass.cpp @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution +// { +// class param_type; + +#include +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 0}; + P p1(b, b+4, p); + P p2(b, b+4, p); + assert(p1 == p2); + } + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 0}; + P p1(b, b+3, p); + P p2(b, b+4, p); + assert(p1 != p2); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_types.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_types.pass.cpp new file mode 100644 index 00000000..4fd475ee --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/param_types.pass.cpp @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution +// { +// class param_type; + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type param_type; + typedef param_type::distribution_type distribution_type; + static_assert((std::is_same::value), ""); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/set_param.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/set_param.pass.cpp new file mode 100644 index 00000000..4e8ac964 --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/set_param.pass.cpp @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution + +// void param(const param_type& parm); + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::param_type P; + double b[] = {10, 14, 16, 17}; + double p[] = {25, 62.5, 12.5, 0}; + P pa(b, b+4, p); + D d; + d.param(pa); + assert(d.param() == pa); + } +} diff --git a/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/types.pass.cpp b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/types.pass.cpp new file mode 100644 index 00000000..2662abfd --- /dev/null +++ b/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/types.pass.cpp @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class piecewise_linear_distribution +// { +// typedef bool result_type; + +#include +#include + +int main() +{ + { + typedef std::piecewise_linear_distribution<> D; + typedef D::result_type result_type; + static_assert((std::is_same::value), ""); + } + { + typedef std::piecewise_linear_distribution D; + typedef D::result_type result_type; + static_assert((std::is_same::value), ""); + } +}