libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

eval_param.pass.cpp (2266B)


      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // REQUIRES: long_tests
     11 
     12 // <random>
     13 
     14 // template<class RealType = double>
     15 // class cauchy_distribution
     16 
     17 // template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
     18 
     19 #include <random>
     20 #include <cassert>
     21 #include <vector>
     22 #include <algorithm>
     23 
     24 double
     25 f(double x, double a, double b)
     26 {
     27     return 1/3.1415926535897932 * std::atan((x - a)/b) + .5;
     28 }
     29 
     30 int main()
     31 {
     32     {
     33         typedef std::cauchy_distribution<> D;
     34         typedef D::param_type P;
     35         typedef std::mt19937 G;
     36         G g;
     37         const double a = 10;
     38         const double b = .5;
     39         D d;
     40         P p(a, b);
     41         const int N = 1000000;
     42         std::vector<D::result_type> u;
     43         for (int i = 0; i < N; ++i)
     44             u.push_back(d(g, p));
     45         std::sort(u.begin(), u.end());
     46         for (int i = 0; i < N; ++i)
     47             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
     48     }
     49     {
     50         typedef std::cauchy_distribution<> D;
     51         typedef D::param_type P;
     52         typedef std::mt19937 G;
     53         G g;
     54         const double a = -1.5;
     55         const double b = 1;
     56         D d;
     57         P p(a, b);
     58         const int N = 1000000;
     59         std::vector<D::result_type> u;
     60         for (int i = 0; i < N; ++i)
     61             u.push_back(d(g, p));
     62         std::sort(u.begin(), u.end());
     63         for (int i = 0; i < N; ++i)
     64             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
     65     }
     66     {
     67         typedef std::cauchy_distribution<> D;
     68         typedef D::param_type P;
     69         typedef std::mt19937 G;
     70         G g;
     71         const double a = .5;
     72         const double b = 2;
     73         D d;
     74         P p(a, b);
     75         const int N = 1000000;
     76         std::vector<D::result_type> u;
     77         for (int i = 0; i < N; ++i)
     78             u.push_back(d(g, p));
     79         std::sort(u.begin(), u.end());
     80         for (int i = 0; i < N; ++i)
     81             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
     82     }
     83 }