xref: /llvm-project/libcxx/test/std/numerics/rand/rand.dist/rand.dist.norm/rand.dist.norm.cauchy/eval.pass.cpp (revision 76aa042dde6ba9ba57c680950f5818259ee02690)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // REQUIRES: long_tests
10 
11 // <random>
12 
13 // template<class RealType = double>
14 // class cauchy_distribution
15 
16 // template<class _URNG> result_type operator()(_URNG& g);
17 
18 #include <random>
19 
20 #include <cassert>
21 #include <cmath>
22 #include <vector>
23 #include <algorithm>
24 
25 #include "test_macros.h"
26 
27 double
f(double x,double a,double b)28 f(double x, double a, double b)
29 {
30     return 1/3.1415926535897932 * std::atan((x - a)/b) + .5;
31 }
32 
main(int,char **)33 int main(int, char**)
34 {
35     {
36         typedef std::cauchy_distribution<> D;
37         typedef std::mt19937 G;
38         G g;
39         const double a = 10;
40         const double b = .5;
41         D d(a, b);
42         const int N = 1000000;
43         std::vector<D::result_type> u;
44         for (int i = 0; i < N; ++i)
45             u.push_back(d(g));
46         std::sort(u.begin(), u.end());
47         for (int i = 0; i < N; ++i)
48           assert(std::abs(f(u[i], a, b) - double(i) / N) < .0013);
49     }
50     {
51         typedef std::cauchy_distribution<> D;
52         typedef std::mt19937 G;
53         G g;
54         const double a = -1.5;
55         const double b = 1;
56         D d(a, b);
57         const int N = 1000000;
58         std::vector<D::result_type> u;
59         for (int i = 0; i < N; ++i)
60             u.push_back(d(g));
61         std::sort(u.begin(), u.end());
62         for (int i = 0; i < N; ++i)
63           assert(std::abs(f(u[i], a, b) - double(i) / N) < .0013);
64     }
65     {
66         typedef std::cauchy_distribution<> D;
67         typedef std::mt19937 G;
68         G g;
69         const double a = .5;
70         const double b = 2;
71         D d(a, b);
72         const int N = 1000000;
73         std::vector<D::result_type> u;
74         for (int i = 0; i < N; ++i)
75             u.push_back(d(g));
76         std::sort(u.begin(), u.end());
77         for (int i = 0; i < N; ++i)
78           assert(std::abs(f(u[i], a, b) - double(i) / N) < .0013);
79     }
80 
81   return 0;
82 }
83