Skip to content

Commit

Permalink
Property testing for Investment.status()
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorLop committed Oct 10, 2023
1 parent 9c30f6f commit 9ce25e0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
44 changes: 41 additions & 3 deletions src/investment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::types::PositiveFloat;
use fake::Dummy;

#[derive(Debug, Clone, Copy, Dummy)]
pub struct Investment {
deposit: PositiveFloat,
#[dummy(faker = "1..1000")]
years: usize,
annual_contribution: PositiveFloat,
interest_rate: f64,
Expand Down Expand Up @@ -151,20 +154,55 @@ mod test {
}
}

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

#[quickcheck_macros::quickcheck]
fn test_investment_simulate(investment: Investment) -> bool {
let results = investment.simulate();

for i in 0..results.len() {
if i != 0 {
if investment.interest_rate > 0.0 {
let conditions: [bool; 3] = [
results[i].interest() > results[i - 1].interest(),
results[i].gross_profit() > results[i - 1].gross_profit(),
results[i].net_profit() > results[i - 1].net_profit(),
];

if !conditions.iter().all(|&x| x == true) {
return false;
}
} else {
let conditions: [bool; 3] = [
results[i].interest() < results[i - 1].interest(),
results[i].gross_profit() < results[i - 1].gross_profit(),
results[i].net_profit() < results[i - 1].net_profit(),
];

if !conditions.iter().all(|&x| x == true) {
return false;
}
}
}
}
true
}

#[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()
}

Expand Down
14 changes: 5 additions & 9 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use fake::{Dummy, Faker};

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct PositiveFloat(pub f64);

Expand All @@ -19,15 +21,9 @@ 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))
}
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))
}
}

Expand Down

0 comments on commit 9ce25e0

Please sign in to comment.