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// UNSUPPORTED: c++03
10
11// <random>
12
13// template<class RealType = double>
14// class piecewise_linear_distribution
15
16// piecewise_linear_distribution(initializer_list<result_type> bl,
17// UnaryOperation fw);
18
19#include <random>
20
21#include <cassert>
22#include <vector>
23
24#include "test_macros.h"
25
26double f(double x)
27{
28 return x*2;
29}
30
31int main(int, char**)
32{
33 {
34 typedef std::piecewise_linear_distribution<> D;
35 D d({}, f);
36 std::vector<double> iv = d.intervals();
37 assert(iv.size() == 2);
38 assert(iv[0] == 0);
39 assert(iv[1] == 1);
40 std::vector<double> dn = d.densities();
41 assert(dn.size() == 2);
42 assert(dn[0] == 1);
43 assert(dn[1] == 1);
44 }
45 {
46 typedef std::piecewise_linear_distribution<> D;
47 D d({12}, f);
48 std::vector<double> iv = d.intervals();
49 assert(iv.size() == 2);
50 assert(iv[0] == 0);
51 assert(iv[1] == 1);
52 std::vector<double> dn = d.densities();
53 assert(dn.size() == 2);
54 assert(dn[0] == 1);
55 assert(dn[1] == 1);
56 }
57 {
58 typedef std::piecewise_linear_distribution<> D;
59 D d({10, 12}, f);
60 std::vector<double> iv = d.intervals();
61 assert(iv.size() == 2);
62 assert(iv[0] == 10);
63 assert(iv[1] == 12);
64 std::vector<double> dn = d.densities();
65 assert(dn.size() == 2);
66 assert(dn[0] == 20./44);
67 assert(dn[1] == 24./44);
68 }
69 {
70 typedef std::piecewise_linear_distribution<> D;
71 D d({6, 10, 14}, f);
72 std::vector<double> iv = d.intervals();
73 assert(iv.size() == 3);
74 assert(iv[0] == 6);
75 assert(iv[1] == 10);
76 assert(iv[2] == 14);
77 std::vector<double> dn = d.densities();
78 assert(dn.size() == 3);
79 assert(dn[0] == 0.075);
80 assert(dn[1] == 0.125);
81 assert(dn[2] == 0.175);
82 }
83
84 return 0;
85}
86

source code of libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/ctor_init_func.pass.cpp