Skip to content

Commit

Permalink
Property testing with quickcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorLop committed Oct 9, 2023
1 parent 69a71c0 commit 9c30f6f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 26 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ edition = "2021"

[dependencies]
clap = { version = "4.4.6", features = ["derive", "cargo"] }
fake = { version = "2.8.0", features = ["dummy", "derive"] }

[dev-dependencies]
pretty_assertions = "1.4.0"
assert_float_eq = "1.1.3"
claim = "0.5.0"
fake = "2.8.0"
proptest = "1.3.1"
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"
rand = "0.8.5"
26 changes: 26 additions & 0 deletions src/investment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ impl Investment {
}
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(test, derive(fake::Dummy))]
pub struct InvestmentStatus {
year: usize,
deposited: PositiveFloat,
Expand Down Expand Up @@ -141,6 +143,30 @@ impl InvestmentStatus {
mod test {
use super::{Investment, InvestmentStatus, PositiveFloat};
use assert_float_eq::{afe_is_f64_near, afe_near_error_msg, assert_f64_near};
use fake::{Fake, Faker};

impl quickcheck::Arbitrary for InvestmentStatus {
fn arbitrary(_g: &mut quickcheck::Gen) -> Self {
Faker.fake()
}
}

#[quickcheck_macros::quickcheck]
fn test_investment_status_interest(status: InvestmentStatus) -> bool {
// Interest < balance
status.interest() < status.balance
}

#[quickcheck_macros::quickcheck]
fn test_investment_status_gross_profit(status: InvestmentStatus) -> bool {
// Gross profit > interest
status.gross_profit() > status.interest()
}
#[quickcheck_macros::quickcheck]
fn test_investment_status_net_profit(status: InvestmentStatus) -> bool {
// Net profit < Gross profit
status.net_profit() <= status.gross_profit()
}

#[test]
fn test_positive_profit_computation() {
Expand Down
70 changes: 44 additions & 26 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,54 @@ impl TryFrom<f64> for PositiveFloat {
}
}

#[cfg(test)]
mod faker {
use super::PositiveFloat;
use fake::{Dummy, Faker};

impl Dummy<Faker> for PositiveFloat {
fn dummy_with_rng<R: fake::Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
Self(rng.gen_range(0.0..100000000000000000.0))
}
}
}

#[cfg(test)]
mod test {
use super::PositiveFloat;
use claim::{assert_err, assert_ok_eq};
use proptest::num::f64::{NEGATIVE, POSITIVE};
use proptest::test_runner::TestRunner;

#[test]
fn test_positive_float_creation() {
let mut runner = TestRunner::default();

runner
.run(&POSITIVE, |val| {
let positive_float = PositiveFloat::try_from(val);
assert_ok_eq!(positive_float, PositiveFloat(val));
Ok(())
})
.unwrap();
use claim::assert_ok_eq;
use rand::Rng;

#[derive(Clone, Debug)]
struct ValidNumberFixture(pub f64);

#[derive(Clone, Debug)]
struct InvalidNumberFixture(pub f64);

impl quickcheck::Arbitrary for ValidNumberFixture {
fn arbitrary(_g: &mut quickcheck::Gen) -> Self {
let mut rng = rand::thread_rng();
Self(rng.gen_range(0.0..10000000000.0))
}
}

impl quickcheck::Arbitrary for InvalidNumberFixture {
fn arbitrary(_g: &mut quickcheck::Gen) -> Self {
let mut rng = rand::thread_rng();
Self(rng.gen_range(-10000000000.0..-0.000001))
}
}

#[quickcheck_macros::quickcheck]
fn test_positive_float_creation(valid_number: ValidNumberFixture) -> bool {
let positive_float = PositiveFloat::try_from(valid_number.0);
assert_ok_eq!(positive_float, PositiveFloat(valid_number.0));
positive_float.is_ok()
}

#[test]
fn test_positive_float_error() {
let mut runner = TestRunner::default();

runner
.run(&NEGATIVE, |val| {
let invalid_float = PositiveFloat::try_from(val);
assert_err!(invalid_float);
Ok(())
})
.unwrap();
#[quickcheck_macros::quickcheck]
fn test_positive_float_error(invalid_number: InvalidNumberFixture) -> bool {
let invalid_float = PositiveFloat::try_from(invalid_number.0);
invalid_float.is_err()
}
}

0 comments on commit 9c30f6f

Please sign in to comment.