Skip to content

Commit 2bcc2d1

Browse files
committed
Add unit tests
1 parent 4e688ba commit 2bcc2d1

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

packages/std/src/math/decimal.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,42 @@ mod tests {
20502050
));
20512051
}
20522052

2053+
#[test]
2054+
fn decimal_to_uint_floor_works() {
2055+
let d = Decimal::from_str("12.000000000000000001").unwrap();
2056+
assert_eq!(d.to_uint_floor(), Uint128::new(12));
2057+
let d = Decimal::from_str("12.345").unwrap();
2058+
assert_eq!(d.to_uint_floor(), Uint128::new(12));
2059+
let d = Decimal::from_str("12.999").unwrap();
2060+
assert_eq!(d.to_uint_floor(), Uint128::new(12));
2061+
2062+
let d = Decimal::from_str("75.0").unwrap();
2063+
assert_eq!(d.to_uint_floor(), Uint128::new(75));
2064+
let d = Decimal::from_str("0.0").unwrap();
2065+
assert_eq!(d.to_uint_floor(), Uint128::new(0));
2066+
2067+
let d = Decimal::MAX;
2068+
assert_eq!(d.to_uint_floor(), Uint128::new(340282366920938463463));
2069+
}
2070+
2071+
#[test]
2072+
fn decimal_to_uint_ceil_works() {
2073+
let d = Decimal::from_str("12.000000000000000001").unwrap();
2074+
assert_eq!(d.to_uint_ceil(), Uint128::new(13));
2075+
let d = Decimal::from_str("12.345").unwrap();
2076+
assert_eq!(d.to_uint_ceil(), Uint128::new(13));
2077+
let d = Decimal::from_str("12.999").unwrap();
2078+
assert_eq!(d.to_uint_ceil(), Uint128::new(13));
2079+
2080+
let d = Decimal::from_str("75.0").unwrap();
2081+
assert_eq!(d.to_uint_ceil(), Uint128::new(75));
2082+
let d = Decimal::from_str("0.0").unwrap();
2083+
assert_eq!(d.to_uint_ceil(), Uint128::new(0));
2084+
2085+
let d = Decimal::MAX;
2086+
assert_eq!(d.to_uint_ceil(), Uint128::new(340282366920938463464));
2087+
}
2088+
20532089
#[test]
20542090
fn decimal_partial_eq() {
20552091
let test_cases = [

packages/std/src/math/decimal256.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,50 @@ mod tests {
21972197
assert_eq!(Decimal256::MAX.checked_ceil(), Err(RoundUpOverflowError));
21982198
}
21992199

2200+
#[test]
2201+
fn decimal256_to_uint_floor_works() {
2202+
let d = Decimal256::from_str("12.000000000000000001").unwrap();
2203+
assert_eq!(d.to_uint_floor(), Uint256::from_u128(12));
2204+
let d = Decimal256::from_str("12.345").unwrap();
2205+
assert_eq!(d.to_uint_floor(), Uint256::from_u128(12));
2206+
let d = Decimal256::from_str("12.999").unwrap();
2207+
assert_eq!(d.to_uint_floor(), Uint256::from_u128(12));
2208+
2209+
let d = Decimal256::from_str("75.0").unwrap();
2210+
assert_eq!(d.to_uint_floor(), Uint256::from_u128(75));
2211+
let d = Decimal256::from_str("0.0").unwrap();
2212+
assert_eq!(d.to_uint_floor(), Uint256::from_u128(0));
2213+
2214+
let d = Decimal256::MAX;
2215+
assert_eq!(
2216+
d.to_uint_floor(),
2217+
Uint256::from_str("115792089237316195423570985008687907853269984665640564039457")
2218+
.unwrap()
2219+
);
2220+
}
2221+
2222+
#[test]
2223+
fn decimal256_to_uint_ceil_works() {
2224+
let d = Decimal256::from_str("12.000000000000000001").unwrap();
2225+
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(13));
2226+
let d = Decimal256::from_str("12.345").unwrap();
2227+
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(13));
2228+
let d = Decimal256::from_str("12.999").unwrap();
2229+
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(13));
2230+
2231+
let d = Decimal256::from_str("75.0").unwrap();
2232+
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(75));
2233+
let d = Decimal256::from_str("0.0").unwrap();
2234+
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(0));
2235+
2236+
let d = Decimal256::MAX;
2237+
assert_eq!(
2238+
d.to_uint_ceil(),
2239+
Uint256::from_str("115792089237316195423570985008687907853269984665640564039458")
2240+
.unwrap()
2241+
);
2242+
}
2243+
22002244
#[test]
22012245
fn decimal256_partial_eq() {
22022246
let test_cases = [

0 commit comments

Comments
 (0)