Skip to content

Commit

Permalink
Merge pull request #20 from invariant-labs/add-checked_div
Browse files Browse the repository at this point in the history
Add checked div
  • Loading branch information
wojciech-cichocki authored Oct 11, 2023
2 parents 0755759 + 65de7ea commit 7a1e0d0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
28 changes: 28 additions & 0 deletions decimal_core/src/checked_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ pub fn generate_checked_ops(characteristics: DecimalCharacteristics) -> proc_mac
.ok_or_else(|| "checked_sub: (self - rhs) subtraction underflow")?
))
}

fn checked_div(self, rhs: Self) -> std::result::Result<Self, String> {
Ok(Self::new(
self.get()
.checked_mul(Self::one())
.ok_or_else(|| "checked_div: (self * Self::one()) multiplication overflow")?
.checked_div(rhs.get())
.ok_or_else(|| "checked_div: ((self * Self::one()) / rhs) division by zero")?
)
)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -54,6 +65,23 @@ pub fn generate_checked_ops(characteristics: DecimalCharacteristics) -> proc_mac
assert_eq!(a.checked_sub(b), Ok(#struct_name::new(24)));
}

#[test]
fn test_checked_div() {
let a = #struct_name::new(2);
let b = #struct_name::new(#struct_name::one());

assert_eq!(a.checked_div(b), Ok(#struct_name::new(2)));
}

#[test]
fn test_0_checked_div() {
let a = #struct_name::new(47);
let b = #struct_name::new(0);
let result = a.checked_div(b);

assert!(result.is_err());
}

#[test]
fn test_underflow_checked_sub() {
let min = #struct_name::new(0);
Expand Down
2 changes: 1 addition & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub trait Factories<T>: Sized {
fn from_scale_up(integer: T, scale: u8) -> Self;
}


pub trait BetweenDecimals<T>: Sized {
fn from_decimal(other: T) -> Self;
fn checked_from_decimal(other: T) -> std::result::Result<Self, String>;
Expand Down Expand Up @@ -70,4 +69,5 @@ pub trait ByNumber<B>: Sized {
pub trait CheckedOps: Sized {
fn checked_add(self, rhs: Self) -> std::result::Result<Self, String>;
fn checked_sub(self, rhs: Self) -> std::result::Result<Self, String>;
fn checked_div(self, rhs: Self) -> std::result::Result<Self, String>;
}
16 changes: 12 additions & 4 deletions src/walkthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,19 @@ mod walkthrough {
let result = percentage.checked_sub(Percentage::new(10));
assert_eq!(result, Ok(Percentage::new(90)));
}
// checked_div
{
let result = Price::from_integer(99).checked_div(Price::from_scale(5, 1));
assert_eq!(result, Ok(Price::from_integer(198)));
}
// checked big div
{
let price = Price::max_instance().checked_big_div(Price::new(50000));
assert_eq!(price, Ok(Price::new(68056473384187692692674921486353642291)));
assert_eq!(
price,
Ok(Price::new(68056473384187692692674921486353642291))
);
}

// checked_from_scale
{
let overflow_err =
Expand All @@ -109,7 +116,8 @@ mod walkthrough {
}
// checked_from_scale_to_value
{
let result = Price::checked_from_scale_to_value(max_price_value, price_scale - 1).unwrap();
let result =
Price::checked_from_scale_to_value(max_price_value, price_scale - 1).unwrap();
assert_eq!(
result,
U256::from_dec_str("3402823669209384634633746074317682114550").unwrap()
Expand All @@ -125,7 +133,7 @@ mod walkthrough {
}
// checked_from_decimal_to_value
{
let result = Price::checked_from_decimal_to_value(Price::max_instance()).unwrap();
let result = Price::checked_from_decimal_to_value(Price::max_instance()).unwrap();
assert_eq!(result, U256::from(Price::max_value()));
}
// checked_big_div_by_number & checked_big_div_by_number_up
Expand Down

0 comments on commit 7a1e0d0

Please sign in to comment.