From f98e41beeb2a97906ca40385891eae8f71cb35d4 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Wed, 10 Jul 2024 13:43:58 +0100 Subject: [PATCH 01/22] __slice to create slices from arrays --- sway-ast/src/intrinsics.rs | 3 + sway-core/src/ir_generation/const_eval.rs | 1 + sway-core/src/ir_generation/function.rs | 54 +++++++++++++ .../ast_node/declaration/declaration.rs | 1 + .../ast_node/expression/intrinsic_function.rs | 81 ++++++++++++++++++- .../semantic_analysis/cei_pattern_analysis.rs | 1 + sway-core/src/type_system/info.rs | 2 +- sway-core/src/type_system/unify/unifier.rs | 7 +- .../src/type_system/unify/unify_check.rs | 8 +- sway-ir/src/irtype.rs | 8 ++ sway-ir/src/optimize/sroa.rs | 1 + sway-parse/src/ty/mod.rs | 7 ++ .../language/slice/slice_intrinsics/Forc.lock | 8 ++ .../language/slice/slice_intrinsics/Forc.toml | 8 ++ .../slice_intrinsics/json_abi_oracle.json | 25 ++++++ .../json_abi_oracle_new_encoding.json | 32 ++++++++ .../slice/slice_intrinsics/src/main.sw | 6 ++ .../language/slice/slice_intrinsics/test.toml | 10 +++ 18 files changed, 258 insertions(+), 5 deletions(-) create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle.json create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/test.toml diff --git a/sway-ast/src/intrinsics.rs b/sway-ast/src/intrinsics.rs index 5200da142ae..221f9a2bc61 100644 --- a/sway-ast/src/intrinsics.rs +++ b/sway-ast/src/intrinsics.rs @@ -41,6 +41,7 @@ pub enum Intrinsic { EncodeBufferEmpty, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_empty() EncodeBufferAppend, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_append(buffer, primitive data type) EncodeBufferAsRawSlice, // let slice: raw_slice = __encode_buffer_as_raw_slice(buffer) + Slice, } impl fmt::Display for Intrinsic { @@ -85,6 +86,7 @@ impl fmt::Display for Intrinsic { Intrinsic::EncodeBufferEmpty => "encode_buffer_empty", Intrinsic::EncodeBufferAppend => "encode_buffer_append", Intrinsic::EncodeBufferAsRawSlice => "encode_buffer_as_raw_slice", + Intrinsic::Slice => "slice", }; write!(f, "{s}") } @@ -133,6 +135,7 @@ impl Intrinsic { "__encode_buffer_empty" => EncodeBufferEmpty, "__encode_buffer_append" => EncodeBufferAppend, "__encode_buffer_as_raw_slice" => EncodeBufferAsRawSlice, + "__slice" => Slice, _ => return None, }) } diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index 157d103589e..d6b3d0b89c6 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -1284,6 +1284,7 @@ fn const_eval_intrinsic( value: ConstantValue::RawUntypedSlice(bytes[0..(len as usize)].to_vec()), })) } + Intrinsic::Slice => todo!(), } } diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index 184a4d88c05..6487c3aa8c8 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -2168,6 +2168,60 @@ impl<'eng> FnCompiler<'eng> { Ok(TerminatorValue::new(buffer, context)) } + Intrinsic::Slice => { + assert!(arguments.len() == 3); + + let array = &arguments[0]; + let item_ir_type = convert_resolved_typeid( + self.engines.te(), + self.engines.de(), + context, + &array.return_type, + &array.span.clone(), + )?; + let array = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, array)? + ); + + let start = &arguments[0]; + let start = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, start)? + ); + + let end = &arguments[0]; + let end = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, end)? + ); + + //asm(array_ptr: array, start: start, end: end, item_len: __size_of::(), slice_ptr, slice_len) { + // lw array_ptr array_ptr i0; + // mul slice_ptr start item_len; + // add slice_ptr slice_ptr array_ptr; // byte offset + // sub slice_len end start; + // mul slice_len slice_len item_len; // length in bytes + // (slice_ptr, slice_len): __slice[T] + //}; + let return_type = Type::get_typed_slice(context, item_ir_type); + let slice = self.current_block.append(context).asm_block( + vec![AsmArg { + name: Ident::new_no_span("array_ptr".into()), + initializer: Some(array), + }], + vec![AsmInstruction { + op_name: Ident::new_no_span("lw".into()), + args: vec![ + Ident::new_no_span("array_ptr".into()), + Ident::new_no_span("array_ptr".into()), + ], + immediate: Some(Ident::new_no_span("i0".into())), + metadata: None, + }], + return_type, + Some(Ident::new_no_span("array_ptr".into())), + ); + + Ok(TerminatorValue::new(slice, context)) + } } } diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs index 1c4b1d977b6..6e9f2aafb1d 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -120,6 +120,7 @@ impl TyDecl { "Variable declaration's type annotation does not match up \ with the assigned expression's type.", ); + let result = ty::TyExpression::type_check(handler, ctx.by_ref(), &var_decl.body); let body = result.unwrap_or_else(|err| { ty::TyExpression::error(err, var_decl.name.span(), engines) diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 9c324cf1289..c60cbc04d0e 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -10,7 +10,8 @@ use crate::{ engine_threading::*, language::{ parsed::{Expression, ExpressionKind}, - ty, Literal, + ty::{self, TyIntrinsicFunctionKind}, + Literal, }, semantic_analysis::{type_check_context::EnforceTypeArguments, TypeCheckContext}, type_system::*, @@ -101,6 +102,84 @@ impl ty::TyIntrinsicFunctionKind { Intrinsic::EncodeBufferAsRawSlice => { type_check_encode_as_raw_slice(handler, ctx, kind, arguments, type_arguments, span) } + Intrinsic::Slice => { + if arguments.len() != 3 { + return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs { + name: kind.to_string(), + expected: 3, + span, + })); + } + + let type_engine = ctx.engines.te(); + let engines = ctx.engines(); + + let mut ctx = ctx; + + // check first argument + let array_span = arguments[0].span.clone(); + let array_type = type_engine.insert(engines, TypeInfo::Unknown, None); + let array_ty_expr = { + let ctx = ctx + .by_ref() + .with_help_text("") + .with_type_annotation(array_type); + ty::TyExpression::type_check(handler, ctx, &arguments[0])? + }; + let TypeInfo::Array(elem_type_arg, len) = &*type_engine.get(array_type) else { + return Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { + name: kind.to_string(), + span: array_span, + hint: "".to_string(), + })); + }; + + // start argument + // check first argument + let start_type = type_engine.insert( + engines, + TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), + None, + ); + let start_ty_expr = { + let ctx = ctx + .by_ref() + .with_help_text("") + .with_type_annotation(start_type); + ty::TyExpression::type_check(handler, ctx, &arguments[1])? + }; + + // end argument + // check first argument + let end_type = type_engine.insert( + engines, + TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), + None, + ); + let end_ty_expr = { + let ctx = ctx + .by_ref() + .with_help_text("") + .with_type_annotation(end_type); + ty::TyExpression::type_check(handler, ctx, &arguments[2])? + }; + + //TODO check start is literal inside of range + //TODO check end is literal inside of range + + let slice_type = + type_engine.insert(engines, TypeInfo::Slice(elem_type_arg.clone()), None); + + Ok(( + TyIntrinsicFunctionKind { + kind, + arguments: vec![array_ty_expr, start_ty_expr, end_ty_expr], + type_arguments: vec![], + span, + }, + slice_type, + )) + } } } } diff --git a/sway-core/src/semantic_analysis/cei_pattern_analysis.rs b/sway-core/src/semantic_analysis/cei_pattern_analysis.rs index 808d114c23a..bf33bffa169 100644 --- a/sway-core/src/semantic_analysis/cei_pattern_analysis.rs +++ b/sway-core/src/semantic_analysis/cei_pattern_analysis.rs @@ -648,6 +648,7 @@ fn effects_of_intrinsic(intr: &sway_ast::Intrinsic) -> HashSet { | EncodeBufferEmpty | EncodeBufferAppend | EncodeBufferAsRawSlice => HashSet::new(), + Slice => todo!(), } } diff --git a/sway-core/src/type_system/info.rs b/sway-core/src/type_system/info.rs index 793be444b13..7ba5a84b547 100644 --- a/sway-core/src/type_system/info.rs +++ b/sway-core/src/type_system/info.rs @@ -1413,7 +1413,6 @@ impl TypeInfo { | TypeInfo::RawUntypedPtr | TypeInfo::RawUntypedSlice | TypeInfo::Ptr(_) - | TypeInfo::Slice(_) | TypeInfo::ErrorRecovery(_) | TypeInfo::TraitType { .. } | TypeInfo::Never => false, @@ -1423,6 +1422,7 @@ impl TypeInfo { | TypeInfo::Custom { .. } | TypeInfo::Tuple(_) | TypeInfo::Array(_, _) + | TypeInfo::Slice(_) | TypeInfo::Contract | TypeInfo::Storage { .. } | TypeInfo::Numeric diff --git a/sway-core/src/type_system/unify/unifier.rs b/sway-core/src/type_system/unify/unifier.rs index 0efc7883153..e94cb3b7384 100644 --- a/sway-core/src/type_system/unify/unifier.rs +++ b/sway-core/src/type_system/unify/unifier.rs @@ -93,8 +93,8 @@ impl<'a> Unifier<'a> { pub(crate) fn unify(&self, handler: &Handler, received: TypeId, expected: TypeId, span: &Span) { use TypeInfo::{ Alias, Array, Boolean, Contract, Enum, Never, Numeric, Placeholder, RawUntypedPtr, - RawUntypedSlice, Ref, StringArray, StringSlice, Struct, Tuple, Unknown, UnknownGeneric, - UnsignedInteger, B256, + RawUntypedSlice, Ref, Slice, StringArray, StringSlice, Struct, Tuple, Unknown, + UnknownGeneric, UnsignedInteger, B256, }; if received == expected { @@ -123,6 +123,9 @@ impl<'a> Unifier<'a> { (Array(re, rc), Array(ee, ec)) if rc.val() == ec.val() => { self.unify_type_arguments_in_parents(handler, received, expected, span, re, ee); } + (Slice(re), Slice(ee)) => { + self.unify_type_arguments_in_parents(handler, received, expected, span, re, ee); + } (Struct(r_decl_ref), Struct(e_decl_ref)) => { let r_decl = self.engines.de().get_struct(r_decl_ref); let e_decl = self.engines.de().get_struct(e_decl_ref); diff --git a/sway-core/src/type_system/unify/unify_check.rs b/sway-core/src/type_system/unify/unify_check.rs index 24f73a077c1..26a0777bdee 100644 --- a/sway-core/src/type_system/unify/unify_check.rs +++ b/sway-core/src/type_system/unify/unify_check.rs @@ -224,7 +224,8 @@ impl<'a> UnifyCheck<'a> { fn check_inner(&self, left: TypeId, right: TypeId) -> bool { use TypeInfo::{ Alias, Array, ContractCaller, Custom, Enum, ErrorRecovery, Never, Numeric, Placeholder, - Ref, StringArray, StringSlice, Struct, Tuple, Unknown, UnknownGeneric, UnsignedInteger, + Ref, Slice, StringArray, StringSlice, Struct, Tuple, Unknown, UnknownGeneric, + UnsignedInteger, }; use UnifyCheckMode::{ Coercion, ConstraintSubset, NonDynamicEquality, NonGenericConstraintSubset, @@ -242,10 +243,15 @@ impl<'a> UnifyCheck<'a> { (Never, Never) => { return true; } + (Array(l0, l1), Array(r0, r1)) => { return self.check_inner(l0.type_id, r0.type_id) && l1.val() == r1.val(); } + (Slice(l0), Slice(r0)) => { + return self.check_inner(l0.type_id, r0.type_id); + } + (Tuple(l_types), Tuple(r_types)) => { let l_types = l_types.iter().map(|x| x.type_id).collect::>(); let r_types = r_types.iter().map(|x| x.type_id).collect::>(); diff --git a/sway-ir/src/irtype.rs b/sway-ir/src/irtype.rs index 3bca2ace003..722031364ab 100644 --- a/sway-ir/src/irtype.rs +++ b/sway-ir/src/irtype.rs @@ -39,6 +39,7 @@ pub enum TypeContent { Struct(Vec), Slice, Pointer(Type), + TypedSlice(Type), } impl Type { @@ -162,6 +163,11 @@ impl Type { Self::get_type(context, &TypeContent::Slice).expect("create_basic_types not called") } + /// Get typed slice type + pub fn get_typed_slice(context: &mut Context, item_ty: Type) -> Type { + Self::get_or_create_unique_type(context, TypeContent::TypedSlice(item_ty)) + } + /// Return a string representation of type, used for printing. pub fn as_string(&self, context: &Context) -> String { let sep_types_str = |agg_content: &Vec, sep: &str| { @@ -190,6 +196,7 @@ impl Type { format!("{{ {} }}", sep_types_str(agg, ", ")) } TypeContent::Slice => "slice".into(), + TypeContent::TypedSlice(ty) => format!("[{}]", ty.as_string(context)), TypeContent::Pointer(ty) => format!("ptr {}", ty.as_string(context)), } } @@ -542,6 +549,7 @@ impl Type { TypeContent::Uint(256) => TypeSize::new(32), TypeContent::Uint(_) => unreachable!(), TypeContent::Slice => TypeSize::new(16), + TypeContent::TypedSlice(..) => TypeSize::new(16), TypeContent::B256 => TypeSize::new(32), TypeContent::StringSlice => TypeSize::new(16), TypeContent::StringArray(n) => { diff --git a/sway-ir/src/optimize/sroa.rs b/sway-ir/src/optimize/sroa.rs index 31550709dfb..70c8160362c 100644 --- a/sway-ir/src/optimize/sroa.rs +++ b/sway-ir/src/optimize/sroa.rs @@ -404,6 +404,7 @@ fn is_processable_aggregate(context: &Context, ty: Type) -> bool { fields.iter().all(|ty| check_sub_types(context, *ty)) } crate::TypeContent::Slice => false, + crate::TypeContent::TypedSlice(..) => false, crate::TypeContent::Pointer(_) => true, crate::TypeContent::StringSlice => false, crate::TypeContent::StringArray(_) => false, diff --git a/sway-parse/src/ty/mod.rs b/sway-parse/src/ty/mod.rs index dcba9b74ef1..7e805b99977 100644 --- a/sway-parse/src/ty/mod.rs +++ b/sway-parse/src/ty/mod.rs @@ -30,6 +30,13 @@ impl Parse for Ty { return Err(parser .emit_error(ParseErrorKind::ExpectedCommaOrCloseParenInTupleOrParenExpression)); } + + // square brackets can be slice ([u64]) or arrays ([u64; 1]) + + // if let Some(descriptor) = SquareBrackets::try_parse(parser)? { + // return Ok(Ty::Slice(descriptor)); + // }; + if let Some(descriptor) = SquareBrackets::try_parse(parser)? { return Ok(Ty::Array(descriptor)); }; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock new file mode 100644 index 00000000000..c5bc5415952 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock @@ -0,0 +1,8 @@ +[[package]] +name = "core" +source = "path+from-root-53D6DC514F06A250" + +[[package]] +name = "slice_intrinsics" +source = "member" +dependencies = ["core"] diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml new file mode 100644 index 00000000000..a7ed10b10d3 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "slice_intrinsics" + +[dependencies] +core = { path = "../../../../../../../../sway-lib-core" } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle.json new file mode 100644 index 00000000000..ad50b55d54c --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle.json @@ -0,0 +1,25 @@ +{ + "configurables": [], + "functions": [ + { + "attributes": null, + "inputs": [], + "name": "main", + "output": { + "name": "", + "type": 0, + "typeArguments": null + } + } + ], + "loggedTypes": [], + "messagesTypes": [], + "types": [ + { + "components": null, + "type": "u64", + "typeId": 0, + "typeParameters": null + } + ] +} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json new file mode 100644 index 00000000000..5d2bf854d27 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json @@ -0,0 +1,32 @@ +{ + "configurables": [], + "encoding": "1", + "functions": [ + { + "attributes": null, + "inputs": [ + { + "name": "a", + "type": 0, + "typeArguments": null + } + ], + "name": "main", + "output": { + "name": "", + "type": 0, + "typeArguments": null + } + } + ], + "loggedTypes": [], + "messagesTypes": [], + "types": [ + { + "components": null, + "type": "raw untyped slice", + "typeId": 0, + "typeParameters": null + } + ] +} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw new file mode 100644 index 00000000000..6da18e1cb16 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -0,0 +1,6 @@ +script; + +fn main() { + let array: [u64; 4] = [1, 2, 3, 4]; + let slice: __slice[u64] = __slice(array, 0, 4); +} diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/test.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/test.toml new file mode 100644 index 00000000000..d238049781f --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/test.toml @@ -0,0 +1,10 @@ +category = "fail" + +# check: $()fn main(a: raw_slice) -> raw_slice +# nextln: $()Returning a type containing `raw_slice` from `main()` is not allowed. Consider converting it into a flat `raw_slice` first + +category_new_encoding = "run" +script_data_new_encoding = "0000000000000001 65" +expected_result_new_encoding = { action = "return_data", value = "0000000000000001 65" } + +validate_abi = true From 673c7671cd68f7b096dbe1b17ccc66812d591da5 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Thu, 11 Jul 2024 13:55:16 +0100 Subject: [PATCH 02/22] __slice intrinsics create a typed slice --- .../asm_generation/fuel/fuel_asm_builder.rs | 6 +- sway-core/src/ir_generation/convert.rs | 11 +- sway-core/src/ir_generation/function.rs | 145 ++++++++++++------ .../ast_node/expression/intrinsic_function.rs | 2 - sway-ir/src/asm.rs | 59 ++++++- sway-ir/src/irtype.rs | 5 +- .../json_abi_oracle_new_encoding.json | 12 +- .../language/slice/slice_intrinsics/test.toml | 9 +- 8 files changed, 183 insertions(+), 66 deletions(-) diff --git a/sway-core/src/asm_generation/fuel/fuel_asm_builder.rs b/sway-core/src/asm_generation/fuel/fuel_asm_builder.rs index fec3b2b83b3..08bb9c7b4f3 100644 --- a/sway-core/src/asm_generation/fuel/fuel_asm_builder.rs +++ b/sway-core/src/asm_generation/fuel/fuel_asm_builder.rs @@ -593,7 +593,7 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> { let op_span = self .md_mgr .md_to_span(self.context, op.metadata) - .unwrap_or_else(Span::dummy); + .unwrap_or_else(|| op.op_name.span()); let opcode = Op::parse_opcode( handler, &op.op_name, @@ -1930,6 +1930,10 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> { .get_type(self.context) .map_or(true, |ty| !self.is_copy_type(&ty)) { + dbg!(stored_val + .get_type(self.context) + .unwrap() + .as_string(self.context)); Err(CompileError::Internal( "Attempt to store a non-copy type.", owning_span.unwrap_or_else(Span::dummy), diff --git a/sway-core/src/ir_generation/convert.rs b/sway-core/src/ir_generation/convert.rs index ddacdeab37a..6a840dbe90f 100644 --- a/sway-core/src/ir_generation/convert.rs +++ b/sway-core/src/ir_generation/convert.rs @@ -154,7 +154,16 @@ fn convert_resolved_type( TypeInfo::RawUntypedPtr => Type::get_uint64(context), TypeInfo::RawUntypedSlice => Type::get_slice(context), TypeInfo::Ptr(_) => Type::get_uint64(context), - TypeInfo::Slice(_) => Type::get_slice(context), + TypeInfo::Slice(elem_type) => { + let elem_type = convert_resolved_typeid( + type_engine, + decl_engine, + context, + &elem_type.type_id, + span, + )?; + Type::get_typed_slice(context, elem_type) + } TypeInfo::Alias { ty, .. } => { convert_resolved_typeid(type_engine, decl_engine, context, &ty.type_id, span)? } diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index 6487c3aa8c8..d33946674c0 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -104,6 +104,30 @@ pub(crate) struct FnCompiler<'eng> { messages_types_map: HashMap, } +fn to_constant(_s: &mut FnCompiler<'_>, context: &mut Context, value: u64) -> Value { + let needed_size = Constant::new_uint(context, 64, value); + Value::new_constant(context, needed_size) +} + +fn save_to_local_return_ptr( + s: &mut FnCompiler<'_>, + context: &mut Context, + value: Value, +) -> Result { + let temp_arg_name = s.lexical_map.insert_anon(); + + let value_type = value.get_type(context).unwrap(); + let local_var = s + .function + .new_local_var(context, temp_arg_name, value_type, None, false) + .map_err(|ir_error| CompileError::InternalOwned(ir_error.to_string(), Span::dummy()))?; + + let local_var_ptr = s.current_block.append(context).get_local(local_var); + let _ = s.current_block.append(context).store(local_var_ptr, value); + + Ok(local_var_ptr) +} + impl<'eng> FnCompiler<'eng> { #[allow(clippy::too_many_arguments)] pub(super) fn new( @@ -1661,27 +1685,6 @@ impl<'eng> FnCompiler<'eng> { .binary_op(BinaryOpKind::Add, len, step) } - fn save_to_local_return_ptr( - s: &mut FnCompiler<'_>, - context: &mut Context, - value: Value, - ) -> Result { - let temp_arg_name = s.lexical_map.insert_anon(); - - let value_type = value.get_type(context).unwrap(); - let local_var = s - .function - .new_local_var(context, temp_arg_name, value_type, None, false) - .map_err(|ir_error| { - CompileError::InternalOwned(ir_error.to_string(), Span::dummy()) - })?; - - let local_var_ptr = s.current_block.append(context).get_local(local_var); - let _ = s.current_block.append(context).store(local_var_ptr, value); - - Ok(local_var_ptr) - } - fn append_with_memcpy( s: &mut FnCompiler<'_>, context: &mut Context, @@ -1847,15 +1850,6 @@ impl<'eng> FnCompiler<'eng> { (merge_block_ptr, merge_block_cap) } - fn to_constant( - _s: &mut FnCompiler<'_>, - context: &mut Context, - needed_size: u64, - ) -> Value { - let needed_size = Constant::new_uint(context, 64, needed_size); - Value::new_constant(context, needed_size) - } - // Grow the buffer if needed let (ptr, cap) = match &*item_type { TypeInfo::Boolean => { @@ -2171,24 +2165,34 @@ impl<'eng> FnCompiler<'eng> { Intrinsic::Slice => { assert!(arguments.len() == 3); + let u8_type = Type::get_uint8(context); + let uint64 = Type::get_uint64(context); + let ptr_u8 = Type::new_ptr(context, u8_type); + let array = &arguments[0]; let item_ir_type = convert_resolved_typeid( self.engines.te(), self.engines.de(), context, - &array.return_type, + &match &*self.engines.te().get(array.return_type) { + TypeInfo::Array(t, _) => t.type_id, + _ => unreachable!(), + }, &array.span.clone(), )?; + let item_ir_type_size = item_ir_type.size(context); let array = return_on_termination_or_extract!( self.compile_expression_to_value(context, md_mgr, array)? ); + let array = save_to_local_return_ptr(self, context, array)?; + let array = self.current_block.append(context).ptr_to_int(array, uint64); - let start = &arguments[0]; + let start = &arguments[1]; let start = return_on_termination_or_extract!( self.compile_expression_to_value(context, md_mgr, start)? ); - let end = &arguments[0]; + let end = &arguments[2]; let end = return_on_termination_or_extract!( self.compile_expression_to_value(context, md_mgr, end)? ); @@ -2201,23 +2205,74 @@ impl<'eng> FnCompiler<'eng> { // mul slice_len slice_len item_len; // length in bytes // (slice_ptr, slice_len): __slice[T] //}; + + let array_ptr_arg = AsmArg { + name: Ident::new_no_span("array_ptr".into()), + initializer: Some(array), + }; + let start_arg = AsmArg { + name: Ident::new_no_span("start".into()), + initializer: Some(start), + }; + let end_arg = AsmArg { + name: Ident::new_no_span("end".into()), + initializer: Some(end), + }; + let item_len_arg = AsmArg { + name: Ident::new_no_span("item_len".into()), + initializer: Some(to_constant(self, context, item_ir_type_size.in_bytes())), + }; + + let slice_ptr_out_arg = AsmArg { + name: Ident::new_no_span("slice_ptr".into()), + initializer: None, + }; + let slice_len_out_arg = AsmArg { + name: Ident::new_no_span("slice_len".into()), + initializer: None, + }; + + let slice_ptr = self.current_block.append(context).asm_block( + vec![ + array_ptr_arg, + start_arg.clone(), + item_len_arg.clone(), + slice_ptr_out_arg, + ], + vec![ + AsmInstruction::mul_no_span("slice_ptr", "start", "item_len"), + AsmInstruction::add_no_span("slice_ptr", "slice_ptr", "array_ptr"), + ], + uint64, + Some(Ident::new_no_span("slice_ptr".into())), + ); + + let slice_len = self.current_block.append(context).asm_block( + vec![start_arg, end_arg, item_len_arg, slice_len_out_arg], + vec![ + AsmInstruction::sub_no_span("slice_len", "end", "start"), + AsmInstruction::mul_no_span("slice_len", "slice_len", "item_len"), + ], + uint64, + Some(Ident::new_no_span("slice_len".into())), + ); + + // compile the slice together let return_type = Type::get_typed_slice(context, item_ir_type); + let slice_as_tuple = self.compile_tuple_from_values( + context, + vec![slice_ptr, slice_len], + vec![uint64, uint64], + None, + )?; let slice = self.current_block.append(context).asm_block( vec![AsmArg { - name: Ident::new_no_span("array_ptr".into()), - initializer: Some(array), - }], - vec![AsmInstruction { - op_name: Ident::new_no_span("lw".into()), - args: vec![ - Ident::new_no_span("array_ptr".into()), - Ident::new_no_span("array_ptr".into()), - ], - immediate: Some(Ident::new_no_span("i0".into())), - metadata: None, + name: Ident::new_no_span("s".into()), + initializer: Some(slice_as_tuple), }], + vec![], return_type, - Some(Ident::new_no_span("array_ptr".into())), + Some(Ident::new_no_span("s".into())), ); Ok(TerminatorValue::new(slice, context)) diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index c60cbc04d0e..e61cc14d82a 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -135,7 +135,6 @@ impl ty::TyIntrinsicFunctionKind { }; // start argument - // check first argument let start_type = type_engine.insert( engines, TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), @@ -150,7 +149,6 @@ impl ty::TyIntrinsicFunctionKind { }; // end argument - // check first argument let end_type = type_engine.insert( engines, TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), diff --git a/sway-ir/src/asm.rs b/sway-ir/src/asm.rs index 8a74df16ecf..34d02ab9c17 100644 --- a/sway-ir/src/asm.rs +++ b/sway-ir/src/asm.rs @@ -14,7 +14,7 @@ //! } //! ``` -use sway_types::ident::Ident; +use sway_types::{ident::Ident, BaseIdent}; use crate::{ context::Context, irtype::Type, metadata::MetadataIndex, pretty::DebugWithContext, value::Value, @@ -43,6 +43,63 @@ pub struct AsmInstruction { pub metadata: Option, } +impl AsmInstruction { + pub fn lw_no_span( + dst: impl Into, + src: impl Into, + offset: impl Into, + ) -> Self { + AsmInstruction { + op_name: Ident::new(sway_types::Span::from_string("lw".into())), + args: vec![ + Ident::new_no_span(dst.into()), + Ident::new_no_span(src.into()), + ], + immediate: Some(Ident::new_no_span(offset.into())), + metadata: None, + } + } + + pub fn mul_no_span(dst: impl Into, a: impl Into, b: impl Into) -> Self { + AsmInstruction { + op_name: Ident::new(sway_types::Span::from_string("mul".into())), + args: vec![ + Ident::new_no_span(dst.into()), + Ident::new_no_span(a.into()), + Ident::new_no_span(b.into()), + ], + immediate: None, + metadata: None, + } + } + + pub fn add_no_span(dst: impl Into, a: impl Into, b: impl Into) -> Self { + AsmInstruction { + op_name: Ident::new(sway_types::Span::from_string("add".into())), + args: vec![ + Ident::new_no_span(dst.into()), + Ident::new_no_span(a.into()), + Ident::new_no_span(b.into()), + ], + immediate: None, + metadata: None, + } + } + + pub fn sub_no_span(dst: impl Into, a: impl Into, b: impl Into) -> Self { + AsmInstruction { + op_name: Ident::new(sway_types::Span::from_string("sub".into())), + args: vec![ + Ident::new_no_span(dst.into()), + Ident::new_no_span(a.into()), + Ident::new_no_span(b.into()), + ], + immediate: None, + metadata: None, + } + } +} + impl AsmBlock { /// Create a new [`AsmBlock`] in the passed context and return its handle. pub fn new( diff --git a/sway-ir/src/irtype.rs b/sway-ir/src/irtype.rs index 722031364ab..af2a3cdf1cc 100644 --- a/sway-ir/src/irtype.rs +++ b/sway-ir/src/irtype.rs @@ -196,7 +196,7 @@ impl Type { format!("{{ {} }}", sep_types_str(agg, ", ")) } TypeContent::Slice => "slice".into(), - TypeContent::TypedSlice(ty) => format!("[{}]", ty.as_string(context)), + TypeContent::TypedSlice(ty) => format!("__slice[{}]", ty.as_string(context)), TypeContent::Pointer(ty) => format!("ptr {}", ty.as_string(context)), } } @@ -216,6 +216,9 @@ impl Type { (TypeContent::Array(l, llen), TypeContent::Array(r, rlen)) => { llen == rlen && l.eq(context, r) } + + (TypeContent::TypedSlice(l), TypeContent::TypedSlice(r)) => l.eq(context, r), + (TypeContent::Struct(l), TypeContent::Struct(r)) | (TypeContent::Union(l), TypeContent::Union(r)) => { l.len() == r.len() && l.iter().zip(r.iter()).all(|(l, r)| l.eq(context, r)) diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json index 5d2bf854d27..e60dda965d4 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json @@ -4,13 +4,7 @@ "functions": [ { "attributes": null, - "inputs": [ - { - "name": "a", - "type": 0, - "typeArguments": null - } - ], + "inputs": [], "name": "main", "output": { "name": "", @@ -23,8 +17,8 @@ "messagesTypes": [], "types": [ { - "components": null, - "type": "raw untyped slice", + "components": [], + "type": "()", "typeId": 0, "typeParameters": null } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/test.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/test.toml index d238049781f..dae3eb53b08 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/test.toml +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/test.toml @@ -1,10 +1,7 @@ -category = "fail" - -# check: $()fn main(a: raw_slice) -> raw_slice -# nextln: $()Returning a type containing `raw_slice` from `main()` is not allowed. Consider converting it into a flat `raw_slice` first +category = "run" category_new_encoding = "run" -script_data_new_encoding = "0000000000000001 65" -expected_result_new_encoding = { action = "return_data", value = "0000000000000001 65" } +script_data_new_encoding = "" +expected_result_new_encoding = { action = "return_data", value = "" } validate_abi = true From e4d4f43e340f835162d3144d9fe1e31cc11588b2 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Thu, 11 Jul 2024 15:07:10 +0100 Subject: [PATCH 03/22] __slice_elem intrinsics returning elem as value --- sway-ast/src/intrinsics.rs | 3 + sway-core/src/ir_generation/const_eval.rs | 1 + sway-core/src/ir_generation/function.rs | 116 +++++++++++++++--- .../ast_node/expression/intrinsic_function.rs | 58 +++++++++ .../semantic_analysis/cei_pattern_analysis.rs | 1 + .../slice/slice_intrinsics/src/main.sw | 11 ++ 6 files changed, 170 insertions(+), 20 deletions(-) diff --git a/sway-ast/src/intrinsics.rs b/sway-ast/src/intrinsics.rs index 221f9a2bc61..0f125e835e6 100644 --- a/sway-ast/src/intrinsics.rs +++ b/sway-ast/src/intrinsics.rs @@ -42,6 +42,7 @@ pub enum Intrinsic { EncodeBufferAppend, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_append(buffer, primitive data type) EncodeBufferAsRawSlice, // let slice: raw_slice = __encode_buffer_as_raw_slice(buffer) Slice, + SliceElem, } impl fmt::Display for Intrinsic { @@ -87,6 +88,7 @@ impl fmt::Display for Intrinsic { Intrinsic::EncodeBufferAppend => "encode_buffer_append", Intrinsic::EncodeBufferAsRawSlice => "encode_buffer_as_raw_slice", Intrinsic::Slice => "slice", + Intrinsic::SliceElem => "slice_elem", }; write!(f, "{s}") } @@ -136,6 +138,7 @@ impl Intrinsic { "__encode_buffer_append" => EncodeBufferAppend, "__encode_buffer_as_raw_slice" => EncodeBufferAsRawSlice, "__slice" => Slice, + "__slice_elem" => SliceElem, _ => return None, }) } diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index d6b3d0b89c6..878d995690e 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -1285,6 +1285,7 @@ fn const_eval_intrinsic( })) } Intrinsic::Slice => todo!(), + Intrinsic::SliceElem => todo!(), } } diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index d33946674c0..d0a4ebcf840 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -128,6 +128,26 @@ fn save_to_local_return_ptr( Ok(local_var_ptr) } +fn calc_addr_as_ptr( + current_block: &mut Block, + context: &mut Context, + ptr: Value, + len: Value, + ptr_to: Type, +) -> Value { + assert!(ptr.get_type(context).unwrap().is_ptr(context)); + assert!(len.get_type(context).unwrap().is_uint64(context)); + + let uint64 = Type::get_uint64(context); + let ptr = current_block.append(context).ptr_to_int(ptr, uint64); + let addr = current_block + .append(context) + .binary_op(BinaryOpKind::Add, ptr, len); + + let ptr_to = Type::new_ptr(context, ptr_to); + current_block.append(context).int_to_ptr(addr, ptr_to) +} + impl<'eng> FnCompiler<'eng> { #[allow(clippy::too_many_arguments)] pub(super) fn new( @@ -1603,26 +1623,6 @@ impl<'eng> FnCompiler<'eng> { .binary_op(BinaryOpKind::Add, len, step) } - fn calc_addr_as_ptr( - current_block: &mut Block, - context: &mut Context, - ptr: Value, - len: Value, - ptr_to: Type, - ) -> Value { - assert!(ptr.get_type(context).unwrap().is_ptr(context)); - assert!(len.get_type(context).unwrap().is_uint64(context)); - - let uint64 = Type::get_uint64(context); - let ptr = current_block.append(context).ptr_to_int(ptr, uint64); - let addr = current_block - .append(context) - .binary_op(BinaryOpKind::Add, ptr, len); - - let ptr_to = Type::new_ptr(context, ptr_to); - current_block.append(context).int_to_ptr(addr, ptr_to) - } - fn append_with_store( current_block: &mut Block, context: &mut Context, @@ -2277,6 +2277,82 @@ impl<'eng> FnCompiler<'eng> { Ok(TerminatorValue::new(slice, context)) } + Intrinsic::SliceElem => { + assert!(arguments.len() == 2); + + let u8_type = Type::get_uint8(context); + let uint64 = Type::get_uint64(context); + let ptr_u8 = Type::new_ptr(context, u8_type); + + let slice = &arguments[0]; + let item_ir_type = convert_resolved_typeid( + self.engines.te(), + self.engines.de(), + context, + &match &*self.engines.te().get(slice.return_type) { + TypeInfo::Slice(t) => t.type_id, + _ => unreachable!(), + }, + &slice.span.clone(), + )?; + let item_ir_type_size = item_ir_type.size(context); + let slice = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, slice)? + ); + let ptr_to_slice = save_to_local_return_ptr(self, context, slice)?; + + let ptr_to_slice_arg = AsmArg { + name: Ident::new_no_span("ptr_to_slice".into()), + initializer: Some(ptr_to_slice), + }; + + let item_ir_type_size = to_constant(self, context, item_ir_type_size.in_bytes()); + let item_len_arg = AsmArg { + name: Ident::new_no_span("item_len".into()), + initializer: Some(item_ir_type_size), + }; + + let idx = &arguments[1]; + let idx = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, idx)? + ); + let idx_arg = AsmArg { + name: Ident::new_no_span("idx".into()), + initializer: Some(idx), + }; + + let offset_out_arg = AsmArg { + name: Ident::new_no_span("offset".into()), + initializer: None, + }; + let ptr_out_arg = AsmArg { + name: Ident::new_no_span("ptr".into()), + initializer: None, + }; + + let ptr_to_elem_type = Type::new_ptr(context, item_ir_type); + + let ptr = self.current_block.append(context).asm_block( + vec![ + ptr_to_slice_arg, + idx_arg, + item_len_arg, + offset_out_arg, + ptr_out_arg, + ], + vec![ + AsmInstruction::lw_no_span("ptr", "ptr_to_slice", "i0"), + AsmInstruction::mul_no_span("offset", "idx", "item_len"), + AsmInstruction::add_no_span("ptr", "ptr", "offset"), + ], + ptr_to_elem_type, + Some(Ident::new_no_span("ptr".into())), + ); + + let elem = self.current_block.append(context).load(ptr); + + Ok(TerminatorValue::new(elem, context)) + } } } diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index e61cc14d82a..9a722d0c71d 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -102,6 +102,7 @@ impl ty::TyIntrinsicFunctionKind { Intrinsic::EncodeBufferAsRawSlice => { type_check_encode_as_raw_slice(handler, ctx, kind, arguments, type_arguments, span) } + Intrinsic::Slice => { if arguments.len() != 3 { return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs { @@ -178,6 +179,63 @@ impl ty::TyIntrinsicFunctionKind { slice_type, )) } + + Intrinsic::SliceElem => { + if arguments.len() != 2 { + return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs { + name: kind.to_string(), + expected: 2, + span, + })); + } + + let type_engine = ctx.engines.te(); + let engines = ctx.engines(); + + let mut ctx = ctx; + + // check first argument + let slice_span = arguments[0].span.clone(); + let slice_type = type_engine.insert(engines, TypeInfo::Unknown, None); + let slice_typed_expr = { + let ctx = ctx + .by_ref() + .with_help_text("") + .with_type_annotation(slice_type); + ty::TyExpression::type_check(handler, ctx, &arguments[0])? + }; + let TypeInfo::Slice(elem_type) = &*type_engine.get(slice_type) else { + return Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { + name: kind.to_string(), + span: slice_span, + hint: "".to_string(), + })); + }; + + // index argument + let index_type = type_engine.insert( + engines, + TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), + None, + ); + let index_typed_expr = { + let ctx = ctx + .by_ref() + .with_help_text("") + .with_type_annotation(index_type); + ty::TyExpression::type_check(handler, ctx, &arguments[1])? + }; + + Ok(( + TyIntrinsicFunctionKind { + kind, + arguments: vec![slice_typed_expr, index_typed_expr], + type_arguments: vec![], + span, + }, + elem_type.type_id, + )) + } } } } diff --git a/sway-core/src/semantic_analysis/cei_pattern_analysis.rs b/sway-core/src/semantic_analysis/cei_pattern_analysis.rs index bf33bffa169..1d72f32cfb1 100644 --- a/sway-core/src/semantic_analysis/cei_pattern_analysis.rs +++ b/sway-core/src/semantic_analysis/cei_pattern_analysis.rs @@ -649,6 +649,7 @@ fn effects_of_intrinsic(intr: &sway_ast::Intrinsic) -> HashSet { | EncodeBufferAppend | EncodeBufferAsRawSlice => HashSet::new(), Slice => todo!(), + SliceElem => todo!(), } } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 6da18e1cb16..6c7a6130eae 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -1,6 +1,17 @@ script; +fn assert(actual: u64, expected: u64) { + if actual != expected { + __revert(actual); + } +} + fn main() { let array: [u64; 4] = [1, 2, 3, 4]; let slice: __slice[u64] = __slice(array, 0, 4); + + assert(__slice_elem(slice, 0), 1); + assert(__slice_elem(slice, 1), 2); + assert(__slice_elem(slice, 2), 3); + assert(__slice_elem(slice, 3), 4); } From d974ada67f39555c7001250b64d0d79edeae3c2a Mon Sep 17 00:00:00 2001 From: xunilrj Date: Fri, 12 Jul 2024 15:25:57 +0100 Subject: [PATCH 04/22] slice of slices --- sway-core/src/ir_generation/function.rs | 46 +++-- .../ast_node/expression/intrinsic_function.rs | 178 ++++++++++-------- .../slice/slice_intrinsics/src/main.sw | 5 + 3 files changed, 138 insertions(+), 91 deletions(-) diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index d0a4ebcf840..9dff20b1e90 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -124,7 +124,6 @@ fn save_to_local_return_ptr( let local_var_ptr = s.current_block.append(context).get_local(local_var); let _ = s.current_block.append(context).store(local_var_ptr, value); - Ok(local_var_ptr) } @@ -2168,24 +2167,41 @@ impl<'eng> FnCompiler<'eng> { let u8_type = Type::get_uint8(context); let uint64 = Type::get_uint64(context); let ptr_u8 = Type::new_ptr(context, u8_type); + let ptr_u64 = Type::new_ptr(context, uint64); - let array = &arguments[0]; - let item_ir_type = convert_resolved_typeid( + let first_argument = &arguments[0]; + let elem_type_id = &match &*self.engines.te().get(first_argument.return_type) { + TypeInfo::Array(t, _) => t.type_id, + TypeInfo::Slice(t) => t.type_id, + _ => unreachable!(), + }; + let elem_ir_type = convert_resolved_typeid( self.engines.te(), self.engines.de(), context, - &match &*self.engines.te().get(array.return_type) { - TypeInfo::Array(t, _) => t.type_id, - _ => unreachable!(), - }, - &array.span.clone(), + elem_type_id, + &first_argument.span.clone(), )?; - let item_ir_type_size = item_ir_type.size(context); - let array = return_on_termination_or_extract!( - self.compile_expression_to_value(context, md_mgr, array)? + let item_ir_type_size = elem_ir_type.size(context); + let first_argument_value = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, first_argument)? ); - let array = save_to_local_return_ptr(self, context, array)?; - let array = self.current_block.append(context).ptr_to_int(array, uint64); + let ptr_to_first_argument = + save_to_local_return_ptr(self, context, first_argument_value)?; + let ptr_to_elements = match &*self.engines.te().get(first_argument.return_type) { + TypeInfo::Array(t, _) => self + .current_block + .append(context) + .ptr_to_int(ptr_to_first_argument, uint64), + TypeInfo::Slice(t) => { + let slice_ptr = self + .current_block + .append(context) + .cast_ptr(ptr_to_first_argument, ptr_u64); + self.current_block.append(context).load(slice_ptr) + } + _ => unreachable!(), + }; let start = &arguments[1]; let start = return_on_termination_or_extract!( @@ -2208,7 +2224,7 @@ impl<'eng> FnCompiler<'eng> { let array_ptr_arg = AsmArg { name: Ident::new_no_span("array_ptr".into()), - initializer: Some(array), + initializer: Some(ptr_to_elements), }; let start_arg = AsmArg { name: Ident::new_no_span("start".into()), @@ -2258,7 +2274,7 @@ impl<'eng> FnCompiler<'eng> { ); // compile the slice together - let return_type = Type::get_typed_slice(context, item_ir_type); + let return_type = Type::get_typed_slice(context, elem_ir_type); let slice_as_tuple = self.compile_tuple_from_values( context, vec![slice_ptr, slice_len], diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 9a722d0c71d..6b20889fe88 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -102,84 +102,9 @@ impl ty::TyIntrinsicFunctionKind { Intrinsic::EncodeBufferAsRawSlice => { type_check_encode_as_raw_slice(handler, ctx, kind, arguments, type_arguments, span) } - Intrinsic::Slice => { - if arguments.len() != 3 { - return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs { - name: kind.to_string(), - expected: 3, - span, - })); - } - - let type_engine = ctx.engines.te(); - let engines = ctx.engines(); - - let mut ctx = ctx; - - // check first argument - let array_span = arguments[0].span.clone(); - let array_type = type_engine.insert(engines, TypeInfo::Unknown, None); - let array_ty_expr = { - let ctx = ctx - .by_ref() - .with_help_text("") - .with_type_annotation(array_type); - ty::TyExpression::type_check(handler, ctx, &arguments[0])? - }; - let TypeInfo::Array(elem_type_arg, len) = &*type_engine.get(array_type) else { - return Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { - name: kind.to_string(), - span: array_span, - hint: "".to_string(), - })); - }; - - // start argument - let start_type = type_engine.insert( - engines, - TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), - None, - ); - let start_ty_expr = { - let ctx = ctx - .by_ref() - .with_help_text("") - .with_type_annotation(start_type); - ty::TyExpression::type_check(handler, ctx, &arguments[1])? - }; - - // end argument - let end_type = type_engine.insert( - engines, - TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), - None, - ); - let end_ty_expr = { - let ctx = ctx - .by_ref() - .with_help_text("") - .with_type_annotation(end_type); - ty::TyExpression::type_check(handler, ctx, &arguments[2])? - }; - - //TODO check start is literal inside of range - //TODO check end is literal inside of range - - let slice_type = - type_engine.insert(engines, TypeInfo::Slice(elem_type_arg.clone()), None); - - Ok(( - TyIntrinsicFunctionKind { - kind, - arguments: vec![array_ty_expr, start_ty_expr, end_ty_expr], - type_arguments: vec![], - span, - }, - slice_type, - )) + type_check_slice(handler, ctx, kind, arguments, type_arguments, span) } - Intrinsic::SliceElem => { if arguments.len() != 2 { return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs { @@ -240,6 +165,107 @@ impl ty::TyIntrinsicFunctionKind { } } +fn type_check_slice( + handler: &Handler, + mut ctx: TypeCheckContext, + kind: sway_ast::Intrinsic, + arguments: &[Expression], + _type_arguments: &[TypeArgument], + span: Span, +) -> Result<(ty::TyIntrinsicFunctionKind, TypeId), ErrorEmitted> { + if arguments.len() != 3 { + return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs { + name: kind.to_string(), + expected: 3, + span, + })); + } + + let type_engine = ctx.engines.te(); + let engines = ctx.engines(); + + // start argument + let start_type = type_engine.insert( + engines, + TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), + None, + ); + let start_ty_expr = { + let ctx = ctx + .by_ref() + .with_help_text("") + .with_type_annotation(start_type); + ty::TyExpression::type_check(handler, ctx, &arguments[1])? + }; + + // end argument + let end_type = type_engine.insert( + engines, + TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), + None, + ); + let end_ty_expr = { + let ctx = ctx + .by_ref() + .with_help_text("") + .with_type_annotation(end_type); + ty::TyExpression::type_check(handler, ctx, &arguments[2])? + }; + + // check first argument + let array_span = arguments[0].span.clone(); + let array_type = type_engine.insert(engines, TypeInfo::Unknown, None); + let array_ty_expr = { + let ctx = ctx + .by_ref() + .with_help_text("") + .with_type_annotation(array_type); + ty::TyExpression::type_check(handler, ctx, &arguments[0])? + }; + + // We can slice arrays or other slices + match &*type_engine.get(array_type) { + TypeInfo::Array(elem_type_arg, _) => { + //TODO check start is literal inside of range + //TODO check end is literal inside of range + + let slice_type = + type_engine.insert(engines, TypeInfo::Slice(elem_type_arg.clone()), None); + + Ok(( + TyIntrinsicFunctionKind { + kind, + arguments: vec![array_ty_expr, start_ty_expr, end_ty_expr], + type_arguments: vec![], + span, + }, + slice_type, + )) + } + TypeInfo::Slice(elem_type_arg) => { + let slice_type = + type_engine.insert(engines, TypeInfo::Slice(elem_type_arg.clone()), None); + + Ok(( + TyIntrinsicFunctionKind { + kind, + arguments: vec![array_ty_expr, start_ty_expr, end_ty_expr], + type_arguments: vec![], + span, + }, + slice_type, + )) + } + _ => { + return Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { + name: kind.to_string(), + span: array_span, + hint: "".to_string(), + })); + } + } +} + fn type_check_encode_as_raw_slice( handler: &Handler, mut ctx: TypeCheckContext, diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 6c7a6130eae..52cb0eb9980 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -14,4 +14,9 @@ fn main() { assert(__slice_elem(slice, 1), 2); assert(__slice_elem(slice, 2), 3); assert(__slice_elem(slice, 3), 4); + + let slice_of_slice: __slice[u64] = __slice(slice, 1, 2); + + assert(__slice_elem(slice_of_slice, 0), 2); + assert(__slice_elem(slice_of_slice, 1), 3); } From 3f0e3add24ccf92e7c4f94aeb26d2b58e7b80f24 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Fri, 12 Jul 2024 16:08:28 +0100 Subject: [PATCH 05/22] better error messages for __slice indices --- sway-core/src/language/literal.rs | 13 +++++ .../ast_node/expression/intrinsic_function.rs | 48 +++++++++++++++++-- sway-error/src/error.rs | 7 ++- .../slice/slice_intrinsics/src/main.sw | 11 +++++ 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/sway-core/src/language/literal.rs b/sway-core/src/language/literal.rs index a50712d04ad..afedaad12d0 100644 --- a/sway-core/src/language/literal.rs +++ b/sway-core/src/language/literal.rs @@ -22,6 +22,19 @@ pub enum Literal { B256([u8; 32]), } +impl Literal { + pub fn cast_value_to_u64(&self) -> Option { + match self { + Literal::U8(v) => Some(*v as u64), + Literal::U16(v) => Some(*v as u64), + Literal::U32(v) => Some(*v as u64), + Literal::U64(v) => Some(*v), + Literal::Numeric(v) => Some(*v), + _ => None, + } + } +} + impl Hash for Literal { fn hash(&self, state: &mut H) { use Literal::*; diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 6b20889fe88..9ec1d1a9ddc 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -223,11 +223,53 @@ fn type_check_slice( ty::TyExpression::type_check(handler, ctx, &arguments[0])? }; + // statically check start and end, if possible + let start_literal = match &start_ty_expr.expression { + ty::TyExpressionVariant::Literal(v) => v.cast_value_to_u64(), + _ => None, + }; + + let end_literal = match &end_ty_expr.expression { + ty::TyExpressionVariant::Literal(v) => v.cast_value_to_u64(), + _ => None, + }; + + if let (Some(start), Some(end)) = (start_literal, end_literal) { + if start >= end { + return Err( + handler.emit_err(CompileError::InvalidRangeEndGreaterThanStart { + start, + end, + span, + }), + ); + } + } + // We can slice arrays or other slices match &*type_engine.get(array_type) { - TypeInfo::Array(elem_type_arg, _) => { - //TODO check start is literal inside of range - //TODO check end is literal inside of range + TypeInfo::Array(elem_type_arg, array_len) => { + let array_len = array_len.val() as u64; + + if let Some(v) = start_literal { + if v >= array_len { + return Err(handler.emit_err(CompileError::ArrayOutOfBounds { + index: v, + count: array_len, + span, + })); + } + } + + if let Some(v) = end_literal { + if v >= array_len { + return Err(handler.emit_err(CompileError::ArrayOutOfBounds { + index: v, + count: array_len, + span, + })); + } + } let slice_type = type_engine.insert(engines, TypeInfo::Slice(elem_type_arg.clone()), None); diff --git a/sway-error/src/error.rs b/sway-error/src/error.rs index 45c42c222dd..2401a678514 100644 --- a/sway-error/src/error.rs +++ b/sway-error/src/error.rs @@ -655,8 +655,12 @@ pub enum CompileError { ContractStorageFromExternalContext { span: Span }, #[error("The {opcode} opcode cannot be used in a predicate.")] InvalidOpcodeFromPredicate { opcode: String, span: Span }, - #[error("Array index out of bounds; the length is {count} but the index is {index}.")] + #[error("Index out of bounds; the length is {count} but the index is {index}.")] ArrayOutOfBounds { index: u64, count: u64, span: Span }, + #[error( + "Invalid range; the range end at index {end} is smaller than its start at index {start}" + )] + InvalidRangeEndGreaterThanStart { start: u64, end: u64, span: Span }, #[error("Tuple index {index} is out of bounds. The tuple has {count} element{}.", plural_s(*count))] TupleIndexOutOfBounds { index: usize, @@ -1220,6 +1224,7 @@ impl Spanned for CompileError { CannotBeEvaluatedToConfigurableSizeUnknown { span } => span.clone(), EncodingUnsupportedType { span } => span.clone(), ConfigurableMissingAbiDecodeInPlace { span } => span.clone(), + InvalidRangeEndGreaterThanStart { span, .. } => span.clone(), } } } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 52cb0eb9980..547742f9b51 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -19,4 +19,15 @@ fn main() { assert(__slice_elem(slice_of_slice, 0), 2); assert(__slice_elem(slice_of_slice, 1), 3); + + // we cannot check index for slices + let slice_of_slice: __slice[u64] = __slice(slice, 100, 200); + // but we can check if start is lower than end + let slice_of_slice: __slice[u64] = __slice(slice, 200, 100); + + // array errors + let array: [u64; 4] = [1, 2, 3, 4]; + let slice: __slice[u64] = __slice(array, 0, 5); + let slice: __slice[u64] = __slice(array, 6, 7); + let slice: __slice[u64] = __slice(array, 2, 1); } From 2275b567585eec2cd6fb881bfeda31d1323b1c6c Mon Sep 17 00:00:00 2001 From: xunilrj Date: Tue, 16 Jul 2024 11:50:26 +0100 Subject: [PATCH 06/22] fix control flow for empty arrays --- .../forc-doc/src/render/item/type_anchor.rs | 12 ++ sway-ast/src/intrinsics.rs | 4 +- sway-core/src/abi_generation/abi_str.rs | 8 + sway-core/src/abi_generation/evm_abi.rs | 3 + sway-core/src/abi_generation/fuel_abi.rs | 41 +++++ .../asm_generation/fuel/fuel_asm_builder.rs | 6 +- .../dead_code_analysis.rs | 52 +++---- sway-core/src/ir_generation/const_eval.rs | 4 + sway-core/src/ir_generation/convert.rs | 26 ++-- .../ty/expression/expression_variant.rs | 9 ++ sway-core/src/lib.rs | 11 +- .../ast_node/declaration/auto_impl.rs | 12 +- .../ast_node/declaration/declaration.rs | 1 - .../ast_node/expression/intrinsic_function.rs | 142 +++++++++--------- .../ast_node/expression/typed_expression.rs | 8 +- .../semantic_analysis/node_dependencies.rs | 1 + .../semantic_analysis/type_check_context.rs | 22 +++ sway-core/src/type_system/info.rs | 2 +- .../src/type_system/substitute/subst_map.rs | 18 +++ sway-error/src/error.rs | 3 + sway-lib-core/src/lib.sw | 1 + sway-lib-core/src/ops.sw | 29 ++++ sway-lib-core/src/slice.sw | 104 +++++++++++++ sway-lsp/src/core/token.rs | 4 + sway-lsp/src/traverse/parsed_tree.rs | 3 + sway-lsp/src/traverse/typed_tree.rs | 3 + sway-parse/src/ty/mod.rs | 7 - .../slice/slice_intrinsics/src/main.sw | 33 ++-- 28 files changed, 414 insertions(+), 155 deletions(-) create mode 100644 sway-lib-core/src/slice.sw diff --git a/forc-plugins/forc-doc/src/render/item/type_anchor.rs b/forc-plugins/forc-doc/src/render/item/type_anchor.rs index 65b75ad8f39..514d7f6dcd2 100644 --- a/forc-plugins/forc-doc/src/render/item/type_anchor.rs +++ b/forc-plugins/forc-doc/src/render/item/type_anchor.rs @@ -33,6 +33,18 @@ pub(crate) fn render_type_anchor( : format!("; {}]", len.val()); }) } + TypeInfo::Slice(ty_arg) => { + let inner = render_type_anchor( + (*render_plan.engines.te().get(ty_arg.type_id)).clone(), + render_plan, + current_module_info, + )?; + Ok(box_html! { + : "__slice["; + : inner; + : "]"; + }) + } TypeInfo::Tuple(ty_args) => { let mut rendered_args: Vec<_> = Vec::new(); for ty_arg in ty_args { diff --git a/sway-ast/src/intrinsics.rs b/sway-ast/src/intrinsics.rs index 0f125e835e6..7107df7b3eb 100644 --- a/sway-ast/src/intrinsics.rs +++ b/sway-ast/src/intrinsics.rs @@ -41,8 +41,8 @@ pub enum Intrinsic { EncodeBufferEmpty, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_empty() EncodeBufferAppend, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_append(buffer, primitive data type) EncodeBufferAsRawSlice, // let slice: raw_slice = __encode_buffer_as_raw_slice(buffer) - Slice, - SliceElem, + Slice, // let ref_to_slice = __slice(array, inclusive_start_index, exclusive_end_index) + SliceElem, // let ref_to_item = __slice_elem(ref to slice, index) } impl fmt::Display for Intrinsic { diff --git a/sway-core/src/abi_generation/abi_str.rs b/sway-core/src/abi_generation/abi_str.rs index b52af834f0c..ed8e9ae3cb0 100644 --- a/sway-core/src/abi_generation/abi_str.rs +++ b/sway-core/src/abi_generation/abi_str.rs @@ -53,6 +53,14 @@ impl TypeId { }; format!("[{}; {}]", inner_type, count.val()) } + (TypeInfo::Slice(type_arg), TypeInfo::Slice(_)) => { + let inner_type = if ctx.abi_with_fully_specified_types { + type_engine.get(type_arg.type_id).abi_str(ctx, engines) + } else { + "_".to_string() + }; + format!("[{}]", inner_type) + } (TypeInfo::Custom { .. }, _) => { format!("generic {}", self_abi_str) } diff --git a/sway-core/src/abi_generation/evm_abi.rs b/sway-core/src/abi_generation/evm_abi.rs index 6cfb1040484..a189f224e59 100644 --- a/sway-core/src/abi_generation/evm_abi.rs +++ b/sway-core/src/abi_generation/evm_abi.rs @@ -49,6 +49,9 @@ fn get_type_str(type_id: &TypeId, engines: &Engines, resolved_type_id: TypeId) - assert_eq!(count.val(), resolved_count.val()); format!("[_; {}]", count.val()) } + (TypeInfo::Slice(_), TypeInfo::Slice(_)) => { + format!("__slice[_]") + } (TypeInfo::Custom { .. }, _) => { format!("generic {}", abi_str(&type_engine.get(*type_id), engines)) } diff --git a/sway-core/src/abi_generation/fuel_abi.rs b/sway-core/src/abi_generation/fuel_abi.rs index f49e8eb6fea..90d3173ad2b 100644 --- a/sway-core/src/abi_generation/fuel_abi.rs +++ b/sway-core/src/abi_generation/fuel_abi.rs @@ -406,6 +406,47 @@ impl TypeId { unreachable!(); } } + TypeInfo::Slice(..) => { + if let TypeInfo::Array(elem_ty, _) = &*type_engine.get(resolved_type_id) { + // The `program_abi::TypeDeclaration`s needed for the array element type + let elem_abi_ty = program_abi::TypeDeclaration { + type_id: elem_ty.initial_type_id.index(), + type_field: elem_ty.initial_type_id.get_abi_type_str( + &ctx.to_str_context(engines, false), + engines, + elem_ty.type_id, + ), + components: elem_ty.initial_type_id.get_abi_type_components( + ctx, + engines, + types, + elem_ty.type_id, + ), + type_parameters: elem_ty.initial_type_id.get_abi_type_parameters( + ctx, + engines, + types, + elem_ty.type_id, + ), + }; + types.push(elem_abi_ty); + + // Generate the JSON data for the array. This is basically a single + // `program_abi::TypeApplication` for the array element type + Some(vec![program_abi::TypeApplication { + name: "__slice_element".to_string(), + type_id: elem_ty.initial_type_id.index(), + type_arguments: elem_ty.initial_type_id.get_abi_type_arguments( + ctx, + engines, + types, + elem_ty.type_id, + ), + }]) + } else { + unreachable!(); + } + } TypeInfo::Tuple(_) => { if let TypeInfo::Tuple(fields) = &*type_engine.get(resolved_type_id) { // A list of all `program_abi::TypeDeclaration`s needed for the tuple fields diff --git a/sway-core/src/asm_generation/fuel/fuel_asm_builder.rs b/sway-core/src/asm_generation/fuel/fuel_asm_builder.rs index 08bb9c7b4f3..fec3b2b83b3 100644 --- a/sway-core/src/asm_generation/fuel/fuel_asm_builder.rs +++ b/sway-core/src/asm_generation/fuel/fuel_asm_builder.rs @@ -593,7 +593,7 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> { let op_span = self .md_mgr .md_to_span(self.context, op.metadata) - .unwrap_or_else(|| op.op_name.span()); + .unwrap_or_else(Span::dummy); let opcode = Op::parse_opcode( handler, &op.op_name, @@ -1930,10 +1930,6 @@ impl<'ir, 'eng> FuelAsmBuilder<'ir, 'eng> { .get_type(self.context) .map_or(true, |ty| !self.is_copy_type(&ty)) { - dbg!(stored_val - .get_type(self.context) - .unwrap() - .as_string(self.context)); Err(CompileError::Internal( "Attempt to store a non-copy type.", owning_span.unwrap_or_else(Span::dummy), diff --git a/sway-core/src/control_flow_analysis/dead_code_analysis.rs b/sway-core/src/control_flow_analysis/dead_code_analysis.rs index 0098313cd58..60843dbf3dd 100644 --- a/sway-core/src/control_flow_analysis/dead_code_analysis.rs +++ b/sway-core/src/control_flow_analysis/dead_code_analysis.rs @@ -970,6 +970,9 @@ fn get_struct_type_info_from_type_id( TypeInfo::Array(type_arg, _) => { get_struct_type_info_from_type_id(type_engine, decl_engine, type_arg.type_id) } + TypeInfo::Slice(type_arg) => { + get_struct_type_info_from_type_id(type_engine, decl_engine, type_arg.type_id) + } _ => Ok(None), } } @@ -1792,35 +1795,28 @@ fn connect_expression<'eng: 'cfg, 'cfg>( elem_type: _, contents, } => { - let mut element_diverge = false; - let nodes = contents - .iter() - .map(|elem| { - if !element_diverge - && type_engine - .get(elem.return_type) - .is_uninhabited(engines.te(), engines.de()) - { - element_diverge = true - } - connect_expression( - engines, - &elem.expression, - graph, - leaves, - exit_node, - "", - tree_type, - elem.span.clone(), - options, - ) - }) - .collect::, _>>()?; - if element_diverge { - Ok(vec![]) - } else { - Ok(nodes.concat()) + let mut last = leaves.to_vec(); + + for elem in contents.iter() { + last = connect_expression( + engines, + &elem.expression, + graph, + last.as_slice(), + None, + "", + tree_type, + elem.span.clone(), + options, + )?; + + // If an element diverges, break the connections and return nothing + if last.is_empty() { + break; + } } + + Ok(last) } ArrayIndex { prefix, index } => { let prefix_idx = connect_expression( diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index 878d995690e..cb0d424f7ce 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -1324,6 +1324,7 @@ mod tests { None, )); + dbg!(2); let r = crate::compile_to_ast( &handler, &engines, @@ -1334,12 +1335,15 @@ mod tests { None, ); + dbg!(2); let (errors, _warnings) = handler.consume(); + dbg!(1); if !errors.is_empty() { panic!("{:#?}", errors); } + dbg!(1); let f = r.unwrap(); let f = f.typed.unwrap(); diff --git a/sway-core/src/ir_generation/convert.rs b/sway-core/src/ir_generation/convert.rs index 6a840dbe90f..1affd7d5c9f 100644 --- a/sway-core/src/ir_generation/convert.rs +++ b/sway-core/src/ir_generation/convert.rs @@ -59,17 +59,11 @@ pub(super) fn convert_resolved_typeid( ast_type: &TypeId, span: &Span, ) -> Result { - // There's probably a better way to convert TypeError to String, but... we'll use something - // other than String eventually? IrError? - convert_resolved_type( - type_engine, - decl_engine, - context, - &type_engine - .to_typeinfo(*ast_type, span) - .map_err(|ty_err| CompileError::InternalOwned(format!("{ty_err:?}"), span.clone()))?, - span, - ) + let t = &type_engine + .to_typeinfo(*ast_type, span) + .map_err(|ty_err| CompileError::TypeMustBeKnownAtThisPoint { span: span.clone() })?; + + convert_resolved_type(type_engine, decl_engine, context, t, span) } pub(super) fn convert_resolved_typeid_no_span( @@ -140,6 +134,16 @@ fn convert_resolved_type( )?; Type::new_array(context, elem_type, length.val() as u64) } + TypeInfo::Slice(elem_type) => { + let elem_type = convert_resolved_typeid( + type_engine, + decl_engine, + context, + &elem_type.type_id, + span, + )?; + Type::get_typed_slice(context, elem_type) + } TypeInfo::Tuple(fields) => { if fields.is_empty() { // XXX We've removed Unit from the core compiler, replaced with an empty Tuple. diff --git a/sway-core/src/language/ty/expression/expression_variant.rs b/sway-core/src/language/ty/expression/expression_variant.rs index 12f68716031..4f5643a5020 100644 --- a/sway-core/src/language/ty/expression/expression_variant.rs +++ b/sway-core/src/language/ty/expression/expression_variant.rs @@ -172,6 +172,15 @@ pub enum TyExpressionVariant { Deref(Box), } +impl TyExpressionVariant { + pub fn as_literal(&self) -> Option<&Literal> { + match self { + TyExpressionVariant::Literal(v) => Some(v), + _ => None, + } + } +} + impl EqWithEngines for TyExpressionVariant {} impl PartialEqWithEngines for TyExpressionVariant { fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool { diff --git a/sway-core/src/lib.rs b/sway-core/src/lib.rs index bdc7e683ceb..81bc45f4005 100644 --- a/sway-core/src/lib.rs +++ b/sway-core/src/lib.rs @@ -828,10 +828,13 @@ pub(crate) fn compile_ast_to_ir_to_asm( program: &ty::TyProgram, build_config: &BuildConfig, ) -> Result { - // The IR pipeline relies on type information being fully resolved. - // If type information is found to still be generic or unresolved inside of - // IR, this is considered an internal compiler error. To resolve this situation, - // we need to explicitly ensure all types are resolved before going into IR. + // IR generaterion requires type information to be fully resolved. + // + // If type information is found to still be generic inside of + // IR, this is considered an internal compiler error. + // + // But, there are genuine cases for types be unknown here, like `let a = []`. These should + // have friendly errors. // // We _could_ introduce a new type here that uses TypeInfo instead of TypeId and throw away // the engine, since we don't need inference for IR. That'd be a _lot_ of copy-pasted code, diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs b/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs index 2731811f6a7..d0629249f51 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs @@ -221,7 +221,7 @@ where }, _ => { let variant_type_name = Self::generate_type(engines, x.type_argument.type_id)?; - format!("{tag_value} => {enum_name}::{variant_name}(buffer.decode::<{variant_type}>()), \n", + format!("{tag_value} => {enum_name}::{variant_name}(buffer.decode::<{variant_type}>()), \n", tag_value = x.tag, enum_name = enum_name, variant_name = name, @@ -551,6 +551,12 @@ where count.val() ) } + TypeInfo::Slice(elem_ty) => { + format!( + "__slice[{}]", + Self::generate_type(engines, elem_ty.type_id)? + ) + } TypeInfo::RawUntypedPtr => "raw_ptr".into(), TypeInfo::RawUntypedSlice => "raw_slice".into(), TypeInfo::Alias { name, .. } => name.to_string(), @@ -847,7 +853,7 @@ where let code = if args_types == "()" { format!( "pub fn __entry() -> raw_slice {{ - let result: {return_type} = main(); + let result: {return_type} = main(); encode::<{return_type}>(result) }}" ) @@ -855,7 +861,7 @@ where format!( "pub fn __entry() -> raw_slice {{ let args: {args_types} = decode_script_data::<{args_types}>(); - let result: {return_type} = main({expanded_args}); + let result: {return_type} = main({expanded_args}); encode::<{return_type}>(result) }}" ) diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs index 6e9f2aafb1d..1c4b1d977b6 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -120,7 +120,6 @@ impl TyDecl { "Variable declaration's type annotation does not match up \ with the assigned expression's type.", ); - let result = ty::TyExpression::type_check(handler, ctx.by_ref(), &var_decl.body); let body = result.unwrap_or_else(|err| { ty::TyExpression::error(err, var_decl.name.span(), engines) diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 9ec1d1a9ddc..8d19b946ca2 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -105,66 +105,74 @@ impl ty::TyIntrinsicFunctionKind { Intrinsic::Slice => { type_check_slice(handler, ctx, kind, arguments, type_arguments, span) } - Intrinsic::SliceElem => { - if arguments.len() != 2 { - return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs { - name: kind.to_string(), - expected: 2, - span, - })); - } - - let type_engine = ctx.engines.te(); - let engines = ctx.engines(); - - let mut ctx = ctx; - - // check first argument - let slice_span = arguments[0].span.clone(); - let slice_type = type_engine.insert(engines, TypeInfo::Unknown, None); - let slice_typed_expr = { - let ctx = ctx - .by_ref() - .with_help_text("") - .with_type_annotation(slice_type); - ty::TyExpression::type_check(handler, ctx, &arguments[0])? - }; - let TypeInfo::Slice(elem_type) = &*type_engine.get(slice_type) else { - return Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { - name: kind.to_string(), - span: slice_span, - hint: "".to_string(), - })); - }; - - // index argument - let index_type = type_engine.insert( - engines, - TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), - None, - ); - let index_typed_expr = { - let ctx = ctx - .by_ref() - .with_help_text("") - .with_type_annotation(index_type); - ty::TyExpression::type_check(handler, ctx, &arguments[1])? - }; - - Ok(( - TyIntrinsicFunctionKind { - kind, - arguments: vec![slice_typed_expr, index_typed_expr], - type_arguments: vec![], - span, - }, - elem_type.type_id, - )) - } + Intrinsic::SliceElem => type_check_slice_elem(arguments, handler, kind, span, ctx), } } } +fn type_check_slice_elem( + arguments: &[Expression], + handler: &Handler, + kind: Intrinsic, + span: Span, + ctx: TypeCheckContext, +) -> Result<(TyIntrinsicFunctionKind, TypeId), ErrorEmitted> { + if arguments.len() != 2 { + return Err(handler.emit_err(CompileError::IntrinsicIncorrectNumArgs { + name: kind.to_string(), + expected: 2, + span, + })); + } + + let type_engine = ctx.engines.te(); + let engines = ctx.engines(); + + let mut ctx = ctx; + + // check first argument + let slice_span = arguments[0].span.clone(); + let slice_type = type_engine.insert(engines, TypeInfo::Unknown, None); + let slice_typed_expr = { + let ctx = ctx + .by_ref() + .with_help_text("") + .with_type_annotation(slice_type); + ty::TyExpression::type_check(handler, ctx, &arguments[0])? + }; + let TypeInfo::Slice(elem_type) = &*type_engine.get(slice_type) else { + return Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { + name: kind.to_string(), + span: slice_span, + hint: "".to_string(), + })); + }; + + // index argument + let index_type = type_engine.insert( + engines, + TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), + None, + ); + let index_typed_expr = { + let ctx = ctx + .by_ref() + .with_help_text("") + .with_type_annotation(index_type); + ty::TyExpression::type_check(handler, ctx, &arguments[1])? + }; + + Ok(( + TyIntrinsicFunctionKind { + kind, + arguments: vec![slice_typed_expr, index_typed_expr], + type_arguments: vec![], + span, + }, + elem_type.type_id, + )) +} + fn type_check_slice( handler: &Handler, mut ctx: TypeCheckContext, @@ -224,18 +232,18 @@ fn type_check_slice( }; // statically check start and end, if possible - let start_literal = match &start_ty_expr.expression { - ty::TyExpressionVariant::Literal(v) => v.cast_value_to_u64(), - _ => None, - }; + let start_literal = start_ty_expr + .expression + .as_literal() + .and_then(|x| x.cast_value_to_u64()); - let end_literal = match &end_ty_expr.expression { - ty::TyExpressionVariant::Literal(v) => v.cast_value_to_u64(), - _ => None, - }; + let end_literal = end_ty_expr + .expression + .as_literal() + .and_then(|x| x.cast_value_to_u64()); if let (Some(start), Some(end)) = (start_literal, end_literal) { - if start >= end { + if start > end { return Err( handler.emit_err(CompileError::InvalidRangeEndGreaterThanStart { start, @@ -252,7 +260,7 @@ fn type_check_slice( let array_len = array_len.val() as u64; if let Some(v) = start_literal { - if v >= array_len { + if v > array_len { return Err(handler.emit_err(CompileError::ArrayOutOfBounds { index: v, count: array_len, @@ -262,7 +270,7 @@ fn type_check_slice( } if let Some(v) = end_literal { - if v >= array_len { + if v > array_len { return Err(handler.emit_err(CompileError::ArrayOutOfBounds { index: v, count: array_len, diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index e5f5ca57586..8a0c4f671c9 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs @@ -1805,20 +1805,20 @@ impl ty::TyExpression { let engines = ctx.engines(); if contents.is_empty() { - let never_type = type_engine.insert(engines, TypeInfo::Never, None); + let elem_type = type_engine.insert(engines, TypeInfo::Unknown, None); return Ok(ty::TyExpression { expression: ty::TyExpressionVariant::Array { - elem_type: never_type, + elem_type, contents: Vec::new(), }, return_type: type_engine.insert( engines, TypeInfo::Array( TypeArgument { - type_id: never_type, + type_id: elem_type, span: Span::dummy(), call_path_tree: None, - initial_type_id: never_type, + initial_type_id: elem_type, }, Length::new(0, Span::dummy()), ), diff --git a/sway-core/src/semantic_analysis/node_dependencies.rs b/sway-core/src/semantic_analysis/node_dependencies.rs index 45d2858a1b8..9fe1e98b670 100644 --- a/sway-core/src/semantic_analysis/node_dependencies.rs +++ b/sway-core/src/semantic_analysis/node_dependencies.rs @@ -794,6 +794,7 @@ impl Dependencies { deps.gather_from_type_argument(engines, elem) }), TypeInfo::Array(elem_type, _) => self.gather_from_type_argument(engines, elem_type), + TypeInfo::Slice(elem_type) => self.gather_from_type_argument(engines, elem_type), TypeInfo::Struct(decl_ref) => self.gather_from_iter( decl_engine.get_struct(decl_ref).fields.iter(), |deps, field| deps.gather_from_type_argument(engines, &field.type_argument), diff --git a/sway-core/src/semantic_analysis/type_check_context.rs b/sway-core/src/semantic_analysis/type_check_context.rs index 621c8e8d838..dc1d4661e98 100644 --- a/sway-core/src/semantic_analysis/type_check_context.rs +++ b/sway-core/src/semantic_analysis/type_check_context.rs @@ -593,6 +593,28 @@ impl<'a> TypeCheckContext<'a> { elem_ty.span.source_id(), ) } + TypeInfo::Slice(mut elem_ty) => { + elem_ty.type_id = self + .resolve( + handler, + elem_ty.type_id, + span, + enforce_type_arguments, + None, + mod_path, + ) + .unwrap_or_else(|err| { + self.engines + .te() + .insert(self.engines, TypeInfo::ErrorRecovery(err), None) + }); + + self.engines.te().insert( + self.engines, + TypeInfo::Slice(elem_ty.clone()), + elem_ty.span.source_id(), + ) + } TypeInfo::Tuple(mut type_arguments) => { for type_argument in type_arguments.iter_mut() { type_argument.type_id = self diff --git a/sway-core/src/type_system/info.rs b/sway-core/src/type_system/info.rs index 7ba5a84b547..22b851aa3b1 100644 --- a/sway-core/src/type_system/info.rs +++ b/sway-core/src/type_system/info.rs @@ -1049,7 +1049,7 @@ impl TypeInfo { TypeInfo::Tuple(fields) => fields .iter() .any(|field_type| id_uninhabited(field_type.type_id)), - TypeInfo::Array(elem_ty, length) => length.val() > 0 && id_uninhabited(elem_ty.type_id), + TypeInfo::Array(elem_ty, _) => id_uninhabited(elem_ty.type_id), TypeInfo::Ptr(ty) => id_uninhabited(ty.type_id), TypeInfo::Alias { name: _, ty } => id_uninhabited(ty.type_id), TypeInfo::Slice(ty) => id_uninhabited(ty.type_id), diff --git a/sway-core/src/type_system/substitute/subst_map.rs b/sway-core/src/type_system/substitute/subst_map.rs index 3d917bc5f43..e3d83bcd135 100644 --- a/sway-core/src/type_system/substitute/subst_map.rs +++ b/sway-core/src/type_system/substitute/subst_map.rs @@ -233,6 +233,14 @@ impl TypeSubstMap { vec![type_argument.type_id], ) } + (TypeInfo::Slice(type_parameter), TypeInfo::Slice(type_argument)) => { + TypeSubstMap::from_superset_and_subset_helper( + type_engine, + decl_engine, + vec![type_parameter.type_id], + vec![type_argument.type_id], + ) + } ( TypeInfo::Storage { fields: type_parameters, @@ -408,6 +416,16 @@ impl TypeSubstMap { ) }) } + TypeInfo::Slice(mut elem_ty) => { + self.find_match(elem_ty.type_id, engines).map(|type_id| { + elem_ty.type_id = type_id; + type_engine.insert( + engines, + TypeInfo::Slice(elem_ty.clone()), + elem_ty.span.source_id(), + ) + }) + } TypeInfo::Tuple(fields) => { let mut need_to_create_new = false; let mut source_id = None; diff --git a/sway-error/src/error.rs b/sway-error/src/error.rs index 2401a678514..5c06273c4bb 100644 --- a/sway-error/src/error.rs +++ b/sway-error/src/error.rs @@ -1009,6 +1009,8 @@ pub enum CompileError { EncodingUnsupportedType { span: Span }, #[error("Configurables need a function named \"abi_decode_in_place\" to be in scope.")] ConfigurableMissingAbiDecodeInPlace { span: Span }, + #[error("Type must be known at this point")] + TypeMustBeKnownAtThisPoint { span: Span }, } impl std::convert::From for CompileError { @@ -1225,6 +1227,7 @@ impl Spanned for CompileError { EncodingUnsupportedType { span } => span.clone(), ConfigurableMissingAbiDecodeInPlace { span } => span.clone(), InvalidRangeEndGreaterThanStart { span, .. } => span.clone(), + TypeMustBeKnownAtThisPoint { span, .. } => span.clone(), } } } diff --git a/sway-lib-core/src/lib.sw b/sway-lib-core/src/lib.sw index 9bec5cc9bd9..4437bdd85cb 100644 --- a/sway-lib-core/src/lib.sw +++ b/sway-lib-core/src/lib.sw @@ -3,6 +3,7 @@ library; pub mod primitives; pub mod raw_ptr; pub mod raw_slice; +pub mod slice; pub mod r#str; pub mod ops; pub mod primitive_conversions; diff --git a/sway-lib-core/src/ops.sw b/sway-lib-core/src/ops.sw index 1229ca8e649..38e1bc8b068 100644 --- a/sway-lib-core/src/ops.sw +++ b/sway-lib-core/src/ops.sw @@ -1,6 +1,7 @@ library; use ::primitives::*; +use ::slice::*; /// Trait for the addition of two values. pub trait Add { @@ -608,6 +609,7 @@ impl Eq for raw_ptr { } } + /// Trait to evaluate if one value is greater or less than another of the same type. pub trait Ord { /// Evaluates if one value of the same type is greater than another. @@ -1257,3 +1259,30 @@ pub fn ok_str_eq() { assert("" != "a"); assert("a" != "b"); } + + + +impl Eq for __slice[T] where T: Eq { + fn eq(self, other: Self) -> bool { + let self_qty = self.len(); + let other_qty = other.len(); + + if self_qty != other_qty { + return false + } + + let mut i = 0u64; + while i < self_qty { + let self_elem = __slice_elem(self, i); + let other_elem = __slice_elem(other, i); + + if self_elem != other_elem { + return false; + } + + i += 1; + } + + true + } +} diff --git a/sway-lib-core/src/slice.sw b/sway-lib-core/src/slice.sw new file mode 100644 index 00000000000..6d5c3131572 --- /dev/null +++ b/sway-lib-core/src/slice.sw @@ -0,0 +1,104 @@ +library; + +use ::raw_ptr::*; + +/// Returns a `__slice[T]` from a pointer and length. +/// +/// # Arguments +/// +/// * `parts`: [(raw_ptr, u64)] - A location in memory and a length to become a `__slice[T]`. +/// +/// # Returns +/// +/// * [__slice[T]] - The newly created `__slice[T]`. +fn from_parts(parts: (raw_ptr, u64)) -> __slice[T] { + asm(ptr: parts) { + ptr: __slice[T] + } +} + +/// Returns a pointer and length from a `__slice[T]`. +/// +/// # Arguments +/// +/// * `slice`: [__slice[T]] - The slice to be broken into its parts. +/// +/// # Returns +/// +/// * [(raw_ptr, u64)] - A tuple of the location in memory of the original `__slice[T]` and its length. +fn into_parts(slice: __slice[T]) -> (raw_ptr, u64) { + asm(ptr: slice) { + ptr: (raw_ptr, u64) + } +} + +impl __slice[T] { + /// Forms a slice from a pointer and a length. + /// + /// # Arguments + /// + /// * `ptr`: [raw_ptr] - The pointer to the location in memory. + /// * `count`: [u64] - The number of `__size_of::` bytes. + /// + /// # Returns + /// + /// * [__slice[T]] - The newly created `__slice[T]`. + /// + /// # Examples + /// + /// ```sway + /// use std::alloc::alloc; + /// + /// fn foo() { + /// let ptr = alloc::(1); + /// let slice = slice::from_parts::(ptr, 1); + /// assert(slice.len() == 1); + /// } + /// ``` + pub fn from_parts(ptr: raw_ptr, count: u64) -> Self { + from_parts((ptr, __mul(count, __size_of::()))) + } + + /// Returns the pointer to the slice. + /// + /// # Returns + /// + /// * [raw_ptr] - The pointer to the location in memory of the `__slice[T]`. + /// + /// # Examples + /// + /// ```sway + /// use std::alloc::alloc; + /// + /// fn foo() { + /// let ptr = alloc::(1); + /// let slice = slice::from_parts::(ptr, 1); + /// let slice_ptr = slice.ptr(); + /// assert(slice_ptr == ptr); + /// } + /// ``` + pub fn ptr(self) -> raw_ptr { + into_parts(self).0 + } + + /// Returns the number of elements in the slice. + /// + /// # Returns + /// + /// * [u64] - The length of the slice based on `size_of::`. + /// + /// # Examples + /// + /// ```sway + /// use std::alloc::alloc; + /// + /// fn foo() { + /// let ptr = alloc::(1); + /// let slice = raw_slice::from_parts::(ptr, 1); + /// assert(slice.len() == 1); + /// } + /// ``` + pub fn len(self) -> u64 { + __div(into_parts(self).1, __size_of::()) + } +} diff --git a/sway-lsp/src/core/token.rs b/sway-lsp/src/core/token.rs index 51a84347d83..3e657a0cff9 100644 --- a/sway-lsp/src/core/token.rs +++ b/sway-lsp/src/core/token.rs @@ -285,6 +285,10 @@ pub fn type_info_to_symbol_kind( let type_info = type_engine.get(elem_ty.type_id); type_info_to_symbol_kind(type_engine, &type_info, Some(&elem_ty.span())) } + TypeInfo::Slice(elem_ty) => { + let type_info = type_engine.get(elem_ty.type_id); + type_info_to_symbol_kind(type_engine, &type_info, Some(&elem_ty.span())) + } _ => SymbolKind::Unknown, } } diff --git a/sway-lsp/src/traverse/parsed_tree.rs b/sway-lsp/src/traverse/parsed_tree.rs index 39ba771f879..869afce1925 100644 --- a/sway-lsp/src/traverse/parsed_tree.rs +++ b/sway-lsp/src/traverse/parsed_tree.rs @@ -1041,6 +1041,9 @@ impl Parse for TypeArgument { ); type_arg.parse(ctx); } + TypeInfo::Slice(type_arg) => { + type_arg.parse(ctx); + } TypeInfo::Tuple(type_arguments) => { adaptive_iter(type_arguments, |type_arg| type_arg.parse(ctx)); } diff --git a/sway-lsp/src/traverse/typed_tree.rs b/sway-lsp/src/traverse/typed_tree.rs index acfa4bf9de2..3c6098cc43d 100644 --- a/sway-lsp/src/traverse/typed_tree.rs +++ b/sway-lsp/src/traverse/typed_tree.rs @@ -1270,6 +1270,9 @@ fn collect_type_id( TypeInfo::Array(type_arg, ..) => { collect_type_argument(ctx, type_arg); } + TypeInfo::Slice(type_arg, ..) => { + collect_type_argument(ctx, type_arg); + } TypeInfo::Tuple(type_arguments) => { adaptive_iter(type_arguments, |type_arg| { collect_type_argument(ctx, type_arg); diff --git a/sway-parse/src/ty/mod.rs b/sway-parse/src/ty/mod.rs index 7e805b99977..dcba9b74ef1 100644 --- a/sway-parse/src/ty/mod.rs +++ b/sway-parse/src/ty/mod.rs @@ -30,13 +30,6 @@ impl Parse for Ty { return Err(parser .emit_error(ParseErrorKind::ExpectedCommaOrCloseParenInTupleOrParenExpression)); } - - // square brackets can be slice ([u64]) or arrays ([u64; 1]) - - // if let Some(descriptor) = SquareBrackets::try_parse(parser)? { - // return Ok(Ty::Slice(descriptor)); - // }; - if let Some(descriptor) = SquareBrackets::try_parse(parser)? { return Ok(Ty::Array(descriptor)); }; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 547742f9b51..2ab971a0027 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -6,28 +6,17 @@ fn assert(actual: u64, expected: u64) { } } -fn main() { - let array: [u64; 4] = [1, 2, 3, 4]; - let slice: __slice[u64] = __slice(array, 0, 4); - - assert(__slice_elem(slice, 0), 1); - assert(__slice_elem(slice, 1), 2); - assert(__slice_elem(slice, 2), 3); - assert(__slice_elem(slice, 3), 4); - - let slice_of_slice: __slice[u64] = __slice(slice, 1, 2); - - assert(__slice_elem(slice_of_slice, 0), 2); - assert(__slice_elem(slice_of_slice, 1), 3); +// we can slice arrays with non-literals +fn slice_method(array: [u64; 4], start: u64, end: u64, expected: __slice[u64]) { + // slice can happen with variables + let actual: __slice[u64] = __slice(array, start, end); - // we cannot check index for slices - let slice_of_slice: __slice[u64] = __slice(slice, 100, 200); - // but we can check if start is lower than end - let slice_of_slice: __slice[u64] = __slice(slice, 200, 100); + if actual != expected { + __revert(1); + } +} - // array errors - let array: [u64; 4] = [1, 2, 3, 4]; - let slice: __slice[u64] = __slice(array, 0, 5); - let slice: __slice[u64] = __slice(array, 6, 7); - let slice: __slice[u64] = __slice(array, 2, 1); +fn main() { + let a: [u64; 0] = []; + __log(1); } From ec906b380c40e871dfc679cb7edff4bca4825e8a Mon Sep 17 00:00:00 2001 From: xunilrj Date: Tue, 16 Jul 2024 12:29:49 +0100 Subject: [PATCH 07/22] fmt and clippy issues --- sway-core/src/abi_generation/evm_abi.rs | 4 +--- sway-core/src/ir_generation/convert.rs | 12 +----------- sway-core/src/ir_generation/function.rs | 10 ++-------- .../ast_node/expression/intrinsic_function.rs | 12 +++++------- .../src/type_system/substitute/subst_map.rs | 19 +++++++------------ sway-ir/src/asm.rs | 2 +- 6 files changed, 17 insertions(+), 42 deletions(-) diff --git a/sway-core/src/abi_generation/evm_abi.rs b/sway-core/src/abi_generation/evm_abi.rs index a189f224e59..f899e0de209 100644 --- a/sway-core/src/abi_generation/evm_abi.rs +++ b/sway-core/src/abi_generation/evm_abi.rs @@ -49,9 +49,7 @@ fn get_type_str(type_id: &TypeId, engines: &Engines, resolved_type_id: TypeId) - assert_eq!(count.val(), resolved_count.val()); format!("[_; {}]", count.val()) } - (TypeInfo::Slice(_), TypeInfo::Slice(_)) => { - format!("__slice[_]") - } + (TypeInfo::Slice(_), TypeInfo::Slice(_)) => "__slice[_]".into(), (TypeInfo::Custom { .. }, _) => { format!("generic {}", abi_str(&type_engine.get(*type_id), engines)) } diff --git a/sway-core/src/ir_generation/convert.rs b/sway-core/src/ir_generation/convert.rs index 1affd7d5c9f..d69153c551f 100644 --- a/sway-core/src/ir_generation/convert.rs +++ b/sway-core/src/ir_generation/convert.rs @@ -61,7 +61,7 @@ pub(super) fn convert_resolved_typeid( ) -> Result { let t = &type_engine .to_typeinfo(*ast_type, span) - .map_err(|ty_err| CompileError::TypeMustBeKnownAtThisPoint { span: span.clone() })?; + .map_err(|_| CompileError::TypeMustBeKnownAtThisPoint { span: span.clone() })?; convert_resolved_type(type_engine, decl_engine, context, t, span) } @@ -158,16 +158,6 @@ fn convert_resolved_type( TypeInfo::RawUntypedPtr => Type::get_uint64(context), TypeInfo::RawUntypedSlice => Type::get_slice(context), TypeInfo::Ptr(_) => Type::get_uint64(context), - TypeInfo::Slice(elem_type) => { - let elem_type = convert_resolved_typeid( - type_engine, - decl_engine, - context, - &elem_type.type_id, - span, - )?; - Type::get_typed_slice(context, elem_type) - } TypeInfo::Alias { ty, .. } => { convert_resolved_typeid(type_engine, decl_engine, context, &ty.type_id, span)? } diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index 9dff20b1e90..da3ef065437 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -2164,9 +2164,7 @@ impl<'eng> FnCompiler<'eng> { Intrinsic::Slice => { assert!(arguments.len() == 3); - let u8_type = Type::get_uint8(context); let uint64 = Type::get_uint64(context); - let ptr_u8 = Type::new_ptr(context, u8_type); let ptr_u64 = Type::new_ptr(context, uint64); let first_argument = &arguments[0]; @@ -2189,11 +2187,11 @@ impl<'eng> FnCompiler<'eng> { let ptr_to_first_argument = save_to_local_return_ptr(self, context, first_argument_value)?; let ptr_to_elements = match &*self.engines.te().get(first_argument.return_type) { - TypeInfo::Array(t, _) => self + TypeInfo::Array(_, _) => self .current_block .append(context) .ptr_to_int(ptr_to_first_argument, uint64), - TypeInfo::Slice(t) => { + TypeInfo::Slice(_) => { let slice_ptr = self .current_block .append(context) @@ -2296,10 +2294,6 @@ impl<'eng> FnCompiler<'eng> { Intrinsic::SliceElem => { assert!(arguments.len() == 2); - let u8_type = Type::get_uint8(context); - let uint64 = Type::get_uint64(context); - let ptr_u8 = Type::new_ptr(context, u8_type); - let slice = &arguments[0]; let item_ir_type = convert_resolved_typeid( self.engines.te(), diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 8d19b946ca2..0879df49d23 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -306,13 +306,11 @@ fn type_check_slice( slice_type, )) } - _ => { - return Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { - name: kind.to_string(), - span: array_span, - hint: "".to_string(), - })); - } + _ => Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { + name: kind.to_string(), + span: array_span, + hint: "".to_string(), + })), } } diff --git a/sway-core/src/type_system/substitute/subst_map.rs b/sway-core/src/type_system/substitute/subst_map.rs index e3d83bcd135..9eb13a8da3d 100644 --- a/sway-core/src/type_system/substitute/subst_map.rs +++ b/sway-core/src/type_system/substitute/subst_map.rs @@ -417,14 +417,13 @@ impl TypeSubstMap { }) } TypeInfo::Slice(mut elem_ty) => { - self.find_match(elem_ty.type_id, engines).map(|type_id| { - elem_ty.type_id = type_id; - type_engine.insert( - engines, - TypeInfo::Slice(elem_ty.clone()), - elem_ty.span.source_id(), - ) - }) + let type_id = self.find_match(elem_ty.type_id, engines)?; + elem_ty.type_id = type_id; + Some(type_engine.insert( + engines, + TypeInfo::Slice(elem_ty.clone()), + elem_ty.span.source_id(), + )) } TypeInfo::Tuple(fields) => { let mut need_to_create_new = false; @@ -488,10 +487,6 @@ impl TypeSubstMap { ty.type_id = type_id; type_engine.insert(engines, TypeInfo::Ptr(ty.clone()), ty.span.source_id()) }), - TypeInfo::Slice(mut ty) => self.find_match(ty.type_id, engines).map(|type_id| { - ty.type_id = type_id; - type_engine.insert(engines, TypeInfo::Slice(ty.clone()), ty.span.source_id()) - }), TypeInfo::TraitType { .. } => iter_for_match(engines, self, &type_info), TypeInfo::Ref { to_mutable_value, diff --git a/sway-ir/src/asm.rs b/sway-ir/src/asm.rs index 34d02ab9c17..a87944e4826 100644 --- a/sway-ir/src/asm.rs +++ b/sway-ir/src/asm.rs @@ -14,7 +14,7 @@ //! } //! ``` -use sway_types::{ident::Ident, BaseIdent}; +use sway_types::ident::Ident; use crate::{ context::Context, irtype::Type, metadata::MetadataIndex, pretty::DebugWithContext, value::Value, From a99a4fc8370beb2aa7050d4709df1ec644c59a53 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Fri, 19 Jul 2024 14:41:18 +0100 Subject: [PATCH 08/22] vec build on top on slice --- a.txt | 784 ++++++++++++ sway-core/src/ir_generation/compile.rs | 14 +- sway-core/src/ir_generation/const_eval.rs | 18 +- sway-core/src/ir_generation/convert.rs | 64 +- sway-core/src/ir_generation/function.rs | 446 +++---- sway-core/src/ir_generation/types.rs | 8 +- sway-core/src/lib.rs | 30 +- .../ast_node/expression/intrinsic_function.rs | 83 +- sway-core/src/type_system/info.rs | 22 + sway-error/src/error.rs | 2 +- sway-ir/src/asm.rs | 19 + sway-lib-core/src/codec.sw | 13 +- sway-lib-core/src/ops.sw | 53 +- sway-lib-core/src/slice.sw | 103 +- .../e2e_vm_tests/in_language_tests/.gitignore | 1 + .../e2e_vm_tests/in_language_tests/Forc.toml | 43 + .../address_inline_tests/Forc.toml | 8 + .../address_inline_tests/src/main.sw | 182 +++ .../alloc_inline_tests/Forc.toml | 8 + .../alloc_inline_tests/src/main.sw | 333 ++++++ .../Forc.toml | 8 + .../src/main.sw | 55 + .../Forc.toml | 8 + .../src/main.sw | 37 + .../Forc.toml | 8 + .../src/main.sw | 55 + .../Forc.toml | 8 + .../src/main.sw | 41 + .../Forc.toml | 8 + .../src/main.sw | 49 + .../assert_inline_tests/Forc.toml | 8 + .../assert_inline_tests/src/main.sw | 171 +++ .../asset_id_contract_tests/Forc.toml | 8 + .../asset_id_contract_tests/src/main.sw | 22 + .../asset_id_inline_tests/Forc.toml | 8 + .../asset_id_inline_tests/src/main.sw | 252 ++++ .../test_programs/b512_inline_tests/Forc.toml | 8 + .../b512_inline_tests/src/main.sw | 284 +++++ .../block_inline_tests/Forc.toml | 8 + .../block_inline_tests/src/main.sw | 55 + .../Forc.toml | 8 + .../src/main.sw | 58 + .../Forc.toml | 8 + .../src/main.sw | 41 + .../Forc.toml | 8 + .../src/main.sw | 58 + .../Forc.toml | 8 + .../src/main.sw | 49 + .../Forc.toml | 8 + .../src/main.sw | 65 + .../bytes_inline_tests/Forc.toml | 8 + .../bytes_inline_tests/src/main.sw | 1047 +++++++++++++++++ .../contract_id_contract_tests/Forc.toml | 8 + .../contract_id_contract_tests/src/main.sw | 20 + .../contract_id_inline_tests/Forc.toml | 8 + .../contract_id_inline_tests/src/main.sw | 182 +++ .../test_programs/ecr_inline_tests/Forc.toml | 8 + .../ecr_inline_tests/src/main.sw | 133 +++ .../flags_inline_tests/Forc.toml | 8 + .../flags_inline_tests/src/main.sw | 67 ++ .../test_programs/hash_inline_tests/Forc.toml | 8 + .../hash_inline_tests/src/main.sw | 457 +++++++ .../identity_inline_tests/Forc.toml | 8 + .../identity_inline_tests/src/main.sw | 247 ++++ .../test_programs/math_inline_tests/Forc.toml | 8 + .../math_inline_tests/src/main.sw | 632 ++++++++++ .../option_inline_tests/Forc.toml | 8 + .../option_inline_tests/src/main.sw | 354 ++++++ .../Forc.toml | 8 + .../src/main.sw | 168 +++ .../Forc.toml | 8 + .../src/main.sw | 14 + .../Forc.toml | 8 + .../src/main.sw | 129 ++ .../Forc.toml | 8 + .../src/main.sw | 311 +++++ .../Forc.toml | 8 + .../src/main.sw | 135 +++ .../Forc.toml | 8 + .../src/main.sw | 141 +++ .../Forc.toml | 8 + .../src/main.sw | 123 ++ .../result_inline_tests/Forc.toml | 8 + .../result_inline_tests/src/main.sw | 194 +++ .../revert_inline_tests/Forc.toml | 8 + .../revert_inline_tests/src/main.sw | 16 + .../string_inline_tests/Forc.toml | 8 + .../string_inline_tests/src/main.sw | 382 ++++++ .../test_programs/u128_inline_tests/Forc.toml | 8 + .../u128_inline_tests/src/main.sw | 744 ++++++++++++ .../test_programs/vec_inline_tests/Forc.toml | 8 + .../vec_inline_tests/src/main.sw | 704 +++++++++++ .../vm_evm_ecr_inline_tests/Forc.toml | 8 + .../vm_evm_ecr_inline_tests/src/main.sw | 26 + .../vm_evm_evm_address_inline_tests/Forc.toml | 8 + .../src/main.sw | 162 +++ test/src/e2e_vm_tests/mod.rs | 33 + .../json_abi_oracle_new_encoding.json | 17 +- .../slice/slice_intrinsics/src/main.sw | 205 +++- 99 files changed, 10035 insertions(+), 438 deletions(-) create mode 100644 a.txt create mode 100644 test/src/e2e_vm_tests/in_language_tests/.gitignore create mode 100644 test/src/e2e_vm_tests/in_language_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/src/main.sw create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/Forc.toml create mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/src/main.sw diff --git a/a.txt b/a.txt new file mode 100644 index 00000000000..d268248d773 --- /dev/null +++ b/a.txt @@ -0,0 +1,784 @@ +Testing should_pass/language/slice/slice_intrinsics ... failed + Failed to compile slice_intrinsics + Compiling should_pass/language/slice/slice_intrinsics ... + Compiling library core (/home/xunilrj/github/sway/sway-lib-core) + Compiling script slice_intrinsics (/home/xunilrj/github/sway/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics) + Backtrace [ + { fn: "sway_core::ir_generation::function::FnCompiler::compile_intrinsic_function" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression_to_value" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, + { fn: "sway_core::ir_generation::compile::compile_fn" }, + { fn: "sway_core::ir_generation::CompiledFunctionCache::ty_function_decl_to_unique_function" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_fn_call" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression_to_value" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, + { fn: "sway_core::ir_generation::compile::compile_fn" }, + { fn: "sway_core::ir_generation::CompiledFunctionCache::ty_function_decl_to_unique_function" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_fn_call" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, + { fn: "sway_core::ir_generation::compile::compile_fn" }, + { fn: "sway_core::ir_generation::compile::compile_entry_function" }, + { fn: "sway_core::ir_generation::compile::compile_script" }, + { fn: "sway_core::ir_generation::compile_program" }, + { fn: "sway_core::ast_to_asm" }, + { fn: "forc_pkg::pkg::compile" }, + { fn: "forc_pkg::pkg::build" }, + { fn: "forc_pkg::pkg::build_with_options" }, + { fn: "test::e2e_vm_tests::harness::compile_to_bytes::{{closure}}" }, + { fn: "test::e2e_vm_tests::TestContext::run::{{closure}}" }, + { fn: "test::e2e_vm_tests::run::{{closure}}" }, + { fn: " as core::future::future::Future>::poll" }, + { fn: "test::main::{{closure}}" }, + { fn: "tokio::runtime::context::runtime::enter_runtime" }, + { fn: "tokio::runtime::runtime::Runtime::block_on" }, + { fn: "test::main" }, + { fn: "std::sys_common::backtrace::__rust_begin_short_backtrace" }, + { fn: "std::rt::lang_start::{{closure}}" }, + { fn: "std::rt::lang_start_internal" }, + { fn: "main" }, + { fn: "__libc_start_main" }, + { fn: "_start" }, + ] + Backtrace [ + { fn: "sway_core::ir_generation::function::FnCompiler::compile_intrinsic_function" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_deref_up_to_ptr" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression_to_value" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, + { fn: "sway_core::ir_generation::compile::compile_fn" }, + { fn: "sway_core::ir_generation::CompiledFunctionCache::ty_function_decl_to_unique_function" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_fn_call" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_fn_call" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression_to_value" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, + { fn: "sway_core::ir_generation::compile::compile_fn" }, + { fn: "sway_core::ir_generation::CompiledFunctionCache::ty_function_decl_to_unique_function" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_fn_call" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, + { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, + { fn: "sway_core::ir_generation::compile::compile_fn" }, + { fn: "sway_core::ir_generation::compile::compile_entry_function" }, + { fn: "sway_core::ir_generation::compile::compile_script" }, + { fn: "sway_core::ir_generation::compile_program" }, + { fn: "sway_core::ast_to_asm" }, + { fn: "forc_pkg::pkg::compile" }, + { fn: "forc_pkg::pkg::build" }, + { fn: "forc_pkg::pkg::build_with_options" }, + { fn: "test::e2e_vm_tests::harness::compile_to_bytes::{{closure}}" }, + { fn: "test::e2e_vm_tests::TestContext::run::{{closure}}" }, + { fn: "test::e2e_vm_tests::run::{{closure}}" }, + { fn: " as core::future::future::Future>::poll" }, + { fn: "test::main::{{closure}}" }, + { fn: "tokio::runtime::context::runtime::enter_runtime" }, + { fn: "tokio::runtime::runtime::Runtime::block_on" }, + { fn: "test::main" }, + { fn: "std::sys_common::backtrace::__rust_begin_short_backtrace" }, + { fn: "std::rt::lang_start::{{closure}}" }, + { fn: "std::rt::lang_start_internal" }, + { fn: "main" }, + { fn: "__libc_start_main" }, + { fn: "_start" }, + ] + Verification failed at push_2::entry + script { + pub entry fn __entry() -> slice, !1 { + local () result + + entry(): + v0 = call main_0(), !2 + v1 = get_local ptr (), result, !3 + store v0 to v1, !3 + v2 = get_local ptr (), result, !4 + v3 = load v2 + v4 = call encode_17(v3), !5 + ret slice v4 + } + + entry_orig fn main_0() -> (), !7 { + local mut { __slice[u64], u64 } v + + entry(): + v0 = call new_1(), !8 + v1 = get_local ptr { __slice[u64], u64 }, v, !9 + store v0 to v1, !9 + v2 = get_local ptr { __slice[u64], u64 }, v, !10 + v3 = const u64 1, !11 + v4 = call push_2(v2, v3), !12 + v5 = const u64 99, !13 + v6 = call encode_9(v5), !14 + v7 = const u64 1515152261580153489 + log slice v6, v7, !14 + v8 = get_local ptr { __slice[u64], u64 }, v, !15 + v9 = load v8 + v10 = const u64 0, !16 + v11 = call get_16(v9, v10), !17 + v12 = const u64 1, !18 + v13 = call assert_13(v11, v12), !19 + v14 = const unit () + ret () v14 + } + + pub fn new_1() -> { __slice[u64], u64 }, !20 { + local { u64, u64 } __anon_0 + local { __slice[u64], u64 } __anon_1 + local u64 ptr + + entry(): + v0 = asm() -> u64 hp, !21 { + } + v1 = get_local ptr u64, ptr, !22 + store v0 to v1, !22 + v2 = get_local ptr u64, ptr, !23 + v3 = load v2 + v4 = get_local ptr { u64, u64 }, __anon_0, !24 + v5 = const u64 0 + v6 = get_elem_ptr v4, ptr u64, v5, !24 + store v3 to v6, !24 + v7 = const u64 1 + v8 = get_elem_ptr v4, ptr u64, v7, !24 + v9 = const u64 0, !25 + store v9 to v8, !24 + v10 = asm(buf: v4) -> __slice[u64] buf, !26 { + } + v11 = get_local ptr { __slice[u64], u64 }, __anon_1, !27 + v12 = const u64 0 + v13 = get_elem_ptr v11, ptr __slice[u64], v12 + store v10 to v13, !27 + v14 = const u64 1 + v15 = get_elem_ptr v11, ptr u64, v14 + v16 = const u64 0, !28 + store v16 to v15, !27 + v17 = load v11 + ret { __slice[u64], u64 } v17 + } + + pub fn push_2(self !29: ptr { __slice[u64], u64 }, _item !30: u64) -> (), !31 { + local { u64, u64 } __anon_0 + local __slice[u64] __anon_1 + local u64 current_cap + local u64 new_cap + local u64 new_cap_in_bytes + local u64 new_item_idx + local u64 old_buf_ptr + local u64 old_cap_in_bytes + local u64 ptr + + entry(self: ptr { __slice[u64], u64 }, _item: u64): + v0 = const u64 1 + v1 = get_elem_ptr self, ptr u64, v0, !32 + v2 = get_local ptr u64, new_item_idx, !33 + store v1 to v2, !33 + ^ Verification failed: Store value and pointer type mismatch. + v3 = const u64 0 + v4 = get_elem_ptr self, ptr __slice[u64], v3, !34 + v5 = load v4 + v6 = call len_3(v5), !35 + v7 = get_local ptr u64, current_cap, !36 + store v6 to v7, !36 + v8 = get_local ptr u64, new_item_idx, !37 + v9 = load v8 + v10 = get_local ptr u64, current_cap, !38 + v11 = load v10 + v12 = call ge_4(v9, v11), !39 + cbr v12, block0(), block4(), !39 + + block0(): + v13 = get_local ptr u64, current_cap, !40 + v14 = load v13 + v15 = const u64 0, !41 + v16 = call eq_6(v14, v15), !42 + cbr v16, block1(), block2(), !42 + + block1(): + v17 = const u64 1, !43 + br block3(v17) + + block2(): + v18 = get_local ptr u64, current_cap, !44 + v19 = load v18 + v20 = const u64 2, !45 + v21 = call multiply_7(v19, v20), !46 + br block3(v21) + + block3(v22: u64): + v23 = get_local ptr u64, new_cap, !47 + store v22 to v23, !47 + v24 = get_local ptr u64, new_cap, !48 + v25 = load v24 + v26 = const u64 8 + v27 = call multiply_7(v25, v26), !49 + v28 = get_local ptr u64, new_cap_in_bytes, !50 + store v27 to v28, !50 + v29 = const u64 0 + v30 = get_elem_ptr self, ptr __slice[u64], v29, !34 + v31 = load v30 + v32 = call ptr_8(v31), !51 + v33 = get_local ptr u64, old_buf_ptr, !52 + store v32 to v33, !52 + v34 = get_local ptr u64, current_cap, !53 + v35 = load v34 + v36 = const u64 8 + v37 = call multiply_7(v35, v36), !54 + v38 = get_local ptr u64, old_cap_in_bytes, !55 + store v37 to v38, !55 + v39 = get_local ptr u64, new_cap_in_bytes, !56 + v40 = load v39 + v41 = get_local ptr u64, old_buf_ptr, !57 + v42 = load v41 + v43 = get_local ptr u64, old_cap_in_bytes, !58 + v44 = load v43 + v45 = asm(new_cap_in_bytes: v40, old_buf_ptr: v42, old_cap_in_bytes: v44) -> u64 hp, !59 { + log hp hp hp hp, !60 + aloc new_cap_in_bytes, !61 + log hp hp hp hp, !62 + mcp hp old_buf_ptr old_cap_in_bytes, !63 + } + v46 = get_local ptr u64, ptr, !64 + store v45 to v46, !64 + v47 = get_local ptr u64, ptr, !65 + v48 = load v47 + v49 = get_local ptr u64, new_cap, !66 + v50 = load v49 + v51 = get_local ptr { u64, u64 }, __anon_0, !67 + v52 = const u64 0 + v53 = get_elem_ptr v51, ptr u64, v52, !67 + store v48 to v53, !67 + v54 = const u64 1 + v55 = get_elem_ptr v51, ptr u64, v54, !67 + store v50 to v55, !67 + v56 = asm(buf: v51) -> __slice[u64] buf, !68 { + } + v57 = const u64 0 + v58 = get_elem_ptr self, ptr __slice[u64], v57, !69 + store v56 to v58, !69 + v59 = const unit () + br block5(v59) + + block4(): + v60 = const unit () + br block5(v60) + + block5(v61: ()): + v62 = const u64 5, !70 + v63 = call encode_9(v62), !71 + v64 = const u64 1515152261580153489 + log slice v63, v64, !71 + v65 = const u64 0 + v66 = get_elem_ptr self, ptr __slice[u64], v65, !34 + v67 = load v66 + v68 = get_local ptr __slice[u64], __anon_1 + store v67 to v68 + v69 = get_local ptr u64, new_item_idx, !72 + v70 = load v69 + v71 = const u64 8 + v72 = asm(ptr_to_slice: v68, idx: v70, item_len: v71, offset, ptr) -> ptr u64 ptr { + lw ptr ptr_to_slice i0 + mul offset idx item_len + add ptr ptr offset + log ptr ptr ptr ptr + } + v73 = load v72 + v74 = const unit () + ret () v74 + } + + pub fn len_3(self !74: __slice[u64]) -> u64, !75 { + local u64 _ + local { u64, u64 } __tuple_2 + local { u64, u64 } __tuple_2_ + local u64 len + + entry(self: __slice[u64]): + v0 = asm(s: self) -> { u64, u64 } s, !76 { + } + v1 = get_local ptr { u64, u64 }, __tuple_2, !77 + store v0 to v1, !77 + v2 = get_local ptr { u64, u64 }, __tuple_2, !77 + v3 = get_local ptr { u64, u64 }, __tuple_2_, !77 + store v2 to v3, !77 + v4 = get_local ptr { u64, u64 }, __tuple_2_, !77 + v5 = const u64 0 + v6 = get_elem_ptr v4, ptr u64, v5, !77 + v7 = get_local ptr u64, _, !77 + store v6 to v7, !77 + v8 = get_local ptr { u64, u64 }, __tuple_2_, !77 + v9 = const u64 1 + v10 = get_elem_ptr v8, ptr u64, v9, !77 + v11 = get_local ptr u64, len, !77 + store v10 to v11, !77 + v12 = get_local ptr u64, len, !78 + v13 = load v12 + ret u64 v13 + } + + pub fn ge_4(self !80: u64, other !81: u64) -> bool, !82 { + entry(self: u64, other: u64): + v0 = call gt_5(self, other), !83 + cbr v0, block1(v0), block0(), !84 + + block0(): + v1 = call eq_6(self, other), !85 + br block1(v1), !84 + + block1(v2: bool): + ret bool v2 + } + + pub fn gt_5(self !86: u64, other !87: u64) -> bool, !88 { + entry(self: u64, other: u64): + v0 = cmp gt self other + ret bool v0 + } + + pub fn eq_6(self !89: u64, other !90: u64) -> bool, !91 { + entry(self: u64, other: u64): + v0 = cmp eq self other + ret bool v0 + } + + pub fn multiply_7(self !92: u64, other !93: u64) -> u64, !94 { + entry(self: u64, other: u64): + v0 = mul self, other + ret u64 v0 + } + + pub fn ptr_8(self !95: __slice[u64]) -> u64, !96 { + local u64 _ + local { u64, u64 } __tuple_1 + local { u64, u64 } __tuple_1_ + local u64 ptr + + entry(self: __slice[u64]): + v0 = asm(s: self) -> { u64, u64 } s, !97 { + } + v1 = get_local ptr { u64, u64 }, __tuple_1, !98 + store v0 to v1, !98 + v2 = get_local ptr { u64, u64 }, __tuple_1, !98 + v3 = get_local ptr { u64, u64 }, __tuple_1_, !98 + store v2 to v3, !98 + v4 = get_local ptr { u64, u64 }, __tuple_1_, !98 + v5 = const u64 0 + v6 = get_elem_ptr v4, ptr u64, v5, !98 + v7 = get_local ptr u64, ptr, !98 + store v6 to v7, !98 + v8 = get_local ptr { u64, u64 }, __tuple_1_, !98 + v9 = const u64 1 + v10 = get_elem_ptr v8, ptr u64, v9, !98 + v11 = get_local ptr u64, _, !98 + store v10 to v11, !98 + v12 = get_local ptr u64, ptr, !99 + v13 = load v12 + ret u64 v13 + } + + pub fn encode_9(item !101: u64) -> slice, !102 { + local { { u64, u64, u64 } } buffer + + entry(item: u64): + v0 = call new_11(), !103 + v1 = call abi_encode_10(item, v0), !104 + v2 = get_local ptr { { u64, u64, u64 } }, buffer, !105 + store v1 to v2, !105 + v3 = get_local ptr { { u64, u64, u64 } }, buffer, !106 + v4 = load v3 + v5 = call as_raw_slice_12(v4), !107 + ret slice v5 + } + + pub fn abi_encode_10(self !108: u64, buffer !109: { { u64, u64, u64 } }) -> { { u64, u64, u64 } }, !110 { + local { u64, u64, u64 } __anon_0 + local { u64, u64, u64 } __anon_1 + local { { u64, u64, u64 } } __anon_2 + + entry(self: u64, buffer: { { u64, u64, u64 } }): + v0 = ptr_to_int buffer to u64 + v1 = int_to_ptr v0 to ptr { { u64, u64, u64 } } + v2 = const u64 0 + v3 = get_elem_ptr v1, ptr { u64, u64, u64 }, v2, !111 + v4 = load v3 + v5 = asm(buffer: v4) -> { u64, u64, u64 } buffer { + } + v6 = get_local ptr { u64, u64, u64 }, __anon_0 + store v5 to v6 + v7 = const u64 0 + v8 = get_elem_ptr v6, ptr u64, v7 + v9 = load v8 + v10 = int_to_ptr v9 to ptr u8 + v11 = const u64 1 + v12 = get_elem_ptr v6, ptr u64, v11 + v13 = load v12 + v14 = const u64 2 + v15 = get_elem_ptr v6, ptr u64, v14 + v16 = load v15 + v17 = const u64 8 + v18 = add v16, v17 + v19 = cmp gt v18 v13 + cbr v19, block1(), block2() + + block0(v20: ptr u8, v21: u64): + v22 = ptr_to_int v20 to u64 + v23 = add v22, v16 + v24 = int_to_ptr v23 to ptr u64 + store self to v24 + v25 = const u64 8 + v26 = add v16, v25 + v27 = ptr_to_int v20 to u64 + v28 = get_local ptr { u64, u64, u64 }, __anon_1 + v29 = const u64 0 + v30 = get_elem_ptr v28, ptr u64, v29 + store v27 to v30 + v31 = const u64 1 + v32 = get_elem_ptr v28, ptr u64, v31 + store v21 to v32 + v33 = const u64 2 + v34 = get_elem_ptr v28, ptr u64, v33 + store v26 to v34 + v35 = asm(buffer: v28) -> { u64, u64, u64 } buffer { + } + v36 = get_local ptr { { u64, u64, u64 } }, __anon_2, !112 + v37 = const u64 0 + v38 = get_elem_ptr v36, ptr { u64, u64, u64 }, v37 + store v35 to v38, !112 + v39 = load v36 + ret { { u64, u64, u64 } } v39 + + block1(): + v40 = const u64 2 + v41 = mul v13, v40 + v42 = asm(new_cap: v41, old_ptr: v10, len: v16) -> ptr u8 hp { + aloc new_cap + mcp hp old_ptr len + } + br block0(v42, v41) + + block2(): + br block0(v10, v13) + } + + pub fn new_11() -> { { u64, u64, u64 } }, !113 { + local { u64, u64, u64 } __anon_0 + local { { u64, u64, u64 } } __anon_1 + + entry(): + v0 = const u64 1024 + v1 = asm(cap: v0) -> u64 hp { + aloc cap + } + v2 = int_to_ptr v1 to ptr u8 + v3 = ptr_to_int v2 to u64 + v4 = get_local ptr { u64, u64, u64 }, __anon_0 + v5 = const u64 0 + v6 = get_elem_ptr v4, ptr u64, v5 + store v3 to v6 + v7 = const u64 1 + v8 = get_elem_ptr v4, ptr u64, v7 + store v0 to v8 + v9 = const u64 2 + v10 = get_elem_ptr v4, ptr u64, v9 + v11 = const u64 0 + store v11 to v10 + v12 = asm(buffer: v4) -> { u64, u64, u64 } buffer { + } + v13 = get_local ptr { { u64, u64, u64 } }, __anon_1, !114 + v14 = const u64 0 + v15 = get_elem_ptr v13, ptr { u64, u64, u64 }, v14 + store v12 to v15, !114 + v16 = load v13 + ret { { u64, u64, u64 } } v16 + } + + pub fn as_raw_slice_12(self !115: { { u64, u64, u64 } }) -> slice, !116 { + local { u64, u64, u64 } __anon_0 + local { u64, u64 } __anon_1 + + entry(self: { { u64, u64, u64 } }): + v0 = ptr_to_int self to u64 + v1 = int_to_ptr v0 to ptr { { u64, u64, u64 } } + v2 = const u64 0 + v3 = get_elem_ptr v1, ptr { u64, u64, u64 }, v2, !111 + v4 = load v3 + v5 = asm(buffer: v4) -> { u64, u64, u64 } buffer { + } + v6 = get_local ptr { u64, u64, u64 }, __anon_0 + store v5 to v6 + v7 = const u64 0 + v8 = get_elem_ptr v6, ptr u64, v7 + v9 = load v8 + v10 = int_to_ptr v9 to ptr u8 + v11 = const u64 1 + v12 = get_elem_ptr v6, ptr u64, v11 + v13 = load v12 + v14 = const u64 2 + v15 = get_elem_ptr v6, ptr u64, v14 + v16 = load v15 + v17 = ptr_to_int v10 to u64 + v18 = get_local ptr { u64, u64 }, __anon_1 + v19 = const u64 0 + v20 = get_elem_ptr v18, ptr u64, v19 + store v17 to v20 + v21 = const u64 1 + v22 = get_elem_ptr v18, ptr u64, v21 + store v16 to v22 + v23 = asm(s: v18) -> slice s { + } + ret slice v23 + } + + fn assert_13(l !117: u64, r !118: u64) -> (), !119 { + entry(l: u64, r: u64): + v0 = call neq_14(l, r), !120 + cbr v0, block0(), block1(), !120 + + block0(): + v1 = call encode_9(l), !121 + v2 = const u64 1515152261580153489 + log slice v1, v2, !121 + v3 = call encode_9(r), !122 + v4 = const u64 1515152261580153489 + log slice v3, v4, !122 + v5 = const u64 1, !123 + revert v5, !124 + + block1(): + v6 = const unit () + br block2(v6) + + block2(v7: ()): + v8 = const unit () + ret () v8 + } + + pub fn neq_14(self !125: u64, other !126: u64) -> bool, !127 { + entry(self: u64, other: u64): + v0 = call eq_6(self, other), !128 + v1 = call not_15(v0), !129 + ret bool v1 + } + + pub fn not_15(self !130: bool) -> bool, !131 { + entry(self: bool): + v0 = const bool false, !132 + v1 = cmp eq self v0 + ret bool v1 + } + + pub fn get_16(self !133: { __slice[u64], u64 }, index !134: u64) -> u64, !135 { + local __slice[u64] __anon_0 + + entry(self: { __slice[u64], u64 }, index: u64): + v0 = ptr_to_int self to u64 + v1 = int_to_ptr v0 to ptr { __slice[u64], u64 } + v2 = const u64 0 + v3 = get_elem_ptr v1, ptr __slice[u64], v2, !34 + v4 = load v3 + v5 = get_local ptr __slice[u64], __anon_0 + store v4 to v5 + v6 = const u64 8 + v7 = asm(ptr_to_slice: v5, idx: index, item_len: v6, offset, ptr) -> ptr u64 ptr { + lw ptr ptr_to_slice i0 + mul offset idx item_len + add ptr ptr offset + log ptr ptr ptr ptr + } + v8 = load v7 + v9 = int_to_ptr v8 to ptr u64, !136 + v10 = load v9 + ret u64 v10 + } + + pub fn encode_17(item !101: ()) -> slice, !102 { + local { { u64, u64, u64 } } buffer + + entry(item: ()): + v0 = call new_11(), !103 + v1 = call abi_encode_18(item, v0), !104 + v2 = get_local ptr { { u64, u64, u64 } }, buffer, !105 + store v1 to v2, !105 + v3 = get_local ptr { { u64, u64, u64 } }, buffer, !106 + v4 = load v3 + v5 = call as_raw_slice_12(v4), !107 + ret slice v5 + } + + pub fn abi_encode_18(self !137: (), buffer !138: { { u64, u64, u64 } }) -> { { u64, u64, u64 } }, !139 { + entry(self: (), buffer: { { u64, u64, u64 } }): + ret { { u64, u64, u64 } } buffer + } + } + + !0 = "" + !1 = span !0 0 123 + !2 = span !0 65 71 + !3 = span !0 48 72 + !4 = span !0 102 108 + !5 = span !0 89 109 + !6 = "/home/xunilrj/github/sway/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw" + !7 = span !6 1517 1624 + !8 = span !6 1556 1566 + !9 = span !6 1534 1567 + !10 = span !6 1572 1573 + !11 = span !6 1579 1580 + !12 = span !6 1572 1581 + !13 = span !6 1593 1595 + !14 = span !6 1587 1596 + !15 = span !6 1609 1610 + !16 = span !6 1615 1616 + !17 = span !6 1609 1617 + !18 = span !6 1619 1620 + !19 = span !6 1602 1621 + !20 = span !6 89 309 + !21 = span !6 130 171 + !22 = span !6 120 172 + !23 = span !6 215 218 + !24 = span !6 214 223 + !25 = span !6 221 222 + !26 = span !6 205 273 + !27 = span !6 181 303 + !28 = span !6 292 293 + !29 = span !6 335 339 + !30 = span !6 341 346 + !31 = span !6 315 1291 + !32 = span !6 55 63 + !33 = span !6 361 389 + !34 = span !6 33 49 + !35 = span !6 416 430 + !36 = span !6 398 431 + !37 = span !6 443 455 + !38 = span !6 459 470 + !39 = span !6 443 470 + !40 = span !6 502 513 + !41 = span !6 517 518 + !42 = span !6 502 518 + !43 = span !6 537 538 + !44 = span !6 576 587 + !45 = span !6 590 591 + !46 = span !6 576 591 + !47 = span !6 485 606 + !48 = span !6 642 649 + !49 = span !6 642 668 + !50 = span !6 619 669 + !51 = span !6 701 715 + !52 = span !6 683 716 + !53 = span !6 752 763 + !54 = span !6 752 782 + !55 = span !6 729 783 + !56 = span !6 829 845 + !57 = span !6 860 871 + !58 = span !6 891 907 + !59 = span !6 807 1110 + !60 = span !6 927 942 + !61 = span !6 960 981 + !62 = span !6 999 1014 + !63 = span !6 1032 1067 + !64 = span !6 797 1111 + !65 = span !6 1146 1149 + !66 = span !6 1151 1158 + !67 = span !6 1145 1159 + !68 = span !6 1136 1209 + !69 = span !6 1125 1209 + !70 = span !6 1236 1237 + !71 = span !6 1230 1238 + !72 = span !6 1271 1283 + !73 = "/home/xunilrj/github/sway/sway-lib-core/src/slice.sw" + !74 = span !73 197 201 + !75 = span !73 186 308 + !76 = span !73 235 289 + !77 = span !73 220 290 + !78 = span !73 299 302 + !79 = "/home/xunilrj/github/sway/sway-lib-core/src/ops.sw" + !80 = span !79 21821 21825 + !81 = span !79 21827 21832 + !82 = span !79 21815 21896 + !83 = span !79 21858 21872 + !84 = span !79 21858 21890 + !85 = span !79 21876 21890 + !86 = span !79 15138 15142 + !87 = span !79 15144 15149 + !88 = span !79 15132 15198 + !89 = span !79 12661 12665 + !90 = span !79 12667 12672 + !91 = span !79 12655 12721 + !92 = span !79 4997 5001 + !93 = span !79 5003 5008 + !94 = span !79 4985 5058 + !95 = span !73 66 70 + !96 = span !73 55 180 + !97 = span !73 108 161 + !98 = span !73 93 162 + !99 = span !73 171 174 + !100 = "/home/xunilrj/github/sway/sway-lib-core/src/codec.sw" + !101 = span !100 64661 64665 + !102 = span !100 64644 64785 + !103 = span !100 64742 64755 + !104 = span !100 64726 64756 + !105 = span !100 64713 64757 + !106 = span !100 64762 64768 + !107 = span !100 64762 64783 + !108 = span !100 4642 4646 + !109 = span !100 4648 4654 + !110 = span !100 4628 4773 + !111 = span !100 55 82 + !112 = span !100 4684 4767 + !113 = span !100 128 228 + !114 = span !100 159 222 + !115 = span !100 483 487 + !116 = span !100 467 559 + !117 = span !6 1394 1395 + !118 = span !6 1400 1401 + !119 = span !6 1381 1515 + !120 = span !6 1443 1449 + !121 = span !6 1460 1468 + !122 = span !6 1478 1486 + !123 = span !6 1505 1506 + !124 = span !6 1496 1507 + !125 = span !79 12285 12289 + !126 = span !79 12291 12296 + !127 = span !79 12278 12350 + !128 = span !79 12323 12337 + !129 = span !79 12322 12344 + !130 = span !79 9972 9976 + !131 = span !79 9965 10019 + !132 = span !79 10007 10012 + !133 = span !6 1308 1312 + !134 = span !6 1314 1319 + !135 = span !6 1297 1377 + !136 = span !6 1341 1371 + !137 = span !100 36453 36457 + !138 = span !100 36459 36465 + !139 = span !100 36439 36507 + + + Failed to compile slice_intrinsics + warning + --> /home/xunilrj/github/sway/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw:49:9 +  | + 47 | + 48 | __log(5); + 49 | __slice_elem(self.buf, new_item_idx); +  | ------------------------------------ This returns a value of type &mut T, which is not assigned to anything and is ignored. + 50 | } +  | + ____ + + error: Internal compiler error: Verification failed: Store value and pointer type mismatch. + Please file an issue on the repository and include the code that triggered this error. + ____ + +  Aborting due to 1 error. + +_________________________________ +Sway tests result: failed. 828 total, 0 passed; 1 failed; 7 disabled [test duration: 00:00:410] +Failing tests: + should_pass/language/slice/slice_intrinsics ... failed diff --git a/sway-core/src/ir_generation/compile.rs b/sway-core/src/ir_generation/compile.rs index 1e8d937b5a2..8d4228de9ef 100644 --- a/sway-core/src/ir_generation/compile.rs +++ b/sway-core/src/ir_generation/compile.rs @@ -11,7 +11,7 @@ use crate::{ use super::{ const_eval::{compile_const_decl, LookupEnv}, - convert::convert_resolved_typeid, + convert::convert_resolved_type_id, function::FnCompiler, CompiledFunctionCache, }; @@ -298,11 +298,11 @@ pub(crate) fn compile_configurables( { let decl = engines.de().get(decl_id); - let ty = convert_resolved_typeid( + let ty = convert_resolved_type_id( engines.te(), engines.de(), context, - &decl.type_ascription.type_id, + decl.type_ascription.type_id, &decl.type_ascription.span, ) .unwrap(); @@ -520,11 +520,11 @@ fn compile_fn( .iter() .map(|param| { // Convert to an IR type. - convert_resolved_typeid( + convert_resolved_type_id( type_engine, decl_engine, context, - ¶m.type_argument.type_id, + param.type_argument.type_id, ¶m.type_argument.span, ) .map(|ty| { @@ -544,11 +544,11 @@ fn compile_fn( .collect::, CompileError>>() .map_err(|err| vec![err])?; - let ret_type = convert_resolved_typeid( + let ret_type = convert_resolved_type_id( type_engine, decl_engine, context, - &return_type.type_id, + return_type.type_id, &return_type.span, ) .map_err(|err| vec![err])?; diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index cb0d424f7ce..1b7a78d452a 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -12,7 +12,7 @@ use crate::{ }; use super::{ - convert::{convert_literal_to_constant, convert_resolved_typeid}, + convert::{convert_literal_to_constant, convert_resolved_type_id}, function::FnCompiler, types::*, }; @@ -1036,11 +1036,11 @@ fn const_eval_intrinsic( } Intrinsic::SizeOfType => { let targ = &intrinsic.type_arguments[0]; - let ir_type = convert_resolved_typeid( + let ir_type = convert_resolved_type_id( lookup.engines.te(), lookup.engines.de(), lookup.context, - &targ.type_id, + targ.type_id, &targ.span, ) .map_err(|_| ConstEvalError::CompileError)?; @@ -1052,11 +1052,11 @@ fn const_eval_intrinsic( Intrinsic::SizeOfVal => { let val = &intrinsic.arguments[0]; let type_id = val.return_type; - let ir_type = convert_resolved_typeid( + let ir_type = convert_resolved_type_id( lookup.engines.te(), lookup.engines.de(), lookup.context, - &type_id, + type_id, &val.span, ) .map_err(|_| ConstEvalError::CompileError)?; @@ -1067,11 +1067,11 @@ fn const_eval_intrinsic( } Intrinsic::SizeOfStr => { let targ = &intrinsic.type_arguments[0]; - let ir_type = convert_resolved_typeid( + let ir_type = convert_resolved_type_id( lookup.engines.te(), lookup.engines.de(), lookup.context, - &targ.type_id, + targ.type_id, &targ.span, ) .map_err(|_| ConstEvalError::CompileError)?; @@ -1084,11 +1084,11 @@ fn const_eval_intrinsic( } Intrinsic::AssertIsStrArray => { let targ = &intrinsic.type_arguments[0]; - let ir_type = convert_resolved_typeid( + let ir_type = convert_resolved_type_id( lookup.engines.te(), lookup.engines.de(), lookup.context, - &targ.type_id, + targ.type_id, &targ.span, ) .map_err(|_| ConstEvalError::CompileError)?; diff --git a/sway-core/src/ir_generation/convert.rs b/sway-core/src/ir_generation/convert.rs index d69153c551f..396425f1ac2 100644 --- a/sway-core/src/ir_generation/convert.rs +++ b/sway-core/src/ir_generation/convert.rs @@ -52,32 +52,29 @@ pub(super) fn convert_literal_to_constant( } } -pub(super) fn convert_resolved_typeid( +pub(super) fn convert_resolved_type_id( type_engine: &TypeEngine, decl_engine: &DeclEngine, context: &mut Context, - ast_type: &TypeId, + ast_type: TypeId, span: &Span, ) -> Result { - let t = &type_engine - .to_typeinfo(*ast_type, span) - .map_err(|_| CompileError::TypeMustBeKnownAtThisPoint { span: span.clone() })?; - - convert_resolved_type(type_engine, decl_engine, context, t, span) + let t = type_engine.get(ast_type); + convert_resolved_type_info(type_engine, decl_engine, context, &*t, span) } pub(super) fn convert_resolved_typeid_no_span( type_engine: &TypeEngine, decl_engine: &DeclEngine, context: &mut Context, - ast_type: &TypeId, + ast_type: TypeId, ) -> Result { let msg = "unknown source location"; let span = crate::span::Span::from_string(msg.to_string()); - convert_resolved_typeid(type_engine, decl_engine, context, ast_type, &span) + convert_resolved_type_id(type_engine, decl_engine, context, ast_type, &span) } -fn convert_resolved_type( +fn convert_resolved_type_info( type_engine: &TypeEngine, decl_engine: &DeclEngine, context: &mut Context, @@ -87,10 +84,10 @@ fn convert_resolved_type( // A handy macro for rejecting unsupported types. macro_rules! reject_type { ($name_str:literal) => {{ - return Err(CompileError::Internal( - concat!($name_str, " type cannot be resolved in IR."), - span.clone(), - )); + return Err(CompileError::TypeMustBeKnownAtThisPoint { + span: span.clone(), + internal: $name_str.into(), + }); }}; } @@ -125,25 +122,16 @@ fn convert_resolved_type( &decl_engine.get_enum(decl_ref).variants, )?, TypeInfo::Array(elem_type, length) => { - let elem_type = convert_resolved_typeid( + let elem_type = convert_resolved_type_id( type_engine, decl_engine, context, - &elem_type.type_id, + elem_type.type_id, span, )?; Type::new_array(context, elem_type, length.val() as u64) } - TypeInfo::Slice(elem_type) => { - let elem_type = convert_resolved_typeid( - type_engine, - decl_engine, - context, - &elem_type.type_id, - span, - )?; - Type::get_typed_slice(context, elem_type) - } + TypeInfo::Tuple(fields) => { if fields.is_empty() { // XXX We've removed Unit from the core compiler, replaced with an empty Tuple. @@ -159,11 +147,31 @@ fn convert_resolved_type( TypeInfo::RawUntypedSlice => Type::get_slice(context), TypeInfo::Ptr(_) => Type::get_uint64(context), TypeInfo::Alias { ty, .. } => { - convert_resolved_typeid(type_engine, decl_engine, context, &ty.type_id, span)? + convert_resolved_type_id(type_engine, decl_engine, context, ty.type_id, span)? + } + // refs to slice are actually fat pointers, + // all others refs are thin pointers. + TypeInfo::Ref { + referenced_type, .. + } => { + if let Some(slice_elem) = type_engine.get(referenced_type.type_id).as_slice() { + let elem_ir_type = convert_resolved_type_id( + type_engine, + decl_engine, + context, + slice_elem.type_id, + span, + )?; + Type::get_typed_slice(context, elem_ir_type) + } else { + Type::get_uint64(context) + } } - TypeInfo::Ref { .. } => Type::get_uint64(context), TypeInfo::Never => Type::get_never(context), + // Unsized types + TypeInfo::Slice(_) => reject_type!("unsized"), + // Unsupported types which shouldn't exist in the AST after type checking and // monomorphisation. TypeInfo::Custom { .. } => reject_type!("Custom"), diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index da3ef065437..b4f2bdde830 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -541,7 +541,7 @@ impl<'eng> FnCompiler<'eng> { ty::TyExpressionVariant::Array { elem_type, contents, - } => self.compile_array_expr(context, md_mgr, elem_type, contents, span_md_idx), + } => self.compile_array_expr(context, md_mgr, *elem_type, contents, span_md_idx), ty::TyExpressionVariant::ArrayIndex { prefix, index } => { self.compile_array_index(context, md_mgr, prefix, index, span_md_idx) } @@ -904,11 +904,11 @@ impl<'eng> FnCompiler<'eng> { Intrinsic::SizeOfVal => { let exp = &arguments[0]; // Compile the expression in case of side-effects but ignore its value. - let ir_type = convert_resolved_typeid( + let ir_type = convert_resolved_type_id( engines.te(), engines.de(), context, - &exp.return_type, + exp.return_type, &exp.span, )?; self.compile_expression_to_value(context, md_mgr, exp)?; @@ -917,11 +917,11 @@ impl<'eng> FnCompiler<'eng> { } Intrinsic::SizeOfType => { let targ = type_arguments[0].clone(); - let ir_type = convert_resolved_typeid( + let ir_type = convert_resolved_type_id( engines.te(), engines.de(), context, - &targ.type_id, + targ.type_id, &targ.span, )?; let val = Constant::get_uint(context, 64, ir_type.size(context).in_bytes()); @@ -929,11 +929,11 @@ impl<'eng> FnCompiler<'eng> { } Intrinsic::SizeOfStr => { let targ = type_arguments[0].clone(); - let ir_type = convert_resolved_typeid( + let ir_type = convert_resolved_type_id( engines.te(), engines.de(), context, - &targ.type_id, + targ.type_id, &targ.span, )?; let val = Constant::get_uint( @@ -960,11 +960,11 @@ impl<'eng> FnCompiler<'eng> { } Intrinsic::AssertIsStrArray => { let targ = type_arguments[0].clone(); - let ir_type = convert_resolved_typeid( + let ir_type = convert_resolved_type_id( engines.te(), engines.de(), context, - &targ.type_id, + targ.type_id, &targ.span, )?; match ir_type.get_content(context) { @@ -1037,11 +1037,11 @@ impl<'eng> FnCompiler<'eng> { // Get the target type from the type argument provided let target_type = &type_arguments[0]; - let target_ir_type = convert_resolved_typeid( + let target_ir_type = convert_resolved_type_id( engines.te(), engines.de(), context, - &target_type.type_id, + target_type.type_id, &target_type.span, )?; @@ -1322,11 +1322,11 @@ impl<'eng> FnCompiler<'eng> { }; let len = type_arguments[0].clone(); - let ir_type = convert_resolved_typeid( + let ir_type = convert_resolved_type_id( engines.te(), engines.de(), context, - &len.type_id, + len.type_id, &len.span, )?; let len_value = Constant::get_uint(context, 64, ir_type.size(context).in_bytes()); @@ -2161,209 +2161,227 @@ impl<'eng> FnCompiler<'eng> { Ok(TerminatorValue::new(buffer, context)) } - Intrinsic::Slice => { - assert!(arguments.len() == 3); + Intrinsic::Slice => self.compile_intrinsic_slice(arguments, context, md_mgr), + Intrinsic::SliceElem => self.compile_intrinsic_slice_elem(arguments, context, md_mgr), + } + } - let uint64 = Type::get_uint64(context); - let ptr_u64 = Type::new_ptr(context, uint64); + fn compile_intrinsic_slice_elem( + &mut self, + arguments: &Vec, + context: &mut Context, + md_mgr: &mut MetadataManager, + ) -> Result { + assert!(arguments.len() == 2); - let first_argument = &arguments[0]; - let elem_type_id = &match &*self.engines.te().get(first_argument.return_type) { - TypeInfo::Array(t, _) => t.type_id, - TypeInfo::Slice(t) => t.type_id, - _ => unreachable!(), - }; - let elem_ir_type = convert_resolved_typeid( - self.engines.te(), - self.engines.de(), - context, - elem_type_id, - &first_argument.span.clone(), - )?; - let item_ir_type_size = elem_ir_type.size(context); - let first_argument_value = return_on_termination_or_extract!( - self.compile_expression_to_value(context, md_mgr, first_argument)? - ); - let ptr_to_first_argument = - save_to_local_return_ptr(self, context, first_argument_value)?; - let ptr_to_elements = match &*self.engines.te().get(first_argument.return_type) { - TypeInfo::Array(_, _) => self - .current_block - .append(context) - .ptr_to_int(ptr_to_first_argument, uint64), - TypeInfo::Slice(_) => { - let slice_ptr = self - .current_block - .append(context) - .cast_ptr(ptr_to_first_argument, ptr_u64); - self.current_block.append(context).load(slice_ptr) - } - _ => unreachable!(), - }; + let te = self.engines.te(); - let start = &arguments[1]; - let start = return_on_termination_or_extract!( - self.compile_expression_to_value(context, md_mgr, start)? - ); + let ref_to_slice_expr = &arguments[0]; + let slice_type_id = te + .get(ref_to_slice_expr.return_type) + .as_reference() + .unwrap() + .1 + .type_id; + let elem_type_id = te.get(slice_type_id).as_slice().unwrap().type_id; + let elem_ir_type = convert_resolved_type_id( + self.engines.te(), + self.engines.de(), + context, + elem_type_id, + &ref_to_slice_expr.span.clone(), + )?; + let elem_ir_type_size = elem_ir_type.size(context); + let ref_to_slice_value = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, ref_to_slice_expr)? + ); + let ptr_to_ref_to_slice = save_to_local_return_ptr(self, context, ref_to_slice_value)?; - let end = &arguments[2]; - let end = return_on_termination_or_extract!( - self.compile_expression_to_value(context, md_mgr, end)? - ); + let ptr_to_slice_arg = AsmArg { + name: Ident::new_no_span("ptr_to_slice".into()), + initializer: Some(ptr_to_ref_to_slice), + }; - //asm(array_ptr: array, start: start, end: end, item_len: __size_of::(), slice_ptr, slice_len) { - // lw array_ptr array_ptr i0; - // mul slice_ptr start item_len; - // add slice_ptr slice_ptr array_ptr; // byte offset - // sub slice_len end start; - // mul slice_len slice_len item_len; // length in bytes - // (slice_ptr, slice_len): __slice[T] - //}; + let elem_ir_type_size = to_constant(self, context, elem_ir_type_size.in_bytes()); + let elem_len_arg = AsmArg { + name: Ident::new_no_span("item_len".into()), + initializer: Some(elem_ir_type_size), + }; - let array_ptr_arg = AsmArg { - name: Ident::new_no_span("array_ptr".into()), - initializer: Some(ptr_to_elements), - }; - let start_arg = AsmArg { - name: Ident::new_no_span("start".into()), - initializer: Some(start), - }; - let end_arg = AsmArg { - name: Ident::new_no_span("end".into()), - initializer: Some(end), - }; - let item_len_arg = AsmArg { - name: Ident::new_no_span("item_len".into()), - initializer: Some(to_constant(self, context, item_ir_type_size.in_bytes())), - }; + let idx = &arguments[1]; + let idx = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, idx)? + ); + let idx_arg = AsmArg { + name: Ident::new_no_span("idx".into()), + initializer: Some(idx), + }; - let slice_ptr_out_arg = AsmArg { - name: Ident::new_no_span("slice_ptr".into()), - initializer: None, - }; - let slice_len_out_arg = AsmArg { - name: Ident::new_no_span("slice_len".into()), - initializer: None, - }; + let offset_out_arg = AsmArg { + name: Ident::new_no_span("offset".into()), + initializer: None, + }; + let ptr_out_arg = AsmArg { + name: Ident::new_no_span("ptr".into()), + initializer: None, + }; - let slice_ptr = self.current_block.append(context).asm_block( - vec![ - array_ptr_arg, - start_arg.clone(), - item_len_arg.clone(), - slice_ptr_out_arg, - ], - vec![ - AsmInstruction::mul_no_span("slice_ptr", "start", "item_len"), - AsmInstruction::add_no_span("slice_ptr", "slice_ptr", "array_ptr"), - ], - uint64, - Some(Ident::new_no_span("slice_ptr".into())), - ); + let ptr_to_elem_type = Type::get_uint64(context); + + let ptr = self.current_block.append(context).asm_block( + vec![ + ptr_to_slice_arg, + idx_arg, + elem_len_arg, + offset_out_arg, + ptr_out_arg, + ], + vec![ + AsmInstruction::lw_no_span("ptr", "ptr_to_slice", "i0"), + AsmInstruction::mul_no_span("offset", "idx", "item_len"), + AsmInstruction::add_no_span("ptr", "ptr", "offset"), + AsmInstruction::log_no_span("ptr", "offset", "idx", "item_len"), + ], + ptr_to_elem_type, + Some(Ident::new_no_span("ptr".into())), + ); - let slice_len = self.current_block.append(context).asm_block( - vec![start_arg, end_arg, item_len_arg, slice_len_out_arg], - vec![ - AsmInstruction::sub_no_span("slice_len", "end", "start"), - AsmInstruction::mul_no_span("slice_len", "slice_len", "item_len"), - ], - uint64, - Some(Ident::new_no_span("slice_len".into())), - ); + Ok(TerminatorValue::new(ptr, context)) + } - // compile the slice together - let return_type = Type::get_typed_slice(context, elem_ir_type); - let slice_as_tuple = self.compile_tuple_from_values( - context, - vec![slice_ptr, slice_len], - vec![uint64, uint64], - None, - )?; - let slice = self.current_block.append(context).asm_block( - vec![AsmArg { - name: Ident::new_no_span("s".into()), - initializer: Some(slice_as_tuple), - }], - vec![], - return_type, - Some(Ident::new_no_span("s".into())), - ); + fn compile_intrinsic_slice( + &mut self, + arguments: &Vec, + context: &mut Context, + md_mgr: &mut MetadataManager, + ) -> Result { + assert!(arguments.len() == 3); + + let uint64 = Type::get_uint64(context); + let ptr_u64 = Type::new_ptr(context, uint64); - Ok(TerminatorValue::new(slice, context)) + let first_argument = &arguments[0]; + let elem_type_id = match &*self.engines.te().get(first_argument.return_type) { + TypeInfo::Array(t, _) => t.type_id, + TypeInfo::Slice(t) => t.type_id, + _ => unreachable!(), + }; + let elem_ir_type = convert_resolved_type_id( + self.engines.te(), + self.engines.de(), + context, + elem_type_id, + &first_argument.span.clone(), + )?; + let item_ir_type_size = elem_ir_type.size(context); + let first_argument_value = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, first_argument)? + ); + let ptr_to_first_argument = save_to_local_return_ptr(self, context, first_argument_value)?; + let ptr_to_elements = match &*self.engines.te().get(first_argument.return_type) { + TypeInfo::Array(_, _) => self + .current_block + .append(context) + .ptr_to_int(ptr_to_first_argument, uint64), + TypeInfo::Slice(_) => { + let slice_ptr = self + .current_block + .append(context) + .cast_ptr(ptr_to_first_argument, ptr_u64); + self.current_block.append(context).load(slice_ptr) } - Intrinsic::SliceElem => { - assert!(arguments.len() == 2); + _ => unreachable!(), + }; - let slice = &arguments[0]; - let item_ir_type = convert_resolved_typeid( - self.engines.te(), - self.engines.de(), - context, - &match &*self.engines.te().get(slice.return_type) { - TypeInfo::Slice(t) => t.type_id, - _ => unreachable!(), - }, - &slice.span.clone(), - )?; - let item_ir_type_size = item_ir_type.size(context); - let slice = return_on_termination_or_extract!( - self.compile_expression_to_value(context, md_mgr, slice)? - ); - let ptr_to_slice = save_to_local_return_ptr(self, context, slice)?; + let start = &arguments[1]; + let start = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, start)? + ); - let ptr_to_slice_arg = AsmArg { - name: Ident::new_no_span("ptr_to_slice".into()), - initializer: Some(ptr_to_slice), - }; + let end = &arguments[2]; + let end = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, end)? + ); - let item_ir_type_size = to_constant(self, context, item_ir_type_size.in_bytes()); - let item_len_arg = AsmArg { - name: Ident::new_no_span("item_len".into()), - initializer: Some(item_ir_type_size), - }; + //asm(array_ptr: array, start: start, end: end, item_len: __size_of::(), slice_ptr, slice_len) { + // lw array_ptr array_ptr i0; + // mul slice_ptr start item_len; + // add slice_ptr slice_ptr array_ptr; // byte offset + // sub slice_len end start; + // mul slice_len slice_len item_len; // length in bytes + // (slice_ptr, slice_len): __slice[T] + //}; - let idx = &arguments[1]; - let idx = return_on_termination_or_extract!( - self.compile_expression_to_value(context, md_mgr, idx)? - ); - let idx_arg = AsmArg { - name: Ident::new_no_span("idx".into()), - initializer: Some(idx), - }; + let array_ptr_arg = AsmArg { + name: Ident::new_no_span("array_ptr".into()), + initializer: Some(ptr_to_elements), + }; + let start_arg = AsmArg { + name: Ident::new_no_span("start".into()), + initializer: Some(start), + }; + let end_arg = AsmArg { + name: Ident::new_no_span("end".into()), + initializer: Some(end), + }; + let item_len_arg = AsmArg { + name: Ident::new_no_span("item_len".into()), + initializer: Some(to_constant(self, context, item_ir_type_size.in_bytes())), + }; - let offset_out_arg = AsmArg { - name: Ident::new_no_span("offset".into()), - initializer: None, - }; - let ptr_out_arg = AsmArg { - name: Ident::new_no_span("ptr".into()), - initializer: None, - }; + let slice_ptr_out_arg = AsmArg { + name: Ident::new_no_span("slice_ptr".into()), + initializer: None, + }; + let slice_len_out_arg = AsmArg { + name: Ident::new_no_span("slice_len".into()), + initializer: None, + }; - let ptr_to_elem_type = Type::new_ptr(context, item_ir_type); + let slice_ptr = self.current_block.append(context).asm_block( + vec![ + array_ptr_arg, + start_arg.clone(), + item_len_arg.clone(), + slice_ptr_out_arg, + ], + vec![ + AsmInstruction::mul_no_span("slice_ptr", "start", "item_len"), + AsmInstruction::add_no_span("slice_ptr", "slice_ptr", "array_ptr"), + ], + uint64, + Some(Ident::new_no_span("slice_ptr".into())), + ); - let ptr = self.current_block.append(context).asm_block( - vec![ - ptr_to_slice_arg, - idx_arg, - item_len_arg, - offset_out_arg, - ptr_out_arg, - ], - vec![ - AsmInstruction::lw_no_span("ptr", "ptr_to_slice", "i0"), - AsmInstruction::mul_no_span("offset", "idx", "item_len"), - AsmInstruction::add_no_span("ptr", "ptr", "offset"), - ], - ptr_to_elem_type, - Some(Ident::new_no_span("ptr".into())), - ); + let slice_len = self.current_block.append(context).asm_block( + vec![start_arg, end_arg, item_len_arg, slice_len_out_arg], + vec![ + AsmInstruction::sub_no_span("slice_len", "end", "start"), + AsmInstruction::mul_no_span("slice_len", "slice_len", "item_len"), + ], + uint64, + Some(Ident::new_no_span("slice_len".into())), + ); - let elem = self.current_block.append(context).load(ptr); + // compile the slice together + let return_type = Type::get_typed_slice(context, elem_ir_type); + let slice_as_tuple = self.compile_tuple_from_values( + context, + vec![slice_ptr, slice_len], + vec![uint64, uint64], + None, + )?; + let slice = self.current_block.append(context).asm_block( + vec![AsmArg { + name: Ident::new_no_span("s".into()), + initializer: Some(slice_as_tuple), + }], + vec![], + return_type, + Some(Ident::new_no_span("s".into())), + ); - Ok(TerminatorValue::new(elem, context)) - } - } + Ok(TerminatorValue::new(slice, context)) } fn compile_return( @@ -2494,11 +2512,11 @@ impl<'eng> FnCompiler<'eng> { )), }?; - let referenced_ir_type = convert_resolved_typeid( + let referenced_ir_type = convert_resolved_type_id( self.engines.te(), self.engines.de(), context, - &referenced_ast_type, + referenced_ast_type, &ast_expr.span.clone(), )?; @@ -2824,7 +2842,7 @@ impl<'eng> FnCompiler<'eng> { self.engines.te(), self.engines.de(), context, - &ast_return_type, + ast_return_type, )?; let ret_is_copy_type = self .engines @@ -2973,7 +2991,7 @@ impl<'eng> FnCompiler<'eng> { self.engines.te(), self.engines.de(), context, - &return_type, + return_type, ) .unwrap_or_else(|_| Type::get_unit(context)); let merge_val_arg_idx = merge_block.new_arg(context, return_type); @@ -3001,11 +3019,11 @@ impl<'eng> FnCompiler<'eng> { variant: &ty::TyEnumVariant, ) -> Result { // Retrieve the type info for the enum. - let enum_type = match convert_resolved_typeid( + let enum_type = match convert_resolved_type_id( self.engines.te(), self.engines.de(), context, - &exp.return_type, + exp.return_type, &exp.span, )? { ty if ty.is_struct(context) => ty, @@ -3282,11 +3300,11 @@ impl<'eng> FnCompiler<'eng> { // accessed and isn't present in the environment. let init_val = self.compile_expression_to_value(context, md_mgr, body); - let return_type = convert_resolved_typeid( + let return_type = convert_resolved_type_id( self.engines.te(), self.engines.de(), context, - &body.return_type, + body.return_type, &body.span, )?; @@ -3361,11 +3379,11 @@ impl<'eng> FnCompiler<'eng> { .lexical_map .insert(call_path.suffix.as_str().to_owned()); - let return_type = convert_resolved_typeid( + let return_type = convert_resolved_type_id( self.engines.te(), self.engines.de(), context, - &value.return_type, + value.return_type, &value.span, )?; @@ -3566,7 +3584,7 @@ impl<'eng> FnCompiler<'eng> { &mut self, context: &mut Context, md_mgr: &mut MetadataManager, - elem_type: &TypeId, + elem_type: TypeId, contents: &[ty::TyExpression], span_md_idx: Option, ) -> Result { @@ -3812,7 +3830,7 @@ impl<'eng> FnCompiler<'eng> { self.engines.te(), self.engines.de(), context, - &struct_field.value.return_type, + struct_field.value.return_type, )?; field_types.push(field_type); } @@ -3891,11 +3909,11 @@ impl<'eng> FnCompiler<'eng> { ) })?; - let field_type = convert_resolved_typeid( + let field_type = convert_resolved_type_id( self.engines.te(), self.engines.de(), context, - &field_type_id, + field_type_id, &ast_field.span, )?; @@ -4050,7 +4068,7 @@ impl<'eng> FnCompiler<'eng> { self.engines.te(), self.engines.de(), context, - &field_expr.return_type, + field_expr.return_type, )?; init_values.push(init_value); init_types.push(init_type); @@ -4074,11 +4092,11 @@ impl<'eng> FnCompiler<'eng> { let tuple_value = return_on_termination_or_extract!( self.compile_expression_to_ptr(context, md_mgr, tuple)? ); - let tuple_type = convert_resolved_typeid( + let tuple_type = convert_resolved_type_id( self.engines.te(), self.engines.de(), context, - &tuple_type, + tuple_type, &span, )?; @@ -4125,7 +4143,7 @@ impl<'eng> FnCompiler<'eng> { self.engines.te(), self.engines.de(), context, - &base_type, + base_type, )?; // Do the actual work. This is a recursive function because we want to drill down @@ -4230,7 +4248,7 @@ impl<'eng> FnCompiler<'eng> { self.engines.te(), self.engines.de(), context, - &return_type, + return_type, )?; let val = self .current_block diff --git a/sway-core/src/ir_generation/types.rs b/sway-core/src/ir_generation/types.rs index 6d549c7f890..63487dd6b78 100644 --- a/sway-core/src/ir_generation/types.rs +++ b/sway-core/src/ir_generation/types.rs @@ -26,7 +26,7 @@ pub(super) fn create_tagged_union_type( type_engine, decl_engine, context, - &tev.type_argument.type_id, + tev.type_argument.type_id, ) }) .collect::, CompileError>>()?; @@ -51,7 +51,7 @@ pub(super) fn create_tuple_aggregate( ) -> Result { let field_types = fields .iter() - .map(|ty_id| convert_resolved_typeid_no_span(type_engine, decl_engine, context, ty_id)) + .map(|ty_id| convert_resolved_typeid_no_span(type_engine, decl_engine, context, *ty_id)) .collect::, CompileError>>()?; Ok(Type::new_struct(context, field_types)) @@ -65,7 +65,7 @@ pub(super) fn create_array_aggregate( count: u64, ) -> Result { let element_type = - convert_resolved_typeid_no_span(type_engine, decl_engine, context, &element_type_id)?; + convert_resolved_typeid_no_span(type_engine, decl_engine, context, element_type_id)?; Ok(Type::new_array(context, element_type, count)) } @@ -77,7 +77,7 @@ pub(super) fn get_struct_for_types( ) -> Result { let types = type_ids .iter() - .map(|ty_id| convert_resolved_typeid_no_span(type_engine, decl_engine, context, ty_id)) + .map(|ty_id| convert_resolved_typeid_no_span(type_engine, decl_engine, context, *ty_id)) .collect::, CompileError>>()?; Ok(Type::new_struct(context, types)) } diff --git a/sway-core/src/lib.rs b/sway-core/src/lib.rs index 81bc45f4005..7508faab6ea 100644 --- a/sway-core/src/lib.rs +++ b/sway-core/src/lib.rs @@ -573,13 +573,17 @@ pub fn parsed_to_ast( } }; - // Skip collecting metadata if we triggered an optimised build from LSP. - let types_metadata = if !lsp_config.as_ref().is_some_and(|lsp| lsp.optimized_build) { + // Skip collecting type metadata and control-flow analysis + // if we triggered an optimised build from LSP. + + let is_lsp_optimized_build = lsp_config.as_ref().is_some_and(|lsp| lsp.optimized_build); + let types_metadata = if !is_lsp_optimized_build { // Collect information about the types used in this program let types_metadata_result = typed_program.collect_types_metadata( handler, &mut CollectTypesMetadataContext::new(engines, experimental, package_name.to_string()), ); + let types_metadata = match types_metadata_result { Ok(types_metadata) => types_metadata, Err(e) => { @@ -588,19 +592,17 @@ pub fn parsed_to_ast( } }; - typed_program - .logged_types - .extend(types_metadata.iter().filter_map(|m| match m { - TypeMetadata::LoggedType(log_id, type_id) => Some((*log_id, *type_id)), - _ => None, - })); + let logged_types = types_metadata.iter().filter_map(|m| match m { + TypeMetadata::LoggedType(log_id, type_id) => Some((*log_id, *type_id)), + _ => None, + }); + typed_program.logged_types.extend(logged_types); - typed_program - .messages_types - .extend(types_metadata.iter().filter_map(|m| match m { - TypeMetadata::MessageType(message_id, type_id) => Some((*message_id, *type_id)), - _ => None, - })); + let message_types = types_metadata.iter().filter_map(|m| match m { + TypeMetadata::MessageType(message_id, type_id) => Some((*message_id, *type_id)), + _ => None, + }); + typed_program.messages_types.extend(message_types); let (print_graph, print_graph_url_format) = match build_config { Some(cfg) => ( diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 0879df49d23..4b642a06c5e 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -131,19 +131,24 @@ fn type_check_slice_elem( let mut ctx = ctx; // check first argument - let slice_span = arguments[0].span.clone(); - let slice_type = type_engine.insert(engines, TypeInfo::Unknown, None); - let slice_typed_expr = { + let first_argument_span = arguments[0].span.clone(); + let first_argument_type = type_engine.insert(engines, TypeInfo::Unknown, None); + let first_argument_typed_expr = { let ctx = ctx .by_ref() .with_help_text("") - .with_type_annotation(slice_type); + .with_type_annotation(first_argument_type); ty::TyExpression::type_check(handler, ctx, &arguments[0])? }; - let TypeInfo::Slice(elem_type) = &*type_engine.get(slice_type) else { + + let first_argument_ref_to_type = type_engine + .get(first_argument_type) + .as_reference() + .map(|(_, t)| type_engine.get(t.type_id)); + let Some(TypeInfo::Slice(elem_type)) = first_argument_ref_to_type.as_deref() else { return Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { name: kind.to_string(), - span: slice_span, + span: first_argument_span, hint: "".to_string(), })); }; @@ -162,14 +167,28 @@ fn type_check_slice_elem( ty::TyExpression::type_check(handler, ctx, &arguments[1])? }; + let return_type = type_engine.insert( + engines, + TypeInfo::Ref { + to_mutable_value: true, + referenced_type: TypeArgument { + type_id: elem_type.type_id, + initial_type_id: elem_type.type_id, + span: Span::dummy(), + call_path_tree: None, + }, + }, + None, + ); + Ok(( TyIntrinsicFunctionKind { kind, - arguments: vec![slice_typed_expr, index_typed_expr], + arguments: vec![first_argument_typed_expr, index_typed_expr], type_arguments: vec![], span, }, - elem_type.type_id, + return_type, )) } @@ -192,7 +211,7 @@ fn type_check_slice( let type_engine = ctx.engines.te(); let engines = ctx.engines(); - // start argument + // start index argument let start_type = type_engine.insert( engines, TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), @@ -206,7 +225,7 @@ fn type_check_slice( ty::TyExpression::type_check(handler, ctx, &arguments[1])? }; - // end argument + // end index argument let end_type = type_engine.insert( engines, TypeInfo::UnsignedInteger(IntegerBits::SixtyFour), @@ -254,6 +273,22 @@ fn type_check_slice( } } + fn create_ref_to_slice(engines: &Engines, elem_type_arg: TypeArgument) -> TypeId { + let type_engine = engines.te(); + let slice_type_id = + type_engine.insert(engines, TypeInfo::Slice(elem_type_arg.clone()), None); + let ref_to_slice_type = TypeInfo::Ref { + to_mutable_value: false, + referenced_type: TypeArgument { + type_id: slice_type_id, + initial_type_id: slice_type_id, + span: Span::dummy(), + call_path_tree: None, + }, + }; + type_engine.insert(engines, ref_to_slice_type, None) + } + // We can slice arrays or other slices match &*type_engine.get(array_type) { TypeInfo::Array(elem_type_arg, array_len) => { @@ -279,9 +314,6 @@ fn type_check_slice( } } - let slice_type = - type_engine.insert(engines, TypeInfo::Slice(elem_type_arg.clone()), None); - Ok(( TyIntrinsicFunctionKind { kind, @@ -289,23 +321,18 @@ fn type_check_slice( type_arguments: vec![], span, }, - slice_type, - )) - } - TypeInfo::Slice(elem_type_arg) => { - let slice_type = - type_engine.insert(engines, TypeInfo::Slice(elem_type_arg.clone()), None); - - Ok(( - TyIntrinsicFunctionKind { - kind, - arguments: vec![array_ty_expr, start_ty_expr, end_ty_expr], - type_arguments: vec![], - span, - }, - slice_type, + create_ref_to_slice(engines, elem_type_arg.clone()), )) } + TypeInfo::Slice(elem_type_arg) => Ok(( + TyIntrinsicFunctionKind { + kind, + arguments: vec![array_ty_expr, start_ty_expr, end_ty_expr], + type_arguments: vec![], + span, + }, + create_ref_to_slice(engines, elem_type_arg.clone()), + )), _ => Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { name: kind.to_string(), span: array_span, diff --git a/sway-core/src/type_system/info.rs b/sway-core/src/type_system/info.rs index 22b851aa3b1..eeb22e6a734 100644 --- a/sway-core/src/type_system/info.rs +++ b/sway-core/src/type_system/info.rs @@ -1175,6 +1175,16 @@ impl TypeInfo { matches!(self, TypeInfo::Ref { .. }) } + pub fn as_reference(&self) -> Option<(&bool, &TypeArgument)> { + match self { + TypeInfo::Ref { + to_mutable_value, + referenced_type, + } => Some((to_mutable_value, referenced_type)), + _ => None, + } + } + pub fn is_array(&self) -> bool { matches!(self, TypeInfo::Array(_, _)) } @@ -1191,6 +1201,18 @@ impl TypeInfo { matches!(self, TypeInfo::Tuple(_)) } + pub fn is_slice(&self) -> bool { + matches!(self, TypeInfo::Slice(_)) + } + + pub fn as_slice(&self) -> Option<&TypeArgument> { + if let TypeInfo::Slice(t) = self { + Some(t) + } else { + None + } + } + pub(crate) fn apply_type_arguments( self, handler: &Handler, diff --git a/sway-error/src/error.rs b/sway-error/src/error.rs index 5c06273c4bb..38ead24b7db 100644 --- a/sway-error/src/error.rs +++ b/sway-error/src/error.rs @@ -1010,7 +1010,7 @@ pub enum CompileError { #[error("Configurables need a function named \"abi_decode_in_place\" to be in scope.")] ConfigurableMissingAbiDecodeInPlace { span: Span }, #[error("Type must be known at this point")] - TypeMustBeKnownAtThisPoint { span: Span }, + TypeMustBeKnownAtThisPoint { span: Span, internal: String }, } impl std::convert::From for CompileError { diff --git a/sway-ir/src/asm.rs b/sway-ir/src/asm.rs index a87944e4826..b6f4cd657c9 100644 --- a/sway-ir/src/asm.rs +++ b/sway-ir/src/asm.rs @@ -44,6 +44,25 @@ pub struct AsmInstruction { } impl AsmInstruction { + pub fn log_no_span( + ra: impl Into, + rb: impl Into, + rc: impl Into, + rd: impl Into, + ) -> Self { + AsmInstruction { + op_name: Ident::new(sway_types::Span::from_string("log".into())), + args: vec![ + Ident::new_no_span(ra.into()), + Ident::new_no_span(rb.into()), + Ident::new_no_span(rc.into()), + Ident::new_no_span(rd.into()), + ], + immediate: None, + metadata: None, + } + } + pub fn lw_no_span( dst: impl Into, src: impl Into, diff --git a/sway-lib-core/src/codec.sw b/sway-lib-core/src/codec.sw index 9ab99efc188..e656bb057f5 100644 --- a/sway-lib-core/src/codec.sw +++ b/sway-lib-core/src/codec.sw @@ -697,6 +697,17 @@ impl AbiEncode for raw_slice { } } +impl AbiEncode for &__slice[T] +where + T: AbiEncode +{ + fn abi_encode(self, buffer: Buffer) -> Buffer { + Buffer { + buffer: __encode_buffer_append(buffer.buffer, self), + } + } +} + impl AbiEncode for [T; 0] where T: AbiEncode, @@ -2533,7 +2544,7 @@ where } } -// Decode +// Decode pub trait AbiDecode { fn abi_decode(ref mut buffer: BufferReader) -> Self; diff --git a/sway-lib-core/src/ops.sw b/sway-lib-core/src/ops.sw index 38e1bc8b068..31c7b9a7bfe 100644 --- a/sway-lib-core/src/ops.sw +++ b/sway-lib-core/src/ops.sw @@ -1260,29 +1260,30 @@ pub fn ok_str_eq() { assert("a" != "b"); } - - -impl Eq for __slice[T] where T: Eq { - fn eq(self, other: Self) -> bool { - let self_qty = self.len(); - let other_qty = other.len(); - - if self_qty != other_qty { - return false - } - - let mut i = 0u64; - while i < self_qty { - let self_elem = __slice_elem(self, i); - let other_elem = __slice_elem(other, i); - - if self_elem != other_elem { - return false; - } - - i += 1; - } - - true - } -} +//impl Eq for __slice[T] +//where +// T: Eq +//{ +// fn eq(self, other: Self) -> bool { +// let self_qty = self.len(); +// let other_qty = other.len(); +// +// if self_qty != other_qty { +// return false +// } +// +// let mut i = 0u64; +// while i < self_qty { +// let self_elem = __slice_elem(self, i); +// let other_elem = __slice_elem(other, i); +// +// if self_elem != other_elem { +// return false; +// } +// +// i += 1; +// } +// +// true +// } +//} diff --git a/sway-lib-core/src/slice.sw b/sway-lib-core/src/slice.sw index 6d5c3131572..10f953d254b 100644 --- a/sway-lib-core/src/slice.sw +++ b/sway-lib-core/src/slice.sw @@ -2,103 +2,18 @@ library; use ::raw_ptr::*; -/// Returns a `__slice[T]` from a pointer and length. -/// -/// # Arguments -/// -/// * `parts`: [(raw_ptr, u64)] - A location in memory and a length to become a `__slice[T]`. -/// -/// # Returns -/// -/// * [__slice[T]] - The newly created `__slice[T]`. -fn from_parts(parts: (raw_ptr, u64)) -> __slice[T] { - asm(ptr: parts) { - ptr: __slice[T] - } -} - -/// Returns a pointer and length from a `__slice[T]`. -/// -/// # Arguments -/// -/// * `slice`: [__slice[T]] - The slice to be broken into its parts. -/// -/// # Returns -/// -/// * [(raw_ptr, u64)] - A tuple of the location in memory of the original `__slice[T]` and its length. -fn into_parts(slice: __slice[T]) -> (raw_ptr, u64) { - asm(ptr: slice) { - ptr: (raw_ptr, u64) - } -} - -impl __slice[T] { - /// Forms a slice from a pointer and a length. - /// - /// # Arguments - /// - /// * `ptr`: [raw_ptr] - The pointer to the location in memory. - /// * `count`: [u64] - The number of `__size_of::` bytes. - /// - /// # Returns - /// - /// * [__slice[T]] - The newly created `__slice[T]`. - /// - /// # Examples - /// - /// ```sway - /// use std::alloc::alloc; - /// - /// fn foo() { - /// let ptr = alloc::(1); - /// let slice = slice::from_parts::(ptr, 1); - /// assert(slice.len() == 1); - /// } - /// ``` - pub fn from_parts(ptr: raw_ptr, count: u64) -> Self { - from_parts((ptr, __mul(count, __size_of::()))) - } - - /// Returns the pointer to the slice. - /// - /// # Returns - /// - /// * [raw_ptr] - The pointer to the location in memory of the `__slice[T]`. - /// - /// # Examples - /// - /// ```sway - /// use std::alloc::alloc; - /// - /// fn foo() { - /// let ptr = alloc::(1); - /// let slice = slice::from_parts::(ptr, 1); - /// let slice_ptr = slice.ptr(); - /// assert(slice_ptr == ptr); - /// } - /// ``` +impl &__slice[T] { pub fn ptr(self) -> raw_ptr { - into_parts(self).0 + let (ptr, _) = asm(s:self) { + s: (raw_ptr, u64) + }; + ptr } - /// Returns the number of elements in the slice. - /// - /// # Returns - /// - /// * [u64] - The length of the slice based on `size_of::`. - /// - /// # Examples - /// - /// ```sway - /// use std::alloc::alloc; - /// - /// fn foo() { - /// let ptr = alloc::(1); - /// let slice = raw_slice::from_parts::(ptr, 1); - /// assert(slice.len() == 1); - /// } - /// ``` pub fn len(self) -> u64 { - __div(into_parts(self).1, __size_of::()) + let (_, len) = asm(s: self) { + s: (raw_ptr, u64) + }; + len } } diff --git a/test/src/e2e_vm_tests/in_language_tests/.gitignore b/test/src/e2e_vm_tests/in_language_tests/.gitignore new file mode 100644 index 00000000000..85059217cde --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/.gitignore @@ -0,0 +1 @@ +Forc.lock diff --git a/test/src/e2e_vm_tests/in_language_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/Forc.toml new file mode 100644 index 00000000000..e015fae4ff0 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/Forc.toml @@ -0,0 +1,43 @@ +[workspace] +members = [ + "test_programs/address_inline_tests", + "test_programs/alloc_inline_tests", + "test_programs/assert_inline_tests", + "test_programs/asset_id_inline_tests", + "test_programs/asset_id_contract_tests", + "test_programs/b512_inline_tests", + "test_programs/block_inline_tests", + "test_programs/bytes_inline_tests", + "test_programs/contract_id_inline_tests", + "test_programs/contract_id_contract_tests", + "test_programs/ecr_inline_tests", + "test_programs/flags_inline_tests", + "test_programs/hash_inline_tests", + "test_programs/identity_inline_tests", + "test_programs/math_inline_tests", + "test_programs/option_inline_tests", + "test_programs/result_inline_tests", + "test_programs/revert_inline_tests", + "test_programs/string_inline_tests", + "test_programs/u128_inline_tests", + "test_programs/vec_inline_tests", + "test_programs/array_conversions_b256_inline_tests", + "test_programs/array_conversions_u16_inline_tests", + "test_programs/array_conversions_u32_inline_tests", + "test_programs/array_conversions_u64_inline_tests", + "test_programs/array_conversions_u256_inline_tests", + "test_programs/bytes_conversions_b256_inline_tests", + "test_programs/bytes_conversions_u16_inline_tests", + "test_programs/bytes_conversions_u32_inline_tests", + "test_programs/bytes_conversions_u64_inline_tests", + "test_programs/bytes_conversions_u256_inline_tests", + "test_programs/primitive_conversions_b256_inline_tests", + "test_programs/primitive_conversions_str_inline_tests", + "test_programs/primitive_conversions_u8_inline_tests", + "test_programs/primitive_conversions_u16_inline_tests", + "test_programs/primitive_conversions_u32_inline_tests", + "test_programs/primitive_conversions_u64_inline_tests", + "test_programs/primitive_conversions_u256_inline_tests", + "test_programs/vm_evm_evm_address_inline_tests", + "test_programs/vm_evm_ecr_inline_tests", +] diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/Forc.toml new file mode 100644 index 00000000000..b4b0a53ec38 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "address_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/src/main.sw new file mode 100644 index 00000000000..f86f63e5a16 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/src/main.sw @@ -0,0 +1,182 @@ +library; + +#[test] +fn address_bits() { + let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let bits1 = address1.bits(); + assert(bits1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + + let address2 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let bits2 = address2.bits(); + assert(bits2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let bits3 = address3.bits(); + assert(bits3 == 0x0000000000000000000000000000000000000000000000000000000000000001); +} + +#[test] +fn address_eq() { + let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let address2 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let address4 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let address5 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let address6 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + assert(address1 == address2); + assert(address3 == address4); + assert(address5 == address6); +} + +#[test] +fn address_ne() { + let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let address2 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let address4 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let address5 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let address6 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + assert(address1 != address3); + assert(address1 != address4); + assert(address1 != address5); + assert(address1 != address6); + assert(address2 != address3); + assert(address2 != address4); + assert(address2 != address5); + assert(address2 != address6); + assert(address3 != address5); + assert(address3 != address6); + assert(address4 != address5); + assert(address4 != address6); +} + +#[test] +fn address_from_b256() { + let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); + assert( + address1 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let address1 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + assert( + address1 + .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + + let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); + assert( + address3 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); +} + +#[test] +fn address_b256_into() { + let b256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000; + let address1: Address = b256_1.into(); + assert( + address1 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let b256_2 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; + let address2: Address = b256_2.into(); + assert( + address2 + .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + + let b256_3 = 0x0000000000000000000000000000000000000000000000000000000000000001; + let address3: Address = b256_3.into(); + assert( + address3 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); +} + +#[test] +fn address_into_b256() { + let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let b256_data1: b256 = address1.into(); + assert( + b256_data1 == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let address2 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let b256_data2: b256 = address2.into(); + assert( + b256_data2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + + let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let b256_data3: b256 = address3.into(); + assert( + b256_data3 == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); +} + +#[test] +fn address_b256_from() { + let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let b256_data1: b256 = b256::from(address1); + assert( + b256_data1 == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let address2 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let b256_data2: b256 = b256::from(address2); + assert( + b256_data2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + + let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let b256_data3: b256 = b256::from(address3); + assert( + b256_data3 == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); +} + +#[test] +fn address_hash() { + use std::hash::{Hash, sha256}; + + let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let digest1 = sha256(address1); + assert(digest1 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); + + let address2 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let digest2 = sha256(address2); + assert(digest2 == 0xaf9613760f72635fbdb44a5a0a63c39f12af30f950a6ee5c971be188e89c4051); + + let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let digest3 = sha256(address3); + assert(digest3 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); + + let address4 = Address::from(0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); + let digest4 = sha256(address4); + assert(digest4 != 0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); +} + +#[test] +fn address_zero() { + let address = Address::zero(); + assert( + address + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); +} + +#[test] +fn address_is_zero() { + let zero_address = Address::zero(); + assert(zero_address.is_zero()); + + let address_2 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); + assert(!address_2.is_zero()); + + let address_3 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + assert(!address_3.is_zero()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/Forc.toml new file mode 100644 index 00000000000..aa8391756f5 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "alloc_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/src/main.sw new file mode 100644 index 00000000000..54c9536290f --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/src/main.sw @@ -0,0 +1,333 @@ +library; + +use std::alloc::*; + +#[test] +fn alloc_alloc() { + // Alloc zero + let zero_ptr = alloc::(0); + assert(!zero_ptr.is_null()); + + // Can alloc u8 + let u8_ptr = alloc::(1); + assert(u8_ptr.read::() == 0u8); + u8_ptr.write(u8::max()); + assert(u8_ptr.read::() == u8::max()); + + // Can alloc u64 + let u64_ptr = alloc::(1); + assert(u64_ptr.read::() == 0u64); + u64_ptr.write(u64::max()); + assert(u64_ptr.read::() == u64::max()); + + // Can alloc b256 + let b256_ptr = alloc::(1); + assert(b256_ptr.read::() == b256::zero()); + b256_ptr.write(b256::max()); + assert(b256_ptr.read::() == b256::max()); + + // Can alloc struct + let address_ptr = alloc::
(1); + assert(address_ptr.read::
() == Address::zero()); + address_ptr.write(Address::from(b256::max())); + assert(address_ptr.read::
() == Address::from(b256::max())); + + // Can alloc multiple + let count = 1000; + let multiple_u64 = alloc::(count); + let mut iter = 0; + while iter < count { + assert(multiple_u64.add::(iter).read::() == 0u64); + iter += 1; + } + + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // Can alloc array + // let array_ptr = alloc::<[u64; 1]>(2); + // assert(array_ptr.read::() == 0u64); + // assert(array_ptr.add::(1).read::() == 0u64); + // array_ptr.write(u64::max()); + // array_ptr.add::(1).write(u64::max()); + // assert(array_ptr.read::() == u64::max()); + // assert(array_ptr.add::(1).read::() == u64::max()); +} + +#[test(should_revert)] +fn revert_alloc_alloc_does_not_exceed_bounds() { + let u64_ptr = alloc::(1); + assert(u64_ptr.read::() == 0u64); + + let _out_of_bounds = u64_ptr.add::(1).read::(); +} + +#[test] +fn alloc_realloc() { + // realloc from zero + let zero_ptr = alloc::(0); + let realloc_zero_ptr = realloc::(zero_ptr, 0, 1); + assert(realloc_zero_ptr.read::() == u64::zero()); + + // Can realloc u8 + let u8_ptr = alloc::(1); + u8_ptr.write(u8::max()); + let realloc_u8_ptr = realloc::(u8_ptr, 1, 2); + assert(realloc_u8_ptr.read::() == u8::max()); + assert(realloc_u8_ptr.add::(1).read::() == u8::zero()); + + // Can alloc u64 + let u64_ptr = alloc::(1); + u64_ptr.write(u64::max()); + let realloc_u64_ptr = realloc::(u64_ptr, 1, 2); + assert(realloc_u64_ptr.read::() == u64::max()); + assert(realloc_u64_ptr.add::(1).read::() == u64::zero()); + + // Can alloc b256 + let b256_ptr = alloc::(1); + b256_ptr.write(b256::max()); + let realloc_b256_ptr = realloc::(b256_ptr, 1, 2); + assert(realloc_b256_ptr.read::() == b256::max()); + assert(realloc_b256_ptr.add::(1).read::() == b256::zero()); + + // Can alloc struct + let address_ptr = alloc::
(1); + address_ptr.write(Address::from(b256::max())); + let realloc_address_ptr = realloc::
(address_ptr, 1, 2); + assert(realloc_address_ptr.read::
() == Address::from(b256::max())); + assert(realloc_address_ptr.add::
(1).read::
() == Address::zero()); + + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // Can realloc array + // let array_ptr = alloc::<[u64; 1]>(2); + // array_ptr.write(u64::max()); + // array_ptr.add::(1).write(u64::max()); + // let realloc_array_ptr = realloc::<[u64; 1]>(array_ptr, 2, 3); + // assert(realloc_array_ptr.read::() == u64::max()); + // assert(realloc_array_ptr.add::(1).read::() == u64::max()); + // assert(realloc_array_ptr.add::(2).read::() == u64::zero()); + + // Can alloc multiple + let count = 100; + let recount = 1000; + + let multiple_u64 = alloc::(count); + let mut iter = 0; + while iter < count { + multiple_u64.add::(iter).write(u64::max()); + iter += 1; + } + + let realloc_multiple_u64 = realloc::(multiple_u64, count, recount); + let mut iter2 = 0; + while iter2 < count { + assert(realloc_multiple_u64.add::(iter2).read::() == u64::max()); + iter2 += 1; + } + let mut iter3 = count; + while iter3 < recount { + assert(realloc_multiple_u64.add::(iter3).read::() == 0u64); + iter3 += 1; + } + + // Edge cases + + // Realloc to same size + let same_u64_ptr = alloc::(2); + same_u64_ptr.write(u64::max()); + let same_realloc_u64_ptr = realloc::(same_u64_ptr, 2, 2); + assert(same_realloc_u64_ptr.read::() == u64::max()); + assert(same_realloc_u64_ptr.add::(1).read::() == u64::zero()); + assert(same_realloc_u64_ptr == same_u64_ptr); + + // Realloc to less size + let less_u64_ptr = alloc::(2); + less_u64_ptr.write(u64::max()); + let less_realloc_u64_ptr = realloc::(less_u64_ptr, 2, 1); + assert(less_realloc_u64_ptr.read::() == u64::max()); + assert(less_realloc_u64_ptr.add::(1).read::() == u64::zero()); + assert(less_realloc_u64_ptr == less_u64_ptr); + + // Realloc excludes values when count is less then total allocated + let exclude_u64_ptr = alloc::(2); + exclude_u64_ptr.write(u64::max()); + exclude_u64_ptr.add::(1).write(u64::max()); + let exclude_realloc_u64_ptr = realloc::(exclude_u64_ptr, 1, 2); + assert(exclude_realloc_u64_ptr.read::() == u64::max()); + assert(exclude_realloc_u64_ptr.add::(1).read::() == u64::zero()); +} + +#[test(should_revert)] +fn revert_alloc_realloc_when_realloc_unallocated_memory() { + let u64_ptr = alloc::(1); + u64_ptr.write(u64::max()); + + let _realloc_b256_ptr = realloc::(u64_ptr, 2, 3); +} + +#[test] +fn alloc_alloc_bytes() { + // Alloc zero + let zero_ptr = alloc_bytes(0); + assert(!zero_ptr.is_null()); + + // Can alloc u8 + let u8_ptr = alloc_bytes(1); + assert(u8_ptr.read::() == 0u8); + u8_ptr.write(u8::max()); + assert(u8_ptr.read::() == u8::max()); + + // Can alloc u64 + let u64_ptr = alloc_bytes(__size_of::()); + assert(u64_ptr.read::() == 0u64); + u64_ptr.write(u64::max()); + assert(u64_ptr.read::() == u64::max()); + + // Can alloc b256 + let b256_ptr = alloc_bytes(__size_of::()); + assert(b256_ptr.read::() == b256::zero()); + b256_ptr.write(b256::max()); + assert(b256_ptr.read::() == b256::max()); + + // Can alloc struct + let address_ptr = alloc_bytes(__size_of_val::
(Address::zero())); + assert(address_ptr.read::
() == Address::zero()); + address_ptr.write(Address::from(b256::max())); + assert(address_ptr.read::
() == Address::from(b256::max())); + + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // Can alloc array + // let array_ptr = alloc_bytes(__size_of::<[u64; 1]>() * 2); + // assert(array_ptr.read::() == 0u64); + // assert(array_ptr.add::(1).read::() == 0u64); + // array_ptr.write(u64::max()); + // array_ptr.add::(1).write(u64::max()); + // assert(array_ptr.read::() == u64::max()); + // assert(array_ptr.add::(1).read::() == u64::max()); + + // Can alloc multiple + let count = 1000; + let multiple_u64 = alloc_bytes(__size_of::() * count); + let mut iter = 0; + while iter < count { + assert(multiple_u64.add::(iter).read::() == 0u64); + iter += 1; + } +} + +#[test(should_revert)] +fn revert_alloc_alloc_bytes_does_not_exceed_bounds() { + let u64_ptr = alloc_bytes(__size_of::()); + assert(u64_ptr.read::() == 0u64); + + let _out_of_bounds = u64_ptr.add::(1).read::(); +} + +#[test] +fn alloc_realloc_bytes() { + // realloc from zero + let zero_ptr = alloc_bytes(0); + let realloc_zero_ptr = realloc_bytes(zero_ptr, 0, 1); + assert(realloc_zero_ptr.read::() == u8::zero()); + + // Can realloc u8 + let u8_ptr = alloc_bytes(1); + u8_ptr.write(u8::max()); + let realloc_u8_ptr = realloc_bytes(u8_ptr, 1, 2); + assert(realloc_u8_ptr.read::() == u8::max()); + assert(realloc_u8_ptr.add::(1).read::() == u8::zero()); + + // Can alloc u64 + let u64_ptr = alloc_bytes(__size_of::()); + u64_ptr.write(u64::max()); + let realloc_u64_ptr = realloc_bytes(u64_ptr, __size_of::(), __size_of::() * 2); + assert(realloc_u64_ptr.read::() == u64::max()); + assert(realloc_u64_ptr.add::(1).read::() == u64::zero()); + + // Can alloc b256 + let b256_ptr = alloc_bytes(__size_of::()); + b256_ptr.write(b256::max()); + let realloc_b256_ptr = realloc_bytes(b256_ptr, __size_of::(), __size_of::() * 2); + assert(realloc_b256_ptr.read::() == b256::max()); + assert(realloc_b256_ptr.add::(1).read::() == b256::zero()); + + // Can alloc struct + let address_ptr = alloc_bytes(__size_of_val::
(Address::zero())); + address_ptr.write(Address::from(b256::max())); + let realloc_address_ptr = realloc_bytes( + address_ptr, + __size_of_val::
(Address::zero()), + __size_of_val::
(Address::zero()) * 2, + ); + assert(realloc_address_ptr.read::
() == Address::from(b256::max())); + assert(realloc_address_ptr.add::
(1).read::
() == Address::zero()); + + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // Can realloc array + // let array_ptr = alloc_bytes(__size_of::<[u64; 1]>() * 2); + // array_ptr.write(u64::max()); + // array_ptr.add::(1).write(u64::max()); + // let realloc_array_ptr = realloc_bytes(array_ptr, __size_of::<[u64; 1]>() * 2, __size_of::<[u64; 1]>() * 3); + // assert(realloc_array_ptr.read::() == u64::max()); + // assert(realloc_array_ptr.add::(1).read::() == u64::max()); + // assert(realloc_array_ptr.add::(2).read::() == u64::zero()); + + // Can alloc multiple + let count = 100; + let recount = 1000; + + let multiple_u64 = alloc_bytes(__size_of::() * count); + let mut iter = 0; + while iter < count { + multiple_u64.add::(iter).write(u64::max()); + iter += 1; + } + + let realloc_multiple_u64 = realloc_bytes( + multiple_u64, + __size_of::() * count, + __size_of::() * recount, + ); + let mut iter2 = 0; + while iter2 < count { + assert(realloc_multiple_u64.add::(iter2).read::() == u64::max()); + iter2 += 1; + } + let mut iter3 = count; + while iter3 < recount { + assert(realloc_multiple_u64.add::(iter3).read::() == 0u64); + iter3 += 1; + } + + // Edge cases + + // Realloc to same size + let same_u64_ptr = alloc_bytes(__size_of::() * 2); + same_u64_ptr.write(u64::max()); + let same_realloc_u64_ptr = realloc_bytes(same_u64_ptr, __size_of::() * 2, __size_of::() * 2); + assert(same_realloc_u64_ptr.read::() == u64::max()); + assert(same_realloc_u64_ptr.add::(1).read::() == u64::zero()); + assert(same_realloc_u64_ptr == same_u64_ptr); + + // Realloc to less size + let less_u64_ptr = alloc_bytes(__size_of::() * 2); + less_u64_ptr.write(u64::max()); + let less_realloc_u64_ptr = realloc_bytes(less_u64_ptr, __size_of::() * 2, __size_of::() * 1); + assert(less_realloc_u64_ptr.read::() == u64::max()); + assert(less_realloc_u64_ptr.add::(1).read::() == u64::zero()); + assert(less_realloc_u64_ptr == less_u64_ptr); + + // Realloc excludes values when count is less then total allocated + let exclude_u64_ptr = alloc_bytes(__size_of::() * 2); + exclude_u64_ptr.write(u64::max()); + exclude_u64_ptr.add::(1).write(u64::max()); + let exclude_realloc_u64_ptr = realloc_bytes(exclude_u64_ptr, __size_of::(), __size_of::() * 2); + assert(exclude_realloc_u64_ptr.read::() == u64::max()); + assert(exclude_realloc_u64_ptr.add::(1).read::() == u64::zero()); +} + +#[test(should_revert)] +fn revert_alloc_realloc_bytes_when_realloc_unallocated_memory() { + let u64_ptr = alloc_bytes(__size_of::()); + u64_ptr.write(u64::max()); + + let _realloc_b256_ptr = realloc_bytes(u64_ptr, __size_of::() * 2, __size_of::() * 3); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/Forc.toml new file mode 100644 index 00000000000..5ef3222bb22 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "array_conversions_b256_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/src/main.sw new file mode 100644 index 00000000000..31813be5546 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/src/main.sw @@ -0,0 +1,55 @@ +library; + +use std::array_conversions::b256::*; + +#[test] +fn b256_from_le_bytes() { + let bytes = [ + 32_u8, 31_u8, 30_u8, 29_u8, 28_u8, 27_u8, 26_u8, 25_u8, 24_u8, 23_u8, 22_u8, + 21_u8, 20_u8, 19_u8, 18_u8, 17_u8, 16_u8, 15_u8, 14_u8, 13_u8, 12_u8, 11_u8, + 10_u8, 9_u8, 8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8, 2_u8, 1_u8, + ]; + + let x = b256::from_le_bytes(bytes); + + assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20); +} + +#[test] +fn b256_to_le_bytes() { + let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; + + let bytes = x.to_le_bytes(); + + let mut i: u8 = 0; + while i < 32_u8 { + assert(bytes[i.as_u64()] == 32_u8 - i); + i += 1_u8; + } +} + +#[test] +fn b256_from_be_bytes() { + let bytes = [ + 1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8, 9_u8, 10_u8, 11_u8, 12_u8, + 13_u8, 14_u8, 15_u8, 16_u8, 17_u8, 18_u8, 19_u8, 20_u8, 21_u8, 22_u8, 23_u8, + 24_u8, 25_u8, 26_u8, 27_u8, 28_u8, 29_u8, 30_u8, 31_u8, 32_u8, + ]; + + let x = b256::from_be_bytes(bytes); + + assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20); +} + +#[test] +fn b256_to_be_bytes() { + let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; + + let bytes = x.to_be_bytes(); + + let mut i: u8 = 0; + while i < 32_u8 { + assert(bytes[i.as_u64()] == i + 1_u8); + i += 1_u8; + } +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/Forc.toml new file mode 100644 index 00000000000..97f65521625 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "array_conversions_u16_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/src/main.sw new file mode 100644 index 00000000000..89ececca4b0 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/src/main.sw @@ -0,0 +1,37 @@ +library; + +use std::array_conversions::u16::*; + +#[test] +fn u16_to_le_bytes() { + let x: u16 = 513; + let result = x.to_le_bytes(); + + assert(result[0] == 1_u8); + assert(result[1] == 2_u8); +} + +#[test] +fn u16_from_le_bytes() { + let bytes = [1_u8, 2_u8]; + let result = u16::from_le_bytes(bytes); + + assert(result == 513_u16); +} + +#[test] +fn u16_to_be_bytes() { + let x: u16 = 513; + let result = x.to_be_bytes(); + + assert(result[0] == 2_u8); + assert(result[1] == 1_u8); +} + +#[test] +fn u16_from_be_bytes() { + let bytes = [2_u8, 1_u8]; + let result = u16::from_be_bytes(bytes); + + assert(result == 513_u16); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/Forc.toml new file mode 100644 index 00000000000..2632e8c5fdc --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "array_conversions_u256_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/src/main.sw new file mode 100644 index 00000000000..c30248efbf5 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/src/main.sw @@ -0,0 +1,55 @@ +library; + +use std::array_conversions::u256::*; + +#[test] +fn u256_from_le_bytes() { + let bytes = [ + 32_u8, 31_u8, 30_u8, 29_u8, 28_u8, 27_u8, 26_u8, 25_u8, 24_u8, 23_u8, 22_u8, + 21_u8, 20_u8, 19_u8, 18_u8, 17_u8, 16_u8, 15_u8, 14_u8, 13_u8, 12_u8, 11_u8, + 10_u8, 9_u8, 8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8, 2_u8, 1_u8, + ]; + + let x = u256::from_le_bytes(bytes); + + assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256); +} + +#[test] +fn u256_to_le_bytes() { + let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256; + + let bytes = x.to_le_bytes(); + + let mut i: u8 = 0; + while i < 32_u8 { + assert(bytes[i.as_u64()] == 32_u8 - i); + i += 1_u8; + } +} + +#[test] +fn u256_from_be_bytes() { + let bytes = [ + 1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8, 9_u8, 10_u8, 11_u8, 12_u8, + 13_u8, 14_u8, 15_u8, 16_u8, 17_u8, 18_u8, 19_u8, 20_u8, 21_u8, 22_u8, 23_u8, + 24_u8, 25_u8, 26_u8, 27_u8, 28_u8, 29_u8, 30_u8, 31_u8, 32_u8, + ]; + + let x = u256::from_be_bytes(bytes); + + assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256); +} + +#[test] +fn u256_to_be_bytes() { + let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256; + + let bytes = x.to_be_bytes(); + + let mut i: u8 = 0; + while i < 32_u8 { + assert(bytes[i.as_u64()] == i + 1_u8); + i += 1_u8; + } +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/Forc.toml new file mode 100644 index 00000000000..c695b8533fe --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "array_conversions_u32_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/src/main.sw new file mode 100644 index 00000000000..2fa1edd90f6 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/src/main.sw @@ -0,0 +1,41 @@ +library; + +use std::array_conversions::u32::*; + +#[test] +fn u32_to_le_bytes() { + let x: u32 = 67305985; + let result = x.to_le_bytes(); + + assert(result[0] == 1_u8); + assert(result[1] == 2_u8); + assert(result[2] == 3_u8); + assert(result[3] == 4_u8); +} + +#[test] +fn u32_from_le_bytes() { + let bytes = [1_u8, 2_u8, 3_u8, 4_u8]; + let result = u32::from_le_bytes(bytes); + + assert(result == 67305985_u32); +} + +#[test] +fn u32_to_be_bytes() { + let x: u32 = 67305985; + let result = x.to_be_bytes(); + + assert(result[0] == 4_u8); + assert(result[1] == 3_u8); + assert(result[2] == 2_u8); + assert(result[3] == 1_u8); +} + +#[test] +fn u32_from_be_bytes() { + let bytes = [4_u8, 3_u8, 2_u8, 1_u8]; + let result = u32::from_be_bytes(bytes); + + assert(result == 67305985_u32); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/Forc.toml new file mode 100644 index 00000000000..556899c47c4 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "array_conversions_u64_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/src/main.sw new file mode 100644 index 00000000000..bafa36017ab --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/src/main.sw @@ -0,0 +1,49 @@ +library; + +use std::array_conversions::u64::*; + +#[test] +fn u64_to_le_bytes() { + let x: u64 = 578437695752307201; + let result = x.to_le_bytes(); + + assert(result[0] == 1_u8); + assert(result[1] == 2_u8); + assert(result[2] == 3_u8); + assert(result[3] == 4_u8); + assert(result[4] == 5_u8); + assert(result[5] == 6_u8); + assert(result[6] == 7_u8); + assert(result[7] == 8_u8); +} + +#[test] +fn u64_from_le_bytes() { + let bytes = [1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8]; + let result = u64::from_le_bytes(bytes); + + assert(result == 578437695752307201); +} + +#[test] +fn u64_to_be_bytes() { + let x: u64 = 578437695752307201; + let result = x.to_be_bytes(); + + assert(result[0] == 8_u8); + assert(result[1] == 7_u8); + assert(result[2] == 6_u8); + assert(result[3] == 5_u8); + assert(result[4] == 4_u8); + assert(result[5] == 3_u8); + assert(result[6] == 2_u8); + assert(result[7] == 1_u8); +} + +#[test] +fn u64_from_be_bytes() { + let bytes = [8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8, 2_u8, 1_u8]; + let result = u64::from_be_bytes(bytes); + + assert(result == 578437695752307201); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/Forc.toml new file mode 100644 index 00000000000..71bac6ffeaf --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "assert_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/src/main.sw new file mode 100644 index 00000000000..916bd7fea4a --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/src/main.sw @@ -0,0 +1,171 @@ +library; + +#[test] +fn assert_assert() { + assert(true); + assert(1 == 1); + assert(1 + 1 == 2); + assert(!false); + assert(true && true); + assert(true || false); + assert(!false && !false); +} + +#[test(should_revert)] +fn revert_assert_assert_when_not_true() { + assert(false); +} + +#[test] +fn assert_assert_eq() { + use std::bytes::Bytes; + + // assert_eq u64 + let a = 42; + let b = 40 + 2; + assert_eq(a, b); + + // assert_eq u32 + let c = 42u32; + let d = 40u32 + 2u32; + assert_eq(c, d); + + // assert_eq u16 + let e = 42u16; + let f = 40u16 + 2u16; + assert_eq(e, f); + + // assert_eq u8 + let g = 42u8; + let h = 40u8 + 2u8; + assert_eq(g, h); + + // assert_eq b256 + let i: b256 = 0b0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000010; + let j: b256 = 0b1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000001 << 1; + assert_eq(i, j); + + // assert_eq u256 + let k: u256 = 0x02u256; + let l: u256 = 0x01u256 + 0x01u256; + assert_eq(k, l); + + // assert_eq struct + let value = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF; + let m = Address::from(value); + let n = Address::from(value); + assert_eq(m, n); + + // assert_eq heap + let mut o = Bytes::new(); + let mut p = Bytes::new(); + o.push(42u8); + o.push(11u8); + o.push(69u8); + p.push(42u8); + p.push(11u8); + p.push(69u8); + assert_eq(o, p); + + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved. + // assert_eq array + // let value = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF; + // let mut q: [u64; 3] = [0, 0, 0]; + // let mut r: [u64; 3] = [0, 0, 0]; + // q[0] = 1; + // q[1] = 2; + // q[2] = 3; + // r[0] = 1; + // r[1] = 2; + // r[2] = 3; + // assert_eq(q, r); + + // assert_eq enum + let value = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF; + let s = Identity::Address(Address::from(value)); + let t = Identity::Address(Address::from(value)); + let u = Identity::ContractId(ContractId::from(value)); + let v = Identity::ContractId(ContractId::from(value)); + assert_eq(s, t); + assert_eq(u, v); +} + +#[test(should_revert)] +fn revert_assert_assert_eq() { + assert_eq(1, 2); +} + +#[test] +fn assert_assert_ne() { + use std::bytes::Bytes; + + // assert_ne u64 + let a = 42; + let b = 40; + assert_ne(a, b); + + // assert_ne u32 + let c = 42u32; + let d = 40u32; + assert_ne(c, d); + + // assert_ne u16 + let e = 42u16; + let f = 40u16; + assert_ne(e, f); + + // assert_ne u8 + let g = 42u8; + let h = 40u8; + assert_ne(g, h); + + // assert_ne b256 + let i: b256 = 0b0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000010; + let j: b256 = 0b1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000001; + assert_ne(i, j); + + // assert_ne u256 + let k: u256 = 0x02u256; + let l: u256 = 0x01u256; + assert_ne(k, l); + + // assert_ne struct + let value = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF; + let value2 = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEE; + let m = Address::from(value); + let n = Address::from(value2); + assert_ne(m, n); + + // test_assert_ne heap + let mut o = Bytes::new(); + let mut p = Bytes::new(); + o.push(42u8); + o.push(11u8); + o.push(69u8); + p.push(42u8); + p.push(11u8); + p.push(70u8); + assert_ne(o, p); + + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved. + // assert_ne array + // let mut q: [u64; 3] = [1, 2, 3]; + // let mut r: [u64; 3] = [0, 0, 0]; + // assert_eq(q, r); + + // assert_ne enum + let value = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF; + let s = Identity::Address(Address::from(value)); + let t = Identity::Address(Address::from(value)); + let u = Identity::ContractId(ContractId::from(value)); + let v = Identity::ContractId(ContractId::from(value)); + assert_ne(u, t); + assert_ne(s, v); + assert_ne(s, u); + assert_ne(v, t); +} + +#[test(should_revert)] +fn revert_assert_assert_ne() { + assert_ne(1, 1); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/Forc.toml new file mode 100644 index 00000000000..646e9e12e7f --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "asset_id_contract_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/src/main.sw new file mode 100644 index 00000000000..16e895c99c9 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/src/main.sw @@ -0,0 +1,22 @@ +contract; + +abi AssetIdTestContract { + fn default_asset_id() -> AssetId; +} + +impl AssetIdTestContract for Contract { + fn default_asset_id() -> AssetId { + AssetId::default() + } +} + +#[test] +fn asset_id_default() { + let contract_id = ContractId::from(CONTRACT_ID); + let asset_id_test_abi = abi(AssetIdTestContract, contract_id.bits()); + + let result_asset_id = asset_id_test_abi.default_asset_id(); + let computed_asset_id = AssetId::new(contract_id, SubId::zero()); + + assert(result_asset_id == computed_asset_id); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/Forc.toml new file mode 100644 index 00000000000..6f1fff646d1 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "asset_id_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/src/main.sw new file mode 100644 index 00000000000..057bf1fe5a5 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/src/main.sw @@ -0,0 +1,252 @@ +library; + +#[test] +fn asset_id_hasher() { + use std::hash::{Hash, sha256}; + + let asset_1 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let digest_1 = sha256(asset_1); + assert(digest_1 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); + + let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let digest_2 = sha256(asset_2); + assert(digest_2 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); + + let asset_3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let digest3 = sha256(asset_3); + assert(digest3 == 0xaf9613760f72635fbdb44a5a0a63c39f12af30f950a6ee5c971be188e89c4051); +} + +#[test] +fn asset_id_eq() { + let asset_1 = AssetId::zero(); + let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let asset_3 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let asset_4 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let asset_5 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let asset_6 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + assert(asset_1 == asset_2); + assert(asset_3 == asset_4); + assert(asset_5 == asset_6); +} + +#[test] +fn asset_id_ne() { + let asset_1 = AssetId::zero(); + let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let asset_3 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let asset_4 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let asset_5 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let asset_6 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + assert(asset_1 != asset_3); + assert(asset_1 != asset_4); + assert(asset_1 != asset_5); + assert(asset_1 != asset_6); + assert(asset_2 != asset_3); + assert(asset_2 != asset_4); + assert(asset_2 != asset_5); + assert(asset_2 != asset_6); + assert(asset_3 != asset_5); + assert(asset_3 != asset_6); +} + +#[test] +fn asset_id_from_b256() { + let asset1 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + assert( + asset1 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let asset2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + assert( + asset2 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + + let asset3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + assert( + asset3 + .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); +} + +#[test] +fn asset_id_b256_into() { + let b256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000; + let asset_1: AssetId = b256_1.into(); + assert( + asset_1 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let b256_2 = 0x0000000000000000000000000000000000000000000000000000000000000001; + let asset_2: AssetId = b256_2.into(); + assert( + asset_2 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + + let b256_3 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; + let asset_3: AssetId = b256_3.into(); + assert( + asset_3 + .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); +} + +#[test] +fn asset_id_into_b256() { + let asset_1 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let b256_1: b256 = asset_1.into(); + assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + + let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let b256_2: b256 = asset_2.into(); + assert(b256_2 == 0x0000000000000000000000000000000000000000000000000000000000000001); + + let asset_3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let b256_3: b256 = asset_3.into(); + assert(b256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); +} + +#[test] +fn asset_id_b256_from() { + let asset_1 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let b256_1: b256 = b256::from(asset_1); + assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + + let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let b256_2: b256 = b256::from(asset_2); + assert(b256_2 == 0x0000000000000000000000000000000000000000000000000000000000000001); + + let asset_3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let b256_3: b256 = b256::from(asset_3); + assert(b256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); +} + +#[test] +fn asset_id_new() { + let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let sub_id_1 = SubId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let asset_1 = AssetId::new(contract_id_1, sub_id_1); + assert( + asset_1 + .bits() == 0xf5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b, + ); + + let contract_id_2 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let sub_id_2 = SubId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let asset_2 = AssetId::new(contract_id_2, sub_id_2); + assert( + asset_2 + .bits() == 0x58e8f2a1f78f0a591feb75aebecaaa81076e4290894b1c445cc32953604db089, + ); + + let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let sub_id_3 = SubId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let asset_3 = AssetId::new(contract_id_3, sub_id_3); + assert( + asset_3 + .bits() == 0x90f4b39548df55ad6187a1d20d731ecee78c545b94afd16f42ef7592d99cd365, + ); + + let contract_id_4 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let sub_id_4 = SubId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let asset_4 = AssetId::new(contract_id_4, sub_id_4); + assert( + asset_4 + .bits() == 0xa5de9b714accd8afaaabf1cbd6e1014c9d07ff95c2ae154d91ec68485b31e7b5, + ); + + let contract_id_5 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let sub_id_5 = SubId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let asset_5 = AssetId::new(contract_id_5, sub_id_5); + assert( + asset_5 + .bits() == 0xbba91ca85dc914b2ec3efb9e16e7267bf9193b14350d20fba8a8b406730ae30a, + ); + + let contract_id_6 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let sub_id_6 = SubId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let asset_6 = AssetId::new(contract_id_6, sub_id_6); + assert( + asset_6 + .bits() == 0x8667e718294e9e0df1d30600ba3eeb201f764aad2dad72748643e4a285e1d1f7, + ); + + let contract_id_7 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let sub_id_7 = SubId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let asset_7 = AssetId::new(contract_id_7, sub_id_7); + assert( + asset_7 + .bits() == 0x4ab9077c34a6903bc59693414a4fe8ccf275d93f2daacd849574933737c27757, + ); + + let contract_id_8 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let sub_id_8 = SubId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let asset_8 = AssetId::new(contract_id_8, sub_id_8); + assert( + asset_8 + .bits() == 0x2a9bb11102924faefcdbd39baa7858c5f5e49ed2a4205f6759c4a8648bee2942, + ); +} + +#[test] +fn asset_id_default_not_in_contract() { + // Because this is not within a contract context, this will return erroneous data + let _asset = AssetId::default(); +} + +#[test] +fn asset_id_base() { + let base_asset = AssetId::base(); + assert( + base_asset + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); +} + +#[test] +fn asset_id_bits() { + let asset1 = AssetId::zero(); + assert( + asset1 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let asset2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + assert( + asset2 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + + let asset3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + assert( + asset3 + .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); +} + +#[test] +fn asset_id_zero() { + let zero_asset = AssetId::zero(); + assert( + zero_asset + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); +} + +#[test] +fn asset_id_is_zero() { + let zero_asset = AssetId::zero(); + assert(zero_asset.is_zero()); + + let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + assert(!asset_2.is_zero()); + + let asset_3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + assert(!asset_3.is_zero()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/Forc.toml new file mode 100644 index 00000000000..3eb4a563a83 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "b512_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/src/main.sw new file mode 100644 index 00000000000..8c5ef0c4381 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/src/main.sw @@ -0,0 +1,284 @@ +library; + +use std::b512::B512; + +#[test] +fn b512_eq() { + let b512_1 = B512::zero(); + let b512_2 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + )); + let b512_3 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + )); + let b512_4 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + )); + let b512_5 = B512::from(( + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + )); + let b512_6 = B512::from(( + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + )); + + assert(b512_1 == b512_2); + assert(b512_3 == b512_4); + assert(b512_5 == b512_6); +} + +#[test] +fn b512_ne() { + let b512_1 = B512::zero(); + let b512_2 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + )); + let b512_3 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + )); + let b512_4 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + )); + let b512_5 = B512::from(( + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + )); + let b512_6 = B512::from(( + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + )); + + assert(b512_1 != b512_3); + assert(b512_1 != b512_4); + assert(b512_1 != b512_5); + assert(b512_1 != b512_6); + assert(b512_2 != b512_3); + assert(b512_2 != b512_4); + assert(b512_2 != b512_5); + assert(b512_2 != b512_6); + assert(b512_3 != b512_5); + assert(b512_3 != b512_6); +} + +#[test] +fn b512_from_b256() { + let b256_tuple_1: (b256, b256) = ( + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + let b512_1 = B512::from(b256_tuple_1); + assert( + b512_1 + .bits()[0] == 0x0000000000000000000000000000000000000000000000000000000000000000 && b512_1 + .bits()[1] == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let b256_tuple_2: (b256, b256) = ( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + let b512_2 = B512::from(b256_tuple_2); + assert( + b512_2 + .bits()[0] == 0x0000000000000000000000000000000000000000000000000000000000000001 && b512_2 + .bits()[1] == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + + let b256_tuple_3: (b256, b256) = ( + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + let b512_3 = B512::from(b256_tuple_3); + assert( + b512_3 + .bits()[0] == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && b512_3 + .bits()[1] == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); +} + +#[test] +fn b512_into_b256() { + let b256_tuple_1: (b256, b256) = ( + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + let b512_1: B512 = b256_tuple_1.into(); + assert( + b512_1 + .bits()[0] == 0x0000000000000000000000000000000000000000000000000000000000000000 && b512_1 + .bits()[1] == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let b256_tuple_2: (b256, b256) = ( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + let b512_2: B512 = b256_tuple_2.into(); + assert( + b512_2 + .bits()[0] == 0x0000000000000000000000000000000000000000000000000000000000000001 && b512_2 + .bits()[1] == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + + let b256_tuple_3: (b256, b256) = ( + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + let b512_3: B512 = b256_tuple_3.into(); + assert( + b512_3 + .bits()[0] == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && b512_3 + .bits()[1] == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); +} + +#[test] +fn b512_b256_from() { + // Glob operator needed for From for (b256, b256) + use std::b512::*; + + let b512_1 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + )); + let b256_tuple_1: (b256, b256) = b512_1.into(); + assert( + b256_tuple_1.0 == 0x0000000000000000000000000000000000000000000000000000000000000000 && b256_tuple_1.1 == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let b512_2 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + )); + let b256_tuple_2: (b256, b256) = b512_2.into(); + assert( + b256_tuple_2.0 == 0x0000000000000000000000000000000000000000000000000000000000000001 && b256_tuple_2.1 == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + + let b512_3 = B512::from(( + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + )); + let b256_tuple_3: (b256, b256) = b512_3.into(); + assert( + b256_tuple_3.0 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && b256_tuple_3.1 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); +} + +#[test] +fn b512_b256_into() { + // Glob operator needed for From for (b256, b256) + use std::b512::*; + + let b512_1 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + )); + let b256_tuple_1: (b256, b256) = <(b256, b256) as From>::from(b512_1); + assert( + b256_tuple_1.0 == 0x0000000000000000000000000000000000000000000000000000000000000000 && b256_tuple_1.1 == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let b512_2 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + )); + let b256_tuple_2: (b256, b256) = <(b256, b256) as From>::from(b512_2); + assert( + b256_tuple_2.0 == 0x0000000000000000000000000000000000000000000000000000000000000001 && b256_tuple_2.1 == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + + let b512_3 = B512::from(( + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + )); + let b256_tuple_3: (b256, b256) = <(b256, b256) as From>::from(b512_3); + assert( + b256_tuple_3.0 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && b256_tuple_3.1 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); +} + +#[test] +fn b512_new() { + let new_b512 = B512::new(); + assert(new_b512.bits()[0] == b256::zero() && new_b512.bits()[1] == b256::zero()); +} + +#[test] +fn b512_bits() { + let b256_tuple_1: (b256, b256) = ( + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + let b256_array_1: [b256; 2] = [ + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + ]; + let b512_1 = B512::from(b256_tuple_1); + assert(b512_1.bits()[0] == b256_array_1[0] && b512_1.bits()[1] == b256_array_1[1]); + + let b256_tuple_2: (b256, b256) = ( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + let b256_array_2: [b256; 2] = [ + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + ]; + let b512_2 = B512::from(b256_tuple_2); + assert(b512_2.bits()[0] == b256_array_2[0] && b512_2.bits()[1] == b256_array_2[1]); + + let b256_tuple_3: (b256, b256) = ( + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + let b256_array_3: [b256; 2] = [ + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ]; + let b512_3 = B512::from(b256_tuple_3); + assert(b512_3.bits()[0] == b256_array_3[0] && b512_3.bits()[1] == b256_array_3[1]); +} + +#[test] +fn b512_zero() { + let zero_b512 = B512::zero(); + assert(zero_b512.bits()[0] == b256::zero() && zero_b512.bits()[1] == b256::zero()); +} + +#[test] +fn b512_is_zero() { + let zero_b512 = B512::zero(); + assert(zero_b512.is_zero()); + + let b512_1 = B512::from(( + b256::zero(), + 0x0000000000000000000000000000000000000000000000000000000000000001, + )); + assert(!b512_1.is_zero()); + + let b512_2 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000001, + b256::zero(), + )); + assert(!b512_2.is_zero()); + + let b512_3 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000001, + )); + assert(!b512_3.is_zero()); + + let b512_4 = B512::from(( + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + )); + assert(!b512_4.is_zero()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/Forc.toml new file mode 100644 index 00000000000..4d1f61ac902 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "block_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/src/main.sw new file mode 100644 index 00000000000..c89fb10d65a --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/src/main.sw @@ -0,0 +1,55 @@ +library; + +use std::block::{block_header_hash, BlockHashError, height, timestamp, timestamp_of_block}; + +#[test] +fn block_height() { + let h = height(); + assert(h >= 1u32); +} + +#[test] +fn block_timestamp() { + let time = timestamp(); + assert(time >= 1); +} + +#[test] +fn block_timestamp_of_block() { + let time = timestamp_of_block(height()); + assert(time >= 1); +} + +#[test] +fn block_block_header_hash() { + let result = block_header_hash(height()); + assert(result.is_ok()); + + let hash = result.unwrap(); + assert(hash != b256::zero()); +} + +#[test(should_revert)] +fn revert_block_header_hash_err_current_height() { + // Get the header hash of the current block. Each time this test runs, the block height will be 1. calling BHSH with a height >= current height will fail. + let mut hash = block_header_hash(height()); + let correct_error = match hash { + Ok(_) => false, + Err(BlockHashError::BlockHeightTooHigh) => true, + }; + + assert(correct_error); +} + +#[test(should_revert)] +fn revert_block_header_hash_err_future_height() { + // Try to get header hash of a block in the future + // The function should return a BlockHashError + let hash = block_header_hash(height() + 1u32); + let correct_error = match hash { + Ok(_) => false, + Err(BlockHashError::BlockHeightTooHigh) => true, + }; + + assert(correct_error); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/Forc.toml new file mode 100644 index 00000000000..58a5ac2c835 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "bytes_conversions_b256_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/src/main.sw new file mode 100644 index 00000000000..2d3bc2fcf3b --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/src/main.sw @@ -0,0 +1,58 @@ +library; + +use std::{bytes::Bytes, bytes_conversions::b256::*}; + +#[test] +fn b256_from_le_bytes() { + let mut bytes = Bytes::with_capacity(32); + let mut i: u8 = 0; + while i < 32_u8 { + bytes.push(32_u8 - i); + i += 1_u8; + } + + let x = b256::from_le_bytes(bytes); + + assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20); +} + +#[test] +fn b256_to_le_bytes() { + let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; + + let bytes = x.to_le_bytes(); + + let mut i: u8 = 0; + while i < 32_u8 { + assert(bytes.get(i.as_u64()).unwrap() == 32_u8 - i); + i += 1_u8; + } +} + +#[test] +fn b256_from_be_bytes() { + let mut bytes = Bytes::with_capacity(32); + + let mut i: u8 = 0; + while i < 32_u8 { + bytes.push(i + 1_u8); + i += 1_u8; + } + + let x = b256::from_be_bytes(bytes); + + assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20); +} + +#[test] +fn b256_to_be_bytes() { + let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; + + let bytes = x.to_be_bytes(); + + let mut i: u8 = 0; + while i < 32_u8 { + assert(bytes.get(i.as_u64()).unwrap() == i + 1_u8); + i += 1_u8; + } +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/Forc.toml new file mode 100644 index 00000000000..7e52a229ec8 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "bytes_conversions_u16_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/src/main.sw new file mode 100644 index 00000000000..2f0c323c358 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/src/main.sw @@ -0,0 +1,41 @@ +library; + +use std::{bytes::Bytes, bytes_conversions::u16::*}; + +#[test] +fn u16_to_le_bytes() { + let x: u16 = 513; + let result = x.to_le_bytes(); + + assert(result.get(0).unwrap() == 1_u8); + assert(result.get(1).unwrap() == 2_u8); +} + +#[test] +fn u16_from_le_bytes() { + let mut bytes = Bytes::new(); + bytes.push(1_u8); + bytes.push(2_u8); + let result = u16::from_le_bytes(bytes); + + assert(result == 513_u16); +} + +#[test] +fn u16_to_be_bytes() { + let x: u16 = 513; + let result = x.to_be_bytes(); + + assert(result.get(0).unwrap() == 2_u8); + assert(result.get(1).unwrap() == 1_u8); +} + +#[test] +fn u16_from_be_bytes() { + let mut bytes = Bytes::new(); + bytes.push(2_u8); + bytes.push(1_u8); + let result = u16::from_be_bytes(bytes); + + assert(result == 513_u16); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/Forc.toml new file mode 100644 index 00000000000..ea3ff0fb77f --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "bytes_conversions_u256_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/src/main.sw new file mode 100644 index 00000000000..02f4849056e --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/src/main.sw @@ -0,0 +1,58 @@ +library; + +use std::{bytes::Bytes, bytes_conversions::u256::*}; + +#[test] +fn u256_from_le_bytes() { + let mut bytes = Bytes::with_capacity(32); + let mut i: u8 = 0; + while i < 32_u8 { + bytes.push(32_u8 - i); + i += 1_u8; + } + + let x = u256::from_le_bytes(bytes); + + assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256); +} + +#[test] +fn u256_to_le_bytes() { + let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256; + + let bytes = x.to_le_bytes(); + + let mut i: u8 = 0; + while i < 32_u8 { + assert(bytes.get(i.as_u64()).unwrap() == 32_u8 - i); + i += 1_u8; + } +} + +#[test] +fn u256_from_be_bytes() { + let mut bytes = Bytes::with_capacity(32); + + let mut i: u8 = 0; + while i < 32_u8 { + bytes.push(i + 1_u8); + i += 1_u8; + } + + let x = u256::from_be_bytes(bytes); + + assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256); +} + +#[test] +fn u256_to_be_bytes() { + let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256; + + let bytes = x.to_be_bytes(); + + let mut i: u8 = 0; + while i < 32_u8 { + assert(bytes.get(i.as_u64()).unwrap() == i + 1_u8); + i += 1_u8; + } +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/Forc.toml new file mode 100644 index 00000000000..48f9066f8ec --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "bytes_conversions_u32_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/src/main.sw new file mode 100644 index 00000000000..f4dcc89cfec --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/src/main.sw @@ -0,0 +1,49 @@ +library; + +use std::{bytes::Bytes, bytes_conversions::u32::*}; + +#[test] +fn u32_to_le_bytes() { + let x: u32 = 67305985; + let result = x.to_le_bytes(); + + assert(result.get(0).unwrap() == 1_u8); + assert(result.get(1).unwrap() == 2_u8); + assert(result.get(2).unwrap() == 3_u8); + assert(result.get(3).unwrap() == 4_u8); +} + +#[test] +fn u32_from_le_bytes() { + let mut bytes = Bytes::new(); + bytes.push(1_u8); + bytes.push(2_u8); + bytes.push(3_u8); + bytes.push(4_u8); + let result = u32::from_le_bytes(bytes); + + assert(result == 67305985_u32); +} + +#[test] +fn u32_to_be_bytes() { + let x: u32 = 67305985; + let result = x.to_be_bytes(); + + assert(result.get(0).unwrap() == 4_u8); + assert(result.get(1).unwrap() == 3_u8); + assert(result.get(2).unwrap() == 2_u8); + assert(result.get(3).unwrap() == 1_u8); +} + +#[test] +fn u32_from_be_bytes() { + let mut bytes = Bytes::new(); + bytes.push(4_u8); + bytes.push(3_u8); + bytes.push(2_u8); + bytes.push(1_u8); + let result = u32::from_be_bytes(bytes); + + assert(result == 67305985_u32); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/Forc.toml new file mode 100644 index 00000000000..3f51fa9d573 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "bytes_conversions_u64_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/src/main.sw new file mode 100644 index 00000000000..9781b1ee663 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/src/main.sw @@ -0,0 +1,65 @@ +library; + +use std::{bytes::Bytes, bytes_conversions::u64::*}; + +#[test] +fn u64_to_be_bytes() { + let x: u64 = 578437695752307201; + let result = x.to_be_bytes(); + + assert(result.get(0).unwrap() == 8_u8); + assert(result.get(1).unwrap() == 7_u8); + assert(result.get(2).unwrap() == 6_u8); + assert(result.get(3).unwrap() == 5_u8); + assert(result.get(4).unwrap() == 4_u8); + assert(result.get(5).unwrap() == 3_u8); + assert(result.get(6).unwrap() == 2_u8); + assert(result.get(7).unwrap() == 1_u8); +} + +#[test] +fn u64_from_be_bytes() { + let mut bytes = Bytes::new(); + bytes.push(8_u8); + bytes.push(7_u8); + bytes.push(6_u8); + bytes.push(5_u8); + bytes.push(4_u8); + bytes.push(3_u8); + bytes.push(2_u8); + bytes.push(1_u8); + let result = u64::from_be_bytes(bytes); + + assert(result == 578437695752307201); +} + +#[test] +fn u64_to_le_bytes() { + let x: u64 = 578437695752307201; + let result = x.to_le_bytes(); + + assert(result.get(0).unwrap() == 1_u8); + assert(result.get(1).unwrap() == 2_u8); + assert(result.get(2).unwrap() == 3_u8); + assert(result.get(3).unwrap() == 4_u8); + assert(result.get(4).unwrap() == 5_u8); + assert(result.get(5).unwrap() == 6_u8); + assert(result.get(6).unwrap() == 7_u8); + assert(result.get(7).unwrap() == 8_u8); +} + +#[test] +fn u64_from_le_bytes() { + let mut bytes = Bytes::new(); + bytes.push(1_u8); + bytes.push(2_u8); + bytes.push(3_u8); + bytes.push(4_u8); + bytes.push(5_u8); + bytes.push(6_u8); + bytes.push(7_u8); + bytes.push(8_u8); + let result = u64::from_le_bytes(bytes); + + assert(result == 578437695752307201); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/Forc.toml new file mode 100644 index 00000000000..ada0818b7ae --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "bytes_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/src/main.sw new file mode 100644 index 00000000000..d5107addd94 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/src/main.sw @@ -0,0 +1,1047 @@ +library; + +use std::bytes::Bytes; + +fn setup() -> (Bytes, u8, u8, u8) { + let mut bytes = Bytes::new(); + let a = 5u8; + let b = 7u8; + let c = 9u8; + bytes.push(a); + bytes.push(b); + bytes.push(c); + (bytes, a, b, c) +} + +#[test()] +fn bytes_new() { + let bytes = Bytes::new(); + assert(bytes.len() == 0); + assert(bytes.capacity() == 0); +} + +#[test] +fn bytes_with_capacity() { + let bytes_1 = Bytes::with_capacity(0); + assert(bytes_1.capacity() == 0); + + let bytes_2 = Bytes::with_capacity(1); + assert(bytes_2.capacity() == 1); + + // 2^6 + let bytes_3 = Bytes::with_capacity(64); + assert(bytes_3.capacity() == 64); + + // 2^11 + let bytes_4 = Bytes::with_capacity(2048); + assert(bytes_4.capacity() == 2048); + + // 2^16 + let bytes_5 = Bytes::with_capacity(65536); + assert(bytes_5.capacity() == 65536); +} + +#[test()] +fn bytes_push() { + let mut bytes = Bytes::new(); + + assert(bytes.len() == 0); + assert(bytes.capacity() == 0); + + bytes.push(1u8); + assert(bytes.len() == 1); + assert(bytes.capacity() == 1); + + bytes.push(2u8); + assert(bytes.len() == 2); + assert(bytes.capacity() == 2); + + // Capacity doubles + bytes.push(3u8); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + + bytes.push(4u8); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); + + // Capacity doubles + bytes.push(5u8); + assert(bytes.len() == 5); + assert(bytes.capacity() == 8); + + bytes.push(6u8); + assert(bytes.len() == 6); + assert(bytes.capacity() == 8); + + bytes.push(7u8); + assert(bytes.len() == 7); + assert(bytes.capacity() == 8); + + bytes.push(8u8); + assert(bytes.len() == 8); + assert(bytes.capacity() == 8); + + // Capacity doubles + bytes.push(9u8); + assert(bytes.len() == 9); + assert(bytes.capacity() == 16); +} + +#[test()] +fn bytes_pop() { + let (mut bytes, a, b, c) = setup(); + assert(bytes.len() == 3); + + bytes.push(42u8); + bytes.push(11u8); + bytes.push(69u8); + bytes.push(100u8); + bytes.push(200u8); + bytes.push(255u8); + bytes.push(180u8); + bytes.push(17u8); + bytes.push(19u8); + assert(bytes.len() == 12); + assert(bytes.capacity() == 16); + + let first = bytes.pop(); + assert(first.unwrap() == 19u8); + assert(bytes.len() == 11); + assert(bytes.capacity() == 16); + + let second = bytes.pop(); + assert(second.unwrap() == 17u8); + assert(bytes.len() == 10); + assert(bytes.capacity() == 16); + + let third = bytes.pop(); + assert(third.unwrap() == 180u8); + assert(bytes.len() == 9); + let _ = bytes.pop(); + let _ = bytes.pop(); + let _ = bytes.pop(); + let _ = bytes.pop(); + let _ = bytes.pop(); + let _ = bytes.pop(); + assert(bytes.len() == 3); + assert(bytes.pop().unwrap() == c); + assert(bytes.pop().unwrap() == b); + assert(bytes.pop().unwrap() == a); + + // Can pop all + assert(bytes.len() == 0); + assert(bytes.capacity() == 16); + assert(bytes.pop().is_none()); +} + +#[test()] +fn bytes_get() { + let (bytes, a, b, c) = setup(); + assert(bytes.len() == 3); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == c); + // get is non-modifying + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == c); + assert(bytes.len() == 3); + + // None if out of bounds + assert(bytes.get(bytes.len()).is_none()); +} + +#[test()] +fn bytes_set() { + let (mut bytes, a, _b, c) = setup(); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + let d = 11u8; + + // Sets in the middle + bytes.set(1, d); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == d); + assert(bytes.get(2).unwrap() == c); +} + +#[test()] +fn bytes_set_twice() { + let (mut bytes, a, _b, c) = setup(); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + let d = 11u8; + let e = 13u8; + + // Sets in the middle + bytes.set(1, d); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == d); + assert(bytes.get(2).unwrap() == c); + + // Twice + bytes.set(1, e); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == e); + assert(bytes.get(2).unwrap() == c); +} + +#[test()] +fn bytes_set_front() { + let (mut bytes, _a, b, c) = setup(); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + let d = 11u8; + + // Sets at the front + bytes.set(0, d); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == d); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == c); +} + +#[test()] +fn bytes_set_back() { + let (mut bytes, a, b, _c) = setup(); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + let d = 11u8; + + // Sets at the back + bytes.set(bytes.len() - 1, d); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == d); +} + +#[test(should_revert)] +fn revert_bytes_set_out_of_bounds() { + let (mut bytes, _a, _b, _c) = setup(); + + bytes.set(bytes.len(), 11u8); +} + +#[test()] +fn bytes_insert() { + let (mut bytes, a, b, c) = setup(); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + + let d = 11u8; + + // Inserts in the middle + bytes.insert(1, d); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == d); + assert(bytes.get(2).unwrap() == b); + assert(bytes.get(3).unwrap() == c); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); +} + +#[test()] +fn bytes_insert_twice() { + let (mut bytes, a, b, c) = setup(); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + + let d = 11u8; + let e = 13u8; + + // Inserts in the middle + bytes.insert(1, d); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == d); + assert(bytes.get(2).unwrap() == b); + assert(bytes.get(3).unwrap() == c); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); + + // Twice + bytes.insert(1, e); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == e); + assert(bytes.get(2).unwrap() == d); + assert(bytes.get(3).unwrap() == b); + assert(bytes.get(4).unwrap() == c); + assert(bytes.len() == 5); + assert(bytes.capacity() == 8); +} + +#[test()] +fn bytes_insert_front() { + let (mut bytes, a, b, c) = setup(); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + + let d = 11u8; + + // Inserts at the front + bytes.insert(0, d); + assert(bytes.get(0).unwrap() == d); + assert(bytes.get(1).unwrap() == a); + assert(bytes.get(2).unwrap() == b); + assert(bytes.get(3).unwrap() == c); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); +} + +#[test()] +fn bytes_insert_before_back() { + let (mut bytes, a, b, c) = setup(); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + + let d = 11u8; + + // Inserts right before the back + bytes.insert(bytes.len() - 1, d); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == d); + assert(bytes.get(3).unwrap() == c); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); +} + +#[test()] +fn bytes_insert_back() { + let (mut bytes, a, b, c) = setup(); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + + let d = 11u8; + + // Inserts at the back + bytes.insert(bytes.len(), d); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == c); + assert(bytes.get(3).unwrap() == d); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); +} + +#[test(should_revert)] +fn revert_bytes_insert_out_of_bounds() { + let (mut bytes, a, _b, _c) = setup(); + + bytes.insert(bytes.len() + 1, a); +} + +#[test()] +fn bytes_remove() { + let (mut bytes, a, b, c) = setup(); + let d = 7u8; + bytes.push(d); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); + + // Remove in the middle + let item1 = bytes.remove(1); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + assert(item1 == b); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == c); + assert(bytes.get(2).unwrap() == d); + assert(bytes.get(3).is_none()); +} + +#[test()] +fn bytes_remove_front() { + let (mut bytes, a, b, c) = setup(); + // Remove at the start + let item = bytes.remove(0); + assert(bytes.len() == 2); + assert(bytes.capacity() == 4); + assert(item == a); + assert(bytes.get(0).unwrap() == b); + assert(bytes.get(1).unwrap() == c); + assert(bytes.get(2).is_none()); +} + +#[test()] +fn bytes_remove_end() { + let (mut bytes, a, b, c) = setup(); + // Remove at the end + let item = bytes.remove(bytes.len() - 1); + assert(bytes.len() == 2); + assert(bytes.capacity() == 4); + assert(item == c); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).is_none()); +} + +#[test()] +fn bytes_remove_all() { + let (mut bytes, a, b, c) = setup(); + // Remove all + let item1 = bytes.remove(0); + let item2 = bytes.remove(0); + let item3 = bytes.remove(0); + assert(bytes.len() == 0); + assert(bytes.capacity() == 4); + assert(item1 == a); + assert(item2 == b); + assert(item3 == c); + assert(bytes.get(0).is_none()); +} + +#[test(should_revert)] +fn revert_bytes_remove_out_of_bounds() { + let (mut bytes, _a, _b, _c) = setup(); + + let _result = bytes.remove(bytes.len()); +} + +#[test()] +fn bytes_swap() { + let (mut bytes, a, b, c) = setup(); + let d = 5u8; + bytes.push(d); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); + + // Swaps Middle + bytes.swap(1, 2); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == c); + assert(bytes.get(2).unwrap() == b); + assert(bytes.get(3).unwrap() == d); +} + +#[test()] +fn bytes_swap_twice() { + let (mut bytes, a, b, c) = setup(); + let d = 5u8; + bytes.push(d); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); + + // Swaps Middle + bytes.swap(1, 2); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == c); + assert(bytes.get(2).unwrap() == b); + assert(bytes.get(3).unwrap() == d); + + bytes.swap(1, 2); + assert(bytes.len() == 4); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == c); + assert(bytes.get(3).unwrap() == d); +} + +#[test()] +fn bytes_swap_front() { + let (mut bytes, a, b, c) = setup(); + + // Swaps Front + bytes.swap(0, 1); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == b); + assert(bytes.get(1).unwrap() == a); + assert(bytes.get(2).unwrap() == c); +} + +#[test()] +fn bytes_swap_end() { + let (mut bytes, a, b, c) = setup(); + + // Swaps back + bytes.swap(2, 1); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == c); + assert(bytes.get(2).unwrap() == b); +} + +#[test()] +fn bytes_swap_front_with_end() { + let (mut bytes, a, b, c) = setup(); + + // Swaps front with back + bytes.swap(0, 2); + assert(bytes.len() == 3); + assert(bytes.capacity() == 4); + assert(bytes.get(0).unwrap() == c); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == a); +} + +#[test(should_revert)] +fn revert_bytes_swap_element_1_out_of_bounds() { + let (mut bytes, _a, _b, _c) = setup(); + + bytes.swap(bytes.len(), 0); +} + +#[test(should_revert)] +fn revert_bytes_swap_element_2_out_of_bounds() { + let (mut bytes, _a, _b, _c) = setup(); + + bytes.swap(0, bytes.len()); +} + +#[test()] +fn bytes_capacity() { + let mut bytes = Bytes::new(); + assert(bytes.capacity() == 0); + + bytes.push(5u8); + assert(bytes.capacity() == 1); + bytes.push(7u8); + assert(bytes.capacity() == 2); + bytes.push(9u8); + assert(bytes.capacity() == 4); + bytes.push(11u8); + assert(bytes.capacity() == 4); + bytes.push(3u8); + assert(bytes.capacity() == 8); +} + +#[test()] +fn bytes_len() { + let (mut bytes, _, _, _) = setup(); + assert(bytes.len() == 3); + + bytes.push(5u8); + assert(bytes.len() == 4); + bytes.push(6u8); + assert(bytes.len() == 5); + bytes.push(7u8); + assert(bytes.len() == 6); + bytes.push(8u8); + assert(bytes.len() == 7); +} + +#[test()] +fn bytes_clear() { + let (mut bytes, _, _, _) = setup(); + assert(bytes.len() == 3); + + bytes.clear(); + assert(bytes.len() == 0); + assert(bytes.capacity() == 0); +} + +#[test()] +fn bytes_clear_twice() { + let (mut bytes, _, _, _) = setup(); + + bytes.clear(); + assert(bytes.len() == 0); + assert(bytes.capacity() == 0); + + // Can clean twice + bytes.push(1u8); + bytes.clear(); + assert(bytes.len() == 0); + assert(bytes.capacity() == 0); +} + +#[test()] +fn bytes_clear_empty_bytes() { + // Clear on empty Bytes + let mut empty_bytes = Bytes::new(); + assert(empty_bytes.len() == 0); + assert(empty_bytes.capacity() == 0); + + empty_bytes.clear(); + assert(empty_bytes.len() == 0); + assert(empty_bytes.capacity() == 0); +} + +#[test] +fn bytes_is_empty() { + let (mut setup_bytes, _, _, _) = setup(); + + assert(!setup_bytes.is_empty()); + + let mut new_bytes = Bytes::new(); + assert(new_bytes.is_empty()); + + let mut capacity_bytes = Bytes::with_capacity(16); + assert(capacity_bytes.is_empty()); +} + +#[test] +fn bytes_ptr() { + let (mut setup_bytes, a, _, _) = setup(); + + let setup_bytes_ptr = setup_bytes.ptr(); + assert(!setup_bytes_ptr.is_null()); + assert(setup_bytes_ptr.read::() == a); + + let mut new_bytes = Bytes::new(); + let new_bytes_ptr = new_bytes.ptr(); + assert(!new_bytes_ptr.is_null()); + + let mut capacity_bytes = Bytes::with_capacity(16); + let capacity_bytes_ptr = capacity_bytes.ptr(); + assert(!capacity_bytes_ptr.is_null()); +} + +#[test()] +fn bytes_split_at() { + let (mut bytes_1, a, b, c) = setup(); + let d = 7u8; + bytes_1.push(d); + assert(bytes_1.len() == 4); + + let index = 2; + let (bytes_1_left, bytes_1_right) = bytes_1.split_at(index); + assert(bytes_1.capacity() == 4); + assert(bytes_1_right.capacity() == 2); + assert(bytes_1_left.capacity() == 2); + assert(bytes_1_left.len() == 2); + assert(bytes_1_right.len() == 2); + assert(bytes_1_left.get(0).unwrap() == a); + assert(bytes_1_left.get(1).unwrap() == b); + assert(bytes_1_right.get(0).unwrap() == c); + assert(bytes_1_right.get(1).unwrap() == d); +} + +#[test()] +fn bytes_split_at_twice() { + let (mut bytes, a, b, _c) = setup(); + let d = 7u8; + bytes.push(d); + assert(bytes.len() == 4); + + let index = 2; + let (bytes_left, _bytes_right) = bytes.split_at(index); + + // Split twice + let index_2 = 1; + let (left_left, left_right) = bytes_left.split_at(index_2); + assert(bytes_left.capacity() == 2); + assert(left_left.capacity() == 1); + assert(left_right.capacity() == 1); + assert(left_left.len() == 1); + assert(left_right.len() == 1); + assert(left_left.get(0).unwrap() == a); + assert(left_right.get(0).unwrap() == b); +} + +#[test()] +fn bytes_split_at_end() { + // // Split at end + let (mut bytes, a, b, c) = setup(); + + let index = bytes.len(); + let (bytes_left, bytes_right) = bytes.split_at(index); + assert(bytes.capacity() == 4); + assert(bytes_left.capacity() == 3); + assert(bytes_right.capacity() == 0); + assert(bytes_left.len() == 3); + assert(bytes_right.len() == 0); + assert(bytes_left.get(0).unwrap() == a); + assert(bytes_left.get(1).unwrap() == b); + assert(bytes_left.get(2).unwrap() == c); + assert(bytes_right.get(0).is_none()); +} + +#[test()] +fn bytes_split_at_front() { + // Split at front + let (mut bytes, a, b, c) = setup(); + + let index = 0; + let (bytes_left, bytes_right) = bytes.split_at(index); + assert(bytes.capacity() == 4); + assert(bytes_left.capacity() == 0); + assert(bytes_right.capacity() == 3); + assert(bytes_left.len() == 0); + assert(bytes_right.len() == 3); + assert(bytes_right.get(0).unwrap() == a); + assert(bytes_right.get(1).unwrap() == b); + assert(bytes_right.get(2).unwrap() == c); + assert(bytes_left.get(0).is_none()); +} + +#[test(should_revert)] +fn revert_bytes_split_at_out_of_bounds() { + let (mut bytes, _a, _b, _c) = setup(); + + let (_bytes_left, _bytes_right) = bytes.split_at(bytes.len() + 1); +} + +#[test()] +fn bytes_append() { + let (mut bytes, a, b, c) = setup(); + assert(bytes.len() == 3); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == c); + + let mut bytes2 = Bytes::new(); + let d = 5u8; + let e = 7u8; + let f = 9u8; + bytes2.push(d); + bytes2.push(e); + bytes2.push(f); + assert(bytes2.len() == 3); + assert(bytes2.get(0).unwrap() == d); + assert(bytes2.get(1).unwrap() == e); + assert(bytes2.get(2).unwrap() == f); + + let first_length = bytes.len(); + let second_length = bytes2.len(); + bytes.append(bytes2); + assert(bytes.len() == first_length + second_length); + assert(bytes.capacity() == first_length + first_length); + assert(bytes2.is_empty()); + let values = [a, b, c, d, e, f]; + let mut i = 0; + while i < 6 { + assert(bytes.get(i).unwrap() == values[i]); + i += 1; + }; +} + +#[test()] +fn bytes_append_empty() { + // Append empty bytes + let (mut bytes, a, b, c) = setup(); + let bytes_length = bytes.len(); + let bytes_original_capacity = bytes.capacity(); + + let mut empty_bytes = Bytes::new(); + bytes.append(empty_bytes); + + // Because empty bytes were appended, no changes to length and capacity were made + assert(bytes.len() == bytes_length); + assert(bytes.capacity() == bytes_original_capacity); + assert(empty_bytes.is_empty()); + + let values = [a, b, c]; + let mut i = 0; + while i < 3 { + assert(bytes.get(i).unwrap() == values[i]); + i += 1; + }; +} + +#[test()] +fn bytes_append_to_empty() { + // Append to empty bytes + let (mut bytes, a, b, c) = setup(); + let bytes_length = bytes.len(); + let bytes_original_capacity = bytes.capacity(); + + // Because empty bytes were appended, no changes to capacity were made + let mut empty_bytes = Bytes::new(); + empty_bytes.append(bytes); + assert(empty_bytes.len() == bytes_length); + assert(empty_bytes.capacity() == bytes_original_capacity); + assert(bytes.is_empty()); + + let values = [a, b, c]; + let mut i = 0; + while i < 3 { + assert(empty_bytes.get(i).unwrap() == values[i]); + i += 1; + }; +} + +#[test()] +fn bytes_eq() { + let (mut bytes, _a, _b, _c) = setup(); + + let d = 5u8; + let e = 7u8; + let f = 9u8; + let mut other = Bytes::new(); + other.push(d); + other.push(e); + other.push(f); + assert(bytes == other); + + other.push(42u8); + bytes.push(42u8); + assert(bytes == other); +} + +#[test()] +fn bytes_ne() { + let (mut bytes, _a, _b, _c) = setup(); + + let d = 5u8; + let e = 7u8; + let f = 9u8; + let mut other = Bytes::new(); + other.push(d); + other.push(e); + other.push(f); + + other.push(42u8); + assert(bytes != other); + + other.swap(0, 1); + assert(bytes != other); +} + +#[test()] +fn bytes_as_raw_slice() { + let (mut bytes, _a, _b, _c) = setup(); + + let slice = bytes.as_raw_slice(); + assert(bytes.ptr() == slice.ptr()); + assert(bytes.len() == slice.number_of_bytes()); +} + +#[test] +fn bytes_from_b256() { + let initial = 0x3333333333333333333333333333333333333333333333333333333333333333; + let b: Bytes = Bytes::from(initial); + let mut control_bytes = Bytes::with_capacity(32); + + let mut i = 0; + while i < 32 { + // 0x33 is 51 in decimal + control_bytes.push(51u8); + i += 1; + } + + assert(b == control_bytes); +} + +#[test] +fn bytes_into_b256() { + let mut initial_bytes = Bytes::with_capacity(32); + + let mut i = 0; + while i < 32 { + // 0x33 is 51 in decimal + initial_bytes.push(51u8); + i += 1; + } + + let value: b256 = initial_bytes.into(); + let expected: b256 = 0x3333333333333333333333333333333333333333333333333333333333333333; + + assert(value == expected); +} + +#[test] +fn bytes_b256_from() { + let control = 0x3333333333333333333333333333333333333333333333333333333333333333; + let mut bytes = Bytes::with_capacity(32); + + let mut i = 0; + while i < 32 { + // 0x33 is 51 in decimal + bytes.push(51u8); + i += 1; + } + + let result_b256: b256 = b256::from(bytes); + + assert(result_b256 == control); +} + +#[test] +fn bytes_b256_into() { + let initial = 0x3333333333333333333333333333333333333333333333333333333333333333; + let mut control = Bytes::with_capacity(32); + + let mut i = 0; + while i < 32 { + // 0x33 is 51 in decimal + control.push(51u8); + i += 1; + } + + let result_bytes: Bytes = initial.into(); + + assert(result_bytes == control); +} + +#[test()] +fn bytes_from_raw_slice() { + let val = 0x3497297632836282349729763283628234972976328362823497297632836282; + let slice = asm(ptr: (__addr_of(val), 32)) { + ptr: raw_slice + }; + + let mut bytes = Bytes::from(slice); + assert(bytes.ptr() != slice.ptr()); // Bytes should own its buffer + assert(bytes.len() == slice.number_of_bytes()); +} + +#[test()] +fn bytes_into_raw_slice() { + let (mut bytes, _a, _b, _c) = setup(); + + let slice: raw_slice = bytes.into(); + + assert(bytes.ptr() == slice.ptr()); + assert(bytes.len() == slice.number_of_bytes()); +} + +#[test()] +fn bytes_raw_slice_from() { + let (mut bytes, _a, _b, _c) = setup(); + + let slice: raw_slice = raw_slice::from(bytes); + + assert(bytes.ptr() == slice.ptr()); + assert(bytes.len() == slice.number_of_bytes()); +} + +#[test()] +fn bytes_raw_slice_into() { + let val = 0x3497297632836282349729763283628234972976328362823497297632836282; + let slice = asm(ptr: (__addr_of(val), 32)) { + ptr: raw_slice + }; + + let bytes: Bytes = slice.into(); + + assert(bytes.ptr() != slice.ptr()); // Bytes should own its buffer + assert(bytes.len() == slice.number_of_bytes()); +} + +#[test()] +fn bytes_from_vec_u8() { + let mut vec = Vec::new(); + let (_, a, b, c) = setup(); + vec.push(a); + vec.push(b); + vec.push(c); + + let bytes = Bytes::from(vec); + + assert(bytes.len() == 3); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == c); +} + +#[test()] +fn bytes_into_vec_u8() { + let (mut bytes, a, b, c) = setup(); + assert(bytes.len() == 3); + + let vec: Vec = bytes.into(); + + assert(vec.len() == 3); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).unwrap() == c); +} + +#[test()] +fn bytes_vec_u8_from() { + let (mut bytes, a, b, c) = setup(); + + let mut vec: Vec = Vec::::from(bytes); + + assert(vec.len() == 3); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).unwrap() == c); +} + +#[test()] +fn bytes_vec_u8_into() { + let mut vec = Vec::new(); + let (_, a, b, c) = setup(); + vec.push(a); + vec.push(b); + vec.push(c); + + let bytes: Bytes = vec.into(); + + assert(bytes.len() == 3); + assert(bytes.get(0).unwrap() == a); + assert(bytes.get(1).unwrap() == b); + assert(bytes.get(2).unwrap() == c); +} + +#[test] +fn bytes_clone() { + let (mut bytes, _a, _b, _c) = setup(); + + let cloned_bytes = bytes.clone(); + + assert(cloned_bytes.ptr() != bytes.ptr()); + assert(cloned_bytes.len() == bytes.len()); + // Capacity is not cloned + // assert(cloned_bytes.capacity() == bytes.capacity()); + assert(cloned_bytes.get(0).unwrap() == bytes.get(0).unwrap()); + assert(cloned_bytes.get(1).unwrap() == bytes.get(1).unwrap()); + assert(cloned_bytes.get(2).unwrap() == bytes.get(2).unwrap()); +} + +#[test] +pub fn test_encode_decode() { + let initial = 0x3333333333333333333333333333333333333333333333333333333333333333; + let initial: Bytes = Bytes::from(initial); + let decoded = abi_decode::(encode(initial)); + + assert_eq(decoded, initial); +} + +#[test()] +fn bytes_test_packing() { + let mut bytes = Bytes::new(); + bytes.push(5u8); + bytes.push(5u8); + bytes.push(5u8); + bytes.push(5u8); + bytes.push(5u8); + bytes.push(5u8); + bytes.push(5u8); + bytes.push(5u8); + bytes.push(5u8); + bytes.push(5u8); + bytes.push(5u8); + assert(bytes.len() == 11); + assert(bytes.capacity() == 16); +} + +#[test()] +fn bytes_test_u8_limits() { + let mut bytes = Bytes::new(); + let max = 255u8; + let min = 0u8; + bytes.push(max); + bytes.push(min); + bytes.push(max); + bytes.push(min); + bytes.push(max); + bytes.push(min); + + assert(bytes.len() == 6); + assert(bytes.capacity() == 8); + assert(bytes.get(0).unwrap() == max); + assert(bytes.get(1).unwrap() == min); + assert(bytes.get(2).unwrap() == max); + assert(bytes.get(3).unwrap() == min); + assert(bytes.get(4).unwrap() == max); + assert(bytes.get(5).unwrap() == min); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/Forc.toml new file mode 100644 index 00000000000..de4d6a7ee46 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "contract_id_contract_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/src/main.sw new file mode 100644 index 00000000000..4ad5554b355 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/src/main.sw @@ -0,0 +1,20 @@ +contract; + +abi ContractIdTest { + fn this_contract_id() -> ContractId; +} + +impl ContractIdTest for Contract { + fn this_contract_id() -> ContractId { + ContractId::this() + } +} + +#[test] +fn contract_id_this() { + let expected_contract_id = ContractId::from(CONTRACT_ID); + let contract_abi = abi(ContractIdTest, expected_contract_id.bits()); + + let result_contract_id = contract_abi.this_contract_id(); + assert(result_contract_id == expected_contract_id); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/Forc.toml new file mode 100644 index 00000000000..a17cad1235c --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "contract_id_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/src/main.sw new file mode 100644 index 00000000000..cfcc216a8d4 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/src/main.sw @@ -0,0 +1,182 @@ +library; + +#[test] +fn contract_id_bits() { + let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let bits1 = contract_id_1.bits(); + assert(bits1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + + let contract_id_2 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let bits2 = contract_id_2.bits(); + assert(bits2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let bits3 = contract_id_3.bits(); + assert(bits3 == 0x0000000000000000000000000000000000000000000000000000000000000001); +} + +#[test] +fn contract_id_eq() { + let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let contract_id_2 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let contract_id_4 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let contract_id_5 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let contract_id_6 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + assert(contract_id_1 == contract_id_2); + assert(contract_id_3 == contract_id_4); + assert(contract_id_5 == contract_id_6); +} + +#[test] +fn contract_id_ne() { + let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let contract_id_2 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let contract_id_4 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let contract_id_5 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let contract_id_6 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + assert(contract_id_1 != contract_id_3); + assert(contract_id_1 != contract_id_4); + assert(contract_id_1 != contract_id_5); + assert(contract_id_1 != contract_id_6); + assert(contract_id_2 != contract_id_3); + assert(contract_id_2 != contract_id_4); + assert(contract_id_2 != contract_id_5); + assert(contract_id_2 != contract_id_6); + assert(contract_id_3 != contract_id_5); + assert(contract_id_3 != contract_id_6); + assert(contract_id_4 != contract_id_5); + assert(contract_id_4 != contract_id_6); +} + +#[test] +fn contract_id_from_b256() { + let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + assert( + contract_id_1 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let contract_id_2 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + assert( + contract_id_2 + .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + + let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + assert( + contract_id_3 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); +} + +#[test] +fn contract_id_b256_into() { + let b256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000; + let contract_id_1: ContractId = b256_1.into(); + assert( + contract_id_1 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let b256_2 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; + let contract_id_2: ContractId = b256_2.into(); + assert( + contract_id_2 + .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + + let b256_3 = 0x0000000000000000000000000000000000000000000000000000000000000001; + let contract_id_3: ContractId = b256_3.into(); + assert( + contract_id_3 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); +} + +#[test] +fn contract_id_into_b256() { + let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let b256_data1: b256 = contract_id_1.into(); + assert( + b256_data1 == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let contract_id_2 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let b256_data2: b256 = contract_id_2.into(); + assert( + b256_data2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + + let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let b256_data3: b256 = contract_id_3.into(); + assert( + b256_data3 == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); +} + +#[test] +fn contract_id_b256_from() { + let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let b256_data1: b256 = b256::from(contract_id_1); + assert( + b256_data1 == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + + let contract_id_2 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let b256_data2: b256 = b256::from(contract_id_2); + assert( + b256_data2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + + let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let b256_data3: b256 = b256::from(contract_id_3); + assert( + b256_data3 == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); +} + +#[test] +fn contract_id_hash() { + use std::hash::{Hash, sha256}; + + let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let digest1 = sha256(contract_id_1); + assert(digest1 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); + + let contract_id_2 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let digest2 = sha256(contract_id_2); + assert(digest2 == 0xaf9613760f72635fbdb44a5a0a63c39f12af30f950a6ee5c971be188e89c4051); + + let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let digest3 = sha256(contract_id_3); + assert(digest3 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); + + let contract_id_4 = ContractId::from(0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); + let digest4 = sha256(contract_id_4); + assert(digest4 != 0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); +} + +#[test] +fn contract_id_zero() { + let contract_id = ContractId::zero(); + assert( + contract_id + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); +} + +#[test] +fn contract_id_is_zero() { + let zero_contract_id = ContractId::zero(); + assert(zero_contract_id.is_zero()); + + let contract_id_2 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); + assert(!contract_id_2.is_zero()); + + let contract_id_3 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + assert(!contract_id_3.is_zero()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/Forc.toml new file mode 100644 index 00000000000..0a8e7ef6ca2 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "ecr_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/src/main.sw new file mode 100644 index 00000000000..e11471b2cbe --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/src/main.sw @@ -0,0 +1,133 @@ +library; + +use std::{ + b512::B512, + ecr::{ + ec_recover, + ec_recover_address, + ec_recover_address_r1, + ec_recover_r1, + ed_verify, + }, + hash::{ + Hash, + sha256, + }, +}; + +#[test] +fn ecr_ec_recover() { + let hi_1 = 0x61f3caf4c0912cec69ff0b226638d397115c623a7f057914d48a7e4daf1cf6d8; + let lo_1 = 0x2555de81cd3a40382d3d64eb1c77e463eea5a76d65ec85f283e0b3d568352678; + let msg_hash_1 = 0xa13f4ab54057ce064d3dd97ac3ff30ed704e73956896c03650fe59b1a561fe15; + let pub_hi = 0x41a55558a3486b6ee3878f55f16879c0798afd772c1506de44aba90d29b6e65c; + let pub_lo = 0x341ca2e0a3d5827e78d838e35b29bebe2a39ac30b58999e1138c9467bf859965; + let signature_1: B512 = B512::from((hi_1, lo_1)); + // A recovered public key pair. + let result_1 = ec_recover(signature_1, msg_hash_1); + + assert(result_1.is_ok()); + assert(result_1.unwrap().bits()[0] == pub_hi); + assert(result_1.unwrap().bits()[1] == pub_lo); + + let hi_2 = b256::zero(); + let lo_2 = 0x2555de81cd3a40382d3d64eb1c77e463eea5a76d65ec85f283e0b3d568352678; + let msg_hash_2 = 0xa13f4ab54057ce064d3dd97ac3ff30ed704e73956896c03650fe59b1a561fe15; + let signature_2: B512 = B512::from((hi_2, lo_2)); + // A recovered public key pair. + let result_2 = ec_recover(signature_2, msg_hash_2); + + assert(result_2.is_err()); +} + +#[test] +fn ecr_ec_recover_r1() { + let hi_1 = 0xbd0c9b8792876712afadbff382e1bf31c44437823ed761cc3600d0016de511ac; + let lo_1 = 0x44ac566bd156b4fc71a4a4cb2655d3da360c695edb27dc3b64d621e122fea23d; + let msg_hash_1 = 0x1e45523606c96c98ba970ff7cf9511fab8b25e1bcd52ced30b81df1e4a9c4323; + let pub_hi_1 = 0xd6ea577a54ae42411fbc78d686d4abba2150ca83540528e4b868002e346004b2; + let pub_lo_1 = 0x62660ecce5979493fe5684526e8e00875b948e507a89a47096bc84064a175452; + let signature_1: B512 = B512::from((hi_1, lo_1)); + // A recovered public key pair. + let result_1 = ec_recover_r1(signature_1, msg_hash_1); + + assert(result_1.is_ok()); + assert(result_1.unwrap().bits()[0] == pub_hi_1); + assert(result_1.unwrap().bits()[1] == pub_lo_1); + + let hi_2 = b256::zero(); + let lo_2 = 0x44ac566bd156b4fc71a4a4cb2655d3da360c695edb27dc3b64d621e122fea23d; + let msg_hash_2 = 0x1e45523606c96c98ba970ff7cf9511fab8b25e1bcd52ced30b81df1e4a9c4323; + let signature_2: B512 = B512::from((hi_2, lo_2)); + let result_2 = ec_recover_r1(signature_2, msg_hash_2); + + assert(result_2.is_err()); +} + +#[test] +fn ecr_ec_recover_address() { + let hi_1 = 0x61f3caf4c0912cec69ff0b226638d397115c623a7f057914d48a7e4daf1cf6d8; + let lo_1 = 0x2555de81cd3a40382d3d64eb1c77e463eea5a76d65ec85f283e0b3d568352678; + let msg_hash_1 = 0xa13f4ab54057ce064d3dd97ac3ff30ed704e73956896c03650fe59b1a561fe15; + let address_1 = Address::from(0x02844f00cce0f608fa3f0f7408bec96bfd757891a6fda6e1fa0f510398304881); + let signature_1: B512 = B512::from((hi_1, lo_1)); + // A recovered Fuel address. + let result_1 = ec_recover_address(signature_1, msg_hash_1); + assert(result_1.is_ok()); + assert(result_1.unwrap() == address_1); + + let hi_2 = b256::zero(); + let lo_2 = 0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d; + let msg_hash_2 = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323; + let signature_2: B512 = B512::from((hi_2, lo_2)); + let result_2 = ec_recover_address(signature_2, msg_hash_2); + + assert(result_2.is_err()); +} + +#[test] +fn ecr_ec_recover_address_r1() { + let hi_1 = 0xbd0c9b8792876713afa8bf3383eebf31c43437823ed761cc3600d0016de5110c; + let lo_1 = 0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d; + let msg_hash_1 = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323; + let address_1 = Address::from(0xb4a5fabee8cc852084b71f17107e9c18d682033a58967027af0ab01edf2f9a6a); + let signature_1: B512 = B512::from((hi_1, lo_1)); + // A recovered Fuel address. + let result_1 = ec_recover_address_r1(signature_1, msg_hash_1); + assert(result_1.is_ok()); + assert(result_1.unwrap() == address_1); + + let hi_2 = b256::zero(); + let lo_2 = 0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d; + let msg_hash_2 = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323; + let signature_2: B512 = B512::from((hi_2, lo_2)); + let result_2 = ec_recover_address_r1(signature_2, msg_hash_2); + + assert(result_2.is_err()); +} + +#[test] +fn ecr_ed_verify() { + let pub_key_1 = 0x314fa58689bbe1da2430517de2d772b384a1c1d2e9cb87e73c6afcf246045b10; + let msg_1 = b256::zero(); + let msg_hash_1 = sha256(msg_1); + + let hi_1 = 0xf38cef9361894be6c6e0eddec28a663d099d7ddff17c8077a1447d7ecb4e6545; + let lo_1 = 0xf5084560039486d3462dd65a40c80a74709b2f06d450ffc5dc00345c6b2cdd00; + let signature_1: B512 = B512::from((hi_1, lo_1)); + // A verified public key with signature + let verified_1 = ed_verify(pub_key_1, signature_1, msg_hash_1); + assert(verified_1.is_ok()); + assert(verified_1.unwrap()); + + let pub_key_2 = 0x314fa58689bbe1da2430517de2d772b384a1c1d2e9cb87e73c6afcf246045b10; + let msg_2 = b256::zero(); + let msg_hash_2 = sha256(msg_2); + + let hi_2 = b256::zero(); + let lo_2 = 0xf5084560039486d3462dd65a40c80a74709b2f06d450ffc5dc00345c6b2cdd00; + let signature_2: B512 = B512::from((hi_2, lo_2)); + let verified_2 = ed_verify(pub_key_2, signature_2, msg_hash_2); + + assert(verified_2.is_err()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/Forc.toml new file mode 100644 index 00000000000..18fc492db04 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "flags_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/src/main.sw new file mode 100644 index 00000000000..dc8d7dac430 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/src/main.sw @@ -0,0 +1,67 @@ +library; + +use std::{ + flags::{ + disable_panic_on_overflow, + disable_panic_on_unsafe_math, + enable_panic_on_overflow, + enable_panic_on_unsafe_math, + set_flags, + }, + registers::error, +}; + +#[test] +fn flags_disable_panic_on_overflow() { + let _ = disable_panic_on_overflow(); + let _bar = u64::max() + 1; + enable_panic_on_overflow(); +} + +#[test] +fn flags_disable_panic_on_overflow_preserving() { + let _ = disable_panic_on_overflow(); + + let prior_flags = disable_panic_on_overflow(); + let _bar = u64::max() + 1; + set_flags(prior_flags); + + let _bar = u64::max() + 1; + + enable_panic_on_overflow(); +} + +#[test] +fn flags_disable_panic_on_unsafe_math() { + let _ = disable_panic_on_unsafe_math(); + + let _bar = asm(r2: 1, r3: 0, r1) { + div r1 r2 r3; + r1: u64 + }; + + assert(error() == 1); + + enable_panic_on_unsafe_math(); +} + +#[test] +fn flags_disable_panic_on_unsafe_math_preserving() { + let _ = disable_panic_on_unsafe_math(); + + let prior_flags = disable_panic_on_unsafe_math(); + let _bar = asm(r2: 1, r3: 0, r1) { + div r1 r2 r3; + r1: u64 + }; + assert(error() == 1); + set_flags(prior_flags); + + let _bar = asm(r2: 1, r3: 0, r1) { + div r1 r2 r3; + r1: u64 + }; + assert(error() == 1); + + enable_panic_on_unsafe_math(); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/Forc.toml new file mode 100644 index 00000000000..b0e33a7ead7 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "hash_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/src/main.sw new file mode 100644 index 00000000000..ccef01df70f --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/src/main.sw @@ -0,0 +1,457 @@ +library; + +use std::{bytes::Bytes, hash::{Hash, Hasher, keccak256, sha256, sha256_str_array},}; + +#[test()] +fn hash_hasher_write_str() { + let mut hasher = Hasher::new(); + hasher.write_str("test"); + let sha256 = hasher.sha256(); + assert(sha256 == 0x9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08); + + let mut hasher = Hasher::new(); + hasher.write_str("Fastest Modular Execution Layer!"); + let sha256 = hasher.sha256(); + assert(sha256 == 0x4a3cd7c8b44dbf7941e55179425f746adeaa97fe2d99b571fffee78e9b41743c); +} + +#[test()] +fn hash_hasher_keccak256_str() { + let mut hasher = Hasher::new(); + hasher.write_str("test"); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658); + + let mut hasher = Hasher::new(); + hasher.write_str("Fastest Modular Execution Layer!"); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xab8e83e041e001bcf797c9cc7d6bc472bfdb8c736bab7999f13b7c26f48c354f); +} + +#[test()] +fn hash_hasher_write_str_array() { + let mut hasher = Hasher::new(); + hasher.write_str_array(__to_str_array("test")); + let sha256 = hasher.sha256(); + assert(sha256 == 0x9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08); + + let mut hasher = Hasher::new(); + hasher.write_str_array(__to_str_array("Fastest Modular Execution Layer!")); + let sha256 = hasher.sha256(); + assert(sha256 == 0x4a3cd7c8b44dbf7941e55179425f746adeaa97fe2d99b571fffee78e9b41743c); +} + +#[test()] +fn hash_hasher_keccak256_str_array() { + let mut hasher = Hasher::new(); + hasher.write_str_array(__to_str_array("test")); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658); + + let mut hasher = Hasher::new(); + hasher.write_str_array(__to_str_array("Fastest Modular Execution Layer!")); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xab8e83e041e001bcf797c9cc7d6bc472bfdb8c736bab7999f13b7c26f48c354f); +} + +// The hashes for the following test can be obtained in Rust by running the following script: +// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a2d83e9ea48b35a3e991c904c3451ed5 +#[test()] +fn hash_hasher_sha256_u8() { + let mut hasher = Hasher::new(); + 0_u8.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d); + + let mut hasher = Hasher::new(); + 1_u8.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a); +} + +#[test()] +fn hash_hasher_keccak256_u8() { + let mut hasher = Hasher::new(); + 0_u8.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a); + + let mut hasher = Hasher::new(); + 1_u8.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); +} + +#[test()] +fn hash_hasher_sha256_u16() { + let mut hasher = Hasher::new(); + 0_u16.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x96a296d224f285c67bee93c30f8a309157f0daa35dc5b87e410b78630a09cfc7); + + let mut hasher = Hasher::new(); + 1_u16.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xb413f47d13ee2fe6c845b2ee141af81de858df4ec549a58b7970bb96645bc8d2); +} + +#[test()] +fn hash_hasher_keccak256_u16() { + let mut hasher = Hasher::new(); + 0_u16.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x54a8c0ab653c15bfb48b47fd011ba2b9617af01cb45cab344acd57c924d56798); + + let mut hasher = Hasher::new(); + 1_u16.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x49d03a195e239b52779866b33024210fc7dc66e9c2998975c0aa45c1702549d5); +} + +#[test()] +fn hash_hasher_sha256_u32() { + let mut hasher = Hasher::new(); + 0_u32.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xdf3f619804a92fdb4057192dc43dd748ea778adc52bc498ce80524c014b81119); + + let mut hasher = Hasher::new(); + 1_u32.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xb40711a88c7039756fb8a73827eabe2c0fe5a0346ca7e0a104adc0fc764f528d); +} + +#[test()] +fn hash_hasher_keccak256_u32() { + let mut hasher = Hasher::new(); + 0_u32.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xe8e77626586f73b955364c7b4bbf0bb7f7685ebd40e852b164633a4acbd3244c); + + let mut hasher = Hasher::new(); + 1_u32.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x51f81bcdfc324a0dff2b5bec9d92e21cbebc4d5e29d3a3d30de3e03fbeab8d7f); +} + +#[test()] +fn hash_hasher_sha256_u64() { + let mut hasher = Hasher::new(); + 0_u64.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xaf5570f5a1810b7af78caf4bc70a660f0df51e42baf91d4de5b2328de0e83dfc); + + let mut hasher = Hasher::new(); + 1_u64.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xcd2662154e6d76b2b2b92e70c0cac3ccf534f9b74eb5b89819ec509083d00a50); +} + +#[test()] +fn hash_hasher_keccak256_u64() { + let mut hasher = Hasher::new(); + 0_u64.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce); + + let mut hasher = Hasher::new(); + 1_u64.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x6c31fc15422ebad28aaf9089c306702f67540b53c7eea8b7d2941044b027100f); +} + +#[test()] +fn hash_hasher_sha256_b256() { + let mut hasher = Hasher::new(); + 0x0000000000000000000000000000000000000000000000000000000000000000 + .hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); + + let mut hasher = Hasher::new(); + 0x0000000000000000000000000000000000000000000000000000000000000001 + .hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); +} + +#[test()] +fn hash_hasher_keccak256_b256() { + let mut hasher = Hasher::new(); + 0x0000000000000000000000000000000000000000000000000000000000000000 + .hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563); + + let mut hasher = Hasher::new(); + 0x0000000000000000000000000000000000000000000000000000000000000001 + .hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6); +} + +#[test] +fn hash_hasher_sha256_u256() { + let mut hasher = Hasher::new(); + 0x0000000000000000000000000000000000000000000000000000000000000000_u256 + .hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); + + let mut hasher = Hasher::new(); + 0x0000000000000000000000000000000000000000000000000000000000000001_u256 + .hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); +} + +#[test] +fn hash_hasher_keccak256_u256() { + let mut hasher = Hasher::new(); + 0x0000000000000000000000000000000000000000000000000000000000000000_u256 + .hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563); + + let mut hasher = Hasher::new(); + 0x0000000000000000000000000000000000000000000000000000000000000001_u256 + .hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6); +} + +#[test()] +fn hash_hasher_sha256_bool() { + let mut hasher = Hasher::new(); + false.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d); + + let mut hasher = Hasher::new(); + true.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a); +} + +#[test()] +fn hash_hasher_keccak256_bool() { + let mut hasher = Hasher::new(); + false.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a); + + let mut hasher = Hasher::new(); + true.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); +} + +#[test] +fn hash_hasher_sha256_bytes() { + let mut hasher = Hasher::new(); + let mut bytes = Bytes::new(); + bytes.push(0u8); + bytes.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d); + + let mut hasher = Hasher::new(); + let mut bytes = Bytes::new(); + bytes.push(1u8); + bytes.hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a); +} + +#[test] +fn hash_hasher_keccak256_bytes() { + let mut hasher = Hasher::new(); + let mut bytes = Bytes::with_capacity(1); + bytes.push(0u8); + bytes.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a); + + let mut hasher = Hasher::new(); + let mut bytes = Bytes::with_capacity(1); + bytes.push(1u8); + bytes.hash(hasher); + let keccak256 = hasher.keccak256(); + assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); +} + +#[test] +fn hash_hasher_sha256_3_tuple() { + let mut hasher = Hasher::new(); + (0_u64, 0_u64, 0_u64).hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x9d908ecfb6b256def8b49a7c504e6c889c4b0e41fe6ce3e01863dd7b61a20aa0); + + let mut hasher = Hasher::new(); + (1_u64, 1_u64, 1_u64).hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xf3dd2c58f4b546018d9a5e147e195b7744eee27b76cae299dad63f221173cca0); +} + +#[test] +fn hash_hasher_sha256_4_tuple() { + let mut hasher = Hasher::new(); + (0_u64, 0_u64, 0_u64, 0_u64).hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); + + let mut hasher = Hasher::new(); + (1_u64, 1_u64, 1_u64, 1_u64).hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x696547da2108716208569c8d60e78fcb423e7ad45cb8c700eeda8a8805bf2571); +} + +#[test] +fn hash_hasher_sha256_5_tuple() { + let mut hasher = Hasher::new(); + (0_u64, 0_u64, 0_u64, 0_u64, 0_u64).hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x2c34ce1df23b838c5abf2a7f6437cca3d3067ed509ff25f11df6b11b582b51eb); + + let mut hasher = Hasher::new(); + (1_u64, 1_u64, 1u64, 1_u64, 1_u64).hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x7bf87db15ea1fff61e936a88ff181b511e66b22417ed270ebb90c298c2088c10); +} + +#[test] +fn hash_hasher_sha256_1_array() { + let mut hasher = Hasher::new(); + [0_u64].hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xaf5570f5a1810b7af78caf4bc70a660f0df51e42baf91d4de5b2328de0e83dfc); + + let mut hasher = Hasher::new(); + [1_u64].hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xcd2662154e6d76b2b2b92e70c0cac3ccf534f9b74eb5b89819ec509083d00a50); +} + +#[test] +fn hash_hasher_sha256_2_array() { + let mut hasher = Hasher::new(); + [0_u64, 0_u64].hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x374708fff7719dd5979ec875d56cd2286f6d3cf7ec317a3b25632aab28ec37bb); + + let mut hasher = Hasher::new(); + [1_u64, 1_u64].hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x532deabf88729cb43995ab5a9cd49bf9b90a079904dc0645ecda9e47ce7345a9); +} + +#[test] +fn hash_hasher_sha256_4_array() { + let mut hasher = Hasher::new(); + [0_u64, 0_u64, 0_u64, 0_u64].hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); + + let mut hasher = Hasher::new(); + [1_u64, 1_u64, 1_u64, 1_u64].hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x696547da2108716208569c8d60e78fcb423e7ad45cb8c700eeda8a8805bf2571); +} + +#[test] +fn hash_hasher_sha256_5_array() { + let mut hasher = Hasher::new(); + [0_u64, 0_u64, 0_u64, 0_u64, 0_u64].hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x2c34ce1df23b838c5abf2a7f6437cca3d3067ed509ff25f11df6b11b582b51eb); + + let mut hasher = Hasher::new(); + [1_u64, 1_u64, 1_u64, 1_u64, 1_u64].hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x7bf87db15ea1fff61e936a88ff181b511e66b22417ed270ebb90c298c2088c10); +} + +#[test] +fn hash_hasher_sha256_6_array() { + let mut hasher = Hasher::new(); + [0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64].hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x17b0761f87b081d5cf10757ccc89f12be355c70e2e29df288b65b30710dcbcd1); + + let mut hasher = Hasher::new(); + [1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64].hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x9cd65c79280fcb0d834da54ea98364d11439ec21e106447abcee2893765809a4); +} + +#[test] +fn hash_hasher_sha256_8_array() { + let mut hasher = Hasher::new(); + [0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64] + .hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xf5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b); + + let mut hasher = Hasher::new(); + [1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64] + .hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x794dde2d7e1d63dc28474122bd094bd35499447b3764dbf6cdf7c75ca73918dc); +} + +#[test] +fn hash_hasher_sha256_9_array() { + let mut hasher = Hasher::new(); + [0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64] + .hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x834a709ba2534ebe3ee1397fd4f7bd288b2acc1d20a08d6c862dcd99b6f04400); + + let mut hasher = Hasher::new(); + [1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64] + .hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0xe62386a1ec5b8fd0ece7344a7cae775d73179cfc0950c4fdeed26c7e8944e795); +} + +#[test] +fn hash_hasher_sha256_10_array() { + let mut hasher = Hasher::new(); + [0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64] + .hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x5b6fb58e61fa475939767d68a446f97f1bff02c0e5935a3ea8bb51e6515783d8); + + let mut hasher = Hasher::new(); + [1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64] + .hash(hasher); + let sha256 = hasher.sha256(); + assert(sha256 == 0x5f80cf4c3ec64f652ea4ba4db7ea12896224546bd2ed4dd2032a8ce12fde16f9); +} + +#[test()] +fn hash_sha256() { + let digest = sha256(0_u64); + assert(digest == 0xaf5570f5a1810b7af78caf4bc70a660f0df51e42baf91d4de5b2328de0e83dfc); + + let digest = sha256(1_u64); + assert(digest == 0xcd2662154e6d76b2b2b92e70c0cac3ccf534f9b74eb5b89819ec509083d00a50); +} + +#[test()] +fn hash_sha256_str_array() { + let digest = sha256_str_array(__to_str_array("test")); + assert(digest == 0x9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08); + + let digest = sha256_str_array(__to_str_array("Fastest Modular Execution Layer!")); + assert(digest == 0x4a3cd7c8b44dbf7941e55179425f746adeaa97fe2d99b571fffee78e9b41743c); +} + +#[test()] +fn hash_keccak256() { + let digest = keccak256(0_u64); + assert(digest == 0x011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce); + + let digest = keccak256(1_u64); + assert(digest == 0x6c31fc15422ebad28aaf9089c306702f67540b53c7eea8b7d2941044b027100f); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/Forc.toml new file mode 100644 index 00000000000..e124feb3c7a --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "identity_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/src/main.sw new file mode 100644 index 00000000000..b6f5d148622 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/src/main.sw @@ -0,0 +1,247 @@ +library; + +#[test] +fn identity_eq() { + let address1 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000000)); + let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000000)); + let address3 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let address4 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let address5 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let address6 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let contract_id1 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000)); + let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000)); + let contract_id3 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let contract_id4 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let contract_id5 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let contract_id6 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + + assert(address1 == address2); + assert(contract_id1 == contract_id2); + assert(address3 == address4); + assert(contract_id3 == contract_id4); + assert(address5 == address6); + assert(contract_id5 == contract_id6); +} + +#[test] +fn identity_ne() { + let address1 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000000)); + let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000000)); + let address3 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let address4 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let address5 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let address6 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let contract_id1 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000)); + let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000)); + let contract_id3 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let contract_id4 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let contract_id5 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let contract_id6 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + + assert(address1 != address3); + assert(address1 != address4); + assert(address1 != address5); + assert(address1 != address6); + assert(address2 != address3); + assert(address2 != address4); + assert(address2 != address5); + assert(address2 != address6); + assert(address3 != address5); + assert(address3 != address6); + assert(address4 != address5); + assert(address4 != address6); + + assert(contract_id1 != contract_id3); + assert(contract_id1 != contract_id4); + assert(contract_id1 != contract_id5); + assert(contract_id1 != contract_id6); + assert(contract_id2 != contract_id3); + assert(contract_id2 != contract_id4); + assert(contract_id2 != contract_id5); + assert(contract_id2 != contract_id6); + assert(contract_id3 != contract_id5); + assert(contract_id3 != contract_id6); + assert(contract_id4 != contract_id5); + assert(contract_id4 != contract_id6); + + assert(address1 != contract_id1); + assert(address1 != contract_id2); + assert(address1 != contract_id3); + assert(address1 != contract_id4); + assert(address1 != contract_id5); + assert(address1 != contract_id6); + assert(address2 != contract_id1); + assert(address2 != contract_id2); + assert(address2 != contract_id3); + assert(address2 != contract_id4); + assert(address2 != contract_id5); + assert(address2 != contract_id6); + assert(address3 != contract_id1); + assert(address3 != contract_id2); + assert(address3 != contract_id3); + assert(address3 != contract_id4); + assert(address3 != contract_id5); + assert(address3 != contract_id6); + assert(address4 != contract_id1); + assert(address4 != contract_id2); + assert(address4 != contract_id3); + assert(address4 != contract_id4); + assert(address4 != contract_id5); + assert(address4 != contract_id6); + assert(address5 != contract_id1); + assert(address5 != contract_id2); + assert(address5 != contract_id3); + assert(address5 != contract_id4); + assert(address5 != contract_id5); + assert(address5 != contract_id6); + assert(address6 != contract_id1); + assert(address6 != contract_id2); + assert(address6 != contract_id3); + assert(address6 != contract_id4); + assert(address6 != contract_id5); + assert(address6 != contract_id6); +} + +#[test] +fn identity_as_address() { + let address1 = Identity::Address(Address::zero()); + let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let contract_id1 = Identity::ContractId(ContractId::zero()); + let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + + assert(address1.as_address().unwrap() == Address::zero()); + assert(contract_id1.as_address().is_none()); + assert( + address2 + .as_address() + .unwrap() == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), + ); + assert(contract_id2.as_address().is_none()); + assert( + address3 + .as_address() + .unwrap() == Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF), + ); + assert(contract_id3.as_address().is_none()); +} + +#[test] +fn identity_as_contract_id() { + let address1 = Identity::Address(Address::zero()); + let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let contract_id1 = Identity::ContractId(ContractId::zero()); + let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + + assert(address1.as_contract_id().is_none()); + assert(contract_id1.as_contract_id().unwrap() == ContractId::zero()); + assert(address2.as_contract_id().is_none()); + assert( + contract_id2 + .as_contract_id() + .unwrap() == ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001), + ); + assert(address3.as_contract_id().is_none()); + assert( + contract_id3 + .as_contract_id() + .unwrap() == ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF), + ); +} + +#[test] +fn identity_is_address() { + let address1 = Identity::Address(Address::zero()); + let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let contract_id1 = Identity::ContractId(ContractId::zero()); + let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + + assert(address1.is_address()); + assert(!contract_id1.is_address()); + assert(address2.is_address()); + assert(!contract_id2.is_address()); + assert(address3.is_address()); + assert(!contract_id3.is_address()); +} + +#[test] +fn identity_is_contract_id() { + let address1 = Identity::Address(Address::zero()); + let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let contract_id1 = Identity::ContractId(ContractId::zero()); + let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + + assert(!address1.is_contract_id()); + assert(contract_id1.is_contract_id()); + assert(!address2.is_contract_id()); + assert(contract_id2.is_contract_id()); + assert(!address3.is_contract_id()); + assert(contract_id3.is_contract_id()); +} + +#[test] +fn identity_bits() { + let address1 = Identity::Address(Address::zero()); + let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let contract_id1 = Identity::ContractId(ContractId::zero()); + let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + + assert(address1.bits() == b256::zero()); + assert(contract_id1.bits() == b256::zero()); + assert( + address2 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + assert( + contract_id2 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + assert( + address3 + .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + assert( + contract_id3 + .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); +} + +#[test] +fn identity_hash() { + use std::hash::{Hash, sha256}; + + let address1 = Identity::Address(Address::zero()); + let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + let contract_id1 = Identity::ContractId(ContractId::zero()); + let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); + + assert( + sha256(address1) == 0x7f9c9e31ac8256ca2f258583df262dbc7d6f68f2a03043d5c99a4ae5a7396ce9, + ); + assert( + sha256(contract_id1) == 0x1a7dfdeaffeedac489287e85be5e9c049a2ff6470f55cf30260f55395ac1b159, + ); + assert( + sha256(address2) == 0x1fd4247443c9440cb3c48c28851937196bc156032d70a96c98e127ecb347e45f, + ); + assert( + sha256(contract_id2) == 0x2e255099d6d6bee307c8e7075acc78f949897c5f67b53adf60724c814d7b90cb, + ); + assert( + sha256(address3) == 0x5e16d316ecd5773e50c3b02737d424192b02f25b4245822079181c557aafda7d, + ); + assert( + sha256(contract_id3) == 0x29fb7cd3be48a8d76bb031f0abce26caa9e092c000cd16bb101d30f63c4c1bc1, + ); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/Forc.toml new file mode 100644 index 00000000000..6ac992f8d23 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "math_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/src/main.sw new file mode 100644 index 00000000000..712409a7df2 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/src/main.sw @@ -0,0 +1,632 @@ +library; + +#[test] +fn math_root_u256() { + let max_u256 = u256::max(); + + assert(0x1u256.sqrt() == 1); + assert(0x4u256.sqrt() == 2); + assert(0x9u256.sqrt() == 3); + assert(0x90u256.sqrt() == 12); + assert(0x400u256.sqrt() == 32); + assert(0x2386f26fc10000u256.sqrt() == 100000000); + assert(0x0u256.sqrt() == 0); + assert(0x2u256.sqrt() == 1); + assert(0x5u256.sqrt() == 2); + assert(0x3e8u256.sqrt() == 31); + assert(max_u256.sqrt() == 0xffffffffffffffffffffffffffffffffu256); +} + +#[test] +fn math_root_u64() { + let max_u64 = u64::max(); + + assert(1.sqrt() == 1); + assert(4.sqrt() == 2); + assert(9.sqrt() == 3); + assert(144.sqrt() == 12); + assert(1024.sqrt() == 32); + assert(10000000000000000.sqrt() == 100000000); + assert(0.sqrt() == 0); + assert(2.sqrt() == 1); + assert(5.sqrt() == 2); + assert(1000.sqrt() == 31); + assert(max_u64.sqrt() == 4294967295); +} + +#[test] +fn math_root_u32() { + let max_u32 = u32::max(); + + assert(1u32.sqrt() == 1); + assert(4u32.sqrt() == 2); + assert(9u32.sqrt() == 3); + assert(144u32.sqrt() == 12); + assert(1024u32.sqrt() == 32); + assert(100000000u32.sqrt() == 10000); + assert(0u32.sqrt() == 0); + assert(2u32.sqrt() == 1); + assert(5u32.sqrt() == 2); + assert(1000u32.sqrt() == 31); + assert(max_u32.sqrt() == 65535); +} + +#[test] +fn math_root_u16() { + let max_u16 = u16::max(); + + assert(1u16.sqrt() == 1); + assert(4u16.sqrt() == 2); + assert(9u16.sqrt() == 3); + assert(144u16.sqrt() == 12); + assert(1024u16.sqrt() == 32); + assert(50625u16.sqrt() == 225); + assert(0u16.sqrt() == 0); + assert(2u16.sqrt() == 1); + assert(5u16.sqrt() == 2); + assert(1000u16.sqrt() == 31); + assert(max_u16.sqrt() == 255); +} + +#[test] +fn math_root_u8() { + let max_u8 = u8::max(); + + assert(1u8.sqrt() == 1); + assert(4u8.sqrt() == 2); + assert(9u8.sqrt() == 3); + assert(144u8.sqrt() == 12); + assert(0u8.sqrt() == 0); + assert(2u8.sqrt() == 1); + assert(5u8.sqrt() == 2); + assert(max_u8.sqrt() == 15); +} + +#[test] +fn math_power_u256() { + let five = 0x0000000000000000000000000000000000000000000000000000000000000005u256; + + // 5^2 = 25 = 0x19 + assert_eq( + five + .pow(2), + 0x0000000000000000000000000000000000000000000000000000000000000019u256, + ); + + // 5^28 = 0x204FCE5E3E2502611 (see https://www.wolframalpha.com/input?i=convert+5%5E28+in+hex) + assert_eq(five.pow(28), 0x0000000000000000204FCE5E3E2502611u256); +} + +#[test] +fn math_power_u64() { + assert(2.pow(2) == 4); + assert(2 ** 2 == 4); + + assert(2.pow(3) == 8); + assert(2 ** 3 == 8); + + assert(42.pow(2) == 1764); + assert(42 ** 2 == 1764); + + assert(42.pow(3) == 74088); + assert(42 ** 3 == 74088); + + assert(100.pow(5) == 10000000000); + assert(100 ** 5 == 10000000000); + + assert(100.pow(8) == 10000000000000000); + assert(100 ** 8 == 10000000000000000); + + assert(100.pow(9) == 1000000000000000000); + assert(100 ** 9 == 1000000000000000000); + + assert(2.pow(0) == 1); + assert(2 ** 0 == 1); + + assert(0.pow(1) == 0); + assert(0 ** 1 == 0); + + assert(0.pow(2) == 0); + assert(0 ** 2 == 0); +} + +#[test] +fn math_power_u32() { + assert(2u32.pow(2u32) == 4u32); + assert(2u32 ** 2u32 == 4u32); + + assert(2u32.pow(3u32) == 8u32); + assert(2u32 ** 3u32 == 8u32); + + assert(42u32.pow(2u32) == 1764u32); + assert(42u32 ** 2u32 == 1764u32); + + assert(100u32.pow(4u32) == 100000000u32); + assert(100u32 ** 4u32 == 100000000u32); + + assert(2u32.pow(0u32) == 1u32); + assert(2u32 ** 0u32 == 1u32); + + assert(0u32.pow(1u32) == 0u32); + assert(0u32 ** 1u32 == 0u32); + + assert(0u32.pow(2u32) == 0u32); + assert(0u32 ** 2u32 == 0u32); +} + +#[test] +fn math_power_u16() { + assert(2u16.pow(2u32) == 4u16); + assert(2u16 ** 2u32 == 4u16); + + assert(2u16.pow(3u32) == 8u16); + assert(2u16 ** 3u32 == 8u16); + + assert(42u16.pow(2u32) == 1764u16); + assert(42u16 ** 2u32 == 1764u16); + + assert(20u16.pow(3u32) == 8000u16); + assert(20u16 ** 3u32 == 8000u16); + + assert(15u16.pow(4u32) == 50625u16); + assert(15u16 ** 4u32 == 50625u16); + + assert(2u16.pow(0u32) == 1u16); + assert(2u16 ** 0u32 == 1u16); + + assert(0u16.pow(1u32) == 0u16); + assert(0u16 ** 1u32 == 0u16); + + assert(0u16.pow(2u32) == 0u16); + assert(0u16 ** 2u32 == 0u16); +} + +#[test] +fn math_power_u8() { + assert(2u8.pow(2u32) == 4u8); + assert(2u8 ** 2u32 == 4u8); + + assert(2u8.pow(3u32) == 8u8); + assert(2u8 ** 3u32 == 8u8); + + assert(4u8.pow(3u32) == 64u8); + assert(4u8 ** 3u32 == 64u8); + + assert(3u8.pow(4u32) == 81u8); + assert(3u8 ** 4u32 == 81u8); + + assert(10u8.pow(2u32) == 100u8); + assert(10u8 ** 2u32 == 100u8); + + assert(5u8.pow(3u32) == 125u8); + assert(5u8 ** 3u32 == 125u8); + + assert(3u8.pow(5u32) == 243u8); + assert(3u8 ** 5u32 == 243u8); + + assert(2u8.pow(0u32) == 1u8); + assert(2u8 ** 0u32 == 1u8); + + assert(0u8.pow(1u32) == 0u8); + assert(0u8 ** 1u32 == 0u8); + + assert(0u8.pow(2u32) == 0u8); + assert(0u8 ** 2u32 == 0u8); +} + +#[test] +fn math_log_u64() { + let max_u64 = u64::max(); + + assert(2.log(2) == 1); + assert(2.log2() == 1); + assert(1.log(3) == 0); + assert(8.log(2) == 3); + assert(8.log2() == 3); + assert(100.log(10) == 2); + assert(100.log(2) == 6); + assert(100.log2() == 6); + assert(100.log(9) == 2); + assert(max_u64.log(10) == 19); + assert(max_u64.log(2) == 63); +} + +#[test] +fn math_log_u32() { + let max_u32 = u32::max(); + + assert(2u32.log(2u32) == 1u32); + assert(100u32.log(10u32) == 2u32); + assert(125u32.log(5u32) == 3u32); + assert(256u32.log(4u32) == 4u32); + assert(max_u32.log(10) == 9); + assert(max_u32.log(2) == 31); +} + +#[test] +fn math_log_u16() { + let max_u16 = u16::max(); + + assert(7u16.log(7u16) == 1u16); + assert(49u16.log(7u16) == 2u16); + assert(27u16.log(3u16) == 3u16); + assert(1024u16.log(2u16) == 10u16); + assert(max_u16.log(10) == 4); + assert(max_u16.log(2) == 15); +} + +#[test] +fn math_log_u8() { + let max_u8 = u8::max(); + + assert(20u8.log(20u8) == 1u8); + assert(81u8.log(9u8) == 2u8); + assert(36u8.log(6u8) == 2u8); + assert(125u8.log(5u8) == 3u8); + assert(max_u8.log(10) == 2); + assert(max_u8.log(2) == 7); +} + +#[test] +fn math_log_u256() { + let max_u256 = u256::max(); + assert(0x2u256.log(0x2u256) == 0x1u256); + assert(0x1u256.log(0x3u256) == 0); + assert(0x8u256.log(0x2u256) == 0x3u256); + assert(0x64u256.log(0xau256) == 0x2u256); + assert(0x64u256.log(0x2u256) == 0x6u256); + assert(0x64u256.log(0x9u256) == 0x2u256); + assert(max_u256.log(0x2u256) == 0xffu256); +} + +#[test] +fn math_log2_u256() { + let max_u256 = u256::max(); + assert(0x2u256.log2() == 0x1u256); + assert(0x401u256.log2() == 0xau256); + assert(max_u256.log2() == 0xffu256); + assert(0x2u256.log2() == 0x1u256); + assert(0x8u256.log2() == 0x3u256); + assert(0x64u256.log2() == 0x6u256); +} + +#[test] +fn math_log2_u64() { + let max_u64 = u64::max(); + assert(max_u64.log2() == 63); +} + +#[test] +fn math_log2_u32() { + let max_u32 = u32::max(); + assert(max_u32.log2() == 31); +} + +#[test] +fn math_log2_u16() { + let max_u16 = u16::max(); + assert(max_u16.log2() == 15); +} + +#[test] +fn math_log2_u8() { + let max_u8 = u8::max(); + assert(max_u8.log2() == 7); +} + +#[test] +fn math_u8_zero() { + let my_u8 = u8::zero(); + assert(my_u8.is_zero()); + + let other_u8 = 1u8; + assert(!other_u8.is_zero()); +} + +#[test] +fn math_u16_zero() { + let my_u16 = u16::zero(); + assert(my_u16.is_zero()); + + let other_u16 = 1u16; + assert(!other_u16.is_zero()); +} + +#[test] +fn math_u32_zero() { + let my_u32 = u32::zero(); + assert(my_u32.is_zero()); + + let other_u32 = 1u32; + assert(!other_u32.is_zero()); +} + +#[test] +fn math_u64_zero() { + let my_u64 = u64::zero(); + assert(my_u64.is_zero()); + + let other_u64 = 1u64; + assert(!other_u64.is_zero()); +} + +#[test] +fn math_u256_zero() { + let my_u256 = u256::zero(); + assert(my_u256.is_zero()); + + let other_u256 = 0x01u256; + assert(!other_u256.is_zero()); +} + +#[test] +fn math_b256_zero() { + let my_b256 = b256::zero(); + assert(my_b256.is_zero()); + + let other_b256 = 0x0000000000000000000000000000000000000000000000000000000000000001; + assert(!other_b256.is_zero()); +} + +#[test] +fn math_test_parity_u256_log_with_ruint() { + // Failure cases found by comparing parity with ruint implementation of U256 + // https://docs.rs/ruint/latest/src/ruint/log.rs.html#45-89 + let a = [ + 2, 4, 4, 4, 4, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, + 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 15, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, + 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, + 25, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 29, + 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, + 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, + 33, 33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, + 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, + 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, + 35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, + 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, + 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, + 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, + 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, + 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, + 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, + 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, + 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, + 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, + 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, + 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, + 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 50, 50, + 50, 50, 50, 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 52, + 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 54, + 54, 54, 54, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, + 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 60, 60, + 60, 60, 61, 61, 61, 62, 62, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, + 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, + 65, 65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, + 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, + 66, 66, 66, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, + 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, + 67, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, + 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, + 68, 68, 68, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, + 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 70, + 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, + 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, + 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, + 72, 72, 72, 72, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, + 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74, 74, + 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, + 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, + 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, + 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, + 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, + 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, + 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 80, 80, 80, 80, + 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, + 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, + 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 83, 83, 83, 83, 83, 83, 83, 83, + 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 84, 84, 84, 84, + 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 85, + 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, + 85, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, + 86, 86, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, + 87, 87, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, + 88, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 90, + 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, + 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 92, 92, + 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, + 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 95, 95, 95, + 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, + 96, 96, 96, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 98, + 98, 98, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 100, 100, 100, 100, 100, + 100, 100, 100, 100, + ]; + let b = [ + 3, 3, 5, 6, 7, 3, 6, 7, 3, 7, 3, 3, 9, 10, 11, 12, 13, 14, 15, 3, 10, 11, 12, 13, 14, 15, + 3, 11, 12, 13, 14, 15, 3, 12, 13, 14, 15, 3, 13, 14, 15, 3, 14, 15, 3, 15, 3, 3, 5, 6, 7, + 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 7, 18, 19, 20, 21, 22, + 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 7, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 3, 5, 6, 7, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 7, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 7, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, + 6, 7, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, + 6, 7, 25, 26, 27, 28, 29, 30, 31, 3, 6, 7, 26, 27, 28, 29, 30, 31, 3, 6, 7, 27, 28, 29, 30, + 31, 3, 6, 7, 28, 29, 30, 31, 3, 6, 7, 29, 30, 31, 3, 6, 7, 30, 31, 3, 6, 7, 31, 3, 6, 7, 3, + 6, 7, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 6, 7, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, + 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 6, 7, + 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 3, 6, 7, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 37, 38, 39, 40, 41, 42, 43, + 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 38, + 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, + 62, 63, 3, 7, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 3, 7, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, + 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, + 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 44, 45, 46, 47, 48, 49, 50, 51, 52, + 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, + 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 3, 7, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, + 62, 63, 3, 7, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 49, 50, + 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 50, 51, 52, 53, 54, 55, 56, 57, 58, + 59, 60, 61, 62, 63, 3, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 52, 53, 54, + 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 54, + 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 56, 57, 58, + 59, 60, 61, 62, 63, 3, 57, 58, 59, 60, 61, 62, 63, 3, 58, 59, 60, 61, 62, 63, 3, 59, 60, + 61, 62, 63, 3, 60, 61, 62, 63, 3, 61, 62, 63, 3, 62, 63, 3, 63, 3, 3, 5, 6, 7, 9, 10, 11, + 12, 13, 14, 15, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, + 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, + 12, 13, 14, 15, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, + 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, + 13, 14, 15, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, + 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, + 15, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, + 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 69, 70, + 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, + 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 70, 71, 72, 73, 74, 75, + 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, + 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, + 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, + 10, 11, 12, 13, 14, 15, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 73, + 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, + 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 74, 75, 76, 77, 78, 79, 80, 81, 82, + 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, + 11, 12, 13, 14, 15, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, + 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 76, 77, 78, 79, 80, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, + 7, 9, 10, 11, 12, 13, 14, 15, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, + 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 78, 79, 80, 81, + 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, + 10, 11, 12, 13, 14, 15, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, + 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 80, 81, 82, 83, 84, 85, 86, 87, + 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, + 7, 10, 11, 12, 13, 14, 15, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, + 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, + 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 84, 85, 86, 87, 88, + 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 85, + 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, + 14, 15, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, + 12, 13, 14, 15, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, + 11, 12, 13, 14, 15, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, + 11, 12, 13, 14, 15, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, + 12, 13, 14, 15, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, + 14, 15, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 92, + 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 93, 94, 95, 96, 97, + 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, + 10, 11, 12, 13, 14, 15, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 96, + 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, + 12, 13, 14, 15, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 99, 100, 3, 5, 6, 7, 10, + 11, 12, 13, 14, 15, 100, 3, 5, 6, 7, 11, 12, 13, 14, 15, + ]; + let expected = [ + 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, + 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 2, 0, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, + 0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0, 0, 0, 3, 1, 1, 0, + 0, 0, 3, 1, 1, 0, 0, 3, 1, 1, 0, 3, 1, 1, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, + 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 3, 0, 3, 3, 2, 2, 2, 1, 1, 1, + 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, + 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, + 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, + 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, + 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, + 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, + 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, + 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, + 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, + 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, + 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 4, 2, 2, 2, + 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, + 1, 1, 1, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, + ]; + + let mut i = 0; + + while i < 1825 { + let ai: u64 = a[i]; + let bi: u64 = b[i]; + let expected_val: u64 = expected[i]; + + let result = u256::from(ai).log(u256::from(bi)); + assert_eq(result, u256::from(expected_val)); + i += 1; + } +} + +#[test(should_revert)] +fn math_u256_log_fail_base_0() { + let _result = u256::from(2_u64).log(u256::from(0_u64)); +} + +#[test(should_revert)] +fn math_u256_log_fail_base_1() { + let _result = u256::from(2_u64).log(u256::from(1_u64)); +} + +#[test(should_revert)] +fn math_u256_log_fail_x_0() { + let _result = u256::from(0_u64).log(u256::from(2_u64)); +} + +#[test(should_revert)] +fn math_u256_log2_fail_x_0() { + let _result = u256::from(0_u64).log2(); +} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/Forc.toml new file mode 100644 index 00000000000..4a79bcec8cd --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "option_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/src/main.sw new file mode 100644 index 00000000000..7ca19c1d4d7 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/src/main.sw @@ -0,0 +1,354 @@ +library; + +#[test] +fn option_eq() { + use std::bytes::Bytes; + + let u8_1 = Some(1u8); + let u8_2 = Some(1u8); + let u16_1 = Some(1u16); + let u16_2 = Some(1u16); + let u32_1 = Some(1u32); + let u32_2 = Some(1u32); + let u64_1 = Some(1u64); + let u64_2 = Some(1u64); + let u256_1 = Some(0x01u256); + let u256_2 = Some(0x01u256); + let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); + let b256_2 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let struct_2 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let enum_2 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let _array_1 = Some([0u64, 0u64]); + let _array_2 = Some([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let mut bytes_2 = Bytes::new(); + bytes_2.push(1u8); + let heap_1 = Some(bytes_1); + let heap_2 = Some(bytes_2); + let none_1 = Option::::None; + let none_2 = Option::::None; + + assert(u8_1 == u8_2); + assert(u16_1 == u16_2); + assert(u32_1 == u32_2); + assert(u64_1 == u64_2); + assert(u256_1 == u256_2); + assert(b256_1 == b256_2); + assert(struct_1 == struct_2); + assert(enum_1 == enum_2); + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // assert(array_1 == array_2); + assert(heap_1 == heap_2); + assert(none_1 == none_2); +} + +#[test] +fn option_ne() { + use std::bytes::Bytes; + + let u8_1 = Some(1u8); + let u8_2 = Some(2u8); + let u16_1 = Some(1u16); + let u16_2 = Some(2u16); + let u32_1 = Some(1u32); + let u32_2 = Some(2u32); + let u64_1 = Some(1u64); + let u64_2 = Some(2u64); + let u256_1 = Some(0x01u256); + let u256_2 = Some(0x02u256); + let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); + let b256_2 = Some(0x0000000000000000000000000000000000000000000000000000000000000002); + let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let struct_2 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000002)); + let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let enum_2 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000002))); + let _array_1 = Some([0u64, 0u64]); + let _array_2 = Some([0u64, 1u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let mut bytes_2 = Bytes::new(); + bytes_2.push(2u8); + let heap_1 = Some(bytes_1); + let heap_2 = Some(bytes_2); + let none_1 = Option::::None; + + assert(u8_1 != u8_2); + assert(u16_1 != u16_2); + assert(u32_1 != u32_2); + assert(u64_1 != u64_2); + assert(u256_1 != u256_2); + assert(b256_1 != b256_2); + assert(struct_1 != struct_2); + assert(enum_1 != enum_2); + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // assert(array_1 != array_2); + assert(heap_1 != heap_2); + assert(none_1 != u64_1); +} + +#[test] +fn option_is_some() { + use std::bytes::Bytes; + + let u8_1 = Some(1u8); + let u16_1 = Some(1u16); + let u32_1 = Some(1u32); + let u64_1 = Some(1u64); + let u256_1 = Some(0x01u256); + let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let array_1 = Some([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1 = Some(bytes_1); + let none_1 = Option::::None; + + assert(u8_1.is_some()); + assert(u16_1.is_some()); + assert(u32_1.is_some()); + assert(u64_1.is_some()); + assert(u256_1.is_some()); + assert(b256_1.is_some()); + assert(struct_1.is_some()); + assert(enum_1.is_some()); + assert(array_1.is_some()); + assert(heap_1.is_some()); + assert(!none_1.is_some()); +} + +#[test] +fn option_is_none() { + use std::bytes::Bytes; + + let u8_1 = Some(1u8); + let u16_1 = Some(1u16); + let u32_1 = Some(1u32); + let u64_1 = Some(1u64); + let u256_1 = Some(0x01u256); + let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let array_1 = Some([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1 = Some(bytes_1); + let none_1 = Option::::None; + + assert(!u8_1.is_none()); + assert(!u16_1.is_none()); + assert(!u32_1.is_none()); + assert(!u64_1.is_none()); + assert(!u256_1.is_none()); + assert(!b256_1.is_none()); + assert(!struct_1.is_none()); + assert(!enum_1.is_none()); + assert(!array_1.is_none()); + assert(!heap_1.is_none()); + assert(none_1.is_none()); +} + +#[test] +fn option_unwrap() { + use std::bytes::Bytes; + + let u8_1 = Some(1u8); + let u16_1 = Some(1u16); + let u32_1 = Some(1u32); + let u64_1 = Some(1u64); + let u256_1 = Some(0x01u256); + let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let _array_1 = Some([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1 = Some(bytes_1); + + assert(u8_1.unwrap() == 1u8); + assert(u16_1.unwrap() == 1u16); + assert(u32_1.unwrap() == 1u32); + assert(u64_1.unwrap() == 1u64); + assert(u256_1.unwrap() == 0x01u256); + assert( + b256_1 + .unwrap() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + assert( + struct_1 + .unwrap() == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), + ); + assert( + enum_1 + .unwrap() == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), + ); + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // assert(array_1.unwrap() == [0u64, 0u64]); + assert(heap_1.unwrap() == bytes_1); +} + +#[test(should_revert)] +fn revert_option_when_unwrap_none() { + let none = Option::::None; + let _result = none.unwrap(); +} + +#[test] +fn option_unwrap_or() { + use std::bytes::Bytes; + + let u8_1 = Some(1u8); + let u16_1 = Some(1u16); + let u32_1 = Some(1u32); + let u64_1 = Some(1u64); + let u256_1 = Some(0x01u256); + let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let _array_1 = Some([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1 = Some(bytes_1); + let none_1 = Option::::None; + + assert(u8_1.unwrap_or(2u8) == 1u8); + assert(u16_1.unwrap_or(2u16) == 1u16); + assert(u32_1.unwrap_or(2u32) == 1u32); + assert(u64_1.unwrap_or(2u64) == 1u64); + assert(u256_1.unwrap_or(0x02u256) == 0x01u256); + assert( + b256_1 + .unwrap_or(0x0000000000000000000000000000000000000000000000000000000000000002) == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + assert( + struct_1 + .unwrap_or(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)) == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), + ); + assert( + enum_1 + .unwrap_or(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000002))) == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), + ); + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // assert(array_1.unwrap_or([1u64, 1u64]) == [0u64, 0u64]); + assert(heap_1.unwrap_or(Bytes::new()) == bytes_1); + assert(none_1.unwrap_or(10u64) == 10u64); +} + +#[test] +fn option_ok_or() { + use std::bytes::Bytes; + + let u8_1 = Some(1u8); + let u16_1 = Some(1u16); + let u32_1 = Some(1u32); + let u64_1 = Some(1u64); + let u256_1 = Some(0x01u256); + let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let _array_1 = Some([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1 = Some(bytes_1); + let none_1 = Option::::None; + + match u8_1.ok_or(2u8) { + Result::Ok(underlying) => assert(underlying == 1u8), + Result::Err => revert(0), + }; + match u16_1.ok_or(2u16) { + Result::Ok(underlying) => assert(underlying == 1u16), + Result::Err => revert(0), + }; + match u32_1.ok_or(2u32) { + Result::Ok(underlying) => assert(underlying == 1u32), + Result::Err => revert(0), + }; + match u64_1.ok_or(2u64) { + Result::Ok(underlying) => assert(underlying == 1u64), + Result::Err => revert(0), + }; + match u256_1.ok_or(0x02u256) { + Result::Ok(underlying) => assert(underlying == 0x01u256), + Result::Err => revert(0), + }; + match b256_1.ok_or(0x0000000000000000000000000000000000000000000000000000000000000002) { + Result::Ok(underlying) => assert( + underlying == 0x0000000000000000000000000000000000000000000000000000000000000001, + ), + Result::Err => revert(0), + }; + match struct_1.ok_or(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)) { + Result::Ok(underlying) => assert( + underlying == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), + ), + Result::Err => revert(0), + }; + match enum_1.ok_or(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000002))) { + Result::Ok(underlying) => assert( + underlying == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), + ), + Result::Err => revert(0), + }; + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // match array_1.ok_or([1u64, 1u64]) { + // Result::Ok(underlying) => assert(underlying == [0u64, 0u64]), + // Result::Err => revert(0), + // }; + match heap_1.ok_or(Bytes::new()) { + Result::Ok(underlying) => assert(underlying == bytes_1), + Result::Err => revert(0), + } + match none_1.ok_or(10u64) { + Result::Ok(_) => revert(0), + Result::Err(underlying) => assert(underlying == 10u64), + } +} + +#[test] +fn option_expect() { + use std::bytes::Bytes; + + let u8_1 = Some(1u8); + let u16_1 = Some(1u16); + let u32_1 = Some(1u32); + let u64_1 = Some(1u64); + let u256_1 = Some(0x01u256); + let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let _array_1 = Some([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1 = Some(bytes_1); + + assert(u8_1.expect("Failed Test") == 1u8); + assert(u16_1.expect("Failed Test") == 1u16); + assert(u32_1.expect("Failed Test") == 1u32); + assert(u64_1.expect("Failed Test") == 1u64); + assert(u256_1.expect("Failed Test") == 0x01u256); + assert( + b256_1 + .expect("Failed Test") == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + assert( + struct_1 + .expect("Failed Test") == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), + ); + assert( + enum_1 + .expect("Failed Test") == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), + ); + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // assert(array_1.expect("Failed Test") == [0u64, 0u64]); + assert(heap_1.expect("Failed Test") == bytes_1); +} + +#[test(should_revert)] +fn revert_option_expect_when_none() { + let none_1 = Option::::None; + let _result = none_1.expect("Failed Test"); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/Forc.toml new file mode 100644 index 00000000000..7df7ff1fc55 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "primitive_conversions_b256_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw new file mode 100644 index 00000000000..21fe0d19f71 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw @@ -0,0 +1,168 @@ +library; + +use std::{b512::B512, bytes::Bytes, primitive_conversions::b256::*, u128::U128}; + +#[test] +fn b256_try_from_bytes() { + let mut initial_bytes = Bytes::with_capacity(32); + let mut i = 0; + while i < 32 { + // 0x33 is 51 in decimal + initial_bytes.push(51u8); + i += 1; + } + let res1 = b256::try_from(initial_bytes); + let expected1 = 0x3333333333333333333333333333333333333333333333333333333333333333; + assert(res1.is_some()); + assert(res1.unwrap() == expected1); + + let mut second_bytes = Bytes::with_capacity(33); + i = 0; + while i < 33 { + // 0x33 is 51 in decimal + second_bytes.push(51u8); + i += 1; + } + let res2 = b256::try_from(second_bytes); + assert(res2.is_none()); + + // bytes is still available to use: + assert(second_bytes.len() == 33); + assert(second_bytes.capacity() == 33); + + let mut third_bytes = Bytes::with_capacity(31); + let mut i = 0; + while i < 31 { + // 0x33 is 51 in decimal + third_bytes.push(51u8); + i += 1; + } + let res3 = b256::try_from(third_bytes); + assert(res3.is_none()); +} + +#[test] +fn b256_try_from_b512() { + let b512_value = B512::new(); + let b256_value = b256::try_from(b512_value); + assert(b256_value.is_some()); + + let b512_value = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000001, + b256::zero(), + )); + let b256_value = b256::try_from(b512_value); + assert(b256_value.is_none()); +} + +#[test] +fn b256_from_u256() { + let u256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000_u256; + let u256_2 = 0x0000000000000000000000000000000000000000000000000000000000000001_u256; + let u256_3 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u256; + + let res1 = b256::from(u256_1); + let res2 = b256::from(u256_2); + let res3 = b256::from(u256_3); + + assert(res1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + assert(res2 == 0x0000000000000000000000000000000000000000000000000000000000000001); + assert(res3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); +} + +#[test] +fn b256_into_u256() { + let u256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000_u256; + let u256_2 = 0x0000000000000000000000000000000000000000000000000000000000000001_u256; + let u256_3 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u256; + + let res1: b256 = u256_1.into(); + let res2: b256 = u256_2.into(); + let res3: b256 = u256_3.into(); + + assert(res1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + assert(res2 == 0x0000000000000000000000000000000000000000000000000000000000000001); + assert(res3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); +} + +#[test] +fn b256_from_u128() { + let b256_value1 = b256::from(U128::from((u64::min(), u64::min()))); + let b256_value2 = b256::from(U128::from((1_u64, 1_u64))); + let b256_value3 = b256::from(U128::from((u64::max(), u64::max()))); + let b256_value4 = b256::from(U128::from((u64::max(), u64::min()))); + let b256_value5 = b256::from(U128::from((u64::min(), u64::max()))); + + assert( + b256_value1 == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + assert( + b256_value2 == 0x0000000000000000000000000000000000000000000000010000000000000001, + ); + assert( + b256_value3 == 0x00000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + assert( + b256_value4 == 0x00000000000000000000000000000000ffffffffffffffff0000000000000000, + ); + assert( + b256_value5 == 0x000000000000000000000000000000000000000000000000ffffffffffffffff, + ); +} + +#[test] +fn b256_into_u128() { + let u128_1 = U128::from((u64::min(), u64::min())); + let u128_2 = U128::from((1_u64, 1_u64)); + let u128_3 = U128::from((u64::max(), u64::max())); + let u128_4 = U128::from((u64::max(), u64::min())); + let u128_5 = U128::from((u64::min(), u64::max())); + + let b256_value1: b256 = u128_1.into(); + let b256_value2: b256 = u128_2.into(); + let b256_value3: b256 = u128_3.into(); + let b256_value4: b256 = u128_4.into(); + let b256_value5: b256 = u128_5.into(); + + assert( + b256_value1 == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + assert( + b256_value2 == 0x0000000000000000000000000000000000000000000000010000000000000001, + ); + assert( + b256_value3 == 0x00000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ); + assert( + b256_value4 == 0x00000000000000000000000000000000ffffffffffffffff0000000000000000, + ); + assert( + b256_value5 == 0x000000000000000000000000000000000000000000000000ffffffffffffffff, + ); +} + +#[test] +fn b256_from_tuple() { + let b256_1 = b256::from((0, 0, 0, 0)); + let b256_2 = b256::from((1, 2, 3, 4)); + let b256_3 = b256::from((u64::max(), u64::max(), u64::max(), u64::max())); + + assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + assert(b256_2 == 0x0000000000000001000000000000000200000000000000030000000000000004); + assert(b256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); +} + +#[test] +fn b256_into_tuple() { + let tuple_1 = (0, 0, 0, 0); + let tuple_2 = (1, 2, 3, 4); + let tuple_3 = (u64::max(), u64::max(), u64::max(), u64::max()); + + let b256_1: b256 = tuple_1.into(); + let b256_2: b256 = tuple_2.into(); + let b256_3: b256 = tuple_3.into(); + + assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + assert(b256_2 == 0x0000000000000001000000000000000200000000000000030000000000000004); + assert(b256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/Forc.toml new file mode 100644 index 00000000000..40441657520 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "primitive_conversions_str_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/src/main.sw new file mode 100644 index 00000000000..e47430dd626 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/src/main.sw @@ -0,0 +1,14 @@ +library; + +use std::primitive_conversions::str::*; + +#[test] +fn str_slice_to_str_array() { + let a = "abcd"; + let b: str[4] = a.try_as_str_array().unwrap(); + assert(__size_of_str_array::() == a.len() && __size_of_val(b) == 8); + + let c = from_str_array(b); + + assert(a == c); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/Forc.toml new file mode 100644 index 00000000000..c1aec88c9bb --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "primitive_conversions_u16_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/src/main.sw new file mode 100644 index 00000000000..6ac9fc8fb8a --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/src/main.sw @@ -0,0 +1,129 @@ +library; + +use std::{primitive_conversions::u16::*, u128::U128}; + +#[test] +fn u16_from_u8() { + let u8_1: u8 = u8::min(); + let u8_2: u8 = 255u8; + let u8_3: u8 = 2u8; + + let u16_1 = u16::from(u8_1); + let u16_2 = u16::from(u8_2); + let u16_3 = u16::from(u8_3); + + assert(u16_1 == 0u16); + assert(u16_2 == 255u16); + assert(u16_3 == 2u16); +} + +#[test] +fn u16_into_u8() { + let u8_1: u8 = u8::min(); + let u8_2: u8 = 255u8; + let u8_3: u8 = 2u8; + + let u16_1: u16 = u8_1.into(); + let u16_2: u16 = u8_2.into(); + let u16_3: u16 = u8_3.into(); + + assert(u16_1 == 0u16); + assert(u16_2 == 255u16); + assert(u16_3 == 2u16); +} + +#[test] +fn u16_try_from_u32() { + let u32_1: u32 = u32::min(); + let u32_2: u32 = u16::max().as_u32(); + let u32_3: u32 = u16::max().as_u32() + 1; + let u32_4: u32 = 2u32; + + let u16_1 = u16::try_from(u32_1); + let u16_2 = u16::try_from(u32_2); + let u16_3 = u16::try_from(u32_3); + let u16_4 = u16::try_from(u32_4); + + assert(u16_1.is_some()); + assert(u16_1.unwrap() == 0u16); + + assert(u16_2.is_some()); + assert(u16_2.unwrap() == u16::max()); + + assert(u16_3.is_none()); + + assert(u16_4.is_some()); + assert(u16_4.unwrap() == 2u16); +} + +#[test] +fn u16_try_from_u64() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = 2; + let u64_3: u64 = u16::max().as_u64(); + let u64_4: u64 = u16::max().as_u64() + 1; + + let u16_1 = u16::try_from(u64_1); + let u16_2 = u16::try_from(u64_2); + let u16_3 = u16::try_from(u64_3); + let u16_4 = u16::try_from(u64_4); + + assert(u16_1.is_some()); + assert(u16_1.unwrap() == 0u16); + + assert(u16_2.is_some()); + assert(u16_2.unwrap() == 2u16); + + assert(u16_3.is_some()); + assert(u16_3.unwrap() == u16::max()); + + assert(u16_4.is_none()); +} + +#[test] +fn u16_try_from_u256() { + let u256_1: u256 = u256::min(); + let u256_2: u256 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; + let u256_3: u256 = u16::max().as_u256(); + let u256_4: u256 = 0x1000000000000000000000000000000000000000000000000000000000000000u256; + + let u16_1 = u16::try_from(u256_1); + let u16_2 = u16::try_from(u256_2); + let u16_3 = u16::try_from(u256_3); + let u16_4 = u16::try_from(u256_4); + + assert(u16_1.is_some()); + assert(u16_1.unwrap() == 0u16); + + assert(u16_2.is_some()); + assert(u16_2.unwrap() == 2u16); + + assert(u16_3.is_some()); + assert(u16_3.unwrap() == u16::max()); + + assert(u16_4.is_none()); +} + +#[test] +fn u16_try_from_u128() { + let u128_1: U128 = U128::new(); + let u128_2: U128 = U128::from((0, 2u64)); + let u128_3: U128 = U128::from((0, u16::max().as_u64())); + let u128_4: U128 = U128::from((0, u16::max().as_u64() + 1)); + + let u16_1 = u16::try_from(u128_1); + let u16_2 = u16::try_from(u128_2); + let u16_3 = u16::try_from(u128_3); + let u16_4 = u16::try_from(u128_4); + + assert(u16_1.is_some()); + assert(u16_1.unwrap() == 0u16); + + assert(u16_2.is_some()); + assert(u16_2.unwrap() == 2u16); + + assert(u16_3.is_some()); + assert(u16_3.unwrap() == u16::max()); + + assert(u16_4.is_none()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/Forc.toml new file mode 100644 index 00000000000..7aba4002607 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "primitive_conversions_u256_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/src/main.sw new file mode 100644 index 00000000000..16e3f754344 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/src/main.sw @@ -0,0 +1,311 @@ +library; + +use std::{b512::B512, primitive_conversions::u256::*, u128::U128}; + +#[test] +fn u256_from_u8() { + let u256_1 = u256::from(u8::min()); + let u256_2 = u256::from(2_u8); + let u256_3 = u256::from(u8::max()); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256, + ); +} + +#[test] +fn u256_into_u8() { + let u8_1 = u8::min(); + let u8_2 = 2_u8; + let u8_3 = u8::max(); + + let u256_1: u256 = u8_1.into(); + let u256_2: u256 = u8_2.into(); + let u256_3: u256 = u8_3.into(); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256, + ); +} + +#[test] +fn u256_from_u16() { + let u256_1 = u256::from(u16::min()); + let u256_2 = u256::from(2_u16); + let u256_3 = u256::from(u16::max()); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256, + ); +} + +#[test] +fn u256_into_u16() { + let u16_1 = u16::min(); + let u16_2 = 2u16; + let u16_3 = u16::max(); + + let u256_1: u256 = u16_1.into(); + let u256_2: u256 = u16_2.into(); + let u256_3: u256 = u16_3.into(); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256, + ); +} + +#[test] +fn u256_from_u32() { + let u256_1 = u256::from(u32::min()); + let u256_2 = u256::from(2u32); + let u256_3 = u256::from(u32::max()); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256, + ); +} + +#[test] +fn u256_into_u32() { + let u32_1 = u32::min(); + let u32_2 = 2u32; + let u32_3 = u32::max(); + + let u256_1: u256 = u32_1.into(); + let u256_2: u256 = u32_2.into(); + let u256_3: u256 = u32_3.into(); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256, + ); +} + +#[test] +fn u256_from_u64() { + let u256_1 = u256::from(u64::min()); + let u256_2 = u256::from(2u64); + let u256_3 = u256::from(u64::max()); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256, + ); +} + +#[test] +fn u256_into_u64() { + let u64_1 = u64::min(); + let u64_2 = 2u64; + let u64_3 = u64::max(); + + let u256_1: u256 = u64_1.into(); + let u256_2: u256 = u64_2.into(); + let u256_3: u256 = u64_3.into(); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256, + ); +} + +#[test] +fn u256_from_b256() { + let u256_1 = u256::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let u256_2 = u256::from(0x0000000000000000000000000000000000000000000000000000000000000002); + let u256_3 = u256::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256, + ); +} + +#[test] +fn u256_into_b256() { + let b256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000; + let b256_2 = 0x0000000000000000000000000000000000000000000000000000000000000002; + let b256_3 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff; + + let u256_1: u256 = b256_1.into(); + let u256_2: u256 = b256_2.into(); + let u256_3: u256 = b256_3.into(); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256, + ); +} + +#[test] +fn u256_from_u128() { + let u256_1 = u256::from(U128::from((u64::min(), u64::min()))); + let u256_2 = u256::from(U128::from((0u64, 2u64))); + let u256_3 = u256::from(U128::from((u64::max(), u64::max()))); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff_u256, + ); +} + +#[test] +fn u256_into_u128() { + let u128_1 = U128::from((u64::min(), u64::min())); + let u128_2 = U128::from((0u64, 2u64)); + let u128_3 = U128::from((u64::max(), u64::max())); + + let u256_1: u256 = u128_1.into(); + let u256_2: u256 = u128_2.into(); + let u256_3: u256 = u128_3.into(); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + assert( + u256_3 == 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff_u256, + ); +} + +#[test] +fn u256_from_tuple() { + let u256_1 = u256::from((u64::min(), u64::min(), u64::min(), u64::min())); + let u256_2 = u256::from((1, 2, 3, 4)); + let u256_3 = u256::from((u64::max(), u64::max(), u64::max(), u64::max())); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000001000000000000000200000000000000030000000000000004_u256, + ); + assert( + u256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u256, + ); +} + +#[test] +fn u256_into_tuple() { + let tuple_1 = (u64::min(), u64::min(), u64::min(), u64::min()); + let tuple_2 = (1, 2, 3, 4); + let tuple_3 = (u64::max(), u64::max(), u64::max(), u64::max()); + + let u256_1: u256 = tuple_1.into(); + let u256_2: u256 = tuple_2.into(); + let u256_3: u256 = tuple_3.into(); + + assert( + u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + assert( + u256_2 == 0x0000000000000001000000000000000200000000000000030000000000000004_u256, + ); + assert( + u256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u256, + ); +} + +#[test] +fn u256_try_from_b512() { + let b512_1 = B512::new(); + let b512_2 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000002, + )); + let b512_3 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + )); + let b512_4 = B512::from(( + 0x0000000000000000000000000000000000000000000000000000000000000001, + 0x0000000000000000000000000000000000000000000000000000000000000000, + )); + + let u256_1 = u256::try_from(b512_1); + let u256_2 = u256::try_from(b512_2); + let u256_3 = u256::try_from(b512_3); + let u256_4 = u256::try_from(b512_4); + + assert(u256_1.is_some()); + assert( + u256_1 + .unwrap() == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, + ); + + assert(u256_2.is_some()); + assert( + u256_2 + .unwrap() == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, + ); + + assert(u256_3.is_some()); + assert( + u256_3 + .unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u256, + ); + + assert(u256_4.is_none()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/Forc.toml new file mode 100644 index 00000000000..f49eef53b36 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "primitive_conversions_u32_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/src/main.sw new file mode 100644 index 00000000000..862cab88c91 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/src/main.sw @@ -0,0 +1,135 @@ +library; + +use std::{primitive_conversions::u32::*, u128::U128}; + +#[test] +fn u32_from_u8() { + let u8_1: u8 = u8::min(); + let u8_2: u8 = 1u8; + let u8_3: u8 = u8::max(); + + let u32_1 = u32::from(u8_1); + let u32_2 = u32::from(u8_2); + let u32_3 = u32::from(u8_3); + + assert(u32_1 == 0u32); + assert(u32_2 == 1u32); + assert(u32_3 == 255u32); +} + +#[test] +fn u32_into_u8() { + let u8_1: u8 = u8::min(); + let u8_2: u8 = 1u8; + let u8_3: u8 = u8::max(); + + let u32_1: u32 = u8_1.into(); + let u32_2: u32 = u8_2.into(); + let u32_3: u32 = u8_3.into(); + + assert(u32_1 == 0u32); + assert(u32_2 == 1u32); + assert(u32_3 == 255u32); +} + +#[test] +fn u32_from_u16() { + let u16_1: u16 = u16::min(); + let u16_2: u16 = 1u16; + let u16_3: u16 = u16::max(); + + let u32_1 = u32::from(u16_1); + let u32_2 = u32::from(u16_2); + let u32_3 = u32::from(u16_3); + + assert(u32_1 == 0u32); + assert(u32_2 == 1u32); + assert(u32_3 == 65535u32); +} + +#[test] +fn u32_into_u16() { + let u16_1: u16 = u16::min(); + let u16_2: u16 = 1u16; + let u16_3: u16 = u16::max(); + + let u32_1: u32 = u16_1.into(); + let u32_2: u32 = u16_2.into(); + let u32_3: u32 = u16_3.into(); + + assert(u32_1 == 0u32); + assert(u32_2 == 1u32); + assert(u32_3 == 65535u32); +} + +#[test] +fn u32_try_from_u64() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = 2u64; + let u64_3: u64 = u32::max().as_u64(); + let u64_4: u64 = u32::max().as_u64() + 1; + + let u32_1 = u32::try_from(u64_1); + let u32_2 = u32::try_from(u64_2); + let u32_3 = u32::try_from(u64_3); + let u32_4 = u32::try_from(u64_4); + + assert(u32_1.is_some()); + assert(u32_1.unwrap() == 0u32); + + assert(u32_2.is_some()); + assert(u32_2.unwrap() == 2u32); + + assert(u32_3.is_some()); + assert(u32_3.unwrap() == u32::max()); + + assert(u32_4.is_none()); +} + +#[test] +fn u32_try_from_u256() { + let u256_1: u256 = 0x0000000000000000000000000000000000000000000000000000000000000000u256; + let u256_2: u256 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; + let u256_3: u256 = u32::max().as_u256(); + let u256_4: u256 = u32::max().as_u256() + 1; + + let u32_1 = u32::try_from(u256_1); + let u32_2 = u32::try_from(u256_2); + let u32_3 = u32::try_from(u256_3); + let u32_4 = u32::try_from(u256_4); + + assert(u32_1.is_some()); + assert(u32_1.unwrap() == 0u32); + + assert(u32_2.is_some()); + assert(u32_2.unwrap() == 2u32); + + assert(u32_3.is_some()); + assert(u32_3.unwrap() == u32::max()); + + assert(u32_4.is_none()); +} + +#[test] +fn u32_try_from_u128() { + let u128_1: U128 = U128::new(); + let u128_2: U128 = U128::from((0u64, 2u32.as_u64())); + let u128_3: U128 = U128::from((0u64, u32::max().as_u64())); + let u128_4: U128 = U128::from((0, u32::max().as_u64() + 1)); + + let u32_1 = u32::try_from(u128_1); + let u32_2 = u32::try_from(u128_2); + let u32_3 = u32::try_from(u128_3); + let u32_4 = u32::try_from(u128_4); + + assert(u32_1.is_some()); + assert(u32_1.unwrap() == 0u32); + + assert(u32_2.is_some()); + assert(u32_2.unwrap() == 2u32); + + assert(u32_3.is_some()); + assert(u32_3.unwrap() == u32::max()); + + assert(u32_4.is_none()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/Forc.toml new file mode 100644 index 00000000000..e44d33356eb --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "primitive_conversions_u64_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/src/main.sw new file mode 100644 index 00000000000..e82b953c1ab --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/src/main.sw @@ -0,0 +1,141 @@ +library; + +use std::{primitive_conversions::u64::*, u128::U128}; + +#[test] +fn u64_from_u8() { + let u8_1: u8 = 0u8; + let u8_2: u8 = 2u8; + let u8_3: u8 = u8::max(); + + let u64_1 = u64::from(u8_1); + let u64_2 = u64::from(u8_2); + let u64_3 = u64::from(u8_3); + + assert(u64_1 == 0u64); + assert(u64_2 == 2u64); + assert(u64_3 == 255u64); +} + +#[test] +fn u64_into_u8() { + let u8_1: u8 = 0u8; + let u8_2: u8 = 2u8; + let u8_3: u8 = u8::max(); + + let u64_1: u64 = u8_1.into(); + let u64_2: u64 = u8_2.into(); + let u64_3: u64 = u8_3.into(); + + assert(u64_1 == 0u64); + assert(u64_2 == 2u64); + assert(u64_3 == 255u64); +} + +#[test] +fn u64_from_u16() { + let u16_1: u16 = u16::min(); + let u16_2: u16 = 2u16; + let u16_3: u16 = u16::max(); + + let u64_1 = u64::from(u16_1); + let u64_2 = u64::from(u16_2); + let u64_3 = u64::from(u16_3); + + assert(u64_1 == 0u64); + assert(u64_2 == 2u64); + assert(u64_3 == 65535u64); +} + +#[test] +fn u64_into_u16() { + let u16_1: u16 = u16::min(); + let u16_2: u16 = 2u16; + let u16_3: u16 = u16::max(); + + let u64_1: u64 = u16_1.into(); + let u64_2: u64 = u16_2.into(); + let u64_3: u64 = u16_3.into(); + + assert(u64_1 == 0u64); + assert(u64_2 == 2u64); + assert(u64_3 == 65535u64); +} + +#[test] +fn u64_from_u32() { + let u32_1: u32 = 0u32; + let u32_2: u32 = 2u32; + let u32_3: u32 = u32::max(); + + let u64_1 = u64::from(u32_1); + let u64_2 = u64::from(u32_2); + let u64_3 = u64::from(u32_3); + + assert(u64_1 == 0u64); + assert(u64_2 == 2u64); + assert(u64_3 == 4294967295u64); +} + +#[test] +fn u64_into_u32() { + let u32_1: u32 = 0u32; + let u32_2: u32 = 2u32; + let u32_3: u32 = u32::max(); + + let u64_1: u64 = u32_1.into(); + let u64_2: u64 = u32_2.into(); + let u64_3: u64 = u32_3.into(); + + assert(u64_1 == 0u64); + assert(u64_2 == 2u64); + assert(u64_3 == 4294967295u64); +} + +#[test] +fn u64_try_from_u256() { + let u256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000u256; + let u256_2 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; + let u256_3 = u64::max().as_u256(); + let u256_4 = u64::max().as_u256() + 1; + + let u64_1 = u64::try_from(u256_1); + let u64_2 = u64::try_from(u256_2); + let u64_3 = u64::try_from(u256_3); + let u64_4 = u64::try_from(u256_4); + + assert(u64_1.is_some()); + assert(u64_1.unwrap() == 0); + + assert(u64_2.is_some()); + assert(u64_2.unwrap() == 2); + + assert(u64_3.is_some()); + assert(u64_3.unwrap() == u64::max()); + + assert(u64_4.is_none()); +} + +#[test] +fn u64_try_from_u128() { + let u128_1: U128 = U128::new(); + let u128_2: U128 = U128::from((0, 2)); + let u128_3: U128 = U128::from((0, u64::max())); + let u128_4: U128 = U128::from((1, 0)); + + let u64_1 = u64::try_from(u128_1); + let u64_2 = u64::try_from(u128_2); + let u64_3 = u64::try_from(u128_3); + let u64_4 = u64::try_from(u128_4); + + assert(u64_1.is_some()); + assert(u64_1.unwrap() == 0u64); + + assert(u64_2.is_some()); + assert(u64_2.unwrap() == 2u64); + + assert(u64_3.is_some()); + assert(u64_3.unwrap() == u64::max()); + + assert(u64_4.is_none()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/Forc.toml new file mode 100644 index 00000000000..11ba8b72886 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "primitive_conversions_u8_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/src/main.sw new file mode 100644 index 00000000000..0fa64a82e6a --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/src/main.sw @@ -0,0 +1,123 @@ +library; + +use std::{primitive_conversions::u8::*, u128::U128}; + +#[test] +fn u8_try_from_u16() { + let u16_1: u16 = u16::min(); + let u16_2: u16 = 2u16; + let u16_3: u16 = u8::max().as_u16(); + let u16_4: u16 = u8::max().as_u16() + 1; + + let u8_1 = u8::try_from(u16_1); + let u8_2 = u8::try_from(u16_2); + let u8_3 = u8::try_from(u16_3); + let u8_4 = u8::try_from(u16_4); + + assert(u8_1.is_some()); + assert(u8_1.unwrap() == u8::min()); + + assert(u8_2.is_some()); + assert(u8_2.unwrap() == 2u8); + + assert(u8_3.is_some()); + assert(u8_3.unwrap() == u8::max()); + + assert(u8_4.is_none()); +} + +#[test] +fn u8_try_from_u32() { + let u32_1: u32 = u32::min(); + let u32_2: u32 = 2u32; + let u32_3: u32 = u8::max().as_u32(); + let u32_4: u32 = u8::max().as_u32() + 1; + + let u8_1 = u8::try_from(u32_1); + let u8_2 = u8::try_from(u32_2); + let u8_3 = u8::try_from(u32_3); + let u8_4 = u8::try_from(u32_4); + + assert(u8_1.is_some()); + assert(u8_1.unwrap() == u8::min()); + + assert(u8_2.is_some()); + assert(u8_2.unwrap() == 2u8); + + assert(u8_3.is_some()); + assert(u8_3.unwrap() == u8::max()); + + assert(u8_4.is_none()); +} + +#[test] +fn u8_try_from_u64() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = 2; + let u64_3: u64 = u8::max().as_u64(); + let u64_4: u64 = u8::max().as_u64() + 1; + + let u8_1 = u8::try_from(u64_1); + let u8_2 = u8::try_from(u64_2); + let u8_3 = u8::try_from(u64_3); + let u8_4 = u8::try_from(u64_4); + + assert(u8_1.is_some()); + assert(u8_1.unwrap() == u8::min()); + + assert(u8_2.is_some()); + assert(u8_2.unwrap() == 2u8); + + assert(u8_3.is_some()); + assert(u8_3.unwrap() == u8::max()); + + assert(u8_4.is_none()); +} + +#[test] +fn u8_try_from_u256() { + let u256_1: u256 = u256::min(); + let u256_2: u256 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; + let u256_3: u256 = u8::max().as_u256(); + let u256_4: u256 = u8::max().as_u256() + 1; + + let u8_1 = u8::try_from(u256_1); + let u8_2 = u8::try_from(u256_2); + let u8_3 = u8::try_from(u256_3); + let u8_4 = u8::try_from(u256_4); + + assert(u8_1.is_some()); + assert(u8_1.unwrap() == u8::min()); + + assert(u8_2.is_some()); + assert(u8_2.unwrap() == 2u8); + + assert(u8_3.is_some()); + assert(u8_3.unwrap() == u8::max()); + + assert(u8_4.is_none()); +} + +#[test] +fn u8_try_from_u128() { + let u128_1: U128 = U128::zero(); + let u128_2: U128 = U128::from((0, 2u8.as_u64())); + let u128_3: U128 = U128::from((0, u8::max().as_u64())); + let u128_4: U128 = U128::from((0, u8::max().as_u64() + 1)); + + let u8_1 = u8::try_from(u128_1); + let u8_2 = u8::try_from(u128_2); + let u8_3 = u8::try_from(u128_3); + let u8_4 = u8::try_from(u128_4); + + assert(u8_1.is_some()); + assert(u8_1.unwrap() == 0u8); + + assert(u8_2.is_some()); + assert(u8_2.unwrap() == 2u8); + + assert(u8_3.is_some()); + assert(u8_3.unwrap() == u8::max()); + + assert(u8_4.is_none()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/Forc.toml new file mode 100644 index 00000000000..be492595367 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "result_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/src/main.sw new file mode 100644 index 00000000000..2166d4b643b --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/src/main.sw @@ -0,0 +1,194 @@ +library; + +#[test] +fn result_is_ok() { + use std::bytes::Bytes; + + let u8_1: Result = Ok(1u8); + let u16_1: Result = Ok(1u16); + let u32_1: Result = Ok(1u32); + let u64_1: Result = Ok(1u64); + let u256_1: Result = Ok(0x01u256); + let b256_1: Result = Ok(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1: Result = Ok(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1: Result = Ok(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let array_1: Result<[u64; 2], str> = Ok([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1: Result = Ok(bytes_1); + let err_1: Result = Err(0u64); + + assert(u8_1.is_ok()); + assert(u16_1.is_ok()); + assert(u32_1.is_ok()); + assert(u64_1.is_ok()); + assert(u256_1.is_ok()); + assert(b256_1.is_ok()); + assert(struct_1.is_ok()); + assert(enum_1.is_ok()); + assert(array_1.is_ok()); + assert(heap_1.is_ok()); + assert(!err_1.is_ok()); +} + +#[test] +fn result_is_err() { + use std::bytes::Bytes; + + let u8_1: Result = Ok(1u8); + let u16_1: Result = Ok(1u16); + let u32_1: Result = Ok(1u32); + let u64_1: Result = Ok(1u64); + let u256_1: Result = Ok(0x01u256); + let b256_1: Result = Ok(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1: Result = Ok(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1: Result = Ok(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let array_1: Result<[u64; 2], str> = Ok([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1: Result = Ok(bytes_1); + let err_1: Result = Err(0u64); + + assert(!u8_1.is_err()); + assert(!u16_1.is_err()); + assert(!u32_1.is_err()); + assert(!u64_1.is_err()); + assert(!u256_1.is_err()); + assert(!b256_1.is_err()); + assert(!struct_1.is_err()); + assert(!enum_1.is_err()); + assert(!array_1.is_err()); + assert(!heap_1.is_err()); + assert(err_1.is_err()); +} + +#[test] +fn result_unwrap() { + use std::bytes::Bytes; + + let u8_1: Result = Ok(1u8); + let u16_1: Result = Ok(1u16); + let u32_1: Result = Ok(1u32); + let u64_1: Result = Ok(1u64); + let u256_1: Result = Ok(0x01u256); + let b256_1: Result = Ok(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1: Result = Ok(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1: Result = Ok(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let _array_1: Result<[u64; 2], str> = Ok([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1: Result = Ok(bytes_1); + + assert(u8_1.unwrap() == 1u8); + assert(u16_1.unwrap() == 1u16); + assert(u32_1.unwrap() == 1u32); + assert(u64_1.unwrap() == 1u64); + assert(u256_1.unwrap() == 0x01u256); + assert( + b256_1 + .unwrap() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + assert( + struct_1 + .unwrap() == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), + ); + assert( + enum_1 + .unwrap() == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), + ); + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // assert(array_1.unwrap() == [0u64, 0u64]); + assert(heap_1.unwrap() == bytes_1); +} + +#[test(should_revert)] +fn revert_result_when_unwrap_none() { + let err_1: Result = Err(0u64); + let _result = err_1.unwrap(); +} + +#[test] +fn result_unwrap_or() { + use std::bytes::Bytes; + + let u8_1: Result = Ok(1u8); + let u16_1: Result = Ok(1u16); + let u32_1: Result = Ok(1u32); + let u64_1: Result = Ok(1u64); + let u256_1: Result = Ok(0x01u256); + let b256_1: Result = Ok(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1: Result = Ok(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1: Result = Ok(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let _array_1: Result<[u64; 2], str> = Ok([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1: Result = Ok(bytes_1); + let err_1: Result = Err(0u64); + + assert(u8_1.unwrap_or(2u8) == 1u8); + assert(u16_1.unwrap_or(2u16) == 1u16); + assert(u32_1.unwrap_or(2u32) == 1u32); + assert(u64_1.unwrap_or(2u64) == 1u64); + assert(u256_1.unwrap_or(0x02u256) == 0x01u256); + assert( + b256_1 + .unwrap_or(0x0000000000000000000000000000000000000000000000000000000000000002) == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + assert( + struct_1 + .unwrap_or(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)) == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), + ); + assert( + enum_1 + .unwrap_or(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000002))) == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), + ); + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // assert(array_1.unwrap_or([1u64, 1u64]) == [0u64, 0u64]); + assert(heap_1.unwrap_or(Bytes::new()) == bytes_1); + assert(err_1.unwrap_or(10u64) == 10u64); +} + +#[test] +fn result_expect() { + use std::bytes::Bytes; + + let u8_1: Result = Ok(1u8); + let u16_1: Result = Ok(1u16); + let u32_1: Result = Ok(1u32); + let u64_1: Result = Ok(1u64); + let u256_1: Result = Ok(0x01u256); + let b256_1: Result = Ok(0x0000000000000000000000000000000000000000000000000000000000000001); + let struct_1: Result = Ok(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); + let enum_1: Result = Ok(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); + let _array_1: Result<[u64; 2], str> = Ok([0u64, 0u64]); + let mut bytes_1 = Bytes::new(); + bytes_1.push(1u8); + let heap_1: Result = Ok(bytes_1); + + assert(u8_1.expect("Failed Test") == 1u8); + assert(u16_1.expect("Failed Test") == 1u16); + assert(u32_1.expect("Failed Test") == 1u32); + assert(u64_1.expect("Failed Test") == 1u64); + assert(u256_1.expect("Failed Test") == 0x01u256); + assert( + b256_1 + .expect("Failed Test") == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + assert( + struct_1 + .expect("Failed Test") == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), + ); + assert( + enum_1 + .expect("Failed Test") == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), + ); + // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved + // assert(array_1.expect("Failed Test") == [0u64, 0u64]); + assert(heap_1.expect("Failed Test") == bytes_1); +} + +#[test(should_revert)] +fn revert_result_expect_when_none() { + let err_1: Result = Err(0u64); + let _result = err_1.expect("Failed Test"); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/Forc.toml new file mode 100644 index 00000000000..4d01e7378cb --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "revert_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/src/main.sw new file mode 100644 index 00000000000..cff1e49fc0e --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/src/main.sw @@ -0,0 +1,16 @@ +library; + +#[test(should_revert)] +fn revert_revert() { + revert(0); +} + +#[test(should_revert)] +fn revert_revert_require() { + require(false, "error"); +} + +#[test] +fn pass_revert_require() { + require(true, "error"); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/Forc.toml new file mode 100644 index 00000000000..fc32fcd089e --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "string_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/src/main.sw new file mode 100644 index 00000000000..079a28ee78d --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/src/main.sw @@ -0,0 +1,382 @@ +library; + +use std::{bytes::Bytes, string::String}; + +#[test] +fn string_as_bytes() { + let mut string = String::new(); + + let bytes = string.as_bytes(); + assert(bytes.len() == 0); + assert(bytes.capacity() == string.capacity()); + + let mut bytes = Bytes::new(); + bytes.push(0u8); + let string = String::from_ascii(bytes); + + let bytes = string.as_bytes(); + assert(bytes.len() == 1); + assert(bytes.capacity() == string.capacity()); +} + +#[test] +fn string_capacity() { + let mut string = String::new(); + + assert(string.capacity() == 0); + + let mut bytes = Bytes::new(); + bytes.push(0u8); + let string = String::from_ascii(bytes); + assert(string.capacity() == 1); +} + +#[test] +fn string_clear() { + // Clear non-empty + let mut bytes = Bytes::new(); + bytes.push(0u8); + let mut string = String::from_ascii(bytes); + assert(!string.is_empty()); + + string.clear(); + assert(string.is_empty()); +} + +#[test] +fn string_clear_empty() { + let mut string = String::new(); + + assert(string.is_empty()); + string.clear(); + assert(string.is_empty()); +} + +#[test] +fn string_from_ascii() { + let mut bytes = Bytes::new(); + bytes.push(0u8); + bytes.push(1u8); + bytes.push(2u8); + bytes.push(3u8); + bytes.push(4u8); + + let mut string_from_ascii = String::from_ascii(bytes); + assert(bytes.len() == string_from_ascii.capacity()); + + let bytes = string_from_ascii.as_bytes(); + assert(bytes.get(0).unwrap() == 0u8); + assert(bytes.get(1).unwrap() == 1u8); + assert(bytes.get(2).unwrap() == 2u8); + assert(bytes.get(3).unwrap() == 3u8); + assert(bytes.get(4).unwrap() == 4u8); + assert(bytes.get(5) == None); +} + +#[test] +fn string_from_ascii_str() { + let mut string_from_ascii = String::from_ascii_str("ABCDEF"); + assert(string_from_ascii.capacity() == 6); + let bytes = string_from_ascii.as_bytes(); + assert(bytes.get(0).unwrap() == 65u8); + assert(bytes.get(1).unwrap() == 66u8); + assert(bytes.get(2).unwrap() == 67u8); + assert(bytes.get(3).unwrap() == 68u8); + assert(bytes.get(4).unwrap() == 69u8); + assert(bytes.get(5).unwrap() == 70u8); + assert(bytes.get(6).is_none()); +} + +#[test] +fn string_is_empty() { + let mut string = String::new(); + + assert(string.is_empty()); + + let mut bytes = Bytes::new(); + bytes.push(0u8); + let string = String::from_ascii(bytes); + assert(!string.is_empty()); + + let mut bytes = Bytes::new(); + bytes.push(0u8); + bytes.push(1u8); + let mut string = String::from_ascii(bytes); + assert(!string.is_empty()); + + string.clear(); + assert(string.is_empty()); +} + +#[test] +fn string_new() { + let mut string = String::new(); + + assert(string.is_empty()); + assert(string.capacity() == 0); +} + +#[test] +fn string_with_capacity() { + let mut iterator = 0; + + while iterator < 16 { + let mut string = String::with_capacity(iterator); + assert(string.capacity() == iterator); + iterator += 1; + } + + let mut string = String::with_capacity(0); + assert(string.capacity() == 0); + + string.clear(); + assert(string.capacity() == 0); + let mut string = String::with_capacity(4); + + assert(string.capacity() == 4); +} + +#[test] +fn string_ptr() { + let string = String::new(); + assert(!string.ptr().is_null()); + + let mut bytes = Bytes::new(); + bytes.push(0u8); + bytes.push(1u8); + bytes.push(2u8); + bytes.push(3u8); + bytes.push(4u8); + + let mut string_from_ascii = String::from_ascii(bytes); + assert(!string_from_ascii.ptr().is_null()); + assert(string_from_ascii.ptr() != bytes.ptr()); +} + +#[test] +fn string_from_bytes() { + let mut bytes = Bytes::new(); + + bytes.push(0u8); + bytes.push(1u8); + bytes.push(2u8); + bytes.push(3u8); + bytes.push(4u8); + + let mut string_from_bytes = String::from(bytes); + let bytes = string_from_bytes.as_bytes(); + assert(bytes.len() == 5); + assert(bytes.capacity() == string_from_bytes.capacity()); + assert(bytes.get(0).unwrap() == 0u8); + assert(bytes.get(1).unwrap() == 1u8); + assert(bytes.get(2).unwrap() == 2u8); +} + +#[test] +fn string_into_bytes() { + let mut string = String::new(); + + let bytes: Bytes = string.into(); + assert(bytes.len() == 0); + assert(bytes.capacity() == string.capacity()); + + let mut bytes = Bytes::new(); + bytes.push(0u8); + let string = String::from_ascii(bytes); + let bytes: Bytes = string.into(); + assert(bytes.len() == 1); + assert(bytes.capacity() == string.capacity()); + assert(bytes.get(0).unwrap() == 0u8); + + let mut bytes = Bytes::new(); + bytes.push(0u8); + bytes.push(1u8); + let string = String::from_ascii(bytes); + let mut bytes: Bytes = string.into(); + assert(bytes.len() == 2); + assert(bytes.capacity() == string.capacity()); + assert(bytes.get(1).unwrap() == 1u8); +} + +#[test] +fn string_bytes_from() { + let mut bytes = Bytes::new(); + + bytes.push(0u8); + bytes.push(1u8); + bytes.push(2u8); + bytes.push(3u8); + bytes.push(4u8); + + let mut string_from_bytes = String::from(bytes); + + let bytes = Bytes::from(string_from_bytes); + assert(bytes.len() == 5); + assert(bytes.capacity() == string_from_bytes.capacity()); + assert(bytes.get(0).unwrap() == 0u8); + assert(bytes.get(1).unwrap() == 1u8); + assert(bytes.get(2).unwrap() == 2u8); +} + +#[test] +fn string_bytes_into() { + let mut bytes = Bytes::new(); + + bytes.push(0u8); + bytes.push(1u8); + bytes.push(2u8); + bytes.push(3u8); + bytes.push(4u8); + + let mut string_from_bytes: String = bytes.into(); + + let bytes: Bytes = string_from_bytes.as_bytes(); + assert(bytes.len() == 5); + assert(bytes.capacity() == string_from_bytes.capacity()); + assert(bytes.get(0).unwrap() == 0u8); + assert(bytes.get(1).unwrap() == 1u8); + assert(bytes.get(2).unwrap() == 2u8); +} + +#[test] +fn string_as_raw_slice() { + let mut bytes = Bytes::new(); + + bytes.push(0u8); + bytes.push(1u8); + bytes.push(2u8); + bytes.push(3u8); + bytes.push(4u8); + + let raw_slice = bytes.as_raw_slice(); + let mut string = String::from(bytes); + + let string_slice = string.as_raw_slice(); + assert(string_slice.number_of_bytes() == raw_slice.number_of_bytes()); +} + +#[test] +fn string_from_raw_slice() { + let mut bytes = Bytes::new(); + + bytes.push(0u8); + bytes.push(1u8); + bytes.push(2u8); + bytes.push(3u8); + bytes.push(4u8); + + let raw_slice = bytes.as_raw_slice(); + let mut string_from_slice = String::from(raw_slice); + let bytes = string_from_slice.as_bytes(); + assert(bytes.len() == 5); + assert(bytes.get(0).unwrap() == 0u8); + assert(bytes.get(1).unwrap() == 1u8); + assert(bytes.get(2).unwrap() == 2u8); +} + +#[test] +fn string_into_raw_slice() { + // Glob operator needed for From for raw_slice + use std::string::*; + + let mut string = String::new(); + + let raw_slice: raw_slice = string.into(); + assert(raw_slice.number_of_bytes() == 0); + + let mut bytes = Bytes::new(); + bytes.push(0u8); + let string = String::from_ascii(bytes); + let raw_slice = string.as_raw_slice(); + assert(raw_slice.number_of_bytes() == 1); + assert(raw_slice.ptr().read_byte() == 0u8); + + let mut bytes = Bytes::new(); + bytes.push(0u8); + bytes.push(1u8); + let string = String::from_ascii(bytes); + let mut raw_slice = string.as_raw_slice(); + assert(raw_slice.number_of_bytes() == 2); + assert(raw_slice.ptr().add_uint_offset(1).read_byte() == 1u8); + + let mut raw_slice = string.as_raw_slice(); + assert(raw_slice.number_of_bytes() == 2); + assert(raw_slice.ptr().read_byte() == 0u8); +} + +#[test] +fn string_raw_slice_into() { + // Glob operator needed for From for raw_slice + use std::string::*; + + let mut bytes = Bytes::new(); + + bytes.push(0u8); + bytes.push(1u8); + bytes.push(2u8); + bytes.push(3u8); + bytes.push(4u8); + + let raw_slice = bytes.as_raw_slice(); + let mut string = String::from(bytes); + + let string_slice: raw_slice = string.into(); + assert(string_slice.number_of_bytes() == raw_slice.number_of_bytes()); +} + +#[test] +fn string_raw_slice_from() { + // Glob operator needed for From for raw_slice + use std::string::*; + + let mut bytes = Bytes::new(); + + bytes.push(0u8); + bytes.push(1u8); + bytes.push(2u8); + bytes.push(3u8); + bytes.push(4u8); + + let raw_slice = bytes.as_raw_slice(); + let mut string: String = String::from(bytes); + + let string_slice = raw_slice::from(string); + assert(string_slice.number_of_bytes() == raw_slice.number_of_bytes()); +} + +#[test] +fn string_test_equal() { + let string1 = String::from_ascii_str("fuel"); + let string2 = String::from_ascii_str("fuel"); + let string3 = String::from_ascii_str("blazingly fast"); + + assert(string1 == string2); + assert(string1 != string3); +} + +#[test] +fn string_test_hash() { + use std::hash::{Hash, sha256}; + + let mut bytes = Bytes::new(); + bytes.push(0u8); + + let string = String::from(bytes); + + assert(sha256(string) == sha256(bytes)); +} + +#[test] +fn string_test_abi_encoding() { + let string = String::from_ascii_str("fuel"); + + let buffer = Buffer::new(); + let encoded_string = string.abi_encode(buffer); + + let encoded_raw_slice = encoded_string.as_raw_slice(); + let mut buffer_reader = BufferReader::from_parts(encoded_raw_slice.ptr(), encoded_raw_slice.number_of_bytes()); + + let decoded_string = String::abi_decode(buffer_reader); + + assert(string == decoded_string); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/Forc.toml new file mode 100644 index 00000000000..c218c1f64cd --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "u128_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/src/main.sw new file mode 100644 index 00000000000..c7fc8405215 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/src/main.sw @@ -0,0 +1,744 @@ +library; + +use std::u128::U128; + +#[test] +fn u128_from_u8() { + let u8_1: u8 = u8::min(); + let u8_2: u8 = u8::max(); + let u8_3: u8 = 1u8; + + let u128_1 = >::from(u8_1); + let u128_2 = >::from(u8_2); + let u128_3 = >::from(u8_3); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 255u64); + assert(u128_3.as_u64().unwrap() == 1u64); +} + +#[test] +fn u128_u8_into() { + let u8_1: u8 = u8::min(); + let u8_2: u8 = u8::max(); + let u8_3: u8 = 1u8; + + let u128_1: U128 = u8_1.into(); + let u128_2: U128 = u8_2.into(); + let u128_3: U128 = u8_3.into(); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 255u64); + assert(u128_3.as_u64().unwrap() == 1u64); +} + +#[test] +fn u128_from_u16() { + let u16_1: u16 = u16::min(); + let u16_2: u16 = u16::max(); + let u16_3: u16 = 1u16; + + let u128_1 = >::from(u16_1); + let u128_2 = >::from(u16_2); + let u128_3 = >::from(u16_3); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 65535u64); + assert(u128_3.as_u64().unwrap() == 1u64); +} + +#[test] +fn u128_u16_into() { + let u16_1: u16 = u16::min(); + let u16_2: u16 = u16::max(); + let u16_3: u16 = 1u16; + + let u128_1: U128 = u16_1.into(); + let u128_2: U128 = u16_2.into(); + let u128_3: U128 = u16_3.into(); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 65535u64); + assert(u128_3.as_u64().unwrap() == 1u64); +} + +#[test] +fn u128_from_u32() { + let u32_1: u32 = u32::min(); + let u32_2: u32 = u32::max(); + let u32_3: u32 = 1u32; + + let u128_1 = >::from(u32_1); + let u128_2 = >::from(u32_2); + let u128_3 = >::from(u32_3); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 4294967295u64); + assert(u128_3.as_u64().unwrap() == 1u64); +} + +#[test] +fn u128_u32_into() { + let u32_1: u32 = u32::min(); + let u32_2: u32 = u32::max(); + let u32_3: u32 = 1u32; + + let u128_1: U128 = u32_1.into(); + let u128_2: U128 = u32_2.into(); + let u128_3: U128 = u32_3.into(); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 4294967295u64); + assert(u128_3.as_u64().unwrap() == 1u64); +} + +#[test] +fn u128_from_u64() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = u64::max(); + let u64_3: u64 = 1u64; + + let u128_1 = >::from(u64_1); + let u128_2 = >::from(u64_2); + let u128_3 = >::from(u64_3); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 18446744073709551615u64); + assert(u128_3.as_u64().unwrap() == 1u64); +} + +#[test] +fn u128_u64_into() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = u64::max(); + let u64_3: u64 = 1u64; + + let u128_1: U128 = u64_1.into(); + let u128_2: U128 = u64_2.into(); + let u128_3: U128 = u64_3.into(); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 18446744073709551615u64); + assert(u128_3.as_u64().unwrap() == 1u64); +} + +#[test] +fn u128_from_u64_tuple() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = u64::max(); + let u64_3: u64 = 1u64; + + let u128_1 = >::from((u64_1, u64_1)); + let u128_2 = >::from((u64_2, u64_2)); + let u128_3 = >::from((u64_3, u64_3)); + + assert(u128_1.upper() == 0u64); + assert(u128_1.lower() == 0u64); + assert(u128_2.upper() == 18446744073709551615u64); + assert(u128_2.lower() == 18446744073709551615u64); + assert(u128_3.upper() == 1u64); + assert(u128_3.lower() == 1u64); +} + +#[test] +fn u128_into_u64_tuple() { + // Glob operator needed for U128.into() + use std::u128::*; + + let u128_1 = U128::from((0u64, 0u64)); + let u128_2 = U128::from((18446744073709551615u64, 18446744073709551615u64)); + let u128_3 = U128::from((1u64, 1u64)); + + let u64_1: (u64, u64) = u128_1.into(); + let u64_2: (u64, u64) = u128_2.into(); + let u64_3: (u64, u64) = u128_3.into(); + + assert(u64_1.0 == u64::min()); + assert(u64_1.1 == u64::min()); + assert(u64_2.0 == u64::max()); + assert(u64_2.1 == u64::max()); + assert(u64_3.0 == 1u64); + assert(u64_3.1 == 1u64); +} + +#[test] +fn u128_u64_tuple_into() { + // Glob operator needed for From for (u64, u64) + use std::u128::*; + + let u64_1: (u64, u64) = (u64::min(), u64::min()); + let u64_2: (u64, u64) = (u64::max(), u64::max()); + let u64_3: (u64, u64) = (1u64, 1u64); + + let u128_1: U128 = u64_1.into(); + let u128_2: U128 = u64_2.into(); + let u128_3: U128 = u64_3.into(); + + assert(u128_1.upper() == 0u64); + assert(u128_1.lower() == 0u64); + assert(u128_2.upper() == 18446744073709551615u64); + assert(u128_2.lower() == 18446744073709551615u64); + assert(u128_3.upper() == 1u64); + assert(u128_3.lower() == 1u64); +} + +#[test] +fn u128_u64_tuple_from() { + // Glob operator needed for From for (u64, u64) + use std::u128::*; + + let u128_1 = U128::from((0u64, 0u64)); + let u128_2 = U128::from((18446744073709551615u64, 18446744073709551615u64)); + let u128_3 = U128::from((1u64, 1u64)); + + let u64_1: (u64, u64) = <(u64, u64) as From>::from(u128_1); + let u64_2: (u64, u64) = <(u64, u64) as From>::from(u128_2); + let u64_3: (u64, u64) = <(u64, u64) as From>::from(u128_3); + + assert(u64_1.0 == u64::min()); + assert(u64_1.1 == u64::min()); + assert(u64_2.0 == u64::max()); + assert(u64_2.1 == u64::max()); + assert(u64_3.0 == 1u64); + assert(u64_3.1 == 1u64); +} + +#[test] +fn u128_eq() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = u64::max(); + let u64_3: u64 = 1u64; + + let u128_1 = >::from((u64_1, u64_1)); + let u128_2 = >::from((u64_1, u64_1)); + let u128_3 = >::from((u64_2, u64_2)); + let u128_4 = >::from((u64_2, u64_2)); + let u128_5 = >::from((u64_3, u64_3)); + let u128_6 = >::from((u64_3, u64_3)); + + assert(u128_1 == u128_2); + assert(u128_3 == u128_4); + assert(u128_5 == u128_6); +} + +#[test] +fn u128_ne() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = u64::max(); + let u64_3: u64 = 1u64; + + let u128_1 = >::from((u64_1, u64_1)); + let u128_2 = >::from((u64_1, u64_2)); + let u128_3 = >::from((u64_2, u64_2)); + let u128_4 = >::from((u64_2, u64_3)); + let u128_5 = >::from((u64_3, u64_3)); + let u128_6 = >::from((u64_3, u64_1)); + + assert(u128_1 != u128_2); + assert(u128_1 != u128_3); + assert(u128_1 != u128_4); + assert(u128_1 != u128_5); + assert(u128_1 != u128_6); + + assert(u128_2 != u128_3); + assert(u128_2 != u128_4); + assert(u128_2 != u128_5); + assert(u128_2 != u128_6); + + assert(u128_3 != u128_4); + assert(u128_3 != u128_5); + assert(u128_3 != u128_6); + + assert(u128_4 != u128_5); + assert(u128_4 != u128_6); + + assert(u128_5 != u128_6); +} + +#[test] +fn u128_ord() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = u64::max(); + let u64_3: u64 = 1u64; + + let u128_1 = >::from((u64_1, u64_1)); // 0, 0 + let u128_2 = >::from((u64_1, u64_3)); // 0, 1 + let u128_3 = >::from((u64_3, u64_1)); // 1, 0 + let u128_4 = >::from((u64_2, u64_2)); // max, max + let u128_5 = >::from((u64_3, u64_2)); // 1, max + let u128_6 = >::from((u64_2, u64_1)); // max, 0 + let u128_7 = >::from((u64_3, u64_3)); // 1, 1 + let u128_8 = >::from((u64_2, u64_3)); // max, 1 + let u128_9 = >::from((u64_1, u64_2)); // 0, max + assert(u128_1 < u128_2); + assert(u128_3 > u128_1); + assert(u128_3 > u128_2); + + assert(u128_4 > u128_1); + assert(u128_4 > u128_2); + assert(u128_4 > u128_3); + assert(u128_4 > u128_5); + assert(u128_4 > u128_6); + assert(u128_4 > u128_7); + assert(u128_4 > u128_8); + assert(u128_4 > u128_9); + + assert(u128_5 > u128_1); + assert(u128_5 > u128_2); + assert(u128_5 > u128_3); + assert(u128_5 < u128_6); + assert(u128_5 > u128_7); + assert(u128_5 < u128_8); + assert(u128_5 > u128_9); + + assert(u128_6 > u128_1); + assert(u128_6 > u128_2); + assert(u128_6 > u128_3); + assert(u128_6 > u128_7); + assert(u128_6 < u128_8); + assert(u128_6 > u128_9); + + assert(u128_7 > u128_1); + assert(u128_7 > u128_2); + assert(u128_7 > u128_3); +} + +#[test] +fn u128_new() { + let new_u128 = U128::new(); + assert(new_u128.upper() == 0); + assert(new_u128.lower() == 0); +} + +#[test] +fn u128_as_u64() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = u64::max(); + let u64_3: u64 = 1u64; + + let u128_1 = >::from(u64_1); + let u128_2 = >::from(u64_2); + let u128_3 = >::from(u64_3); + + assert(u128_1.as_u64().unwrap() == 0u64); + assert(u128_2.as_u64().unwrap() == 18446744073709551615u64); + assert(u128_3.as_u64().unwrap() == 1u64); + + let u128_4 = >::from((u64_3, u64_1)); + let u128_5 = >::from((u64_2, u64_1)); + let u128_6 = >::from((u64_2, u64_2)); + + assert(u128_4.as_u64().is_err()); + assert(u128_5.as_u64().is_err()); + assert(u128_6.as_u64().is_err()); +} + +#[test] +fn u128_min() { + let new_u128 = U128::min(); + assert(new_u128.upper() == 0); + assert(new_u128.lower() == 0); +} + +#[test] +fn u128_max() { + let new_u128 = U128::max(); + assert(new_u128.upper() == u64::max()); + assert(new_u128.lower() == u64::max()); +} + +#[test] +fn u128_bits() { + assert(U128::bits() == 128u32); +} + +#[test] +fn u128_upper() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = u64::max(); + let u64_3: u64 = 1u64; + + let u128_1 = >::from((u64_1, u64_1)); + let u128_2 = >::from((u64_2, u64_2)); + let u128_3 = >::from((u64_3, u64_3)); + + assert(u128_1.upper() == 0u64); + assert(u128_2.upper() == 18446744073709551615u64); + assert(u128_3.upper() == 1u64); +} + +#[test] +fn u128_lower() { + let u64_1: u64 = u64::min(); + let u64_2: u64 = u64::max(); + let u64_3: u64 = 1u64; + + let u128_1 = >::from((u64_1, u64_1)); + let u128_2 = >::from((u64_2, u64_2)); + let u128_3 = >::from((u64_3, u64_3)); + + assert(u128_1.lower() == 0u64); + assert(u128_2.lower() == 18446744073709551615u64); + assert(u128_3.lower() == 1u64); +} + +#[test] +fn u128_zero() { + let zero_u128 = U128::zero(); + assert(zero_u128.as_u64().unwrap() == 0u64); +} + +#[test] +fn u128_is_zero() { + let zero_u128 = U128::zero(); + assert(zero_u128.is_zero()); + + let other1_u128 = U128::from((0, 1)); + assert(!other1_u128.is_zero()); + + let other2_u128 = U128::from((1, 0)); + assert(!other2_u128.is_zero()); + + let other3_u128 = U128::from((1, 1)); + assert(!other3_u128.is_zero()); + + let other4_u128 = U128::from((0, 0)); + assert(other4_u128.is_zero()); +} + +#[test] +fn u128_bitwise_and() { + let one = U128::from((0, 1)); + let two = U128::from((0, 2)); + let three = U128::from((0, 3)); + let thirteen = U128::from((0, 13)); + let one_upper = U128::from((1, 0)); + let zero = U128::zero(); + + assert(one & two == zero); + assert(one & three == one); + assert(one & thirteen == one); + assert(one & one_upper == zero); + + assert(two & three == two); + assert(two & thirteen == zero); + assert(two & one_upper == zero); + + assert(three & thirteen == one); + assert(three & one_upper == zero); + + assert(thirteen & one_upper == zero); + + assert(one & one == one); + assert(two & two == two); + assert(three & three == three); + assert(thirteen & thirteen == thirteen); + assert(one_upper & one_upper == one_upper); +} + +#[test] +fn u128_bitwise_or() { + let one = U128::from((0, 1)); + let two = U128::from((0, 2)); + let three = U128::from((0, 3)); + let thirteen = U128::from((0, 13)); + let one_upper = U128::from((1, 0)); + + assert(one | two == three); + assert(one | three == three); + assert(one | thirteen == thirteen); + assert(one | one_upper == one_upper + one); + + assert(two | three == three); + assert(two | thirteen == thirteen + two); + assert(two | one_upper == one_upper + two); + + assert(three | thirteen == thirteen + two); + assert(three | one_upper == one_upper + three); + + assert(thirteen | one_upper == one_upper + thirteen); + + assert(one | one == one); + assert(two | two == two); + assert(three | three == three); + assert(thirteen | thirteen == thirteen); + assert(one_upper | one_upper == one_upper); +} + +#[test] +fn u128_shift() { + let one = U128::from((0, 1)); + let one_upper = U128::from((1, 0)); + + let right_shift_one_upper = one_upper >> 1; + assert(right_shift_one_upper.upper() == 0); + assert(right_shift_one_upper.lower() == (1 << 63)); + + let left_shift_one_upper_right_shift = right_shift_one_upper << 1; + assert(left_shift_one_upper_right_shift == one_upper); + + let one_left_shift_64 = one << 64; + assert(one_left_shift_64.upper() == 1); + assert(one_left_shift_64.lower() == 0); + + let three_left_shift_one = U128::from((0, 3)) << 1; + assert(three_left_shift_one.upper() == 0); + assert(three_left_shift_one.lower() == 6); +} + +#[test] +fn u128_not() { + let not_0_3 = !U128::from((0, 3)); + assert(not_0_3.upper() == u64::max()); + assert(not_0_3.lower() == u64::max() - 3); + + let not_3_3 = !U128::from((3, 3)); + assert(not_3_3.upper() == u64::max() - 3); + assert(not_3_3.lower() == u64::max() - 3); + + let not_3_0 = !U128::from((3, 0)); + assert(not_3_0.upper() == u64::max() - 3); + assert(not_3_0.lower() == u64::max()); +} + +#[test] +fn u128_add() { + let first = U128::from((0, 0)); + let second = U128::from((0, 1)); + let max_u64 = U128::from((0, u64::max())); + + let one = first + second; + assert(one.upper() == 0); + assert(one.lower() == 1); + + let two = one + one; + assert(two.upper() == 0); + assert(two.lower() == 2); + + let add_of_one = max_u64 + one; + assert(add_of_one.upper() == 1); + assert(add_of_one.lower() == 0); + + let add_of_two = max_u64 + two; + assert(add_of_two.upper() == 1); + assert(add_of_two.lower() == 1); + + let add_max = max_u64 + max_u64; + assert(add_max.upper() == 1); + assert(add_max.lower() == u64::max() - 1); +} + +#[test(should_revert)] +fn revert_u128_add() { + let one = U128::from((0, 1)); + let max_u64 = U128::from((u64::max(), u64::max())); + + let _result = one + max_u64; +} + +#[test] +fn u128_sub() { + let first = U128::from((0, 0)); + let second = U128::from((0, 1)); + let two = U128::from((0, 2)); + let max_u64 = U128::from((0, u64::max())); + + let sub_one = second - first; + assert(sub_one.upper() == 0); + assert(sub_one.lower() == 1); + + let sub_zero = first - first; + assert(sub_zero.upper() == 0); + assert(sub_zero.lower() == 0); + + let add_of_two = max_u64 + two; + let sub_max_again = add_of_two - two; + assert(sub_max_again.upper() == 0); + assert(sub_max_again.lower() == u64::max()); +} + +#[test(should_revert)] +fn revert_u128_sub() { + let first = U128::from((0, 0)); + let second = U128::from((0, 1)); + + let _result = first - second; +} + +#[test] +fn u128_multiply() { + let two = U128::from((0, 2)); + let max_u64 = U128::from((0, u64::max())); + + let mul_128_of_two = max_u64 * two; + assert(mul_128_of_two.upper() == 1); + assert(mul_128_of_two.lower() == u64::max() - 1); + + let mul_128_of_four = mul_128_of_two * two; + assert(mul_128_of_four.upper() == 3); + assert(mul_128_of_four.lower() == u64::max() - 3); + + let mul_128_max = max_u64 * max_u64; + assert(mul_128_max.upper() == u64::max() - 1); + assert(mul_128_max.lower() == 1); +} + +#[test(should_revert)] +fn revert_u128_multiply() { + let first = U128::from((0, 2)); + let second = U128::from((u64::max(), 1)); + + let _result = first * second; +} + +#[test] +fn u128_divide() { + let two = U128::from((0, 2)); + let max_u64 = U128::from((0, u64::max())); + + let div_max_two = max_u64 / two; + assert(div_max_two.upper() == 0); + assert(div_max_two.lower() == u64::max() >> 1); + + // Product of u64::MAX and u64::MAX. + let dividend = U128::from((u64::max() - 1, 1)); + let div_max_max = dividend / max_u64; + assert(div_max_max.upper() == 0); + assert(div_max_max.lower() == u64::max()); +} + +#[test(should_revert)] +fn revert_u128_divide_by_zero() { + let first = U128::from((0, 1)); + let second = U128::from((0, 0)); + + let _result = first / second; +} + +#[test] +fn u128_pow() { + let mut u_128 = U128::from((0, 7)); + let mut pow_of_u_128 = u_128.pow(2u32); + assert(pow_of_u_128 == U128::from((0, 49))); + + pow_of_u_128 = u_128.pow(3u32); + assert(pow_of_u_128 == U128::from((0, 343))); + + u_128 = U128::from((0, 3)); + pow_of_u_128 = u_128.pow(2u32); + assert(pow_of_u_128 == U128::from((0, 9))); + + u_128 = U128::from((0, 5)); + pow_of_u_128 = u_128.pow(2u32); + assert(pow_of_u_128 == U128::from((0, 25))); + + pow_of_u_128 = u_128.pow(7u32); + assert(pow_of_u_128 == U128::from((0, 78125))); + + u_128 = U128::from((0, 8)); + pow_of_u_128 = u_128.pow(2u32); + assert(pow_of_u_128 == U128::from((0, 64))); + + pow_of_u_128 = u_128.pow(9u32); + assert(pow_of_u_128 == U128::from((0, 134217728))); + + u_128 = U128::from((0, 10)); + pow_of_u_128 = u_128.pow(2u32); + assert(pow_of_u_128 == U128::from((0, 100))); + + pow_of_u_128 = u_128.pow(5u32); + assert(pow_of_u_128 == U128::from((0, 100000))); + + u_128 = U128::from((0, 12)); + pow_of_u_128 = u_128.pow(2u32); + assert(pow_of_u_128 == U128::from((0, 144))); + + pow_of_u_128 = u_128.pow(3u32); + assert(pow_of_u_128 == U128::from((0, 1728))); + + // Test reassignment + u_128 = U128::from((0, 13)); + u_128 = u_128.pow(1u32); + assert(u_128 == U128::from((0, 13))); +} + +#[test] +fn u128_root() { + let mut u_128: U128 = U128::from((0, 49)); + let mut root_of_u_128 = u_128.sqrt(); + + assert(root_of_u_128 == U128::from((0, 7))); + + u_128 = U128::from((0, 25)); + root_of_u_128 = u_128.sqrt(); + assert(root_of_u_128 == U128::from((0, 5))); + + u_128 = U128::from((0, 81)); + root_of_u_128 = u_128.sqrt(); + assert(root_of_u_128 == U128::from((0, 9))); + + u_128 = U128::from((0, 144)); + root_of_u_128 = u_128.sqrt(); + assert(root_of_u_128 == U128::from((0, 12))); + + u_128 = U128::from((0, 1)); + root_of_u_128 = u_128.sqrt(); + assert(root_of_u_128 == U128::from((0, 1))); +} + +#[test] +fn u128_log() { + let u_128_0: U128 = U128::from((0, 0)); + let u_128_1: U128 = U128::from((0, 1)); + let u_128_2: U128 = U128::from((0, 2)); + let u_128_3: U128 = U128::from((0, 3)); + let u_128_6: U128 = U128::from((0, 6)); + let u_128_8: U128 = U128::from((0, 8)); + let u_128_9: U128 = U128::from((0, 9)); + let u_128_10: U128 = U128::from((0, 10)); + let u_128_21: U128 = U128::from((0, 21)); + let u_128_42: U128 = U128::from((0, 42)); + let u_128_64: U128 = U128::from((0, 64)); + let u_128_100: U128 = U128::from((0, 100)); + let u_128_127: U128 = U128::from((0, 127)); + let u_128_max_div_2: U128 = U128::from((1, 0)); + let u_128_max: U128 = U128::max(); + + assert(u_128_2.log(u_128_2) == u_128_1); + assert(u_128_1.log(u_128_3) == u_128_0); + assert(u_128_8.log(u_128_2) == u_128_3); + assert(u_128_100.log(u_128_10) == u_128_2); + assert(u_128_100.log(u_128_2) == u_128_6); + assert(u_128_100.log(u_128_9) == u_128_2); + assert(u_128_max.log(u_128_2) == u_128_127); + assert(u_128_max.log(u_128_9) == u_128_42); + assert(u_128_max_div_2.log(u_128_2) == u_128_64); + assert(u_128_max_div_2.log(u_128_9) == u_128_21); +} + +#[test] +fn u128_binary_log() { + let u_128_1: U128 = U128::from((0, 1)); + let u_128_2: U128 = U128::from((0, 2)); + let u_128_3: U128 = U128::from((0, 3)); + let u_128_6: U128 = U128::from((0, 6)); + let u_128_8: U128 = U128::from((0, 8)); + let u_128_64: U128 = U128::from((0, 64)); + let u_128_127: U128 = U128::from((0, 127)); + let u_128_100: U128 = U128::from((0, 100)); + let u_128_max_div_2: U128 = U128::from((1, 0)); + let u_128_max: U128 = U128::max(); + + assert(u_128_2.log2() == u_128_1); + assert(u_128_8.log2() == u_128_3); + assert(u_128_100.log2() == u_128_6); + assert(u_128_max.log2() == u_128_127); + assert(u_128_max_div_2.log2() == u_128_64); +} + +#[test(should_revert)] +fn revert_u128_binary_log() { + let u_128_0: U128 = U128::from((0, 0)); + + let _result = u_128_0.log2(); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/Forc.toml new file mode 100644 index 00000000000..3481c693acf --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "vec_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/src/main.sw new file mode 100644 index 00000000000..78e2df5d1e5 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/src/main.sw @@ -0,0 +1,704 @@ +library; +fn setup() -> (Vec, u64, u64, u64) { + let mut vec: Vec = Vec::new(); + let a = 5u64; + let b = 7u64; + let c = 9u64; + vec.push(a); + vec.push(b); + vec.push(c); + (vec, a, b, c) +} + +#[test] +fn vec_new() { + let new_vec: Vec = Vec::new(); + assert(new_vec.len() == 0); + assert(new_vec.capacity() == 0); +} + +#[test] +fn vec_with_capacity() { + let vec_1: Vec = Vec::with_capacity(0); + assert(vec_1.capacity() == 0); + + let vec_2: Vec = Vec::with_capacity(1); + assert(vec_2.capacity() == 1); + + // 2^6 + let vec_3: Vec = Vec::with_capacity(64); + assert(vec_3.capacity() == 64); + + // 2^11 + let vec_4: Vec = Vec::with_capacity(2048); + assert(vec_4.capacity() == 2048); + + // 2^16 + let vec_5: Vec = Vec::with_capacity(65536); + assert(vec_5.capacity() == 65536); +} + +#[test()] +fn vec_push() { + let mut vec: Vec = Vec::new(); + + assert(vec.len() == 0); + assert(vec.capacity() == 0); + + vec.push(1u64); + assert(vec.len() == 1); + assert(vec.capacity() == 1); + + vec.push(2u64); + assert(vec.len() == 2); + assert(vec.capacity() == 2); + + // Capacity doubles + vec.push(3u64); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + + vec.push(4u64); + assert(vec.len() == 4); + assert(vec.capacity() == 4); + + // Capacity doubles + vec.push(5u64); + assert(vec.len() == 5); + assert(vec.capacity() == 8); + + vec.push(6u64); + assert(vec.len() == 6); + assert(vec.capacity() == 8); + + vec.push(7u64); + assert(vec.len() == 7); + assert(vec.capacity() == 8); + + vec.push(8u64); + assert(vec.len() == 8); + assert(vec.capacity() == 8); + + // Capacity doubles + vec.push(9u64); + assert(vec.len() == 9); + assert(vec.capacity() == 16); +} + +#[test()] +fn vec_capacity() { + let mut vec: Vec = Vec::new(); + assert(vec.capacity() == 0); + + vec.push(5u64); + assert(vec.capacity() == 1); + vec.push(7u64); + assert(vec.capacity() == 2); + vec.push(9u64); + assert(vec.capacity() == 4); + vec.push(11u64); + assert(vec.capacity() == 4); + vec.push(3u64); + assert(vec.capacity() == 8); +} + +#[test()] +fn vec_clear() { + let (mut vec, _, _, _) = setup(); + assert(vec.len() == 3); + + vec.clear(); + assert(vec.len() == 0); +} + +#[test()] +fn vec_clear_twice() { + let (mut vec, _, _, _) = setup(); + + vec.clear(); + assert(vec.len() == 0); + + // Can clean twice + vec.push(1u64); + vec.clear(); + assert(vec.len() == 0); +} + +#[test()] +fn vec_clear_empty_vec() { + // Clear on empty vec + let mut empty_vec: Vec = Vec::new(); + assert(empty_vec.len() == 0); + assert(empty_vec.capacity() == 0); + + empty_vec.clear(); + assert(empty_vec.len() == 0); + assert(empty_vec.capacity() == 0); +} + +#[test()] +fn vec_get() { + let (vec, a, b, c) = setup(); + assert(vec.len() == 3); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).unwrap() == c); + // get is non-modifying + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).unwrap() == c); + assert(vec.len() == 3); + + // None if out of bounds + assert(vec.get(vec.len()).is_none()); +} + +#[test()] +fn vec_len() { + let (mut vec, _, _, _) = setup(); + assert(vec.len() == 3); + + vec.push(5u64); + assert(vec.len() == 4); + vec.push(6u64); + assert(vec.len() == 5); + vec.push(7u64); + assert(vec.len() == 6); + vec.push(8u64); + assert(vec.len() == 7); +} + +#[test] +fn vec_is_empty() { + let (mut setup_vec, _, _, _) = setup(); + + assert(!setup_vec.is_empty()); + + let mut new_vec: Vec = Vec::new(); + assert(new_vec.is_empty()); + + let mut capacity_vec: Vec = Vec::with_capacity(16); + assert(capacity_vec.is_empty()); +} + +#[test()] +fn vec_remove() { + let (mut vec, a, b, c) = setup(); + let d = 7u64; + vec.push(d); + assert(vec.len() == 4); + assert(vec.capacity() == 4); + + // Remove in the middle + let item1 = vec.remove(1); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + assert(item1 == b); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == c); + assert(vec.get(2).unwrap() == d); + assert(vec.get(3).is_none()); +} + +#[test()] +fn vec_remove_front() { + let (mut vec, a, b, c) = setup(); + // Remove at the start + let item = vec.remove(0); + assert(vec.len() == 2); + assert(vec.capacity() == 4); + assert(item == a); + assert(vec.get(0).unwrap() == b); + assert(vec.get(1).unwrap() == c); + assert(vec.get(2).is_none()); +} + +#[test()] +fn vec_remove_end() { + let (mut vec, a, b, c) = setup(); + // Remove at the end + let item = vec.remove(vec.len() - 1); + assert(vec.len() == 2); + assert(vec.capacity() == 4); + assert(item == c); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).is_none()); +} + +#[test()] +fn vec_remove_all() { + let (mut vec, a, b, c) = setup(); + // Remove all + let item1 = vec.remove(0); + let item2 = vec.remove(0); + let item3 = vec.remove(0); + assert(vec.len() == 0); + assert(vec.capacity() == 4); + assert(item1 == a); + assert(item2 == b); + assert(item3 == c); + assert(vec.get(0).is_none()); +} + +#[test(should_revert)] +fn revert_vec_remove_out_of_bounds() { + let (mut vec, _a, _b, _c) = setup(); + + let _result = vec.remove(vec.len()); +} + +#[test()] +fn vec_insert() { + let (mut vec, a, b, c) = setup(); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + + let d = 11u64; + + // Inserts in the middle + vec.insert(1, d); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == d); + assert(vec.get(2).unwrap() == b); + assert(vec.get(3).unwrap() == c); + assert(vec.len() == 4); + assert(vec.capacity() == 4); +} + +#[test()] +fn vec_insert_twice() { + let (mut vec, a, b, c) = setup(); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + + let d = 11u64; + let e = 13u64; + + // Inserts in the middle + vec.insert(1, d); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == d); + assert(vec.get(2).unwrap() == b); + assert(vec.get(3).unwrap() == c); + assert(vec.len() == 4); + assert(vec.capacity() == 4); + + // Twice + vec.insert(1, e); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == e); + assert(vec.get(2).unwrap() == d); + assert(vec.get(3).unwrap() == b); + assert(vec.get(4).unwrap() == c); + assert(vec.len() == 5); + assert(vec.capacity() == 8); +} + +#[test()] +fn vec_insert_front() { + let (mut vec, a, b, c) = setup(); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + + let d = 11u64; + + // Inserts at the front + vec.insert(0, d); + assert(vec.get(0).unwrap() == d); + assert(vec.get(1).unwrap() == a); + assert(vec.get(2).unwrap() == b); + assert(vec.get(3).unwrap() == c); + assert(vec.len() == 4); + assert(vec.capacity() == 4); +} + +#[test()] +fn vec_insert_before_back() { + let (mut vec, a, b, c) = setup(); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + + let d = 11u64; + + // Inserts right before the back + vec.insert(vec.len() - 1, d); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).unwrap() == d); + assert(vec.get(3).unwrap() == c); + assert(vec.len() == 4); + assert(vec.capacity() == 4); +} + +#[test()] +fn vec_insert_back() { + let (mut vec, a, b, c) = setup(); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + + let d = 11u64; + + // Inserts at the back + vec.insert(vec.len(), d); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).unwrap() == c); + assert(vec.get(3).unwrap() == d); + assert(vec.len() == 4); + assert(vec.capacity() == 4); +} + +#[test(should_revert)] +fn revert_vec_insert_out_of_bounds() { + let (mut vec, a, _b, _c) = setup(); + + vec.insert(vec.len() + 1, a); +} + +#[test()] +fn vec_pop() { + let (mut vec, a, b, c) = setup(); + assert(vec.len() == 3); + + vec.push(42u64); + vec.push(11u64); + vec.push(69u64); + vec.push(100u64); + vec.push(200u64); + vec.push(255u64); + vec.push(180u64); + vec.push(17u64); + vec.push(19u64); + assert(vec.len() == 12); + assert(vec.capacity() == 16); + + let first = vec.pop(); + assert(first.unwrap() == 19u64); + assert(vec.len() == 11); + assert(vec.capacity() == 16); + + let second = vec.pop(); + assert(second.unwrap() == 17u64); + assert(vec.len() == 10); + assert(vec.capacity() == 16); + + let third = vec.pop(); + assert(third.unwrap() == 180u64); + assert(vec.len() == 9); + let _ = vec.pop(); + let _ = vec.pop(); + let _ = vec.pop(); + let _ = vec.pop(); + let _ = vec.pop(); + let _ = vec.pop(); + assert(vec.len() == 3); + assert(vec.pop().unwrap() == c); + assert(vec.pop().unwrap() == b); + assert(vec.pop().unwrap() == a); + + // Can pop all + assert(vec.len() == 0); + assert(vec.capacity() == 16); + assert(vec.pop().is_none()); +} + +#[test()] +fn vec_swap() { + let (mut vec, a, b, c) = setup(); + let d = 5u64; + vec.push(d); + assert(vec.len() == 4); + assert(vec.capacity() == 4); + + // Swaps Middle + vec.swap(1, 2); + assert(vec.len() == 4); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == c); + assert(vec.get(2).unwrap() == b); + assert(vec.get(3).unwrap() == d); +} + +#[test()] +fn vec_swap_twice() { + let (mut vec, a, b, c) = setup(); + let d = 5u64; + vec.push(d); + assert(vec.len() == 4); + assert(vec.capacity() == 4); + + // Swaps Middle + vec.swap(1, 2); + assert(vec.len() == 4); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == c); + assert(vec.get(2).unwrap() == b); + assert(vec.get(3).unwrap() == d); + + vec.swap(1, 2); + assert(vec.len() == 4); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).unwrap() == c); + assert(vec.get(3).unwrap() == d); +} + +#[test()] +fn vec_swap_front() { + let (mut vec, a, b, c) = setup(); + + // Swaps Front + vec.swap(0, 1); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == b); + assert(vec.get(1).unwrap() == a); + assert(vec.get(2).unwrap() == c); +} + +#[test()] +fn vec_swap_end() { + let (mut vec, a, b, c) = setup(); + + // Swaps back + vec.swap(2, 1); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == c); + assert(vec.get(2).unwrap() == b); +} + +#[test()] +fn vec_swap_front_with_end() { + let (mut vec, a, b, c) = setup(); + + // Swaps front with back + vec.swap(0, 2); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == c); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).unwrap() == a); +} + +#[test(should_revert)] +fn revert_vec_swap_element_1_out_of_bounds() { + let (mut vec, _a, _b, _c) = setup(); + + vec.swap(vec.len(), 0); +} + +#[test(should_revert)] +fn revert_vec_swap_element_2_out_of_bounds() { + let (mut vec, _a, _b, _c) = setup(); + + vec.swap(0, vec.len()); +} + +#[test()] +fn vec_set() { + let (mut vec, a, _b, c) = setup(); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + let d = 11u64; + + // Sets in the middle + vec.set(1, d); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == d); + assert(vec.get(2).unwrap() == c); +} + +#[test()] +fn vec_set_twice() { + let (mut vec, a, _b, c) = setup(); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + let d = 11u64; + let e = 13u64; + + // Sets in the middle + vec.set(1, d); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == d); + assert(vec.get(2).unwrap() == c); + + // Twice + vec.set(1, e); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == e); + assert(vec.get(2).unwrap() == c); +} + +#[test()] +fn vec_set_front() { + let (mut vec, _a, b, c) = setup(); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + let d = 11u64; + + // Sets at the front + vec.set(0, d); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == d); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).unwrap() == c); +} + +#[test()] +fn vec_set_back() { + let (mut vec, a, b, _c) = setup(); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + let d = 11u64; + + // Sets at the back + vec.set(vec.len() - 1, d); + assert(vec.len() == 3); + assert(vec.capacity() == 4); + assert(vec.get(0).unwrap() == a); + assert(vec.get(1).unwrap() == b); + assert(vec.get(2).unwrap() == d); +} + +#[test(should_revert)] +fn revert_vec_set_out_of_bounds() { + let (mut vec, _a, _b, _c) = setup(); + + vec.set(vec.len(), 11u64); +} + +#[test] +fn vec_iter() { + let mut vector: Vec = Vec::new(); + + let number0 = 0; + let number1 = 1; + let number2 = 2; + let number3 = 3; + let number4 = 4; + + vector.push(number0); + vector.push(number1); + vector.push(number2); + vector.push(number3); + vector.push(number4); + + let mut iter = vector.iter(); + + assert(iter.next() == Some(number0)); + assert(iter.next() == Some(number1)); + assert(iter.next() == Some(number2)); + assert(iter.next() == Some(number3)); + assert(iter.next() == Some(number4)); + assert(iter.next() == None); + assert(iter.next() == None); +} + +#[test] +fn vec_ptr() { + let (mut setup_vec, a, _, _) = setup(); + + let setup_vec_ptr = setup_vec.ptr(); + assert(!setup_vec_ptr.is_null()); + assert(setup_vec_ptr.read::() == a); + + let mut new_vec: Vec = Vec::new(); + let new_vec_ptr = new_vec.ptr(); + assert(!new_vec_ptr.is_null()); + + let mut capacity_vec: Vec = Vec::with_capacity(16); + let capacity_vec_ptr = capacity_vec.ptr(); + assert(!capacity_vec_ptr.is_null()); +} + +#[test()] +fn vec_as_raw_slice() { + let (mut vec, _a, _b, _c) = setup(); + + let slice = vec.as_raw_slice(); + assert(vec.ptr() == slice.ptr()); + assert(vec.len() == slice.len::()); +} + +#[test()] +fn vec_from_raw_slice() { + let val = 0x3497297632836282349729763283628234972976328362823497297632836282; + let slice = asm(ptr: (__addr_of(val), 32)) { + ptr: raw_slice + }; + + let mut vec: Vec = Vec::from(slice); + assert(vec.ptr() != slice.ptr()); // Vec should own its buffer + assert(vec.len() == slice.len::()); +} + +// TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6085 is resolved +// #[test()] +// fn vec_into_raw_slice() { +// // Glob operator needed for From for raw_slice +// use std::vec::*; + +// let (mut vec, _a, _b, _c) = setup(); + +// let slice: raw_slice = vec.into(); + +// assert(vec.ptr() == slice.ptr()); +// assert(vec.len() == slice.len::()); +// } + +// TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6085 is resolved +// #[test()] +// fn vec_raw_slice_from() { +// // Glob operator needed for From for raw_slice +// use std::vec::*; + +// let (mut vec, _a, _b, _c) = setup(); + +// let slice: raw_slice = >>::from(vec); + +// assert(vec.ptr() == slice.ptr()); +// assert(vec.len() == slice.len::()); +// } + + +#[test()] +fn vec_raw_slice_into() { + let val = 0x3497297632836282349729763283628234972976328362823497297632836282; + let slice = asm(ptr: (__addr_of(val), 32)) { + ptr: raw_slice + }; + + let vec: Vec = slice.into(); + + assert(vec.ptr() != slice.ptr()); // Vec should own its buffer + assert(vec.len() == slice.len::()); +} + +#[test()] +fn vec_encode_and_decode() { + let mut v1: Vec = Vec::new(); + v1.push(1); + v1.push(2); + v1.push(3); + + let v2 = abi_decode::>(encode(v1)); + + assert(v2.len() == 3); + assert(v2.capacity() == 3); + assert(v2.get(0) == Some(1)); + assert(v2.get(1) == Some(2)); + assert(v2.get(2) == Some(3)); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/Forc.toml new file mode 100644 index 00000000000..ecdc1cddaad --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "vm_evm_ecr_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/src/main.sw new file mode 100644 index 00000000000..f676386a47c --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/src/main.sw @@ -0,0 +1,26 @@ +library; + +use std::{b512::B512, vm::evm::{ecr::ec_recover_evm_address, evm_address::EvmAddress,}}; + +#[test] +fn ecr_ec_recover() { + let hi_1 = 0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c; + let lo_1 = 0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d; + let msg_hash_1 = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323; + let expected_evm_address = EvmAddress::from(0x0000000000000000000000000ec44cf95ce5051ef590e6d420f8e722dd160ecb); + let signature_1: B512 = B512::from((hi_1, lo_1)); + + let result_1 = ec_recover_evm_address(signature_1, msg_hash_1); + + assert(result_1.is_ok()); + assert(result_1.unwrap() == expected_evm_address); + + let hi_2 = 0xbd0c9b8792876713afa8bf1383eebf31c43437823ed761cc3600d0016de5110c; + let lo_2 = 0xee45573606c96c98ba170ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323; + let msg_hash_2 = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52cad30b89df1e4a9c4323; + let signature_2: B512 = B512::from((hi_2, lo_2)); + + let result_2 = ec_recover_evm_address(signature_2, msg_hash_2); + + assert(result_2.is_err()); +} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/Forc.toml new file mode 100644 index 00000000000..f51e58605eb --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "vm_evm_evm_address_inline_tests" + +[dependencies] +std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/src/main.sw new file mode 100644 index 00000000000..460d9e8e0d5 --- /dev/null +++ b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/src/main.sw @@ -0,0 +1,162 @@ +library; + +use std::vm::evm::evm_address::EvmAddress; + +#[test] +fn evm_address_bits() { + let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + + assert( + evm_address_1 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + assert( + evm_address_2 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + assert( + evm_address_3 + .bits() == 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff, + ); +} + +#[test] +fn evm_address_eq() { + let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let evm_address_3 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let evm_address_4 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let evm_address_5 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + let evm_address_6 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + + assert(evm_address_1 == evm_address_2); + assert(evm_address_3 == evm_address_4); + assert(evm_address_5 == evm_address_6); +} + +#[test] +fn evm_address_ne() { + let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + + assert(evm_address_1 != evm_address_2); + assert(evm_address_1 != evm_address_3); + assert(evm_address_2 != evm_address_3); +} + +#[test] +fn evm_address_from_b256() { + let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + + assert( + evm_address_1 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); + assert( + evm_address_2 + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, + ); + assert( + evm_address_3 + .bits() == 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff, + ); +} + +#[test] +fn evm_address_into_b256() { + let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + + let b256_1: b256 = evm_address_1.into(); + let b256_2: b256 = evm_address_2.into(); + let b256_3: b256 = evm_address_3.into(); + + assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + assert(b256_2 == 0x0000000000000000000000000000000000000000000000000000000000000001); + assert(b256_3 == 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff); +} + +#[test] +fn evm_address_b256_from() { + // Glob operator is needed for from for b256 + use std::vm::evm::evm_address::*; + + let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + + let b256_1 = b256::from(evm_address_1); + let b256_2 = b256::from(evm_address_2); + let b256_3 = b256::from(evm_address_3); + + assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + assert(b256_2 == 0x0000000000000000000000000000000000000000000000000000000000000001); + assert(b256_3 == 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff); +} + +#[test] +fn evm_address_b256_into() { + // Glob operator is needed for into for b256 + use std::vm::evm::evm_address::*; + + let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + + let b256_1: b256 = evm_address_1.into(); + let b256_2: b256 = evm_address_2.into(); + let b256_3: b256 = evm_address_3.into(); + + assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); + assert(b256_2 == 0x0000000000000000000000000000000000000000000000000000000000000001); + assert(b256_3 == 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff); +} + +#[test] +fn evm_address_zero() { + let evm_address = EvmAddress::zero(); + assert( + evm_address + .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, + ); +} + +#[test] +fn evm_address_is_zero() { + let evm_address_1 = EvmAddress::zero(); + let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let evm_address_3 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let evm_address_4 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); + + assert(evm_address_1.is_zero()); + assert(evm_address_2.is_zero()); + assert(!evm_address_3.is_zero()); + assert(!evm_address_4.is_zero()); +} + +#[test] +fn evm_address_hash() { + use std::hash::{Hash, sha256}; + + let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); + let digest_1 = sha256(evm_address_1); + assert(digest_1 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); + + let evm_address_2 = EvmAddress::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + let digest_2 = sha256(evm_address_2); + assert(digest_2 == 0x78230345cedf8e92525c3cfdb8a95e947de1ed72e881b055dd80f9e523ff33e0); + + let evm_address_3 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); + let digest_3 = sha256(evm_address_3); + assert(digest_3 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); + + let evm_address_4 = EvmAddress::from(0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); + let digest_4 = sha256(evm_address_4); + assert(digest_4 != 0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); +} diff --git a/test/src/e2e_vm_tests/mod.rs b/test/src/e2e_vm_tests/mod.rs index b5ed9a43040..b163d64d892 100644 --- a/test/src/e2e_vm_tests/mod.rs +++ b/test/src/e2e_vm_tests/mod.rs @@ -12,6 +12,7 @@ use core::fmt; use forc_pkg::BuildProfile; use forc_test::decode_log_data; use fuel_vm::fuel_tx; +use fuel_vm::fuel_types::canonical::Input; use fuel_vm::prelude::*; use regex::Regex; use std::collections::HashSet; @@ -113,6 +114,8 @@ struct TestContext { } fn print_receipts(output: &mut String, receipts: &[Receipt]) { + let mut text_log = String::new(); + use std::fmt::Write; let _ = writeln!(output, " {}", "Receipts".green().bold()); for (i, receipt) in receipts.iter().enumerate() { @@ -129,6 +132,28 @@ fn print_receipts(output: &mut String, receipts: &[Receipt]) { is, data, } => { + // Small hack to allow log from tests. + if *ra == u64::MAX { + match rb { + 0 => { + let mut data = data.as_deref().unwrap(); + data.skip(8).unwrap(); + let s = std::str::from_utf8(data).unwrap(); + + text_log.push_str(s); + } + 1 => { + let data = data.as_deref().unwrap(); + let s = u64::from_be_bytes(data.try_into().unwrap()); + + text_log.push_str(&format!("[{}]", s)); + } + 2 => { + text_log.push('\n'); + } + _ => {} + } + } let _ = write!(output, " LogData\n ID: {id:?}\n RA: {ra:?}\n RB: {rb:?}\n Ptr: {ptr:?}\n Len: {len:?}\n Digest: {digest:?}\n PC: {pc:?}\n IS: {is:?}\n Data: {data:?}\n"); } Receipt::ReturnData { @@ -238,6 +263,14 @@ fn print_receipts(output: &mut String, receipts: &[Receipt]) { } } } + + if !text_log.is_empty() { + let _ = writeln!(output, " {}", "Text Logs".green().bold()); + + for l in text_log.lines() { + let _ = writeln!(output, "{l}"); + } + } } impl TestContext { diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json index e60dda965d4..17f3d600e0d 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/json_abi_oracle_new_encoding.json @@ -13,7 +13,16 @@ } } ], - "loggedTypes": [], + "loggedTypes": [ + { + "logId": "1515152261580153489", + "loggedType": { + "name": "", + "type": 1, + "typeArguments": null + } + } + ], "messagesTypes": [], "types": [ { @@ -21,6 +30,12 @@ "type": "()", "typeId": 0, "typeParameters": null + }, + { + "components": null, + "type": "u64", + "typeId": 1, + "typeParameters": null } ] } \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 2ab971a0027..7c1ba559a4d 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -1,22 +1,205 @@ script; -fn assert(actual: u64, expected: u64) { - if actual != expected { - __revert(actual); +trait Log { + fn log(self); +} + +impl Log for str { + fn log(self) { + let encoded = encode(self); + asm(ra: u64::max(), ptr: encoded.ptr(), len: encoded.len::()) { + logd ra zero ptr len; + } + } +} + + +impl Log for u64 { + fn log(self) { + let encoded = encode(self); + asm(ra: u64::max(), ptr: encoded.ptr(), len: encoded.len::()) { + logd ra one ptr len; + } } } -// we can slice arrays with non-literals -fn slice_method(array: [u64; 4], start: u64, end: u64, expected: __slice[u64]) { - // slice can happen with variables - let actual: __slice[u64] = __slice(array, start, end); +struct NewLine {} - if actual != expected { - __revert(1); +fn new_line() -> NewLine { + NewLine { } +} + +impl Log for NewLine { + fn log(self) { + asm(ra: u64::max(), rb: 2) { + logd ra rb zero zero; + } + } +} + +impl Log for (A, B) +where + A: Log, + B: Log, +{ + fn log(self) { + self.0.log(); + self.1.log(); + } +} + +impl Log for (A, B, C) +where + A: Log, + B: Log, + C: Log, +{ + #[allow(dead_code)] + fn log(self) { + self.0.log(); + self.1.log(); + self.2.log(); + } +} + +impl Log for (A, B, C, D) +where + A: Log, + B: Log, + C: Log, + D: Log +{ + fn log(self) { + self.0.log(); + self.1.log(); + self.2.log(); + self.3.log(); + } +} + + +impl Log for (A, B, C, D, E) +where + A: Log, + B: Log, + C: Log, + D: Log, + E: Log, +{ + fn log(self) { + self.0.log(); + self.1.log(); + self.2.log(); + self.3.log(); + self.4.log(); + } +} + +impl Log for Vec { + fn log(self) { + ( + "Vec", + asm(v: self.buf.ptr()) { v: u64 }, + self.buf.len(), + self.len, + ).log(); + } +} + +pub struct Vec { + buf: &__slice[T], + len: u64, +} + +impl Vec { + pub fn new() -> Self { + let ptr = asm() { + hp: raw_ptr + }; + Self { + buf: asm(buf: (ptr, 0)) { + buf: &__slice[T] + }, + len: 0 + } + } + + pub fn push(ref mut self, item: T) { + ("Vec::push", new_line()).log(); + (self, new_line()).log(); + + let new_item_idx = self.len; + let current_cap = self.buf.len(); + if new_item_idx >= current_cap { + let new_cap = if current_cap == 0 { + 1 + } else { + current_cap * 2 + }; + let new_cap_in_bytes = new_cap * __size_of::(); + + let old_buf_ptr = self.buf.ptr(); + let old_cap_in_bytes = current_cap * __size_of::(); + + let ptr = asm(new_cap_in_bytes: new_cap_in_bytes, old_buf_ptr: old_buf_ptr, old_cap_in_bytes: old_cap_in_bytes) { + aloc new_cap_in_bytes; + mcp hp old_buf_ptr old_cap_in_bytes; + hp: raw_ptr + }; + + self.buf = asm(buf: (ptr, new_cap)) { + buf: &__slice[T] + }; + } + + (self, new_line()).log(); + + let v: &mut T = __slice_elem(self.buf, new_item_idx); + ("elem", new_item_idx, " at ", asm(v: v) { v: u64 }, NewLine{}).log(); + *v = item; + + self.len += 1; + } + + pub fn get(self, index: u64) -> T { + ("Vec::get ", index).log(); + let item: &mut T = __slice_elem(self.buf, index); + (asm(v: item) { v: u64 }, NewLine{}).log(); + *item + } +} + +fn assert(l: T, r: T) +where + T: Eq + AbiEncode +{ + if l != r { + __log(l); + __log(r); + __revert(1) } } fn main() { - let a: [u64; 0] = []; - __log(1); + let mut v: Vec = Vec::new(); + v.push(1); + assert(v.get(0), 1); + + v.push(2); + v.push(3); + assert(v.get(0), 1); + assert(v.get(1), 2); + assert(v.get(2), 3); + + v.push(4); + v.push(5); + v.push(6); + v.push(7); + assert(v.get(0), 1); + assert(v.get(1), 2); + assert(v.get(2), 3); + assert(v.get(3), 4); + assert(v.get(4), 5); + assert(v.get(5), 6); + assert(v.get(6), 7); } From 35d252ca6b0d618d550373868b8560e6588c8bef Mon Sep 17 00:00:00 2001 From: xunilrj Date: Fri, 19 Jul 2024 15:43:15 +0100 Subject: [PATCH 09/22] better dbg --- test/src/e2e_vm_tests/mod.rs | 2 +- .../language/slice/slice_intrinsics/Forc.lock | 8 + .../language/slice/slice_intrinsics/Forc.toml | 1 + .../slice/slice_intrinsics/src/main.sw | 170 ++++++------------ test/src/e2e_vm_tests/utils/.gitignore | 2 + test/src/e2e_vm_tests/utils/Forc.toml | 8 + test/src/e2e_vm_tests/utils/src/main.sw | 146 +++++++++++++++ 7 files changed, 216 insertions(+), 121 deletions(-) create mode 100644 test/src/e2e_vm_tests/utils/.gitignore create mode 100644 test/src/e2e_vm_tests/utils/Forc.toml create mode 100644 test/src/e2e_vm_tests/utils/src/main.sw diff --git a/test/src/e2e_vm_tests/mod.rs b/test/src/e2e_vm_tests/mod.rs index b163d64d892..4c8664fbe40 100644 --- a/test/src/e2e_vm_tests/mod.rs +++ b/test/src/e2e_vm_tests/mod.rs @@ -146,7 +146,7 @@ fn print_receipts(output: &mut String, receipts: &[Receipt]) { let data = data.as_deref().unwrap(); let s = u64::from_be_bytes(data.try_into().unwrap()); - text_log.push_str(&format!("[{}]", s)); + text_log.push_str(&format!("{}", s)); } 2 => { text_log.push('\n'); diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock index c5bc5415952..a7de4b3565f 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.lock @@ -5,4 +5,12 @@ source = "path+from-root-53D6DC514F06A250" [[package]] name = "slice_intrinsics" source = "member" +dependencies = [ + "core", + "utils", +] + +[[package]] +name = "utils" +source = "path+from-root-53D6DC514F06A250" dependencies = ["core"] diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml index a7ed10b10d3..fe47736bbcf 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/Forc.toml @@ -6,3 +6,4 @@ name = "slice_intrinsics" [dependencies] core = { path = "../../../../../../../../sway-lib-core" } +utils = { path = "../../../../../../../../test/src/e2e_vm_tests/utils" } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 7c1ba559a4d..cf66404e5b4 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -1,132 +1,65 @@ script; -trait Log { - fn log(self); -} - -impl Log for str { - fn log(self) { - let encoded = encode(self); - asm(ra: u64::max(), ptr: encoded.ptr(), len: encoded.len::()) { - logd ra zero ptr len; - } - } -} - - -impl Log for u64 { - fn log(self) { - let encoded = encode(self); - asm(ra: u64::max(), ptr: encoded.ptr(), len: encoded.len::()) { - logd ra one ptr len; - } +use utils::*; + +fn alloc_slice(len: u64) -> &__slice[T] { + let size_in_bytes = len * __size_of::(); + let ptr = asm(size_in_bytes: size_in_bytes) { + aloc size_in_bytes; + hp: raw_ptr + }; + asm(buf: (ptr, len)) { + buf: &__slice[T] } } -struct NewLine {} - -fn new_line() -> NewLine { - NewLine { } -} - -impl Log for NewLine { - fn log(self) { - asm(ra: u64::max(), rb: 2) { - logd ra rb zero zero; - } - } -} +fn realloc_slice(old: &__slice[T], len: u64) -> &__slice[T] { + let old_ptr = old.ptr(); + let old_len_in_bytes = old.len() * __size_of::(); -impl Log for (A, B) -where - A: Log, - B: Log, -{ - fn log(self) { - self.0.log(); - self.1.log(); - } -} + let new_len_in_bytes = len * __size_of::(); + let new_ptr = asm(new_len_in_bytes: new_len_in_bytes, old_ptr: old_ptr, old_len_in_bytes: old_len_in_bytes) { + aloc new_len_in_bytes; + mcp hp old_ptr old_len_in_bytes; + hp: raw_ptr + }; -impl Log for (A, B, C) -where - A: Log, - B: Log, - C: Log, -{ - #[allow(dead_code)] - fn log(self) { - self.0.log(); - self.1.log(); - self.2.log(); + asm(buf: (new_ptr, len)) { + buf: &__slice[T] } } -impl Log for (A, B, C, D) -where - A: Log, - B: Log, - C: Log, - D: Log -{ - fn log(self) { - self.0.log(); - self.1.log(); - self.2.log(); - self.3.log(); - } -} - - -impl Log for (A, B, C, D, E) -where - A: Log, - B: Log, - C: Log, - D: Log, - E: Log, -{ - fn log(self) { - self.0.log(); - self.1.log(); - self.2.log(); - self.3.log(); - self.4.log(); - } +pub struct Vec { + buf: &__slice[T], + len: u64, } -impl Log for Vec { - fn log(self) { +impl Dbg for Vec { + fn dbg(self) { ( - "Vec", + "Vec { buf: (ptr: ", asm(v: self.buf.ptr()) { v: u64 }, + ", len: ", self.buf.len(), + "), len: ", self.len, - ).log(); + ")" + ).dbg(); } } -pub struct Vec { - buf: &__slice[T], - len: u64, -} impl Vec { pub fn new() -> Self { - let ptr = asm() { - hp: raw_ptr - }; Self { - buf: asm(buf: (ptr, 0)) { - buf: &__slice[T] - }, + buf: alloc_slice::(0), len: 0 } } pub fn push(ref mut self, item: T) { - ("Vec::push", new_line()).log(); - (self, new_line()).log(); + "Vec::push(...)".dbgln(); + (" ", self).dbgln(); let new_item_idx = self.len; let current_cap = self.buf.len(); @@ -136,35 +69,32 @@ impl Vec { } else { current_cap * 2 }; - let new_cap_in_bytes = new_cap * __size_of::(); - - let old_buf_ptr = self.buf.ptr(); - let old_cap_in_bytes = current_cap * __size_of::(); - - let ptr = asm(new_cap_in_bytes: new_cap_in_bytes, old_buf_ptr: old_buf_ptr, old_cap_in_bytes: old_cap_in_bytes) { - aloc new_cap_in_bytes; - mcp hp old_buf_ptr old_cap_in_bytes; - hp: raw_ptr - }; - - self.buf = asm(buf: (ptr, new_cap)) { - buf: &__slice[T] - }; + self.buf = realloc_slice(self.buf, new_cap); + (" After realloc: ", self).dbgln(); } - (self, new_line()).log(); - let v: &mut T = __slice_elem(self.buf, new_item_idx); - ("elem", new_item_idx, " at ", asm(v: v) { v: u64 }, NewLine{}).log(); + + let buffer_addr = asm(v: self.buf.ptr()) { v: u64 }; + let elem_addr = asm(v: v) { v: u64 }; + (" elem ", new_item_idx, " at ", elem_addr, " buffer offset (in bytes): ", elem_addr - buffer_addr).dbgln(); *v = item; self.len += 1; + + (" ", self).dbgln(); } pub fn get(self, index: u64) -> T { - ("Vec::get ", index).log(); + ("Vec::get(", index, ")").dbgln(); + (" ", self).dbgln(); + let item: &mut T = __slice_elem(self.buf, index); - (asm(v: item) { v: u64 }, NewLine{}).log(); + + let buffer_addr = asm(v: self.buf.ptr()) { v: u64 }; + let elem_addr = asm(v: item) { v: u64 }; + (" element ", index, " at ", elem_addr, " buffer offset (in bytes): ", elem_addr - buffer_addr).dbgln(); + *item } } diff --git a/test/src/e2e_vm_tests/utils/.gitignore b/test/src/e2e_vm_tests/utils/.gitignore new file mode 100644 index 00000000000..77d3844f58c --- /dev/null +++ b/test/src/e2e_vm_tests/utils/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/test/src/e2e_vm_tests/utils/Forc.toml b/test/src/e2e_vm_tests/utils/Forc.toml new file mode 100644 index 00000000000..10a6cda5587 --- /dev/null +++ b/test/src/e2e_vm_tests/utils/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "utils" + +[dependencies] +core = { path = "../../../../sway-lib-core" } diff --git a/test/src/e2e_vm_tests/utils/src/main.sw b/test/src/e2e_vm_tests/utils/src/main.sw new file mode 100644 index 00000000000..4dcfbf52f28 --- /dev/null +++ b/test/src/e2e_vm_tests/utils/src/main.sw @@ -0,0 +1,146 @@ +library; + +/// This is only useful for the e2e harness setup, because +/// no one else knows how to "decode" this into meaningful +// textual logs. +pub trait Dbg { + fn dbg(self); +} { + fn dbgln(self) { + self.dbg(); + asm(ra: u64::max(), rb: 2) { + logd ra rb zero zero; + } + } +} + +impl Dbg for str { + fn dbg(self) { + let encoded = encode(self); + asm(ra: u64::max(), ptr: encoded.ptr(), len: encoded.len::()) { + logd ra zero ptr len; + } + } +} + + +impl Dbg for u64 { + fn dbg(self) { + let encoded = encode(self); + asm(ra: u64::max(), ptr: encoded.ptr(), len: encoded.len::()) { + logd ra one ptr len; + } + } +} + +pub struct NewLine {} + +pub fn new_line() -> NewLine { + NewLine { } +} + +impl Dbg for NewLine { + fn dbg(self) { + asm(ra: u64::max(), rb: 2) { + logd ra rb zero zero; + } + } +} + +impl Dbg for (A, B) +where + A: Dbg, + B: Dbg, +{ + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + } +} + +impl Dbg for (A, B, C) +where + A: Dbg, + B: Dbg, + C: Dbg, +{ + #[allow(dead_code)] + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + self.2.dbg(); + } +} + +impl Dbg for (A, B, C, D) +where + A: Dbg, + B: Dbg, + C: Dbg, + D: Dbg +{ + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + self.2.dbg(); + self.3.dbg(); + } +} + + +impl Dbg for (A, B, C, D, E) +where + A: Dbg, + B: Dbg, + C: Dbg, + D: Dbg, + E: Dbg, +{ + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + self.2.dbg(); + self.3.dbg(); + self.4.dbg(); + } +} + +impl Dbg for (A, B, C, D, E, F) +where + A: Dbg, + B: Dbg, + C: Dbg, + D: Dbg, + E: Dbg, + F: Dbg, +{ + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + self.2.dbg(); + self.3.dbg(); + self.4.dbg(); + self.5.dbg(); + } +} + +impl Dbg for (A, B, C, D, E, F, G) +where + A: Dbg, + B: Dbg, + C: Dbg, + D: Dbg, + E: Dbg, + F: Dbg, + G: Dbg +{ + fn dbg(self) { + self.0.dbg(); + self.1.dbg(); + self.2.dbg(); + self.3.dbg(); + self.4.dbg(); + self.5.dbg(); + self.6.dbg(); + } +} From 9cb86ffb27ff18c7f6f0f5713c086e2bcfd407c3 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Mon, 22 Jul 2024 12:21:38 +0100 Subject: [PATCH 10/22] fix commit --- sway-core/src/ir_generation/convert.rs | 2 +- sway-core/src/ir_generation/function.rs | 4 +- .../e2e_vm_tests/in_language_tests/.gitignore | 1 - .../e2e_vm_tests/in_language_tests/Forc.toml | 43 - .../address_inline_tests/Forc.toml | 8 - .../address_inline_tests/src/main.sw | 182 --- .../alloc_inline_tests/Forc.toml | 8 - .../alloc_inline_tests/src/main.sw | 333 ------ .../Forc.toml | 8 - .../src/main.sw | 55 - .../Forc.toml | 8 - .../src/main.sw | 37 - .../Forc.toml | 8 - .../src/main.sw | 55 - .../Forc.toml | 8 - .../src/main.sw | 41 - .../Forc.toml | 8 - .../src/main.sw | 49 - .../assert_inline_tests/Forc.toml | 8 - .../assert_inline_tests/src/main.sw | 171 --- .../asset_id_contract_tests/Forc.toml | 8 - .../asset_id_contract_tests/src/main.sw | 22 - .../asset_id_inline_tests/Forc.toml | 8 - .../asset_id_inline_tests/src/main.sw | 252 ---- .../test_programs/b512_inline_tests/Forc.toml | 8 - .../b512_inline_tests/src/main.sw | 284 ----- .../block_inline_tests/Forc.toml | 8 - .../block_inline_tests/src/main.sw | 55 - .../Forc.toml | 8 - .../src/main.sw | 58 - .../Forc.toml | 8 - .../src/main.sw | 41 - .../Forc.toml | 8 - .../src/main.sw | 58 - .../Forc.toml | 8 - .../src/main.sw | 49 - .../Forc.toml | 8 - .../src/main.sw | 65 - .../bytes_inline_tests/Forc.toml | 8 - .../bytes_inline_tests/src/main.sw | 1047 ----------------- .../contract_id_contract_tests/Forc.toml | 8 - .../contract_id_contract_tests/src/main.sw | 20 - .../contract_id_inline_tests/Forc.toml | 8 - .../contract_id_inline_tests/src/main.sw | 182 --- .../test_programs/ecr_inline_tests/Forc.toml | 8 - .../ecr_inline_tests/src/main.sw | 133 --- .../flags_inline_tests/Forc.toml | 8 - .../flags_inline_tests/src/main.sw | 67 -- .../test_programs/hash_inline_tests/Forc.toml | 8 - .../hash_inline_tests/src/main.sw | 457 ------- .../identity_inline_tests/Forc.toml | 8 - .../identity_inline_tests/src/main.sw | 247 ---- .../test_programs/math_inline_tests/Forc.toml | 8 - .../math_inline_tests/src/main.sw | 632 ---------- .../option_inline_tests/Forc.toml | 8 - .../option_inline_tests/src/main.sw | 354 ------ .../Forc.toml | 8 - .../src/main.sw | 168 --- .../Forc.toml | 8 - .../src/main.sw | 14 - .../Forc.toml | 8 - .../src/main.sw | 129 -- .../Forc.toml | 8 - .../src/main.sw | 311 ----- .../Forc.toml | 8 - .../src/main.sw | 135 --- .../Forc.toml | 8 - .../src/main.sw | 141 --- .../Forc.toml | 8 - .../src/main.sw | 123 -- .../result_inline_tests/Forc.toml | 8 - .../result_inline_tests/src/main.sw | 194 --- .../revert_inline_tests/Forc.toml | 8 - .../revert_inline_tests/src/main.sw | 16 - .../string_inline_tests/Forc.toml | 8 - .../string_inline_tests/src/main.sw | 382 ------ .../test_programs/u128_inline_tests/Forc.toml | 8 - .../u128_inline_tests/src/main.sw | 744 ------------ .../test_programs/vec_inline_tests/Forc.toml | 8 - .../vec_inline_tests/src/main.sw | 704 ----------- .../vm_evm_ecr_inline_tests/Forc.toml | 8 - .../vm_evm_ecr_inline_tests/src/main.sw | 26 - .../vm_evm_evm_address_inline_tests/Forc.toml | 8 - .../src/main.sw | 162 --- .../should_fail/array_oob/test.toml | 2 +- .../array_oob_global_const_index/test.toml | 2 +- .../array_oob_variable_index/test.toml | 2 +- 87 files changed, 6 insertions(+), 8565 deletions(-) delete mode 100644 test/src/e2e_vm_tests/in_language_tests/.gitignore delete mode 100644 test/src/e2e_vm_tests/in_language_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/src/main.sw delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/Forc.toml delete mode 100644 test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/src/main.sw diff --git a/sway-core/src/ir_generation/convert.rs b/sway-core/src/ir_generation/convert.rs index 396425f1ac2..3aa22401338 100644 --- a/sway-core/src/ir_generation/convert.rs +++ b/sway-core/src/ir_generation/convert.rs @@ -60,7 +60,7 @@ pub(super) fn convert_resolved_type_id( span: &Span, ) -> Result { let t = type_engine.get(ast_type); - convert_resolved_type_info(type_engine, decl_engine, context, &*t, span) + convert_resolved_type_info(type_engine, decl_engine, context, &t, span) } pub(super) fn convert_resolved_typeid_no_span( diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index b4f2bdde830..0203d230340 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -2168,7 +2168,7 @@ impl<'eng> FnCompiler<'eng> { fn compile_intrinsic_slice_elem( &mut self, - arguments: &Vec, + arguments: &[ty::TyExpression], context: &mut Context, md_mgr: &mut MetadataManager, ) -> Result { @@ -2251,7 +2251,7 @@ impl<'eng> FnCompiler<'eng> { fn compile_intrinsic_slice( &mut self, - arguments: &Vec, + arguments: &[ty::TyExpression], context: &mut Context, md_mgr: &mut MetadataManager, ) -> Result { diff --git a/test/src/e2e_vm_tests/in_language_tests/.gitignore b/test/src/e2e_vm_tests/in_language_tests/.gitignore deleted file mode 100644 index 85059217cde..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/.gitignore +++ /dev/null @@ -1 +0,0 @@ -Forc.lock diff --git a/test/src/e2e_vm_tests/in_language_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/Forc.toml deleted file mode 100644 index e015fae4ff0..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/Forc.toml +++ /dev/null @@ -1,43 +0,0 @@ -[workspace] -members = [ - "test_programs/address_inline_tests", - "test_programs/alloc_inline_tests", - "test_programs/assert_inline_tests", - "test_programs/asset_id_inline_tests", - "test_programs/asset_id_contract_tests", - "test_programs/b512_inline_tests", - "test_programs/block_inline_tests", - "test_programs/bytes_inline_tests", - "test_programs/contract_id_inline_tests", - "test_programs/contract_id_contract_tests", - "test_programs/ecr_inline_tests", - "test_programs/flags_inline_tests", - "test_programs/hash_inline_tests", - "test_programs/identity_inline_tests", - "test_programs/math_inline_tests", - "test_programs/option_inline_tests", - "test_programs/result_inline_tests", - "test_programs/revert_inline_tests", - "test_programs/string_inline_tests", - "test_programs/u128_inline_tests", - "test_programs/vec_inline_tests", - "test_programs/array_conversions_b256_inline_tests", - "test_programs/array_conversions_u16_inline_tests", - "test_programs/array_conversions_u32_inline_tests", - "test_programs/array_conversions_u64_inline_tests", - "test_programs/array_conversions_u256_inline_tests", - "test_programs/bytes_conversions_b256_inline_tests", - "test_programs/bytes_conversions_u16_inline_tests", - "test_programs/bytes_conversions_u32_inline_tests", - "test_programs/bytes_conversions_u64_inline_tests", - "test_programs/bytes_conversions_u256_inline_tests", - "test_programs/primitive_conversions_b256_inline_tests", - "test_programs/primitive_conversions_str_inline_tests", - "test_programs/primitive_conversions_u8_inline_tests", - "test_programs/primitive_conversions_u16_inline_tests", - "test_programs/primitive_conversions_u32_inline_tests", - "test_programs/primitive_conversions_u64_inline_tests", - "test_programs/primitive_conversions_u256_inline_tests", - "test_programs/vm_evm_evm_address_inline_tests", - "test_programs/vm_evm_ecr_inline_tests", -] diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/Forc.toml deleted file mode 100644 index b4b0a53ec38..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "address_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/src/main.sw deleted file mode 100644 index f86f63e5a16..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/address_inline_tests/src/main.sw +++ /dev/null @@ -1,182 +0,0 @@ -library; - -#[test] -fn address_bits() { - let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let bits1 = address1.bits(); - assert(bits1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - - let address2 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let bits2 = address2.bits(); - assert(bits2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - - let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let bits3 = address3.bits(); - assert(bits3 == 0x0000000000000000000000000000000000000000000000000000000000000001); -} - -#[test] -fn address_eq() { - let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let address2 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let address4 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let address5 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let address6 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - - assert(address1 == address2); - assert(address3 == address4); - assert(address5 == address6); -} - -#[test] -fn address_ne() { - let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let address2 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let address4 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let address5 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let address6 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - - assert(address1 != address3); - assert(address1 != address4); - assert(address1 != address5); - assert(address1 != address6); - assert(address2 != address3); - assert(address2 != address4); - assert(address2 != address5); - assert(address2 != address6); - assert(address3 != address5); - assert(address3 != address6); - assert(address4 != address5); - assert(address4 != address6); -} - -#[test] -fn address_from_b256() { - let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); - assert( - address1 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let address1 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - assert( - address1 - .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - - let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert( - address3 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn address_b256_into() { - let b256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000; - let address1: Address = b256_1.into(); - assert( - address1 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let b256_2 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; - let address2: Address = b256_2.into(); - assert( - address2 - .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - - let b256_3 = 0x0000000000000000000000000000000000000000000000000000000000000001; - let address3: Address = b256_3.into(); - assert( - address3 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn address_into_b256() { - let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let b256_data1: b256 = address1.into(); - assert( - b256_data1 == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let address2 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let b256_data2: b256 = address2.into(); - assert( - b256_data2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - - let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_data3: b256 = address3.into(); - assert( - b256_data3 == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn address_b256_from() { - let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let b256_data1: b256 = b256::from(address1); - assert( - b256_data1 == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let address2 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let b256_data2: b256 = b256::from(address2); - assert( - b256_data2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - - let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_data3: b256 = b256::from(address3); - assert( - b256_data3 == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn address_hash() { - use std::hash::{Hash, sha256}; - - let address1 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let digest1 = sha256(address1); - assert(digest1 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let address2 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let digest2 = sha256(address2); - assert(digest2 == 0xaf9613760f72635fbdb44a5a0a63c39f12af30f950a6ee5c971be188e89c4051); - - let address3 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let digest3 = sha256(address3); - assert(digest3 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); - - let address4 = Address::from(0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); - let digest4 = sha256(address4); - assert(digest4 != 0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); -} - -#[test] -fn address_zero() { - let address = Address::zero(); - assert( - address - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); -} - -#[test] -fn address_is_zero() { - let zero_address = Address::zero(); - assert(zero_address.is_zero()); - - let address_2 = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert(!address_2.is_zero()); - - let address_3 = Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - assert(!address_3.is_zero()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/Forc.toml deleted file mode 100644 index aa8391756f5..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "alloc_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/src/main.sw deleted file mode 100644 index 54c9536290f..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/alloc_inline_tests/src/main.sw +++ /dev/null @@ -1,333 +0,0 @@ -library; - -use std::alloc::*; - -#[test] -fn alloc_alloc() { - // Alloc zero - let zero_ptr = alloc::(0); - assert(!zero_ptr.is_null()); - - // Can alloc u8 - let u8_ptr = alloc::(1); - assert(u8_ptr.read::() == 0u8); - u8_ptr.write(u8::max()); - assert(u8_ptr.read::() == u8::max()); - - // Can alloc u64 - let u64_ptr = alloc::(1); - assert(u64_ptr.read::() == 0u64); - u64_ptr.write(u64::max()); - assert(u64_ptr.read::() == u64::max()); - - // Can alloc b256 - let b256_ptr = alloc::(1); - assert(b256_ptr.read::() == b256::zero()); - b256_ptr.write(b256::max()); - assert(b256_ptr.read::() == b256::max()); - - // Can alloc struct - let address_ptr = alloc::
(1); - assert(address_ptr.read::
() == Address::zero()); - address_ptr.write(Address::from(b256::max())); - assert(address_ptr.read::
() == Address::from(b256::max())); - - // Can alloc multiple - let count = 1000; - let multiple_u64 = alloc::(count); - let mut iter = 0; - while iter < count { - assert(multiple_u64.add::(iter).read::() == 0u64); - iter += 1; - } - - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // Can alloc array - // let array_ptr = alloc::<[u64; 1]>(2); - // assert(array_ptr.read::() == 0u64); - // assert(array_ptr.add::(1).read::() == 0u64); - // array_ptr.write(u64::max()); - // array_ptr.add::(1).write(u64::max()); - // assert(array_ptr.read::() == u64::max()); - // assert(array_ptr.add::(1).read::() == u64::max()); -} - -#[test(should_revert)] -fn revert_alloc_alloc_does_not_exceed_bounds() { - let u64_ptr = alloc::(1); - assert(u64_ptr.read::() == 0u64); - - let _out_of_bounds = u64_ptr.add::(1).read::(); -} - -#[test] -fn alloc_realloc() { - // realloc from zero - let zero_ptr = alloc::(0); - let realloc_zero_ptr = realloc::(zero_ptr, 0, 1); - assert(realloc_zero_ptr.read::() == u64::zero()); - - // Can realloc u8 - let u8_ptr = alloc::(1); - u8_ptr.write(u8::max()); - let realloc_u8_ptr = realloc::(u8_ptr, 1, 2); - assert(realloc_u8_ptr.read::() == u8::max()); - assert(realloc_u8_ptr.add::(1).read::() == u8::zero()); - - // Can alloc u64 - let u64_ptr = alloc::(1); - u64_ptr.write(u64::max()); - let realloc_u64_ptr = realloc::(u64_ptr, 1, 2); - assert(realloc_u64_ptr.read::() == u64::max()); - assert(realloc_u64_ptr.add::(1).read::() == u64::zero()); - - // Can alloc b256 - let b256_ptr = alloc::(1); - b256_ptr.write(b256::max()); - let realloc_b256_ptr = realloc::(b256_ptr, 1, 2); - assert(realloc_b256_ptr.read::() == b256::max()); - assert(realloc_b256_ptr.add::(1).read::() == b256::zero()); - - // Can alloc struct - let address_ptr = alloc::
(1); - address_ptr.write(Address::from(b256::max())); - let realloc_address_ptr = realloc::
(address_ptr, 1, 2); - assert(realloc_address_ptr.read::
() == Address::from(b256::max())); - assert(realloc_address_ptr.add::
(1).read::
() == Address::zero()); - - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // Can realloc array - // let array_ptr = alloc::<[u64; 1]>(2); - // array_ptr.write(u64::max()); - // array_ptr.add::(1).write(u64::max()); - // let realloc_array_ptr = realloc::<[u64; 1]>(array_ptr, 2, 3); - // assert(realloc_array_ptr.read::() == u64::max()); - // assert(realloc_array_ptr.add::(1).read::() == u64::max()); - // assert(realloc_array_ptr.add::(2).read::() == u64::zero()); - - // Can alloc multiple - let count = 100; - let recount = 1000; - - let multiple_u64 = alloc::(count); - let mut iter = 0; - while iter < count { - multiple_u64.add::(iter).write(u64::max()); - iter += 1; - } - - let realloc_multiple_u64 = realloc::(multiple_u64, count, recount); - let mut iter2 = 0; - while iter2 < count { - assert(realloc_multiple_u64.add::(iter2).read::() == u64::max()); - iter2 += 1; - } - let mut iter3 = count; - while iter3 < recount { - assert(realloc_multiple_u64.add::(iter3).read::() == 0u64); - iter3 += 1; - } - - // Edge cases - - // Realloc to same size - let same_u64_ptr = alloc::(2); - same_u64_ptr.write(u64::max()); - let same_realloc_u64_ptr = realloc::(same_u64_ptr, 2, 2); - assert(same_realloc_u64_ptr.read::() == u64::max()); - assert(same_realloc_u64_ptr.add::(1).read::() == u64::zero()); - assert(same_realloc_u64_ptr == same_u64_ptr); - - // Realloc to less size - let less_u64_ptr = alloc::(2); - less_u64_ptr.write(u64::max()); - let less_realloc_u64_ptr = realloc::(less_u64_ptr, 2, 1); - assert(less_realloc_u64_ptr.read::() == u64::max()); - assert(less_realloc_u64_ptr.add::(1).read::() == u64::zero()); - assert(less_realloc_u64_ptr == less_u64_ptr); - - // Realloc excludes values when count is less then total allocated - let exclude_u64_ptr = alloc::(2); - exclude_u64_ptr.write(u64::max()); - exclude_u64_ptr.add::(1).write(u64::max()); - let exclude_realloc_u64_ptr = realloc::(exclude_u64_ptr, 1, 2); - assert(exclude_realloc_u64_ptr.read::() == u64::max()); - assert(exclude_realloc_u64_ptr.add::(1).read::() == u64::zero()); -} - -#[test(should_revert)] -fn revert_alloc_realloc_when_realloc_unallocated_memory() { - let u64_ptr = alloc::(1); - u64_ptr.write(u64::max()); - - let _realloc_b256_ptr = realloc::(u64_ptr, 2, 3); -} - -#[test] -fn alloc_alloc_bytes() { - // Alloc zero - let zero_ptr = alloc_bytes(0); - assert(!zero_ptr.is_null()); - - // Can alloc u8 - let u8_ptr = alloc_bytes(1); - assert(u8_ptr.read::() == 0u8); - u8_ptr.write(u8::max()); - assert(u8_ptr.read::() == u8::max()); - - // Can alloc u64 - let u64_ptr = alloc_bytes(__size_of::()); - assert(u64_ptr.read::() == 0u64); - u64_ptr.write(u64::max()); - assert(u64_ptr.read::() == u64::max()); - - // Can alloc b256 - let b256_ptr = alloc_bytes(__size_of::()); - assert(b256_ptr.read::() == b256::zero()); - b256_ptr.write(b256::max()); - assert(b256_ptr.read::() == b256::max()); - - // Can alloc struct - let address_ptr = alloc_bytes(__size_of_val::
(Address::zero())); - assert(address_ptr.read::
() == Address::zero()); - address_ptr.write(Address::from(b256::max())); - assert(address_ptr.read::
() == Address::from(b256::max())); - - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // Can alloc array - // let array_ptr = alloc_bytes(__size_of::<[u64; 1]>() * 2); - // assert(array_ptr.read::() == 0u64); - // assert(array_ptr.add::(1).read::() == 0u64); - // array_ptr.write(u64::max()); - // array_ptr.add::(1).write(u64::max()); - // assert(array_ptr.read::() == u64::max()); - // assert(array_ptr.add::(1).read::() == u64::max()); - - // Can alloc multiple - let count = 1000; - let multiple_u64 = alloc_bytes(__size_of::() * count); - let mut iter = 0; - while iter < count { - assert(multiple_u64.add::(iter).read::() == 0u64); - iter += 1; - } -} - -#[test(should_revert)] -fn revert_alloc_alloc_bytes_does_not_exceed_bounds() { - let u64_ptr = alloc_bytes(__size_of::()); - assert(u64_ptr.read::() == 0u64); - - let _out_of_bounds = u64_ptr.add::(1).read::(); -} - -#[test] -fn alloc_realloc_bytes() { - // realloc from zero - let zero_ptr = alloc_bytes(0); - let realloc_zero_ptr = realloc_bytes(zero_ptr, 0, 1); - assert(realloc_zero_ptr.read::() == u8::zero()); - - // Can realloc u8 - let u8_ptr = alloc_bytes(1); - u8_ptr.write(u8::max()); - let realloc_u8_ptr = realloc_bytes(u8_ptr, 1, 2); - assert(realloc_u8_ptr.read::() == u8::max()); - assert(realloc_u8_ptr.add::(1).read::() == u8::zero()); - - // Can alloc u64 - let u64_ptr = alloc_bytes(__size_of::()); - u64_ptr.write(u64::max()); - let realloc_u64_ptr = realloc_bytes(u64_ptr, __size_of::(), __size_of::() * 2); - assert(realloc_u64_ptr.read::() == u64::max()); - assert(realloc_u64_ptr.add::(1).read::() == u64::zero()); - - // Can alloc b256 - let b256_ptr = alloc_bytes(__size_of::()); - b256_ptr.write(b256::max()); - let realloc_b256_ptr = realloc_bytes(b256_ptr, __size_of::(), __size_of::() * 2); - assert(realloc_b256_ptr.read::() == b256::max()); - assert(realloc_b256_ptr.add::(1).read::() == b256::zero()); - - // Can alloc struct - let address_ptr = alloc_bytes(__size_of_val::
(Address::zero())); - address_ptr.write(Address::from(b256::max())); - let realloc_address_ptr = realloc_bytes( - address_ptr, - __size_of_val::
(Address::zero()), - __size_of_val::
(Address::zero()) * 2, - ); - assert(realloc_address_ptr.read::
() == Address::from(b256::max())); - assert(realloc_address_ptr.add::
(1).read::
() == Address::zero()); - - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // Can realloc array - // let array_ptr = alloc_bytes(__size_of::<[u64; 1]>() * 2); - // array_ptr.write(u64::max()); - // array_ptr.add::(1).write(u64::max()); - // let realloc_array_ptr = realloc_bytes(array_ptr, __size_of::<[u64; 1]>() * 2, __size_of::<[u64; 1]>() * 3); - // assert(realloc_array_ptr.read::() == u64::max()); - // assert(realloc_array_ptr.add::(1).read::() == u64::max()); - // assert(realloc_array_ptr.add::(2).read::() == u64::zero()); - - // Can alloc multiple - let count = 100; - let recount = 1000; - - let multiple_u64 = alloc_bytes(__size_of::() * count); - let mut iter = 0; - while iter < count { - multiple_u64.add::(iter).write(u64::max()); - iter += 1; - } - - let realloc_multiple_u64 = realloc_bytes( - multiple_u64, - __size_of::() * count, - __size_of::() * recount, - ); - let mut iter2 = 0; - while iter2 < count { - assert(realloc_multiple_u64.add::(iter2).read::() == u64::max()); - iter2 += 1; - } - let mut iter3 = count; - while iter3 < recount { - assert(realloc_multiple_u64.add::(iter3).read::() == 0u64); - iter3 += 1; - } - - // Edge cases - - // Realloc to same size - let same_u64_ptr = alloc_bytes(__size_of::() * 2); - same_u64_ptr.write(u64::max()); - let same_realloc_u64_ptr = realloc_bytes(same_u64_ptr, __size_of::() * 2, __size_of::() * 2); - assert(same_realloc_u64_ptr.read::() == u64::max()); - assert(same_realloc_u64_ptr.add::(1).read::() == u64::zero()); - assert(same_realloc_u64_ptr == same_u64_ptr); - - // Realloc to less size - let less_u64_ptr = alloc_bytes(__size_of::() * 2); - less_u64_ptr.write(u64::max()); - let less_realloc_u64_ptr = realloc_bytes(less_u64_ptr, __size_of::() * 2, __size_of::() * 1); - assert(less_realloc_u64_ptr.read::() == u64::max()); - assert(less_realloc_u64_ptr.add::(1).read::() == u64::zero()); - assert(less_realloc_u64_ptr == less_u64_ptr); - - // Realloc excludes values when count is less then total allocated - let exclude_u64_ptr = alloc_bytes(__size_of::() * 2); - exclude_u64_ptr.write(u64::max()); - exclude_u64_ptr.add::(1).write(u64::max()); - let exclude_realloc_u64_ptr = realloc_bytes(exclude_u64_ptr, __size_of::(), __size_of::() * 2); - assert(exclude_realloc_u64_ptr.read::() == u64::max()); - assert(exclude_realloc_u64_ptr.add::(1).read::() == u64::zero()); -} - -#[test(should_revert)] -fn revert_alloc_realloc_bytes_when_realloc_unallocated_memory() { - let u64_ptr = alloc_bytes(__size_of::()); - u64_ptr.write(u64::max()); - - let _realloc_b256_ptr = realloc_bytes(u64_ptr, __size_of::() * 2, __size_of::() * 3); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/Forc.toml deleted file mode 100644 index 5ef3222bb22..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "array_conversions_b256_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/src/main.sw deleted file mode 100644 index 31813be5546..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_b256_inline_tests/src/main.sw +++ /dev/null @@ -1,55 +0,0 @@ -library; - -use std::array_conversions::b256::*; - -#[test] -fn b256_from_le_bytes() { - let bytes = [ - 32_u8, 31_u8, 30_u8, 29_u8, 28_u8, 27_u8, 26_u8, 25_u8, 24_u8, 23_u8, 22_u8, - 21_u8, 20_u8, 19_u8, 18_u8, 17_u8, 16_u8, 15_u8, 14_u8, 13_u8, 12_u8, 11_u8, - 10_u8, 9_u8, 8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8, 2_u8, 1_u8, - ]; - - let x = b256::from_le_bytes(bytes); - - assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20); -} - -#[test] -fn b256_to_le_bytes() { - let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; - - let bytes = x.to_le_bytes(); - - let mut i: u8 = 0; - while i < 32_u8 { - assert(bytes[i.as_u64()] == 32_u8 - i); - i += 1_u8; - } -} - -#[test] -fn b256_from_be_bytes() { - let bytes = [ - 1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8, 9_u8, 10_u8, 11_u8, 12_u8, - 13_u8, 14_u8, 15_u8, 16_u8, 17_u8, 18_u8, 19_u8, 20_u8, 21_u8, 22_u8, 23_u8, - 24_u8, 25_u8, 26_u8, 27_u8, 28_u8, 29_u8, 30_u8, 31_u8, 32_u8, - ]; - - let x = b256::from_be_bytes(bytes); - - assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20); -} - -#[test] -fn b256_to_be_bytes() { - let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; - - let bytes = x.to_be_bytes(); - - let mut i: u8 = 0; - while i < 32_u8 { - assert(bytes[i.as_u64()] == i + 1_u8); - i += 1_u8; - } -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/Forc.toml deleted file mode 100644 index 97f65521625..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "array_conversions_u16_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/src/main.sw deleted file mode 100644 index 89ececca4b0..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u16_inline_tests/src/main.sw +++ /dev/null @@ -1,37 +0,0 @@ -library; - -use std::array_conversions::u16::*; - -#[test] -fn u16_to_le_bytes() { - let x: u16 = 513; - let result = x.to_le_bytes(); - - assert(result[0] == 1_u8); - assert(result[1] == 2_u8); -} - -#[test] -fn u16_from_le_bytes() { - let bytes = [1_u8, 2_u8]; - let result = u16::from_le_bytes(bytes); - - assert(result == 513_u16); -} - -#[test] -fn u16_to_be_bytes() { - let x: u16 = 513; - let result = x.to_be_bytes(); - - assert(result[0] == 2_u8); - assert(result[1] == 1_u8); -} - -#[test] -fn u16_from_be_bytes() { - let bytes = [2_u8, 1_u8]; - let result = u16::from_be_bytes(bytes); - - assert(result == 513_u16); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/Forc.toml deleted file mode 100644 index 2632e8c5fdc..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "array_conversions_u256_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/src/main.sw deleted file mode 100644 index c30248efbf5..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u256_inline_tests/src/main.sw +++ /dev/null @@ -1,55 +0,0 @@ -library; - -use std::array_conversions::u256::*; - -#[test] -fn u256_from_le_bytes() { - let bytes = [ - 32_u8, 31_u8, 30_u8, 29_u8, 28_u8, 27_u8, 26_u8, 25_u8, 24_u8, 23_u8, 22_u8, - 21_u8, 20_u8, 19_u8, 18_u8, 17_u8, 16_u8, 15_u8, 14_u8, 13_u8, 12_u8, 11_u8, - 10_u8, 9_u8, 8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8, 2_u8, 1_u8, - ]; - - let x = u256::from_le_bytes(bytes); - - assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256); -} - -#[test] -fn u256_to_le_bytes() { - let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256; - - let bytes = x.to_le_bytes(); - - let mut i: u8 = 0; - while i < 32_u8 { - assert(bytes[i.as_u64()] == 32_u8 - i); - i += 1_u8; - } -} - -#[test] -fn u256_from_be_bytes() { - let bytes = [ - 1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8, 9_u8, 10_u8, 11_u8, 12_u8, - 13_u8, 14_u8, 15_u8, 16_u8, 17_u8, 18_u8, 19_u8, 20_u8, 21_u8, 22_u8, 23_u8, - 24_u8, 25_u8, 26_u8, 27_u8, 28_u8, 29_u8, 30_u8, 31_u8, 32_u8, - ]; - - let x = u256::from_be_bytes(bytes); - - assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256); -} - -#[test] -fn u256_to_be_bytes() { - let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256; - - let bytes = x.to_be_bytes(); - - let mut i: u8 = 0; - while i < 32_u8 { - assert(bytes[i.as_u64()] == i + 1_u8); - i += 1_u8; - } -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/Forc.toml deleted file mode 100644 index c695b8533fe..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "array_conversions_u32_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/src/main.sw deleted file mode 100644 index 2fa1edd90f6..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u32_inline_tests/src/main.sw +++ /dev/null @@ -1,41 +0,0 @@ -library; - -use std::array_conversions::u32::*; - -#[test] -fn u32_to_le_bytes() { - let x: u32 = 67305985; - let result = x.to_le_bytes(); - - assert(result[0] == 1_u8); - assert(result[1] == 2_u8); - assert(result[2] == 3_u8); - assert(result[3] == 4_u8); -} - -#[test] -fn u32_from_le_bytes() { - let bytes = [1_u8, 2_u8, 3_u8, 4_u8]; - let result = u32::from_le_bytes(bytes); - - assert(result == 67305985_u32); -} - -#[test] -fn u32_to_be_bytes() { - let x: u32 = 67305985; - let result = x.to_be_bytes(); - - assert(result[0] == 4_u8); - assert(result[1] == 3_u8); - assert(result[2] == 2_u8); - assert(result[3] == 1_u8); -} - -#[test] -fn u32_from_be_bytes() { - let bytes = [4_u8, 3_u8, 2_u8, 1_u8]; - let result = u32::from_be_bytes(bytes); - - assert(result == 67305985_u32); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/Forc.toml deleted file mode 100644 index 556899c47c4..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "array_conversions_u64_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/src/main.sw deleted file mode 100644 index bafa36017ab..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/array_conversions_u64_inline_tests/src/main.sw +++ /dev/null @@ -1,49 +0,0 @@ -library; - -use std::array_conversions::u64::*; - -#[test] -fn u64_to_le_bytes() { - let x: u64 = 578437695752307201; - let result = x.to_le_bytes(); - - assert(result[0] == 1_u8); - assert(result[1] == 2_u8); - assert(result[2] == 3_u8); - assert(result[3] == 4_u8); - assert(result[4] == 5_u8); - assert(result[5] == 6_u8); - assert(result[6] == 7_u8); - assert(result[7] == 8_u8); -} - -#[test] -fn u64_from_le_bytes() { - let bytes = [1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8]; - let result = u64::from_le_bytes(bytes); - - assert(result == 578437695752307201); -} - -#[test] -fn u64_to_be_bytes() { - let x: u64 = 578437695752307201; - let result = x.to_be_bytes(); - - assert(result[0] == 8_u8); - assert(result[1] == 7_u8); - assert(result[2] == 6_u8); - assert(result[3] == 5_u8); - assert(result[4] == 4_u8); - assert(result[5] == 3_u8); - assert(result[6] == 2_u8); - assert(result[7] == 1_u8); -} - -#[test] -fn u64_from_be_bytes() { - let bytes = [8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8, 2_u8, 1_u8]; - let result = u64::from_be_bytes(bytes); - - assert(result == 578437695752307201); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/Forc.toml deleted file mode 100644 index 71bac6ffeaf..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "assert_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/src/main.sw deleted file mode 100644 index 916bd7fea4a..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/assert_inline_tests/src/main.sw +++ /dev/null @@ -1,171 +0,0 @@ -library; - -#[test] -fn assert_assert() { - assert(true); - assert(1 == 1); - assert(1 + 1 == 2); - assert(!false); - assert(true && true); - assert(true || false); - assert(!false && !false); -} - -#[test(should_revert)] -fn revert_assert_assert_when_not_true() { - assert(false); -} - -#[test] -fn assert_assert_eq() { - use std::bytes::Bytes; - - // assert_eq u64 - let a = 42; - let b = 40 + 2; - assert_eq(a, b); - - // assert_eq u32 - let c = 42u32; - let d = 40u32 + 2u32; - assert_eq(c, d); - - // assert_eq u16 - let e = 42u16; - let f = 40u16 + 2u16; - assert_eq(e, f); - - // assert_eq u8 - let g = 42u8; - let h = 40u8 + 2u8; - assert_eq(g, h); - - // assert_eq b256 - let i: b256 = 0b0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000010; - let j: b256 = 0b1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000001 << 1; - assert_eq(i, j); - - // assert_eq u256 - let k: u256 = 0x02u256; - let l: u256 = 0x01u256 + 0x01u256; - assert_eq(k, l); - - // assert_eq struct - let value = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF; - let m = Address::from(value); - let n = Address::from(value); - assert_eq(m, n); - - // assert_eq heap - let mut o = Bytes::new(); - let mut p = Bytes::new(); - o.push(42u8); - o.push(11u8); - o.push(69u8); - p.push(42u8); - p.push(11u8); - p.push(69u8); - assert_eq(o, p); - - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved. - // assert_eq array - // let value = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF; - // let mut q: [u64; 3] = [0, 0, 0]; - // let mut r: [u64; 3] = [0, 0, 0]; - // q[0] = 1; - // q[1] = 2; - // q[2] = 3; - // r[0] = 1; - // r[1] = 2; - // r[2] = 3; - // assert_eq(q, r); - - // assert_eq enum - let value = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF; - let s = Identity::Address(Address::from(value)); - let t = Identity::Address(Address::from(value)); - let u = Identity::ContractId(ContractId::from(value)); - let v = Identity::ContractId(ContractId::from(value)); - assert_eq(s, t); - assert_eq(u, v); -} - -#[test(should_revert)] -fn revert_assert_assert_eq() { - assert_eq(1, 2); -} - -#[test] -fn assert_assert_ne() { - use std::bytes::Bytes; - - // assert_ne u64 - let a = 42; - let b = 40; - assert_ne(a, b); - - // assert_ne u32 - let c = 42u32; - let d = 40u32; - assert_ne(c, d); - - // assert_ne u16 - let e = 42u16; - let f = 40u16; - assert_ne(e, f); - - // assert_ne u8 - let g = 42u8; - let h = 40u8; - assert_ne(g, h); - - // assert_ne b256 - let i: b256 = 0b0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000001_0000000000000000000000000000000000000000000000000000000000000010; - let j: b256 = 0b1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000000_1000000000000000000000000000000000000000000000000000000000000001; - assert_ne(i, j); - - // assert_ne u256 - let k: u256 = 0x02u256; - let l: u256 = 0x01u256; - assert_ne(k, l); - - // assert_ne struct - let value = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF; - let value2 = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEE; - let m = Address::from(value); - let n = Address::from(value2); - assert_ne(m, n); - - // test_assert_ne heap - let mut o = Bytes::new(); - let mut p = Bytes::new(); - o.push(42u8); - o.push(11u8); - o.push(69u8); - p.push(42u8); - p.push(11u8); - p.push(70u8); - assert_ne(o, p); - - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved. - // assert_ne array - // let mut q: [u64; 3] = [1, 2, 3]; - // let mut r: [u64; 3] = [0, 0, 0]; - // assert_eq(q, r); - - // assert_ne enum - let value = 0xBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF; - let s = Identity::Address(Address::from(value)); - let t = Identity::Address(Address::from(value)); - let u = Identity::ContractId(ContractId::from(value)); - let v = Identity::ContractId(ContractId::from(value)); - assert_ne(u, t); - assert_ne(s, v); - assert_ne(s, u); - assert_ne(v, t); -} - -#[test(should_revert)] -fn revert_assert_assert_ne() { - assert_ne(1, 1); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/Forc.toml deleted file mode 100644 index 646e9e12e7f..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "asset_id_contract_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/src/main.sw deleted file mode 100644 index 16e895c99c9..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_contract_tests/src/main.sw +++ /dev/null @@ -1,22 +0,0 @@ -contract; - -abi AssetIdTestContract { - fn default_asset_id() -> AssetId; -} - -impl AssetIdTestContract for Contract { - fn default_asset_id() -> AssetId { - AssetId::default() - } -} - -#[test] -fn asset_id_default() { - let contract_id = ContractId::from(CONTRACT_ID); - let asset_id_test_abi = abi(AssetIdTestContract, contract_id.bits()); - - let result_asset_id = asset_id_test_abi.default_asset_id(); - let computed_asset_id = AssetId::new(contract_id, SubId::zero()); - - assert(result_asset_id == computed_asset_id); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/Forc.toml deleted file mode 100644 index 6f1fff646d1..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "asset_id_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/src/main.sw deleted file mode 100644 index 057bf1fe5a5..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/asset_id_inline_tests/src/main.sw +++ /dev/null @@ -1,252 +0,0 @@ -library; - -#[test] -fn asset_id_hasher() { - use std::hash::{Hash, sha256}; - - let asset_1 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let digest_1 = sha256(asset_1); - assert(digest_1 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let digest_2 = sha256(asset_2); - assert(digest_2 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); - - let asset_3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let digest3 = sha256(asset_3); - assert(digest3 == 0xaf9613760f72635fbdb44a5a0a63c39f12af30f950a6ee5c971be188e89c4051); -} - -#[test] -fn asset_id_eq() { - let asset_1 = AssetId::zero(); - let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let asset_3 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let asset_4 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let asset_5 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let asset_6 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - - assert(asset_1 == asset_2); - assert(asset_3 == asset_4); - assert(asset_5 == asset_6); -} - -#[test] -fn asset_id_ne() { - let asset_1 = AssetId::zero(); - let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let asset_3 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let asset_4 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let asset_5 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let asset_6 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - - assert(asset_1 != asset_3); - assert(asset_1 != asset_4); - assert(asset_1 != asset_5); - assert(asset_1 != asset_6); - assert(asset_2 != asset_3); - assert(asset_2 != asset_4); - assert(asset_2 != asset_5); - assert(asset_2 != asset_6); - assert(asset_3 != asset_5); - assert(asset_3 != asset_6); -} - -#[test] -fn asset_id_from_b256() { - let asset1 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - assert( - asset1 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let asset2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert( - asset2 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - - let asset3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - assert( - asset3 - .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); -} - -#[test] -fn asset_id_b256_into() { - let b256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000; - let asset_1: AssetId = b256_1.into(); - assert( - asset_1 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let b256_2 = 0x0000000000000000000000000000000000000000000000000000000000000001; - let asset_2: AssetId = b256_2.into(); - assert( - asset_2 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - - let b256_3 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; - let asset_3: AssetId = b256_3.into(); - assert( - asset_3 - .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); -} - -#[test] -fn asset_id_into_b256() { - let asset_1 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let b256_1: b256 = asset_1.into(); - assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - - let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_2: b256 = asset_2.into(); - assert(b256_2 == 0x0000000000000000000000000000000000000000000000000000000000000001); - - let asset_3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let b256_3: b256 = asset_3.into(); - assert(b256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); -} - -#[test] -fn asset_id_b256_from() { - let asset_1 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let b256_1: b256 = b256::from(asset_1); - assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - - let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_2: b256 = b256::from(asset_2); - assert(b256_2 == 0x0000000000000000000000000000000000000000000000000000000000000001); - - let asset_3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let b256_3: b256 = b256::from(asset_3); - assert(b256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); -} - -#[test] -fn asset_id_new() { - let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let sub_id_1 = SubId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let asset_1 = AssetId::new(contract_id_1, sub_id_1); - assert( - asset_1 - .bits() == 0xf5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b, - ); - - let contract_id_2 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let sub_id_2 = SubId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let asset_2 = AssetId::new(contract_id_2, sub_id_2); - assert( - asset_2 - .bits() == 0x58e8f2a1f78f0a591feb75aebecaaa81076e4290894b1c445cc32953604db089, - ); - - let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let sub_id_3 = SubId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let asset_3 = AssetId::new(contract_id_3, sub_id_3); - assert( - asset_3 - .bits() == 0x90f4b39548df55ad6187a1d20d731ecee78c545b94afd16f42ef7592d99cd365, - ); - - let contract_id_4 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let sub_id_4 = SubId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let asset_4 = AssetId::new(contract_id_4, sub_id_4); - assert( - asset_4 - .bits() == 0xa5de9b714accd8afaaabf1cbd6e1014c9d07ff95c2ae154d91ec68485b31e7b5, - ); - - let contract_id_5 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let sub_id_5 = SubId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let asset_5 = AssetId::new(contract_id_5, sub_id_5); - assert( - asset_5 - .bits() == 0xbba91ca85dc914b2ec3efb9e16e7267bf9193b14350d20fba8a8b406730ae30a, - ); - - let contract_id_6 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let sub_id_6 = SubId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let asset_6 = AssetId::new(contract_id_6, sub_id_6); - assert( - asset_6 - .bits() == 0x8667e718294e9e0df1d30600ba3eeb201f764aad2dad72748643e4a285e1d1f7, - ); - - let contract_id_7 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let sub_id_7 = SubId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let asset_7 = AssetId::new(contract_id_7, sub_id_7); - assert( - asset_7 - .bits() == 0x4ab9077c34a6903bc59693414a4fe8ccf275d93f2daacd849574933737c27757, - ); - - let contract_id_8 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let sub_id_8 = SubId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let asset_8 = AssetId::new(contract_id_8, sub_id_8); - assert( - asset_8 - .bits() == 0x2a9bb11102924faefcdbd39baa7858c5f5e49ed2a4205f6759c4a8648bee2942, - ); -} - -#[test] -fn asset_id_default_not_in_contract() { - // Because this is not within a contract context, this will return erroneous data - let _asset = AssetId::default(); -} - -#[test] -fn asset_id_base() { - let base_asset = AssetId::base(); - assert( - base_asset - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); -} - -#[test] -fn asset_id_bits() { - let asset1 = AssetId::zero(); - assert( - asset1 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let asset2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert( - asset2 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - - let asset3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - assert( - asset3 - .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); -} - -#[test] -fn asset_id_zero() { - let zero_asset = AssetId::zero(); - assert( - zero_asset - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); -} - -#[test] -fn asset_id_is_zero() { - let zero_asset = AssetId::zero(); - assert(zero_asset.is_zero()); - - let asset_2 = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert(!asset_2.is_zero()); - - let asset_3 = AssetId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - assert(!asset_3.is_zero()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/Forc.toml deleted file mode 100644 index 3eb4a563a83..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "b512_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/src/main.sw deleted file mode 100644 index 8c5ef0c4381..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/b512_inline_tests/src/main.sw +++ /dev/null @@ -1,284 +0,0 @@ -library; - -use std::b512::B512; - -#[test] -fn b512_eq() { - let b512_1 = B512::zero(); - let b512_2 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - )); - let b512_3 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - )); - let b512_4 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - )); - let b512_5 = B512::from(( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - )); - let b512_6 = B512::from(( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - )); - - assert(b512_1 == b512_2); - assert(b512_3 == b512_4); - assert(b512_5 == b512_6); -} - -#[test] -fn b512_ne() { - let b512_1 = B512::zero(); - let b512_2 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - )); - let b512_3 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - )); - let b512_4 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - )); - let b512_5 = B512::from(( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - )); - let b512_6 = B512::from(( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - )); - - assert(b512_1 != b512_3); - assert(b512_1 != b512_4); - assert(b512_1 != b512_5); - assert(b512_1 != b512_6); - assert(b512_2 != b512_3); - assert(b512_2 != b512_4); - assert(b512_2 != b512_5); - assert(b512_2 != b512_6); - assert(b512_3 != b512_5); - assert(b512_3 != b512_6); -} - -#[test] -fn b512_from_b256() { - let b256_tuple_1: (b256, b256) = ( - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - let b512_1 = B512::from(b256_tuple_1); - assert( - b512_1 - .bits()[0] == 0x0000000000000000000000000000000000000000000000000000000000000000 && b512_1 - .bits()[1] == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let b256_tuple_2: (b256, b256) = ( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - let b512_2 = B512::from(b256_tuple_2); - assert( - b512_2 - .bits()[0] == 0x0000000000000000000000000000000000000000000000000000000000000001 && b512_2 - .bits()[1] == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - - let b256_tuple_3: (b256, b256) = ( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - let b512_3 = B512::from(b256_tuple_3); - assert( - b512_3 - .bits()[0] == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && b512_3 - .bits()[1] == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); -} - -#[test] -fn b512_into_b256() { - let b256_tuple_1: (b256, b256) = ( - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - let b512_1: B512 = b256_tuple_1.into(); - assert( - b512_1 - .bits()[0] == 0x0000000000000000000000000000000000000000000000000000000000000000 && b512_1 - .bits()[1] == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let b256_tuple_2: (b256, b256) = ( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - let b512_2: B512 = b256_tuple_2.into(); - assert( - b512_2 - .bits()[0] == 0x0000000000000000000000000000000000000000000000000000000000000001 && b512_2 - .bits()[1] == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - - let b256_tuple_3: (b256, b256) = ( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - let b512_3: B512 = b256_tuple_3.into(); - assert( - b512_3 - .bits()[0] == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && b512_3 - .bits()[1] == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); -} - -#[test] -fn b512_b256_from() { - // Glob operator needed for From for (b256, b256) - use std::b512::*; - - let b512_1 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - )); - let b256_tuple_1: (b256, b256) = b512_1.into(); - assert( - b256_tuple_1.0 == 0x0000000000000000000000000000000000000000000000000000000000000000 && b256_tuple_1.1 == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let b512_2 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - )); - let b256_tuple_2: (b256, b256) = b512_2.into(); - assert( - b256_tuple_2.0 == 0x0000000000000000000000000000000000000000000000000000000000000001 && b256_tuple_2.1 == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - - let b512_3 = B512::from(( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - )); - let b256_tuple_3: (b256, b256) = b512_3.into(); - assert( - b256_tuple_3.0 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && b256_tuple_3.1 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); -} - -#[test] -fn b512_b256_into() { - // Glob operator needed for From for (b256, b256) - use std::b512::*; - - let b512_1 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - )); - let b256_tuple_1: (b256, b256) = <(b256, b256) as From>::from(b512_1); - assert( - b256_tuple_1.0 == 0x0000000000000000000000000000000000000000000000000000000000000000 && b256_tuple_1.1 == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let b512_2 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - )); - let b256_tuple_2: (b256, b256) = <(b256, b256) as From>::from(b512_2); - assert( - b256_tuple_2.0 == 0x0000000000000000000000000000000000000000000000000000000000000001 && b256_tuple_2.1 == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - - let b512_3 = B512::from(( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - )); - let b256_tuple_3: (b256, b256) = <(b256, b256) as From>::from(b512_3); - assert( - b256_tuple_3.0 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && b256_tuple_3.1 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); -} - -#[test] -fn b512_new() { - let new_b512 = B512::new(); - assert(new_b512.bits()[0] == b256::zero() && new_b512.bits()[1] == b256::zero()); -} - -#[test] -fn b512_bits() { - let b256_tuple_1: (b256, b256) = ( - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - let b256_array_1: [b256; 2] = [ - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - ]; - let b512_1 = B512::from(b256_tuple_1); - assert(b512_1.bits()[0] == b256_array_1[0] && b512_1.bits()[1] == b256_array_1[1]); - - let b256_tuple_2: (b256, b256) = ( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - let b256_array_2: [b256; 2] = [ - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - ]; - let b512_2 = B512::from(b256_tuple_2); - assert(b512_2.bits()[0] == b256_array_2[0] && b512_2.bits()[1] == b256_array_2[1]); - - let b256_tuple_3: (b256, b256) = ( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - let b256_array_3: [b256; 2] = [ - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ]; - let b512_3 = B512::from(b256_tuple_3); - assert(b512_3.bits()[0] == b256_array_3[0] && b512_3.bits()[1] == b256_array_3[1]); -} - -#[test] -fn b512_zero() { - let zero_b512 = B512::zero(); - assert(zero_b512.bits()[0] == b256::zero() && zero_b512.bits()[1] == b256::zero()); -} - -#[test] -fn b512_is_zero() { - let zero_b512 = B512::zero(); - assert(zero_b512.is_zero()); - - let b512_1 = B512::from(( - b256::zero(), - 0x0000000000000000000000000000000000000000000000000000000000000001, - )); - assert(!b512_1.is_zero()); - - let b512_2 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - b256::zero(), - )); - assert(!b512_2.is_zero()); - - let b512_3 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - )); - assert(!b512_3.is_zero()); - - let b512_4 = B512::from(( - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - )); - assert(!b512_4.is_zero()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/Forc.toml deleted file mode 100644 index 4d1f61ac902..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "block_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/src/main.sw deleted file mode 100644 index c89fb10d65a..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/block_inline_tests/src/main.sw +++ /dev/null @@ -1,55 +0,0 @@ -library; - -use std::block::{block_header_hash, BlockHashError, height, timestamp, timestamp_of_block}; - -#[test] -fn block_height() { - let h = height(); - assert(h >= 1u32); -} - -#[test] -fn block_timestamp() { - let time = timestamp(); - assert(time >= 1); -} - -#[test] -fn block_timestamp_of_block() { - let time = timestamp_of_block(height()); - assert(time >= 1); -} - -#[test] -fn block_block_header_hash() { - let result = block_header_hash(height()); - assert(result.is_ok()); - - let hash = result.unwrap(); - assert(hash != b256::zero()); -} - -#[test(should_revert)] -fn revert_block_header_hash_err_current_height() { - // Get the header hash of the current block. Each time this test runs, the block height will be 1. calling BHSH with a height >= current height will fail. - let mut hash = block_header_hash(height()); - let correct_error = match hash { - Ok(_) => false, - Err(BlockHashError::BlockHeightTooHigh) => true, - }; - - assert(correct_error); -} - -#[test(should_revert)] -fn revert_block_header_hash_err_future_height() { - // Try to get header hash of a block in the future - // The function should return a BlockHashError - let hash = block_header_hash(height() + 1u32); - let correct_error = match hash { - Ok(_) => false, - Err(BlockHashError::BlockHeightTooHigh) => true, - }; - - assert(correct_error); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/Forc.toml deleted file mode 100644 index 58a5ac2c835..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "bytes_conversions_b256_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/src/main.sw deleted file mode 100644 index 2d3bc2fcf3b..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/src/main.sw +++ /dev/null @@ -1,58 +0,0 @@ -library; - -use std::{bytes::Bytes, bytes_conversions::b256::*}; - -#[test] -fn b256_from_le_bytes() { - let mut bytes = Bytes::with_capacity(32); - let mut i: u8 = 0; - while i < 32_u8 { - bytes.push(32_u8 - i); - i += 1_u8; - } - - let x = b256::from_le_bytes(bytes); - - assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20); -} - -#[test] -fn b256_to_le_bytes() { - let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; - - let bytes = x.to_le_bytes(); - - let mut i: u8 = 0; - while i < 32_u8 { - assert(bytes.get(i.as_u64()).unwrap() == 32_u8 - i); - i += 1_u8; - } -} - -#[test] -fn b256_from_be_bytes() { - let mut bytes = Bytes::with_capacity(32); - - let mut i: u8 = 0; - while i < 32_u8 { - bytes.push(i + 1_u8); - i += 1_u8; - } - - let x = b256::from_be_bytes(bytes); - - assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20); -} - -#[test] -fn b256_to_be_bytes() { - let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; - - let bytes = x.to_be_bytes(); - - let mut i: u8 = 0; - while i < 32_u8 { - assert(bytes.get(i.as_u64()).unwrap() == i + 1_u8); - i += 1_u8; - } -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/Forc.toml deleted file mode 100644 index 7e52a229ec8..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "bytes_conversions_u16_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/src/main.sw deleted file mode 100644 index 2f0c323c358..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/src/main.sw +++ /dev/null @@ -1,41 +0,0 @@ -library; - -use std::{bytes::Bytes, bytes_conversions::u16::*}; - -#[test] -fn u16_to_le_bytes() { - let x: u16 = 513; - let result = x.to_le_bytes(); - - assert(result.get(0).unwrap() == 1_u8); - assert(result.get(1).unwrap() == 2_u8); -} - -#[test] -fn u16_from_le_bytes() { - let mut bytes = Bytes::new(); - bytes.push(1_u8); - bytes.push(2_u8); - let result = u16::from_le_bytes(bytes); - - assert(result == 513_u16); -} - -#[test] -fn u16_to_be_bytes() { - let x: u16 = 513; - let result = x.to_be_bytes(); - - assert(result.get(0).unwrap() == 2_u8); - assert(result.get(1).unwrap() == 1_u8); -} - -#[test] -fn u16_from_be_bytes() { - let mut bytes = Bytes::new(); - bytes.push(2_u8); - bytes.push(1_u8); - let result = u16::from_be_bytes(bytes); - - assert(result == 513_u16); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/Forc.toml deleted file mode 100644 index ea3ff0fb77f..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "bytes_conversions_u256_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/src/main.sw deleted file mode 100644 index 02f4849056e..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/src/main.sw +++ /dev/null @@ -1,58 +0,0 @@ -library; - -use std::{bytes::Bytes, bytes_conversions::u256::*}; - -#[test] -fn u256_from_le_bytes() { - let mut bytes = Bytes::with_capacity(32); - let mut i: u8 = 0; - while i < 32_u8 { - bytes.push(32_u8 - i); - i += 1_u8; - } - - let x = u256::from_le_bytes(bytes); - - assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256); -} - -#[test] -fn u256_to_le_bytes() { - let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256; - - let bytes = x.to_le_bytes(); - - let mut i: u8 = 0; - while i < 32_u8 { - assert(bytes.get(i.as_u64()).unwrap() == 32_u8 - i); - i += 1_u8; - } -} - -#[test] -fn u256_from_be_bytes() { - let mut bytes = Bytes::with_capacity(32); - - let mut i: u8 = 0; - while i < 32_u8 { - bytes.push(i + 1_u8); - i += 1_u8; - } - - let x = u256::from_be_bytes(bytes); - - assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256); -} - -#[test] -fn u256_to_be_bytes() { - let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256; - - let bytes = x.to_be_bytes(); - - let mut i: u8 = 0; - while i < 32_u8 { - assert(bytes.get(i.as_u64()).unwrap() == i + 1_u8); - i += 1_u8; - } -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/Forc.toml deleted file mode 100644 index 48f9066f8ec..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "bytes_conversions_u32_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/src/main.sw deleted file mode 100644 index f4dcc89cfec..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/src/main.sw +++ /dev/null @@ -1,49 +0,0 @@ -library; - -use std::{bytes::Bytes, bytes_conversions::u32::*}; - -#[test] -fn u32_to_le_bytes() { - let x: u32 = 67305985; - let result = x.to_le_bytes(); - - assert(result.get(0).unwrap() == 1_u8); - assert(result.get(1).unwrap() == 2_u8); - assert(result.get(2).unwrap() == 3_u8); - assert(result.get(3).unwrap() == 4_u8); -} - -#[test] -fn u32_from_le_bytes() { - let mut bytes = Bytes::new(); - bytes.push(1_u8); - bytes.push(2_u8); - bytes.push(3_u8); - bytes.push(4_u8); - let result = u32::from_le_bytes(bytes); - - assert(result == 67305985_u32); -} - -#[test] -fn u32_to_be_bytes() { - let x: u32 = 67305985; - let result = x.to_be_bytes(); - - assert(result.get(0).unwrap() == 4_u8); - assert(result.get(1).unwrap() == 3_u8); - assert(result.get(2).unwrap() == 2_u8); - assert(result.get(3).unwrap() == 1_u8); -} - -#[test] -fn u32_from_be_bytes() { - let mut bytes = Bytes::new(); - bytes.push(4_u8); - bytes.push(3_u8); - bytes.push(2_u8); - bytes.push(1_u8); - let result = u32::from_be_bytes(bytes); - - assert(result == 67305985_u32); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/Forc.toml deleted file mode 100644 index 3f51fa9d573..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "bytes_conversions_u64_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/src/main.sw deleted file mode 100644 index 9781b1ee663..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/src/main.sw +++ /dev/null @@ -1,65 +0,0 @@ -library; - -use std::{bytes::Bytes, bytes_conversions::u64::*}; - -#[test] -fn u64_to_be_bytes() { - let x: u64 = 578437695752307201; - let result = x.to_be_bytes(); - - assert(result.get(0).unwrap() == 8_u8); - assert(result.get(1).unwrap() == 7_u8); - assert(result.get(2).unwrap() == 6_u8); - assert(result.get(3).unwrap() == 5_u8); - assert(result.get(4).unwrap() == 4_u8); - assert(result.get(5).unwrap() == 3_u8); - assert(result.get(6).unwrap() == 2_u8); - assert(result.get(7).unwrap() == 1_u8); -} - -#[test] -fn u64_from_be_bytes() { - let mut bytes = Bytes::new(); - bytes.push(8_u8); - bytes.push(7_u8); - bytes.push(6_u8); - bytes.push(5_u8); - bytes.push(4_u8); - bytes.push(3_u8); - bytes.push(2_u8); - bytes.push(1_u8); - let result = u64::from_be_bytes(bytes); - - assert(result == 578437695752307201); -} - -#[test] -fn u64_to_le_bytes() { - let x: u64 = 578437695752307201; - let result = x.to_le_bytes(); - - assert(result.get(0).unwrap() == 1_u8); - assert(result.get(1).unwrap() == 2_u8); - assert(result.get(2).unwrap() == 3_u8); - assert(result.get(3).unwrap() == 4_u8); - assert(result.get(4).unwrap() == 5_u8); - assert(result.get(5).unwrap() == 6_u8); - assert(result.get(6).unwrap() == 7_u8); - assert(result.get(7).unwrap() == 8_u8); -} - -#[test] -fn u64_from_le_bytes() { - let mut bytes = Bytes::new(); - bytes.push(1_u8); - bytes.push(2_u8); - bytes.push(3_u8); - bytes.push(4_u8); - bytes.push(5_u8); - bytes.push(6_u8); - bytes.push(7_u8); - bytes.push(8_u8); - let result = u64::from_le_bytes(bytes); - - assert(result == 578437695752307201); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/Forc.toml deleted file mode 100644 index ada0818b7ae..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "bytes_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/src/main.sw deleted file mode 100644 index d5107addd94..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/bytes_inline_tests/src/main.sw +++ /dev/null @@ -1,1047 +0,0 @@ -library; - -use std::bytes::Bytes; - -fn setup() -> (Bytes, u8, u8, u8) { - let mut bytes = Bytes::new(); - let a = 5u8; - let b = 7u8; - let c = 9u8; - bytes.push(a); - bytes.push(b); - bytes.push(c); - (bytes, a, b, c) -} - -#[test()] -fn bytes_new() { - let bytes = Bytes::new(); - assert(bytes.len() == 0); - assert(bytes.capacity() == 0); -} - -#[test] -fn bytes_with_capacity() { - let bytes_1 = Bytes::with_capacity(0); - assert(bytes_1.capacity() == 0); - - let bytes_2 = Bytes::with_capacity(1); - assert(bytes_2.capacity() == 1); - - // 2^6 - let bytes_3 = Bytes::with_capacity(64); - assert(bytes_3.capacity() == 64); - - // 2^11 - let bytes_4 = Bytes::with_capacity(2048); - assert(bytes_4.capacity() == 2048); - - // 2^16 - let bytes_5 = Bytes::with_capacity(65536); - assert(bytes_5.capacity() == 65536); -} - -#[test()] -fn bytes_push() { - let mut bytes = Bytes::new(); - - assert(bytes.len() == 0); - assert(bytes.capacity() == 0); - - bytes.push(1u8); - assert(bytes.len() == 1); - assert(bytes.capacity() == 1); - - bytes.push(2u8); - assert(bytes.len() == 2); - assert(bytes.capacity() == 2); - - // Capacity doubles - bytes.push(3u8); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - - bytes.push(4u8); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); - - // Capacity doubles - bytes.push(5u8); - assert(bytes.len() == 5); - assert(bytes.capacity() == 8); - - bytes.push(6u8); - assert(bytes.len() == 6); - assert(bytes.capacity() == 8); - - bytes.push(7u8); - assert(bytes.len() == 7); - assert(bytes.capacity() == 8); - - bytes.push(8u8); - assert(bytes.len() == 8); - assert(bytes.capacity() == 8); - - // Capacity doubles - bytes.push(9u8); - assert(bytes.len() == 9); - assert(bytes.capacity() == 16); -} - -#[test()] -fn bytes_pop() { - let (mut bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - - bytes.push(42u8); - bytes.push(11u8); - bytes.push(69u8); - bytes.push(100u8); - bytes.push(200u8); - bytes.push(255u8); - bytes.push(180u8); - bytes.push(17u8); - bytes.push(19u8); - assert(bytes.len() == 12); - assert(bytes.capacity() == 16); - - let first = bytes.pop(); - assert(first.unwrap() == 19u8); - assert(bytes.len() == 11); - assert(bytes.capacity() == 16); - - let second = bytes.pop(); - assert(second.unwrap() == 17u8); - assert(bytes.len() == 10); - assert(bytes.capacity() == 16); - - let third = bytes.pop(); - assert(third.unwrap() == 180u8); - assert(bytes.len() == 9); - let _ = bytes.pop(); - let _ = bytes.pop(); - let _ = bytes.pop(); - let _ = bytes.pop(); - let _ = bytes.pop(); - let _ = bytes.pop(); - assert(bytes.len() == 3); - assert(bytes.pop().unwrap() == c); - assert(bytes.pop().unwrap() == b); - assert(bytes.pop().unwrap() == a); - - // Can pop all - assert(bytes.len() == 0); - assert(bytes.capacity() == 16); - assert(bytes.pop().is_none()); -} - -#[test()] -fn bytes_get() { - let (bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == c); - // get is non-modifying - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == c); - assert(bytes.len() == 3); - - // None if out of bounds - assert(bytes.get(bytes.len()).is_none()); -} - -#[test()] -fn bytes_set() { - let (mut bytes, a, _b, c) = setup(); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - let d = 11u8; - - // Sets in the middle - bytes.set(1, d); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == d); - assert(bytes.get(2).unwrap() == c); -} - -#[test()] -fn bytes_set_twice() { - let (mut bytes, a, _b, c) = setup(); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - let d = 11u8; - let e = 13u8; - - // Sets in the middle - bytes.set(1, d); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == d); - assert(bytes.get(2).unwrap() == c); - - // Twice - bytes.set(1, e); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == e); - assert(bytes.get(2).unwrap() == c); -} - -#[test()] -fn bytes_set_front() { - let (mut bytes, _a, b, c) = setup(); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - let d = 11u8; - - // Sets at the front - bytes.set(0, d); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == d); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == c); -} - -#[test()] -fn bytes_set_back() { - let (mut bytes, a, b, _c) = setup(); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - let d = 11u8; - - // Sets at the back - bytes.set(bytes.len() - 1, d); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == d); -} - -#[test(should_revert)] -fn revert_bytes_set_out_of_bounds() { - let (mut bytes, _a, _b, _c) = setup(); - - bytes.set(bytes.len(), 11u8); -} - -#[test()] -fn bytes_insert() { - let (mut bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - - let d = 11u8; - - // Inserts in the middle - bytes.insert(1, d); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == d); - assert(bytes.get(2).unwrap() == b); - assert(bytes.get(3).unwrap() == c); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); -} - -#[test()] -fn bytes_insert_twice() { - let (mut bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - - let d = 11u8; - let e = 13u8; - - // Inserts in the middle - bytes.insert(1, d); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == d); - assert(bytes.get(2).unwrap() == b); - assert(bytes.get(3).unwrap() == c); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); - - // Twice - bytes.insert(1, e); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == e); - assert(bytes.get(2).unwrap() == d); - assert(bytes.get(3).unwrap() == b); - assert(bytes.get(4).unwrap() == c); - assert(bytes.len() == 5); - assert(bytes.capacity() == 8); -} - -#[test()] -fn bytes_insert_front() { - let (mut bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - - let d = 11u8; - - // Inserts at the front - bytes.insert(0, d); - assert(bytes.get(0).unwrap() == d); - assert(bytes.get(1).unwrap() == a); - assert(bytes.get(2).unwrap() == b); - assert(bytes.get(3).unwrap() == c); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); -} - -#[test()] -fn bytes_insert_before_back() { - let (mut bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - - let d = 11u8; - - // Inserts right before the back - bytes.insert(bytes.len() - 1, d); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == d); - assert(bytes.get(3).unwrap() == c); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); -} - -#[test()] -fn bytes_insert_back() { - let (mut bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - - let d = 11u8; - - // Inserts at the back - bytes.insert(bytes.len(), d); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == c); - assert(bytes.get(3).unwrap() == d); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); -} - -#[test(should_revert)] -fn revert_bytes_insert_out_of_bounds() { - let (mut bytes, a, _b, _c) = setup(); - - bytes.insert(bytes.len() + 1, a); -} - -#[test()] -fn bytes_remove() { - let (mut bytes, a, b, c) = setup(); - let d = 7u8; - bytes.push(d); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); - - // Remove in the middle - let item1 = bytes.remove(1); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - assert(item1 == b); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == c); - assert(bytes.get(2).unwrap() == d); - assert(bytes.get(3).is_none()); -} - -#[test()] -fn bytes_remove_front() { - let (mut bytes, a, b, c) = setup(); - // Remove at the start - let item = bytes.remove(0); - assert(bytes.len() == 2); - assert(bytes.capacity() == 4); - assert(item == a); - assert(bytes.get(0).unwrap() == b); - assert(bytes.get(1).unwrap() == c); - assert(bytes.get(2).is_none()); -} - -#[test()] -fn bytes_remove_end() { - let (mut bytes, a, b, c) = setup(); - // Remove at the end - let item = bytes.remove(bytes.len() - 1); - assert(bytes.len() == 2); - assert(bytes.capacity() == 4); - assert(item == c); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).is_none()); -} - -#[test()] -fn bytes_remove_all() { - let (mut bytes, a, b, c) = setup(); - // Remove all - let item1 = bytes.remove(0); - let item2 = bytes.remove(0); - let item3 = bytes.remove(0); - assert(bytes.len() == 0); - assert(bytes.capacity() == 4); - assert(item1 == a); - assert(item2 == b); - assert(item3 == c); - assert(bytes.get(0).is_none()); -} - -#[test(should_revert)] -fn revert_bytes_remove_out_of_bounds() { - let (mut bytes, _a, _b, _c) = setup(); - - let _result = bytes.remove(bytes.len()); -} - -#[test()] -fn bytes_swap() { - let (mut bytes, a, b, c) = setup(); - let d = 5u8; - bytes.push(d); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); - - // Swaps Middle - bytes.swap(1, 2); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == c); - assert(bytes.get(2).unwrap() == b); - assert(bytes.get(3).unwrap() == d); -} - -#[test()] -fn bytes_swap_twice() { - let (mut bytes, a, b, c) = setup(); - let d = 5u8; - bytes.push(d); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); - - // Swaps Middle - bytes.swap(1, 2); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == c); - assert(bytes.get(2).unwrap() == b); - assert(bytes.get(3).unwrap() == d); - - bytes.swap(1, 2); - assert(bytes.len() == 4); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == c); - assert(bytes.get(3).unwrap() == d); -} - -#[test()] -fn bytes_swap_front() { - let (mut bytes, a, b, c) = setup(); - - // Swaps Front - bytes.swap(0, 1); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == b); - assert(bytes.get(1).unwrap() == a); - assert(bytes.get(2).unwrap() == c); -} - -#[test()] -fn bytes_swap_end() { - let (mut bytes, a, b, c) = setup(); - - // Swaps back - bytes.swap(2, 1); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == c); - assert(bytes.get(2).unwrap() == b); -} - -#[test()] -fn bytes_swap_front_with_end() { - let (mut bytes, a, b, c) = setup(); - - // Swaps front with back - bytes.swap(0, 2); - assert(bytes.len() == 3); - assert(bytes.capacity() == 4); - assert(bytes.get(0).unwrap() == c); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == a); -} - -#[test(should_revert)] -fn revert_bytes_swap_element_1_out_of_bounds() { - let (mut bytes, _a, _b, _c) = setup(); - - bytes.swap(bytes.len(), 0); -} - -#[test(should_revert)] -fn revert_bytes_swap_element_2_out_of_bounds() { - let (mut bytes, _a, _b, _c) = setup(); - - bytes.swap(0, bytes.len()); -} - -#[test()] -fn bytes_capacity() { - let mut bytes = Bytes::new(); - assert(bytes.capacity() == 0); - - bytes.push(5u8); - assert(bytes.capacity() == 1); - bytes.push(7u8); - assert(bytes.capacity() == 2); - bytes.push(9u8); - assert(bytes.capacity() == 4); - bytes.push(11u8); - assert(bytes.capacity() == 4); - bytes.push(3u8); - assert(bytes.capacity() == 8); -} - -#[test()] -fn bytes_len() { - let (mut bytes, _, _, _) = setup(); - assert(bytes.len() == 3); - - bytes.push(5u8); - assert(bytes.len() == 4); - bytes.push(6u8); - assert(bytes.len() == 5); - bytes.push(7u8); - assert(bytes.len() == 6); - bytes.push(8u8); - assert(bytes.len() == 7); -} - -#[test()] -fn bytes_clear() { - let (mut bytes, _, _, _) = setup(); - assert(bytes.len() == 3); - - bytes.clear(); - assert(bytes.len() == 0); - assert(bytes.capacity() == 0); -} - -#[test()] -fn bytes_clear_twice() { - let (mut bytes, _, _, _) = setup(); - - bytes.clear(); - assert(bytes.len() == 0); - assert(bytes.capacity() == 0); - - // Can clean twice - bytes.push(1u8); - bytes.clear(); - assert(bytes.len() == 0); - assert(bytes.capacity() == 0); -} - -#[test()] -fn bytes_clear_empty_bytes() { - // Clear on empty Bytes - let mut empty_bytes = Bytes::new(); - assert(empty_bytes.len() == 0); - assert(empty_bytes.capacity() == 0); - - empty_bytes.clear(); - assert(empty_bytes.len() == 0); - assert(empty_bytes.capacity() == 0); -} - -#[test] -fn bytes_is_empty() { - let (mut setup_bytes, _, _, _) = setup(); - - assert(!setup_bytes.is_empty()); - - let mut new_bytes = Bytes::new(); - assert(new_bytes.is_empty()); - - let mut capacity_bytes = Bytes::with_capacity(16); - assert(capacity_bytes.is_empty()); -} - -#[test] -fn bytes_ptr() { - let (mut setup_bytes, a, _, _) = setup(); - - let setup_bytes_ptr = setup_bytes.ptr(); - assert(!setup_bytes_ptr.is_null()); - assert(setup_bytes_ptr.read::() == a); - - let mut new_bytes = Bytes::new(); - let new_bytes_ptr = new_bytes.ptr(); - assert(!new_bytes_ptr.is_null()); - - let mut capacity_bytes = Bytes::with_capacity(16); - let capacity_bytes_ptr = capacity_bytes.ptr(); - assert(!capacity_bytes_ptr.is_null()); -} - -#[test()] -fn bytes_split_at() { - let (mut bytes_1, a, b, c) = setup(); - let d = 7u8; - bytes_1.push(d); - assert(bytes_1.len() == 4); - - let index = 2; - let (bytes_1_left, bytes_1_right) = bytes_1.split_at(index); - assert(bytes_1.capacity() == 4); - assert(bytes_1_right.capacity() == 2); - assert(bytes_1_left.capacity() == 2); - assert(bytes_1_left.len() == 2); - assert(bytes_1_right.len() == 2); - assert(bytes_1_left.get(0).unwrap() == a); - assert(bytes_1_left.get(1).unwrap() == b); - assert(bytes_1_right.get(0).unwrap() == c); - assert(bytes_1_right.get(1).unwrap() == d); -} - -#[test()] -fn bytes_split_at_twice() { - let (mut bytes, a, b, _c) = setup(); - let d = 7u8; - bytes.push(d); - assert(bytes.len() == 4); - - let index = 2; - let (bytes_left, _bytes_right) = bytes.split_at(index); - - // Split twice - let index_2 = 1; - let (left_left, left_right) = bytes_left.split_at(index_2); - assert(bytes_left.capacity() == 2); - assert(left_left.capacity() == 1); - assert(left_right.capacity() == 1); - assert(left_left.len() == 1); - assert(left_right.len() == 1); - assert(left_left.get(0).unwrap() == a); - assert(left_right.get(0).unwrap() == b); -} - -#[test()] -fn bytes_split_at_end() { - // // Split at end - let (mut bytes, a, b, c) = setup(); - - let index = bytes.len(); - let (bytes_left, bytes_right) = bytes.split_at(index); - assert(bytes.capacity() == 4); - assert(bytes_left.capacity() == 3); - assert(bytes_right.capacity() == 0); - assert(bytes_left.len() == 3); - assert(bytes_right.len() == 0); - assert(bytes_left.get(0).unwrap() == a); - assert(bytes_left.get(1).unwrap() == b); - assert(bytes_left.get(2).unwrap() == c); - assert(bytes_right.get(0).is_none()); -} - -#[test()] -fn bytes_split_at_front() { - // Split at front - let (mut bytes, a, b, c) = setup(); - - let index = 0; - let (bytes_left, bytes_right) = bytes.split_at(index); - assert(bytes.capacity() == 4); - assert(bytes_left.capacity() == 0); - assert(bytes_right.capacity() == 3); - assert(bytes_left.len() == 0); - assert(bytes_right.len() == 3); - assert(bytes_right.get(0).unwrap() == a); - assert(bytes_right.get(1).unwrap() == b); - assert(bytes_right.get(2).unwrap() == c); - assert(bytes_left.get(0).is_none()); -} - -#[test(should_revert)] -fn revert_bytes_split_at_out_of_bounds() { - let (mut bytes, _a, _b, _c) = setup(); - - let (_bytes_left, _bytes_right) = bytes.split_at(bytes.len() + 1); -} - -#[test()] -fn bytes_append() { - let (mut bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == c); - - let mut bytes2 = Bytes::new(); - let d = 5u8; - let e = 7u8; - let f = 9u8; - bytes2.push(d); - bytes2.push(e); - bytes2.push(f); - assert(bytes2.len() == 3); - assert(bytes2.get(0).unwrap() == d); - assert(bytes2.get(1).unwrap() == e); - assert(bytes2.get(2).unwrap() == f); - - let first_length = bytes.len(); - let second_length = bytes2.len(); - bytes.append(bytes2); - assert(bytes.len() == first_length + second_length); - assert(bytes.capacity() == first_length + first_length); - assert(bytes2.is_empty()); - let values = [a, b, c, d, e, f]; - let mut i = 0; - while i < 6 { - assert(bytes.get(i).unwrap() == values[i]); - i += 1; - }; -} - -#[test()] -fn bytes_append_empty() { - // Append empty bytes - let (mut bytes, a, b, c) = setup(); - let bytes_length = bytes.len(); - let bytes_original_capacity = bytes.capacity(); - - let mut empty_bytes = Bytes::new(); - bytes.append(empty_bytes); - - // Because empty bytes were appended, no changes to length and capacity were made - assert(bytes.len() == bytes_length); - assert(bytes.capacity() == bytes_original_capacity); - assert(empty_bytes.is_empty()); - - let values = [a, b, c]; - let mut i = 0; - while i < 3 { - assert(bytes.get(i).unwrap() == values[i]); - i += 1; - }; -} - -#[test()] -fn bytes_append_to_empty() { - // Append to empty bytes - let (mut bytes, a, b, c) = setup(); - let bytes_length = bytes.len(); - let bytes_original_capacity = bytes.capacity(); - - // Because empty bytes were appended, no changes to capacity were made - let mut empty_bytes = Bytes::new(); - empty_bytes.append(bytes); - assert(empty_bytes.len() == bytes_length); - assert(empty_bytes.capacity() == bytes_original_capacity); - assert(bytes.is_empty()); - - let values = [a, b, c]; - let mut i = 0; - while i < 3 { - assert(empty_bytes.get(i).unwrap() == values[i]); - i += 1; - }; -} - -#[test()] -fn bytes_eq() { - let (mut bytes, _a, _b, _c) = setup(); - - let d = 5u8; - let e = 7u8; - let f = 9u8; - let mut other = Bytes::new(); - other.push(d); - other.push(e); - other.push(f); - assert(bytes == other); - - other.push(42u8); - bytes.push(42u8); - assert(bytes == other); -} - -#[test()] -fn bytes_ne() { - let (mut bytes, _a, _b, _c) = setup(); - - let d = 5u8; - let e = 7u8; - let f = 9u8; - let mut other = Bytes::new(); - other.push(d); - other.push(e); - other.push(f); - - other.push(42u8); - assert(bytes != other); - - other.swap(0, 1); - assert(bytes != other); -} - -#[test()] -fn bytes_as_raw_slice() { - let (mut bytes, _a, _b, _c) = setup(); - - let slice = bytes.as_raw_slice(); - assert(bytes.ptr() == slice.ptr()); - assert(bytes.len() == slice.number_of_bytes()); -} - -#[test] -fn bytes_from_b256() { - let initial = 0x3333333333333333333333333333333333333333333333333333333333333333; - let b: Bytes = Bytes::from(initial); - let mut control_bytes = Bytes::with_capacity(32); - - let mut i = 0; - while i < 32 { - // 0x33 is 51 in decimal - control_bytes.push(51u8); - i += 1; - } - - assert(b == control_bytes); -} - -#[test] -fn bytes_into_b256() { - let mut initial_bytes = Bytes::with_capacity(32); - - let mut i = 0; - while i < 32 { - // 0x33 is 51 in decimal - initial_bytes.push(51u8); - i += 1; - } - - let value: b256 = initial_bytes.into(); - let expected: b256 = 0x3333333333333333333333333333333333333333333333333333333333333333; - - assert(value == expected); -} - -#[test] -fn bytes_b256_from() { - let control = 0x3333333333333333333333333333333333333333333333333333333333333333; - let mut bytes = Bytes::with_capacity(32); - - let mut i = 0; - while i < 32 { - // 0x33 is 51 in decimal - bytes.push(51u8); - i += 1; - } - - let result_b256: b256 = b256::from(bytes); - - assert(result_b256 == control); -} - -#[test] -fn bytes_b256_into() { - let initial = 0x3333333333333333333333333333333333333333333333333333333333333333; - let mut control = Bytes::with_capacity(32); - - let mut i = 0; - while i < 32 { - // 0x33 is 51 in decimal - control.push(51u8); - i += 1; - } - - let result_bytes: Bytes = initial.into(); - - assert(result_bytes == control); -} - -#[test()] -fn bytes_from_raw_slice() { - let val = 0x3497297632836282349729763283628234972976328362823497297632836282; - let slice = asm(ptr: (__addr_of(val), 32)) { - ptr: raw_slice - }; - - let mut bytes = Bytes::from(slice); - assert(bytes.ptr() != slice.ptr()); // Bytes should own its buffer - assert(bytes.len() == slice.number_of_bytes()); -} - -#[test()] -fn bytes_into_raw_slice() { - let (mut bytes, _a, _b, _c) = setup(); - - let slice: raw_slice = bytes.into(); - - assert(bytes.ptr() == slice.ptr()); - assert(bytes.len() == slice.number_of_bytes()); -} - -#[test()] -fn bytes_raw_slice_from() { - let (mut bytes, _a, _b, _c) = setup(); - - let slice: raw_slice = raw_slice::from(bytes); - - assert(bytes.ptr() == slice.ptr()); - assert(bytes.len() == slice.number_of_bytes()); -} - -#[test()] -fn bytes_raw_slice_into() { - let val = 0x3497297632836282349729763283628234972976328362823497297632836282; - let slice = asm(ptr: (__addr_of(val), 32)) { - ptr: raw_slice - }; - - let bytes: Bytes = slice.into(); - - assert(bytes.ptr() != slice.ptr()); // Bytes should own its buffer - assert(bytes.len() == slice.number_of_bytes()); -} - -#[test()] -fn bytes_from_vec_u8() { - let mut vec = Vec::new(); - let (_, a, b, c) = setup(); - vec.push(a); - vec.push(b); - vec.push(c); - - let bytes = Bytes::from(vec); - - assert(bytes.len() == 3); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == c); -} - -#[test()] -fn bytes_into_vec_u8() { - let (mut bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - - let vec: Vec = bytes.into(); - - assert(vec.len() == 3); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).unwrap() == c); -} - -#[test()] -fn bytes_vec_u8_from() { - let (mut bytes, a, b, c) = setup(); - - let mut vec: Vec = Vec::::from(bytes); - - assert(vec.len() == 3); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).unwrap() == c); -} - -#[test()] -fn bytes_vec_u8_into() { - let mut vec = Vec::new(); - let (_, a, b, c) = setup(); - vec.push(a); - vec.push(b); - vec.push(c); - - let bytes: Bytes = vec.into(); - - assert(bytes.len() == 3); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == b); - assert(bytes.get(2).unwrap() == c); -} - -#[test] -fn bytes_clone() { - let (mut bytes, _a, _b, _c) = setup(); - - let cloned_bytes = bytes.clone(); - - assert(cloned_bytes.ptr() != bytes.ptr()); - assert(cloned_bytes.len() == bytes.len()); - // Capacity is not cloned - // assert(cloned_bytes.capacity() == bytes.capacity()); - assert(cloned_bytes.get(0).unwrap() == bytes.get(0).unwrap()); - assert(cloned_bytes.get(1).unwrap() == bytes.get(1).unwrap()); - assert(cloned_bytes.get(2).unwrap() == bytes.get(2).unwrap()); -} - -#[test] -pub fn test_encode_decode() { - let initial = 0x3333333333333333333333333333333333333333333333333333333333333333; - let initial: Bytes = Bytes::from(initial); - let decoded = abi_decode::(encode(initial)); - - assert_eq(decoded, initial); -} - -#[test()] -fn bytes_test_packing() { - let mut bytes = Bytes::new(); - bytes.push(5u8); - bytes.push(5u8); - bytes.push(5u8); - bytes.push(5u8); - bytes.push(5u8); - bytes.push(5u8); - bytes.push(5u8); - bytes.push(5u8); - bytes.push(5u8); - bytes.push(5u8); - bytes.push(5u8); - assert(bytes.len() == 11); - assert(bytes.capacity() == 16); -} - -#[test()] -fn bytes_test_u8_limits() { - let mut bytes = Bytes::new(); - let max = 255u8; - let min = 0u8; - bytes.push(max); - bytes.push(min); - bytes.push(max); - bytes.push(min); - bytes.push(max); - bytes.push(min); - - assert(bytes.len() == 6); - assert(bytes.capacity() == 8); - assert(bytes.get(0).unwrap() == max); - assert(bytes.get(1).unwrap() == min); - assert(bytes.get(2).unwrap() == max); - assert(bytes.get(3).unwrap() == min); - assert(bytes.get(4).unwrap() == max); - assert(bytes.get(5).unwrap() == min); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/Forc.toml deleted file mode 100644 index de4d6a7ee46..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "contract_id_contract_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/src/main.sw deleted file mode 100644 index 4ad5554b355..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_contract_tests/src/main.sw +++ /dev/null @@ -1,20 +0,0 @@ -contract; - -abi ContractIdTest { - fn this_contract_id() -> ContractId; -} - -impl ContractIdTest for Contract { - fn this_contract_id() -> ContractId { - ContractId::this() - } -} - -#[test] -fn contract_id_this() { - let expected_contract_id = ContractId::from(CONTRACT_ID); - let contract_abi = abi(ContractIdTest, expected_contract_id.bits()); - - let result_contract_id = contract_abi.this_contract_id(); - assert(result_contract_id == expected_contract_id); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/Forc.toml deleted file mode 100644 index a17cad1235c..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "contract_id_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/src/main.sw deleted file mode 100644 index cfcc216a8d4..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/contract_id_inline_tests/src/main.sw +++ /dev/null @@ -1,182 +0,0 @@ -library; - -#[test] -fn contract_id_bits() { - let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let bits1 = contract_id_1.bits(); - assert(bits1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - - let contract_id_2 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let bits2 = contract_id_2.bits(); - assert(bits2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - - let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let bits3 = contract_id_3.bits(); - assert(bits3 == 0x0000000000000000000000000000000000000000000000000000000000000001); -} - -#[test] -fn contract_id_eq() { - let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let contract_id_2 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let contract_id_4 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let contract_id_5 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let contract_id_6 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - - assert(contract_id_1 == contract_id_2); - assert(contract_id_3 == contract_id_4); - assert(contract_id_5 == contract_id_6); -} - -#[test] -fn contract_id_ne() { - let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let contract_id_2 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let contract_id_4 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let contract_id_5 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let contract_id_6 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - - assert(contract_id_1 != contract_id_3); - assert(contract_id_1 != contract_id_4); - assert(contract_id_1 != contract_id_5); - assert(contract_id_1 != contract_id_6); - assert(contract_id_2 != contract_id_3); - assert(contract_id_2 != contract_id_4); - assert(contract_id_2 != contract_id_5); - assert(contract_id_2 != contract_id_6); - assert(contract_id_3 != contract_id_5); - assert(contract_id_3 != contract_id_6); - assert(contract_id_4 != contract_id_5); - assert(contract_id_4 != contract_id_6); -} - -#[test] -fn contract_id_from_b256() { - let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - assert( - contract_id_1 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let contract_id_2 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - assert( - contract_id_2 - .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - - let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert( - contract_id_3 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn contract_id_b256_into() { - let b256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000; - let contract_id_1: ContractId = b256_1.into(); - assert( - contract_id_1 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let b256_2 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; - let contract_id_2: ContractId = b256_2.into(); - assert( - contract_id_2 - .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - - let b256_3 = 0x0000000000000000000000000000000000000000000000000000000000000001; - let contract_id_3: ContractId = b256_3.into(); - assert( - contract_id_3 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn contract_id_into_b256() { - let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let b256_data1: b256 = contract_id_1.into(); - assert( - b256_data1 == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let contract_id_2 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let b256_data2: b256 = contract_id_2.into(); - assert( - b256_data2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - - let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_data3: b256 = contract_id_3.into(); - assert( - b256_data3 == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn contract_id_b256_from() { - let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let b256_data1: b256 = b256::from(contract_id_1); - assert( - b256_data1 == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - - let contract_id_2 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let b256_data2: b256 = b256::from(contract_id_2); - assert( - b256_data2 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - - let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_data3: b256 = b256::from(contract_id_3); - assert( - b256_data3 == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn contract_id_hash() { - use std::hash::{Hash, sha256}; - - let contract_id_1 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let digest1 = sha256(contract_id_1); - assert(digest1 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let contract_id_2 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let digest2 = sha256(contract_id_2); - assert(digest2 == 0xaf9613760f72635fbdb44a5a0a63c39f12af30f950a6ee5c971be188e89c4051); - - let contract_id_3 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let digest3 = sha256(contract_id_3); - assert(digest3 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); - - let contract_id_4 = ContractId::from(0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); - let digest4 = sha256(contract_id_4); - assert(digest4 != 0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); -} - -#[test] -fn contract_id_zero() { - let contract_id = ContractId::zero(); - assert( - contract_id - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); -} - -#[test] -fn contract_id_is_zero() { - let zero_contract_id = ContractId::zero(); - assert(zero_contract_id.is_zero()); - - let contract_id_2 = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert(!contract_id_2.is_zero()); - - let contract_id_3 = ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - assert(!contract_id_3.is_zero()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/Forc.toml deleted file mode 100644 index 0a8e7ef6ca2..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "ecr_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/src/main.sw deleted file mode 100644 index e11471b2cbe..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/ecr_inline_tests/src/main.sw +++ /dev/null @@ -1,133 +0,0 @@ -library; - -use std::{ - b512::B512, - ecr::{ - ec_recover, - ec_recover_address, - ec_recover_address_r1, - ec_recover_r1, - ed_verify, - }, - hash::{ - Hash, - sha256, - }, -}; - -#[test] -fn ecr_ec_recover() { - let hi_1 = 0x61f3caf4c0912cec69ff0b226638d397115c623a7f057914d48a7e4daf1cf6d8; - let lo_1 = 0x2555de81cd3a40382d3d64eb1c77e463eea5a76d65ec85f283e0b3d568352678; - let msg_hash_1 = 0xa13f4ab54057ce064d3dd97ac3ff30ed704e73956896c03650fe59b1a561fe15; - let pub_hi = 0x41a55558a3486b6ee3878f55f16879c0798afd772c1506de44aba90d29b6e65c; - let pub_lo = 0x341ca2e0a3d5827e78d838e35b29bebe2a39ac30b58999e1138c9467bf859965; - let signature_1: B512 = B512::from((hi_1, lo_1)); - // A recovered public key pair. - let result_1 = ec_recover(signature_1, msg_hash_1); - - assert(result_1.is_ok()); - assert(result_1.unwrap().bits()[0] == pub_hi); - assert(result_1.unwrap().bits()[1] == pub_lo); - - let hi_2 = b256::zero(); - let lo_2 = 0x2555de81cd3a40382d3d64eb1c77e463eea5a76d65ec85f283e0b3d568352678; - let msg_hash_2 = 0xa13f4ab54057ce064d3dd97ac3ff30ed704e73956896c03650fe59b1a561fe15; - let signature_2: B512 = B512::from((hi_2, lo_2)); - // A recovered public key pair. - let result_2 = ec_recover(signature_2, msg_hash_2); - - assert(result_2.is_err()); -} - -#[test] -fn ecr_ec_recover_r1() { - let hi_1 = 0xbd0c9b8792876712afadbff382e1bf31c44437823ed761cc3600d0016de511ac; - let lo_1 = 0x44ac566bd156b4fc71a4a4cb2655d3da360c695edb27dc3b64d621e122fea23d; - let msg_hash_1 = 0x1e45523606c96c98ba970ff7cf9511fab8b25e1bcd52ced30b81df1e4a9c4323; - let pub_hi_1 = 0xd6ea577a54ae42411fbc78d686d4abba2150ca83540528e4b868002e346004b2; - let pub_lo_1 = 0x62660ecce5979493fe5684526e8e00875b948e507a89a47096bc84064a175452; - let signature_1: B512 = B512::from((hi_1, lo_1)); - // A recovered public key pair. - let result_1 = ec_recover_r1(signature_1, msg_hash_1); - - assert(result_1.is_ok()); - assert(result_1.unwrap().bits()[0] == pub_hi_1); - assert(result_1.unwrap().bits()[1] == pub_lo_1); - - let hi_2 = b256::zero(); - let lo_2 = 0x44ac566bd156b4fc71a4a4cb2655d3da360c695edb27dc3b64d621e122fea23d; - let msg_hash_2 = 0x1e45523606c96c98ba970ff7cf9511fab8b25e1bcd52ced30b81df1e4a9c4323; - let signature_2: B512 = B512::from((hi_2, lo_2)); - let result_2 = ec_recover_r1(signature_2, msg_hash_2); - - assert(result_2.is_err()); -} - -#[test] -fn ecr_ec_recover_address() { - let hi_1 = 0x61f3caf4c0912cec69ff0b226638d397115c623a7f057914d48a7e4daf1cf6d8; - let lo_1 = 0x2555de81cd3a40382d3d64eb1c77e463eea5a76d65ec85f283e0b3d568352678; - let msg_hash_1 = 0xa13f4ab54057ce064d3dd97ac3ff30ed704e73956896c03650fe59b1a561fe15; - let address_1 = Address::from(0x02844f00cce0f608fa3f0f7408bec96bfd757891a6fda6e1fa0f510398304881); - let signature_1: B512 = B512::from((hi_1, lo_1)); - // A recovered Fuel address. - let result_1 = ec_recover_address(signature_1, msg_hash_1); - assert(result_1.is_ok()); - assert(result_1.unwrap() == address_1); - - let hi_2 = b256::zero(); - let lo_2 = 0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d; - let msg_hash_2 = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323; - let signature_2: B512 = B512::from((hi_2, lo_2)); - let result_2 = ec_recover_address(signature_2, msg_hash_2); - - assert(result_2.is_err()); -} - -#[test] -fn ecr_ec_recover_address_r1() { - let hi_1 = 0xbd0c9b8792876713afa8bf3383eebf31c43437823ed761cc3600d0016de5110c; - let lo_1 = 0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d; - let msg_hash_1 = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323; - let address_1 = Address::from(0xb4a5fabee8cc852084b71f17107e9c18d682033a58967027af0ab01edf2f9a6a); - let signature_1: B512 = B512::from((hi_1, lo_1)); - // A recovered Fuel address. - let result_1 = ec_recover_address_r1(signature_1, msg_hash_1); - assert(result_1.is_ok()); - assert(result_1.unwrap() == address_1); - - let hi_2 = b256::zero(); - let lo_2 = 0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d; - let msg_hash_2 = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323; - let signature_2: B512 = B512::from((hi_2, lo_2)); - let result_2 = ec_recover_address_r1(signature_2, msg_hash_2); - - assert(result_2.is_err()); -} - -#[test] -fn ecr_ed_verify() { - let pub_key_1 = 0x314fa58689bbe1da2430517de2d772b384a1c1d2e9cb87e73c6afcf246045b10; - let msg_1 = b256::zero(); - let msg_hash_1 = sha256(msg_1); - - let hi_1 = 0xf38cef9361894be6c6e0eddec28a663d099d7ddff17c8077a1447d7ecb4e6545; - let lo_1 = 0xf5084560039486d3462dd65a40c80a74709b2f06d450ffc5dc00345c6b2cdd00; - let signature_1: B512 = B512::from((hi_1, lo_1)); - // A verified public key with signature - let verified_1 = ed_verify(pub_key_1, signature_1, msg_hash_1); - assert(verified_1.is_ok()); - assert(verified_1.unwrap()); - - let pub_key_2 = 0x314fa58689bbe1da2430517de2d772b384a1c1d2e9cb87e73c6afcf246045b10; - let msg_2 = b256::zero(); - let msg_hash_2 = sha256(msg_2); - - let hi_2 = b256::zero(); - let lo_2 = 0xf5084560039486d3462dd65a40c80a74709b2f06d450ffc5dc00345c6b2cdd00; - let signature_2: B512 = B512::from((hi_2, lo_2)); - let verified_2 = ed_verify(pub_key_2, signature_2, msg_hash_2); - - assert(verified_2.is_err()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/Forc.toml deleted file mode 100644 index 18fc492db04..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "flags_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/src/main.sw deleted file mode 100644 index dc8d7dac430..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/flags_inline_tests/src/main.sw +++ /dev/null @@ -1,67 +0,0 @@ -library; - -use std::{ - flags::{ - disable_panic_on_overflow, - disable_panic_on_unsafe_math, - enable_panic_on_overflow, - enable_panic_on_unsafe_math, - set_flags, - }, - registers::error, -}; - -#[test] -fn flags_disable_panic_on_overflow() { - let _ = disable_panic_on_overflow(); - let _bar = u64::max() + 1; - enable_panic_on_overflow(); -} - -#[test] -fn flags_disable_panic_on_overflow_preserving() { - let _ = disable_panic_on_overflow(); - - let prior_flags = disable_panic_on_overflow(); - let _bar = u64::max() + 1; - set_flags(prior_flags); - - let _bar = u64::max() + 1; - - enable_panic_on_overflow(); -} - -#[test] -fn flags_disable_panic_on_unsafe_math() { - let _ = disable_panic_on_unsafe_math(); - - let _bar = asm(r2: 1, r3: 0, r1) { - div r1 r2 r3; - r1: u64 - }; - - assert(error() == 1); - - enable_panic_on_unsafe_math(); -} - -#[test] -fn flags_disable_panic_on_unsafe_math_preserving() { - let _ = disable_panic_on_unsafe_math(); - - let prior_flags = disable_panic_on_unsafe_math(); - let _bar = asm(r2: 1, r3: 0, r1) { - div r1 r2 r3; - r1: u64 - }; - assert(error() == 1); - set_flags(prior_flags); - - let _bar = asm(r2: 1, r3: 0, r1) { - div r1 r2 r3; - r1: u64 - }; - assert(error() == 1); - - enable_panic_on_unsafe_math(); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/Forc.toml deleted file mode 100644 index b0e33a7ead7..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "hash_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/src/main.sw deleted file mode 100644 index ccef01df70f..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/hash_inline_tests/src/main.sw +++ /dev/null @@ -1,457 +0,0 @@ -library; - -use std::{bytes::Bytes, hash::{Hash, Hasher, keccak256, sha256, sha256_str_array},}; - -#[test()] -fn hash_hasher_write_str() { - let mut hasher = Hasher::new(); - hasher.write_str("test"); - let sha256 = hasher.sha256(); - assert(sha256 == 0x9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08); - - let mut hasher = Hasher::new(); - hasher.write_str("Fastest Modular Execution Layer!"); - let sha256 = hasher.sha256(); - assert(sha256 == 0x4a3cd7c8b44dbf7941e55179425f746adeaa97fe2d99b571fffee78e9b41743c); -} - -#[test()] -fn hash_hasher_keccak256_str() { - let mut hasher = Hasher::new(); - hasher.write_str("test"); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658); - - let mut hasher = Hasher::new(); - hasher.write_str("Fastest Modular Execution Layer!"); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0xab8e83e041e001bcf797c9cc7d6bc472bfdb8c736bab7999f13b7c26f48c354f); -} - -#[test()] -fn hash_hasher_write_str_array() { - let mut hasher = Hasher::new(); - hasher.write_str_array(__to_str_array("test")); - let sha256 = hasher.sha256(); - assert(sha256 == 0x9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08); - - let mut hasher = Hasher::new(); - hasher.write_str_array(__to_str_array("Fastest Modular Execution Layer!")); - let sha256 = hasher.sha256(); - assert(sha256 == 0x4a3cd7c8b44dbf7941e55179425f746adeaa97fe2d99b571fffee78e9b41743c); -} - -#[test()] -fn hash_hasher_keccak256_str_array() { - let mut hasher = Hasher::new(); - hasher.write_str_array(__to_str_array("test")); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658); - - let mut hasher = Hasher::new(); - hasher.write_str_array(__to_str_array("Fastest Modular Execution Layer!")); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0xab8e83e041e001bcf797c9cc7d6bc472bfdb8c736bab7999f13b7c26f48c354f); -} - -// The hashes for the following test can be obtained in Rust by running the following script: -// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a2d83e9ea48b35a3e991c904c3451ed5 -#[test()] -fn hash_hasher_sha256_u8() { - let mut hasher = Hasher::new(); - 0_u8.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d); - - let mut hasher = Hasher::new(); - 1_u8.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a); -} - -#[test()] -fn hash_hasher_keccak256_u8() { - let mut hasher = Hasher::new(); - 0_u8.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a); - - let mut hasher = Hasher::new(); - 1_u8.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); -} - -#[test()] -fn hash_hasher_sha256_u16() { - let mut hasher = Hasher::new(); - 0_u16.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x96a296d224f285c67bee93c30f8a309157f0daa35dc5b87e410b78630a09cfc7); - - let mut hasher = Hasher::new(); - 1_u16.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xb413f47d13ee2fe6c845b2ee141af81de858df4ec549a58b7970bb96645bc8d2); -} - -#[test()] -fn hash_hasher_keccak256_u16() { - let mut hasher = Hasher::new(); - 0_u16.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x54a8c0ab653c15bfb48b47fd011ba2b9617af01cb45cab344acd57c924d56798); - - let mut hasher = Hasher::new(); - 1_u16.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x49d03a195e239b52779866b33024210fc7dc66e9c2998975c0aa45c1702549d5); -} - -#[test()] -fn hash_hasher_sha256_u32() { - let mut hasher = Hasher::new(); - 0_u32.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xdf3f619804a92fdb4057192dc43dd748ea778adc52bc498ce80524c014b81119); - - let mut hasher = Hasher::new(); - 1_u32.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xb40711a88c7039756fb8a73827eabe2c0fe5a0346ca7e0a104adc0fc764f528d); -} - -#[test()] -fn hash_hasher_keccak256_u32() { - let mut hasher = Hasher::new(); - 0_u32.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0xe8e77626586f73b955364c7b4bbf0bb7f7685ebd40e852b164633a4acbd3244c); - - let mut hasher = Hasher::new(); - 1_u32.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x51f81bcdfc324a0dff2b5bec9d92e21cbebc4d5e29d3a3d30de3e03fbeab8d7f); -} - -#[test()] -fn hash_hasher_sha256_u64() { - let mut hasher = Hasher::new(); - 0_u64.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xaf5570f5a1810b7af78caf4bc70a660f0df51e42baf91d4de5b2328de0e83dfc); - - let mut hasher = Hasher::new(); - 1_u64.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xcd2662154e6d76b2b2b92e70c0cac3ccf534f9b74eb5b89819ec509083d00a50); -} - -#[test()] -fn hash_hasher_keccak256_u64() { - let mut hasher = Hasher::new(); - 0_u64.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce); - - let mut hasher = Hasher::new(); - 1_u64.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x6c31fc15422ebad28aaf9089c306702f67540b53c7eea8b7d2941044b027100f); -} - -#[test()] -fn hash_hasher_sha256_b256() { - let mut hasher = Hasher::new(); - 0x0000000000000000000000000000000000000000000000000000000000000000 - .hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let mut hasher = Hasher::new(); - 0x0000000000000000000000000000000000000000000000000000000000000001 - .hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); -} - -#[test()] -fn hash_hasher_keccak256_b256() { - let mut hasher = Hasher::new(); - 0x0000000000000000000000000000000000000000000000000000000000000000 - .hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563); - - let mut hasher = Hasher::new(); - 0x0000000000000000000000000000000000000000000000000000000000000001 - .hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6); -} - -#[test] -fn hash_hasher_sha256_u256() { - let mut hasher = Hasher::new(); - 0x0000000000000000000000000000000000000000000000000000000000000000_u256 - .hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let mut hasher = Hasher::new(); - 0x0000000000000000000000000000000000000000000000000000000000000001_u256 - .hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); -} - -#[test] -fn hash_hasher_keccak256_u256() { - let mut hasher = Hasher::new(); - 0x0000000000000000000000000000000000000000000000000000000000000000_u256 - .hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563); - - let mut hasher = Hasher::new(); - 0x0000000000000000000000000000000000000000000000000000000000000001_u256 - .hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6); -} - -#[test()] -fn hash_hasher_sha256_bool() { - let mut hasher = Hasher::new(); - false.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d); - - let mut hasher = Hasher::new(); - true.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a); -} - -#[test()] -fn hash_hasher_keccak256_bool() { - let mut hasher = Hasher::new(); - false.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a); - - let mut hasher = Hasher::new(); - true.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); -} - -#[test] -fn hash_hasher_sha256_bytes() { - let mut hasher = Hasher::new(); - let mut bytes = Bytes::new(); - bytes.push(0u8); - bytes.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d); - - let mut hasher = Hasher::new(); - let mut bytes = Bytes::new(); - bytes.push(1u8); - bytes.hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a); -} - -#[test] -fn hash_hasher_keccak256_bytes() { - let mut hasher = Hasher::new(); - let mut bytes = Bytes::with_capacity(1); - bytes.push(0u8); - bytes.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a); - - let mut hasher = Hasher::new(); - let mut bytes = Bytes::with_capacity(1); - bytes.push(1u8); - bytes.hash(hasher); - let keccak256 = hasher.keccak256(); - assert(keccak256 == 0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2); -} - -#[test] -fn hash_hasher_sha256_3_tuple() { - let mut hasher = Hasher::new(); - (0_u64, 0_u64, 0_u64).hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x9d908ecfb6b256def8b49a7c504e6c889c4b0e41fe6ce3e01863dd7b61a20aa0); - - let mut hasher = Hasher::new(); - (1_u64, 1_u64, 1_u64).hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xf3dd2c58f4b546018d9a5e147e195b7744eee27b76cae299dad63f221173cca0); -} - -#[test] -fn hash_hasher_sha256_4_tuple() { - let mut hasher = Hasher::new(); - (0_u64, 0_u64, 0_u64, 0_u64).hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let mut hasher = Hasher::new(); - (1_u64, 1_u64, 1_u64, 1_u64).hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x696547da2108716208569c8d60e78fcb423e7ad45cb8c700eeda8a8805bf2571); -} - -#[test] -fn hash_hasher_sha256_5_tuple() { - let mut hasher = Hasher::new(); - (0_u64, 0_u64, 0_u64, 0_u64, 0_u64).hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x2c34ce1df23b838c5abf2a7f6437cca3d3067ed509ff25f11df6b11b582b51eb); - - let mut hasher = Hasher::new(); - (1_u64, 1_u64, 1u64, 1_u64, 1_u64).hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x7bf87db15ea1fff61e936a88ff181b511e66b22417ed270ebb90c298c2088c10); -} - -#[test] -fn hash_hasher_sha256_1_array() { - let mut hasher = Hasher::new(); - [0_u64].hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xaf5570f5a1810b7af78caf4bc70a660f0df51e42baf91d4de5b2328de0e83dfc); - - let mut hasher = Hasher::new(); - [1_u64].hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xcd2662154e6d76b2b2b92e70c0cac3ccf534f9b74eb5b89819ec509083d00a50); -} - -#[test] -fn hash_hasher_sha256_2_array() { - let mut hasher = Hasher::new(); - [0_u64, 0_u64].hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x374708fff7719dd5979ec875d56cd2286f6d3cf7ec317a3b25632aab28ec37bb); - - let mut hasher = Hasher::new(); - [1_u64, 1_u64].hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x532deabf88729cb43995ab5a9cd49bf9b90a079904dc0645ecda9e47ce7345a9); -} - -#[test] -fn hash_hasher_sha256_4_array() { - let mut hasher = Hasher::new(); - [0_u64, 0_u64, 0_u64, 0_u64].hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let mut hasher = Hasher::new(); - [1_u64, 1_u64, 1_u64, 1_u64].hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x696547da2108716208569c8d60e78fcb423e7ad45cb8c700eeda8a8805bf2571); -} - -#[test] -fn hash_hasher_sha256_5_array() { - let mut hasher = Hasher::new(); - [0_u64, 0_u64, 0_u64, 0_u64, 0_u64].hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x2c34ce1df23b838c5abf2a7f6437cca3d3067ed509ff25f11df6b11b582b51eb); - - let mut hasher = Hasher::new(); - [1_u64, 1_u64, 1_u64, 1_u64, 1_u64].hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x7bf87db15ea1fff61e936a88ff181b511e66b22417ed270ebb90c298c2088c10); -} - -#[test] -fn hash_hasher_sha256_6_array() { - let mut hasher = Hasher::new(); - [0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64].hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x17b0761f87b081d5cf10757ccc89f12be355c70e2e29df288b65b30710dcbcd1); - - let mut hasher = Hasher::new(); - [1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64].hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x9cd65c79280fcb0d834da54ea98364d11439ec21e106447abcee2893765809a4); -} - -#[test] -fn hash_hasher_sha256_8_array() { - let mut hasher = Hasher::new(); - [0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64] - .hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xf5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b); - - let mut hasher = Hasher::new(); - [1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64] - .hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x794dde2d7e1d63dc28474122bd094bd35499447b3764dbf6cdf7c75ca73918dc); -} - -#[test] -fn hash_hasher_sha256_9_array() { - let mut hasher = Hasher::new(); - [0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64] - .hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x834a709ba2534ebe3ee1397fd4f7bd288b2acc1d20a08d6c862dcd99b6f04400); - - let mut hasher = Hasher::new(); - [1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64] - .hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0xe62386a1ec5b8fd0ece7344a7cae775d73179cfc0950c4fdeed26c7e8944e795); -} - -#[test] -fn hash_hasher_sha256_10_array() { - let mut hasher = Hasher::new(); - [0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64, 0_u64] - .hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x5b6fb58e61fa475939767d68a446f97f1bff02c0e5935a3ea8bb51e6515783d8); - - let mut hasher = Hasher::new(); - [1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64, 1_u64] - .hash(hasher); - let sha256 = hasher.sha256(); - assert(sha256 == 0x5f80cf4c3ec64f652ea4ba4db7ea12896224546bd2ed4dd2032a8ce12fde16f9); -} - -#[test()] -fn hash_sha256() { - let digest = sha256(0_u64); - assert(digest == 0xaf5570f5a1810b7af78caf4bc70a660f0df51e42baf91d4de5b2328de0e83dfc); - - let digest = sha256(1_u64); - assert(digest == 0xcd2662154e6d76b2b2b92e70c0cac3ccf534f9b74eb5b89819ec509083d00a50); -} - -#[test()] -fn hash_sha256_str_array() { - let digest = sha256_str_array(__to_str_array("test")); - assert(digest == 0x9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08); - - let digest = sha256_str_array(__to_str_array("Fastest Modular Execution Layer!")); - assert(digest == 0x4a3cd7c8b44dbf7941e55179425f746adeaa97fe2d99b571fffee78e9b41743c); -} - -#[test()] -fn hash_keccak256() { - let digest = keccak256(0_u64); - assert(digest == 0x011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce); - - let digest = keccak256(1_u64); - assert(digest == 0x6c31fc15422ebad28aaf9089c306702f67540b53c7eea8b7d2941044b027100f); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/Forc.toml deleted file mode 100644 index e124feb3c7a..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "identity_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/src/main.sw deleted file mode 100644 index b6f5d148622..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/identity_inline_tests/src/main.sw +++ /dev/null @@ -1,247 +0,0 @@ -library; - -#[test] -fn identity_eq() { - let address1 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000000)); - let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000000)); - let address3 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let address4 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let address5 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let address6 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let contract_id1 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000)); - let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000)); - let contract_id3 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let contract_id4 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let contract_id5 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let contract_id6 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - - assert(address1 == address2); - assert(contract_id1 == contract_id2); - assert(address3 == address4); - assert(contract_id3 == contract_id4); - assert(address5 == address6); - assert(contract_id5 == contract_id6); -} - -#[test] -fn identity_ne() { - let address1 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000000)); - let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000000)); - let address3 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let address4 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let address5 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let address6 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let contract_id1 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000)); - let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000)); - let contract_id3 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let contract_id4 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let contract_id5 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let contract_id6 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - - assert(address1 != address3); - assert(address1 != address4); - assert(address1 != address5); - assert(address1 != address6); - assert(address2 != address3); - assert(address2 != address4); - assert(address2 != address5); - assert(address2 != address6); - assert(address3 != address5); - assert(address3 != address6); - assert(address4 != address5); - assert(address4 != address6); - - assert(contract_id1 != contract_id3); - assert(contract_id1 != contract_id4); - assert(contract_id1 != contract_id5); - assert(contract_id1 != contract_id6); - assert(contract_id2 != contract_id3); - assert(contract_id2 != contract_id4); - assert(contract_id2 != contract_id5); - assert(contract_id2 != contract_id6); - assert(contract_id3 != contract_id5); - assert(contract_id3 != contract_id6); - assert(contract_id4 != contract_id5); - assert(contract_id4 != contract_id6); - - assert(address1 != contract_id1); - assert(address1 != contract_id2); - assert(address1 != contract_id3); - assert(address1 != contract_id4); - assert(address1 != contract_id5); - assert(address1 != contract_id6); - assert(address2 != contract_id1); - assert(address2 != contract_id2); - assert(address2 != contract_id3); - assert(address2 != contract_id4); - assert(address2 != contract_id5); - assert(address2 != contract_id6); - assert(address3 != contract_id1); - assert(address3 != contract_id2); - assert(address3 != contract_id3); - assert(address3 != contract_id4); - assert(address3 != contract_id5); - assert(address3 != contract_id6); - assert(address4 != contract_id1); - assert(address4 != contract_id2); - assert(address4 != contract_id3); - assert(address4 != contract_id4); - assert(address4 != contract_id5); - assert(address4 != contract_id6); - assert(address5 != contract_id1); - assert(address5 != contract_id2); - assert(address5 != contract_id3); - assert(address5 != contract_id4); - assert(address5 != contract_id5); - assert(address5 != contract_id6); - assert(address6 != contract_id1); - assert(address6 != contract_id2); - assert(address6 != contract_id3); - assert(address6 != contract_id4); - assert(address6 != contract_id5); - assert(address6 != contract_id6); -} - -#[test] -fn identity_as_address() { - let address1 = Identity::Address(Address::zero()); - let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let contract_id1 = Identity::ContractId(ContractId::zero()); - let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - - assert(address1.as_address().unwrap() == Address::zero()); - assert(contract_id1.as_address().is_none()); - assert( - address2 - .as_address() - .unwrap() == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), - ); - assert(contract_id2.as_address().is_none()); - assert( - address3 - .as_address() - .unwrap() == Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF), - ); - assert(contract_id3.as_address().is_none()); -} - -#[test] -fn identity_as_contract_id() { - let address1 = Identity::Address(Address::zero()); - let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let contract_id1 = Identity::ContractId(ContractId::zero()); - let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - - assert(address1.as_contract_id().is_none()); - assert(contract_id1.as_contract_id().unwrap() == ContractId::zero()); - assert(address2.as_contract_id().is_none()); - assert( - contract_id2 - .as_contract_id() - .unwrap() == ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001), - ); - assert(address3.as_contract_id().is_none()); - assert( - contract_id3 - .as_contract_id() - .unwrap() == ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF), - ); -} - -#[test] -fn identity_is_address() { - let address1 = Identity::Address(Address::zero()); - let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let contract_id1 = Identity::ContractId(ContractId::zero()); - let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - - assert(address1.is_address()); - assert(!contract_id1.is_address()); - assert(address2.is_address()); - assert(!contract_id2.is_address()); - assert(address3.is_address()); - assert(!contract_id3.is_address()); -} - -#[test] -fn identity_is_contract_id() { - let address1 = Identity::Address(Address::zero()); - let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let contract_id1 = Identity::ContractId(ContractId::zero()); - let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - - assert(!address1.is_contract_id()); - assert(contract_id1.is_contract_id()); - assert(!address2.is_contract_id()); - assert(contract_id2.is_contract_id()); - assert(!address3.is_contract_id()); - assert(contract_id3.is_contract_id()); -} - -#[test] -fn identity_bits() { - let address1 = Identity::Address(Address::zero()); - let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let contract_id1 = Identity::ContractId(ContractId::zero()); - let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - - assert(address1.bits() == b256::zero()); - assert(contract_id1.bits() == b256::zero()); - assert( - address2 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - assert( - contract_id2 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - assert( - address3 - .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - assert( - contract_id3 - .bits() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); -} - -#[test] -fn identity_hash() { - use std::hash::{Hash, sha256}; - - let address1 = Identity::Address(Address::zero()); - let address2 = Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let address3 = Identity::Address(Address::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - let contract_id1 = Identity::ContractId(ContractId::zero()); - let contract_id2 = Identity::ContractId(ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let contract_id3 = Identity::ContractId(ContractId::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); - - assert( - sha256(address1) == 0x7f9c9e31ac8256ca2f258583df262dbc7d6f68f2a03043d5c99a4ae5a7396ce9, - ); - assert( - sha256(contract_id1) == 0x1a7dfdeaffeedac489287e85be5e9c049a2ff6470f55cf30260f55395ac1b159, - ); - assert( - sha256(address2) == 0x1fd4247443c9440cb3c48c28851937196bc156032d70a96c98e127ecb347e45f, - ); - assert( - sha256(contract_id2) == 0x2e255099d6d6bee307c8e7075acc78f949897c5f67b53adf60724c814d7b90cb, - ); - assert( - sha256(address3) == 0x5e16d316ecd5773e50c3b02737d424192b02f25b4245822079181c557aafda7d, - ); - assert( - sha256(contract_id3) == 0x29fb7cd3be48a8d76bb031f0abce26caa9e092c000cd16bb101d30f63c4c1bc1, - ); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/Forc.toml deleted file mode 100644 index 6ac992f8d23..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "math_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/src/main.sw deleted file mode 100644 index 712409a7df2..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/math_inline_tests/src/main.sw +++ /dev/null @@ -1,632 +0,0 @@ -library; - -#[test] -fn math_root_u256() { - let max_u256 = u256::max(); - - assert(0x1u256.sqrt() == 1); - assert(0x4u256.sqrt() == 2); - assert(0x9u256.sqrt() == 3); - assert(0x90u256.sqrt() == 12); - assert(0x400u256.sqrt() == 32); - assert(0x2386f26fc10000u256.sqrt() == 100000000); - assert(0x0u256.sqrt() == 0); - assert(0x2u256.sqrt() == 1); - assert(0x5u256.sqrt() == 2); - assert(0x3e8u256.sqrt() == 31); - assert(max_u256.sqrt() == 0xffffffffffffffffffffffffffffffffu256); -} - -#[test] -fn math_root_u64() { - let max_u64 = u64::max(); - - assert(1.sqrt() == 1); - assert(4.sqrt() == 2); - assert(9.sqrt() == 3); - assert(144.sqrt() == 12); - assert(1024.sqrt() == 32); - assert(10000000000000000.sqrt() == 100000000); - assert(0.sqrt() == 0); - assert(2.sqrt() == 1); - assert(5.sqrt() == 2); - assert(1000.sqrt() == 31); - assert(max_u64.sqrt() == 4294967295); -} - -#[test] -fn math_root_u32() { - let max_u32 = u32::max(); - - assert(1u32.sqrt() == 1); - assert(4u32.sqrt() == 2); - assert(9u32.sqrt() == 3); - assert(144u32.sqrt() == 12); - assert(1024u32.sqrt() == 32); - assert(100000000u32.sqrt() == 10000); - assert(0u32.sqrt() == 0); - assert(2u32.sqrt() == 1); - assert(5u32.sqrt() == 2); - assert(1000u32.sqrt() == 31); - assert(max_u32.sqrt() == 65535); -} - -#[test] -fn math_root_u16() { - let max_u16 = u16::max(); - - assert(1u16.sqrt() == 1); - assert(4u16.sqrt() == 2); - assert(9u16.sqrt() == 3); - assert(144u16.sqrt() == 12); - assert(1024u16.sqrt() == 32); - assert(50625u16.sqrt() == 225); - assert(0u16.sqrt() == 0); - assert(2u16.sqrt() == 1); - assert(5u16.sqrt() == 2); - assert(1000u16.sqrt() == 31); - assert(max_u16.sqrt() == 255); -} - -#[test] -fn math_root_u8() { - let max_u8 = u8::max(); - - assert(1u8.sqrt() == 1); - assert(4u8.sqrt() == 2); - assert(9u8.sqrt() == 3); - assert(144u8.sqrt() == 12); - assert(0u8.sqrt() == 0); - assert(2u8.sqrt() == 1); - assert(5u8.sqrt() == 2); - assert(max_u8.sqrt() == 15); -} - -#[test] -fn math_power_u256() { - let five = 0x0000000000000000000000000000000000000000000000000000000000000005u256; - - // 5^2 = 25 = 0x19 - assert_eq( - five - .pow(2), - 0x0000000000000000000000000000000000000000000000000000000000000019u256, - ); - - // 5^28 = 0x204FCE5E3E2502611 (see https://www.wolframalpha.com/input?i=convert+5%5E28+in+hex) - assert_eq(five.pow(28), 0x0000000000000000204FCE5E3E2502611u256); -} - -#[test] -fn math_power_u64() { - assert(2.pow(2) == 4); - assert(2 ** 2 == 4); - - assert(2.pow(3) == 8); - assert(2 ** 3 == 8); - - assert(42.pow(2) == 1764); - assert(42 ** 2 == 1764); - - assert(42.pow(3) == 74088); - assert(42 ** 3 == 74088); - - assert(100.pow(5) == 10000000000); - assert(100 ** 5 == 10000000000); - - assert(100.pow(8) == 10000000000000000); - assert(100 ** 8 == 10000000000000000); - - assert(100.pow(9) == 1000000000000000000); - assert(100 ** 9 == 1000000000000000000); - - assert(2.pow(0) == 1); - assert(2 ** 0 == 1); - - assert(0.pow(1) == 0); - assert(0 ** 1 == 0); - - assert(0.pow(2) == 0); - assert(0 ** 2 == 0); -} - -#[test] -fn math_power_u32() { - assert(2u32.pow(2u32) == 4u32); - assert(2u32 ** 2u32 == 4u32); - - assert(2u32.pow(3u32) == 8u32); - assert(2u32 ** 3u32 == 8u32); - - assert(42u32.pow(2u32) == 1764u32); - assert(42u32 ** 2u32 == 1764u32); - - assert(100u32.pow(4u32) == 100000000u32); - assert(100u32 ** 4u32 == 100000000u32); - - assert(2u32.pow(0u32) == 1u32); - assert(2u32 ** 0u32 == 1u32); - - assert(0u32.pow(1u32) == 0u32); - assert(0u32 ** 1u32 == 0u32); - - assert(0u32.pow(2u32) == 0u32); - assert(0u32 ** 2u32 == 0u32); -} - -#[test] -fn math_power_u16() { - assert(2u16.pow(2u32) == 4u16); - assert(2u16 ** 2u32 == 4u16); - - assert(2u16.pow(3u32) == 8u16); - assert(2u16 ** 3u32 == 8u16); - - assert(42u16.pow(2u32) == 1764u16); - assert(42u16 ** 2u32 == 1764u16); - - assert(20u16.pow(3u32) == 8000u16); - assert(20u16 ** 3u32 == 8000u16); - - assert(15u16.pow(4u32) == 50625u16); - assert(15u16 ** 4u32 == 50625u16); - - assert(2u16.pow(0u32) == 1u16); - assert(2u16 ** 0u32 == 1u16); - - assert(0u16.pow(1u32) == 0u16); - assert(0u16 ** 1u32 == 0u16); - - assert(0u16.pow(2u32) == 0u16); - assert(0u16 ** 2u32 == 0u16); -} - -#[test] -fn math_power_u8() { - assert(2u8.pow(2u32) == 4u8); - assert(2u8 ** 2u32 == 4u8); - - assert(2u8.pow(3u32) == 8u8); - assert(2u8 ** 3u32 == 8u8); - - assert(4u8.pow(3u32) == 64u8); - assert(4u8 ** 3u32 == 64u8); - - assert(3u8.pow(4u32) == 81u8); - assert(3u8 ** 4u32 == 81u8); - - assert(10u8.pow(2u32) == 100u8); - assert(10u8 ** 2u32 == 100u8); - - assert(5u8.pow(3u32) == 125u8); - assert(5u8 ** 3u32 == 125u8); - - assert(3u8.pow(5u32) == 243u8); - assert(3u8 ** 5u32 == 243u8); - - assert(2u8.pow(0u32) == 1u8); - assert(2u8 ** 0u32 == 1u8); - - assert(0u8.pow(1u32) == 0u8); - assert(0u8 ** 1u32 == 0u8); - - assert(0u8.pow(2u32) == 0u8); - assert(0u8 ** 2u32 == 0u8); -} - -#[test] -fn math_log_u64() { - let max_u64 = u64::max(); - - assert(2.log(2) == 1); - assert(2.log2() == 1); - assert(1.log(3) == 0); - assert(8.log(2) == 3); - assert(8.log2() == 3); - assert(100.log(10) == 2); - assert(100.log(2) == 6); - assert(100.log2() == 6); - assert(100.log(9) == 2); - assert(max_u64.log(10) == 19); - assert(max_u64.log(2) == 63); -} - -#[test] -fn math_log_u32() { - let max_u32 = u32::max(); - - assert(2u32.log(2u32) == 1u32); - assert(100u32.log(10u32) == 2u32); - assert(125u32.log(5u32) == 3u32); - assert(256u32.log(4u32) == 4u32); - assert(max_u32.log(10) == 9); - assert(max_u32.log(2) == 31); -} - -#[test] -fn math_log_u16() { - let max_u16 = u16::max(); - - assert(7u16.log(7u16) == 1u16); - assert(49u16.log(7u16) == 2u16); - assert(27u16.log(3u16) == 3u16); - assert(1024u16.log(2u16) == 10u16); - assert(max_u16.log(10) == 4); - assert(max_u16.log(2) == 15); -} - -#[test] -fn math_log_u8() { - let max_u8 = u8::max(); - - assert(20u8.log(20u8) == 1u8); - assert(81u8.log(9u8) == 2u8); - assert(36u8.log(6u8) == 2u8); - assert(125u8.log(5u8) == 3u8); - assert(max_u8.log(10) == 2); - assert(max_u8.log(2) == 7); -} - -#[test] -fn math_log_u256() { - let max_u256 = u256::max(); - assert(0x2u256.log(0x2u256) == 0x1u256); - assert(0x1u256.log(0x3u256) == 0); - assert(0x8u256.log(0x2u256) == 0x3u256); - assert(0x64u256.log(0xau256) == 0x2u256); - assert(0x64u256.log(0x2u256) == 0x6u256); - assert(0x64u256.log(0x9u256) == 0x2u256); - assert(max_u256.log(0x2u256) == 0xffu256); -} - -#[test] -fn math_log2_u256() { - let max_u256 = u256::max(); - assert(0x2u256.log2() == 0x1u256); - assert(0x401u256.log2() == 0xau256); - assert(max_u256.log2() == 0xffu256); - assert(0x2u256.log2() == 0x1u256); - assert(0x8u256.log2() == 0x3u256); - assert(0x64u256.log2() == 0x6u256); -} - -#[test] -fn math_log2_u64() { - let max_u64 = u64::max(); - assert(max_u64.log2() == 63); -} - -#[test] -fn math_log2_u32() { - let max_u32 = u32::max(); - assert(max_u32.log2() == 31); -} - -#[test] -fn math_log2_u16() { - let max_u16 = u16::max(); - assert(max_u16.log2() == 15); -} - -#[test] -fn math_log2_u8() { - let max_u8 = u8::max(); - assert(max_u8.log2() == 7); -} - -#[test] -fn math_u8_zero() { - let my_u8 = u8::zero(); - assert(my_u8.is_zero()); - - let other_u8 = 1u8; - assert(!other_u8.is_zero()); -} - -#[test] -fn math_u16_zero() { - let my_u16 = u16::zero(); - assert(my_u16.is_zero()); - - let other_u16 = 1u16; - assert(!other_u16.is_zero()); -} - -#[test] -fn math_u32_zero() { - let my_u32 = u32::zero(); - assert(my_u32.is_zero()); - - let other_u32 = 1u32; - assert(!other_u32.is_zero()); -} - -#[test] -fn math_u64_zero() { - let my_u64 = u64::zero(); - assert(my_u64.is_zero()); - - let other_u64 = 1u64; - assert(!other_u64.is_zero()); -} - -#[test] -fn math_u256_zero() { - let my_u256 = u256::zero(); - assert(my_u256.is_zero()); - - let other_u256 = 0x01u256; - assert(!other_u256.is_zero()); -} - -#[test] -fn math_b256_zero() { - let my_b256 = b256::zero(); - assert(my_b256.is_zero()); - - let other_b256 = 0x0000000000000000000000000000000000000000000000000000000000000001; - assert(!other_b256.is_zero()); -} - -#[test] -fn math_test_parity_u256_log_with_ruint() { - // Failure cases found by comparing parity with ruint implementation of U256 - // https://docs.rs/ruint/latest/src/ruint/log.rs.html#45-89 - let a = [ - 2, 4, 4, 4, 4, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, - 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 15, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, - 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, - 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, - 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, - 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, - 25, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 29, - 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, - 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, - 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, - 33, 33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, - 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, - 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, - 35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, - 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, - 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, - 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, - 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, - 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, - 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, - 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, - 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, - 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, - 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, - 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, - 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, - 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 50, 50, - 50, 50, 50, 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 52, - 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 54, - 54, 54, 54, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, - 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 60, 60, - 60, 60, 61, 61, 61, 62, 62, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, - 64, 64, 64, 64, 64, 64, 64, 64, 64, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, - 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, - 65, 65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, - 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, - 66, 66, 66, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, - 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, - 67, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, - 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, - 68, 68, 68, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, - 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 70, - 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, - 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71, - 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, - 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, - 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, - 72, 72, 72, 72, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, - 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74, 74, - 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, - 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, - 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, - 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, - 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, - 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, - 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 80, 80, 80, 80, - 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, - 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, - 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, - 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 83, 83, 83, 83, 83, 83, 83, 83, - 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 84, 84, 84, 84, - 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 85, - 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, - 85, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, - 86, 86, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, - 87, 87, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, - 88, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 90, - 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, - 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 92, 92, - 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, - 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 95, 95, 95, - 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, - 96, 96, 96, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 98, - 98, 98, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 100, 100, 100, 100, 100, - 100, 100, 100, 100, - ]; - let b = [ - 3, 3, 5, 6, 7, 3, 6, 7, 3, 7, 3, 3, 9, 10, 11, 12, 13, 14, 15, 3, 10, 11, 12, 13, 14, 15, - 3, 11, 12, 13, 14, 15, 3, 12, 13, 14, 15, 3, 13, 14, 15, 3, 14, 15, 3, 15, 3, 3, 5, 6, 7, - 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 7, 18, 19, 20, 21, 22, - 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 7, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 3, 5, 6, 7, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 7, 21, 22, 23, - 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 7, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, - 6, 7, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, - 6, 7, 25, 26, 27, 28, 29, 30, 31, 3, 6, 7, 26, 27, 28, 29, 30, 31, 3, 6, 7, 27, 28, 29, 30, - 31, 3, 6, 7, 28, 29, 30, 31, 3, 6, 7, 29, 30, 31, 3, 6, 7, 30, 31, 3, 6, 7, 31, 3, 6, 7, 3, - 6, 7, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, - 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 6, 7, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, - 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 6, 7, - 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, - 58, 59, 60, 61, 62, 63, 3, 6, 7, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, - 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 37, 38, 39, 40, 41, 42, 43, - 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 38, - 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, - 62, 63, 3, 7, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, - 58, 59, 60, 61, 62, 63, 3, 7, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, - 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, - 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 43, 44, 45, 46, 47, 48, 49, 50, 51, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 44, 45, 46, 47, 48, 49, 50, 51, 52, - 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, - 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, - 58, 59, 60, 61, 62, 63, 3, 7, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, - 62, 63, 3, 7, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 7, 49, 50, - 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 50, 51, 52, 53, 54, 55, 56, 57, 58, - 59, 60, 61, 62, 63, 3, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 52, 53, 54, - 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 54, - 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 55, 56, 57, 58, 59, 60, 61, 62, 63, 3, 56, 57, 58, - 59, 60, 61, 62, 63, 3, 57, 58, 59, 60, 61, 62, 63, 3, 58, 59, 60, 61, 62, 63, 3, 59, 60, - 61, 62, 63, 3, 60, 61, 62, 63, 3, 61, 62, 63, 3, 62, 63, 3, 63, 3, 3, 5, 6, 7, 9, 10, 11, - 12, 13, 14, 15, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, - 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, - 12, 13, 14, 15, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, - 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, - 13, 14, 15, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, - 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, - 15, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, - 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 69, 70, - 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, - 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 70, 71, 72, 73, 74, 75, - 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, - 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, - 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, - 10, 11, 12, 13, 14, 15, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, - 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 73, - 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, - 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 74, 75, 76, 77, 78, 79, 80, 81, 82, - 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, - 11, 12, 13, 14, 15, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, - 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 76, 77, 78, 79, 80, - 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, - 7, 9, 10, 11, 12, 13, 14, 15, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, - 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 78, 79, 80, 81, - 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, - 10, 11, 12, 13, 14, 15, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, - 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 80, 81, 82, 83, 84, 85, 86, 87, - 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, - 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, - 7, 10, 11, 12, 13, 14, 15, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, - 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, - 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 84, 85, 86, 87, 88, - 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 85, - 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, - 14, 15, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, - 12, 13, 14, 15, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, - 11, 12, 13, 14, 15, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, - 11, 12, 13, 14, 15, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, - 12, 13, 14, 15, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, - 14, 15, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 92, - 93, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 93, 94, 95, 96, 97, - 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 94, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, - 10, 11, 12, 13, 14, 15, 95, 96, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 96, - 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 97, 98, 99, 100, 3, 5, 6, 7, 10, 11, - 12, 13, 14, 15, 98, 99, 100, 3, 5, 6, 7, 10, 11, 12, 13, 14, 15, 99, 100, 3, 5, 6, 7, 10, - 11, 12, 13, 14, 15, 100, 3, 5, 6, 7, 11, 12, 13, 14, 15, - ]; - let expected = [ - 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, - 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 2, 0, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0, - 0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0, 0, 0, 3, 1, 1, 0, - 0, 0, 3, 1, 1, 0, 0, 3, 1, 1, 0, 3, 1, 1, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, - 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 3, 0, 3, 3, 2, 2, 2, 1, 1, 1, - 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, - 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, - 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, - 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, 1, 1, - 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 1, - 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, - 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, - 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, - 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, - 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, - 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 4, 2, 2, 2, - 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 4, 2, 2, 2, 1, 1, 1, - 1, 1, 1, 0, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 4, 2, 2, 2, 1, 1, 1, 1, 1, - ]; - - let mut i = 0; - - while i < 1825 { - let ai: u64 = a[i]; - let bi: u64 = b[i]; - let expected_val: u64 = expected[i]; - - let result = u256::from(ai).log(u256::from(bi)); - assert_eq(result, u256::from(expected_val)); - i += 1; - } -} - -#[test(should_revert)] -fn math_u256_log_fail_base_0() { - let _result = u256::from(2_u64).log(u256::from(0_u64)); -} - -#[test(should_revert)] -fn math_u256_log_fail_base_1() { - let _result = u256::from(2_u64).log(u256::from(1_u64)); -} - -#[test(should_revert)] -fn math_u256_log_fail_x_0() { - let _result = u256::from(0_u64).log(u256::from(2_u64)); -} - -#[test(should_revert)] -fn math_u256_log2_fail_x_0() { - let _result = u256::from(0_u64).log2(); -} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/Forc.toml deleted file mode 100644 index 4a79bcec8cd..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "option_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/src/main.sw deleted file mode 100644 index 7ca19c1d4d7..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/option_inline_tests/src/main.sw +++ /dev/null @@ -1,354 +0,0 @@ -library; - -#[test] -fn option_eq() { - use std::bytes::Bytes; - - let u8_1 = Some(1u8); - let u8_2 = Some(1u8); - let u16_1 = Some(1u16); - let u16_2 = Some(1u16); - let u32_1 = Some(1u32); - let u32_2 = Some(1u32); - let u64_1 = Some(1u64); - let u64_2 = Some(1u64); - let u256_1 = Some(0x01u256); - let u256_2 = Some(0x01u256); - let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_2 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let struct_2 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let enum_2 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let _array_1 = Some([0u64, 0u64]); - let _array_2 = Some([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let mut bytes_2 = Bytes::new(); - bytes_2.push(1u8); - let heap_1 = Some(bytes_1); - let heap_2 = Some(bytes_2); - let none_1 = Option::::None; - let none_2 = Option::::None; - - assert(u8_1 == u8_2); - assert(u16_1 == u16_2); - assert(u32_1 == u32_2); - assert(u64_1 == u64_2); - assert(u256_1 == u256_2); - assert(b256_1 == b256_2); - assert(struct_1 == struct_2); - assert(enum_1 == enum_2); - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // assert(array_1 == array_2); - assert(heap_1 == heap_2); - assert(none_1 == none_2); -} - -#[test] -fn option_ne() { - use std::bytes::Bytes; - - let u8_1 = Some(1u8); - let u8_2 = Some(2u8); - let u16_1 = Some(1u16); - let u16_2 = Some(2u16); - let u32_1 = Some(1u32); - let u32_2 = Some(2u32); - let u64_1 = Some(1u64); - let u64_2 = Some(2u64); - let u256_1 = Some(0x01u256); - let u256_2 = Some(0x02u256); - let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_2 = Some(0x0000000000000000000000000000000000000000000000000000000000000002); - let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let struct_2 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000002)); - let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let enum_2 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000002))); - let _array_1 = Some([0u64, 0u64]); - let _array_2 = Some([0u64, 1u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let mut bytes_2 = Bytes::new(); - bytes_2.push(2u8); - let heap_1 = Some(bytes_1); - let heap_2 = Some(bytes_2); - let none_1 = Option::::None; - - assert(u8_1 != u8_2); - assert(u16_1 != u16_2); - assert(u32_1 != u32_2); - assert(u64_1 != u64_2); - assert(u256_1 != u256_2); - assert(b256_1 != b256_2); - assert(struct_1 != struct_2); - assert(enum_1 != enum_2); - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // assert(array_1 != array_2); - assert(heap_1 != heap_2); - assert(none_1 != u64_1); -} - -#[test] -fn option_is_some() { - use std::bytes::Bytes; - - let u8_1 = Some(1u8); - let u16_1 = Some(1u16); - let u32_1 = Some(1u32); - let u64_1 = Some(1u64); - let u256_1 = Some(0x01u256); - let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let array_1 = Some([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1 = Some(bytes_1); - let none_1 = Option::::None; - - assert(u8_1.is_some()); - assert(u16_1.is_some()); - assert(u32_1.is_some()); - assert(u64_1.is_some()); - assert(u256_1.is_some()); - assert(b256_1.is_some()); - assert(struct_1.is_some()); - assert(enum_1.is_some()); - assert(array_1.is_some()); - assert(heap_1.is_some()); - assert(!none_1.is_some()); -} - -#[test] -fn option_is_none() { - use std::bytes::Bytes; - - let u8_1 = Some(1u8); - let u16_1 = Some(1u16); - let u32_1 = Some(1u32); - let u64_1 = Some(1u64); - let u256_1 = Some(0x01u256); - let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let array_1 = Some([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1 = Some(bytes_1); - let none_1 = Option::::None; - - assert(!u8_1.is_none()); - assert(!u16_1.is_none()); - assert(!u32_1.is_none()); - assert(!u64_1.is_none()); - assert(!u256_1.is_none()); - assert(!b256_1.is_none()); - assert(!struct_1.is_none()); - assert(!enum_1.is_none()); - assert(!array_1.is_none()); - assert(!heap_1.is_none()); - assert(none_1.is_none()); -} - -#[test] -fn option_unwrap() { - use std::bytes::Bytes; - - let u8_1 = Some(1u8); - let u16_1 = Some(1u16); - let u32_1 = Some(1u32); - let u64_1 = Some(1u64); - let u256_1 = Some(0x01u256); - let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let _array_1 = Some([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1 = Some(bytes_1); - - assert(u8_1.unwrap() == 1u8); - assert(u16_1.unwrap() == 1u16); - assert(u32_1.unwrap() == 1u32); - assert(u64_1.unwrap() == 1u64); - assert(u256_1.unwrap() == 0x01u256); - assert( - b256_1 - .unwrap() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - assert( - struct_1 - .unwrap() == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), - ); - assert( - enum_1 - .unwrap() == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), - ); - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // assert(array_1.unwrap() == [0u64, 0u64]); - assert(heap_1.unwrap() == bytes_1); -} - -#[test(should_revert)] -fn revert_option_when_unwrap_none() { - let none = Option::::None; - let _result = none.unwrap(); -} - -#[test] -fn option_unwrap_or() { - use std::bytes::Bytes; - - let u8_1 = Some(1u8); - let u16_1 = Some(1u16); - let u32_1 = Some(1u32); - let u64_1 = Some(1u64); - let u256_1 = Some(0x01u256); - let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let _array_1 = Some([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1 = Some(bytes_1); - let none_1 = Option::::None; - - assert(u8_1.unwrap_or(2u8) == 1u8); - assert(u16_1.unwrap_or(2u16) == 1u16); - assert(u32_1.unwrap_or(2u32) == 1u32); - assert(u64_1.unwrap_or(2u64) == 1u64); - assert(u256_1.unwrap_or(0x02u256) == 0x01u256); - assert( - b256_1 - .unwrap_or(0x0000000000000000000000000000000000000000000000000000000000000002) == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - assert( - struct_1 - .unwrap_or(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)) == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), - ); - assert( - enum_1 - .unwrap_or(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000002))) == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), - ); - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // assert(array_1.unwrap_or([1u64, 1u64]) == [0u64, 0u64]); - assert(heap_1.unwrap_or(Bytes::new()) == bytes_1); - assert(none_1.unwrap_or(10u64) == 10u64); -} - -#[test] -fn option_ok_or() { - use std::bytes::Bytes; - - let u8_1 = Some(1u8); - let u16_1 = Some(1u16); - let u32_1 = Some(1u32); - let u64_1 = Some(1u64); - let u256_1 = Some(0x01u256); - let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let _array_1 = Some([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1 = Some(bytes_1); - let none_1 = Option::::None; - - match u8_1.ok_or(2u8) { - Result::Ok(underlying) => assert(underlying == 1u8), - Result::Err => revert(0), - }; - match u16_1.ok_or(2u16) { - Result::Ok(underlying) => assert(underlying == 1u16), - Result::Err => revert(0), - }; - match u32_1.ok_or(2u32) { - Result::Ok(underlying) => assert(underlying == 1u32), - Result::Err => revert(0), - }; - match u64_1.ok_or(2u64) { - Result::Ok(underlying) => assert(underlying == 1u64), - Result::Err => revert(0), - }; - match u256_1.ok_or(0x02u256) { - Result::Ok(underlying) => assert(underlying == 0x01u256), - Result::Err => revert(0), - }; - match b256_1.ok_or(0x0000000000000000000000000000000000000000000000000000000000000002) { - Result::Ok(underlying) => assert( - underlying == 0x0000000000000000000000000000000000000000000000000000000000000001, - ), - Result::Err => revert(0), - }; - match struct_1.ok_or(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)) { - Result::Ok(underlying) => assert( - underlying == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), - ), - Result::Err => revert(0), - }; - match enum_1.ok_or(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000002))) { - Result::Ok(underlying) => assert( - underlying == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), - ), - Result::Err => revert(0), - }; - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // match array_1.ok_or([1u64, 1u64]) { - // Result::Ok(underlying) => assert(underlying == [0u64, 0u64]), - // Result::Err => revert(0), - // }; - match heap_1.ok_or(Bytes::new()) { - Result::Ok(underlying) => assert(underlying == bytes_1), - Result::Err => revert(0), - } - match none_1.ok_or(10u64) { - Result::Ok(_) => revert(0), - Result::Err(underlying) => assert(underlying == 10u64), - } -} - -#[test] -fn option_expect() { - use std::bytes::Bytes; - - let u8_1 = Some(1u8); - let u16_1 = Some(1u16); - let u32_1 = Some(1u32); - let u64_1 = Some(1u64); - let u256_1 = Some(0x01u256); - let b256_1 = Some(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1 = Some(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1 = Some(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let _array_1 = Some([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1 = Some(bytes_1); - - assert(u8_1.expect("Failed Test") == 1u8); - assert(u16_1.expect("Failed Test") == 1u16); - assert(u32_1.expect("Failed Test") == 1u32); - assert(u64_1.expect("Failed Test") == 1u64); - assert(u256_1.expect("Failed Test") == 0x01u256); - assert( - b256_1 - .expect("Failed Test") == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - assert( - struct_1 - .expect("Failed Test") == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), - ); - assert( - enum_1 - .expect("Failed Test") == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), - ); - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // assert(array_1.expect("Failed Test") == [0u64, 0u64]); - assert(heap_1.expect("Failed Test") == bytes_1); -} - -#[test(should_revert)] -fn revert_option_expect_when_none() { - let none_1 = Option::::None; - let _result = none_1.expect("Failed Test"); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/Forc.toml deleted file mode 100644 index 7df7ff1fc55..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "primitive_conversions_b256_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw deleted file mode 100644 index 21fe0d19f71..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw +++ /dev/null @@ -1,168 +0,0 @@ -library; - -use std::{b512::B512, bytes::Bytes, primitive_conversions::b256::*, u128::U128}; - -#[test] -fn b256_try_from_bytes() { - let mut initial_bytes = Bytes::with_capacity(32); - let mut i = 0; - while i < 32 { - // 0x33 is 51 in decimal - initial_bytes.push(51u8); - i += 1; - } - let res1 = b256::try_from(initial_bytes); - let expected1 = 0x3333333333333333333333333333333333333333333333333333333333333333; - assert(res1.is_some()); - assert(res1.unwrap() == expected1); - - let mut second_bytes = Bytes::with_capacity(33); - i = 0; - while i < 33 { - // 0x33 is 51 in decimal - second_bytes.push(51u8); - i += 1; - } - let res2 = b256::try_from(second_bytes); - assert(res2.is_none()); - - // bytes is still available to use: - assert(second_bytes.len() == 33); - assert(second_bytes.capacity() == 33); - - let mut third_bytes = Bytes::with_capacity(31); - let mut i = 0; - while i < 31 { - // 0x33 is 51 in decimal - third_bytes.push(51u8); - i += 1; - } - let res3 = b256::try_from(third_bytes); - assert(res3.is_none()); -} - -#[test] -fn b256_try_from_b512() { - let b512_value = B512::new(); - let b256_value = b256::try_from(b512_value); - assert(b256_value.is_some()); - - let b512_value = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - b256::zero(), - )); - let b256_value = b256::try_from(b512_value); - assert(b256_value.is_none()); -} - -#[test] -fn b256_from_u256() { - let u256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000_u256; - let u256_2 = 0x0000000000000000000000000000000000000000000000000000000000000001_u256; - let u256_3 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u256; - - let res1 = b256::from(u256_1); - let res2 = b256::from(u256_2); - let res3 = b256::from(u256_3); - - assert(res1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - assert(res2 == 0x0000000000000000000000000000000000000000000000000000000000000001); - assert(res3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); -} - -#[test] -fn b256_into_u256() { - let u256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000_u256; - let u256_2 = 0x0000000000000000000000000000000000000000000000000000000000000001_u256; - let u256_3 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u256; - - let res1: b256 = u256_1.into(); - let res2: b256 = u256_2.into(); - let res3: b256 = u256_3.into(); - - assert(res1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - assert(res2 == 0x0000000000000000000000000000000000000000000000000000000000000001); - assert(res3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); -} - -#[test] -fn b256_from_u128() { - let b256_value1 = b256::from(U128::from((u64::min(), u64::min()))); - let b256_value2 = b256::from(U128::from((1_u64, 1_u64))); - let b256_value3 = b256::from(U128::from((u64::max(), u64::max()))); - let b256_value4 = b256::from(U128::from((u64::max(), u64::min()))); - let b256_value5 = b256::from(U128::from((u64::min(), u64::max()))); - - assert( - b256_value1 == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - assert( - b256_value2 == 0x0000000000000000000000000000000000000000000000010000000000000001, - ); - assert( - b256_value3 == 0x00000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - assert( - b256_value4 == 0x00000000000000000000000000000000ffffffffffffffff0000000000000000, - ); - assert( - b256_value5 == 0x000000000000000000000000000000000000000000000000ffffffffffffffff, - ); -} - -#[test] -fn b256_into_u128() { - let u128_1 = U128::from((u64::min(), u64::min())); - let u128_2 = U128::from((1_u64, 1_u64)); - let u128_3 = U128::from((u64::max(), u64::max())); - let u128_4 = U128::from((u64::max(), u64::min())); - let u128_5 = U128::from((u64::min(), u64::max())); - - let b256_value1: b256 = u128_1.into(); - let b256_value2: b256 = u128_2.into(); - let b256_value3: b256 = u128_3.into(); - let b256_value4: b256 = u128_4.into(); - let b256_value5: b256 = u128_5.into(); - - assert( - b256_value1 == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - assert( - b256_value2 == 0x0000000000000000000000000000000000000000000000010000000000000001, - ); - assert( - b256_value3 == 0x00000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - ); - assert( - b256_value4 == 0x00000000000000000000000000000000ffffffffffffffff0000000000000000, - ); - assert( - b256_value5 == 0x000000000000000000000000000000000000000000000000ffffffffffffffff, - ); -} - -#[test] -fn b256_from_tuple() { - let b256_1 = b256::from((0, 0, 0, 0)); - let b256_2 = b256::from((1, 2, 3, 4)); - let b256_3 = b256::from((u64::max(), u64::max(), u64::max(), u64::max())); - - assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - assert(b256_2 == 0x0000000000000001000000000000000200000000000000030000000000000004); - assert(b256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); -} - -#[test] -fn b256_into_tuple() { - let tuple_1 = (0, 0, 0, 0); - let tuple_2 = (1, 2, 3, 4); - let tuple_3 = (u64::max(), u64::max(), u64::max(), u64::max()); - - let b256_1: b256 = tuple_1.into(); - let b256_2: b256 = tuple_2.into(); - let b256_3: b256 = tuple_3.into(); - - assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - assert(b256_2 == 0x0000000000000001000000000000000200000000000000030000000000000004); - assert(b256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/Forc.toml deleted file mode 100644 index 40441657520..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "primitive_conversions_str_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/src/main.sw deleted file mode 100644 index e47430dd626..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_str_inline_tests/src/main.sw +++ /dev/null @@ -1,14 +0,0 @@ -library; - -use std::primitive_conversions::str::*; - -#[test] -fn str_slice_to_str_array() { - let a = "abcd"; - let b: str[4] = a.try_as_str_array().unwrap(); - assert(__size_of_str_array::() == a.len() && __size_of_val(b) == 8); - - let c = from_str_array(b); - - assert(a == c); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/Forc.toml deleted file mode 100644 index c1aec88c9bb..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "primitive_conversions_u16_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/src/main.sw deleted file mode 100644 index 6ac9fc8fb8a..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/src/main.sw +++ /dev/null @@ -1,129 +0,0 @@ -library; - -use std::{primitive_conversions::u16::*, u128::U128}; - -#[test] -fn u16_from_u8() { - let u8_1: u8 = u8::min(); - let u8_2: u8 = 255u8; - let u8_3: u8 = 2u8; - - let u16_1 = u16::from(u8_1); - let u16_2 = u16::from(u8_2); - let u16_3 = u16::from(u8_3); - - assert(u16_1 == 0u16); - assert(u16_2 == 255u16); - assert(u16_3 == 2u16); -} - -#[test] -fn u16_into_u8() { - let u8_1: u8 = u8::min(); - let u8_2: u8 = 255u8; - let u8_3: u8 = 2u8; - - let u16_1: u16 = u8_1.into(); - let u16_2: u16 = u8_2.into(); - let u16_3: u16 = u8_3.into(); - - assert(u16_1 == 0u16); - assert(u16_2 == 255u16); - assert(u16_3 == 2u16); -} - -#[test] -fn u16_try_from_u32() { - let u32_1: u32 = u32::min(); - let u32_2: u32 = u16::max().as_u32(); - let u32_3: u32 = u16::max().as_u32() + 1; - let u32_4: u32 = 2u32; - - let u16_1 = u16::try_from(u32_1); - let u16_2 = u16::try_from(u32_2); - let u16_3 = u16::try_from(u32_3); - let u16_4 = u16::try_from(u32_4); - - assert(u16_1.is_some()); - assert(u16_1.unwrap() == 0u16); - - assert(u16_2.is_some()); - assert(u16_2.unwrap() == u16::max()); - - assert(u16_3.is_none()); - - assert(u16_4.is_some()); - assert(u16_4.unwrap() == 2u16); -} - -#[test] -fn u16_try_from_u64() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = 2; - let u64_3: u64 = u16::max().as_u64(); - let u64_4: u64 = u16::max().as_u64() + 1; - - let u16_1 = u16::try_from(u64_1); - let u16_2 = u16::try_from(u64_2); - let u16_3 = u16::try_from(u64_3); - let u16_4 = u16::try_from(u64_4); - - assert(u16_1.is_some()); - assert(u16_1.unwrap() == 0u16); - - assert(u16_2.is_some()); - assert(u16_2.unwrap() == 2u16); - - assert(u16_3.is_some()); - assert(u16_3.unwrap() == u16::max()); - - assert(u16_4.is_none()); -} - -#[test] -fn u16_try_from_u256() { - let u256_1: u256 = u256::min(); - let u256_2: u256 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; - let u256_3: u256 = u16::max().as_u256(); - let u256_4: u256 = 0x1000000000000000000000000000000000000000000000000000000000000000u256; - - let u16_1 = u16::try_from(u256_1); - let u16_2 = u16::try_from(u256_2); - let u16_3 = u16::try_from(u256_3); - let u16_4 = u16::try_from(u256_4); - - assert(u16_1.is_some()); - assert(u16_1.unwrap() == 0u16); - - assert(u16_2.is_some()); - assert(u16_2.unwrap() == 2u16); - - assert(u16_3.is_some()); - assert(u16_3.unwrap() == u16::max()); - - assert(u16_4.is_none()); -} - -#[test] -fn u16_try_from_u128() { - let u128_1: U128 = U128::new(); - let u128_2: U128 = U128::from((0, 2u64)); - let u128_3: U128 = U128::from((0, u16::max().as_u64())); - let u128_4: U128 = U128::from((0, u16::max().as_u64() + 1)); - - let u16_1 = u16::try_from(u128_1); - let u16_2 = u16::try_from(u128_2); - let u16_3 = u16::try_from(u128_3); - let u16_4 = u16::try_from(u128_4); - - assert(u16_1.is_some()); - assert(u16_1.unwrap() == 0u16); - - assert(u16_2.is_some()); - assert(u16_2.unwrap() == 2u16); - - assert(u16_3.is_some()); - assert(u16_3.unwrap() == u16::max()); - - assert(u16_4.is_none()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/Forc.toml deleted file mode 100644 index 7aba4002607..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "primitive_conversions_u256_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/src/main.sw deleted file mode 100644 index 16e3f754344..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/src/main.sw +++ /dev/null @@ -1,311 +0,0 @@ -library; - -use std::{b512::B512, primitive_conversions::u256::*, u128::U128}; - -#[test] -fn u256_from_u8() { - let u256_1 = u256::from(u8::min()); - let u256_2 = u256::from(2_u8); - let u256_3 = u256::from(u8::max()); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256, - ); -} - -#[test] -fn u256_into_u8() { - let u8_1 = u8::min(); - let u8_2 = 2_u8; - let u8_3 = u8::max(); - - let u256_1: u256 = u8_1.into(); - let u256_2: u256 = u8_2.into(); - let u256_3: u256 = u8_3.into(); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256, - ); -} - -#[test] -fn u256_from_u16() { - let u256_1 = u256::from(u16::min()); - let u256_2 = u256::from(2_u16); - let u256_3 = u256::from(u16::max()); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256, - ); -} - -#[test] -fn u256_into_u16() { - let u16_1 = u16::min(); - let u16_2 = 2u16; - let u16_3 = u16::max(); - - let u256_1: u256 = u16_1.into(); - let u256_2: u256 = u16_2.into(); - let u256_3: u256 = u16_3.into(); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256, - ); -} - -#[test] -fn u256_from_u32() { - let u256_1 = u256::from(u32::min()); - let u256_2 = u256::from(2u32); - let u256_3 = u256::from(u32::max()); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256, - ); -} - -#[test] -fn u256_into_u32() { - let u32_1 = u32::min(); - let u32_2 = 2u32; - let u32_3 = u32::max(); - - let u256_1: u256 = u32_1.into(); - let u256_2: u256 = u32_2.into(); - let u256_3: u256 = u32_3.into(); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256, - ); -} - -#[test] -fn u256_from_u64() { - let u256_1 = u256::from(u64::min()); - let u256_2 = u256::from(2u64); - let u256_3 = u256::from(u64::max()); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256, - ); -} - -#[test] -fn u256_into_u64() { - let u64_1 = u64::min(); - let u64_2 = 2u64; - let u64_3 = u64::max(); - - let u256_1: u256 = u64_1.into(); - let u256_2: u256 = u64_2.into(); - let u256_3: u256 = u64_3.into(); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256, - ); -} - -#[test] -fn u256_from_b256() { - let u256_1 = u256::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let u256_2 = u256::from(0x0000000000000000000000000000000000000000000000000000000000000002); - let u256_3 = u256::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256, - ); -} - -#[test] -fn u256_into_b256() { - let b256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000; - let b256_2 = 0x0000000000000000000000000000000000000000000000000000000000000002; - let b256_3 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff; - - let u256_1: u256 = b256_1.into(); - let u256_2: u256 = b256_2.into(); - let u256_3: u256 = b256_3.into(); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256, - ); -} - -#[test] -fn u256_from_u128() { - let u256_1 = u256::from(U128::from((u64::min(), u64::min()))); - let u256_2 = u256::from(U128::from((0u64, 2u64))); - let u256_3 = u256::from(U128::from((u64::max(), u64::max()))); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff_u256, - ); -} - -#[test] -fn u256_into_u128() { - let u128_1 = U128::from((u64::min(), u64::min())); - let u128_2 = U128::from((0u64, 2u64)); - let u128_3 = U128::from((u64::max(), u64::max())); - - let u256_1: u256 = u128_1.into(); - let u256_2: u256 = u128_2.into(); - let u256_3: u256 = u128_3.into(); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - assert( - u256_3 == 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff_u256, - ); -} - -#[test] -fn u256_from_tuple() { - let u256_1 = u256::from((u64::min(), u64::min(), u64::min(), u64::min())); - let u256_2 = u256::from((1, 2, 3, 4)); - let u256_3 = u256::from((u64::max(), u64::max(), u64::max(), u64::max())); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000001000000000000000200000000000000030000000000000004_u256, - ); - assert( - u256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u256, - ); -} - -#[test] -fn u256_into_tuple() { - let tuple_1 = (u64::min(), u64::min(), u64::min(), u64::min()); - let tuple_2 = (1, 2, 3, 4); - let tuple_3 = (u64::max(), u64::max(), u64::max(), u64::max()); - - let u256_1: u256 = tuple_1.into(); - let u256_2: u256 = tuple_2.into(); - let u256_3: u256 = tuple_3.into(); - - assert( - u256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - assert( - u256_2 == 0x0000000000000001000000000000000200000000000000030000000000000004_u256, - ); - assert( - u256_3 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u256, - ); -} - -#[test] -fn u256_try_from_b512() { - let b512_1 = B512::new(); - let b512_2 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000002, - )); - let b512_3 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, - )); - let b512_4 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000000, - )); - - let u256_1 = u256::try_from(b512_1); - let u256_2 = u256::try_from(b512_2); - let u256_3 = u256::try_from(b512_3); - let u256_4 = u256::try_from(b512_4); - - assert(u256_1.is_some()); - assert( - u256_1 - .unwrap() == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - - assert(u256_2.is_some()); - assert( - u256_2 - .unwrap() == 0x0000000000000000000000000000000000000000000000000000000000000002_u256, - ); - - assert(u256_3.is_some()); - assert( - u256_3 - .unwrap() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u256, - ); - - assert(u256_4.is_none()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/Forc.toml deleted file mode 100644 index f49eef53b36..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "primitive_conversions_u32_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/src/main.sw deleted file mode 100644 index 862cab88c91..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/src/main.sw +++ /dev/null @@ -1,135 +0,0 @@ -library; - -use std::{primitive_conversions::u32::*, u128::U128}; - -#[test] -fn u32_from_u8() { - let u8_1: u8 = u8::min(); - let u8_2: u8 = 1u8; - let u8_3: u8 = u8::max(); - - let u32_1 = u32::from(u8_1); - let u32_2 = u32::from(u8_2); - let u32_3 = u32::from(u8_3); - - assert(u32_1 == 0u32); - assert(u32_2 == 1u32); - assert(u32_3 == 255u32); -} - -#[test] -fn u32_into_u8() { - let u8_1: u8 = u8::min(); - let u8_2: u8 = 1u8; - let u8_3: u8 = u8::max(); - - let u32_1: u32 = u8_1.into(); - let u32_2: u32 = u8_2.into(); - let u32_3: u32 = u8_3.into(); - - assert(u32_1 == 0u32); - assert(u32_2 == 1u32); - assert(u32_3 == 255u32); -} - -#[test] -fn u32_from_u16() { - let u16_1: u16 = u16::min(); - let u16_2: u16 = 1u16; - let u16_3: u16 = u16::max(); - - let u32_1 = u32::from(u16_1); - let u32_2 = u32::from(u16_2); - let u32_3 = u32::from(u16_3); - - assert(u32_1 == 0u32); - assert(u32_2 == 1u32); - assert(u32_3 == 65535u32); -} - -#[test] -fn u32_into_u16() { - let u16_1: u16 = u16::min(); - let u16_2: u16 = 1u16; - let u16_3: u16 = u16::max(); - - let u32_1: u32 = u16_1.into(); - let u32_2: u32 = u16_2.into(); - let u32_3: u32 = u16_3.into(); - - assert(u32_1 == 0u32); - assert(u32_2 == 1u32); - assert(u32_3 == 65535u32); -} - -#[test] -fn u32_try_from_u64() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = 2u64; - let u64_3: u64 = u32::max().as_u64(); - let u64_4: u64 = u32::max().as_u64() + 1; - - let u32_1 = u32::try_from(u64_1); - let u32_2 = u32::try_from(u64_2); - let u32_3 = u32::try_from(u64_3); - let u32_4 = u32::try_from(u64_4); - - assert(u32_1.is_some()); - assert(u32_1.unwrap() == 0u32); - - assert(u32_2.is_some()); - assert(u32_2.unwrap() == 2u32); - - assert(u32_3.is_some()); - assert(u32_3.unwrap() == u32::max()); - - assert(u32_4.is_none()); -} - -#[test] -fn u32_try_from_u256() { - let u256_1: u256 = 0x0000000000000000000000000000000000000000000000000000000000000000u256; - let u256_2: u256 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; - let u256_3: u256 = u32::max().as_u256(); - let u256_4: u256 = u32::max().as_u256() + 1; - - let u32_1 = u32::try_from(u256_1); - let u32_2 = u32::try_from(u256_2); - let u32_3 = u32::try_from(u256_3); - let u32_4 = u32::try_from(u256_4); - - assert(u32_1.is_some()); - assert(u32_1.unwrap() == 0u32); - - assert(u32_2.is_some()); - assert(u32_2.unwrap() == 2u32); - - assert(u32_3.is_some()); - assert(u32_3.unwrap() == u32::max()); - - assert(u32_4.is_none()); -} - -#[test] -fn u32_try_from_u128() { - let u128_1: U128 = U128::new(); - let u128_2: U128 = U128::from((0u64, 2u32.as_u64())); - let u128_3: U128 = U128::from((0u64, u32::max().as_u64())); - let u128_4: U128 = U128::from((0, u32::max().as_u64() + 1)); - - let u32_1 = u32::try_from(u128_1); - let u32_2 = u32::try_from(u128_2); - let u32_3 = u32::try_from(u128_3); - let u32_4 = u32::try_from(u128_4); - - assert(u32_1.is_some()); - assert(u32_1.unwrap() == 0u32); - - assert(u32_2.is_some()); - assert(u32_2.unwrap() == 2u32); - - assert(u32_3.is_some()); - assert(u32_3.unwrap() == u32::max()); - - assert(u32_4.is_none()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/Forc.toml deleted file mode 100644 index e44d33356eb..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "primitive_conversions_u64_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/src/main.sw deleted file mode 100644 index e82b953c1ab..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/src/main.sw +++ /dev/null @@ -1,141 +0,0 @@ -library; - -use std::{primitive_conversions::u64::*, u128::U128}; - -#[test] -fn u64_from_u8() { - let u8_1: u8 = 0u8; - let u8_2: u8 = 2u8; - let u8_3: u8 = u8::max(); - - let u64_1 = u64::from(u8_1); - let u64_2 = u64::from(u8_2); - let u64_3 = u64::from(u8_3); - - assert(u64_1 == 0u64); - assert(u64_2 == 2u64); - assert(u64_3 == 255u64); -} - -#[test] -fn u64_into_u8() { - let u8_1: u8 = 0u8; - let u8_2: u8 = 2u8; - let u8_3: u8 = u8::max(); - - let u64_1: u64 = u8_1.into(); - let u64_2: u64 = u8_2.into(); - let u64_3: u64 = u8_3.into(); - - assert(u64_1 == 0u64); - assert(u64_2 == 2u64); - assert(u64_3 == 255u64); -} - -#[test] -fn u64_from_u16() { - let u16_1: u16 = u16::min(); - let u16_2: u16 = 2u16; - let u16_3: u16 = u16::max(); - - let u64_1 = u64::from(u16_1); - let u64_2 = u64::from(u16_2); - let u64_3 = u64::from(u16_3); - - assert(u64_1 == 0u64); - assert(u64_2 == 2u64); - assert(u64_3 == 65535u64); -} - -#[test] -fn u64_into_u16() { - let u16_1: u16 = u16::min(); - let u16_2: u16 = 2u16; - let u16_3: u16 = u16::max(); - - let u64_1: u64 = u16_1.into(); - let u64_2: u64 = u16_2.into(); - let u64_3: u64 = u16_3.into(); - - assert(u64_1 == 0u64); - assert(u64_2 == 2u64); - assert(u64_3 == 65535u64); -} - -#[test] -fn u64_from_u32() { - let u32_1: u32 = 0u32; - let u32_2: u32 = 2u32; - let u32_3: u32 = u32::max(); - - let u64_1 = u64::from(u32_1); - let u64_2 = u64::from(u32_2); - let u64_3 = u64::from(u32_3); - - assert(u64_1 == 0u64); - assert(u64_2 == 2u64); - assert(u64_3 == 4294967295u64); -} - -#[test] -fn u64_into_u32() { - let u32_1: u32 = 0u32; - let u32_2: u32 = 2u32; - let u32_3: u32 = u32::max(); - - let u64_1: u64 = u32_1.into(); - let u64_2: u64 = u32_2.into(); - let u64_3: u64 = u32_3.into(); - - assert(u64_1 == 0u64); - assert(u64_2 == 2u64); - assert(u64_3 == 4294967295u64); -} - -#[test] -fn u64_try_from_u256() { - let u256_1 = 0x0000000000000000000000000000000000000000000000000000000000000000u256; - let u256_2 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; - let u256_3 = u64::max().as_u256(); - let u256_4 = u64::max().as_u256() + 1; - - let u64_1 = u64::try_from(u256_1); - let u64_2 = u64::try_from(u256_2); - let u64_3 = u64::try_from(u256_3); - let u64_4 = u64::try_from(u256_4); - - assert(u64_1.is_some()); - assert(u64_1.unwrap() == 0); - - assert(u64_2.is_some()); - assert(u64_2.unwrap() == 2); - - assert(u64_3.is_some()); - assert(u64_3.unwrap() == u64::max()); - - assert(u64_4.is_none()); -} - -#[test] -fn u64_try_from_u128() { - let u128_1: U128 = U128::new(); - let u128_2: U128 = U128::from((0, 2)); - let u128_3: U128 = U128::from((0, u64::max())); - let u128_4: U128 = U128::from((1, 0)); - - let u64_1 = u64::try_from(u128_1); - let u64_2 = u64::try_from(u128_2); - let u64_3 = u64::try_from(u128_3); - let u64_4 = u64::try_from(u128_4); - - assert(u64_1.is_some()); - assert(u64_1.unwrap() == 0u64); - - assert(u64_2.is_some()); - assert(u64_2.unwrap() == 2u64); - - assert(u64_3.is_some()); - assert(u64_3.unwrap() == u64::max()); - - assert(u64_4.is_none()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/Forc.toml deleted file mode 100644 index 11ba8b72886..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "primitive_conversions_u8_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/src/main.sw deleted file mode 100644 index 0fa64a82e6a..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/src/main.sw +++ /dev/null @@ -1,123 +0,0 @@ -library; - -use std::{primitive_conversions::u8::*, u128::U128}; - -#[test] -fn u8_try_from_u16() { - let u16_1: u16 = u16::min(); - let u16_2: u16 = 2u16; - let u16_3: u16 = u8::max().as_u16(); - let u16_4: u16 = u8::max().as_u16() + 1; - - let u8_1 = u8::try_from(u16_1); - let u8_2 = u8::try_from(u16_2); - let u8_3 = u8::try_from(u16_3); - let u8_4 = u8::try_from(u16_4); - - assert(u8_1.is_some()); - assert(u8_1.unwrap() == u8::min()); - - assert(u8_2.is_some()); - assert(u8_2.unwrap() == 2u8); - - assert(u8_3.is_some()); - assert(u8_3.unwrap() == u8::max()); - - assert(u8_4.is_none()); -} - -#[test] -fn u8_try_from_u32() { - let u32_1: u32 = u32::min(); - let u32_2: u32 = 2u32; - let u32_3: u32 = u8::max().as_u32(); - let u32_4: u32 = u8::max().as_u32() + 1; - - let u8_1 = u8::try_from(u32_1); - let u8_2 = u8::try_from(u32_2); - let u8_3 = u8::try_from(u32_3); - let u8_4 = u8::try_from(u32_4); - - assert(u8_1.is_some()); - assert(u8_1.unwrap() == u8::min()); - - assert(u8_2.is_some()); - assert(u8_2.unwrap() == 2u8); - - assert(u8_3.is_some()); - assert(u8_3.unwrap() == u8::max()); - - assert(u8_4.is_none()); -} - -#[test] -fn u8_try_from_u64() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = 2; - let u64_3: u64 = u8::max().as_u64(); - let u64_4: u64 = u8::max().as_u64() + 1; - - let u8_1 = u8::try_from(u64_1); - let u8_2 = u8::try_from(u64_2); - let u8_3 = u8::try_from(u64_3); - let u8_4 = u8::try_from(u64_4); - - assert(u8_1.is_some()); - assert(u8_1.unwrap() == u8::min()); - - assert(u8_2.is_some()); - assert(u8_2.unwrap() == 2u8); - - assert(u8_3.is_some()); - assert(u8_3.unwrap() == u8::max()); - - assert(u8_4.is_none()); -} - -#[test] -fn u8_try_from_u256() { - let u256_1: u256 = u256::min(); - let u256_2: u256 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; - let u256_3: u256 = u8::max().as_u256(); - let u256_4: u256 = u8::max().as_u256() + 1; - - let u8_1 = u8::try_from(u256_1); - let u8_2 = u8::try_from(u256_2); - let u8_3 = u8::try_from(u256_3); - let u8_4 = u8::try_from(u256_4); - - assert(u8_1.is_some()); - assert(u8_1.unwrap() == u8::min()); - - assert(u8_2.is_some()); - assert(u8_2.unwrap() == 2u8); - - assert(u8_3.is_some()); - assert(u8_3.unwrap() == u8::max()); - - assert(u8_4.is_none()); -} - -#[test] -fn u8_try_from_u128() { - let u128_1: U128 = U128::zero(); - let u128_2: U128 = U128::from((0, 2u8.as_u64())); - let u128_3: U128 = U128::from((0, u8::max().as_u64())); - let u128_4: U128 = U128::from((0, u8::max().as_u64() + 1)); - - let u8_1 = u8::try_from(u128_1); - let u8_2 = u8::try_from(u128_2); - let u8_3 = u8::try_from(u128_3); - let u8_4 = u8::try_from(u128_4); - - assert(u8_1.is_some()); - assert(u8_1.unwrap() == 0u8); - - assert(u8_2.is_some()); - assert(u8_2.unwrap() == 2u8); - - assert(u8_3.is_some()); - assert(u8_3.unwrap() == u8::max()); - - assert(u8_4.is_none()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/Forc.toml deleted file mode 100644 index be492595367..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "result_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/src/main.sw deleted file mode 100644 index 2166d4b643b..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/result_inline_tests/src/main.sw +++ /dev/null @@ -1,194 +0,0 @@ -library; - -#[test] -fn result_is_ok() { - use std::bytes::Bytes; - - let u8_1: Result = Ok(1u8); - let u16_1: Result = Ok(1u16); - let u32_1: Result = Ok(1u32); - let u64_1: Result = Ok(1u64); - let u256_1: Result = Ok(0x01u256); - let b256_1: Result = Ok(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1: Result = Ok(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1: Result = Ok(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let array_1: Result<[u64; 2], str> = Ok([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1: Result = Ok(bytes_1); - let err_1: Result = Err(0u64); - - assert(u8_1.is_ok()); - assert(u16_1.is_ok()); - assert(u32_1.is_ok()); - assert(u64_1.is_ok()); - assert(u256_1.is_ok()); - assert(b256_1.is_ok()); - assert(struct_1.is_ok()); - assert(enum_1.is_ok()); - assert(array_1.is_ok()); - assert(heap_1.is_ok()); - assert(!err_1.is_ok()); -} - -#[test] -fn result_is_err() { - use std::bytes::Bytes; - - let u8_1: Result = Ok(1u8); - let u16_1: Result = Ok(1u16); - let u32_1: Result = Ok(1u32); - let u64_1: Result = Ok(1u64); - let u256_1: Result = Ok(0x01u256); - let b256_1: Result = Ok(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1: Result = Ok(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1: Result = Ok(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let array_1: Result<[u64; 2], str> = Ok([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1: Result = Ok(bytes_1); - let err_1: Result = Err(0u64); - - assert(!u8_1.is_err()); - assert(!u16_1.is_err()); - assert(!u32_1.is_err()); - assert(!u64_1.is_err()); - assert(!u256_1.is_err()); - assert(!b256_1.is_err()); - assert(!struct_1.is_err()); - assert(!enum_1.is_err()); - assert(!array_1.is_err()); - assert(!heap_1.is_err()); - assert(err_1.is_err()); -} - -#[test] -fn result_unwrap() { - use std::bytes::Bytes; - - let u8_1: Result = Ok(1u8); - let u16_1: Result = Ok(1u16); - let u32_1: Result = Ok(1u32); - let u64_1: Result = Ok(1u64); - let u256_1: Result = Ok(0x01u256); - let b256_1: Result = Ok(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1: Result = Ok(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1: Result = Ok(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let _array_1: Result<[u64; 2], str> = Ok([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1: Result = Ok(bytes_1); - - assert(u8_1.unwrap() == 1u8); - assert(u16_1.unwrap() == 1u16); - assert(u32_1.unwrap() == 1u32); - assert(u64_1.unwrap() == 1u64); - assert(u256_1.unwrap() == 0x01u256); - assert( - b256_1 - .unwrap() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - assert( - struct_1 - .unwrap() == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), - ); - assert( - enum_1 - .unwrap() == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), - ); - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // assert(array_1.unwrap() == [0u64, 0u64]); - assert(heap_1.unwrap() == bytes_1); -} - -#[test(should_revert)] -fn revert_result_when_unwrap_none() { - let err_1: Result = Err(0u64); - let _result = err_1.unwrap(); -} - -#[test] -fn result_unwrap_or() { - use std::bytes::Bytes; - - let u8_1: Result = Ok(1u8); - let u16_1: Result = Ok(1u16); - let u32_1: Result = Ok(1u32); - let u64_1: Result = Ok(1u64); - let u256_1: Result = Ok(0x01u256); - let b256_1: Result = Ok(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1: Result = Ok(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1: Result = Ok(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let _array_1: Result<[u64; 2], str> = Ok([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1: Result = Ok(bytes_1); - let err_1: Result = Err(0u64); - - assert(u8_1.unwrap_or(2u8) == 1u8); - assert(u16_1.unwrap_or(2u16) == 1u16); - assert(u32_1.unwrap_or(2u32) == 1u32); - assert(u64_1.unwrap_or(2u64) == 1u64); - assert(u256_1.unwrap_or(0x02u256) == 0x01u256); - assert( - b256_1 - .unwrap_or(0x0000000000000000000000000000000000000000000000000000000000000002) == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - assert( - struct_1 - .unwrap_or(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)) == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), - ); - assert( - enum_1 - .unwrap_or(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000002))) == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), - ); - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // assert(array_1.unwrap_or([1u64, 1u64]) == [0u64, 0u64]); - assert(heap_1.unwrap_or(Bytes::new()) == bytes_1); - assert(err_1.unwrap_or(10u64) == 10u64); -} - -#[test] -fn result_expect() { - use std::bytes::Bytes; - - let u8_1: Result = Ok(1u8); - let u16_1: Result = Ok(1u16); - let u32_1: Result = Ok(1u32); - let u64_1: Result = Ok(1u64); - let u256_1: Result = Ok(0x01u256); - let b256_1: Result = Ok(0x0000000000000000000000000000000000000000000000000000000000000001); - let struct_1: Result = Ok(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)); - let enum_1: Result = Ok(Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001))); - let _array_1: Result<[u64; 2], str> = Ok([0u64, 0u64]); - let mut bytes_1 = Bytes::new(); - bytes_1.push(1u8); - let heap_1: Result = Ok(bytes_1); - - assert(u8_1.expect("Failed Test") == 1u8); - assert(u16_1.expect("Failed Test") == 1u16); - assert(u32_1.expect("Failed Test") == 1u32); - assert(u64_1.expect("Failed Test") == 1u64); - assert(u256_1.expect("Failed Test") == 0x01u256); - assert( - b256_1 - .expect("Failed Test") == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - assert( - struct_1 - .expect("Failed Test") == Address::from(0x0000000000000000000000000000000000000000000000000000000000000001), - ); - assert( - enum_1 - .expect("Failed Test") == Identity::Address(Address::from(0x0000000000000000000000000000000000000000000000000000000000000001)), - ); - // TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6086 is resolved - // assert(array_1.expect("Failed Test") == [0u64, 0u64]); - assert(heap_1.expect("Failed Test") == bytes_1); -} - -#[test(should_revert)] -fn revert_result_expect_when_none() { - let err_1: Result = Err(0u64); - let _result = err_1.expect("Failed Test"); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/Forc.toml deleted file mode 100644 index 4d01e7378cb..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "revert_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/src/main.sw deleted file mode 100644 index cff1e49fc0e..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/revert_inline_tests/src/main.sw +++ /dev/null @@ -1,16 +0,0 @@ -library; - -#[test(should_revert)] -fn revert_revert() { - revert(0); -} - -#[test(should_revert)] -fn revert_revert_require() { - require(false, "error"); -} - -#[test] -fn pass_revert_require() { - require(true, "error"); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/Forc.toml deleted file mode 100644 index fc32fcd089e..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "string_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/src/main.sw deleted file mode 100644 index 079a28ee78d..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/string_inline_tests/src/main.sw +++ /dev/null @@ -1,382 +0,0 @@ -library; - -use std::{bytes::Bytes, string::String}; - -#[test] -fn string_as_bytes() { - let mut string = String::new(); - - let bytes = string.as_bytes(); - assert(bytes.len() == 0); - assert(bytes.capacity() == string.capacity()); - - let mut bytes = Bytes::new(); - bytes.push(0u8); - let string = String::from_ascii(bytes); - - let bytes = string.as_bytes(); - assert(bytes.len() == 1); - assert(bytes.capacity() == string.capacity()); -} - -#[test] -fn string_capacity() { - let mut string = String::new(); - - assert(string.capacity() == 0); - - let mut bytes = Bytes::new(); - bytes.push(0u8); - let string = String::from_ascii(bytes); - assert(string.capacity() == 1); -} - -#[test] -fn string_clear() { - // Clear non-empty - let mut bytes = Bytes::new(); - bytes.push(0u8); - let mut string = String::from_ascii(bytes); - assert(!string.is_empty()); - - string.clear(); - assert(string.is_empty()); -} - -#[test] -fn string_clear_empty() { - let mut string = String::new(); - - assert(string.is_empty()); - string.clear(); - assert(string.is_empty()); -} - -#[test] -fn string_from_ascii() { - let mut bytes = Bytes::new(); - bytes.push(0u8); - bytes.push(1u8); - bytes.push(2u8); - bytes.push(3u8); - bytes.push(4u8); - - let mut string_from_ascii = String::from_ascii(bytes); - assert(bytes.len() == string_from_ascii.capacity()); - - let bytes = string_from_ascii.as_bytes(); - assert(bytes.get(0).unwrap() == 0u8); - assert(bytes.get(1).unwrap() == 1u8); - assert(bytes.get(2).unwrap() == 2u8); - assert(bytes.get(3).unwrap() == 3u8); - assert(bytes.get(4).unwrap() == 4u8); - assert(bytes.get(5) == None); -} - -#[test] -fn string_from_ascii_str() { - let mut string_from_ascii = String::from_ascii_str("ABCDEF"); - assert(string_from_ascii.capacity() == 6); - let bytes = string_from_ascii.as_bytes(); - assert(bytes.get(0).unwrap() == 65u8); - assert(bytes.get(1).unwrap() == 66u8); - assert(bytes.get(2).unwrap() == 67u8); - assert(bytes.get(3).unwrap() == 68u8); - assert(bytes.get(4).unwrap() == 69u8); - assert(bytes.get(5).unwrap() == 70u8); - assert(bytes.get(6).is_none()); -} - -#[test] -fn string_is_empty() { - let mut string = String::new(); - - assert(string.is_empty()); - - let mut bytes = Bytes::new(); - bytes.push(0u8); - let string = String::from_ascii(bytes); - assert(!string.is_empty()); - - let mut bytes = Bytes::new(); - bytes.push(0u8); - bytes.push(1u8); - let mut string = String::from_ascii(bytes); - assert(!string.is_empty()); - - string.clear(); - assert(string.is_empty()); -} - -#[test] -fn string_new() { - let mut string = String::new(); - - assert(string.is_empty()); - assert(string.capacity() == 0); -} - -#[test] -fn string_with_capacity() { - let mut iterator = 0; - - while iterator < 16 { - let mut string = String::with_capacity(iterator); - assert(string.capacity() == iterator); - iterator += 1; - } - - let mut string = String::with_capacity(0); - assert(string.capacity() == 0); - - string.clear(); - assert(string.capacity() == 0); - let mut string = String::with_capacity(4); - - assert(string.capacity() == 4); -} - -#[test] -fn string_ptr() { - let string = String::new(); - assert(!string.ptr().is_null()); - - let mut bytes = Bytes::new(); - bytes.push(0u8); - bytes.push(1u8); - bytes.push(2u8); - bytes.push(3u8); - bytes.push(4u8); - - let mut string_from_ascii = String::from_ascii(bytes); - assert(!string_from_ascii.ptr().is_null()); - assert(string_from_ascii.ptr() != bytes.ptr()); -} - -#[test] -fn string_from_bytes() { - let mut bytes = Bytes::new(); - - bytes.push(0u8); - bytes.push(1u8); - bytes.push(2u8); - bytes.push(3u8); - bytes.push(4u8); - - let mut string_from_bytes = String::from(bytes); - let bytes = string_from_bytes.as_bytes(); - assert(bytes.len() == 5); - assert(bytes.capacity() == string_from_bytes.capacity()); - assert(bytes.get(0).unwrap() == 0u8); - assert(bytes.get(1).unwrap() == 1u8); - assert(bytes.get(2).unwrap() == 2u8); -} - -#[test] -fn string_into_bytes() { - let mut string = String::new(); - - let bytes: Bytes = string.into(); - assert(bytes.len() == 0); - assert(bytes.capacity() == string.capacity()); - - let mut bytes = Bytes::new(); - bytes.push(0u8); - let string = String::from_ascii(bytes); - let bytes: Bytes = string.into(); - assert(bytes.len() == 1); - assert(bytes.capacity() == string.capacity()); - assert(bytes.get(0).unwrap() == 0u8); - - let mut bytes = Bytes::new(); - bytes.push(0u8); - bytes.push(1u8); - let string = String::from_ascii(bytes); - let mut bytes: Bytes = string.into(); - assert(bytes.len() == 2); - assert(bytes.capacity() == string.capacity()); - assert(bytes.get(1).unwrap() == 1u8); -} - -#[test] -fn string_bytes_from() { - let mut bytes = Bytes::new(); - - bytes.push(0u8); - bytes.push(1u8); - bytes.push(2u8); - bytes.push(3u8); - bytes.push(4u8); - - let mut string_from_bytes = String::from(bytes); - - let bytes = Bytes::from(string_from_bytes); - assert(bytes.len() == 5); - assert(bytes.capacity() == string_from_bytes.capacity()); - assert(bytes.get(0).unwrap() == 0u8); - assert(bytes.get(1).unwrap() == 1u8); - assert(bytes.get(2).unwrap() == 2u8); -} - -#[test] -fn string_bytes_into() { - let mut bytes = Bytes::new(); - - bytes.push(0u8); - bytes.push(1u8); - bytes.push(2u8); - bytes.push(3u8); - bytes.push(4u8); - - let mut string_from_bytes: String = bytes.into(); - - let bytes: Bytes = string_from_bytes.as_bytes(); - assert(bytes.len() == 5); - assert(bytes.capacity() == string_from_bytes.capacity()); - assert(bytes.get(0).unwrap() == 0u8); - assert(bytes.get(1).unwrap() == 1u8); - assert(bytes.get(2).unwrap() == 2u8); -} - -#[test] -fn string_as_raw_slice() { - let mut bytes = Bytes::new(); - - bytes.push(0u8); - bytes.push(1u8); - bytes.push(2u8); - bytes.push(3u8); - bytes.push(4u8); - - let raw_slice = bytes.as_raw_slice(); - let mut string = String::from(bytes); - - let string_slice = string.as_raw_slice(); - assert(string_slice.number_of_bytes() == raw_slice.number_of_bytes()); -} - -#[test] -fn string_from_raw_slice() { - let mut bytes = Bytes::new(); - - bytes.push(0u8); - bytes.push(1u8); - bytes.push(2u8); - bytes.push(3u8); - bytes.push(4u8); - - let raw_slice = bytes.as_raw_slice(); - let mut string_from_slice = String::from(raw_slice); - let bytes = string_from_slice.as_bytes(); - assert(bytes.len() == 5); - assert(bytes.get(0).unwrap() == 0u8); - assert(bytes.get(1).unwrap() == 1u8); - assert(bytes.get(2).unwrap() == 2u8); -} - -#[test] -fn string_into_raw_slice() { - // Glob operator needed for From for raw_slice - use std::string::*; - - let mut string = String::new(); - - let raw_slice: raw_slice = string.into(); - assert(raw_slice.number_of_bytes() == 0); - - let mut bytes = Bytes::new(); - bytes.push(0u8); - let string = String::from_ascii(bytes); - let raw_slice = string.as_raw_slice(); - assert(raw_slice.number_of_bytes() == 1); - assert(raw_slice.ptr().read_byte() == 0u8); - - let mut bytes = Bytes::new(); - bytes.push(0u8); - bytes.push(1u8); - let string = String::from_ascii(bytes); - let mut raw_slice = string.as_raw_slice(); - assert(raw_slice.number_of_bytes() == 2); - assert(raw_slice.ptr().add_uint_offset(1).read_byte() == 1u8); - - let mut raw_slice = string.as_raw_slice(); - assert(raw_slice.number_of_bytes() == 2); - assert(raw_slice.ptr().read_byte() == 0u8); -} - -#[test] -fn string_raw_slice_into() { - // Glob operator needed for From for raw_slice - use std::string::*; - - let mut bytes = Bytes::new(); - - bytes.push(0u8); - bytes.push(1u8); - bytes.push(2u8); - bytes.push(3u8); - bytes.push(4u8); - - let raw_slice = bytes.as_raw_slice(); - let mut string = String::from(bytes); - - let string_slice: raw_slice = string.into(); - assert(string_slice.number_of_bytes() == raw_slice.number_of_bytes()); -} - -#[test] -fn string_raw_slice_from() { - // Glob operator needed for From for raw_slice - use std::string::*; - - let mut bytes = Bytes::new(); - - bytes.push(0u8); - bytes.push(1u8); - bytes.push(2u8); - bytes.push(3u8); - bytes.push(4u8); - - let raw_slice = bytes.as_raw_slice(); - let mut string: String = String::from(bytes); - - let string_slice = raw_slice::from(string); - assert(string_slice.number_of_bytes() == raw_slice.number_of_bytes()); -} - -#[test] -fn string_test_equal() { - let string1 = String::from_ascii_str("fuel"); - let string2 = String::from_ascii_str("fuel"); - let string3 = String::from_ascii_str("blazingly fast"); - - assert(string1 == string2); - assert(string1 != string3); -} - -#[test] -fn string_test_hash() { - use std::hash::{Hash, sha256}; - - let mut bytes = Bytes::new(); - bytes.push(0u8); - - let string = String::from(bytes); - - assert(sha256(string) == sha256(bytes)); -} - -#[test] -fn string_test_abi_encoding() { - let string = String::from_ascii_str("fuel"); - - let buffer = Buffer::new(); - let encoded_string = string.abi_encode(buffer); - - let encoded_raw_slice = encoded_string.as_raw_slice(); - let mut buffer_reader = BufferReader::from_parts(encoded_raw_slice.ptr(), encoded_raw_slice.number_of_bytes()); - - let decoded_string = String::abi_decode(buffer_reader); - - assert(string == decoded_string); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/Forc.toml deleted file mode 100644 index c218c1f64cd..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "u128_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/src/main.sw deleted file mode 100644 index c7fc8405215..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/u128_inline_tests/src/main.sw +++ /dev/null @@ -1,744 +0,0 @@ -library; - -use std::u128::U128; - -#[test] -fn u128_from_u8() { - let u8_1: u8 = u8::min(); - let u8_2: u8 = u8::max(); - let u8_3: u8 = 1u8; - - let u128_1 = >::from(u8_1); - let u128_2 = >::from(u8_2); - let u128_3 = >::from(u8_3); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 255u64); - assert(u128_3.as_u64().unwrap() == 1u64); -} - -#[test] -fn u128_u8_into() { - let u8_1: u8 = u8::min(); - let u8_2: u8 = u8::max(); - let u8_3: u8 = 1u8; - - let u128_1: U128 = u8_1.into(); - let u128_2: U128 = u8_2.into(); - let u128_3: U128 = u8_3.into(); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 255u64); - assert(u128_3.as_u64().unwrap() == 1u64); -} - -#[test] -fn u128_from_u16() { - let u16_1: u16 = u16::min(); - let u16_2: u16 = u16::max(); - let u16_3: u16 = 1u16; - - let u128_1 = >::from(u16_1); - let u128_2 = >::from(u16_2); - let u128_3 = >::from(u16_3); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 65535u64); - assert(u128_3.as_u64().unwrap() == 1u64); -} - -#[test] -fn u128_u16_into() { - let u16_1: u16 = u16::min(); - let u16_2: u16 = u16::max(); - let u16_3: u16 = 1u16; - - let u128_1: U128 = u16_1.into(); - let u128_2: U128 = u16_2.into(); - let u128_3: U128 = u16_3.into(); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 65535u64); - assert(u128_3.as_u64().unwrap() == 1u64); -} - -#[test] -fn u128_from_u32() { - let u32_1: u32 = u32::min(); - let u32_2: u32 = u32::max(); - let u32_3: u32 = 1u32; - - let u128_1 = >::from(u32_1); - let u128_2 = >::from(u32_2); - let u128_3 = >::from(u32_3); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 4294967295u64); - assert(u128_3.as_u64().unwrap() == 1u64); -} - -#[test] -fn u128_u32_into() { - let u32_1: u32 = u32::min(); - let u32_2: u32 = u32::max(); - let u32_3: u32 = 1u32; - - let u128_1: U128 = u32_1.into(); - let u128_2: U128 = u32_2.into(); - let u128_3: U128 = u32_3.into(); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 4294967295u64); - assert(u128_3.as_u64().unwrap() == 1u64); -} - -#[test] -fn u128_from_u64() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = u64::max(); - let u64_3: u64 = 1u64; - - let u128_1 = >::from(u64_1); - let u128_2 = >::from(u64_2); - let u128_3 = >::from(u64_3); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 18446744073709551615u64); - assert(u128_3.as_u64().unwrap() == 1u64); -} - -#[test] -fn u128_u64_into() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = u64::max(); - let u64_3: u64 = 1u64; - - let u128_1: U128 = u64_1.into(); - let u128_2: U128 = u64_2.into(); - let u128_3: U128 = u64_3.into(); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 18446744073709551615u64); - assert(u128_3.as_u64().unwrap() == 1u64); -} - -#[test] -fn u128_from_u64_tuple() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = u64::max(); - let u64_3: u64 = 1u64; - - let u128_1 = >::from((u64_1, u64_1)); - let u128_2 = >::from((u64_2, u64_2)); - let u128_3 = >::from((u64_3, u64_3)); - - assert(u128_1.upper() == 0u64); - assert(u128_1.lower() == 0u64); - assert(u128_2.upper() == 18446744073709551615u64); - assert(u128_2.lower() == 18446744073709551615u64); - assert(u128_3.upper() == 1u64); - assert(u128_3.lower() == 1u64); -} - -#[test] -fn u128_into_u64_tuple() { - // Glob operator needed for U128.into() - use std::u128::*; - - let u128_1 = U128::from((0u64, 0u64)); - let u128_2 = U128::from((18446744073709551615u64, 18446744073709551615u64)); - let u128_3 = U128::from((1u64, 1u64)); - - let u64_1: (u64, u64) = u128_1.into(); - let u64_2: (u64, u64) = u128_2.into(); - let u64_3: (u64, u64) = u128_3.into(); - - assert(u64_1.0 == u64::min()); - assert(u64_1.1 == u64::min()); - assert(u64_2.0 == u64::max()); - assert(u64_2.1 == u64::max()); - assert(u64_3.0 == 1u64); - assert(u64_3.1 == 1u64); -} - -#[test] -fn u128_u64_tuple_into() { - // Glob operator needed for From for (u64, u64) - use std::u128::*; - - let u64_1: (u64, u64) = (u64::min(), u64::min()); - let u64_2: (u64, u64) = (u64::max(), u64::max()); - let u64_3: (u64, u64) = (1u64, 1u64); - - let u128_1: U128 = u64_1.into(); - let u128_2: U128 = u64_2.into(); - let u128_3: U128 = u64_3.into(); - - assert(u128_1.upper() == 0u64); - assert(u128_1.lower() == 0u64); - assert(u128_2.upper() == 18446744073709551615u64); - assert(u128_2.lower() == 18446744073709551615u64); - assert(u128_3.upper() == 1u64); - assert(u128_3.lower() == 1u64); -} - -#[test] -fn u128_u64_tuple_from() { - // Glob operator needed for From for (u64, u64) - use std::u128::*; - - let u128_1 = U128::from((0u64, 0u64)); - let u128_2 = U128::from((18446744073709551615u64, 18446744073709551615u64)); - let u128_3 = U128::from((1u64, 1u64)); - - let u64_1: (u64, u64) = <(u64, u64) as From>::from(u128_1); - let u64_2: (u64, u64) = <(u64, u64) as From>::from(u128_2); - let u64_3: (u64, u64) = <(u64, u64) as From>::from(u128_3); - - assert(u64_1.0 == u64::min()); - assert(u64_1.1 == u64::min()); - assert(u64_2.0 == u64::max()); - assert(u64_2.1 == u64::max()); - assert(u64_3.0 == 1u64); - assert(u64_3.1 == 1u64); -} - -#[test] -fn u128_eq() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = u64::max(); - let u64_3: u64 = 1u64; - - let u128_1 = >::from((u64_1, u64_1)); - let u128_2 = >::from((u64_1, u64_1)); - let u128_3 = >::from((u64_2, u64_2)); - let u128_4 = >::from((u64_2, u64_2)); - let u128_5 = >::from((u64_3, u64_3)); - let u128_6 = >::from((u64_3, u64_3)); - - assert(u128_1 == u128_2); - assert(u128_3 == u128_4); - assert(u128_5 == u128_6); -} - -#[test] -fn u128_ne() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = u64::max(); - let u64_3: u64 = 1u64; - - let u128_1 = >::from((u64_1, u64_1)); - let u128_2 = >::from((u64_1, u64_2)); - let u128_3 = >::from((u64_2, u64_2)); - let u128_4 = >::from((u64_2, u64_3)); - let u128_5 = >::from((u64_3, u64_3)); - let u128_6 = >::from((u64_3, u64_1)); - - assert(u128_1 != u128_2); - assert(u128_1 != u128_3); - assert(u128_1 != u128_4); - assert(u128_1 != u128_5); - assert(u128_1 != u128_6); - - assert(u128_2 != u128_3); - assert(u128_2 != u128_4); - assert(u128_2 != u128_5); - assert(u128_2 != u128_6); - - assert(u128_3 != u128_4); - assert(u128_3 != u128_5); - assert(u128_3 != u128_6); - - assert(u128_4 != u128_5); - assert(u128_4 != u128_6); - - assert(u128_5 != u128_6); -} - -#[test] -fn u128_ord() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = u64::max(); - let u64_3: u64 = 1u64; - - let u128_1 = >::from((u64_1, u64_1)); // 0, 0 - let u128_2 = >::from((u64_1, u64_3)); // 0, 1 - let u128_3 = >::from((u64_3, u64_1)); // 1, 0 - let u128_4 = >::from((u64_2, u64_2)); // max, max - let u128_5 = >::from((u64_3, u64_2)); // 1, max - let u128_6 = >::from((u64_2, u64_1)); // max, 0 - let u128_7 = >::from((u64_3, u64_3)); // 1, 1 - let u128_8 = >::from((u64_2, u64_3)); // max, 1 - let u128_9 = >::from((u64_1, u64_2)); // 0, max - assert(u128_1 < u128_2); - assert(u128_3 > u128_1); - assert(u128_3 > u128_2); - - assert(u128_4 > u128_1); - assert(u128_4 > u128_2); - assert(u128_4 > u128_3); - assert(u128_4 > u128_5); - assert(u128_4 > u128_6); - assert(u128_4 > u128_7); - assert(u128_4 > u128_8); - assert(u128_4 > u128_9); - - assert(u128_5 > u128_1); - assert(u128_5 > u128_2); - assert(u128_5 > u128_3); - assert(u128_5 < u128_6); - assert(u128_5 > u128_7); - assert(u128_5 < u128_8); - assert(u128_5 > u128_9); - - assert(u128_6 > u128_1); - assert(u128_6 > u128_2); - assert(u128_6 > u128_3); - assert(u128_6 > u128_7); - assert(u128_6 < u128_8); - assert(u128_6 > u128_9); - - assert(u128_7 > u128_1); - assert(u128_7 > u128_2); - assert(u128_7 > u128_3); -} - -#[test] -fn u128_new() { - let new_u128 = U128::new(); - assert(new_u128.upper() == 0); - assert(new_u128.lower() == 0); -} - -#[test] -fn u128_as_u64() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = u64::max(); - let u64_3: u64 = 1u64; - - let u128_1 = >::from(u64_1); - let u128_2 = >::from(u64_2); - let u128_3 = >::from(u64_3); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 18446744073709551615u64); - assert(u128_3.as_u64().unwrap() == 1u64); - - let u128_4 = >::from((u64_3, u64_1)); - let u128_5 = >::from((u64_2, u64_1)); - let u128_6 = >::from((u64_2, u64_2)); - - assert(u128_4.as_u64().is_err()); - assert(u128_5.as_u64().is_err()); - assert(u128_6.as_u64().is_err()); -} - -#[test] -fn u128_min() { - let new_u128 = U128::min(); - assert(new_u128.upper() == 0); - assert(new_u128.lower() == 0); -} - -#[test] -fn u128_max() { - let new_u128 = U128::max(); - assert(new_u128.upper() == u64::max()); - assert(new_u128.lower() == u64::max()); -} - -#[test] -fn u128_bits() { - assert(U128::bits() == 128u32); -} - -#[test] -fn u128_upper() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = u64::max(); - let u64_3: u64 = 1u64; - - let u128_1 = >::from((u64_1, u64_1)); - let u128_2 = >::from((u64_2, u64_2)); - let u128_3 = >::from((u64_3, u64_3)); - - assert(u128_1.upper() == 0u64); - assert(u128_2.upper() == 18446744073709551615u64); - assert(u128_3.upper() == 1u64); -} - -#[test] -fn u128_lower() { - let u64_1: u64 = u64::min(); - let u64_2: u64 = u64::max(); - let u64_3: u64 = 1u64; - - let u128_1 = >::from((u64_1, u64_1)); - let u128_2 = >::from((u64_2, u64_2)); - let u128_3 = >::from((u64_3, u64_3)); - - assert(u128_1.lower() == 0u64); - assert(u128_2.lower() == 18446744073709551615u64); - assert(u128_3.lower() == 1u64); -} - -#[test] -fn u128_zero() { - let zero_u128 = U128::zero(); - assert(zero_u128.as_u64().unwrap() == 0u64); -} - -#[test] -fn u128_is_zero() { - let zero_u128 = U128::zero(); - assert(zero_u128.is_zero()); - - let other1_u128 = U128::from((0, 1)); - assert(!other1_u128.is_zero()); - - let other2_u128 = U128::from((1, 0)); - assert(!other2_u128.is_zero()); - - let other3_u128 = U128::from((1, 1)); - assert(!other3_u128.is_zero()); - - let other4_u128 = U128::from((0, 0)); - assert(other4_u128.is_zero()); -} - -#[test] -fn u128_bitwise_and() { - let one = U128::from((0, 1)); - let two = U128::from((0, 2)); - let three = U128::from((0, 3)); - let thirteen = U128::from((0, 13)); - let one_upper = U128::from((1, 0)); - let zero = U128::zero(); - - assert(one & two == zero); - assert(one & three == one); - assert(one & thirteen == one); - assert(one & one_upper == zero); - - assert(two & three == two); - assert(two & thirteen == zero); - assert(two & one_upper == zero); - - assert(three & thirteen == one); - assert(three & one_upper == zero); - - assert(thirteen & one_upper == zero); - - assert(one & one == one); - assert(two & two == two); - assert(three & three == three); - assert(thirteen & thirteen == thirteen); - assert(one_upper & one_upper == one_upper); -} - -#[test] -fn u128_bitwise_or() { - let one = U128::from((0, 1)); - let two = U128::from((0, 2)); - let three = U128::from((0, 3)); - let thirteen = U128::from((0, 13)); - let one_upper = U128::from((1, 0)); - - assert(one | two == three); - assert(one | three == three); - assert(one | thirteen == thirteen); - assert(one | one_upper == one_upper + one); - - assert(two | three == three); - assert(two | thirteen == thirteen + two); - assert(two | one_upper == one_upper + two); - - assert(three | thirteen == thirteen + two); - assert(three | one_upper == one_upper + three); - - assert(thirteen | one_upper == one_upper + thirteen); - - assert(one | one == one); - assert(two | two == two); - assert(three | three == three); - assert(thirteen | thirteen == thirteen); - assert(one_upper | one_upper == one_upper); -} - -#[test] -fn u128_shift() { - let one = U128::from((0, 1)); - let one_upper = U128::from((1, 0)); - - let right_shift_one_upper = one_upper >> 1; - assert(right_shift_one_upper.upper() == 0); - assert(right_shift_one_upper.lower() == (1 << 63)); - - let left_shift_one_upper_right_shift = right_shift_one_upper << 1; - assert(left_shift_one_upper_right_shift == one_upper); - - let one_left_shift_64 = one << 64; - assert(one_left_shift_64.upper() == 1); - assert(one_left_shift_64.lower() == 0); - - let three_left_shift_one = U128::from((0, 3)) << 1; - assert(three_left_shift_one.upper() == 0); - assert(three_left_shift_one.lower() == 6); -} - -#[test] -fn u128_not() { - let not_0_3 = !U128::from((0, 3)); - assert(not_0_3.upper() == u64::max()); - assert(not_0_3.lower() == u64::max() - 3); - - let not_3_3 = !U128::from((3, 3)); - assert(not_3_3.upper() == u64::max() - 3); - assert(not_3_3.lower() == u64::max() - 3); - - let not_3_0 = !U128::from((3, 0)); - assert(not_3_0.upper() == u64::max() - 3); - assert(not_3_0.lower() == u64::max()); -} - -#[test] -fn u128_add() { - let first = U128::from((0, 0)); - let second = U128::from((0, 1)); - let max_u64 = U128::from((0, u64::max())); - - let one = first + second; - assert(one.upper() == 0); - assert(one.lower() == 1); - - let two = one + one; - assert(two.upper() == 0); - assert(two.lower() == 2); - - let add_of_one = max_u64 + one; - assert(add_of_one.upper() == 1); - assert(add_of_one.lower() == 0); - - let add_of_two = max_u64 + two; - assert(add_of_two.upper() == 1); - assert(add_of_two.lower() == 1); - - let add_max = max_u64 + max_u64; - assert(add_max.upper() == 1); - assert(add_max.lower() == u64::max() - 1); -} - -#[test(should_revert)] -fn revert_u128_add() { - let one = U128::from((0, 1)); - let max_u64 = U128::from((u64::max(), u64::max())); - - let _result = one + max_u64; -} - -#[test] -fn u128_sub() { - let first = U128::from((0, 0)); - let second = U128::from((0, 1)); - let two = U128::from((0, 2)); - let max_u64 = U128::from((0, u64::max())); - - let sub_one = second - first; - assert(sub_one.upper() == 0); - assert(sub_one.lower() == 1); - - let sub_zero = first - first; - assert(sub_zero.upper() == 0); - assert(sub_zero.lower() == 0); - - let add_of_two = max_u64 + two; - let sub_max_again = add_of_two - two; - assert(sub_max_again.upper() == 0); - assert(sub_max_again.lower() == u64::max()); -} - -#[test(should_revert)] -fn revert_u128_sub() { - let first = U128::from((0, 0)); - let second = U128::from((0, 1)); - - let _result = first - second; -} - -#[test] -fn u128_multiply() { - let two = U128::from((0, 2)); - let max_u64 = U128::from((0, u64::max())); - - let mul_128_of_two = max_u64 * two; - assert(mul_128_of_two.upper() == 1); - assert(mul_128_of_two.lower() == u64::max() - 1); - - let mul_128_of_four = mul_128_of_two * two; - assert(mul_128_of_four.upper() == 3); - assert(mul_128_of_four.lower() == u64::max() - 3); - - let mul_128_max = max_u64 * max_u64; - assert(mul_128_max.upper() == u64::max() - 1); - assert(mul_128_max.lower() == 1); -} - -#[test(should_revert)] -fn revert_u128_multiply() { - let first = U128::from((0, 2)); - let second = U128::from((u64::max(), 1)); - - let _result = first * second; -} - -#[test] -fn u128_divide() { - let two = U128::from((0, 2)); - let max_u64 = U128::from((0, u64::max())); - - let div_max_two = max_u64 / two; - assert(div_max_two.upper() == 0); - assert(div_max_two.lower() == u64::max() >> 1); - - // Product of u64::MAX and u64::MAX. - let dividend = U128::from((u64::max() - 1, 1)); - let div_max_max = dividend / max_u64; - assert(div_max_max.upper() == 0); - assert(div_max_max.lower() == u64::max()); -} - -#[test(should_revert)] -fn revert_u128_divide_by_zero() { - let first = U128::from((0, 1)); - let second = U128::from((0, 0)); - - let _result = first / second; -} - -#[test] -fn u128_pow() { - let mut u_128 = U128::from((0, 7)); - let mut pow_of_u_128 = u_128.pow(2u32); - assert(pow_of_u_128 == U128::from((0, 49))); - - pow_of_u_128 = u_128.pow(3u32); - assert(pow_of_u_128 == U128::from((0, 343))); - - u_128 = U128::from((0, 3)); - pow_of_u_128 = u_128.pow(2u32); - assert(pow_of_u_128 == U128::from((0, 9))); - - u_128 = U128::from((0, 5)); - pow_of_u_128 = u_128.pow(2u32); - assert(pow_of_u_128 == U128::from((0, 25))); - - pow_of_u_128 = u_128.pow(7u32); - assert(pow_of_u_128 == U128::from((0, 78125))); - - u_128 = U128::from((0, 8)); - pow_of_u_128 = u_128.pow(2u32); - assert(pow_of_u_128 == U128::from((0, 64))); - - pow_of_u_128 = u_128.pow(9u32); - assert(pow_of_u_128 == U128::from((0, 134217728))); - - u_128 = U128::from((0, 10)); - pow_of_u_128 = u_128.pow(2u32); - assert(pow_of_u_128 == U128::from((0, 100))); - - pow_of_u_128 = u_128.pow(5u32); - assert(pow_of_u_128 == U128::from((0, 100000))); - - u_128 = U128::from((0, 12)); - pow_of_u_128 = u_128.pow(2u32); - assert(pow_of_u_128 == U128::from((0, 144))); - - pow_of_u_128 = u_128.pow(3u32); - assert(pow_of_u_128 == U128::from((0, 1728))); - - // Test reassignment - u_128 = U128::from((0, 13)); - u_128 = u_128.pow(1u32); - assert(u_128 == U128::from((0, 13))); -} - -#[test] -fn u128_root() { - let mut u_128: U128 = U128::from((0, 49)); - let mut root_of_u_128 = u_128.sqrt(); - - assert(root_of_u_128 == U128::from((0, 7))); - - u_128 = U128::from((0, 25)); - root_of_u_128 = u_128.sqrt(); - assert(root_of_u_128 == U128::from((0, 5))); - - u_128 = U128::from((0, 81)); - root_of_u_128 = u_128.sqrt(); - assert(root_of_u_128 == U128::from((0, 9))); - - u_128 = U128::from((0, 144)); - root_of_u_128 = u_128.sqrt(); - assert(root_of_u_128 == U128::from((0, 12))); - - u_128 = U128::from((0, 1)); - root_of_u_128 = u_128.sqrt(); - assert(root_of_u_128 == U128::from((0, 1))); -} - -#[test] -fn u128_log() { - let u_128_0: U128 = U128::from((0, 0)); - let u_128_1: U128 = U128::from((0, 1)); - let u_128_2: U128 = U128::from((0, 2)); - let u_128_3: U128 = U128::from((0, 3)); - let u_128_6: U128 = U128::from((0, 6)); - let u_128_8: U128 = U128::from((0, 8)); - let u_128_9: U128 = U128::from((0, 9)); - let u_128_10: U128 = U128::from((0, 10)); - let u_128_21: U128 = U128::from((0, 21)); - let u_128_42: U128 = U128::from((0, 42)); - let u_128_64: U128 = U128::from((0, 64)); - let u_128_100: U128 = U128::from((0, 100)); - let u_128_127: U128 = U128::from((0, 127)); - let u_128_max_div_2: U128 = U128::from((1, 0)); - let u_128_max: U128 = U128::max(); - - assert(u_128_2.log(u_128_2) == u_128_1); - assert(u_128_1.log(u_128_3) == u_128_0); - assert(u_128_8.log(u_128_2) == u_128_3); - assert(u_128_100.log(u_128_10) == u_128_2); - assert(u_128_100.log(u_128_2) == u_128_6); - assert(u_128_100.log(u_128_9) == u_128_2); - assert(u_128_max.log(u_128_2) == u_128_127); - assert(u_128_max.log(u_128_9) == u_128_42); - assert(u_128_max_div_2.log(u_128_2) == u_128_64); - assert(u_128_max_div_2.log(u_128_9) == u_128_21); -} - -#[test] -fn u128_binary_log() { - let u_128_1: U128 = U128::from((0, 1)); - let u_128_2: U128 = U128::from((0, 2)); - let u_128_3: U128 = U128::from((0, 3)); - let u_128_6: U128 = U128::from((0, 6)); - let u_128_8: U128 = U128::from((0, 8)); - let u_128_64: U128 = U128::from((0, 64)); - let u_128_127: U128 = U128::from((0, 127)); - let u_128_100: U128 = U128::from((0, 100)); - let u_128_max_div_2: U128 = U128::from((1, 0)); - let u_128_max: U128 = U128::max(); - - assert(u_128_2.log2() == u_128_1); - assert(u_128_8.log2() == u_128_3); - assert(u_128_100.log2() == u_128_6); - assert(u_128_max.log2() == u_128_127); - assert(u_128_max_div_2.log2() == u_128_64); -} - -#[test(should_revert)] -fn revert_u128_binary_log() { - let u_128_0: U128 = U128::from((0, 0)); - - let _result = u_128_0.log2(); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/Forc.toml deleted file mode 100644 index 3481c693acf..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "vec_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/src/main.sw deleted file mode 100644 index 78e2df5d1e5..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/vec_inline_tests/src/main.sw +++ /dev/null @@ -1,704 +0,0 @@ -library; -fn setup() -> (Vec, u64, u64, u64) { - let mut vec: Vec = Vec::new(); - let a = 5u64; - let b = 7u64; - let c = 9u64; - vec.push(a); - vec.push(b); - vec.push(c); - (vec, a, b, c) -} - -#[test] -fn vec_new() { - let new_vec: Vec = Vec::new(); - assert(new_vec.len() == 0); - assert(new_vec.capacity() == 0); -} - -#[test] -fn vec_with_capacity() { - let vec_1: Vec = Vec::with_capacity(0); - assert(vec_1.capacity() == 0); - - let vec_2: Vec = Vec::with_capacity(1); - assert(vec_2.capacity() == 1); - - // 2^6 - let vec_3: Vec = Vec::with_capacity(64); - assert(vec_3.capacity() == 64); - - // 2^11 - let vec_4: Vec = Vec::with_capacity(2048); - assert(vec_4.capacity() == 2048); - - // 2^16 - let vec_5: Vec = Vec::with_capacity(65536); - assert(vec_5.capacity() == 65536); -} - -#[test()] -fn vec_push() { - let mut vec: Vec = Vec::new(); - - assert(vec.len() == 0); - assert(vec.capacity() == 0); - - vec.push(1u64); - assert(vec.len() == 1); - assert(vec.capacity() == 1); - - vec.push(2u64); - assert(vec.len() == 2); - assert(vec.capacity() == 2); - - // Capacity doubles - vec.push(3u64); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - - vec.push(4u64); - assert(vec.len() == 4); - assert(vec.capacity() == 4); - - // Capacity doubles - vec.push(5u64); - assert(vec.len() == 5); - assert(vec.capacity() == 8); - - vec.push(6u64); - assert(vec.len() == 6); - assert(vec.capacity() == 8); - - vec.push(7u64); - assert(vec.len() == 7); - assert(vec.capacity() == 8); - - vec.push(8u64); - assert(vec.len() == 8); - assert(vec.capacity() == 8); - - // Capacity doubles - vec.push(9u64); - assert(vec.len() == 9); - assert(vec.capacity() == 16); -} - -#[test()] -fn vec_capacity() { - let mut vec: Vec = Vec::new(); - assert(vec.capacity() == 0); - - vec.push(5u64); - assert(vec.capacity() == 1); - vec.push(7u64); - assert(vec.capacity() == 2); - vec.push(9u64); - assert(vec.capacity() == 4); - vec.push(11u64); - assert(vec.capacity() == 4); - vec.push(3u64); - assert(vec.capacity() == 8); -} - -#[test()] -fn vec_clear() { - let (mut vec, _, _, _) = setup(); - assert(vec.len() == 3); - - vec.clear(); - assert(vec.len() == 0); -} - -#[test()] -fn vec_clear_twice() { - let (mut vec, _, _, _) = setup(); - - vec.clear(); - assert(vec.len() == 0); - - // Can clean twice - vec.push(1u64); - vec.clear(); - assert(vec.len() == 0); -} - -#[test()] -fn vec_clear_empty_vec() { - // Clear on empty vec - let mut empty_vec: Vec = Vec::new(); - assert(empty_vec.len() == 0); - assert(empty_vec.capacity() == 0); - - empty_vec.clear(); - assert(empty_vec.len() == 0); - assert(empty_vec.capacity() == 0); -} - -#[test()] -fn vec_get() { - let (vec, a, b, c) = setup(); - assert(vec.len() == 3); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).unwrap() == c); - // get is non-modifying - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).unwrap() == c); - assert(vec.len() == 3); - - // None if out of bounds - assert(vec.get(vec.len()).is_none()); -} - -#[test()] -fn vec_len() { - let (mut vec, _, _, _) = setup(); - assert(vec.len() == 3); - - vec.push(5u64); - assert(vec.len() == 4); - vec.push(6u64); - assert(vec.len() == 5); - vec.push(7u64); - assert(vec.len() == 6); - vec.push(8u64); - assert(vec.len() == 7); -} - -#[test] -fn vec_is_empty() { - let (mut setup_vec, _, _, _) = setup(); - - assert(!setup_vec.is_empty()); - - let mut new_vec: Vec = Vec::new(); - assert(new_vec.is_empty()); - - let mut capacity_vec: Vec = Vec::with_capacity(16); - assert(capacity_vec.is_empty()); -} - -#[test()] -fn vec_remove() { - let (mut vec, a, b, c) = setup(); - let d = 7u64; - vec.push(d); - assert(vec.len() == 4); - assert(vec.capacity() == 4); - - // Remove in the middle - let item1 = vec.remove(1); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - assert(item1 == b); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == c); - assert(vec.get(2).unwrap() == d); - assert(vec.get(3).is_none()); -} - -#[test()] -fn vec_remove_front() { - let (mut vec, a, b, c) = setup(); - // Remove at the start - let item = vec.remove(0); - assert(vec.len() == 2); - assert(vec.capacity() == 4); - assert(item == a); - assert(vec.get(0).unwrap() == b); - assert(vec.get(1).unwrap() == c); - assert(vec.get(2).is_none()); -} - -#[test()] -fn vec_remove_end() { - let (mut vec, a, b, c) = setup(); - // Remove at the end - let item = vec.remove(vec.len() - 1); - assert(vec.len() == 2); - assert(vec.capacity() == 4); - assert(item == c); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).is_none()); -} - -#[test()] -fn vec_remove_all() { - let (mut vec, a, b, c) = setup(); - // Remove all - let item1 = vec.remove(0); - let item2 = vec.remove(0); - let item3 = vec.remove(0); - assert(vec.len() == 0); - assert(vec.capacity() == 4); - assert(item1 == a); - assert(item2 == b); - assert(item3 == c); - assert(vec.get(0).is_none()); -} - -#[test(should_revert)] -fn revert_vec_remove_out_of_bounds() { - let (mut vec, _a, _b, _c) = setup(); - - let _result = vec.remove(vec.len()); -} - -#[test()] -fn vec_insert() { - let (mut vec, a, b, c) = setup(); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - - let d = 11u64; - - // Inserts in the middle - vec.insert(1, d); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == d); - assert(vec.get(2).unwrap() == b); - assert(vec.get(3).unwrap() == c); - assert(vec.len() == 4); - assert(vec.capacity() == 4); -} - -#[test()] -fn vec_insert_twice() { - let (mut vec, a, b, c) = setup(); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - - let d = 11u64; - let e = 13u64; - - // Inserts in the middle - vec.insert(1, d); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == d); - assert(vec.get(2).unwrap() == b); - assert(vec.get(3).unwrap() == c); - assert(vec.len() == 4); - assert(vec.capacity() == 4); - - // Twice - vec.insert(1, e); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == e); - assert(vec.get(2).unwrap() == d); - assert(vec.get(3).unwrap() == b); - assert(vec.get(4).unwrap() == c); - assert(vec.len() == 5); - assert(vec.capacity() == 8); -} - -#[test()] -fn vec_insert_front() { - let (mut vec, a, b, c) = setup(); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - - let d = 11u64; - - // Inserts at the front - vec.insert(0, d); - assert(vec.get(0).unwrap() == d); - assert(vec.get(1).unwrap() == a); - assert(vec.get(2).unwrap() == b); - assert(vec.get(3).unwrap() == c); - assert(vec.len() == 4); - assert(vec.capacity() == 4); -} - -#[test()] -fn vec_insert_before_back() { - let (mut vec, a, b, c) = setup(); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - - let d = 11u64; - - // Inserts right before the back - vec.insert(vec.len() - 1, d); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).unwrap() == d); - assert(vec.get(3).unwrap() == c); - assert(vec.len() == 4); - assert(vec.capacity() == 4); -} - -#[test()] -fn vec_insert_back() { - let (mut vec, a, b, c) = setup(); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - - let d = 11u64; - - // Inserts at the back - vec.insert(vec.len(), d); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).unwrap() == c); - assert(vec.get(3).unwrap() == d); - assert(vec.len() == 4); - assert(vec.capacity() == 4); -} - -#[test(should_revert)] -fn revert_vec_insert_out_of_bounds() { - let (mut vec, a, _b, _c) = setup(); - - vec.insert(vec.len() + 1, a); -} - -#[test()] -fn vec_pop() { - let (mut vec, a, b, c) = setup(); - assert(vec.len() == 3); - - vec.push(42u64); - vec.push(11u64); - vec.push(69u64); - vec.push(100u64); - vec.push(200u64); - vec.push(255u64); - vec.push(180u64); - vec.push(17u64); - vec.push(19u64); - assert(vec.len() == 12); - assert(vec.capacity() == 16); - - let first = vec.pop(); - assert(first.unwrap() == 19u64); - assert(vec.len() == 11); - assert(vec.capacity() == 16); - - let second = vec.pop(); - assert(second.unwrap() == 17u64); - assert(vec.len() == 10); - assert(vec.capacity() == 16); - - let third = vec.pop(); - assert(third.unwrap() == 180u64); - assert(vec.len() == 9); - let _ = vec.pop(); - let _ = vec.pop(); - let _ = vec.pop(); - let _ = vec.pop(); - let _ = vec.pop(); - let _ = vec.pop(); - assert(vec.len() == 3); - assert(vec.pop().unwrap() == c); - assert(vec.pop().unwrap() == b); - assert(vec.pop().unwrap() == a); - - // Can pop all - assert(vec.len() == 0); - assert(vec.capacity() == 16); - assert(vec.pop().is_none()); -} - -#[test()] -fn vec_swap() { - let (mut vec, a, b, c) = setup(); - let d = 5u64; - vec.push(d); - assert(vec.len() == 4); - assert(vec.capacity() == 4); - - // Swaps Middle - vec.swap(1, 2); - assert(vec.len() == 4); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == c); - assert(vec.get(2).unwrap() == b); - assert(vec.get(3).unwrap() == d); -} - -#[test()] -fn vec_swap_twice() { - let (mut vec, a, b, c) = setup(); - let d = 5u64; - vec.push(d); - assert(vec.len() == 4); - assert(vec.capacity() == 4); - - // Swaps Middle - vec.swap(1, 2); - assert(vec.len() == 4); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == c); - assert(vec.get(2).unwrap() == b); - assert(vec.get(3).unwrap() == d); - - vec.swap(1, 2); - assert(vec.len() == 4); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).unwrap() == c); - assert(vec.get(3).unwrap() == d); -} - -#[test()] -fn vec_swap_front() { - let (mut vec, a, b, c) = setup(); - - // Swaps Front - vec.swap(0, 1); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == b); - assert(vec.get(1).unwrap() == a); - assert(vec.get(2).unwrap() == c); -} - -#[test()] -fn vec_swap_end() { - let (mut vec, a, b, c) = setup(); - - // Swaps back - vec.swap(2, 1); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == c); - assert(vec.get(2).unwrap() == b); -} - -#[test()] -fn vec_swap_front_with_end() { - let (mut vec, a, b, c) = setup(); - - // Swaps front with back - vec.swap(0, 2); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == c); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).unwrap() == a); -} - -#[test(should_revert)] -fn revert_vec_swap_element_1_out_of_bounds() { - let (mut vec, _a, _b, _c) = setup(); - - vec.swap(vec.len(), 0); -} - -#[test(should_revert)] -fn revert_vec_swap_element_2_out_of_bounds() { - let (mut vec, _a, _b, _c) = setup(); - - vec.swap(0, vec.len()); -} - -#[test()] -fn vec_set() { - let (mut vec, a, _b, c) = setup(); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - let d = 11u64; - - // Sets in the middle - vec.set(1, d); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == d); - assert(vec.get(2).unwrap() == c); -} - -#[test()] -fn vec_set_twice() { - let (mut vec, a, _b, c) = setup(); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - let d = 11u64; - let e = 13u64; - - // Sets in the middle - vec.set(1, d); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == d); - assert(vec.get(2).unwrap() == c); - - // Twice - vec.set(1, e); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == e); - assert(vec.get(2).unwrap() == c); -} - -#[test()] -fn vec_set_front() { - let (mut vec, _a, b, c) = setup(); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - let d = 11u64; - - // Sets at the front - vec.set(0, d); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == d); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).unwrap() == c); -} - -#[test()] -fn vec_set_back() { - let (mut vec, a, b, _c) = setup(); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - let d = 11u64; - - // Sets at the back - vec.set(vec.len() - 1, d); - assert(vec.len() == 3); - assert(vec.capacity() == 4); - assert(vec.get(0).unwrap() == a); - assert(vec.get(1).unwrap() == b); - assert(vec.get(2).unwrap() == d); -} - -#[test(should_revert)] -fn revert_vec_set_out_of_bounds() { - let (mut vec, _a, _b, _c) = setup(); - - vec.set(vec.len(), 11u64); -} - -#[test] -fn vec_iter() { - let mut vector: Vec = Vec::new(); - - let number0 = 0; - let number1 = 1; - let number2 = 2; - let number3 = 3; - let number4 = 4; - - vector.push(number0); - vector.push(number1); - vector.push(number2); - vector.push(number3); - vector.push(number4); - - let mut iter = vector.iter(); - - assert(iter.next() == Some(number0)); - assert(iter.next() == Some(number1)); - assert(iter.next() == Some(number2)); - assert(iter.next() == Some(number3)); - assert(iter.next() == Some(number4)); - assert(iter.next() == None); - assert(iter.next() == None); -} - -#[test] -fn vec_ptr() { - let (mut setup_vec, a, _, _) = setup(); - - let setup_vec_ptr = setup_vec.ptr(); - assert(!setup_vec_ptr.is_null()); - assert(setup_vec_ptr.read::() == a); - - let mut new_vec: Vec = Vec::new(); - let new_vec_ptr = new_vec.ptr(); - assert(!new_vec_ptr.is_null()); - - let mut capacity_vec: Vec = Vec::with_capacity(16); - let capacity_vec_ptr = capacity_vec.ptr(); - assert(!capacity_vec_ptr.is_null()); -} - -#[test()] -fn vec_as_raw_slice() { - let (mut vec, _a, _b, _c) = setup(); - - let slice = vec.as_raw_slice(); - assert(vec.ptr() == slice.ptr()); - assert(vec.len() == slice.len::()); -} - -#[test()] -fn vec_from_raw_slice() { - let val = 0x3497297632836282349729763283628234972976328362823497297632836282; - let slice = asm(ptr: (__addr_of(val), 32)) { - ptr: raw_slice - }; - - let mut vec: Vec = Vec::from(slice); - assert(vec.ptr() != slice.ptr()); // Vec should own its buffer - assert(vec.len() == slice.len::()); -} - -// TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6085 is resolved -// #[test()] -// fn vec_into_raw_slice() { -// // Glob operator needed for From for raw_slice -// use std::vec::*; - -// let (mut vec, _a, _b, _c) = setup(); - -// let slice: raw_slice = vec.into(); - -// assert(vec.ptr() == slice.ptr()); -// assert(vec.len() == slice.len::()); -// } - -// TODO: Uncomment when https://github.com/FuelLabs/sway/issues/6085 is resolved -// #[test()] -// fn vec_raw_slice_from() { -// // Glob operator needed for From for raw_slice -// use std::vec::*; - -// let (mut vec, _a, _b, _c) = setup(); - -// let slice: raw_slice = >>::from(vec); - -// assert(vec.ptr() == slice.ptr()); -// assert(vec.len() == slice.len::()); -// } - - -#[test()] -fn vec_raw_slice_into() { - let val = 0x3497297632836282349729763283628234972976328362823497297632836282; - let slice = asm(ptr: (__addr_of(val), 32)) { - ptr: raw_slice - }; - - let vec: Vec = slice.into(); - - assert(vec.ptr() != slice.ptr()); // Vec should own its buffer - assert(vec.len() == slice.len::()); -} - -#[test()] -fn vec_encode_and_decode() { - let mut v1: Vec = Vec::new(); - v1.push(1); - v1.push(2); - v1.push(3); - - let v2 = abi_decode::>(encode(v1)); - - assert(v2.len() == 3); - assert(v2.capacity() == 3); - assert(v2.get(0) == Some(1)); - assert(v2.get(1) == Some(2)); - assert(v2.get(2) == Some(3)); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/Forc.toml deleted file mode 100644 index ecdc1cddaad..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "vm_evm_ecr_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/src/main.sw deleted file mode 100644 index f676386a47c..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_ecr_inline_tests/src/main.sw +++ /dev/null @@ -1,26 +0,0 @@ -library; - -use std::{b512::B512, vm::evm::{ecr::ec_recover_evm_address, evm_address::EvmAddress,}}; - -#[test] -fn ecr_ec_recover() { - let hi_1 = 0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c; - let lo_1 = 0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d; - let msg_hash_1 = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323; - let expected_evm_address = EvmAddress::from(0x0000000000000000000000000ec44cf95ce5051ef590e6d420f8e722dd160ecb); - let signature_1: B512 = B512::from((hi_1, lo_1)); - - let result_1 = ec_recover_evm_address(signature_1, msg_hash_1); - - assert(result_1.is_ok()); - assert(result_1.unwrap() == expected_evm_address); - - let hi_2 = 0xbd0c9b8792876713afa8bf1383eebf31c43437823ed761cc3600d0016de5110c; - let lo_2 = 0xee45573606c96c98ba170ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323; - let msg_hash_2 = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52cad30b89df1e4a9c4323; - let signature_2: B512 = B512::from((hi_2, lo_2)); - - let result_2 = ec_recover_evm_address(signature_2, msg_hash_2); - - assert(result_2.is_err()); -} diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/Forc.toml b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/Forc.toml deleted file mode 100644 index f51e58605eb..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/Forc.toml +++ /dev/null @@ -1,8 +0,0 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "vm_evm_evm_address_inline_tests" - -[dependencies] -std = { path = "../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/src/main.sw b/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/src/main.sw deleted file mode 100644 index 460d9e8e0d5..00000000000 --- a/test/src/e2e_vm_tests/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/src/main.sw +++ /dev/null @@ -1,162 +0,0 @@ -library; - -use std::vm::evm::evm_address::EvmAddress; - -#[test] -fn evm_address_bits() { - let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - - assert( - evm_address_1 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - assert( - evm_address_2 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - assert( - evm_address_3 - .bits() == 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff, - ); -} - -#[test] -fn evm_address_eq() { - let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let evm_address_3 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let evm_address_4 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let evm_address_5 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - let evm_address_6 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - - assert(evm_address_1 == evm_address_2); - assert(evm_address_3 == evm_address_4); - assert(evm_address_5 == evm_address_6); -} - -#[test] -fn evm_address_ne() { - let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - - assert(evm_address_1 != evm_address_2); - assert(evm_address_1 != evm_address_3); - assert(evm_address_2 != evm_address_3); -} - -#[test] -fn evm_address_from_b256() { - let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - - assert( - evm_address_1 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); - assert( - evm_address_2 - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); - assert( - evm_address_3 - .bits() == 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff, - ); -} - -#[test] -fn evm_address_into_b256() { - let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - - let b256_1: b256 = evm_address_1.into(); - let b256_2: b256 = evm_address_2.into(); - let b256_3: b256 = evm_address_3.into(); - - assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - assert(b256_2 == 0x0000000000000000000000000000000000000000000000000000000000000001); - assert(b256_3 == 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff); -} - -#[test] -fn evm_address_b256_from() { - // Glob operator is needed for from for b256 - use std::vm::evm::evm_address::*; - - let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - - let b256_1 = b256::from(evm_address_1); - let b256_2 = b256::from(evm_address_2); - let b256_3 = b256::from(evm_address_3); - - assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - assert(b256_2 == 0x0000000000000000000000000000000000000000000000000000000000000001); - assert(b256_3 == 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff); -} - -#[test] -fn evm_address_b256_into() { - // Glob operator is needed for into for b256 - use std::vm::evm::evm_address::*; - - let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let evm_address_3 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - - let b256_1: b256 = evm_address_1.into(); - let b256_2: b256 = evm_address_2.into(); - let b256_3: b256 = evm_address_3.into(); - - assert(b256_1 == 0x0000000000000000000000000000000000000000000000000000000000000000); - assert(b256_2 == 0x0000000000000000000000000000000000000000000000000000000000000001); - assert(b256_3 == 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff); -} - -#[test] -fn evm_address_zero() { - let evm_address = EvmAddress::zero(); - assert( - evm_address - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000000, - ); -} - -#[test] -fn evm_address_is_zero() { - let evm_address_1 = EvmAddress::zero(); - let evm_address_2 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let evm_address_3 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let evm_address_4 = EvmAddress::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - - assert(evm_address_1.is_zero()); - assert(evm_address_2.is_zero()); - assert(!evm_address_3.is_zero()); - assert(!evm_address_4.is_zero()); -} - -#[test] -fn evm_address_hash() { - use std::hash::{Hash, sha256}; - - let evm_address_1 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000000); - let digest_1 = sha256(evm_address_1); - assert(digest_1 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let evm_address_2 = EvmAddress::from(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - let digest_2 = sha256(evm_address_2); - assert(digest_2 == 0x78230345cedf8e92525c3cfdb8a95e947de1ed72e881b055dd80f9e523ff33e0); - - let evm_address_3 = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let digest_3 = sha256(evm_address_3); - assert(digest_3 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); - - let evm_address_4 = EvmAddress::from(0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); - let digest_4 = sha256(evm_address_4); - assert(digest_4 != 0x4eaddc8cfcdd27223821e3e31ab54b2416dd3b0c1a86afd7e8d6538ca1bd0a77); -} diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/array_oob/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob/test.toml index f4fb799b640..059ddb65458 100644 --- a/test/src/e2e_vm_tests/test_programs/should_fail/array_oob/test.toml +++ b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob/test.toml @@ -1,3 +1,3 @@ category = "fail" -# check: $()Array index out of bounds; the length is 3 but the index is 4. +# check: $()Index out of bounds; the length is 3 but the index is 4. diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_global_const_index/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_global_const_index/test.toml index f4fb799b640..059ddb65458 100644 --- a/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_global_const_index/test.toml +++ b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_global_const_index/test.toml @@ -1,3 +1,3 @@ category = "fail" -# check: $()Array index out of bounds; the length is 3 but the index is 4. +# check: $()Index out of bounds; the length is 3 but the index is 4. diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_variable_index/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_variable_index/test.toml index f4fb799b640..059ddb65458 100644 --- a/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_variable_index/test.toml +++ b/test/src/e2e_vm_tests/test_programs/should_fail/array_oob_variable_index/test.toml @@ -1,3 +1,3 @@ category = "fail" -# check: $()Array index out of bounds; the length is 3 but the index is 4. +# check: $()Index out of bounds; the length is 3 but the index is 4. From 5fe13590274407468715acf84a783bcfbeb0749f Mon Sep 17 00:00:00 2001 From: xunilrj Date: Mon, 22 Jul 2024 12:27:49 +0100 Subject: [PATCH 11/22] fmt sway cor and stdlib --- sway-lib-core/src/codec.sw | 2 +- sway-lib-core/src/ops.sw | 1 - sway-lib-core/src/slice.sw | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/sway-lib-core/src/codec.sw b/sway-lib-core/src/codec.sw index e656bb057f5..59a70ece81c 100644 --- a/sway-lib-core/src/codec.sw +++ b/sway-lib-core/src/codec.sw @@ -699,7 +699,7 @@ impl AbiEncode for raw_slice { impl AbiEncode for &__slice[T] where - T: AbiEncode + T: AbiEncode, { fn abi_encode(self, buffer: Buffer) -> Buffer { Buffer { diff --git a/sway-lib-core/src/ops.sw b/sway-lib-core/src/ops.sw index 31c7b9a7bfe..aaa46315bfc 100644 --- a/sway-lib-core/src/ops.sw +++ b/sway-lib-core/src/ops.sw @@ -609,7 +609,6 @@ impl Eq for raw_ptr { } } - /// Trait to evaluate if one value is greater or less than another of the same type. pub trait Ord { /// Evaluates if one value of the same type is greater than another. diff --git a/sway-lib-core/src/slice.sw b/sway-lib-core/src/slice.sw index 10f953d254b..c2a19246696 100644 --- a/sway-lib-core/src/slice.sw +++ b/sway-lib-core/src/slice.sw @@ -4,7 +4,7 @@ use ::raw_ptr::*; impl &__slice[T] { pub fn ptr(self) -> raw_ptr { - let (ptr, _) = asm(s:self) { + let (ptr, _) = asm(s: self) { s: (raw_ptr, u64) }; ptr From 1262b1ac2a8f281e3e0547119456ea623fa797cd Mon Sep 17 00:00:00 2001 From: xunilrj Date: Mon, 22 Jul 2024 14:49:19 +0100 Subject: [PATCH 12/22] __slice and __slice_elem for const --- sway-ast/src/intrinsics.rs | 2 +- sway-core/src/abi_generation/fuel_abi.rs | 4 +- .../src/asm_generation/fuel/data_section.rs | 1 + .../miden_vm/miden_vm_asm_builder.rs | 1 + sway-core/src/ir_generation/const_eval.rs | 62 ++++++++++++++++++- sway-ir/src/constant.rs | 1 + sway-ir/src/printer.rs | 9 +++ .../slice/slice_intrinsics/src/main.sw | 13 ++++ 8 files changed, 87 insertions(+), 6 deletions(-) diff --git a/sway-ast/src/intrinsics.rs b/sway-ast/src/intrinsics.rs index 7107df7b3eb..1f9d98e62bc 100644 --- a/sway-ast/src/intrinsics.rs +++ b/sway-ast/src/intrinsics.rs @@ -41,7 +41,7 @@ pub enum Intrinsic { EncodeBufferEmpty, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_empty() EncodeBufferAppend, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_append(buffer, primitive data type) EncodeBufferAsRawSlice, // let slice: raw_slice = __encode_buffer_as_raw_slice(buffer) - Slice, // let ref_to_slice = __slice(array, inclusive_start_index, exclusive_end_index) + Slice, // let ref_to_slice = __slice(array or ref to slice, inclusive_start_index, exclusive_end_index) SliceElem, // let ref_to_item = __slice_elem(ref to slice, index) } diff --git a/sway-core/src/abi_generation/fuel_abi.rs b/sway-core/src/abi_generation/fuel_abi.rs index 90d3173ad2b..f4d0205d8b0 100644 --- a/sway-core/src/abi_generation/fuel_abi.rs +++ b/sway-core/src/abi_generation/fuel_abi.rs @@ -407,8 +407,8 @@ impl TypeId { } } TypeInfo::Slice(..) => { - if let TypeInfo::Array(elem_ty, _) = &*type_engine.get(resolved_type_id) { - // The `program_abi::TypeDeclaration`s needed for the array element type + if let TypeInfo::Slice(elem_ty) = &*type_engine.get(resolved_type_id) { + // The `program_abi::TypeDeclaration`s needed for the slice element type let elem_abi_ty = program_abi::TypeDeclaration { type_id: elem_ty.initial_type_id.index(), type_field: elem_ty.initial_type_id.get_abi_type_str( diff --git a/sway-core/src/asm_generation/fuel/data_section.rs b/sway-core/src/asm_generation/fuel/data_section.rs index 10dc27f0da8..74949aad566 100644 --- a/sway-core/src/asm_generation/fuel/data_section.rs +++ b/sway-core/src/asm_generation/fuel/data_section.rs @@ -123,6 +123,7 @@ impl Entry { name, padding, ), + ConstantValue::Slice(_) => todo!(), ConstantValue::Struct(_) => Entry::new_collection( constant .struct_fields_with_padding(context) diff --git a/sway-core/src/asm_generation/miden_vm/miden_vm_asm_builder.rs b/sway-core/src/asm_generation/miden_vm/miden_vm_asm_builder.rs index ff0575219a0..87696ca002a 100644 --- a/sway-core/src/asm_generation/miden_vm/miden_vm_asm_builder.rs +++ b/sway-core/src/asm_generation/miden_vm/miden_vm_asm_builder.rs @@ -662,6 +662,7 @@ impl<'ir, 'eng> MidenVMAsmBuilder<'ir, 'eng> { B256(_) => todo!(), String(_) => todo!(), Array(_) => todo!(), + Slice(_) => todo!(), Struct(_) => todo!(), Reference(_) => todo!(), RawUntypedSlice(_) => todo!(), diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index 1b7a78d452a..391ab4453ea 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -620,9 +620,24 @@ fn const_eval_typed_expr( } } } - ty::TyExpressionVariant::Ref(_) | ty::TyExpressionVariant::Deref(_) => { + ty::TyExpressionVariant::Ref(_) => { return Err(ConstEvalError::CompileError); } + ty::TyExpressionVariant::Deref(v) => match &v.expression { + ty::TyExpressionVariant::IntrinsicFunction(f) => match f.kind { + Intrinsic::SliceElem => { + let v = const_eval_intrinsic(lookup, known_consts, f)? + .ok_or(ConstEvalError::CompileError)?; + let v = match v.value { + ConstantValue::Reference(v) => v, + _ => todo!(), + }; + Some((*v).clone()) + } + _ => todo!(), + }, + _ => return Err(ConstEvalError::CompileError), + }, ty::TyExpressionVariant::EnumTag { exp } => { let value = const_eval_typed_expr(lookup, known_consts, exp)?.map(|x| x.value); if let Some(ConstantValue::Struct(fields)) = value { @@ -1284,8 +1299,49 @@ fn const_eval_intrinsic( value: ConstantValue::RawUntypedSlice(bytes[0..(len as usize)].to_vec()), })) } - Intrinsic::Slice => todo!(), - Intrinsic::SliceElem => todo!(), + Intrinsic::Slice => { + let start = match &args[1].value { + ConstantValue::Uint(v) => *v, + _ => todo!(), + } as usize; + let end = match &args[2].value { + ConstantValue::Uint(v) => *v, + _ => todo!(), + } as usize; + + match &args[0].value { + ConstantValue::Array(v) => { + let elem_type = v[0].ty.clone(); + let slice = (&v[start..end]).iter().cloned().collect::>(); + Ok(Some(Constant { + ty: Type::get_typed_slice(lookup.context, elem_type), + value: ConstantValue::Slice(slice), + })) + } + ConstantValue::Reference(r) => match &r.value { + ConstantValue::Slice(_) => todo!(), + _ => todo!(), + }, + _ => todo!(), + } + } + Intrinsic::SliceElem => { + let idx = match &args[1].value { + ConstantValue::Uint(v) => *v, + _ => todo!(), + } as usize; + + match &args[0].value { + ConstantValue::Slice(s) => { + let v = s[idx].clone(); + Ok(Some(Constant { + ty: Type::new_ptr(lookup.context, v.ty.clone()), + value: ConstantValue::Reference(Box::new(v)), + })) + } + _ => todo!(), + } + } } } diff --git a/sway-ir/src/constant.rs b/sway-ir/src/constant.rs index 9aaae3c4218..fc4d33a3125 100644 --- a/sway-ir/src/constant.rs +++ b/sway-ir/src/constant.rs @@ -23,6 +23,7 @@ pub enum ConstantValue { B256(B256), String(Vec), Array(Vec), + Slice(Vec), Struct(Vec), Reference(Box), RawUntypedSlice(Vec), diff --git a/sway-ir/src/printer.rs b/sway-ir/src/printer.rs index 19b90c92861..bcb957b2999 100644 --- a/sway-ir/src/printer.rs +++ b/sway-ir/src/printer.rs @@ -1188,6 +1188,15 @@ impl Constant { .collect::>() .join(", ") ), + ConstantValue::Slice(elems) => format!( + "__slice[{}] [{}]", + self.ty.as_string(context), + elems + .iter() + .map(|elem| elem.as_lit_string(context)) + .collect::>() + .join(", ") + ), ConstantValue::Struct(fields) => format!( "{} {{ {} }}", self.ty.as_string(context), diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index cf66404e5b4..2ec96b3966e 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -132,4 +132,17 @@ fn main() { assert(v.get(4), 5); assert(v.get(5), 6); assert(v.get(6), 7); + + let a: [u64; 5] = [1, 2, 3, 4, 5]; + let s = __slice(a, 0, 5); + assert(1, *__slice_elem(s, 0)); + assert(2, *__slice_elem(s, 1)); + assert(3, *__slice_elem(s, 2)); + assert(4, *__slice_elem(s, 3)); + assert(5, *__slice_elem(s, 4)); + + const ARRAY: [u64; 5] = [1, 2, 3, 4, 5]; + const SLICE: &__slice[u64] = __slice(ARRAY, 0, 5); + const ELEM: u64 = *__slice_elem(SLICE, 0); + assert(1, ELEM); } From 5fb54e213a716b33733afee7eabc19f27f29c75e Mon Sep 17 00:00:00 2001 From: xunilrj Date: Mon, 22 Jul 2024 14:57:59 +0100 Subject: [PATCH 13/22] fmt and clippy issues --- sway-core/src/ir_generation/const_eval.rs | 6 +++--- .../should_pass/language/slice/slice_intrinsics/src/main.sw | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index 391ab4453ea..265ca09cddc 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -1311,8 +1311,8 @@ fn const_eval_intrinsic( match &args[0].value { ConstantValue::Array(v) => { - let elem_type = v[0].ty.clone(); - let slice = (&v[start..end]).iter().cloned().collect::>(); + let elem_type = v[0].ty; + let slice = v[start..end].to_vec(); Ok(Some(Constant { ty: Type::get_typed_slice(lookup.context, elem_type), value: ConstantValue::Slice(slice), @@ -1335,7 +1335,7 @@ fn const_eval_intrinsic( ConstantValue::Slice(s) => { let v = s[idx].clone(); Ok(Some(Constant { - ty: Type::new_ptr(lookup.context, v.ty.clone()), + ty: Type::new_ptr(lookup.context, v.ty), value: ConstantValue::Reference(Box::new(v)), })) } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 2ec96b3966e..4456f42a53a 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -141,6 +141,11 @@ fn main() { assert(4, *__slice_elem(s, 3)); assert(5, *__slice_elem(s, 4)); + let s = __slice(a, 1, 4); + assert(2, *__slice_elem(s, 0)); + assert(3, *__slice_elem(s, 1)); + assert(4, *__slice_elem(s, 2)); + const ARRAY: [u64; 5] = [1, 2, 3, 4, 5]; const SLICE: &__slice[u64] = __slice(ARRAY, 0, 5); const ELEM: u64 = *__slice_elem(SLICE, 0); From 016fce51cb3fd70f4b92fc9140c7d3c57433c3b5 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Tue, 23 Jul 2024 16:40:49 +0100 Subject: [PATCH 14/22] better error messages for slice in constants --- sway-core/src/language/ty/module.rs | 58 ++++++++++++++++++- sway-core/src/language/ty/program.rs | 31 +++++----- sway-error/src/error.rs | 3 + .../slice/slice_intrinsics/Forc.lock | 8 +++ .../slice/slice_intrinsics/Forc.toml | 8 +++ .../slice_intrinsics/json_abi_oracle.json | 25 ++++++++ .../json_abi_oracle_new_encoding.json | 26 +++++++++ .../slice/slice_intrinsics/src/main.sw | 28 +++++++++ .../slice/slice_intrinsics/test.toml | 26 +++++++++ .../slice/slice_intrinsics/src/main.sw | 34 +++++------ 10 files changed, 208 insertions(+), 39 deletions(-) create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/json_abi_oracle.json create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/json_abi_oracle_new_encoding.json create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/test.toml diff --git a/sway-core/src/language/ty/module.rs b/sway-core/src/language/ty/module.rs index 21bc45eb6cf..33078fb2bb6 100644 --- a/sway-core/src/language/ty/module.rs +++ b/sway-core/src/language/ty/module.rs @@ -4,9 +4,8 @@ use sway_error::handler::{ErrorEmitted, Handler}; use sway_types::Span; use crate::{ - decl_engine::{DeclEngine, DeclRef, DeclRefFunction}, - language::ModName, - language::{ty::*, HasModule, HasSubmodules}, + decl_engine::{DeclEngine, DeclEngineGet, DeclRef, DeclRefFunction}, + language::{ty::*, HasModule, HasSubmodules, ModName}, semantic_analysis::namespace, transform::{self, AllowDeprecatedState}, Engines, @@ -21,6 +20,59 @@ pub struct TyModule { pub attributes: transform::AttributesMap, } +impl TyModule { + /// Iter on all constants in this module, which means, globals constants and + /// local constants, but it does not enter into submodules. + pub fn iter_constants(&self, de: &DeclEngine) -> Vec { + fn inside_code_block(de: &DeclEngine, block: &TyCodeBlock) -> Vec { + block + .contents + .iter() + .flat_map(|node| inside_ast_node(de, node)) + .collect::>() + } + + fn inside_ast_node(de: &DeclEngine, node: &TyAstNode) -> Vec { + match &node.content { + TyAstNodeContent::Declaration(decl) => match decl { + TyDecl::ConstantDecl(decl) => { + vec![decl.clone()] + } + TyDecl::FunctionDecl(decl) => { + let decl = de.get(&decl.decl_id); + inside_code_block(de, &decl.body) + } + TyDecl::ImplSelfOrTrait(decl) => { + let decl = de.get(&decl.decl_id); + decl.items + .iter() + .flat_map(|item| match item { + TyTraitItem::Fn(decl) => { + let decl = de.get(decl.id()); + inside_code_block(de, &decl.body) + } + TyTraitItem::Constant(decl) => { + vec![ConstantDecl { + decl_id: *decl.id(), + }] + } + _ => vec![], + }) + .collect() + } + _ => vec![], + }, + _ => vec![], + } + } + + self.all_nodes + .iter() + .flat_map(|node| inside_ast_node(de, node)) + .collect::>() + } +} + #[derive(Clone, Debug)] pub struct TySubmodule { pub module: TyModule, diff --git a/sway-core/src/language/ty/program.rs b/sway-core/src/language/ty/program.rs index 7d585533f18..92d273f1526 100644 --- a/sway-core/src/language/ty/program.rs +++ b/sway-core/src/language/ty/program.rs @@ -72,7 +72,6 @@ impl TyProgram { let decl_engine = engines.de(); // Validate all submodules - let mut non_configurables_constants = vec![]; let mut configurables = vec![]; for (_, submodule) in &root.submodules { match Self::validate_root( @@ -114,13 +113,6 @@ impl TyProgram { declarations.push(TyDecl::FunctionDecl(FunctionDecl { decl_id: *decl_id })); } - TyAstNodeContent::Declaration(TyDecl::ConstantDecl(ConstantDecl { - decl_id, - .. - })) => { - let decl = (*decl_engine.get_constant(decl_id)).clone(); - non_configurables_constants.push(decl); - } TyAstNodeContent::Declaration(TyDecl::ConfigurableDecl(ConfigurableDecl { decl_id, .. @@ -404,6 +396,7 @@ impl TyProgram { &c.type_ascription, |t| match t { TypeInfo::StringSlice => Some(TypeNotAllowedReason::StringSliceInConfigurables), + TypeInfo::Slice(_) => Some(TypeNotAllowedReason::SliceInConst), _ => None, }, ) { @@ -411,16 +404,18 @@ impl TyProgram { } } - for c in non_configurables_constants.iter() { - if let Some(error) = get_type_not_allowed_error( - engines, - c.return_type, - &c.type_ascription, - |t| match t { - TypeInfo::StringSlice => Some(TypeNotAllowedReason::StringSliceInConst), - _ => None, - }, - ) { + // verify all constants + for decl in root.iter_constants(decl_engine).iter() { + let decl = decl_engine.get_constant(&decl.decl_id); + let e = + get_type_not_allowed_error(engines, decl.return_type, &decl.type_ascription, |t| { + match t { + TypeInfo::StringSlice => Some(TypeNotAllowedReason::StringSliceInConst), + TypeInfo::Slice(_) => Some(TypeNotAllowedReason::SliceInConst), + _ => None, + } + }); + if let Some(error) = e { handler.emit_err(error); } } diff --git a/sway-error/src/error.rs b/sway-error/src/error.rs index 38ead24b7db..f0bb9c26a84 100644 --- a/sway-error/src/error.rs +++ b/sway-error/src/error.rs @@ -2676,6 +2676,9 @@ pub enum TypeNotAllowedReason { #[error("`str` or a type containing `str` on `const` is not allowed.")] StringSliceInConst, + + #[error("slices or types containing slices on `const` are not allowed.")] + SliceInConst, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/Forc.lock new file mode 100644 index 00000000000..c5bc5415952 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/Forc.lock @@ -0,0 +1,8 @@ +[[package]] +name = "core" +source = "path+from-root-53D6DC514F06A250" + +[[package]] +name = "slice_intrinsics" +source = "member" +dependencies = ["core"] diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/Forc.toml new file mode 100644 index 00000000000..011fe1ab735 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "slice_intrinsics" + +[dependencies] +core = { path = "../../../../../../../sway-lib-core" } diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/json_abi_oracle.json b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/json_abi_oracle.json new file mode 100644 index 00000000000..ad50b55d54c --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/json_abi_oracle.json @@ -0,0 +1,25 @@ +{ + "configurables": [], + "functions": [ + { + "attributes": null, + "inputs": [], + "name": "main", + "output": { + "name": "", + "type": 0, + "typeArguments": null + } + } + ], + "loggedTypes": [], + "messagesTypes": [], + "types": [ + { + "components": null, + "type": "u64", + "typeId": 0, + "typeParameters": null + } + ] +} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/json_abi_oracle_new_encoding.json b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/json_abi_oracle_new_encoding.json new file mode 100644 index 00000000000..e60dda965d4 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/json_abi_oracle_new_encoding.json @@ -0,0 +1,26 @@ +{ + "configurables": [], + "encoding": "1", + "functions": [ + { + "attributes": null, + "inputs": [], + "name": "main", + "output": { + "name": "", + "type": 0, + "typeArguments": null + } + } + ], + "loggedTypes": [], + "messagesTypes": [], + "types": [ + { + "components": [], + "type": "()", + "typeId": 0, + "typeParameters": null + } + ] +} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/src/main.sw new file mode 100644 index 00000000000..b2bb36b32ab --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/src/main.sw @@ -0,0 +1,28 @@ +script; + +// slice cannot be consts +const GLOBAL_ARRAY: [u64; 5] = [1, 2, 3, 4, 5]; +const GLOBAL_SLICE: &__slice[u64] = __slice(GLOBAL_ARRAY, 0, 5); + +fn main() { + // slice cannot be consts + const LOCAL_ARRAY: [u64; 5] = [1, 2, 3, 4, 5]; + const LOCAL_SLICE: &__slice[u64] = __slice(LOCAL_ARRAY, 0, 5); + + // Wrong start index + let a: [u64; 5] = [1, 2, 3, 4, 5]; + let _ = __slice(a, 6, 7); + + // Wrong end index + let a: [u64; 5] = [1, 2, 3, 4, 5]; + let _ = __slice(a, 0, 6); + + // Wrong first argument + __slice(0, 0, 0); + + // Wrong start index + __slice(0, "", 0); + + // Wrong end index + __slice(0, 0, ""); +} diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/test.toml new file mode 100644 index 00000000000..3bbe64aee42 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/test.toml @@ -0,0 +1,26 @@ +category = "fail" + +# check: let _ = __slice(a, 6, 7) +# nextln: $()Index out of bounds; the length is 5 but the index is 6. + +# check: let _ = __slice(a, 0, 6) +# nextln: $()Index out of bounds; the length is 5 but the index is 6. + +# check: __slice(0, 0, 0) +# nextln: $()Unsupported argument type to intrinsic "slice" + +# check: __slice(0, "", 0) +# nextln: $()Mismatched types +# nextln: $()expected: u64 +# nextln: $()found: str + +# check: __slice(0, 0, "") +# nextln: $()Mismatched types +# nextln: $()expected: u64 +# nextln: $()found: str + +# check: &__slice[u64] = __slice(GLOBAL_ARRAY, 0, 5) +# nextln: $()slices or types containing slices on `const` are not allowed + +# check: &__slice[u64] = __slice(LOCAL_ARRAY, 0, 5) +# nextln: $()slices or types containing slices on `const` are not allowed diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 4456f42a53a..43f9edd086d 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -111,6 +111,22 @@ where } fn main() { + // slice arrays + let a: [u64; 5] = [1, 2, 3, 4, 5]; + let s = __slice(a, 0, 5); + assert(1, *__slice_elem(s, 0)); + assert(2, *__slice_elem(s, 1)); + assert(3, *__slice_elem(s, 2)); + assert(4, *__slice_elem(s, 3)); + assert(5, *__slice_elem(s, 4)); + + // slice another slice + let s = __slice(a, 1, 4); + assert(2, *__slice_elem(s, 0)); + assert(3, *__slice_elem(s, 1)); + assert(4, *__slice_elem(s, 2)); + + // Vec impl using slices let mut v: Vec = Vec::new(); v.push(1); assert(v.get(0), 1); @@ -132,22 +148,4 @@ fn main() { assert(v.get(4), 5); assert(v.get(5), 6); assert(v.get(6), 7); - - let a: [u64; 5] = [1, 2, 3, 4, 5]; - let s = __slice(a, 0, 5); - assert(1, *__slice_elem(s, 0)); - assert(2, *__slice_elem(s, 1)); - assert(3, *__slice_elem(s, 2)); - assert(4, *__slice_elem(s, 3)); - assert(5, *__slice_elem(s, 4)); - - let s = __slice(a, 1, 4); - assert(2, *__slice_elem(s, 0)); - assert(3, *__slice_elem(s, 1)); - assert(4, *__slice_elem(s, 2)); - - const ARRAY: [u64; 5] = [1, 2, 3, 4, 5]; - const SLICE: &__slice[u64] = __slice(ARRAY, 0, 5); - const ELEM: u64 = *__slice_elem(SLICE, 0); - assert(1, ELEM); } From e41f7ebe6045e8881e3c7b311cf249f0db4c6d08 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Wed, 24 Jul 2024 11:01:03 +0100 Subject: [PATCH 15/22] removing todos --- .../src/asm_generation/fuel/data_section.rs | 4 +- sway-core/src/ir_generation/const_eval.rs | 108 +++++++++++------- .../src/language/ty/expression/expression.rs | 7 ++ sway-ir/src/constant.rs | 7 ++ sway-ir/src/irtype.rs | 9 ++ .../slice/slice_intrinsics/src/main.sw | 3 + 6 files changed, 95 insertions(+), 43 deletions(-) diff --git a/sway-core/src/asm_generation/fuel/data_section.rs b/sway-core/src/asm_generation/fuel/data_section.rs index 74949aad566..3db12443f4f 100644 --- a/sway-core/src/asm_generation/fuel/data_section.rs +++ b/sway-core/src/asm_generation/fuel/data_section.rs @@ -123,7 +123,6 @@ impl Entry { name, padding, ), - ConstantValue::Slice(_) => todo!(), ConstantValue::Struct(_) => Entry::new_collection( constant .struct_fields_with_padding(context) @@ -138,6 +137,9 @@ impl Entry { ConstantValue::Reference(_) => { todo!("Constant references are currently not supported.") } + ConstantValue::Slice(_) => { + todo!("Constant slices are currently not supported.") + } } } diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index 265ca09cddc..f05a3bce926 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -623,21 +623,23 @@ fn const_eval_typed_expr( ty::TyExpressionVariant::Ref(_) => { return Err(ConstEvalError::CompileError); } - ty::TyExpressionVariant::Deref(v) => match &v.expression { - ty::TyExpressionVariant::IntrinsicFunction(f) => match f.kind { - Intrinsic::SliceElem => { - let v = const_eval_intrinsic(lookup, known_consts, f)? - .ok_or(ConstEvalError::CompileError)?; - let v = match v.value { - ConstantValue::Reference(v) => v, - _ => todo!(), - }; - Some((*v).clone()) - } - _ => todo!(), - }, - _ => return Err(ConstEvalError::CompileError), - }, + // We support *__slice_elem(...) + ty::TyExpressionVariant::Deref(expr) => { + let value = expr + .as_intrinsic() + .filter(|x| matches!(x.kind, Intrinsic::SliceElem)) + .ok_or(ConstEvalError::CompileError) + .and_then(|kind| const_eval_intrinsic(lookup, known_consts, kind)); + if let Ok(Some(Constant { + value: ConstantValue::Reference(value), + .. + })) = value + { + Some(*value.clone()) + } else { + return Err(ConstEvalError::CompileError); + } + } ty::TyExpressionVariant::EnumTag { exp } => { let value = const_eval_typed_expr(lookup, known_consts, exp)?.map(|x| x.value); if let Some(ConstantValue::Struct(fields)) = value { @@ -1300,46 +1302,68 @@ fn const_eval_intrinsic( })) } Intrinsic::Slice => { - let start = match &args[1].value { - ConstantValue::Uint(v) => *v, - _ => todo!(), - } as usize; - let end = match &args[2].value { - ConstantValue::Uint(v) => *v, - _ => todo!(), - } as usize; + let start = args[1].as_uint().expect("Type check allowed non u64") as usize; + let end = args[2].as_uint().expect("Type check allowed non u64") as usize; match &args[0].value { - ConstantValue::Array(v) => { - let elem_type = v[0].ty; - let slice = v[start..end].to_vec(); + ConstantValue::Array(elements) => { + let slice = elements + .get(start..end) + .ok_or(ConstEvalError::CompileError)?; + let elem_type = args[0] + .ty + .get_array_elem_type(lookup.context) + .expect("unexpected non array"); Ok(Some(Constant { ty: Type::get_typed_slice(lookup.context, elem_type), - value: ConstantValue::Slice(slice), + value: ConstantValue::Slice(slice.to_vec()), })) } ConstantValue::Reference(r) => match &r.value { - ConstantValue::Slice(_) => todo!(), - _ => todo!(), + ConstantValue::Slice(elements) => { + let slice = elements + .get(start..end) + .ok_or(ConstEvalError::CompileError)?; + let elem_type = args[0] + .ty + .get_typed_slice_elem_type(lookup.context) + .expect("unexpected non slice"); + Ok(Some(Constant { + ty: Type::get_typed_slice(lookup.context, elem_type), + value: ConstantValue::Slice(slice.to_vec()), + })) + } + _ => Err(ConstEvalError::CannotBeEvaluatedToConst { + span: intrinsic.span.clone(), + }), + _ => Err(ConstEvalError::CannotBeEvaluatedToConst { + span: intrinsic.span.clone(), + }), }, - _ => todo!(), + _ => Err(ConstEvalError::CannotBeEvaluatedToConst { + span: intrinsic.span.clone(), + }) } } Intrinsic::SliceElem => { - let idx = match &args[1].value { - ConstantValue::Uint(v) => *v, - _ => todo!(), - } as usize; + let idx = args[1].as_uint().expect("Type check allowed non u64") as usize; match &args[0].value { - ConstantValue::Slice(s) => { - let v = s[idx].clone(); - Ok(Some(Constant { - ty: Type::new_ptr(lookup.context, v.ty), - value: ConstantValue::Reference(Box::new(v)), - })) - } - _ => todo!(), + ConstantValue::Reference(r) => match &r.value { + ConstantValue::Slice(elements) => { + let v = elements[idx].clone(); + Ok(Some(Constant { + ty: Type::new_ptr(lookup.context, v.ty), + value: ConstantValue::Reference(Box::new(v)), + })) + } + _ => Err(ConstEvalError::CannotBeEvaluatedToConst { + span: intrinsic.span.clone(), + }), + }, + _ => Err(ConstEvalError::CannotBeEvaluatedToConst { + span: intrinsic.span.clone(), + }), } } } diff --git a/sway-core/src/language/ty/expression/expression.rs b/sway-core/src/language/ty/expression/expression.rs index fc1b1b3a0f5..762b3fd2384 100644 --- a/sway-core/src/language/ty/expression/expression.rs +++ b/sway-core/src/language/ty/expression/expression.rs @@ -423,4 +423,11 @@ impl TyExpression { _ => {} } } + + pub fn as_intrinsic(&self) -> Option<&TyIntrinsicFunctionKind> { + match &self.expression { + TyExpressionVariant::IntrinsicFunction(v) => Some(v), + _ => None, + } + } } diff --git a/sway-ir/src/constant.rs b/sway-ir/src/constant.rs index fc4d33a3125..b3715b16803 100644 --- a/sway-ir/src/constant.rs +++ b/sway-ir/src/constant.rs @@ -270,4 +270,11 @@ impl Constant { _ => false, } } + + pub fn as_uint(&self) -> Option { + match &self.value { + ConstantValue::Uint(v) => Some(*v), + _ => None, + } + } } diff --git a/sway-ir/src/irtype.rs b/sway-ir/src/irtype.rs index af2a3cdf1cc..1211c1da6fe 100644 --- a/sway-ir/src/irtype.rs +++ b/sway-ir/src/irtype.rs @@ -461,6 +461,15 @@ impl Type { } } + /// Get the type of the array element, if applicable. + pub fn get_typed_slice_elem_type(&self, context: &Context) -> Option { + if let TypeContent::TypedSlice(ty) = *self.get_content(context) { + Some(ty) + } else { + None + } + } + /// Get the length of the array , if applicable. pub fn get_array_len(&self, context: &Context) -> Option { if let TypeContent::Array(_, n) = *self.get_content(context) { diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 43f9edd086d..17850c5571e 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -113,6 +113,9 @@ where fn main() { // slice arrays let a: [u64; 5] = [1, 2, 3, 4, 5]; + + + let s = __slice(a, 0, 5); assert(1, *__slice_elem(s, 0)); assert(2, *__slice_elem(s, 1)); From 771299708df93a75b9c27edc0f67112a8e73830d Mon Sep 17 00:00:00 2001 From: xunilrj Date: Wed, 24 Jul 2024 11:04:08 +0100 Subject: [PATCH 16/22] removing dbg --- sway-core/src/ir_generation/const_eval.rs | 9 +-------- sway-core/src/semantic_analysis/cei_pattern_analysis.rs | 6 +++--- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index f05a3bce926..401d6d9c4a0 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -1336,13 +1336,10 @@ fn const_eval_intrinsic( _ => Err(ConstEvalError::CannotBeEvaluatedToConst { span: intrinsic.span.clone(), }), - _ => Err(ConstEvalError::CannotBeEvaluatedToConst { - span: intrinsic.span.clone(), - }), }, _ => Err(ConstEvalError::CannotBeEvaluatedToConst { span: intrinsic.span.clone(), - }) + }), } } Intrinsic::SliceElem => { @@ -1404,7 +1401,6 @@ mod tests { None, )); - dbg!(2); let r = crate::compile_to_ast( &handler, &engines, @@ -1415,15 +1411,12 @@ mod tests { None, ); - dbg!(2); let (errors, _warnings) = handler.consume(); - dbg!(1); if !errors.is_empty() { panic!("{:#?}", errors); } - dbg!(1); let f = r.unwrap(); let f = f.typed.unwrap(); diff --git a/sway-core/src/semantic_analysis/cei_pattern_analysis.rs b/sway-core/src/semantic_analysis/cei_pattern_analysis.rs index 1d72f32cfb1..51db787fa75 100644 --- a/sway-core/src/semantic_analysis/cei_pattern_analysis.rs +++ b/sway-core/src/semantic_analysis/cei_pattern_analysis.rs @@ -647,9 +647,9 @@ fn effects_of_intrinsic(intr: &sway_ast::Intrinsic) -> HashSet { | Not | EncodeBufferEmpty | EncodeBufferAppend - | EncodeBufferAsRawSlice => HashSet::new(), - Slice => todo!(), - SliceElem => todo!(), + | EncodeBufferAsRawSlice + | Slice + | SliceElem => HashSet::new(), } } From 6812bf0b3c620e921999394f2371b69231f96574 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Wed, 24 Jul 2024 20:02:03 +0100 Subject: [PATCH 17/22] removing a.txt --- a.txt | 784 ---------------------------------------------------------- 1 file changed, 784 deletions(-) delete mode 100644 a.txt diff --git a/a.txt b/a.txt deleted file mode 100644 index d268248d773..00000000000 --- a/a.txt +++ /dev/null @@ -1,784 +0,0 @@ -Testing should_pass/language/slice/slice_intrinsics ... failed - Failed to compile slice_intrinsics - Compiling should_pass/language/slice/slice_intrinsics ... - Compiling library core (/home/xunilrj/github/sway/sway-lib-core) - Compiling script slice_intrinsics (/home/xunilrj/github/sway/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics) - Backtrace [ - { fn: "sway_core::ir_generation::function::FnCompiler::compile_intrinsic_function" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression_to_value" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, - { fn: "sway_core::ir_generation::compile::compile_fn" }, - { fn: "sway_core::ir_generation::CompiledFunctionCache::ty_function_decl_to_unique_function" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_fn_call" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression_to_value" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, - { fn: "sway_core::ir_generation::compile::compile_fn" }, - { fn: "sway_core::ir_generation::CompiledFunctionCache::ty_function_decl_to_unique_function" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_fn_call" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, - { fn: "sway_core::ir_generation::compile::compile_fn" }, - { fn: "sway_core::ir_generation::compile::compile_entry_function" }, - { fn: "sway_core::ir_generation::compile::compile_script" }, - { fn: "sway_core::ir_generation::compile_program" }, - { fn: "sway_core::ast_to_asm" }, - { fn: "forc_pkg::pkg::compile" }, - { fn: "forc_pkg::pkg::build" }, - { fn: "forc_pkg::pkg::build_with_options" }, - { fn: "test::e2e_vm_tests::harness::compile_to_bytes::{{closure}}" }, - { fn: "test::e2e_vm_tests::TestContext::run::{{closure}}" }, - { fn: "test::e2e_vm_tests::run::{{closure}}" }, - { fn: " as core::future::future::Future>::poll" }, - { fn: "test::main::{{closure}}" }, - { fn: "tokio::runtime::context::runtime::enter_runtime" }, - { fn: "tokio::runtime::runtime::Runtime::block_on" }, - { fn: "test::main" }, - { fn: "std::sys_common::backtrace::__rust_begin_short_backtrace" }, - { fn: "std::rt::lang_start::{{closure}}" }, - { fn: "std::rt::lang_start_internal" }, - { fn: "main" }, - { fn: "__libc_start_main" }, - { fn: "_start" }, - ] - Backtrace [ - { fn: "sway_core::ir_generation::function::FnCompiler::compile_intrinsic_function" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_deref_up_to_ptr" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression_to_value" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, - { fn: "sway_core::ir_generation::compile::compile_fn" }, - { fn: "sway_core::ir_generation::CompiledFunctionCache::ty_function_decl_to_unique_function" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_fn_call" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_fn_call" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression_to_value" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, - { fn: "sway_core::ir_generation::compile::compile_fn" }, - { fn: "sway_core::ir_generation::CompiledFunctionCache::ty_function_decl_to_unique_function" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_fn_call" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_expression" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block::{{closure}}" }, - { fn: "sway_core::ir_generation::function::FnCompiler::compile_code_block_to_value" }, - { fn: "sway_core::ir_generation::compile::compile_fn" }, - { fn: "sway_core::ir_generation::compile::compile_entry_function" }, - { fn: "sway_core::ir_generation::compile::compile_script" }, - { fn: "sway_core::ir_generation::compile_program" }, - { fn: "sway_core::ast_to_asm" }, - { fn: "forc_pkg::pkg::compile" }, - { fn: "forc_pkg::pkg::build" }, - { fn: "forc_pkg::pkg::build_with_options" }, - { fn: "test::e2e_vm_tests::harness::compile_to_bytes::{{closure}}" }, - { fn: "test::e2e_vm_tests::TestContext::run::{{closure}}" }, - { fn: "test::e2e_vm_tests::run::{{closure}}" }, - { fn: " as core::future::future::Future>::poll" }, - { fn: "test::main::{{closure}}" }, - { fn: "tokio::runtime::context::runtime::enter_runtime" }, - { fn: "tokio::runtime::runtime::Runtime::block_on" }, - { fn: "test::main" }, - { fn: "std::sys_common::backtrace::__rust_begin_short_backtrace" }, - { fn: "std::rt::lang_start::{{closure}}" }, - { fn: "std::rt::lang_start_internal" }, - { fn: "main" }, - { fn: "__libc_start_main" }, - { fn: "_start" }, - ] - Verification failed at push_2::entry - script { - pub entry fn __entry() -> slice, !1 { - local () result - - entry(): - v0 = call main_0(), !2 - v1 = get_local ptr (), result, !3 - store v0 to v1, !3 - v2 = get_local ptr (), result, !4 - v3 = load v2 - v4 = call encode_17(v3), !5 - ret slice v4 - } - - entry_orig fn main_0() -> (), !7 { - local mut { __slice[u64], u64 } v - - entry(): - v0 = call new_1(), !8 - v1 = get_local ptr { __slice[u64], u64 }, v, !9 - store v0 to v1, !9 - v2 = get_local ptr { __slice[u64], u64 }, v, !10 - v3 = const u64 1, !11 - v4 = call push_2(v2, v3), !12 - v5 = const u64 99, !13 - v6 = call encode_9(v5), !14 - v7 = const u64 1515152261580153489 - log slice v6, v7, !14 - v8 = get_local ptr { __slice[u64], u64 }, v, !15 - v9 = load v8 - v10 = const u64 0, !16 - v11 = call get_16(v9, v10), !17 - v12 = const u64 1, !18 - v13 = call assert_13(v11, v12), !19 - v14 = const unit () - ret () v14 - } - - pub fn new_1() -> { __slice[u64], u64 }, !20 { - local { u64, u64 } __anon_0 - local { __slice[u64], u64 } __anon_1 - local u64 ptr - - entry(): - v0 = asm() -> u64 hp, !21 { - } - v1 = get_local ptr u64, ptr, !22 - store v0 to v1, !22 - v2 = get_local ptr u64, ptr, !23 - v3 = load v2 - v4 = get_local ptr { u64, u64 }, __anon_0, !24 - v5 = const u64 0 - v6 = get_elem_ptr v4, ptr u64, v5, !24 - store v3 to v6, !24 - v7 = const u64 1 - v8 = get_elem_ptr v4, ptr u64, v7, !24 - v9 = const u64 0, !25 - store v9 to v8, !24 - v10 = asm(buf: v4) -> __slice[u64] buf, !26 { - } - v11 = get_local ptr { __slice[u64], u64 }, __anon_1, !27 - v12 = const u64 0 - v13 = get_elem_ptr v11, ptr __slice[u64], v12 - store v10 to v13, !27 - v14 = const u64 1 - v15 = get_elem_ptr v11, ptr u64, v14 - v16 = const u64 0, !28 - store v16 to v15, !27 - v17 = load v11 - ret { __slice[u64], u64 } v17 - } - - pub fn push_2(self !29: ptr { __slice[u64], u64 }, _item !30: u64) -> (), !31 { - local { u64, u64 } __anon_0 - local __slice[u64] __anon_1 - local u64 current_cap - local u64 new_cap - local u64 new_cap_in_bytes - local u64 new_item_idx - local u64 old_buf_ptr - local u64 old_cap_in_bytes - local u64 ptr - - entry(self: ptr { __slice[u64], u64 }, _item: u64): - v0 = const u64 1 - v1 = get_elem_ptr self, ptr u64, v0, !32 - v2 = get_local ptr u64, new_item_idx, !33 - store v1 to v2, !33 - ^ Verification failed: Store value and pointer type mismatch. - v3 = const u64 0 - v4 = get_elem_ptr self, ptr __slice[u64], v3, !34 - v5 = load v4 - v6 = call len_3(v5), !35 - v7 = get_local ptr u64, current_cap, !36 - store v6 to v7, !36 - v8 = get_local ptr u64, new_item_idx, !37 - v9 = load v8 - v10 = get_local ptr u64, current_cap, !38 - v11 = load v10 - v12 = call ge_4(v9, v11), !39 - cbr v12, block0(), block4(), !39 - - block0(): - v13 = get_local ptr u64, current_cap, !40 - v14 = load v13 - v15 = const u64 0, !41 - v16 = call eq_6(v14, v15), !42 - cbr v16, block1(), block2(), !42 - - block1(): - v17 = const u64 1, !43 - br block3(v17) - - block2(): - v18 = get_local ptr u64, current_cap, !44 - v19 = load v18 - v20 = const u64 2, !45 - v21 = call multiply_7(v19, v20), !46 - br block3(v21) - - block3(v22: u64): - v23 = get_local ptr u64, new_cap, !47 - store v22 to v23, !47 - v24 = get_local ptr u64, new_cap, !48 - v25 = load v24 - v26 = const u64 8 - v27 = call multiply_7(v25, v26), !49 - v28 = get_local ptr u64, new_cap_in_bytes, !50 - store v27 to v28, !50 - v29 = const u64 0 - v30 = get_elem_ptr self, ptr __slice[u64], v29, !34 - v31 = load v30 - v32 = call ptr_8(v31), !51 - v33 = get_local ptr u64, old_buf_ptr, !52 - store v32 to v33, !52 - v34 = get_local ptr u64, current_cap, !53 - v35 = load v34 - v36 = const u64 8 - v37 = call multiply_7(v35, v36), !54 - v38 = get_local ptr u64, old_cap_in_bytes, !55 - store v37 to v38, !55 - v39 = get_local ptr u64, new_cap_in_bytes, !56 - v40 = load v39 - v41 = get_local ptr u64, old_buf_ptr, !57 - v42 = load v41 - v43 = get_local ptr u64, old_cap_in_bytes, !58 - v44 = load v43 - v45 = asm(new_cap_in_bytes: v40, old_buf_ptr: v42, old_cap_in_bytes: v44) -> u64 hp, !59 { - log hp hp hp hp, !60 - aloc new_cap_in_bytes, !61 - log hp hp hp hp, !62 - mcp hp old_buf_ptr old_cap_in_bytes, !63 - } - v46 = get_local ptr u64, ptr, !64 - store v45 to v46, !64 - v47 = get_local ptr u64, ptr, !65 - v48 = load v47 - v49 = get_local ptr u64, new_cap, !66 - v50 = load v49 - v51 = get_local ptr { u64, u64 }, __anon_0, !67 - v52 = const u64 0 - v53 = get_elem_ptr v51, ptr u64, v52, !67 - store v48 to v53, !67 - v54 = const u64 1 - v55 = get_elem_ptr v51, ptr u64, v54, !67 - store v50 to v55, !67 - v56 = asm(buf: v51) -> __slice[u64] buf, !68 { - } - v57 = const u64 0 - v58 = get_elem_ptr self, ptr __slice[u64], v57, !69 - store v56 to v58, !69 - v59 = const unit () - br block5(v59) - - block4(): - v60 = const unit () - br block5(v60) - - block5(v61: ()): - v62 = const u64 5, !70 - v63 = call encode_9(v62), !71 - v64 = const u64 1515152261580153489 - log slice v63, v64, !71 - v65 = const u64 0 - v66 = get_elem_ptr self, ptr __slice[u64], v65, !34 - v67 = load v66 - v68 = get_local ptr __slice[u64], __anon_1 - store v67 to v68 - v69 = get_local ptr u64, new_item_idx, !72 - v70 = load v69 - v71 = const u64 8 - v72 = asm(ptr_to_slice: v68, idx: v70, item_len: v71, offset, ptr) -> ptr u64 ptr { - lw ptr ptr_to_slice i0 - mul offset idx item_len - add ptr ptr offset - log ptr ptr ptr ptr - } - v73 = load v72 - v74 = const unit () - ret () v74 - } - - pub fn len_3(self !74: __slice[u64]) -> u64, !75 { - local u64 _ - local { u64, u64 } __tuple_2 - local { u64, u64 } __tuple_2_ - local u64 len - - entry(self: __slice[u64]): - v0 = asm(s: self) -> { u64, u64 } s, !76 { - } - v1 = get_local ptr { u64, u64 }, __tuple_2, !77 - store v0 to v1, !77 - v2 = get_local ptr { u64, u64 }, __tuple_2, !77 - v3 = get_local ptr { u64, u64 }, __tuple_2_, !77 - store v2 to v3, !77 - v4 = get_local ptr { u64, u64 }, __tuple_2_, !77 - v5 = const u64 0 - v6 = get_elem_ptr v4, ptr u64, v5, !77 - v7 = get_local ptr u64, _, !77 - store v6 to v7, !77 - v8 = get_local ptr { u64, u64 }, __tuple_2_, !77 - v9 = const u64 1 - v10 = get_elem_ptr v8, ptr u64, v9, !77 - v11 = get_local ptr u64, len, !77 - store v10 to v11, !77 - v12 = get_local ptr u64, len, !78 - v13 = load v12 - ret u64 v13 - } - - pub fn ge_4(self !80: u64, other !81: u64) -> bool, !82 { - entry(self: u64, other: u64): - v0 = call gt_5(self, other), !83 - cbr v0, block1(v0), block0(), !84 - - block0(): - v1 = call eq_6(self, other), !85 - br block1(v1), !84 - - block1(v2: bool): - ret bool v2 - } - - pub fn gt_5(self !86: u64, other !87: u64) -> bool, !88 { - entry(self: u64, other: u64): - v0 = cmp gt self other - ret bool v0 - } - - pub fn eq_6(self !89: u64, other !90: u64) -> bool, !91 { - entry(self: u64, other: u64): - v0 = cmp eq self other - ret bool v0 - } - - pub fn multiply_7(self !92: u64, other !93: u64) -> u64, !94 { - entry(self: u64, other: u64): - v0 = mul self, other - ret u64 v0 - } - - pub fn ptr_8(self !95: __slice[u64]) -> u64, !96 { - local u64 _ - local { u64, u64 } __tuple_1 - local { u64, u64 } __tuple_1_ - local u64 ptr - - entry(self: __slice[u64]): - v0 = asm(s: self) -> { u64, u64 } s, !97 { - } - v1 = get_local ptr { u64, u64 }, __tuple_1, !98 - store v0 to v1, !98 - v2 = get_local ptr { u64, u64 }, __tuple_1, !98 - v3 = get_local ptr { u64, u64 }, __tuple_1_, !98 - store v2 to v3, !98 - v4 = get_local ptr { u64, u64 }, __tuple_1_, !98 - v5 = const u64 0 - v6 = get_elem_ptr v4, ptr u64, v5, !98 - v7 = get_local ptr u64, ptr, !98 - store v6 to v7, !98 - v8 = get_local ptr { u64, u64 }, __tuple_1_, !98 - v9 = const u64 1 - v10 = get_elem_ptr v8, ptr u64, v9, !98 - v11 = get_local ptr u64, _, !98 - store v10 to v11, !98 - v12 = get_local ptr u64, ptr, !99 - v13 = load v12 - ret u64 v13 - } - - pub fn encode_9(item !101: u64) -> slice, !102 { - local { { u64, u64, u64 } } buffer - - entry(item: u64): - v0 = call new_11(), !103 - v1 = call abi_encode_10(item, v0), !104 - v2 = get_local ptr { { u64, u64, u64 } }, buffer, !105 - store v1 to v2, !105 - v3 = get_local ptr { { u64, u64, u64 } }, buffer, !106 - v4 = load v3 - v5 = call as_raw_slice_12(v4), !107 - ret slice v5 - } - - pub fn abi_encode_10(self !108: u64, buffer !109: { { u64, u64, u64 } }) -> { { u64, u64, u64 } }, !110 { - local { u64, u64, u64 } __anon_0 - local { u64, u64, u64 } __anon_1 - local { { u64, u64, u64 } } __anon_2 - - entry(self: u64, buffer: { { u64, u64, u64 } }): - v0 = ptr_to_int buffer to u64 - v1 = int_to_ptr v0 to ptr { { u64, u64, u64 } } - v2 = const u64 0 - v3 = get_elem_ptr v1, ptr { u64, u64, u64 }, v2, !111 - v4 = load v3 - v5 = asm(buffer: v4) -> { u64, u64, u64 } buffer { - } - v6 = get_local ptr { u64, u64, u64 }, __anon_0 - store v5 to v6 - v7 = const u64 0 - v8 = get_elem_ptr v6, ptr u64, v7 - v9 = load v8 - v10 = int_to_ptr v9 to ptr u8 - v11 = const u64 1 - v12 = get_elem_ptr v6, ptr u64, v11 - v13 = load v12 - v14 = const u64 2 - v15 = get_elem_ptr v6, ptr u64, v14 - v16 = load v15 - v17 = const u64 8 - v18 = add v16, v17 - v19 = cmp gt v18 v13 - cbr v19, block1(), block2() - - block0(v20: ptr u8, v21: u64): - v22 = ptr_to_int v20 to u64 - v23 = add v22, v16 - v24 = int_to_ptr v23 to ptr u64 - store self to v24 - v25 = const u64 8 - v26 = add v16, v25 - v27 = ptr_to_int v20 to u64 - v28 = get_local ptr { u64, u64, u64 }, __anon_1 - v29 = const u64 0 - v30 = get_elem_ptr v28, ptr u64, v29 - store v27 to v30 - v31 = const u64 1 - v32 = get_elem_ptr v28, ptr u64, v31 - store v21 to v32 - v33 = const u64 2 - v34 = get_elem_ptr v28, ptr u64, v33 - store v26 to v34 - v35 = asm(buffer: v28) -> { u64, u64, u64 } buffer { - } - v36 = get_local ptr { { u64, u64, u64 } }, __anon_2, !112 - v37 = const u64 0 - v38 = get_elem_ptr v36, ptr { u64, u64, u64 }, v37 - store v35 to v38, !112 - v39 = load v36 - ret { { u64, u64, u64 } } v39 - - block1(): - v40 = const u64 2 - v41 = mul v13, v40 - v42 = asm(new_cap: v41, old_ptr: v10, len: v16) -> ptr u8 hp { - aloc new_cap - mcp hp old_ptr len - } - br block0(v42, v41) - - block2(): - br block0(v10, v13) - } - - pub fn new_11() -> { { u64, u64, u64 } }, !113 { - local { u64, u64, u64 } __anon_0 - local { { u64, u64, u64 } } __anon_1 - - entry(): - v0 = const u64 1024 - v1 = asm(cap: v0) -> u64 hp { - aloc cap - } - v2 = int_to_ptr v1 to ptr u8 - v3 = ptr_to_int v2 to u64 - v4 = get_local ptr { u64, u64, u64 }, __anon_0 - v5 = const u64 0 - v6 = get_elem_ptr v4, ptr u64, v5 - store v3 to v6 - v7 = const u64 1 - v8 = get_elem_ptr v4, ptr u64, v7 - store v0 to v8 - v9 = const u64 2 - v10 = get_elem_ptr v4, ptr u64, v9 - v11 = const u64 0 - store v11 to v10 - v12 = asm(buffer: v4) -> { u64, u64, u64 } buffer { - } - v13 = get_local ptr { { u64, u64, u64 } }, __anon_1, !114 - v14 = const u64 0 - v15 = get_elem_ptr v13, ptr { u64, u64, u64 }, v14 - store v12 to v15, !114 - v16 = load v13 - ret { { u64, u64, u64 } } v16 - } - - pub fn as_raw_slice_12(self !115: { { u64, u64, u64 } }) -> slice, !116 { - local { u64, u64, u64 } __anon_0 - local { u64, u64 } __anon_1 - - entry(self: { { u64, u64, u64 } }): - v0 = ptr_to_int self to u64 - v1 = int_to_ptr v0 to ptr { { u64, u64, u64 } } - v2 = const u64 0 - v3 = get_elem_ptr v1, ptr { u64, u64, u64 }, v2, !111 - v4 = load v3 - v5 = asm(buffer: v4) -> { u64, u64, u64 } buffer { - } - v6 = get_local ptr { u64, u64, u64 }, __anon_0 - store v5 to v6 - v7 = const u64 0 - v8 = get_elem_ptr v6, ptr u64, v7 - v9 = load v8 - v10 = int_to_ptr v9 to ptr u8 - v11 = const u64 1 - v12 = get_elem_ptr v6, ptr u64, v11 - v13 = load v12 - v14 = const u64 2 - v15 = get_elem_ptr v6, ptr u64, v14 - v16 = load v15 - v17 = ptr_to_int v10 to u64 - v18 = get_local ptr { u64, u64 }, __anon_1 - v19 = const u64 0 - v20 = get_elem_ptr v18, ptr u64, v19 - store v17 to v20 - v21 = const u64 1 - v22 = get_elem_ptr v18, ptr u64, v21 - store v16 to v22 - v23 = asm(s: v18) -> slice s { - } - ret slice v23 - } - - fn assert_13(l !117: u64, r !118: u64) -> (), !119 { - entry(l: u64, r: u64): - v0 = call neq_14(l, r), !120 - cbr v0, block0(), block1(), !120 - - block0(): - v1 = call encode_9(l), !121 - v2 = const u64 1515152261580153489 - log slice v1, v2, !121 - v3 = call encode_9(r), !122 - v4 = const u64 1515152261580153489 - log slice v3, v4, !122 - v5 = const u64 1, !123 - revert v5, !124 - - block1(): - v6 = const unit () - br block2(v6) - - block2(v7: ()): - v8 = const unit () - ret () v8 - } - - pub fn neq_14(self !125: u64, other !126: u64) -> bool, !127 { - entry(self: u64, other: u64): - v0 = call eq_6(self, other), !128 - v1 = call not_15(v0), !129 - ret bool v1 - } - - pub fn not_15(self !130: bool) -> bool, !131 { - entry(self: bool): - v0 = const bool false, !132 - v1 = cmp eq self v0 - ret bool v1 - } - - pub fn get_16(self !133: { __slice[u64], u64 }, index !134: u64) -> u64, !135 { - local __slice[u64] __anon_0 - - entry(self: { __slice[u64], u64 }, index: u64): - v0 = ptr_to_int self to u64 - v1 = int_to_ptr v0 to ptr { __slice[u64], u64 } - v2 = const u64 0 - v3 = get_elem_ptr v1, ptr __slice[u64], v2, !34 - v4 = load v3 - v5 = get_local ptr __slice[u64], __anon_0 - store v4 to v5 - v6 = const u64 8 - v7 = asm(ptr_to_slice: v5, idx: index, item_len: v6, offset, ptr) -> ptr u64 ptr { - lw ptr ptr_to_slice i0 - mul offset idx item_len - add ptr ptr offset - log ptr ptr ptr ptr - } - v8 = load v7 - v9 = int_to_ptr v8 to ptr u64, !136 - v10 = load v9 - ret u64 v10 - } - - pub fn encode_17(item !101: ()) -> slice, !102 { - local { { u64, u64, u64 } } buffer - - entry(item: ()): - v0 = call new_11(), !103 - v1 = call abi_encode_18(item, v0), !104 - v2 = get_local ptr { { u64, u64, u64 } }, buffer, !105 - store v1 to v2, !105 - v3 = get_local ptr { { u64, u64, u64 } }, buffer, !106 - v4 = load v3 - v5 = call as_raw_slice_12(v4), !107 - ret slice v5 - } - - pub fn abi_encode_18(self !137: (), buffer !138: { { u64, u64, u64 } }) -> { { u64, u64, u64 } }, !139 { - entry(self: (), buffer: { { u64, u64, u64 } }): - ret { { u64, u64, u64 } } buffer - } - } - - !0 = "" - !1 = span !0 0 123 - !2 = span !0 65 71 - !3 = span !0 48 72 - !4 = span !0 102 108 - !5 = span !0 89 109 - !6 = "/home/xunilrj/github/sway/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw" - !7 = span !6 1517 1624 - !8 = span !6 1556 1566 - !9 = span !6 1534 1567 - !10 = span !6 1572 1573 - !11 = span !6 1579 1580 - !12 = span !6 1572 1581 - !13 = span !6 1593 1595 - !14 = span !6 1587 1596 - !15 = span !6 1609 1610 - !16 = span !6 1615 1616 - !17 = span !6 1609 1617 - !18 = span !6 1619 1620 - !19 = span !6 1602 1621 - !20 = span !6 89 309 - !21 = span !6 130 171 - !22 = span !6 120 172 - !23 = span !6 215 218 - !24 = span !6 214 223 - !25 = span !6 221 222 - !26 = span !6 205 273 - !27 = span !6 181 303 - !28 = span !6 292 293 - !29 = span !6 335 339 - !30 = span !6 341 346 - !31 = span !6 315 1291 - !32 = span !6 55 63 - !33 = span !6 361 389 - !34 = span !6 33 49 - !35 = span !6 416 430 - !36 = span !6 398 431 - !37 = span !6 443 455 - !38 = span !6 459 470 - !39 = span !6 443 470 - !40 = span !6 502 513 - !41 = span !6 517 518 - !42 = span !6 502 518 - !43 = span !6 537 538 - !44 = span !6 576 587 - !45 = span !6 590 591 - !46 = span !6 576 591 - !47 = span !6 485 606 - !48 = span !6 642 649 - !49 = span !6 642 668 - !50 = span !6 619 669 - !51 = span !6 701 715 - !52 = span !6 683 716 - !53 = span !6 752 763 - !54 = span !6 752 782 - !55 = span !6 729 783 - !56 = span !6 829 845 - !57 = span !6 860 871 - !58 = span !6 891 907 - !59 = span !6 807 1110 - !60 = span !6 927 942 - !61 = span !6 960 981 - !62 = span !6 999 1014 - !63 = span !6 1032 1067 - !64 = span !6 797 1111 - !65 = span !6 1146 1149 - !66 = span !6 1151 1158 - !67 = span !6 1145 1159 - !68 = span !6 1136 1209 - !69 = span !6 1125 1209 - !70 = span !6 1236 1237 - !71 = span !6 1230 1238 - !72 = span !6 1271 1283 - !73 = "/home/xunilrj/github/sway/sway-lib-core/src/slice.sw" - !74 = span !73 197 201 - !75 = span !73 186 308 - !76 = span !73 235 289 - !77 = span !73 220 290 - !78 = span !73 299 302 - !79 = "/home/xunilrj/github/sway/sway-lib-core/src/ops.sw" - !80 = span !79 21821 21825 - !81 = span !79 21827 21832 - !82 = span !79 21815 21896 - !83 = span !79 21858 21872 - !84 = span !79 21858 21890 - !85 = span !79 21876 21890 - !86 = span !79 15138 15142 - !87 = span !79 15144 15149 - !88 = span !79 15132 15198 - !89 = span !79 12661 12665 - !90 = span !79 12667 12672 - !91 = span !79 12655 12721 - !92 = span !79 4997 5001 - !93 = span !79 5003 5008 - !94 = span !79 4985 5058 - !95 = span !73 66 70 - !96 = span !73 55 180 - !97 = span !73 108 161 - !98 = span !73 93 162 - !99 = span !73 171 174 - !100 = "/home/xunilrj/github/sway/sway-lib-core/src/codec.sw" - !101 = span !100 64661 64665 - !102 = span !100 64644 64785 - !103 = span !100 64742 64755 - !104 = span !100 64726 64756 - !105 = span !100 64713 64757 - !106 = span !100 64762 64768 - !107 = span !100 64762 64783 - !108 = span !100 4642 4646 - !109 = span !100 4648 4654 - !110 = span !100 4628 4773 - !111 = span !100 55 82 - !112 = span !100 4684 4767 - !113 = span !100 128 228 - !114 = span !100 159 222 - !115 = span !100 483 487 - !116 = span !100 467 559 - !117 = span !6 1394 1395 - !118 = span !6 1400 1401 - !119 = span !6 1381 1515 - !120 = span !6 1443 1449 - !121 = span !6 1460 1468 - !122 = span !6 1478 1486 - !123 = span !6 1505 1506 - !124 = span !6 1496 1507 - !125 = span !79 12285 12289 - !126 = span !79 12291 12296 - !127 = span !79 12278 12350 - !128 = span !79 12323 12337 - !129 = span !79 12322 12344 - !130 = span !79 9972 9976 - !131 = span !79 9965 10019 - !132 = span !79 10007 10012 - !133 = span !6 1308 1312 - !134 = span !6 1314 1319 - !135 = span !6 1297 1377 - !136 = span !6 1341 1371 - !137 = span !100 36453 36457 - !138 = span !100 36459 36465 - !139 = span !100 36439 36507 - - - Failed to compile slice_intrinsics - warning - --> /home/xunilrj/github/sway/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw:49:9 -  | - 47 | - 48 | __log(5); - 49 | __slice_elem(self.buf, new_item_idx); -  | ------------------------------------ This returns a value of type &mut T, which is not assigned to anything and is ignored. - 50 | } -  | - ____ - - error: Internal compiler error: Verification failed: Store value and pointer type mismatch. - Please file an issue on the repository and include the code that triggered this error. - ____ - -  Aborting due to 1 error. - -_________________________________ -Sway tests result: failed. 828 total, 0 passed; 1 failed; 7 disabled [test duration: 00:00:410] -Failing tests: - should_pass/language/slice/slice_intrinsics ... failed From ac16800bc2d209b97d6b51e4633d9aa513d995de Mon Sep 17 00:00:00 2001 From: xunilrj Date: Thu, 25 Jul 2024 13:17:14 +0100 Subject: [PATCH 18/22] __elem_at working for slices and arrays --- sway-ast/src/intrinsics.rs | 8 +- sway-core/src/ir_generation/const_eval.rs | 4 +- sway-core/src/ir_generation/function.rs | 244 +++++++----------- .../ast_node/expression/intrinsic_function.rs | 78 +++--- .../semantic_analysis/cei_pattern_analysis.rs | 2 +- .../slice/slice_intrinsics/src/main.sw | 36 +-- 6 files changed, 163 insertions(+), 209 deletions(-) diff --git a/sway-ast/src/intrinsics.rs b/sway-ast/src/intrinsics.rs index 1f9d98e62bc..182d47fa3d9 100644 --- a/sway-ast/src/intrinsics.rs +++ b/sway-ast/src/intrinsics.rs @@ -41,8 +41,8 @@ pub enum Intrinsic { EncodeBufferEmpty, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_empty() EncodeBufferAppend, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_append(buffer, primitive data type) EncodeBufferAsRawSlice, // let slice: raw_slice = __encode_buffer_as_raw_slice(buffer) - Slice, // let ref_to_slice = __slice(array or ref to slice, inclusive_start_index, exclusive_end_index) - SliceElem, // let ref_to_item = __slice_elem(ref to slice, index) + Slice, // let ref_to_slice = __slice::(item: T, inclusive_start_index, exclusive_end_index) + ElemAt, // let elem: &T = __elem_at::(item: T, index) } impl fmt::Display for Intrinsic { @@ -88,7 +88,7 @@ impl fmt::Display for Intrinsic { Intrinsic::EncodeBufferAppend => "encode_buffer_append", Intrinsic::EncodeBufferAsRawSlice => "encode_buffer_as_raw_slice", Intrinsic::Slice => "slice", - Intrinsic::SliceElem => "slice_elem", + Intrinsic::ElemAt => "elem_at", }; write!(f, "{s}") } @@ -138,7 +138,7 @@ impl Intrinsic { "__encode_buffer_append" => EncodeBufferAppend, "__encode_buffer_as_raw_slice" => EncodeBufferAsRawSlice, "__slice" => Slice, - "__slice_elem" => SliceElem, + "__elem_at" => ElemAt, _ => return None, }) } diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index 401d6d9c4a0..5a321dcc87d 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -627,7 +627,7 @@ fn const_eval_typed_expr( ty::TyExpressionVariant::Deref(expr) => { let value = expr .as_intrinsic() - .filter(|x| matches!(x.kind, Intrinsic::SliceElem)) + .filter(|x| matches!(x.kind, Intrinsic::ElemAt)) .ok_or(ConstEvalError::CompileError) .and_then(|kind| const_eval_intrinsic(lookup, known_consts, kind)); if let Ok(Some(Constant { @@ -1342,7 +1342,7 @@ fn const_eval_intrinsic( }), } } - Intrinsic::SliceElem => { + Intrinsic::ElemAt => { let idx = args[1].as_uint().expect("Type check allowed non u64") as usize; match &args[0].value { diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index 0203d230340..c2a5ee3b889 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -6,20 +6,14 @@ use super::{ CompiledFunctionCache, }; use crate::{ - engine_threading::*, - ir_generation::const_eval::{ + decl_engine::DeclEngine, engine_threading::*, ir_generation::const_eval::{ compile_constant_expression, compile_constant_expression_to_constant, - }, - language::{ + }, language::{ ty::{ - self, ProjectionKind, TyConfigurableDecl, TyConstantDecl, TyExpressionVariant, - TyStorageField, + self, ProjectionKind, TyConfigurableDecl, TyConstantDecl, TyExpression, TyExpressionVariant, TyStorageField }, *, - }, - metadata::MetadataManager, - type_system::*, - types::*, + }, metadata::MetadataManager, type_system::*, types::* }; use indexmap::IndexMap; @@ -2162,91 +2156,114 @@ impl<'eng> FnCompiler<'eng> { Ok(TerminatorValue::new(buffer, context)) } Intrinsic::Slice => self.compile_intrinsic_slice(arguments, context, md_mgr), - Intrinsic::SliceElem => self.compile_intrinsic_slice_elem(arguments, context, md_mgr), + Intrinsic::ElemAt => self.compile_intrinsic_elem_at(arguments, context, md_mgr), } } - fn compile_intrinsic_slice_elem( - &mut self, - arguments: &[ty::TyExpression], - context: &mut Context, - md_mgr: &mut MetadataManager, - ) -> Result { - assert!(arguments.len() == 2); + fn ptr_to_first_element(&mut self, context: &mut Context, first_argument_expr: &TyExpression, first_argument_value: Value, md_mgr: &mut MetadataManager,) -> Result<(Value, TypeId), CompileError> { + let te = self.engines.te(); + + let err = CompileError::TypeArgumentsNotAllowed { span: first_argument_expr.span.clone() }; + + let first_argument_value = save_to_local_return_ptr(self, context, first_argument_value)?; + + match &*te.get(first_argument_expr.return_type) { + TypeInfo::Array(elem_ty, _) => { + Ok((first_argument_value, elem_ty.type_id)) + }, + TypeInfo::Ref { referenced_type, .. } => match &*te.get(referenced_type.type_id) { + TypeInfo::Slice(elem_ty) => { + let ptr_arg = AsmArg { + name: Ident::new_no_span("ptr".into()), + initializer: Some(first_argument_value), + }; + + let return_type = Type::get_uint64(context); + let ptr_to_first_element = self.current_block.append(context).asm_block( + vec![ + ptr_arg, + ], + vec![ + AsmInstruction::lw_no_span("ptr", "ptr", "i0"), + ], + return_type, + Some(Ident::new_no_span("ptr".into())), + ); + Ok((ptr_to_first_element, elem_ty.type_id)) + }, + _ => Err(err), + }, + _ => Err(err) + } + } + fn advance_ptr_n_elements(&mut self, context: &mut Context, first_argument_expr: &TyExpression, ptr: Value, elem_type_id: TypeId, idx: Value) -> Result<(Value, Type), CompileError> { let te = self.engines.te(); + let de = self.engines.de(); - let ref_to_slice_expr = &arguments[0]; - let slice_type_id = te - .get(ref_to_slice_expr.return_type) - .as_reference() - .unwrap() - .1 - .type_id; - let elem_type_id = te.get(slice_type_id).as_slice().unwrap().type_id; let elem_ir_type = convert_resolved_type_id( - self.engines.te(), - self.engines.de(), + te, + de, context, elem_type_id, - &ref_to_slice_expr.span.clone(), + &first_argument_expr.span.clone(), )?; let elem_ir_type_size = elem_ir_type.size(context); - let ref_to_slice_value = return_on_termination_or_extract!( - self.compile_expression_to_value(context, md_mgr, ref_to_slice_expr)? - ); - let ptr_to_ref_to_slice = save_to_local_return_ptr(self, context, ref_to_slice_value)?; - - let ptr_to_slice_arg = AsmArg { - name: Ident::new_no_span("ptr_to_slice".into()), - initializer: Some(ptr_to_ref_to_slice), - }; - let elem_ir_type_size = to_constant(self, context, elem_ir_type_size.in_bytes()); - let elem_len_arg = AsmArg { - name: Ident::new_no_span("item_len".into()), + let elem_ir_type_size_arg = AsmArg { + name: Ident::new_no_span("elem_ir_type_size".into()), initializer: Some(elem_ir_type_size), }; - let idx = &arguments[1]; - let idx = return_on_termination_or_extract!( - self.compile_expression_to_value(context, md_mgr, idx)? - ); let idx_arg = AsmArg { name: Ident::new_no_span("idx".into()), initializer: Some(idx), }; - let offset_out_arg = AsmArg { - name: Ident::new_no_span("offset".into()), - initializer: None, - }; - let ptr_out_arg = AsmArg { + let ptr_arg = AsmArg { name: Ident::new_no_span("ptr".into()), - initializer: None, + initializer: Some(ptr), }; - let ptr_to_elem_type = Type::get_uint64(context); - + let return_type = Type::get_uint64(context); let ptr = self.current_block.append(context).asm_block( vec![ - ptr_to_slice_arg, idx_arg, - elem_len_arg, - offset_out_arg, - ptr_out_arg, + elem_ir_type_size_arg, + ptr_arg, ], vec![ - AsmInstruction::lw_no_span("ptr", "ptr_to_slice", "i0"), - AsmInstruction::mul_no_span("offset", "idx", "item_len"), - AsmInstruction::add_no_span("ptr", "ptr", "offset"), - AsmInstruction::log_no_span("ptr", "offset", "idx", "item_len"), + AsmInstruction::mul_no_span("idx", "idx", "elem_ir_type_size"), + AsmInstruction::add_no_span("ptr", "ptr", "idx"), ], - ptr_to_elem_type, + return_type, Some(Ident::new_no_span("ptr".into())), ); - Ok(TerminatorValue::new(ptr, context)) + Ok((ptr, elem_ir_type)) + } + + fn compile_intrinsic_elem_at( + &mut self, + arguments: &[ty::TyExpression], + context: &mut Context, + md_mgr: &mut MetadataManager, + ) -> Result { + assert!(arguments.len() == 2); + + let first_argument_expr = &arguments[0]; + let first_argument_value = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, first_argument_expr)? + ); + let (ptr_to_first_elem, elem_type_id) = self.ptr_to_first_element(context, first_argument_expr, first_argument_value, md_mgr)?; + + let idx = &arguments[1]; + let idx = return_on_termination_or_extract!( + self.compile_expression_to_value(context, md_mgr, idx)? + ); + let (ptr_to_elem, _) = self.advance_ptr_n_elements(context, first_argument_expr, ptr_to_first_elem, elem_type_id, idx)?; + + Ok(TerminatorValue::new(ptr_to_elem, context)) } fn compile_intrinsic_slice( @@ -2257,117 +2274,32 @@ impl<'eng> FnCompiler<'eng> { ) -> Result { assert!(arguments.len() == 3); - let uint64 = Type::get_uint64(context); - let ptr_u64 = Type::new_ptr(context, uint64); - - let first_argument = &arguments[0]; - let elem_type_id = match &*self.engines.te().get(first_argument.return_type) { - TypeInfo::Array(t, _) => t.type_id, - TypeInfo::Slice(t) => t.type_id, - _ => unreachable!(), - }; - let elem_ir_type = convert_resolved_type_id( - self.engines.te(), - self.engines.de(), - context, - elem_type_id, - &first_argument.span.clone(), - )?; - let item_ir_type_size = elem_ir_type.size(context); + let first_argument_expr = &arguments[0]; let first_argument_value = return_on_termination_or_extract!( - self.compile_expression_to_value(context, md_mgr, first_argument)? + self.compile_expression_to_value(context, md_mgr, first_argument_expr)? ); - let ptr_to_first_argument = save_to_local_return_ptr(self, context, first_argument_value)?; - let ptr_to_elements = match &*self.engines.te().get(first_argument.return_type) { - TypeInfo::Array(_, _) => self - .current_block - .append(context) - .ptr_to_int(ptr_to_first_argument, uint64), - TypeInfo::Slice(_) => { - let slice_ptr = self - .current_block - .append(context) - .cast_ptr(ptr_to_first_argument, ptr_u64); - self.current_block.append(context).load(slice_ptr) - } - _ => unreachable!(), - }; + let (ptr_to_first_elem, elem_type_id) = self.ptr_to_first_element(context, first_argument_expr, first_argument_value, md_mgr)?; let start = &arguments[1]; let start = return_on_termination_or_extract!( self.compile_expression_to_value(context, md_mgr, start)? ); + let (ptr_to_elem, elem_ir_type) = self.advance_ptr_n_elements(context, first_argument_expr, ptr_to_first_elem, elem_type_id, start.clone())?; let end = &arguments[2]; let end = return_on_termination_or_extract!( self.compile_expression_to_value(context, md_mgr, end)? ); - //asm(array_ptr: array, start: start, end: end, item_len: __size_of::(), slice_ptr, slice_len) { - // lw array_ptr array_ptr i0; - // mul slice_ptr start item_len; - // add slice_ptr slice_ptr array_ptr; // byte offset - // sub slice_len end start; - // mul slice_len slice_len item_len; // length in bytes - // (slice_ptr, slice_len): __slice[T] - //}; - - let array_ptr_arg = AsmArg { - name: Ident::new_no_span("array_ptr".into()), - initializer: Some(ptr_to_elements), - }; - let start_arg = AsmArg { - name: Ident::new_no_span("start".into()), - initializer: Some(start), - }; - let end_arg = AsmArg { - name: Ident::new_no_span("end".into()), - initializer: Some(end), - }; - let item_len_arg = AsmArg { - name: Ident::new_no_span("item_len".into()), - initializer: Some(to_constant(self, context, item_ir_type_size.in_bytes())), - }; - - let slice_ptr_out_arg = AsmArg { - name: Ident::new_no_span("slice_ptr".into()), - initializer: None, - }; - let slice_len_out_arg = AsmArg { - name: Ident::new_no_span("slice_len".into()), - initializer: None, - }; - - let slice_ptr = self.current_block.append(context).asm_block( - vec![ - array_ptr_arg, - start_arg.clone(), - item_len_arg.clone(), - slice_ptr_out_arg, - ], - vec![ - AsmInstruction::mul_no_span("slice_ptr", "start", "item_len"), - AsmInstruction::add_no_span("slice_ptr", "slice_ptr", "array_ptr"), - ], - uint64, - Some(Ident::new_no_span("slice_ptr".into())), - ); - - let slice_len = self.current_block.append(context).asm_block( - vec![start_arg, end_arg, item_len_arg, slice_len_out_arg], - vec![ - AsmInstruction::sub_no_span("slice_len", "end", "start"), - AsmInstruction::mul_no_span("slice_len", "slice_len", "item_len"), - ], - uint64, - Some(Ident::new_no_span("slice_len".into())), - ); + let slice_len = self.current_block.append(context) + .binary_op(BinaryOpKind::Sub, end, start); // compile the slice together + let uint64 = Type::get_uint64(context); let return_type = Type::get_typed_slice(context, elem_ir_type); let slice_as_tuple = self.compile_tuple_from_values( context, - vec![slice_ptr, slice_len], + vec![ptr_to_elem, slice_len], vec![uint64, uint64], None, )?; diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index 4b642a06c5e..d6ac875aedf 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -105,12 +105,12 @@ impl ty::TyIntrinsicFunctionKind { Intrinsic::Slice => { type_check_slice(handler, ctx, kind, arguments, type_arguments, span) } - Intrinsic::SliceElem => type_check_slice_elem(arguments, handler, kind, span, ctx), + Intrinsic::ElemAt => type_check_elem_at(arguments, handler, kind, span, ctx), } } } -fn type_check_slice_elem( +fn type_check_elem_at( arguments: &[Expression], handler: &Handler, kind: Intrinsic, @@ -141,11 +141,18 @@ fn type_check_slice_elem( ty::TyExpression::type_check(handler, ctx, &arguments[0])? }; - let first_argument_ref_to_type = type_engine - .get(first_argument_type) - .as_reference() - .map(|(_, t)| type_engine.get(t.type_id)); - let Some(TypeInfo::Slice(elem_type)) = first_argument_ref_to_type.as_deref() else { + // first argument can be array or ref to slice + let elem_type = match &*type_engine.get(first_argument_type) { + TypeInfo::Array(elem_ty, _) => Some(elem_ty.type_id), + TypeInfo::Ref { referenced_type, .. } => { + match &*type_engine.get(referenced_type.type_id) { + TypeInfo::Slice(elem_ty) => Some(elem_ty.type_id), + _ => None + } + }, + _ => None, + }; + let Some(elem_type_type_id) = elem_type else { return Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { name: kind.to_string(), span: first_argument_span, @@ -172,8 +179,8 @@ fn type_check_slice_elem( TypeInfo::Ref { to_mutable_value: true, referenced_type: TypeArgument { - type_id: elem_type.type_id, - initial_type_id: elem_type.type_id, + type_id: elem_type_type_id, + initial_type_id: elem_type_type_id, span: Span::dummy(), call_path_tree: None, }, @@ -240,13 +247,13 @@ fn type_check_slice( }; // check first argument - let array_span = arguments[0].span.clone(); - let array_type = type_engine.insert(engines, TypeInfo::Unknown, None); - let array_ty_expr = { + let first_argument_span = arguments[0].span.clone(); + let first_argument_type = type_engine.insert(engines, TypeInfo::Unknown, None); + let first_argument_ty_expr = { let ctx = ctx .by_ref() .with_help_text("") - .with_type_annotation(array_type); + .with_type_annotation(first_argument_type); ty::TyExpression::type_check(handler, ctx, &arguments[0])? }; @@ -290,7 +297,12 @@ fn type_check_slice( } // We can slice arrays or other slices - match &*type_engine.get(array_type) { + let err = CompileError::IntrinsicUnsupportedArgType { + name: kind.to_string(), + span: first_argument_span, + hint: "".to_string(), + }; + let r = match &*type_engine.get(first_argument_type) { TypeInfo::Array(elem_type_arg, array_len) => { let array_len = array_len.val() as u64; @@ -314,30 +326,36 @@ fn type_check_slice( } } - Ok(( + Some(( TyIntrinsicFunctionKind { kind, - arguments: vec![array_ty_expr, start_ty_expr, end_ty_expr], + arguments: vec![first_argument_ty_expr, start_ty_expr, end_ty_expr], type_arguments: vec![], span, }, create_ref_to_slice(engines, elem_type_arg.clone()), )) } - TypeInfo::Slice(elem_type_arg) => Ok(( - TyIntrinsicFunctionKind { - kind, - arguments: vec![array_ty_expr, start_ty_expr, end_ty_expr], - type_arguments: vec![], - span, - }, - create_ref_to_slice(engines, elem_type_arg.clone()), - )), - _ => Err(handler.emit_err(CompileError::IntrinsicUnsupportedArgType { - name: kind.to_string(), - span: array_span, - hint: "".to_string(), - })), + TypeInfo::Ref { referenced_type, .. } => { + match &*type_engine.get(referenced_type.type_id) { + TypeInfo::Slice(elem_type_arg) => Some(( + TyIntrinsicFunctionKind { + kind, + arguments: vec![first_argument_ty_expr, start_ty_expr, end_ty_expr], + type_arguments: vec![], + span, + }, + create_ref_to_slice(engines, elem_type_arg.clone()), + )), + _ => None, + } + }, + _ => None, + }; + + match r { + Some(r) => Ok(r), + None => Err(handler.emit_err(err)), } } diff --git a/sway-core/src/semantic_analysis/cei_pattern_analysis.rs b/sway-core/src/semantic_analysis/cei_pattern_analysis.rs index 51db787fa75..1dbad596ed4 100644 --- a/sway-core/src/semantic_analysis/cei_pattern_analysis.rs +++ b/sway-core/src/semantic_analysis/cei_pattern_analysis.rs @@ -649,7 +649,7 @@ fn effects_of_intrinsic(intr: &sway_ast::Intrinsic) -> HashSet { | EncodeBufferAppend | EncodeBufferAsRawSlice | Slice - | SliceElem => HashSet::new(), + | ElemAt => HashSet::new(), } } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 17850c5571e..65d119eb9ea 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -73,7 +73,7 @@ impl Vec { (" After realloc: ", self).dbgln(); } - let v: &mut T = __slice_elem(self.buf, new_item_idx); + let v: &mut T = __elem_at(self.buf, new_item_idx); let buffer_addr = asm(v: self.buf.ptr()) { v: u64 }; let elem_addr = asm(v: v) { v: u64 }; @@ -89,7 +89,7 @@ impl Vec { ("Vec::get(", index, ")").dbgln(); (" ", self).dbgln(); - let item: &mut T = __slice_elem(self.buf, index); + let item: &mut T = __elem_at(self.buf, index); let buffer_addr = asm(v: self.buf.ptr()) { v: u64 }; let elem_addr = asm(v: item) { v: u64 }; @@ -111,23 +111,27 @@ where } fn main() { - // slice arrays - let a: [u64; 5] = [1, 2, 3, 4, 5]; - - + // index arrays + let some_array: [u64; 5] = [1, 2, 3, 4, 5]; + assert(1, *__elem_at(some_array, 0)); + assert(2, *__elem_at(some_array, 1)); + assert(3, *__elem_at(some_array, 2)); + assert(4, *__elem_at(some_array, 3)); + assert(5, *__elem_at(some_array, 4)); - let s = __slice(a, 0, 5); - assert(1, *__slice_elem(s, 0)); - assert(2, *__slice_elem(s, 1)); - assert(3, *__slice_elem(s, 2)); - assert(4, *__slice_elem(s, 3)); - assert(5, *__slice_elem(s, 4)); + // slice arrays + let some_slice: &__slice[u64] = __slice(some_array, 0, 5); + assert(1, *__elem_at(some_slice, 0)); + assert(2, *__elem_at(some_slice, 1)); + assert(3, *__elem_at(some_slice, 2)); + assert(4, *__elem_at(some_slice, 3)); + assert(5, *__elem_at(some_slice, 4)); // slice another slice - let s = __slice(a, 1, 4); - assert(2, *__slice_elem(s, 0)); - assert(3, *__slice_elem(s, 1)); - assert(4, *__slice_elem(s, 2)); + let another_slice: &__slice[u64] = __slice(some_slice, 1, 4); + assert(2, *__elem_at(another_slice, 0)); + assert(3, *__elem_at(another_slice, 1)); + assert(4, *__elem_at(another_slice, 2)); // Vec impl using slices let mut v: Vec = Vec::new(); From 47629a73578a3b521b8930eb99f74725ef7b376b Mon Sep 17 00:00:00 2001 From: xunilrj Date: Thu, 25 Jul 2024 13:19:33 +0100 Subject: [PATCH 19/22] clippy and fmt issues --- sway-core/src/ir_generation/function.rs | 89 ++++++++++++------- .../ast_node/expression/intrinsic_function.rs | 38 ++++---- 2 files changed, 78 insertions(+), 49 deletions(-) diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index c2a5ee3b889..e3134d7e9eb 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -6,14 +6,20 @@ use super::{ CompiledFunctionCache, }; use crate::{ - decl_engine::DeclEngine, engine_threading::*, ir_generation::const_eval::{ + engine_threading::*, + ir_generation::const_eval::{ compile_constant_expression, compile_constant_expression_to_constant, - }, language::{ + }, + language::{ ty::{ - self, ProjectionKind, TyConfigurableDecl, TyConstantDecl, TyExpression, TyExpressionVariant, TyStorageField + self, ProjectionKind, TyConfigurableDecl, TyConstantDecl, TyExpression, + TyExpressionVariant, TyStorageField, }, *, - }, metadata::MetadataManager, type_system::*, types::* + }, + metadata::MetadataManager, + type_system::*, + types::*, }; use indexmap::IndexMap; @@ -2160,18 +2166,26 @@ impl<'eng> FnCompiler<'eng> { } } - fn ptr_to_first_element(&mut self, context: &mut Context, first_argument_expr: &TyExpression, first_argument_value: Value, md_mgr: &mut MetadataManager,) -> Result<(Value, TypeId), CompileError> { + fn ptr_to_first_element( + &mut self, + context: &mut Context, + first_argument_expr: &TyExpression, + first_argument_value: Value, + _md_mgr: &mut MetadataManager, + ) -> Result<(Value, TypeId), CompileError> { let te = self.engines.te(); - let err = CompileError::TypeArgumentsNotAllowed { span: first_argument_expr.span.clone() }; - + let err = CompileError::TypeArgumentsNotAllowed { + span: first_argument_expr.span.clone(), + }; + let first_argument_value = save_to_local_return_ptr(self, context, first_argument_value)?; match &*te.get(first_argument_expr.return_type) { - TypeInfo::Array(elem_ty, _) => { - Ok((first_argument_value, elem_ty.type_id)) - }, - TypeInfo::Ref { referenced_type, .. } => match &*te.get(referenced_type.type_id) { + TypeInfo::Array(elem_ty, _) => Ok((first_argument_value, elem_ty.type_id)), + TypeInfo::Ref { + referenced_type, .. + } => match &*te.get(referenced_type.type_id) { TypeInfo::Slice(elem_ty) => { let ptr_arg = AsmArg { name: Ident::new_no_span("ptr".into()), @@ -2180,24 +2194,27 @@ impl<'eng> FnCompiler<'eng> { let return_type = Type::get_uint64(context); let ptr_to_first_element = self.current_block.append(context).asm_block( - vec![ - ptr_arg, - ], - vec![ - AsmInstruction::lw_no_span("ptr", "ptr", "i0"), - ], + vec![ptr_arg], + vec![AsmInstruction::lw_no_span("ptr", "ptr", "i0")], return_type, Some(Ident::new_no_span("ptr".into())), ); Ok((ptr_to_first_element, elem_ty.type_id)) - }, + } _ => Err(err), }, - _ => Err(err) + _ => Err(err), } } - fn advance_ptr_n_elements(&mut self, context: &mut Context, first_argument_expr: &TyExpression, ptr: Value, elem_type_id: TypeId, idx: Value) -> Result<(Value, Type), CompileError> { + fn advance_ptr_n_elements( + &mut self, + context: &mut Context, + first_argument_expr: &TyExpression, + ptr: Value, + elem_type_id: TypeId, + idx: Value, + ) -> Result<(Value, Type), CompileError> { let te = self.engines.te(); let de = self.engines.de(); @@ -2227,11 +2244,7 @@ impl<'eng> FnCompiler<'eng> { let return_type = Type::get_uint64(context); let ptr = self.current_block.append(context).asm_block( - vec![ - idx_arg, - elem_ir_type_size_arg, - ptr_arg, - ], + vec![idx_arg, elem_ir_type_size_arg, ptr_arg], vec![ AsmInstruction::mul_no_span("idx", "idx", "elem_ir_type_size"), AsmInstruction::add_no_span("ptr", "ptr", "idx"), @@ -2255,13 +2268,20 @@ impl<'eng> FnCompiler<'eng> { let first_argument_value = return_on_termination_or_extract!( self.compile_expression_to_value(context, md_mgr, first_argument_expr)? ); - let (ptr_to_first_elem, elem_type_id) = self.ptr_to_first_element(context, first_argument_expr, first_argument_value, md_mgr)?; + let (ptr_to_first_elem, elem_type_id) = + self.ptr_to_first_element(context, first_argument_expr, first_argument_value, md_mgr)?; let idx = &arguments[1]; let idx = return_on_termination_or_extract!( self.compile_expression_to_value(context, md_mgr, idx)? ); - let (ptr_to_elem, _) = self.advance_ptr_n_elements(context, first_argument_expr, ptr_to_first_elem, elem_type_id, idx)?; + let (ptr_to_elem, _) = self.advance_ptr_n_elements( + context, + first_argument_expr, + ptr_to_first_elem, + elem_type_id, + idx, + )?; Ok(TerminatorValue::new(ptr_to_elem, context)) } @@ -2278,20 +2298,29 @@ impl<'eng> FnCompiler<'eng> { let first_argument_value = return_on_termination_or_extract!( self.compile_expression_to_value(context, md_mgr, first_argument_expr)? ); - let (ptr_to_first_elem, elem_type_id) = self.ptr_to_first_element(context, first_argument_expr, first_argument_value, md_mgr)?; + let (ptr_to_first_elem, elem_type_id) = + self.ptr_to_first_element(context, first_argument_expr, first_argument_value, md_mgr)?; let start = &arguments[1]; let start = return_on_termination_or_extract!( self.compile_expression_to_value(context, md_mgr, start)? ); - let (ptr_to_elem, elem_ir_type) = self.advance_ptr_n_elements(context, first_argument_expr, ptr_to_first_elem, elem_type_id, start.clone())?; + let (ptr_to_elem, elem_ir_type) = self.advance_ptr_n_elements( + context, + first_argument_expr, + ptr_to_first_elem, + elem_type_id, + start, + )?; let end = &arguments[2]; let end = return_on_termination_or_extract!( self.compile_expression_to_value(context, md_mgr, end)? ); - let slice_len = self.current_block.append(context) + let slice_len = self + .current_block + .append(context) .binary_op(BinaryOpKind::Sub, end, start); // compile the slice together diff --git a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs index d6ac875aedf..e041de4bf45 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs @@ -144,11 +144,11 @@ fn type_check_elem_at( // first argument can be array or ref to slice let elem_type = match &*type_engine.get(first_argument_type) { TypeInfo::Array(elem_ty, _) => Some(elem_ty.type_id), - TypeInfo::Ref { referenced_type, .. } => { - match &*type_engine.get(referenced_type.type_id) { - TypeInfo::Slice(elem_ty) => Some(elem_ty.type_id), - _ => None - } + TypeInfo::Ref { + referenced_type, .. + } => match &*type_engine.get(referenced_type.type_id) { + TypeInfo::Slice(elem_ty) => Some(elem_ty.type_id), + _ => None, }, _ => None, }; @@ -336,20 +336,20 @@ fn type_check_slice( create_ref_to_slice(engines, elem_type_arg.clone()), )) } - TypeInfo::Ref { referenced_type, .. } => { - match &*type_engine.get(referenced_type.type_id) { - TypeInfo::Slice(elem_type_arg) => Some(( - TyIntrinsicFunctionKind { - kind, - arguments: vec![first_argument_ty_expr, start_ty_expr, end_ty_expr], - type_arguments: vec![], - span, - }, - create_ref_to_slice(engines, elem_type_arg.clone()), - )), - _ => None, - } - }, + TypeInfo::Ref { + referenced_type, .. + } => match &*type_engine.get(referenced_type.type_id) { + TypeInfo::Slice(elem_type_arg) => Some(( + TyIntrinsicFunctionKind { + kind, + arguments: vec![first_argument_ty_expr, start_ty_expr, end_ty_expr], + type_arguments: vec![], + span, + }, + create_ref_to_slice(engines, elem_type_arg.clone()), + )), + _ => None, + }, _ => None, }; From 0a81c42ed36ad706855629cccb80a056e5b195a3 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Thu, 25 Jul 2024 16:59:14 +0100 Subject: [PATCH 20/22] fix register usage --- sway-core/src/ir_generation/function.rs | 45 ++++++++++++++++--- .../slice/slice_intrinsics/src/main.sw | 6 +++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index e3134d7e9eb..87aff94a9c7 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -2192,13 +2192,19 @@ impl<'eng> FnCompiler<'eng> { initializer: Some(first_argument_value), }; + let ptr_out_arg = AsmArg { + name: Ident::new_no_span("ptr_out".into()), + initializer: Some(first_argument_value), + }; + let return_type = Type::get_uint64(context); let ptr_to_first_element = self.current_block.append(context).asm_block( - vec![ptr_arg], - vec![AsmInstruction::lw_no_span("ptr", "ptr", "i0")], + vec![ptr_arg, ptr_out_arg], + vec![AsmInstruction::lw_no_span("ptr_out", "ptr", "i0")], return_type, - Some(Ident::new_no_span("ptr".into())), + Some(Ident::new_no_span("ptr_out".into())), ); + Ok((ptr_to_first_element, elem_ty.type_id)) } _ => Err(err), @@ -2232,6 +2238,11 @@ impl<'eng> FnCompiler<'eng> { initializer: Some(elem_ir_type_size), }; + let offset_temp_arg = AsmArg { + name: Ident::new_no_span("offset_temp".into()), + initializer: None, + }; + let idx_arg = AsmArg { name: Ident::new_no_span("idx".into()), initializer: Some(idx), @@ -2242,12 +2253,17 @@ impl<'eng> FnCompiler<'eng> { initializer: Some(ptr), }; + let ptr_out_arg = AsmArg { + name: Ident::new_no_span("ptr_out".into()), + initializer: Some(ptr), + }; + let return_type = Type::get_uint64(context); let ptr = self.current_block.append(context).asm_block( - vec![idx_arg, elem_ir_type_size_arg, ptr_arg], + vec![idx_arg, elem_ir_type_size_arg, ptr_arg, offset_temp_arg, ptr_out_arg], vec![ - AsmInstruction::mul_no_span("idx", "idx", "elem_ir_type_size"), - AsmInstruction::add_no_span("ptr", "ptr", "idx"), + AsmInstruction::mul_no_span("offset_temp", "idx", "elem_ir_type_size"), + AsmInstruction::add_no_span("ptr_out", "ptr", "offset_temp"), ], return_type, Some(Ident::new_no_span("ptr".into())), @@ -2318,6 +2334,23 @@ impl<'eng> FnCompiler<'eng> { self.compile_expression_to_value(context, md_mgr, end)? ); + let return_type = Type::get_unit(context); + self.current_block.append(context).asm_block( + vec![AsmArg { + name: Ident::new_no_span("end".into()), + initializer: Some(end), + }, AsmArg { + name: Ident::new_no_span("start".into()), + initializer: Some(start), + }], + vec![ + AsmInstruction::log_no_span("end", "start", "end", "start") + ], + return_type, + None + ); + + let slice_len = self .current_block .append(context) diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw index 65d119eb9ea..6364100f77c 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/slice/slice_intrinsics/src/main.sw @@ -155,4 +155,10 @@ fn main() { assert(v.get(4), 5); assert(v.get(5), 6); assert(v.get(6), 7); + + //indices as expressions + assert(2, *__elem_at(some_array, v.get(0))); + + let _some_slice: &__slice[u64] = __slice(some_array, v.get(0), v.get(4)); + assert(2, *__elem_at(some_slice, v.get(0))); } From c2a7e00b9c522a85b8494915b8e434ac02cae6b6 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Thu, 25 Jul 2024 17:00:39 +0100 Subject: [PATCH 21/22] fmt and clippy issues --- sway-core/src/ir_generation/function.rs | 30 +++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/sway-core/src/ir_generation/function.rs b/sway-core/src/ir_generation/function.rs index 87aff94a9c7..628ea89e903 100644 --- a/sway-core/src/ir_generation/function.rs +++ b/sway-core/src/ir_generation/function.rs @@ -2204,7 +2204,7 @@ impl<'eng> FnCompiler<'eng> { return_type, Some(Ident::new_no_span("ptr_out".into())), ); - + Ok((ptr_to_first_element, elem_ty.type_id)) } _ => Err(err), @@ -2260,7 +2260,13 @@ impl<'eng> FnCompiler<'eng> { let return_type = Type::get_uint64(context); let ptr = self.current_block.append(context).asm_block( - vec![idx_arg, elem_ir_type_size_arg, ptr_arg, offset_temp_arg, ptr_out_arg], + vec![ + idx_arg, + elem_ir_type_size_arg, + ptr_arg, + offset_temp_arg, + ptr_out_arg, + ], vec![ AsmInstruction::mul_no_span("offset_temp", "idx", "elem_ir_type_size"), AsmInstruction::add_no_span("ptr_out", "ptr", "offset_temp"), @@ -2336,21 +2342,21 @@ impl<'eng> FnCompiler<'eng> { let return_type = Type::get_unit(context); self.current_block.append(context).asm_block( - vec![AsmArg { - name: Ident::new_no_span("end".into()), - initializer: Some(end), - }, AsmArg { - name: Ident::new_no_span("start".into()), - initializer: Some(start), - }], vec![ - AsmInstruction::log_no_span("end", "start", "end", "start") + AsmArg { + name: Ident::new_no_span("end".into()), + initializer: Some(end), + }, + AsmArg { + name: Ident::new_no_span("start".into()), + initializer: Some(start), + }, ], + vec![AsmInstruction::log_no_span("end", "start", "end", "start")], return_type, - None + None, ); - let slice_len = self .current_block .append(context) From 8c17162f5d1fe8b8b2feafb30248ca20712b4303 Mon Sep 17 00:00:00 2001 From: xunilrj Date: Fri, 26 Jul 2024 14:33:40 +0100 Subject: [PATCH 22/22] testing wrong index types --- .../slice/slice_intrinsics/src/main.sw | 16 ++++++++++++++-- .../slice/slice_intrinsics/test.toml | 17 +++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/src/main.sw index b2bb36b32ab..919047ccbf1 100644 --- a/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/src/main.sw @@ -21,8 +21,20 @@ fn main() { __slice(0, 0, 0); // Wrong start index - __slice(0, "", 0); + __slice(a, "", 0); // Wrong end index - __slice(0, 0, ""); + __slice(a, 0, ""); + + let a: [u64; 5] = [1, 2, 3, 4, 5]; + let s: &__slice[u64] = __slice(LOCAL_ARRAY, 0, 5); + + // Wrong first argument + __elem_at(0, 0); + + // Wrong index type + __elem_at(a, ""); + + // Wrong index type + __elem_at(s, ""); } diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/test.toml index 3bbe64aee42..e9cb4879e6c 100644 --- a/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/test.toml +++ b/test/src/e2e_vm_tests/test_programs/should_fail/slice/slice_intrinsics/test.toml @@ -9,15 +9,28 @@ category = "fail" # check: __slice(0, 0, 0) # nextln: $()Unsupported argument type to intrinsic "slice" -# check: __slice(0, "", 0) +# check: __slice(a, "", 0) # nextln: $()Mismatched types # nextln: $()expected: u64 # nextln: $()found: str -# check: __slice(0, 0, "") +# check: __slice(a, 0, "") # nextln: $()Mismatched types # nextln: $()expected: u64 # nextln: $()found: str +# +# check: __elem_at(0, 0); +# nextln: $()Unsupported argument type to intrinsic "elem_at" + +# check: __elem_at(a, ""); +# nextln: $()Mismatched types. +# nextln: $()expected: u64 +# nextln: $()found: str. + +# check: __elem_at(s, ""); +# nextln: $()Mismatched types. +# nextln: $()expected: u64 +# nextln: $()found: str. # check: &__slice[u64] = __slice(GLOBAL_ARRAY, 0, 5) # nextln: $()slices or types containing slices on `const` are not allowed