From 76957ebff595fabc5b283646c69dda95f2031016 Mon Sep 17 00:00:00 2001 From: zielvna Date: Wed, 20 Sep 2023 16:05:57 +0200 Subject: [PATCH 1/3] add tests to factories functions --- decimal_core/src/factories.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/decimal_core/src/factories.rs b/decimal_core/src/factories.rs index 87e0fce..41d47dd 100644 --- a/decimal_core/src/factories.rs +++ b/decimal_core/src/factories.rs @@ -126,6 +126,7 @@ pub fn generate_factories(characteristics: DecimalCharacteristics) -> proc_macro base.checked_mul(multiplier.try_into().map_err(|_| "checked_from_scale_to_value: can't convert multiplier to big_type")?) .ok_or_else(|| "checked_from_scale_to_value: (multiplier * base) overflow")? } else { + // no possibility of overflow because of scale limit let denominator: u128 = 10u128.checked_pow((scale - #scale) as u32).ok_or_else(|| "checked_from_scale_to_value: denominator overflow")?; let base: #big_type = #big_type::try_from( val.try_into().map_err(|_| "checked_from_scale_to_value: can't convert val to base")?) @@ -229,7 +230,36 @@ pub fn generate_factories(characteristics: DecimalCharacteristics) -> proc_macro #struct_name::checked_from_scale(max_val, 100_000).is_err(), true ); + } + + #[test] + fn test_checked_from_scale_to_value() { + let result: i32 = #struct_name::checked_from_scale_to_value(0, 0).unwrap().try_into().unwrap(); + assert_eq!(result, 0); + + let result: i32 = #struct_name::checked_from_scale_to_value(0, 3).unwrap().try_into().unwrap(); + assert_eq!(result, 0); + + let result: i32 = #struct_name::checked_from_scale_to_value(42, #scale).unwrap().try_into().unwrap(); + assert_eq!(result, 42); + + let result: i32 = #struct_name::checked_from_scale_to_value(42, #scale + 1).unwrap().try_into().unwrap(); + assert_eq!(result, 4); + + let max_val = #struct_name::max_value(); + assert_eq!( + #struct_name::checked_from_scale_to_value(max_val, 100_000).is_err(), + true + ); + } + + #[test] + fn test_checked_from_decimal_to_value() { + let result: i32 = #struct_name::checked_from_decimal_to_value(#struct_name::new(1)).unwrap().try_into().unwrap(); + assert_eq!(result, 1); + let result: i32 = #struct_name::checked_from_decimal_to_value(#struct_name::new(42)).unwrap().try_into().unwrap(); + assert_eq!(result, 42); } } )) From 02a59fe3c70268e7879bb465f624c4a6913f3154 Mon Sep 17 00:00:00 2001 From: Wojciech Date: Wed, 20 Sep 2023 16:26:25 +0200 Subject: [PATCH 2/3] Comment out similar case --- decimal_core/src/factories.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/decimal_core/src/factories.rs b/decimal_core/src/factories.rs index 41d47dd..ea4517c 100644 --- a/decimal_core/src/factories.rs +++ b/decimal_core/src/factories.rs @@ -121,6 +121,7 @@ pub fn generate_factories(characteristics: DecimalCharacteristics) -> proc_macro val.try_into().map_err(|_| "checked_from_scale_to_value: can't convert val to base")?) .map_err(|_| "checked_from_scale_to_value: can't convert val to big_type" )?; + // no possibility of overflow because of scale limit let multiplier: u128 = 10u128.checked_pow((#scale - scale) as u32).ok_or_else(|| "checked_from_scale_to_value: multiplier overflow")?; base.checked_mul(multiplier.try_into().map_err(|_| "checked_from_scale_to_value: can't convert multiplier to big_type")?) From 209ad2b8c93371d5b3b515e28035c429c08f1a9f Mon Sep 17 00:00:00 2001 From: zielvna Date: Wed, 20 Sep 2023 16:28:45 +0200 Subject: [PATCH 3/3] add test case with big scale to checked_from_scale_to_value function --- decimal_core/src/factories.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/decimal_core/src/factories.rs b/decimal_core/src/factories.rs index ea4517c..2f188ea 100644 --- a/decimal_core/src/factories.rs +++ b/decimal_core/src/factories.rs @@ -251,7 +251,10 @@ pub fn generate_factories(characteristics: DecimalCharacteristics) -> proc_macro assert_eq!( #struct_name::checked_from_scale_to_value(max_val, 100_000).is_err(), true - ); + ); + + let result: i32 = #struct_name::checked_from_scale_to_value(1, 30).unwrap().try_into().unwrap(); + assert_eq!(result, 0); } #[test]