diff --git a/radix-engine-macros/src/decimal.rs b/radix-engine-macros/src/decimal.rs index dec1679c6c1..bf83643e69c 100644 --- a/radix-engine-macros/src/decimal.rs +++ b/radix-engine-macros/src/decimal.rs @@ -9,11 +9,22 @@ extern crate radix_engine_common; macro_rules! get_decimal { ($type:ident) => { paste! { - fn [< get_ $type:snake:lower _from_expr >](expr: &Expr) -> Result<$type> { + fn [< get_ $type:snake:lower _from_expr >](expr: &Expr, negate: bool) -> Result<$type> { match expr { Expr::Lit(lit) => match &lit.lit { - Lit::Str(lit_str) => $type::try_from(lit_str.value()) - .map_err(|err| Error::new(lit_str.span(), [< $type:snake:lower _error_reason >](err).to_string())), + Lit::Str(lit_str) => { + // Do not allow string literal preceeded with '-', eg. -"12.3" + if !negate { + $type::try_from(lit_str.value()) + .map_err(|err| Error::new(lit_str.span(), [< $type:snake:lower _error_reason >](err).to_string())) + } + else { + Err(Error::new( + lit_str.span(), + "This macro only supports string, integer and float literals.", + )) + } + }, Lit::Int(lit_int) => { if lit_int.suffix() != "" { Err(Error::new( @@ -21,8 +32,16 @@ macro_rules! get_decimal { format!("No suffix is allowed. Remove the {}.", lit_int.suffix()), )) } else { - $type::try_from(lit_int.base10_digits()) - .map_err(|err| Error::new(lit_int.span(), [< $type:snake:lower _error_reason >](err).to_string())) + let mut val = $type::try_from(lit_int.base10_digits()) + .map_err(|err| Error::new(lit_int.span(), [< $type:snake:lower _error_reason >](err).to_string()))?; + + // Negate received value if negate flag is set. + // Safe from overflow. + if negate { + val = val.checked_neg() + .ok_or(Error::new(lit_int.span(), [< $type:snake:lower _error_reason >]([< Parse $type Error >]::Overflow).to_string()))?; + } + Ok(val) } } Lit::Float(lit_float) => { @@ -32,7 +51,19 @@ macro_rules! get_decimal { format!("No suffix is allowed. Remove the {}.", lit_float.suffix()), )) } else { - $type::try_from(lit_float.base10_digits()) + let digits = lit_float.base10_digits(); + + // Preceed the literal digits with '-" if negate flag is set. + // And then convert received string to decimal. + // This is to avoid negation of the received decimal, which + // overflows for MIN value. + let s = if negate { + "-".to_string() + digits + } else { + digits.to_string() + }; + + $type::try_from(s) .map_err(|err| Error::new(lit_float.span(), [< $type:snake:lower _error_reason >](err).to_string())) } } @@ -43,13 +74,15 @@ macro_rules! get_decimal { }, Expr::Unary(unary) => match unary.op { UnOp::Neg(unary_neg) => { - let res = [< get_ $type:snake:lower _from_expr >](unary.expr.as_ref()); - match res { - Ok(val) => { - let val = val.checked_neg().ok_or(Error::new(unary_neg.span, "Parsing failed due to overflow."))?; - Ok(val) - }, - Err(err) => Err(Error::new(unary_neg.span, err)), + // Do not allow multiple '-' + if !negate { + [< get_ $type:snake:lower _from_expr >](unary.expr.as_ref(), true) + } + else { + Err(Error::new( + unary_neg.span(), + "This macro only supports string, integer and float literals.", + )) } } other_unary => Err(Error::new( @@ -121,7 +154,7 @@ pub fn to_decimal(input: TokenStream) -> Result { // Parse the input into an Expression let expr = parse::(input)?; - let decimal = get_decimal_from_expr(&expr)?; + let decimal = get_decimal_from_expr(&expr, false)?; let int = decimal.0; let arr = int.to_digits(); let i0 = arr[0]; @@ -137,7 +170,7 @@ pub fn to_precise_decimal(input: TokenStream) -> Result { // Parse the input into an Expression let expr = parse::(input)?; - let decimal = get_precise_decimal_from_expr(&expr)?; + let decimal = get_precise_decimal_from_expr(&expr, false)?; let int = decimal.0; let arr = int.to_digits(); let i0 = arr[0]; diff --git a/radix-engine-tests/Cargo.toml b/radix-engine-tests/Cargo.toml index 232e5b7f45c..f4d931fdcab 100644 --- a/radix-engine-tests/Cargo.toml +++ b/radix-engine-tests/Cargo.toml @@ -38,6 +38,7 @@ paste = { version = "1.0.13" } lazy_static = { version = "1.4.0" } hex = { version = "0.4.3", default-features = false } tuple-return = { path = "./tests/blueprints/tuple-return", features = ["test"], default-features = false } +trybuild = { version = "1.0.85" } [build-dependencies] walkdir = { version = "2.3.3", optional = true } diff --git a/radix-engine-tests/tests/blueprints/Cargo.toml b/radix-engine-tests/tests/blueprints/Cargo.toml index db03f0842f2..0c04a83f544 100644 --- a/radix-engine-tests/tests/blueprints/Cargo.toml +++ b/radix-engine-tests/tests/blueprints/Cargo.toml @@ -65,7 +65,8 @@ members = [ "wasm_non_mvp", "royalty-edge-cases", "system_wasm_buffers", - "event-replacement" + "event-replacement", + "decimal" ] [profile.release] diff --git a/radix-engine-tests/tests/blueprints/decimal/Cargo.toml b/radix-engine-tests/tests/blueprints/decimal/Cargo.toml new file mode 100644 index 00000000000..ae066015e4a --- /dev/null +++ b/radix-engine-tests/tests/blueprints/decimal/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "decimal" +version = "1.0.0-rc1" +edition = "2021" + +[dependencies] +sbor = { path = "../../../../sbor" } +scrypto = { path = "../../../../scrypto" } + +[dev-dependencies] +radix-engine = { path = "../../../../radix-engine" } + +[lib] +doctest = false +crate-type = ["cdylib", "lib"] diff --git a/radix-engine-tests/tests/blueprints/decimal/src/lib.rs b/radix-engine-tests/tests/blueprints/decimal/src/lib.rs new file mode 100644 index 00000000000..87e9152504e --- /dev/null +++ b/radix-engine-tests/tests/blueprints/decimal/src/lib.rs @@ -0,0 +1,35 @@ +use scrypto::prelude::*; + +#[blueprint] +mod cast_test { + const GD: Decimal = dec!(2); + const GP: PreciseDecimal = pdec!(2); + + struct DecimalTest {} + + impl DecimalTest { + pub fn test_dec_macro() -> Decimal { + const C: Decimal = dec!("1111.2222"); + static S: Decimal = dec!(2222.1111); + + C.checked_add(S) + .unwrap() + .checked_add(dec!(-0.3333)) + .unwrap() + .checked_mul(GD) + .unwrap() + } + + pub fn test_pdec_macro() -> PreciseDecimal { + const C: PreciseDecimal = pdec!("1111.2222"); + static S: PreciseDecimal = pdec!(2222.1111); + + C.checked_add(S) + .unwrap() + .checked_add(pdec!(-0.3333)) + .unwrap() + .checked_mul(GP) + .unwrap() + } + } +} diff --git a/radix-engine-tests/tests/dec_macros/Cargo.toml b/radix-engine-tests/tests/dec_macros/Cargo.toml new file mode 100644 index 00000000000..b89aad8d7eb --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/Cargo.toml @@ -0,0 +1,31 @@ +[workspace] +members = ["."] + +[package] +name = "dec_macros" +version = "1.0.0-rc1" +edition = "2021" + +[dependencies] +radix-engine-interface = { path = "../../../radix-engine-interface", default-features = false } + +# Stub lib to make 'trybuild' crate happy +[lib] +path = "src/lib.rs" + +# Below targets are not required to for the 'dec_macro' tests. +# Adding just a few as a template. +# If one needs to build some other target with 'cargo build' then below shall be updated. +[[bin]] +name = "dec_success" +path = "src/dec_success.rs" + +[[bin]] +name = "dec_err_expr_not_supported" +path = "src/dec_err_expr_not_supported.rs" + +[features] +default = ["std"] +std = ["radix-engine-interface/std"] +alloc = ["radix-engine-interface/alloc"] + diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_1.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_1.rs new file mode 100644 index 00000000000..34b4fcfb0ca --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_1.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(1.); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_1.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_1.stderr new file mode 100644 index 00000000000..9fa80800f97 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_1.stderr @@ -0,0 +1,5 @@ +error: If there is a decimal point, the number must include at least one digit after it. + --> src/dec_err_empty_fractional_part_1.rs:4:29 + | +4 | const X: Decimal = dec!(1.); + | ^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_2.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_2.rs new file mode 100644 index 00000000000..8e9aa14b518 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_2.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!("1."); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_2.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_2.stderr new file mode 100644 index 00000000000..0aae5674e3e --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_2.stderr @@ -0,0 +1,5 @@ +error: If there is a decimal point, the number must include at least one digit after it. + --> src/dec_err_empty_fractional_part_2.rs:4:29 + | +4 | const X: Decimal = dec!("1."); + | ^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_empty_integral_part.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_integral_part.rs new file mode 100644 index 00000000000..fa4027b4b89 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_integral_part.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(".0"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_empty_integral_part.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_integral_part.stderr new file mode 100644 index 00000000000..0b54b84c5b5 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_empty_integral_part.stderr @@ -0,0 +1,5 @@ +error: If there is a decimal point, the number must include at least one digit before it. Use a 0 if necessary. + --> src/dec_err_empty_integral_part.rs:4:29 + | +4 | const X: Decimal = dec!(".0"); + | ^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_expected_expr.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_expected_expr.rs new file mode 100644 index 00000000000..ddbcbbdd3ab --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_expected_expr.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(.1); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_expected_expr.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_expected_expr.stderr new file mode 100644 index 00000000000..6581ae76c62 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_expected_expr.stderr @@ -0,0 +1,5 @@ +error: expected expression + --> src/dec_err_expected_expr.rs:4:29 + | +4 | const X: Decimal = dec!(.1); + | ^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_expr_not_supported.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_expr_not_supported.rs new file mode 100644 index 00000000000..bfb19ca0973 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_expr_not_supported.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(a); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_expr_not_supported.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_expr_not_supported.stderr new file mode 100644 index 00000000000..be161fc478a --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_expr_not_supported.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/dec_err_expr_not_supported.rs:4:29 + | +4 | const X: Decimal = dec!(a); + | ^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_expr_unary_op_not_supported.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_expr_unary_op_not_supported.rs new file mode 100644 index 00000000000..aee012feeb4 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_expr_unary_op_not_supported.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(!1); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_expr_unary_op_not_supported.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_expr_unary_op_not_supported.stderr new file mode 100644 index 00000000000..68cf2d5d8ad --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_expr_unary_op_not_supported.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/dec_err_expr_unary_op_not_supported.rs:4:29 + | +4 | const X: Decimal = dec!(!1); + | ^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_float_suffix.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_float_suffix.rs new file mode 100644 index 00000000000..fa6f8a03768 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_float_suffix.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(11.12f64); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_float_suffix.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_float_suffix.stderr new file mode 100644 index 00000000000..d8a9a7a8b31 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_float_suffix.stderr @@ -0,0 +1,5 @@ +error: No suffix is allowed. Remove the f64. + --> src/dec_err_float_suffix.rs:4:29 + | +4 | const X: Decimal = dec!(11.12f64); + | ^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_int_suffix.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_int_suffix.rs new file mode 100644 index 00000000000..e7bdc225e49 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_int_suffix.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(11_i64); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_int_suffix.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_int_suffix.stderr new file mode 100644 index 00000000000..638ab013aab --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_int_suffix.stderr @@ -0,0 +1,5 @@ +error: No suffix is allowed. Remove the i64. + --> src/dec_err_int_suffix.rs:4:29 + | +4 | const X: Decimal = dec!(11_i64); + | ^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_invalid_digit.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_invalid_digit.rs new file mode 100644 index 00000000000..83456586b5e --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_invalid_digit.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(11.12e3); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_invalid_digit.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_invalid_digit.stderr new file mode 100644 index 00000000000..6b78494d48a --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_invalid_digit.stderr @@ -0,0 +1,5 @@ +error: There is an invalid character. + --> src/dec_err_invalid_digit.rs:4:29 + | +4 | const X: Decimal = dec!(11.12e3); + | ^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_literal_not_supported.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_literal_not_supported.rs new file mode 100644 index 00000000000..4091d1e94ac --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_literal_not_supported.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!('a'); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_literal_not_supported.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_literal_not_supported.stderr new file mode 100644 index 00000000000..ba454c53c80 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_literal_not_supported.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/dec_err_literal_not_supported.rs:4:29 + | +4 | const X: Decimal = dec!('a'); + | ^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_more_than_one_decimal_point.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_more_than_one_decimal_point.rs new file mode 100644 index 00000000000..2cf75486ad2 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_more_than_one_decimal_point.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!("1.000.0"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_more_than_one_decimal_point.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_more_than_one_decimal_point.stderr new file mode 100644 index 00000000000..046725295a7 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_more_than_one_decimal_point.stderr @@ -0,0 +1,5 @@ +error: A decimal cannot have more than one decimal point. + --> src/dec_err_more_than_one_decimal_point.rs:4:29 + | +4 | const X: Decimal = dec!("1.000.0"); + | ^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_multiple_negations.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_multiple_negations.rs new file mode 100644 index 00000000000..00b3a95fe1d --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_multiple_negations.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(--1); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_multiple_negations.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_multiple_negations.stderr new file mode 100644 index 00000000000..3b349ae04a2 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_multiple_negations.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/dec_err_multiple_negations.rs:4:30 + | +4 | const X: Decimal = dec!(--1); + | ^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_negation_and_string.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_negation_and_string.rs new file mode 100644 index 00000000000..80c90e36eaa --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_negation_and_string.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(-"112.3"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_negation_and_string.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_negation_and_string.stderr new file mode 100644 index 00000000000..f2a8f7db183 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_negation_and_string.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/dec_err_negation_and_string.rs:4:30 + | +4 | const X: Decimal = dec!(-"112.3"); + | ^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_1.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_1.rs new file mode 100644 index 00000000000..a1adb6d9109 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_1.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(-3138550867693340381917894711603833208051.177722232017256449); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_1.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_1.stderr new file mode 100644 index 00000000000..68703a595e4 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_1.stderr @@ -0,0 +1,5 @@ +error: The number is too large to fit in a decimal. + --> src/dec_err_overflow_1.rs:4:30 + | +4 | const X: Decimal = dec!(-3138550867693340381917894711603833208051.177722232017256449); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_2.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_2.rs new file mode 100644 index 00000000000..4b23f9669a7 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_2.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!("-3138550867693340381917894711603833208051.177722232017256449"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_2.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_2.stderr new file mode 100644 index 00000000000..a90c73bd5b2 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_2.stderr @@ -0,0 +1,5 @@ +error: The number is too large to fit in a decimal. + --> src/dec_err_overflow_2.rs:4:29 + | +4 | const X: Decimal = dec!("-3138550867693340381917894711603833208051.177722232017256449"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_3.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_3.rs new file mode 100644 index 00000000000..3cb6d60ab1b --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_3.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(3138550867693340381917894711603833208051.177722232017256448); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_3.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_3.stderr new file mode 100644 index 00000000000..4767c9a35fa --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_3.stderr @@ -0,0 +1,5 @@ +error: The number is too large to fit in a decimal. + --> src/dec_err_overflow_3.rs:4:29 + | +4 | const X: Decimal = dec!(3138550867693340381917894711603833208051.177722232017256448); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_4.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_4.rs new file mode 100644 index 00000000000..299bb861454 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_4.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!("3138550867693340381917894711603833208051.177722232017256448"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_4.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_4.stderr new file mode 100644 index 00000000000..4389c686e27 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_overflow_4.stderr @@ -0,0 +1,5 @@ +error: The number is too large to fit in a decimal. + --> src/dec_err_overflow_4.rs:4:29 + | +4 | const X: Decimal = dec!("3138550867693340381917894711603833208051.177722232017256448"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_too_many_decimal_places.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_too_many_decimal_places.rs new file mode 100644 index 00000000000..2394e84dd86 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_too_many_decimal_places.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(1.0000000000000000001); +} diff --git a/radix-engine-tests/tests/dec_macros/src/dec_err_too_many_decimal_places.stderr b/radix-engine-tests/tests/dec_macros/src/dec_err_too_many_decimal_places.stderr new file mode 100644 index 00000000000..d0f16c918dd --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_too_many_decimal_places.stderr @@ -0,0 +1,5 @@ +error: A decimal cannot have more than eighteen decimal places. + --> src/dec_err_too_many_decimal_places.rs:4:29 + | +4 | const X: Decimal = dec!(1.0000000000000000001); + | ^^^^^^^^^^^^^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_invalid_expression.stderr b/radix-engine-tests/tests/dec_macros/src/dec_invalid_expression.stderr new file mode 100644 index 00000000000..1478f7d53ac --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_invalid_expression.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/dec_invalid_expression.rs:5:29 + | +5 | const X: Decimal = dec!('a'); + | ^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/dec_success.rs b/radix-engine-tests/tests/dec_macros/src/dec_success.rs new file mode 100644 index 00000000000..30f0c01d1d8 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_success.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!("1"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/lib.rs b/radix-engine-tests/tests/dec_macros/src/lib.rs new file mode 100644 index 00000000000..aa8ff46d1f9 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/lib.rs @@ -0,0 +1,2 @@ +#[allow(dead_code)] +fn stub() {} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_1.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_1.rs new file mode 100644 index 00000000000..5e71b3b7f5e --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_1.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: PreciseDecimal = pdec!(1.); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_1.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_1.stderr new file mode 100644 index 00000000000..4bbf709db8d --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_1.stderr @@ -0,0 +1,5 @@ +error: If there is a decimal point, the number must include at least one digit after it. + --> src/pdec_err_empty_fractional_part_1.rs:4:37 + | +4 | const X: PreciseDecimal = pdec!(1.); + | ^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_2.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_2.rs new file mode 100644 index 00000000000..8e9aa14b518 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_2.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!("1."); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_2.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_2.stderr new file mode 100644 index 00000000000..5f888de94fd --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_2.stderr @@ -0,0 +1,5 @@ +error: If there is a decimal point, the number must include at least one digit after it. + --> src/pdec_err_empty_fractional_part_2.rs:4:29 + | +4 | const X: Decimal = dec!("1."); + | ^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_integral_part.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_integral_part.rs new file mode 100644 index 00000000000..356f72f5a9d --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_integral_part.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: PreciseDecimal = pdec!(".0"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_integral_part.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_integral_part.stderr new file mode 100644 index 00000000000..6867047726b --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_empty_integral_part.stderr @@ -0,0 +1,5 @@ +error: If there is a decimal point, the number must include at least one digit before it. Use a 0 if necessary. + --> src/pdec_err_empty_integral_part.rs:4:37 + | +4 | const X: PreciseDecimal = pdec!(".0"); + | ^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_expected_expr.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_expected_expr.rs new file mode 100644 index 00000000000..ddbcbbdd3ab --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_expected_expr.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(.1); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_expected_expr.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_expected_expr.stderr new file mode 100644 index 00000000000..691e6c686ba --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_expected_expr.stderr @@ -0,0 +1,5 @@ +error: expected expression + --> src/pdec_err_expected_expr.rs:4:29 + | +4 | const X: Decimal = dec!(.1); + | ^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_not_supported.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_not_supported.rs new file mode 100644 index 00000000000..bfb19ca0973 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_not_supported.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(a); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_not_supported.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_not_supported.stderr new file mode 100644 index 00000000000..48098a8ec7c --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_not_supported.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/pdec_err_expr_not_supported.rs:4:29 + | +4 | const X: Decimal = dec!(a); + | ^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_unary_op_not_supported.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_unary_op_not_supported.rs new file mode 100644 index 00000000000..aee012feeb4 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_unary_op_not_supported.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(!1); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_unary_op_not_supported.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_unary_op_not_supported.stderr new file mode 100644 index 00000000000..a8982ffe16b --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_expr_unary_op_not_supported.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/pdec_err_expr_unary_op_not_supported.rs:4:29 + | +4 | const X: Decimal = dec!(!1); + | ^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_float_suffix.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_float_suffix.rs new file mode 100644 index 00000000000..fa6f8a03768 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_float_suffix.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(11.12f64); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_float_suffix.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_float_suffix.stderr new file mode 100644 index 00000000000..2e71f7cbc0b --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_float_suffix.stderr @@ -0,0 +1,5 @@ +error: No suffix is allowed. Remove the f64. + --> src/pdec_err_float_suffix.rs:4:29 + | +4 | const X: Decimal = dec!(11.12f64); + | ^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_int_suffix.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_int_suffix.rs new file mode 100644 index 00000000000..e7bdc225e49 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_int_suffix.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(11_i64); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_int_suffix.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_int_suffix.stderr new file mode 100644 index 00000000000..f2adde67550 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_int_suffix.stderr @@ -0,0 +1,5 @@ +error: No suffix is allowed. Remove the i64. + --> src/pdec_err_int_suffix.rs:4:29 + | +4 | const X: Decimal = dec!(11_i64); + | ^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_invalid_digit.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_invalid_digit.rs new file mode 100644 index 00000000000..83456586b5e --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_invalid_digit.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(11.12e3); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_invalid_digit.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_invalid_digit.stderr new file mode 100644 index 00000000000..9cb65ac7c1d --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_invalid_digit.stderr @@ -0,0 +1,5 @@ +error: There is an invalid character. + --> src/pdec_err_invalid_digit.rs:4:29 + | +4 | const X: Decimal = dec!(11.12e3); + | ^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_literal_not_supported.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_literal_not_supported.rs new file mode 100644 index 00000000000..4091d1e94ac --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_literal_not_supported.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!('a'); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_literal_not_supported.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_literal_not_supported.stderr new file mode 100644 index 00000000000..0020777a979 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_literal_not_supported.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/pdec_err_literal_not_supported.rs:4:29 + | +4 | const X: Decimal = dec!('a'); + | ^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_more_than_one_decimal_point.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_more_than_one_decimal_point.rs new file mode 100644 index 00000000000..2cf75486ad2 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_more_than_one_decimal_point.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!("1.000.0"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_more_than_one_decimal_point.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_more_than_one_decimal_point.stderr new file mode 100644 index 00000000000..8cf114a1919 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_more_than_one_decimal_point.stderr @@ -0,0 +1,5 @@ +error: A decimal cannot have more than one decimal point. + --> src/pdec_err_more_than_one_decimal_point.rs:4:29 + | +4 | const X: Decimal = dec!("1.000.0"); + | ^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_multiple_negations.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_multiple_negations.rs new file mode 100644 index 00000000000..00b3a95fe1d --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_multiple_negations.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(--1); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_multiple_negations.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_multiple_negations.stderr new file mode 100644 index 00000000000..c98760c32a4 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_multiple_negations.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/pdec_err_multiple_negations.rs:4:30 + | +4 | const X: Decimal = dec!(--1); + | ^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_negation_and_string.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_negation_and_string.rs new file mode 100644 index 00000000000..80c90e36eaa --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_negation_and_string.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!(-"112.3"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_negation_and_string.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_negation_and_string.stderr new file mode 100644 index 00000000000..c2a618cef4b --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_negation_and_string.stderr @@ -0,0 +1,5 @@ +error: This macro only supports string, integer and float literals. + --> src/pdec_err_negation_and_string.rs:4:30 + | +4 | const X: Decimal = dec!(-"112.3"); + | ^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_1.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_1.rs new file mode 100644 index 00000000000..45f4da762a8 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_1.rs @@ -0,0 +1,6 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: PreciseDecimal = + pdec!(-57896044618658097711785492504343953926634.992332820282019728792003956564819969); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_1.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_1.stderr new file mode 100644 index 00000000000..c1837018b47 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_1.stderr @@ -0,0 +1,5 @@ +error: The number being too large to fit in a precise decimal + --> src/pdec_err_overflow_1.rs:5:16 + | +5 | pdec!(-57896044618658097711785492504343953926634.992332820282019728792003956564819969); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_2.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_2.rs new file mode 100644 index 00000000000..4b23f9669a7 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_2.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!("-3138550867693340381917894711603833208051.177722232017256449"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_2.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_2.stderr new file mode 100644 index 00000000000..05199d13b5d --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_2.stderr @@ -0,0 +1,5 @@ +error: The number is too large to fit in a decimal. + --> src/pdec_err_overflow_2.rs:4:29 + | +4 | const X: Decimal = dec!("-3138550867693340381917894711603833208051.177722232017256449"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_3.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_3.rs new file mode 100644 index 00000000000..8accf5d23e6 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_3.rs @@ -0,0 +1,6 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: PreciseDecimal = + pdec!(57896044618658097711785492504343953926634.992332820282019728792003956564819968); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_3.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_3.stderr new file mode 100644 index 00000000000..7ede67ceca6 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_3.stderr @@ -0,0 +1,5 @@ +error: The number being too large to fit in a precise decimal + --> src/pdec_err_overflow_3.rs:5:15 + | +5 | pdec!(57896044618658097711785492504343953926634.992332820282019728792003956564819968); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_4.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_4.rs new file mode 100644 index 00000000000..1db316fdce7 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_4.rs @@ -0,0 +1,6 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: PreciseDecimal = + pdec!("57896044618658097711785492504343953926634.992332820282019728792003956564819968"); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_4.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_4.stderr new file mode 100644 index 00000000000..c0359e3f386 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_4.stderr @@ -0,0 +1,5 @@ +error: The number being too large to fit in a precise decimal + --> src/pdec_err_overflow_4.rs:5:15 + | +5 | pdec!("57896044618658097711785492504343953926634.992332820282019728792003956564819968"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_too_many_decimal_places.rs b/radix-engine-tests/tests/dec_macros/src/pdec_err_too_many_decimal_places.rs new file mode 100644 index 00000000000..87d6b204206 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_too_many_decimal_places.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: PreciseDecimal = pdec!(1.0000000000000000000000000000000000001); +} diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_err_too_many_decimal_places.stderr b/radix-engine-tests/tests/dec_macros/src/pdec_err_too_many_decimal_places.stderr new file mode 100644 index 00000000000..362bf2947c2 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_err_too_many_decimal_places.stderr @@ -0,0 +1,5 @@ +error: A precise decimal cannot have more than thirty-six decimal places. + --> src/pdec_err_too_many_decimal_places.rs:4:37 + | +4 | const X: PreciseDecimal = pdec!(1.0000000000000000000000000000000000001); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/radix-engine-tests/tests/dec_macros/src/pdec_success.rs b/radix-engine-tests/tests/dec_macros/src/pdec_success.rs new file mode 100644 index 00000000000..30f0c01d1d8 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/pdec_success.rs @@ -0,0 +1,5 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!("1"); +} diff --git a/radix-engine-tests/tests/decimal.rs b/radix-engine-tests/tests/decimal.rs index 938c8b760a4..0c1373b0dbd 100644 --- a/radix-engine-tests/tests/decimal.rs +++ b/radix-engine-tests/tests/decimal.rs @@ -1,17 +1,50 @@ +mod package_loader; +use package_loader::PackageLoader; use radix_engine_common::math::*; use radix_engine_interface::{dec, pdec}; +use scrypto_unit::*; +use std::env; +use transaction::prelude::*; +use trybuild; #[test] -fn test_dec_macro() { +fn test_dec_macro_try_compile() { + // Change CARGO_MANIFEST_DIR to tests/dec_macros, where the dec_macros test crate is located. + // By default 'trybuild' crate uses current manifest dir, but it does not work with + // radix-engine-tests dir (presumably too complicated set of dependencies and features). + let manifest_dir = env::current_dir().unwrap().join("tests/dec_macros"); + env::set_var("CARGO_MANIFEST_DIR", &manifest_dir); + + // Also change the current dir to the 'dec_macros' dir. + // Otherwise 'trybuild' will not be able to find files to compile. + assert!(env::set_current_dir(manifest_dir).is_ok()); + + let t = trybuild::TestCases::new(); + + // Paths must be relative to the manifest_dir + t.pass("src/dec_success.rs"); + t.compile_fail("src/dec_err_*.rs"); + t.pass("src/pdec_success.rs"); + t.compile_fail("src/pdec_err_*.rs"); +} + +#[test] +fn test_dec_macro_valid() { let x1 = dec!("1.1"); assert_eq!(x1, Decimal::try_from("1.1").unwrap()); let x2 = dec!("3138550867693340381917894711603833208051.177722232017256447"); assert_eq!(x2, Decimal::MAX); + let x2 = dec!(3138550867693340381917894711603833208051.177722232017256447); + assert_eq!(x2, Decimal::MAX); + let x3 = dec!("-3138550867693340381917894711603833208051.177722232017256448"); assert_eq!(x3, Decimal::MIN); + let x3 = dec!(-3138550867693340381917894711603833208051.177722232017256448); + assert_eq!(x3, Decimal::MIN); + const X1: Decimal = dec!("111111.10"); assert_eq!(X1, Decimal::try_from("111111.10").unwrap()); @@ -56,7 +89,7 @@ fn test_dec_macro() { } #[test] -fn test_pdec_macro() { +fn test_pdec_macro_valid() { let x1 = pdec!("1.1"); assert_eq!(x1, PreciseDecimal::try_from("1.1").unwrap()); @@ -64,10 +97,16 @@ fn test_pdec_macro() { pdec!("57896044618658097711785492504343953926634.992332820282019728792003956564819967"); assert_eq!(x2, PreciseDecimal::MAX); + let x2 = pdec!(57896044618658097711785492504343953926634.992332820282019728792003956564819967); + assert_eq!(x2, PreciseDecimal::MAX); + let x3 = pdec!("-57896044618658097711785492504343953926634.992332820282019728792003956564819968"); assert_eq!(x3, PreciseDecimal::MIN); + let x3 = pdec!(-57896044618658097711785492504343953926634.992332820282019728792003956564819968); + assert_eq!(x3, PreciseDecimal::MIN); + const X1: PreciseDecimal = pdec!("111111.10"); assert_eq!(X1, PreciseDecimal::try_from("111111.10").unwrap()); @@ -109,3 +148,50 @@ fn test_pdec_macro() { pdec!("-57896044618658097711785492504343953926634.992332820282019728792003956564819968"); assert_eq!(X13, PreciseDecimal::MIN); } +#[test] +fn test_dec_macro_in_scrypto() { + // Arrange + let mut test_runner = TestRunnerBuilder::new().build(); + let package_address = test_runner.publish_package_simple(PackageLoader::get("decimal")); + + // Act + let manifest = ManifestBuilder::new() + .lock_fee_from_faucet() + .call_function( + package_address, + "DecimalTest", + "test_dec_macro", + manifest_args!(), + ) + .build(); + let receipt = test_runner.execute_manifest(manifest, vec![]); + + // Assert + let result = receipt.expect_commit_success(); + let output = result.outcome.expect_success(); + output[1].expect_return_value(&Decimal::from(6666)); +} + +#[test] +fn test_pdec_macro_in_scrypto() { + // Arrange + let mut test_runner = TestRunnerBuilder::new().build(); + let package_address = test_runner.publish_package_simple(PackageLoader::get("decimal")); + + // Act + let manifest = ManifestBuilder::new() + .lock_fee_from_faucet() + .call_function( + package_address, + "DecimalTest", + "test_pdec_macro", + manifest_args!(), + ) + .build(); + let receipt = test_runner.execute_manifest(manifest, vec![]); + + // Assert + let result = receipt.expect_commit_success(); + let output = result.outcome.expect_success(); + output[1].expect_return_value(&PreciseDecimal::from(6666)); +}