-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathedge_cases.rs
240 lines (205 loc) · 7.79 KB
/
edge_cases.rs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! A generator that checks a handful of cases near infinities, zeros, asymptotes, and NaNs.
use libm::support::{Float, Int};
use crate::domain::get_domain;
use crate::gen::KnownSize;
use crate::run_cfg::{check_near_count, check_point_count};
use crate::{CheckCtx, FloatExt, GeneratorKind, MathOp, test_log};
/// Generate a sequence of edge cases, e.g. numbers near zeroes and infiniteis.
pub trait EdgeCaseInput<Op> {
fn get_cases(ctx: CheckCtx) -> (impl Iterator<Item = Self> + Send, u64);
}
/// Create a list of values around interesting points (infinities, zeroes, NaNs).
fn float_edge_cases<Op>(
ctx: &CheckCtx,
argnum: usize,
) -> (impl Iterator<Item = Op::FTy> + Clone, u64)
where
Op: MathOp,
{
let mut ret = Vec::new();
let values = &mut ret;
let domain = get_domain::<_, i8>(ctx.fn_ident, argnum).unwrap_float();
let domain_start = domain.range_start();
let domain_end = domain.range_end();
let check_points = check_point_count(ctx);
let near_points = check_near_count(ctx);
// Check near some notable constants
count_up(Op::FTy::ONE, near_points, values);
count_up(Op::FTy::ZERO, near_points, values);
count_up(Op::FTy::NEG_ONE, near_points, values);
count_down(Op::FTy::ONE, near_points, values);
count_down(Op::FTy::ZERO, near_points, values);
count_down(Op::FTy::NEG_ONE, near_points, values);
values.push(Op::FTy::NEG_ZERO);
// Check values near the extremes
count_up(Op::FTy::NEG_INFINITY, near_points, values);
count_down(Op::FTy::INFINITY, near_points, values);
count_down(domain_end, near_points, values);
count_up(domain_start, near_points, values);
count_down(domain_start, near_points, values);
count_up(domain_end, near_points, values);
count_down(domain_end, near_points, values);
// Check some special values that aren't included in the above ranges
values.push(Op::FTy::NAN);
values.extend(Op::FTy::consts().iter());
// Check around asymptotes
if let Some(f) = domain.check_points {
let iter = f();
for x in iter.take(check_points) {
count_up(x, near_points, values);
count_down(x, near_points, values);
}
}
// Some results may overlap so deduplicate the vector to save test cycles.
values.sort_by_key(|x| x.to_bits());
values.dedup_by_key(|x| x.to_bits());
let count = ret.len().try_into().unwrap();
test_log(&format!(
"{gen_kind:?} {basis:?} {fn_ident} arg {arg}/{args}: {count} edge cases",
gen_kind = ctx.gen_kind,
basis = ctx.basis,
fn_ident = ctx.fn_ident,
arg = argnum + 1,
args = ctx.input_count(),
));
(ret.into_iter(), count)
}
/// Add `AROUND` values starting at and including `x` and counting up. Uses the smallest possible
/// increments (1 ULP).
fn count_up<F: Float>(mut x: F, points: u64, values: &mut Vec<F>) {
assert!(!x.is_nan());
let mut count = 0;
while x < F::INFINITY && count < points {
values.push(x);
x = x.next_up();
count += 1;
}
}
/// Add `AROUND` values starting at and including `x` and counting down. Uses the smallest possible
/// increments (1 ULP).
fn count_down<F: Float>(mut x: F, points: u64, values: &mut Vec<F>) {
assert!(!x.is_nan());
let mut count = 0;
while x > F::NEG_INFINITY && count < points {
values.push(x);
x = x.next_down();
count += 1;
}
}
/// Create a list of values around interesting integer points (min, zero, max).
pub fn int_edge_cases<I: Int>(
ctx: &CheckCtx,
_argnum: usize,
) -> (impl Iterator<Item = I> + Clone, u64) {
let mut values = Vec::new();
let near_points = check_near_count(ctx);
for up_from in [I::MIN, I::ZERO] {
let mut x = up_from;
for _ in 0..near_points {
values.push(x);
x += I::ONE;
}
}
for down_from in [I::ZERO, I::MAX] {
let mut x = down_from;
for _ in 0..near_points {
values.push(x);
x -= I::ONE;
}
}
values.sort();
values.dedup();
let len = values.len().try_into().unwrap();
(values.into_iter(), len)
}
macro_rules! impl_edge_case_input {
($fty:ty) => {
impl<Op> EdgeCaseInput<Op> for ($fty,)
where
Op: MathOp<RustArgs = Self, FTy = $fty>,
{
fn get_cases(ctx: CheckCtx) -> (impl Iterator<Item = Self>, u64) {
let ctx = &ctx;
let (iter0, steps0) = float_edge_cases::<Op>(ctx, 0);
let iter0 = iter0.map(|v| (v,));
(iter0, steps0)
}
}
impl<Op> EdgeCaseInput<Op> for ($fty, $fty)
where
Op: MathOp<RustArgs = Self, FTy = $fty>,
{
fn get_cases(ctx: CheckCtx) -> (impl Iterator<Item = Self>, u64) {
let ctx = &ctx;
let (iter0, steps0) = float_edge_cases::<Op>(ctx, 0);
let (iter1, steps1) = float_edge_cases::<Op>(ctx, 1);
let iter =
iter0.flat_map(move |first| iter1.clone().map(move |second| (first, second)));
let count = steps0.checked_mul(steps1).unwrap();
(iter, count)
}
}
impl<Op> EdgeCaseInput<Op> for ($fty, $fty, $fty)
where
Op: MathOp<RustArgs = Self, FTy = $fty>,
{
fn get_cases(ctx: CheckCtx) -> (impl Iterator<Item = Self>, u64) {
let ctx = &ctx;
let (iter0, steps0) = float_edge_cases::<Op>(ctx, 0);
let (iter1, steps1) = float_edge_cases::<Op>(ctx, 1);
let (iter2, steps2) = float_edge_cases::<Op>(ctx, 2);
let iter = iter0
.flat_map(move |first| iter1.clone().map(move |second| (first, second)))
.flat_map(move |(first, second)| {
iter2.clone().map(move |third| (first, second, third))
});
let count = steps0.checked_mul(steps1).unwrap().checked_mul(steps2).unwrap();
(iter, count)
}
}
impl<Op> EdgeCaseInput<Op> for (i32, $fty)
where
Op: MathOp<RustArgs = Self, FTy = $fty>,
{
fn get_cases(ctx: CheckCtx) -> (impl Iterator<Item = Self>, u64) {
let ctx = &ctx;
let (iter0, steps0) = int_edge_cases(ctx, 0);
let (iter1, steps1) = float_edge_cases::<Op>(ctx, 1);
let iter =
iter0.flat_map(move |first| iter1.clone().map(move |second| (first, second)));
let count = steps0.checked_mul(steps1).unwrap();
(iter, count)
}
}
impl<Op> EdgeCaseInput<Op> for ($fty, i32)
where
Op: MathOp<RustArgs = Self, FTy = $fty>,
{
fn get_cases(ctx: CheckCtx) -> (impl Iterator<Item = Self>, u64) {
let ctx = &ctx;
let (iter0, steps0) = float_edge_cases::<Op>(ctx, 0);
let (iter1, steps1) = int_edge_cases(ctx, 1);
let iter =
iter0.flat_map(move |first| iter1.clone().map(move |second| (first, second)));
let count = steps0.checked_mul(steps1).unwrap();
(iter, count)
}
}
};
}
#[cfg(f16_enabled)]
impl_edge_case_input!(f16);
impl_edge_case_input!(f32);
impl_edge_case_input!(f64);
#[cfg(f128_enabled)]
impl_edge_case_input!(f128);
pub fn get_test_cases<Op>(ctx: CheckCtx) -> (impl Iterator<Item = Op::RustArgs> + Send, u64)
where
Op: MathOp,
Op::RustArgs: EdgeCaseInput<Op>,
{
assert_eq!(ctx.gen_kind, GeneratorKind::EdgeCases);
let (iter, count) = Op::RustArgs::get_cases(ctx);
// Wrap in `KnownSize` so we get an assertion if the cuunt is wrong.
(KnownSize::new(iter, count), count)
}