-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrange.cpp
More file actions
131 lines (99 loc) · 2.55 KB
/
Copy pathrange.cpp
File metadata and controls
131 lines (99 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenQMC Project.
#include "hypothesis.h"
#include <oqmc/pcg.h>
#include <oqmc/range.h>
#include <oqmc/unused.h>
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
namespace
{
struct SamplerV1
{
void initialise(int seed)
{
state = oqmc::pcg::init(seed);
}
void sample(int index, std::uint32_t out[2])
{
OQMC_MAYBE_UNUSED(index);
// Does not divide into UINT32_MAX to test debiasing.
constexpr auto range = UINT32_MAX / 4 * 3;
constexpr auto scalar = UINT64_MAX / range;
std::uint32_t rnd[2];
rnd[0] = oqmc::uintToRange(oqmc::pcg::rng(state), range);
rnd[1] = oqmc::uintToRange(oqmc::pcg::rng(state), range);
out[0] = std::uint32_t(std::uint64_t(rnd[0]) * scalar >> 32);
out[1] = std::uint32_t(std::uint64_t(rnd[1]) * scalar >> 32);
}
std::uint32_t state;
};
ALL_HYPOTHESIS_TESTS(RangeTest, Debiased, (SamplerV1()))
constexpr std::array<std::uint32_t, 20> primes{
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
};
constexpr std::array<std::uint32_t, 10> powers{
1, 2, 4, 8, 16, 32, 64, 128, 256, 512,
};
TEST(RangeTest, BoundedRange)
{
for(auto range : primes)
{
auto state = oqmc::pcg::init();
for(int i = 0; i < 128; ++i)
{
const auto rnd = oqmc::uintToRange(oqmc::pcg::rng(state), range);
EXPECT_LT(rnd, range);
}
}
for(auto range : powers)
{
auto state = oqmc::pcg::init();
for(int i = 0; i < 128; ++i)
{
const auto rnd = oqmc::uintToRange(oqmc::pcg::rng(state), range);
EXPECT_LT(rnd, range);
}
}
}
TEST(RangeTest, BoundedBeginEnd)
{
for(auto range : primes)
{
auto state = oqmc::pcg::init();
for(int i = 0; i < 128; ++i)
{
auto stateA = state;
auto stateB = state;
const auto begin = range;
const auto end = range * 2;
const auto rndA =
oqmc::uintToRange(oqmc::pcg::rng(stateA), begin, end);
const auto rndB = oqmc::uintToRange(oqmc::pcg::rng(stateB), range);
EXPECT_GE(rndA, begin);
EXPECT_LT(rndA, end);
EXPECT_EQ(rndA - begin, rndB);
state = stateA;
}
}
for(auto range : powers)
{
auto state = oqmc::pcg::init();
for(int i = 0; i < 128; ++i)
{
auto stateA = state;
auto stateB = state;
const auto begin = range;
const auto end = range * 2;
const auto rndA =
oqmc::uintToRange(oqmc::pcg::rng(stateA), begin, end);
const auto rndB = oqmc::uintToRange(oqmc::pcg::rng(stateB), range);
EXPECT_GE(rndA, begin);
EXPECT_LT(rndA, end);
EXPECT_EQ(rndA - begin, rndB);
state = stateA;
}
}
}
} // namespace