precision to steel decimal crate implemented

This commit is contained in:
filipriec
2025-07-07 00:31:13 +02:00
parent 314a957922
commit 4d5d22d0c2
4 changed files with 566 additions and 37 deletions

View File

@@ -243,3 +243,55 @@ fn test_scientific_notation(#[case] a: &str, #[case] b: &str, #[case] expected:
let result = decimal_add(a.to_string(), b.to_string()).unwrap();
assert_eq!(result, expected);
}
// Test precision behavior
#[rstest]
#[case("5", "0", "5")] // Integer + integer = integer
#[case("5.0", "0", "5.0")] // Decimal + integer = decimal
#[case("5.00", "0.00", "5.00")] // Preserves highest precision
#[case("5.1", "0.23", "5.33")] // Normal decimal arithmetic
fn test_precision_preservation(#[case] a: &str, #[case] b: &str, #[case] expected: &str) {
let result = decimal_add(a.to_string(), b.to_string()).unwrap();
assert_eq!(result, expected);
}
// Test explicit precision functions
#[rstest]
#[case("5.123", "2.456", 0, "8")] // 0 decimal places
#[case("5.123", "2.456", 2, "7.58")] // 2 decimal places
#[case("5.123", "2.456", 4, "7.5790")] // 4 decimal places
fn test_explicit_precision(#[case] a: &str, #[case] b: &str, #[case] precision: u32, #[case] expected: &str) {
let result = decimal_add_p(a.to_string(), b.to_string(), precision).unwrap();
assert_eq!(result, expected);
}
// Test scientific notation edge cases
#[rstest]
#[case("1e0", "1")] // Simple case
#[case("1.0e0", "1.0")] // Preserves decimal
#[case("1e-2", "0.01")] // Negative exponent
#[case("1.5e-3", "0.0015")] // Decimal + negative exponent
#[case("2.5e2", "250.0")] // Decimal + positive exponent
fn test_scientific_edge_cases(#[case] input: &str, #[case] expected: &str) {
let result = to_decimal(input.to_string()).unwrap();
assert_eq!(result, expected);
}
// Test precision functions
#[test]
fn test_precision_functions() {
// Test setting precision
assert_eq!(set_precision(2), "Precision set to 2 decimal places");
assert_eq!(get_precision(), "2");
// Test with precision set
let result = decimal_add("1.567".to_string(), "2.891".to_string()).unwrap();
assert_eq!(result, "4.46");
// Test clearing precision
assert_eq!(get_precision(), "full");
// Test with full precision
let result = decimal_add("1.567".to_string(), "2.891".to_string()).unwrap();
assert_eq!(result, "4.458");
}