lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

decimal.rs (2025B)


      1 mod common;
      2 
      3 use core::str::FromStr;
      4 
      5 use radroots_core::RadrootsCoreDecimal;
      6 
      7 #[test]
      8 fn display_normalizes_trailing_zeros() {
      9     let d = RadrootsCoreDecimal::from_str("1.2300").unwrap();
     10     assert_eq!(d.to_string(), "1.23");
     11 }
     12 
     13 #[test]
     14 fn scale_reflects_input_precision() {
     15     let d = RadrootsCoreDecimal::from_str("1.2300").unwrap();
     16     assert_eq!(d.scale(), 4);
     17 }
     18 
     19 #[test]
     20 fn to_u64_exact_requires_whole_number() {
     21     let whole = common::dec("42.0");
     22     let frac = common::dec("42.5");
     23     assert_eq!(whole.to_u64_exact(), Some(42));
     24     assert_eq!(frac.to_u64_exact(), None);
     25 }
     26 
     27 #[test]
     28 fn from_f64_display_roundtrips_reasonably() {
     29     let d = RadrootsCoreDecimal::from_f64_display(1.25).unwrap();
     30     let v = d.to_f64_lossy().expect("f64 conversion");
     31     assert!((v - 1.25).abs() < 1e-12);
     32 }
     33 
     34 #[test]
     35 fn from_str_exact_and_conversion_impl_paths_are_exercised() {
     36     let exact = RadrootsCoreDecimal::from_str_exact("42.000").unwrap();
     37     assert_eq!(exact, common::dec("42"));
     38 
     39     let from_decimal = RadrootsCoreDecimal::from(rust_decimal::Decimal::from(5u32));
     40     assert_eq!(from_decimal, common::dec("5"));
     41     let back: rust_decimal::Decimal = from_decimal.into();
     42     assert_eq!(back, rust_decimal::Decimal::from(5u32));
     43 
     44     let from_u32 = RadrootsCoreDecimal::from(7u32);
     45     let from_i32 = RadrootsCoreDecimal::from(-2i32);
     46     let from_u64 = RadrootsCoreDecimal::from(11u64);
     47     let from_i64 = RadrootsCoreDecimal::from(-9i64);
     48     assert_eq!(from_u32, common::dec("7"));
     49     assert_eq!(from_i32, common::dec("-2"));
     50     assert_eq!(from_u64, common::dec("11"));
     51     assert_eq!(from_i64, common::dec("-9"));
     52 }
     53 
     54 #[cfg(feature = "serde")]
     55 #[test]
     56 fn serde_deserialize_error_paths_are_exercised() {
     57     let parse_err = serde_json::from_str::<RadrootsCoreDecimal>("\"not-a-decimal\"").unwrap_err();
     58     assert!(!parse_err.to_string().is_empty());
     59     let non_string_err = serde_json::from_str::<RadrootsCoreDecimal>("123").unwrap_err();
     60     assert!(non_string_err.to_string().contains("invalid type"));
     61 }