From f9ef414fa8aaf9f1e6a374d50f91a3a44afa9637 Mon Sep 17 00:00:00 2001 From: iqdecay Date: Mon, 11 Dec 2023 16:25:08 +0100 Subject: [PATCH] test: test `calculate_num_of_elements` (#1230) Closes #1229. --- packages/fuels-core/src/types/param_types.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/fuels-core/src/types/param_types.rs b/packages/fuels-core/src/types/param_types.rs index abbf8bb689..b6cbde50b3 100644 --- a/packages/fuels-core/src/types/param_types.rs +++ b/packages/fuels-core/src/types/param_types.rs @@ -1814,6 +1814,25 @@ mod tests { assert_eq!(param_type, ParamType::String); } + #[test] + fn calculate_num_of_elements() -> Result<()> { + let failing_param_type = ParamType::Array(Box::new(ParamType::U16), usize::MAX); + assert!(ParamType::calculate_num_of_elements(&failing_param_type, 0) + .unwrap_err() + .to_string() + .contains("Cannot calculate the number of elements")); + let zero_sized_type = ParamType::Array(Box::new(ParamType::StringArray(0)), 1000); + assert!(ParamType::calculate_num_of_elements(&zero_sized_type, 0) + .unwrap_err() + .to_string() + .contains("the type is zero-sized")); + assert!(ParamType::calculate_num_of_elements(&ParamType::U16, 9) + .unwrap_err() + .to_string() + .contains("1 extra bytes detected while decoding heap type")); + Ok(()) + } + fn given_type_with_path(path: &str) -> Type { Type { type_field: format!("struct {path}"),