From 515dc598f62cd2cf785a90bb824d22b801f0ec68 Mon Sep 17 00:00:00 2001 From: Lukasz Rubaszewski Date: Thu, 21 Sep 2023 10:22:36 +0200 Subject: [PATCH 1/6] Test dec!()/pdec!() macros in scrypto --- .../tests/blueprints/Cargo.toml | 3 +- .../tests/blueprints/decimal/Cargo.toml | 15 ++++++ .../tests/blueprints/decimal/src/lib.rs | 35 +++++++++++++ radix-engine-tests/tests/decimal.rs | 52 +++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 radix-engine-tests/tests/blueprints/decimal/Cargo.toml create mode 100644 radix-engine-tests/tests/blueprints/decimal/src/lib.rs 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/decimal.rs b/radix-engine-tests/tests/decimal.rs index 938c8b760a4..018b42edeca 100644 --- a/radix-engine-tests/tests/decimal.rs +++ b/radix-engine-tests/tests/decimal.rs @@ -1,5 +1,57 @@ +mod package_loader; +use package_loader::PackageLoader; use radix_engine_common::math::*; use radix_engine_interface::{dec, pdec}; +use scrypto_unit::*; +use transaction::prelude::*; + +#[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)); +} #[test] fn test_dec_macro() { From 4a62ba798e978180892e038e9548f090682804f3 Mon Sep 17 00:00:00 2001 From: Lukasz Rubaszewski Date: Thu, 21 Sep 2023 14:22:50 +0200 Subject: [PATCH 2/6] Test failing compilation for dec!() and pdec!() --- radix-engine-tests/Cargo.toml | 1 + .../tests/dec_macros/Cargo.toml | 30 +++++++++++++++++++ .../src/dec_err_expr_not_supported.rs | 7 +++++ .../src/dec_err_expr_not_supported.stderr | 5 ++++ .../tests/dec_macros/src/dec_success.rs | 6 ++++ .../tests/dec_macros/src/lib.rs | 1 + radix-engine-tests/tests/decimal.rs | 21 +++++++++++++ 7 files changed, 71 insertions(+) create mode 100644 radix-engine-tests/tests/dec_macros/Cargo.toml create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_expr_not_supported.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_expr_not_supported.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_success.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/lib.rs diff --git a/radix-engine-tests/Cargo.toml b/radix-engine-tests/Cargo.toml index 06ab81d82aa..c290e6e887c 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/dec_macros/Cargo.toml b/radix-engine-tests/tests/dec_macros/Cargo.toml new file mode 100644 index 00000000000..4c92525c537 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/Cargo.toml @@ -0,0 +1,30 @@ +[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 them to allow quick verification with 'cargo build' command +[[bin]] +name = "dec_success" +path = "src/dec_success.rs" + +[[bin]] +name = "dec_invalid_expression" +path = "src/dec_invalid_expression.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_expr_not_supported.rs b/radix-engine-tests/tests/dec_macros/src/dec_err_expr_not_supported.rs new file mode 100644 index 00000000000..7b6f6f4a78a --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_err_expr_not_supported.rs @@ -0,0 +1,7 @@ +use radix_engine_interface::prelude::*; + +fn main() { + // Invalid expression + const X: Decimal = dec!(a); + let _ = format!("{:?}", X); +} 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..6b6a6b85f29 --- /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/fail.rs:4:29 + | +4 | 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..f21cd97129a --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/dec_success.rs @@ -0,0 +1,6 @@ +use radix_engine_interface::prelude::*; + +fn main() { + const X: Decimal = dec!("1"); + let _ = format!("{:?}", X); +} 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..242fc297951 --- /dev/null +++ b/radix-engine-tests/tests/dec_macros/src/lib.rs @@ -0,0 +1 @@ +fn stub() {} diff --git a/radix-engine-tests/tests/decimal.rs b/radix-engine-tests/tests/decimal.rs index 018b42edeca..25650391d1d 100644 --- a/radix-engine-tests/tests/decimal.rs +++ b/radix-engine-tests/tests/decimal.rs @@ -3,7 +3,28 @@ 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_try_build() { + println!("current_dir = {:?}", env::current_dir()); + // 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_invalid_expression.rs"); +} #[test] fn test_dec_macro_in_scrypto() { From ad8e4c0051566f0970283b92a17a8feea4e58a22 Mon Sep 17 00:00:00 2001 From: Lukasz Rubaszewski Date: Thu, 21 Sep 2023 17:21:59 +0200 Subject: [PATCH 3/6] Fix dec macros - disallow multiple negations - disallow negation before string literal - fix overflow when converting from MIN float literal --- radix-engine-macros/src/decimal.rs | 63 +++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 15 deletions(-) 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]; From f209c768ab6bb859d7f350593ec5ebed08d95fef Mon Sep 17 00:00:00 2001 From: Lukasz Rubaszewski Date: Thu, 21 Sep 2023 17:26:04 +0200 Subject: [PATCH 4/6] Add more tests for valid dec macro conversions --- radix-engine-tests/tests/decimal.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/radix-engine-tests/tests/decimal.rs b/radix-engine-tests/tests/decimal.rs index 25650391d1d..1957ff5d7f6 100644 --- a/radix-engine-tests/tests/decimal.rs +++ b/radix-engine-tests/tests/decimal.rs @@ -82,9 +82,15 @@ fn test_dec_macro() { 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()); @@ -137,10 +143,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()); From 1a3b2d8d9e218e9d9a674ce089fb01565c9ca427 Mon Sep 17 00:00:00 2001 From: Lukasz Rubaszewski Date: Thu, 21 Sep 2023 17:58:21 +0200 Subject: [PATCH 5/6] Add more failing compilation tests for dec!() --- .../tests/dec_macros/Cargo.toml | 7 +- .../src/dec_err_empty_fractional_part_1.rs | 5 + .../dec_err_empty_fractional_part_1.stderr | 5 + .../src/dec_err_empty_fractional_part_2.rs | 5 + .../dec_err_empty_fractional_part_2.stderr | 5 + .../src/dec_err_empty_integral_part.rs | 5 + .../src/dec_err_empty_integral_part.stderr | 5 + .../dec_macros/src/dec_err_expected_expr.rs | 5 + .../src/dec_err_expected_expr.stderr | 5 + .../src/dec_err_expr_not_supported.rs | 2 - .../src/dec_err_expr_not_supported.stderr | 2 +- .../dec_err_expr_unary_op_not_supported.rs | 5 + ...dec_err_expr_unary_op_not_supported.stderr | 5 + .../dec_macros/src/dec_err_float_suffix.rs | 5 + .../src/dec_err_float_suffix.stderr | 5 + .../dec_macros/src/dec_err_int_suffix.rs | 5 + .../dec_macros/src/dec_err_int_suffix.stderr | 5 + .../dec_macros/src/dec_err_invalid_digit.rs | 5 + .../src/dec_err_invalid_digit.stderr | 5 + .../src/dec_err_literal_not_supported.rs | 5 + .../src/dec_err_literal_not_supported.stderr | 5 + .../dec_err_more_than_one_decimal_point.rs | 5 + ...dec_err_more_than_one_decimal_point.stderr | 5 + .../src/dec_err_multiple_negations.rs | 5 + .../src/dec_err_multiple_negations.stderr | 5 + .../src/dec_err_negation_and_string.rs | 5 + .../src/dec_err_negation_and_string.stderr | 5 + .../dec_macros/src/dec_err_overflow_1.rs | 5 + .../dec_macros/src/dec_err_overflow_1.stderr | 5 + .../dec_macros/src/dec_err_overflow_2.rs | 5 + .../dec_macros/src/dec_err_overflow_2.stderr | 5 + .../dec_macros/src/dec_err_overflow_3.rs | 5 + .../dec_macros/src/dec_err_overflow_3.stderr | 5 + .../dec_macros/src/dec_err_overflow_4.rs | 5 + .../dec_macros/src/dec_err_overflow_4.stderr | 5 + .../src/dec_err_too_many_decimal_places.rs | 5 + .../dec_err_too_many_decimal_places.stderr | 5 + .../src/dec_invalid_expression.stderr | 5 + .../tests/dec_macros/src/dec_success.rs | 1 - .../tests/dec_macros/src/lib.rs | 1 + radix-engine-tests/tests/decimal.rs | 103 +++++++++--------- 41 files changed, 232 insertions(+), 59 deletions(-) create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_1.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_1.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_2.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_empty_fractional_part_2.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_empty_integral_part.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_empty_integral_part.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_expected_expr.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_expected_expr.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_expr_unary_op_not_supported.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_expr_unary_op_not_supported.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_float_suffix.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_float_suffix.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_int_suffix.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_int_suffix.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_invalid_digit.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_invalid_digit.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_literal_not_supported.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_literal_not_supported.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_more_than_one_decimal_point.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_more_than_one_decimal_point.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_multiple_negations.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_multiple_negations.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_negation_and_string.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_negation_and_string.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_overflow_1.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_overflow_1.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_overflow_2.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_overflow_2.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_overflow_3.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_overflow_3.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_overflow_4.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_overflow_4.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_too_many_decimal_places.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_err_too_many_decimal_places.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/dec_invalid_expression.stderr diff --git a/radix-engine-tests/tests/dec_macros/Cargo.toml b/radix-engine-tests/tests/dec_macros/Cargo.toml index 4c92525c537..b89aad8d7eb 100644 --- a/radix-engine-tests/tests/dec_macros/Cargo.toml +++ b/radix-engine-tests/tests/dec_macros/Cargo.toml @@ -14,14 +14,15 @@ radix-engine-interface = { path = "../../../radix-engine-interface", default-fea path = "src/lib.rs" # Below targets are not required to for the 'dec_macro' tests. -# Adding them to allow quick verification with 'cargo build' command +# 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_invalid_expression" -path = "src/dec_invalid_expression.rs" +name = "dec_err_expr_not_supported" +path = "src/dec_err_expr_not_supported.rs" [features] default = ["std"] 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 index 7b6f6f4a78a..bfb19ca0973 100644 --- 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 @@ -1,7 +1,5 @@ use radix_engine_interface::prelude::*; fn main() { - // Invalid expression const X: Decimal = dec!(a); - let _ = format!("{:?}", X); } 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 index 6b6a6b85f29..be161fc478a 100644 --- 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 @@ -1,5 +1,5 @@ error: This macro only supports string, integer and float literals. - --> src/fail.rs:4:29 + --> 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..23d9ede929b --- /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.0000000000000000000000000); +} 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..98ddd1aec0f --- /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.0000000000000000000000000); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 index f21cd97129a..30f0c01d1d8 100644 --- a/radix-engine-tests/tests/dec_macros/src/dec_success.rs +++ b/radix-engine-tests/tests/dec_macros/src/dec_success.rs @@ -2,5 +2,4 @@ use radix_engine_interface::prelude::*; fn main() { const X: Decimal = dec!("1"); - let _ = format!("{:?}", X); } diff --git a/radix-engine-tests/tests/dec_macros/src/lib.rs b/radix-engine-tests/tests/dec_macros/src/lib.rs index 242fc297951..aa8ff46d1f9 100644 --- a/radix-engine-tests/tests/dec_macros/src/lib.rs +++ b/radix-engine-tests/tests/dec_macros/src/lib.rs @@ -1 +1,2 @@ +#[allow(dead_code)] fn stub() {} diff --git a/radix-engine-tests/tests/decimal.rs b/radix-engine-tests/tests/decimal.rs index 1957ff5d7f6..dbe793ff5a6 100644 --- a/radix-engine-tests/tests/decimal.rs +++ b/radix-engine-tests/tests/decimal.rs @@ -9,10 +9,9 @@ use trybuild; #[test] fn test_dec_macro_try_build() { - println!("current_dir = {:?}", env::current_dir()); // 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) + // 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); @@ -21,61 +20,14 @@ fn test_dec_macro_try_build() { 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_invalid_expression.rs"); -} - -#[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)); + t.compile_fail("src/dec_err_*.rs"); } #[test] -fn test_dec_macro() { +fn test_dec_macro_valid() { let x1 = dec!("1.1"); assert_eq!(x1, Decimal::try_from("1.1").unwrap()); @@ -194,3 +146,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)); +} From 29880946550649d93a93dbf6dcd4ef7fc1f099d5 Mon Sep 17 00:00:00 2001 From: Lukasz Rubaszewski Date: Thu, 21 Sep 2023 18:10:58 +0200 Subject: [PATCH 6/6] Add more failing compilation tests for pdec!() --- .../tests/dec_macros/src/dec_err_too_many_decimal_places.rs | 2 +- .../dec_macros/src/dec_err_too_many_decimal_places.stderr | 4 ++-- .../dec_macros/src/pdec_err_empty_fractional_part_1.rs | 5 +++++ .../dec_macros/src/pdec_err_empty_fractional_part_1.stderr | 5 +++++ .../dec_macros/src/pdec_err_empty_fractional_part_2.rs | 5 +++++ .../dec_macros/src/pdec_err_empty_fractional_part_2.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_empty_integral_part.rs | 5 +++++ .../dec_macros/src/pdec_err_empty_integral_part.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_expected_expr.rs | 5 +++++ .../tests/dec_macros/src/pdec_err_expected_expr.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_expr_not_supported.rs | 5 +++++ .../tests/dec_macros/src/pdec_err_expr_not_supported.stderr | 5 +++++ .../dec_macros/src/pdec_err_expr_unary_op_not_supported.rs | 5 +++++ .../src/pdec_err_expr_unary_op_not_supported.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_float_suffix.rs | 5 +++++ .../tests/dec_macros/src/pdec_err_float_suffix.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_int_suffix.rs | 5 +++++ .../tests/dec_macros/src/pdec_err_int_suffix.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_invalid_digit.rs | 5 +++++ .../tests/dec_macros/src/pdec_err_invalid_digit.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_literal_not_supported.rs | 5 +++++ .../dec_macros/src/pdec_err_literal_not_supported.stderr | 5 +++++ .../dec_macros/src/pdec_err_more_than_one_decimal_point.rs | 5 +++++ .../src/pdec_err_more_than_one_decimal_point.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_multiple_negations.rs | 5 +++++ .../tests/dec_macros/src/pdec_err_multiple_negations.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_negation_and_string.rs | 5 +++++ .../dec_macros/src/pdec_err_negation_and_string.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_overflow_1.rs | 6 ++++++ .../tests/dec_macros/src/pdec_err_overflow_1.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_overflow_2.rs | 5 +++++ .../tests/dec_macros/src/pdec_err_overflow_2.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_overflow_3.rs | 6 ++++++ .../tests/dec_macros/src/pdec_err_overflow_3.stderr | 5 +++++ .../tests/dec_macros/src/pdec_err_overflow_4.rs | 6 ++++++ .../tests/dec_macros/src/pdec_err_overflow_4.stderr | 5 +++++ .../dec_macros/src/pdec_err_too_many_decimal_places.rs | 5 +++++ .../dec_macros/src/pdec_err_too_many_decimal_places.stderr | 5 +++++ radix-engine-tests/tests/dec_macros/src/pdec_success.rs | 5 +++++ radix-engine-tests/tests/decimal.rs | 6 ++++-- 40 files changed, 195 insertions(+), 5 deletions(-) create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_1.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_1.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_2.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_empty_fractional_part_2.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_empty_integral_part.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_empty_integral_part.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_expected_expr.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_expected_expr.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_expr_not_supported.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_expr_not_supported.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_expr_unary_op_not_supported.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_expr_unary_op_not_supported.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_float_suffix.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_float_suffix.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_int_suffix.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_int_suffix.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_invalid_digit.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_invalid_digit.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_literal_not_supported.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_literal_not_supported.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_more_than_one_decimal_point.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_more_than_one_decimal_point.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_multiple_negations.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_multiple_negations.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_negation_and_string.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_negation_and_string.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_1.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_1.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_2.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_2.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_3.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_3.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_4.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_overflow_4.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_too_many_decimal_places.rs create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_err_too_many_decimal_places.stderr create mode 100644 radix-engine-tests/tests/dec_macros/src/pdec_success.rs 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 index 23d9ede929b..2394e84dd86 100644 --- 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 @@ -1,5 +1,5 @@ use radix_engine_interface::prelude::*; fn main() { - const X: Decimal = dec!(1.0000000000000000000000000); + 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 index 98ddd1aec0f..d0f16c918dd 100644 --- 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 @@ -1,5 +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.0000000000000000000000000); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ +4 | const X: Decimal = dec!(1.0000000000000000001); + | ^^^^^^^^^^^^^^^^^^^^^ 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 dbe793ff5a6..0c1373b0dbd 100644 --- a/radix-engine-tests/tests/decimal.rs +++ b/radix-engine-tests/tests/decimal.rs @@ -8,7 +8,7 @@ use transaction::prelude::*; use trybuild; #[test] -fn test_dec_macro_try_build() { +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). @@ -24,6 +24,8 @@ fn test_dec_macro_try_build() { // 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] @@ -87,7 +89,7 @@ fn test_dec_macro_valid() { } #[test] -fn test_pdec_macro() { +fn test_pdec_macro_valid() { let x1 = pdec!("1.1"); assert_eq!(x1, PreciseDecimal::try_from("1.1").unwrap());