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, const param_type& parm);
17 
18 #include <random>
19 #include <cassert>
20 #include <vector>
21 #include <algorithm>
22 
23 #include "test_macros.h"
24 
25 double
f(double x,double a,double b)26 f(double x, double a, double b)
27 {
28     return 1/3.1415926535897932 * std::atan((x - a)/b) + .5;
29 }
30 
main(int,char **)31 int main(int, char**)
32 {
33     {
34         typedef std::cauchy_distribution<> D;
35         typedef D::param_type P;
36         typedef std::mt19937 G;
37         G g;
38         const double a = 10;
39         const double b = .5;
40         D d;
41         P p(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, p));
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) < .001);
49     }
50     {
51         typedef std::cauchy_distribution<> D;
52         typedef D::param_type P;
53         typedef std::mt19937 G;
54         G g;
55         const double a = -1.5;
56         const double b = 1;
57         D d;
58         P p(a, b);
59         const int N = 1000000;
60         std::vector<D::result_type> u;
61         for (int i = 0; i < N; ++i)
62             u.push_back(d(g, p));
63         std::sort(u.begin(), u.end());
64         for (int i = 0; i < N; ++i)
65             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
66     }
67     {
68         typedef std::cauchy_distribution<> D;
69         typedef D::param_type P;
70         typedef std::mt19937 G;
71         G g;
72         const double a = .5;
73         const double b = 2;
74         D d;
75         P p(a, b);
76         const int N = 1000000;
77         std::vector<D::result_type> u;
78         for (int i = 0; i < N; ++i)
79             u.push_back(d(g, p));
80         std::sort(u.begin(), u.end());
81         for (int i = 0; i < N; ++i)
82             assert(std::abs(f(u[i], a, b) - double(i)/N) < .001);
83     }
84 
85   return 0;
86 }
87