From fb0e5ec825c26d4ef8a99bcb38b95a4ca1c0316e Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Wed, 19 May 2010 01:53:57 +0000 Subject: [PATCH] [rand.dist.samp.discrete] llvm-svn: 104103 --- libcxx/include/random | 279 +++++++++++++++++- .../rand.dist.samp.discrete/assign.pass.cpp | 35 +++ .../rand.dist.samp.discrete/copy.pass.cpp | 33 +++ .../ctor_default.pass.cpp | 29 ++ .../ctor_func.pass.cpp | 60 ++++ .../ctor_init.pass.cpp | 81 +++++ .../ctor_iterator.pass.cpp | 87 ++++++ .../ctor_param.pass.cpp | 33 +++ .../rand.dist.samp.discrete/eq.pass.cpp | 45 +++ .../rand.dist.samp.discrete/eval.pass.cpp | 277 +++++++++++++++++ .../eval_param.pass.cpp | 43 +++ .../get_param.pass.cpp | 30 ++ .../rand.dist.samp.discrete/io.pass.cpp | 42 +++ .../rand.dist.samp.discrete/max.pass.cpp | 34 +++ .../rand.dist.samp.discrete/min.pass.cpp | 28 ++ .../param_assign.pass.cpp | 32 ++ .../param_copy.pass.cpp | 31 ++ .../param_ctor_default.pass.cpp | 30 ++ .../param_ctor_func.pass.cpp | 64 ++++ .../param_ctor_init.pass.cpp | 88 ++++++ .../param_ctor_iterator.pass.cpp | 94 ++++++ .../rand.dist.samp.discrete/param_eq.pass.cpp | 39 +++ .../param_types.pass.cpp | 28 ++ .../set_param.pass.cpp | 31 ++ .../rand.dist.samp.discrete/types.pass.cpp | 32 ++ 25 files changed, 1602 insertions(+), 3 deletions(-) create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/assign.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/copy.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_default.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_func.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_init.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_iterator.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_param.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eq.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eval_param.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/get_param.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/io.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/max.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/min.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_assign.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_copy.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_default.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_func.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_init.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_iterator.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_eq.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_types.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/set_param.pass.cpp create mode 100644 libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/types.pass.cpp diff --git a/libcxx/include/random b/libcxx/include/random index 6a3dcce7e22b..0ee6633273d9 100644 --- a/libcxx/include/random +++ b/libcxx/include/random @@ -1410,7 +1410,71 @@ public: }; template - class discrete_distribution; +class discrete_distribution +{ +public: + // types + typedef IntType result_type; + + class param_type + { + public: + typedef discrete_distribution distribution_type; + + param_type(); + template + param_type(InputIterator firstW, InputIterator lastW); + param_type(initializer_list wl); + template + param_type(size_t nw, double xmin, double xmax, UnaryOperation fw); + + vector probabilities() 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 + discrete_distribution(); + template + discrete_distribution(InputIterator firstW, InputIterator lastW); + discrete_distribution(initializer_list wl); + template + discrete_distribution(size_t nw, double xmin, double xmax, + UnaryOperation fw); + explicit discrete_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 probabilities() const; + + param_type param() const; + void param(const param_type& parm); + + result_type min() const; + result_type max() const; + + friend bool operator==(const discrete_distribution& x, + const discrete_distribution& y); + friend bool operator!=(const discrete_distribution& x, + const discrete_distribution& y); + + template + friend + basic_ostream& + operator<<(basic_ostream& os, + const discrete_distribution& x); + + template + friend + basic_istream& + operator>>(basic_istream& is, + discrete_distribution& x); +}; template class piecewise_constant_distribution; @@ -1428,6 +1492,7 @@ template #include #include #include +#include #include #include #include @@ -4075,7 +4140,7 @@ public: result_type s() const {return __p_.s();} param_type param() const {return __p_;} - void param(const param_type& __p) {return __p_ = __p;} + void param(const param_type& __p) {__p_ = __p;} result_type min() const {return 0;} result_type max() const {return numeric_limits::infinity();} @@ -5176,6 +5241,8 @@ operator>>(basic_istream<_CharT, _Traits>& __is, return __is; } +// student_t_distribution + template class student_t_distribution { @@ -5220,7 +5287,7 @@ public: result_type n() const {return __p_.n();} param_type param() const {return __p_;} - void param(const param_type& __p) {return __p_ = __p;} + void param(const param_type& __p) {__p_ = __p;} result_type min() const {return -numeric_limits::infinity();} result_type max() const {return numeric_limits::infinity();} @@ -5270,6 +5337,212 @@ operator>>(basic_istream<_CharT, _Traits>& __is, return __is; } +// discrete_distribution + +template +class discrete_distribution +{ +public: + // types + typedef _IntType result_type; + + class param_type + { + vector __p_; + public: + typedef discrete_distribution distribution_type; + + param_type() {} + template + param_type(_InputIterator __f, _InputIterator __l) + : __p_(__f, __l) {__init();} + param_type(initializer_list __wl) + : __p_(__wl.begin(), __wl.end()) {__init();} + template + param_type(size_t __nw, double __xmin, double __xmax, + _UnaryOperation __fw); + + vector probabilities() const; + + friend bool operator==(const param_type& __x, const param_type& __y) + {return __x.__p_ == __y.__p_;} + friend bool operator!=(const param_type& __x, const param_type& __y) + {return !(__x == __y);} + + private: + void __init(); + + friend class discrete_distribution; + + template + friend + basic_ostream<_CharT, _Traits>& + operator<<(basic_ostream<_CharT, _Traits>& __os, + const discrete_distribution<_IT>& __x); + + template + friend + basic_istream<_CharT, _Traits>& + operator>>(basic_istream<_CharT, _Traits>& __is, + discrete_distribution<_IT>& __x); + }; + +private: + param_type __p_; + +public: + // constructor and reset functions + discrete_distribution() {} + template + discrete_distribution(_InputIterator __f, _InputIterator __l) + : __p_(__f, __l) {} + discrete_distribution(initializer_list __wl) + : __p_(__wl) {} + template + discrete_distribution(size_t __nw, double __xmin, double __xmax, + _UnaryOperation __fw) + : __p_(__nw, __xmin, __xmax, __fw) {} + explicit discrete_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 probabilities() const {return __p_.probabilities();} + + param_type param() const {return __p_;} + void param(const param_type& __p) {__p_ = __p;} + + result_type min() const {return 0;} + result_type max() const {return __p_.__p_.size();} + + friend bool operator==(const discrete_distribution& __x, + const discrete_distribution& __y) + {return __x.__p_ == __y.__p_;} + friend bool operator!=(const discrete_distribution& __x, + const discrete_distribution& __y) + {return !(__x == __y);} + + template + friend + basic_ostream<_CharT, _Traits>& + operator<<(basic_ostream<_CharT, _Traits>& __os, + const discrete_distribution<_IT>& __x); + + template + friend + basic_istream<_CharT, _Traits>& + operator>>(basic_istream<_CharT, _Traits>& __is, + discrete_distribution<_IT>& __x); +}; + +template +template +discrete_distribution<_IntType>::param_type::param_type(size_t __nw, + double __xmin, + double __xmax, + _UnaryOperation __fw) +{ + if (__nw > 1) + { + __p_.reserve(__nw - 1); + double __d = (__xmax - __xmin) / __nw; + double __d2 = __d / 2; + for (size_t __k = 0; __k < __nw; ++__k) + __p_.push_back(__fw(__xmin + __k * __d + __d2)); + __init(); + } +} + +template +void +discrete_distribution<_IntType>::param_type::__init() +{ + if (!__p_.empty()) + { + if (__p_.size() > 1) + { + double __s = _STD::accumulate(__p_.begin(), __p_.end(), 0.0); + for (_STD::vector::iterator __i = __p_.begin(), __e = __p_.end(); + __i < __e; ++__i) + *__i /= __s; + vector __t(__p_.size() - 1); + _STD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin()); + swap(__p_, __t); + } + else + { + __p_.clear(); + __p_.shrink_to_fit(); + } + } +} + +template +vector +discrete_distribution<_IntType>::param_type::probabilities() const +{ + size_t __n = __p_.size(); + _STD::vector __p(__n+1); + _STD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin()); + if (__n > 0) + __p[__n] = 1 - __p_[__n-1]; + else + __p[0] = 1; + return __p; +} + +template +template +_IntType +discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) +{ + uniform_real_distribution __gen; + return static_cast<_IntType>( + _STD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) - + __p.__p_.begin()); +} + +template +basic_ostream<_CharT, _Traits>& +operator<<(basic_ostream<_CharT, _Traits>& __os, + const discrete_distribution<_IT>& __x) +{ + __save_flags<_CharT, _Traits> _(__os); + __os.flags(ios_base::dec | ios_base::left); + _CharT __sp = __os.widen(' '); + __os.fill(__sp); + size_t __n = __x.__p_.__p_.size(); + __os << __n; + for (size_t __i = 0; __i < __n; ++__i) + __os << __sp << __x.__p_.__p_[__i]; + return __os; +} + +template +basic_istream<_CharT, _Traits>& +operator>>(basic_istream<_CharT, _Traits>& __is, + discrete_distribution<_IT>& __x) +{ + typedef discrete_distribution<_IT> _Eng; + typedef typename _Eng::result_type result_type; + typedef typename _Eng::param_type param_type; + __save_flags<_CharT, _Traits> _(__is); + __is.flags(ios_base::dec | ios_base::skipws); + size_t __n; + __is >> __n; + std::vector __p(__n); + for (size_t __i = 0; __i < __n; ++__i) + __is >> __p[__i]; + if (!__is.fail()) + swap(__x.__p_.__p_, __p); + return __is; +} + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_RANDOM diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/assign.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/assign.pass.cpp new file mode 100644 index 000000000000..617fa75d6419 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/assign.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 discrete_distribution + +// discrete_distribution& operator=(const discrete_distribution&); + +#include +#include + +void +test1() +{ + typedef std::discrete_distribution<> D; + double p[] = {2, 4, 1, 8}; + D d1(p, p+4); + D d2; + assert(d1 != d2); + d2 = d1; + assert(d1 == d2); +} + +int main() +{ + test1(); +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/copy.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/copy.pass.cpp new file mode 100644 index 000000000000..1a453be6c339 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/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 discrete_distribution + +// discrete_distribution(const discrete_distribution&); + +#include +#include + +void +test1() +{ + typedef std::discrete_distribution<> D; + double p[] = {2, 4, 1, 8}; + D d1(p, p+4); + D d2 = d1; + assert(d1 == d2); +} + +int main() +{ + test1(); +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_default.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_default.pass.cpp new file mode 100644 index 000000000000..dd4019308151 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_default.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// discrete_distribution(initializer_list wl); + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + D d; + std::vector p = d.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_func.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_func.pass.cpp new file mode 100644 index 000000000000..bad03ab5224d --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_func.pass.cpp @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// template +// discrete_distribution(size_t nw, double xmin, double xmax, +// UnaryOperation fw); + +#include +#include + +double fw(double x) +{ + return x+1; +} + +int main() +{ + { + typedef std::discrete_distribution<> D; + D d(0, 0, 1, fw); + std::vector p = d.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + D d(1, 0, 1, fw); + std::vector p = d.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + D d(2, 0.5, 1.5, fw); + std::vector p = d.probabilities(); + assert(p.size() == 2); + assert(p[0] == .4375); + assert(p[1] == .5625); + } + { + typedef std::discrete_distribution<> D; + D d(4, 0, 2, fw); + std::vector p = d.probabilities(); + assert(p.size() == 4); + assert(p[0] == .15625); + assert(p[1] == .21875); + assert(p[2] == .28125); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_init.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_init.pass.cpp new file mode 100644 index 000000000000..fffb0dbaf432 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_init.pass.cpp @@ -0,0 +1,81 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// discrete_distribution(initializer_list wl); + +#include +#include + +int main() +{ +#ifdef _LIBCPP_MOVE + { + typedef std::discrete_distribution<> D; + D d = {}; + std::vector p = d.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + D d = {10}; + std::vector p = d.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + D d = {10, 30}; + std::vector p = d.probabilities(); + assert(p.size() == 2); + assert(p[0] == 0.25); + assert(p[1] == 0.75); + } + { + typedef std::discrete_distribution<> D; + D d = {30, 10}; + std::vector p = d.probabilities(); + assert(p.size() == 2); + assert(p[0] == 0.75); + assert(p[1] == 0.25); + } + { + typedef std::discrete_distribution<> D; + D d = {30, 0, 10}; + std::vector p = d.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0.75); + assert(p[1] == 0); + assert(p[2] == 0.25); + } + { + typedef std::discrete_distribution<> D; + D d = {0, 30, 10}; + std::vector p = d.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0); + assert(p[1] == 0.75); + assert(p[2] == 0.25); + } + { + typedef std::discrete_distribution<> D; + D d = {0, 0, 10}; + std::vector p = d.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0); + assert(p[1] == 0); + assert(p[2] == 1); + } +#endif +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_iterator.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_iterator.pass.cpp new file mode 100644 index 000000000000..9bc2161c7b31 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_iterator.pass.cpp @@ -0,0 +1,87 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// template +// discrete_distribution(InputIterator firstW, InputIterator lastW); + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + double p0[] = {1}; + D d(p0, p0); + std::vector p = d.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + double p0[] = {10}; + D d(p0, p0+1); + std::vector p = d.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + double p0[] = {10, 30}; + D d(p0, p0+2); + std::vector p = d.probabilities(); + assert(p.size() == 2); + assert(p[0] == 0.25); + assert(p[1] == 0.75); + } + { + typedef std::discrete_distribution<> D; + double p0[] = {30, 10}; + D d(p0, p0+2); + std::vector p = d.probabilities(); + assert(p.size() == 2); + assert(p[0] == 0.75); + assert(p[1] == 0.25); + } + { + typedef std::discrete_distribution<> D; + double p0[] = {30, 0, 10}; + D d(p0, p0+3); + std::vector p = d.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0.75); + assert(p[1] == 0); + assert(p[2] == 0.25); + } + { + typedef std::discrete_distribution<> D; + double p0[] = {0, 30, 10}; + D d(p0, p0+3); + std::vector p = d.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0); + assert(p[1] == 0.75); + assert(p[2] == 0.25); + } + { + typedef std::discrete_distribution<> D; + double p0[] = {0, 0, 10}; + D d(p0, p0+3); + std::vector p = d.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0); + assert(p[1] == 0); + assert(p[2] == 1); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_param.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_param.pass.cpp new file mode 100644 index 000000000000..10651ddc116a --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/ctor_param.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 discrete_distribution + +// explicit discrete_distribution(const param_type& parm); + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + double p0[] = {10, 30}; + P pa(p0, p0+2); + D d(pa); + std::vector p = d.probabilities(); + assert(p.size() == 2); + assert(p[0] == 0.25); + assert(p[1] == 0.75); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eq.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eq.pass.cpp new file mode 100644 index 000000000000..5691bbf33a94 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eq.pass.cpp @@ -0,0 +1,45 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// bool operator=(const discrete_distribution& x, +// const discrete_distribution& y); +// bool operator!(const discrete_distribution& x, +// const discrete_distribution& y); + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + D d1; + D d2; + assert(d1 == d2); + } + { + typedef std::discrete_distribution<> D; + double p0[] = {1}; + D d1(p0, p0+1); + D d2; + assert(d1 == d2); + } + { + typedef std::discrete_distribution<> D; + double p0[] = {10, 30}; + D d1(p0, p0+2); + D d2; + assert(d1 != d2); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp new file mode 100644 index 000000000000..037aa39e3651 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp @@ -0,0 +1,277 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// template result_type operator()(_URNG& g); + +#include +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + D d; + const int N = 100; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + assert((double)u[i]/N == prob[i]); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {.3}; + D d(p0, p0+1); + const int N = 100; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + assert((double)u[i]/N == prob[i]); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {.75, .25}; + D d(p0, p0+2); + const int N = 1000000; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {0, 1}; + D d(p0, p0+2); + const int N = 1000000; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + assert((double)u[0]/N == prob[0]); + assert((double)u[1]/N == prob[1]); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {1, 0}; + D d(p0, p0+2); + const int N = 1000000; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + assert((double)u[0]/N == prob[0]); + assert((double)u[1]/N == prob[1]); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {.3, .1, .6}; + D d(p0, p0+3); + const int N = 10000000; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {0, 25, 75}; + D d(p0, p0+3); + const int N = 1000000; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + if (prob[i] != 0) + assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); + else + assert(u[i] == 0); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {25, 0, 75}; + D d(p0, p0+3); + const int N = 1000000; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + if (prob[i] != 0) + assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); + else + assert(u[i] == 0); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {25, 75, 0}; + D d(p0, p0+3); + const int N = 1000000; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + if (prob[i] != 0) + assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); + else + assert(u[i] == 0); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {0, 0, 1}; + D d(p0, p0+3); + const int N = 100; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + if (prob[i] != 0) + assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); + else + assert(u[i] == 0); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {0, 1, 0}; + D d(p0, p0+3); + const int N = 100; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + if (prob[i] != 0) + assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); + else + assert(u[i] == 0); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {1, 0, 0}; + D d(p0, p0+3); + const int N = 100; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + if (prob[i] != 0) + assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); + else + assert(u[i] == 0); + } + { + typedef std::discrete_distribution<> D; + typedef std::minstd_rand G; + G g; + double p0[] = {33, 0, 0, 67}; + D d(p0, p0+3); + const int N = 1000000; + std::vector u(d.max()+1); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g); + assert(d.min() <= v && v <= d.max()); + u[v]++; + } + std::vector prob = d.probabilities(); + for (int i = 0; i <= d.max(); ++i) + if (prob[i] != 0) + assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); + else + assert(u[i] == 0); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eval_param.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eval_param.pass.cpp new file mode 100644 index 000000000000..d2c5a6788de1 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/eval_param.pass.cpp @@ -0,0 +1,43 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// template result_type operator()(_URNG& g, const param_type& parm); + +#include +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + typedef std::minstd_rand G; + G g; + D d; + double p0[] = {.3, .1, .6}; + P p(p0, p0+3); + const int N = 10000000; + std::vector u(3); + for (int i = 0; i < N; ++i) + { + D::result_type v = d(g, p); + assert(0 <= v && v <= 2); + u[v]++; + } + std::vector prob = p.probabilities(); + for (int i = 0; i <= 2; ++i) + assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/get_param.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/get_param.pass.cpp new file mode 100644 index 000000000000..1e36a2623795 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/get_param.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 discrete_distribution + +// param_type param() const; + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + double p0[] = {.3, .1, .6}; + P p(p0, p0+3); + D d(p); + assert(d.param() == p); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/io.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/io.pass.cpp new file mode 100644 index 000000000000..c8e8a5e1c20b --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/io.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 discrete_distribution + +// template +// basic_ostream& +// operator<<(basic_ostream& os, +// const discrete_distribution& x); +// +// template +// basic_istream& +// operator>>(basic_istream& is, +// discrete_distribution& x); + +#include +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + double p0[] = {.3, .1, .6}; + D d1(p0, p0+3); + std::ostringstream os; + os << d1; + std::istringstream is(os.str()); + D d2; + is >> d2; + assert(d1 == d2); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/max.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/max.pass.cpp new file mode 100644 index 000000000000..c7787f1f3725 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/max.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 discrete_distribution + +// result_type max() const; + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + double p0[] = {.3, .1, .6}; + D d(p0, p0+3); + assert(d.max() == 2); + } + { + typedef std::discrete_distribution<> D; + double p0[] = {.3, .1, .6, .2}; + D d(p0, p0+4); + assert(d.max() == 3); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/min.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/min.pass.cpp new file mode 100644 index 000000000000..64738e474c68 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/min.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 discrete_distribution + +// result_type min() const; + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + double p0[] = {.3, .1, .6}; + D d(p0, p0+3); + assert(d.min() == 0); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_assign.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_assign.pass.cpp new file mode 100644 index 000000000000..acbf6fab4c00 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_assign.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 discrete_distribution +// { +// class param_type; + +#include +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type param_type; + double d0[] = {.3, .1, .6}; + param_type p0(d0, d0+3); + param_type p; + p = p0; + assert(p == p0); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_copy.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_copy.pass.cpp new file mode 100644 index 000000000000..e8877b8595ad --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_copy.pass.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution +// { +// class param_type; + +#include +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type param_type; + double d0[] = {.3, .1, .6}; + param_type p0(d0, d0+3); + param_type p = p0; + assert(p == p0); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_default.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_default.pass.cpp new file mode 100644 index 000000000000..756dd3ee09c7 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_default.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 discrete_distribution + +// param_type(initializer_list wl); + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa; + std::vector p = pa.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_func.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_func.pass.cpp new file mode 100644 index 000000000000..648305d85400 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_func.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// template +// param_type(size_t nw, double xmin, double xmax, +// UnaryOperation fw); + +#include +#include + +double fw(double x) +{ + return x+1; +} + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa(0, 0, 1, fw); + std::vector p = pa.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa(1, 0, 1, fw); + std::vector p = pa.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa(2, 0.5, 1.5, fw); + std::vector p = pa.probabilities(); + assert(p.size() == 2); + assert(p[0] == .4375); + assert(p[1] == .5625); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa(4, 0, 2, fw); + std::vector p = pa.probabilities(); + assert(p.size() == 4); + assert(p[0] == .15625); + assert(p[1] == .21875); + assert(p[2] == .28125); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_init.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_init.pass.cpp new file mode 100644 index 000000000000..bf01b0ae17f2 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_init.pass.cpp @@ -0,0 +1,88 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// param_type(initializer_list wl); + +#include +#include + +int main() +{ +#ifdef _LIBCPP_MOVE + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa = {}; + std::vector p = pa.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa = {10}; + std::vector p = pa.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa = {10, 30}; + std::vector p = pa.probabilities(); + assert(p.size() == 2); + assert(p[0] == 0.25); + assert(p[1] == 0.75); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa = {30, 10}; + std::vector p = pa.probabilities(); + assert(p.size() == 2); + assert(p[0] == 0.75); + assert(p[1] == 0.25); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa = {30, 0, 10}; + std::vector p = pa.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0.75); + assert(p[1] == 0); + assert(p[2] == 0.25); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa = {0, 30, 10}; + std::vector p = pa.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0); + assert(p[1] == 0.75); + assert(p[2] == 0.25); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + P pa = {0, 0, 10}; + std::vector p = pa.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0); + assert(p[1] == 0); + assert(p[2] == 1); + } +#endif +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_iterator.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_iterator.pass.cpp new file mode 100644 index 000000000000..110dd6154131 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_ctor_iterator.pass.cpp @@ -0,0 +1,94 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// template +// param_type(InputIterator firstW, InputIterator lastW); + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + double p0[] = {1}; + P pa(p0, p0); + std::vector p = pa.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + double p0[] = {10}; + P pa(p0, p0+1); + std::vector p = pa.probabilities(); + assert(p.size() == 1); + assert(p[0] == 1); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + double p0[] = {10, 30}; + P pa(p0, p0+2); + std::vector p = pa.probabilities(); + assert(p.size() == 2); + assert(p[0] == 0.25); + assert(p[1] == 0.75); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + double p0[] = {30, 10}; + P pa(p0, p0+2); + std::vector p = pa.probabilities(); + assert(p.size() == 2); + assert(p[0] == 0.75); + assert(p[1] == 0.25); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + double p0[] = {30, 0, 10}; + P pa(p0, p0+3); + std::vector p = pa.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0.75); + assert(p[1] == 0); + assert(p[2] == 0.25); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + double p0[] = {0, 30, 10}; + P pa(p0, p0+3); + std::vector p = pa.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0); + assert(p[1] == 0.75); + assert(p[2] == 0.25); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + double p0[] = {0, 0, 10}; + P pa(p0, p0+3); + std::vector p = pa.probabilities(); + assert(p.size() == 3); + assert(p[0] == 0); + assert(p[1] == 0); + assert(p[2] == 1); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_eq.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_eq.pass.cpp new file mode 100644 index 000000000000..dd10bff7df82 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_eq.pass.cpp @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution +// { +// class param_type; + +#include +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type param_type; + double p0[] = {30, 10}; + param_type p1(p0, p0+2); + param_type p2(p0, p0+2); + assert(p1 == p2); + } + { + typedef std::discrete_distribution<> D; + typedef D::param_type param_type; + double p0[] = {30, 10}; + param_type p1(p0, p0+2); + param_type p2; + assert(p1 != p2); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_types.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/param_types.pass.cpp new file mode 100644 index 000000000000..82d2bdcb5074 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/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 discrete_distribution +// { +// class param_type; + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type param_type; + typedef param_type::distribution_type distribution_type; + static_assert((std::is_same::value), ""); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/set_param.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/set_param.pass.cpp new file mode 100644 index 000000000000..f3043f703216 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/set_param.pass.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class discrete_distribution + +// void param(const param_type& parm); + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::param_type P; + double d0[] = {.3, .1, .6}; + P p(d0, d0+3); + D d; + d.param(p); + assert(d.param() == p); + } +} diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/types.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/types.pass.cpp new file mode 100644 index 000000000000..8871e1d6ab07 --- /dev/null +++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.discrete/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 discrete_distribution +// { +// typedef bool result_type; + +#include +#include + +int main() +{ + { + typedef std::discrete_distribution<> D; + typedef D::result_type result_type; + static_assert((std::is_same::value), ""); + } + { + typedef std::discrete_distribution D; + typedef D::result_type result_type; + static_assert((std::is_same::value), ""); + } +}