Skip to content

Commit

Permalink
Cover utils crate with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slumber committed Nov 30, 2020
1 parent 7a61e24 commit 54ce750
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
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.

5 changes: 4 additions & 1 deletion core/lib/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ categories = ["cryptography"]
[dependencies]
num = { version = "0.2", features = ["serde"] }
bigdecimal = { version = "0.1", features = ["serde"]}
serde = "1.0"
serde = { version = "1.0", features = ["derive"] }
anyhow = "1.0"
futures = "0.3"

[dev-dependencies]
serde_json = "1.0.0"
54 changes: 54 additions & 0 deletions core/lib/utils/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,57 @@ pub fn round_precision(num: &Ratio<BigUint>, precision: usize) -> Ratio<BigUint>
let numerator = (num * &ten_pow).trunc().to_integer();
Ratio::new(numerator, ten_pow)
}

#[cfg(test)]
mod test {
use super::*;
use std::str::FromStr;

#[test]
fn test_ratio_to_big_decimal() {
let ratio = Ratio::from_integer(BigUint::from(0u32));
let dec = ratio_to_big_decimal(&ratio, 1);
assert_eq!(dec.to_string(), "0.0");
let ratio = Ratio::from_integer(BigUint::from(1234u32));
let dec = ratio_to_big_decimal(&ratio, 7);
assert_eq!(dec.to_string(), "1234.0000000");
// 4 divided by 9 is 0.(4).
let ratio = Ratio::new(BigUint::from(4u32), BigUint::from(9u32));
let dec = ratio_to_big_decimal(&ratio, 12);
assert_eq!(dec.to_string(), "0.444444444444");
// First 7 decimal digits of pi.
let ratio = Ratio::new(BigUint::from(52163u32), BigUint::from(16604u32));
let dec = ratio_to_big_decimal(&ratio, 6);
assert_eq!(dec.to_string(), "3.141592");
}

#[test]
fn test_big_decimal_to_ratio() {
// Expect unsigned number.
let dec = BigDecimal::from(-1);
assert!(big_decimal_to_ratio(&dec).is_err());
let expected = Ratio::from_integer(BigUint::from(0u32));
let dec = BigDecimal::from(0);
let ratio = big_decimal_to_ratio(&dec).unwrap();
assert_eq!(ratio, expected);
let expected = Ratio::new(BigUint::from(1234567u32), BigUint::from(10000u32));
let dec = BigDecimal::from_str("123.4567").unwrap();
let ratio = big_decimal_to_ratio(&dec).unwrap();
assert_eq!(ratio, expected);
}

#[test]
fn test_round_precision() {
let ratio = Ratio::new(BigUint::from(4u32), BigUint::from(9u32));
let rounded = round_precision(&ratio, 6);
assert_eq!(ratio_to_big_decimal(&rounded, 6).to_string(), "0.444444");
let ratio = Ratio::new(BigUint::from(355u32), BigUint::from(113u32));
let rounded = round_precision(&ratio, 6);
assert_eq!(ratio_to_big_decimal(&rounded, 6).to_string(), "3.141592");
// 9.87648 with precision of 2 digits is 987 / 100.
let ratio = Ratio::new(BigUint::from(123456u32), BigUint::from(12500u32));
let rounded = round_precision(&ratio, 2);
let expected = Ratio::new(BigUint::from(987u32), BigUint::from(100u32));
assert_eq!(rounded, expected);
}
}
22 changes: 22 additions & 0 deletions core/lib/utils/src/env_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,25 @@ where
})
.ok()
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_env_tools() {
const KEY: &str = "KEY";
// Our test environment variable.
env::set_var(KEY, "123");
assert_eq!(get_env(KEY), "123");
assert_eq!(parse_env::<i32>(KEY), 123);
assert_eq!(parse_env_if_exists::<i32>(KEY), Some(123));

env::remove_var(KEY);
assert_eq!(parse_env_if_exists::<i32>(KEY), None);

env::set_var(KEY, "ABC123");
let parsed: i32 = parse_env_with(KEY, |key| &key[3..]);
assert_eq!(parsed, 123);
}
}
36 changes: 36 additions & 0 deletions core/lib/utils/src/serde_wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,39 @@ impl From<BigUint> for BigUintSerdeWrapper {
BigUintSerdeWrapper(uint)
}
}

#[cfg(test)]
mod test {
use super::*;

/// Tests that `Ratio` serializer works correctly.
#[test]
fn test_ratio_serialize_as_decimal() {
#[derive(Clone, Serialize, Deserialize)]
struct RatioSerdeWrapper(
#[serde(with = "UnsignedRatioSerializeAsDecimal")] pub Ratio<BigUint>,
);
// It's essential that this number is a finite decimal, otherwise the precision will be lost
// and the assertion will fail.
let expected = RatioSerdeWrapper(Ratio::new(
BigUint::from(120315391195132u64),
BigUint::from(1250000000u64),
));
let value =
serde_json::to_value(expected.clone()).expect("cannot serialize Ratio as Decimal");
let ratio: RatioSerdeWrapper =
serde_json::from_value(value).expect("cannot deserialize Ratio from Decimal");
assert_eq!(expected.0, ratio.0);
}

/// Tests that `BigUint` serializer works correctly.
#[test]
fn test_serde_big_uint_wrapper() {
let expected = BigUint::from(u64::MAX);
let wrapper = BigUintSerdeWrapper::from(expected.clone());
let value = serde_json::to_value(wrapper).expect("cannot serialize BigUintSerdeWrapper");
let uint: BigUintSerdeWrapper =
serde_json::from_value(value).expect("cannot deserialize BigUintSerdeWrapper");
assert_eq!(uint.0, expected);
}
}

0 comments on commit 54ce750

Please sign in to comment.