Skip to content

Commit

Permalink
Add quickcheck for to_{f32,f64} consistent with u128
Browse files Browse the repository at this point in the history
  • Loading branch information
dramforever committed Apr 28, 2023
1 parent 7433d22 commit 2bc992c
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions ci/big_quickcheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

use num_bigint::{BigInt, BigUint};
use num_integer::Integer;
use num_traits::{Num, One, Signed, Zero};
use quickcheck::{Gen, QuickCheck, TestResult};
use num_traits::{Num, One, Signed, ToPrimitive, Zero};
use quickcheck::{Arbitrary, Gen, QuickCheck, TestResult};
use quickcheck_macros::quickcheck;
use std::{fmt, ops::BitOr};

#[quickcheck]
fn quickcheck_unsigned_eq_reflexive(a: BigUint) -> bool {
Expand Down Expand Up @@ -357,3 +358,38 @@ fn quickcheck_modpow() {

qc.quickcheck(test_modpow as fn(i128, u128, i128) -> TestResult);
}

#[derive(Clone, Copy)]
struct SparseU128(u128);

impl fmt::Debug for SparseU128 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("SparseU128")
.field(&format_args!("{:#x}", self.0))
.finish()
}
}

// Generates a value with only a few bits set to test rounding
impl Arbitrary for SparseU128 {
fn arbitrary(gen: &mut Gen) -> Self {
let (a0, a1, a2, a3) = <_>::arbitrary(gen);
let value = [a0, a1, a2, a3]
.iter()
.map(|p: &u32| 1 << (p % 128))
.fold(0, u128::bitor);
SparseU128(value)
}
}

// Check for consistency of conversion to float with u128

#[quickcheck]
fn quickcheck_to_f32_through_u128(value: SparseU128) -> bool {
BigInt::from(value.0).to_f32() == Some(value.0 as f32)
}

#[quickcheck]
fn quickcheck_to_f64_through_u128(value: SparseU128) -> bool {
BigInt::from(value.0).to_f64() == Some(value.0 as f64)
}

0 comments on commit 2bc992c

Please sign in to comment.