|
| 1 | +//! A generator that checks a handful of cases near infinities, zeros, asymptotes, and NaNs. |
| 2 | +
|
| 3 | +use libm::support::Float; |
| 4 | + |
| 5 | +use crate::domain::{Domain, HasDomain}; |
| 6 | +use crate::{FloatExt, MathOp}; |
| 7 | + |
| 8 | +/// Number of values near an interesting point to check. |
| 9 | +// FIXME(ntests): replace this with a more logical algorithm |
| 10 | +const AROUND: usize = 100; |
| 11 | + |
| 12 | +/// Functions have infinite asymptotes, limit how many we check. |
| 13 | +const MAX_CHECK_POINTS: usize = 10; |
| 14 | + |
| 15 | +/// Create a list of values around interesting points (infinities, zeroes, NaNs). |
| 16 | +pub fn get_test_cases<Op>() -> impl Iterator<Item = (Op::FTy,)> |
| 17 | +where |
| 18 | + Op: MathOp + HasDomain<Op::FTy>, |
| 19 | +{ |
| 20 | + let mut values = Vec::new(); |
| 21 | + populate_values::<Op::FTy>(&mut values, Op::D); |
| 22 | + values.sort_by_key(|x| x.to_bits()); |
| 23 | + values.dedup_by_key(|x| x.to_bits()); |
| 24 | + values.into_iter().map(|v| (v,)) |
| 25 | +} |
| 26 | + |
| 27 | +fn populate_values<F: Float>(values: &mut Vec<F>, domain: Domain<F>) { |
| 28 | + // Check near some notable constants |
| 29 | + count_up(F::ONE, values); |
| 30 | + count_up(F::ZERO, values); |
| 31 | + count_up(F::NEG_ONE, values); |
| 32 | + count_down(F::ONE, values); |
| 33 | + count_down(F::ZERO, values); |
| 34 | + count_down(F::NEG_ONE, values); |
| 35 | + values.push(F::NEG_ZERO); |
| 36 | + |
| 37 | + // Check values near the extremes |
| 38 | + values.push(F::INFINITY); |
| 39 | + values.push(F::NEG_INFINITY); |
| 40 | + values.push(F::MIN); |
| 41 | + values.push(F::MAX); |
| 42 | + count_up(F::MIN, values); |
| 43 | + count_down(F::MAX, values); |
| 44 | + |
| 45 | + // Check some special values that aren't included in the above ranges |
| 46 | + values.push(F::NAN); |
| 47 | + values.extend(F::consts().iter()); |
| 48 | + |
| 49 | + // Check around asymptotest |
| 50 | + if let Some(f) = domain.check_points { |
| 51 | + let iter = f(); |
| 52 | + for x in iter.take(MAX_CHECK_POINTS) { |
| 53 | + count_up(x, values); |
| 54 | + count_down(x, values); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Add `AROUND` values starting at `x` and counting up, using the smallest possible increments |
| 60 | +/// (1 ULP). |
| 61 | +fn count_up<F: Float>(mut x: F, values: &mut Vec<F>) { |
| 62 | + assert!(!x.is_nan()); |
| 63 | + assert!(!x.is_infinite()); |
| 64 | + |
| 65 | + let mut count = 0; |
| 66 | + while !x.is_infinite() && count < AROUND { |
| 67 | + values.push(x); |
| 68 | + x = x.next_up(); |
| 69 | + count += 1; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// Add `AROUND` values starting at `x` and counting down, using the smallest possible increments |
| 74 | +/// (1 ULP). |
| 75 | +fn count_down<F: Float>(mut x: F, values: &mut Vec<F>) { |
| 76 | + assert!(!x.is_nan()); |
| 77 | + assert!(!x.is_infinite()); |
| 78 | + |
| 79 | + let mut count = 0; |
| 80 | + while !x.is_infinite() && count < AROUND { |
| 81 | + values.push(x); |
| 82 | + x = x.next_down(); |
| 83 | + count += 1; |
| 84 | + } |
| 85 | +} |
0 commit comments