Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check add and sub operation for owned fraction #229

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions invoice/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use strict_encoding::{StrictDeserialize, StrictSerialize};
use strict_types::StrictVal;

use crate::LIB_NAME_RGB_CONTRACT;

Expand Down Expand Up @@ -91,6 +92,50 @@
)]
pub struct OwnedFraction(u64);

impl OwnedFraction {
pub const ZERO: Self = OwnedFraction(0);

pub fn from_strict_val_unchecked(value: &StrictVal) -> Self {
value.unwrap_uint::<u64>().into()
}

pub fn value(self) -> u64 { self.0 }

pub fn saturating_add(&self, other: impl Into<Self>) -> Self {
self.0.saturating_add(other.into().0).into()
}
pub fn saturating_sub(&self, other: impl Into<Self>) -> Self {
self.0.saturating_sub(other.into().0).into()
}

Check warning on line 109 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L104-L109

Added lines #L104 - L109 were not covered by tests

pub fn saturating_add_assign(&mut self, other: impl Into<Self>) {
*self = self.0.saturating_add(other.into().0).into();
}
pub fn saturating_sub_assign(&mut self, other: impl Into<Self>) {
*self = self.0.saturating_sub(other.into().0).into();
}

Check warning on line 116 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L111-L116

Added lines #L111 - L116 were not covered by tests

#[must_use]
pub fn checked_add(&self, other: impl Into<Self>) -> Option<Self> {
self.0.checked_add(other.into().0).map(Self)
}
#[must_use]
pub fn checked_sub(&self, other: impl Into<Self>) -> Option<Self> {
self.0.checked_sub(other.into().0).map(Self)
}

#[must_use]
pub fn checked_add_assign(&mut self, other: impl Into<Self>) -> Option<()> {
*self = self.0.checked_add(other.into().0).map(Self)?;
Some(())
}
#[must_use]
pub fn checked_sub_assign(&mut self, other: impl Into<Self>) -> Option<()> {
*self = self.0.checked_sub(other.into().0).map(Self)?;
Some(())
}
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, Display)]
#[display("{1}@{0}")]
#[derive(StrictType, StrictEncode, StrictDecode)]
Expand Down Expand Up @@ -156,3 +201,95 @@
}
}
}

#[cfg(test)]
mod test {
use strict_types::value::StrictNum;

use super::*;

#[test]
fn owned_fraction_from_str() {
let owned_fraction = match OwnedFraction::from_str("1") {
Ok(value) => value,
Err(_) => OwnedFraction::ZERO,

Check warning on line 215 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L215

Added line #L215 was not covered by tests
};

assert_eq!(owned_fraction.value(), 1);
assert_eq!(format!("{owned_fraction}"), "1");
}

#[test]
fn owned_fraction_from_strict_val() {
// note that the strict number is u128 but not u64
let owned_fraction =
OwnedFraction::from_strict_val_unchecked(&StrictVal::Number(StrictNum::Uint(1)));

assert_eq!(owned_fraction.value(), 1);
assert_eq!(format!("{owned_fraction}"), "1");
}

#[test]
fn owned_fraction_add_assign() {
let mut owned_fraction = match OwnedFraction::from_str("1") {
Ok(value) => value,
Err(_) => OwnedFraction::ZERO,

Check warning on line 236 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L236

Added line #L236 was not covered by tests
};

let _ = owned_fraction.checked_add_assign(OwnedFraction::ZERO);
assert_eq!(owned_fraction.value(), 1);
assert_eq!(format!("{owned_fraction}"), "1");
}

#[test]
fn owned_fraction_add() {
let owned_fraction = match OwnedFraction::from_str("1") {
Ok(value) => value,
Err(_) => OwnedFraction::ZERO,

Check warning on line 248 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L248

Added line #L248 was not covered by tests
};

let owned = match owned_fraction.checked_add(OwnedFraction::ZERO) {
Some(value) => value,
None => OwnedFraction::ZERO,

Check warning on line 253 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L253

Added line #L253 was not covered by tests
};
assert_eq!(owned.value(), 1);
assert_eq!(format!("{owned}"), "1");
}

#[test]
fn owned_fraction_sub() {
let owned_fraction = match OwnedFraction::from_str("1") {
Ok(value) => value,
Err(_) => OwnedFraction::ZERO,

Check warning on line 263 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L263

Added line #L263 was not covered by tests
};

let other_fraction = match OwnedFraction::from_str("1") {
Ok(value) => value,
Err(_) => OwnedFraction::ZERO,

Check warning on line 268 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L268

Added line #L268 was not covered by tests
};

let owned = match owned_fraction.checked_sub(other_fraction) {
Some(value) => value,
None => OwnedFraction::ZERO,

Check warning on line 273 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L273

Added line #L273 was not covered by tests
};
assert_eq!(owned.value(), 0);
assert_eq!(format!("{owned}"), "0");
}

#[test]
fn owned_fraction_sub_assign() {
let mut owned_fraction = match OwnedFraction::from_str("1") {
Ok(value) => value,
Err(_) => OwnedFraction::ZERO,

Check warning on line 283 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L283

Added line #L283 was not covered by tests
};

let other_fraction = match OwnedFraction::from_str("1") {
Ok(value) => value,
Err(_) => OwnedFraction::ZERO,

Check warning on line 288 in invoice/src/data.rs

View check run for this annotation

Codecov / codecov/patch

invoice/src/data.rs#L288

Added line #L288 was not covered by tests
};

let _ = owned_fraction.checked_sub_assign(other_fraction);
assert_eq!(owned_fraction.value(), 0);
assert_eq!(format!("{owned_fraction}"), "0");
}
}
Loading