Skip to content

Commit

Permalink
[vm] Added delta op tests
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemitenkov committed Jul 26, 2022
1 parent 3b9269e commit b0e5396
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ aptos-crypto-derive = { path = "../crates/aptos-crypto-derive" }
move-deps = { path = "../aptos-move/move-deps", features = ["address32"] }

[dev-dependencies]
claim = "0.5.0"
proptest = "1.0.0"
proptest-derive = "0.3.0"
regex = "1.5.5"
Expand Down
34 changes: 34 additions & 0 deletions types/src/delta_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,37 @@ pub fn serialize(value: &u128) -> Vec<u8> {
pub fn deserialize(value_bytes: &Vec<u8>) -> u128 {
bcs::from_bytes(value_bytes).expect("unexpected deserialization error")
}

#[cfg(test)]
mod tests {
use super::*;
use claim::{assert_matches, assert_some_eq};

fn addition(value: u128, limit: u128) -> DeltaOperation {
DeltaOperation::Addition { value, limit }
}

fn subtraction(value: u128) -> DeltaOperation {
DeltaOperation::Subtraction { value }
}

#[test]
fn test_delta_addition() {
let add5 = addition(5, 100);
assert_some_eq!(add5.apply_to(0), 5);
assert_some_eq!(add5.apply_to(5), 10);
assert_some_eq!(add5.apply_to(95), 100);

assert_matches!(add5.apply_to(96), None);
}

#[test]
fn test_delta_subtraction() {
let sub5 = subtraction(5);
assert_matches!(sub5.apply_to(0), None);
assert_matches!(sub5.apply_to(1), None);

assert_some_eq!(sub5.apply_to(5), 0);
assert_some_eq!(sub5.apply_to(100), 95);
}
}

0 comments on commit b0e5396

Please sign in to comment.