diff --git a/sway-lib-std/src/identity.sw b/sway-lib-std/src/identity.sw index 61dff156aed..1fde1654b67 100644 --- a/sway-lib-std/src/identity.sw +++ b/sway-lib-std/src/identity.sw @@ -8,3 +8,13 @@ pub enum Identity { Address: Address, ContractId: ContractId, } + +impl core::ops::Eq for Identity { + fn eq(self, other: Self) -> bool { + match (self, other) { + (Identity::Address(address1), Identity::Address(address2)) => { address1 == address2 }, + (Identity::ContractId(asset1), Identity::ContractId(asset2)) => { asset1 == asset2 }, + _ => { false }, + } + } +} diff --git a/test/src/e2e_vm_tests/mod.rs b/test/src/e2e_vm_tests/mod.rs index b03499a779c..b528ad411cb 100644 --- a/test/src/e2e_vm_tests/mod.rs +++ b/test/src/e2e_vm_tests/mod.rs @@ -161,6 +161,7 @@ pub fn run(locked: bool, filter_regex: Option) { ProgramState::Return(1), ), // true ("should_pass/stdlib/ge_test", ProgramState::Return(1)), // true + ("should_pass/stdlib/identity_eq", ProgramState::Return(1)), // true ("should_pass/stdlib/intrinsics", ProgramState::Return(1)), // true ("should_pass/stdlib/option", ProgramState::Return(1)), // true ("should_pass/stdlib/require", ProgramState::Return(1)), // true diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/.gitignore b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/.gitignore new file mode 100644 index 00000000000..77d3844f58c --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/Forc.lock new file mode 100644 index 00000000000..0ff6c098ef4 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/Forc.lock @@ -0,0 +1,14 @@ +[[package]] +name = 'core' +source = 'path+from-root-E7BBED1900347958' +dependencies = [] + +[[package]] +name = 'identity_eq' +source = 'root' +dependencies = ['std'] + +[[package]] +name = 'std' +source = 'path+from-root-E7BBED1900347958' +dependencies = ['core'] diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/Forc.toml new file mode 100644 index 00000000000..31487ea07e2 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "identity_eq" + +[dependencies] +std = { path = "../../../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/json_abi_oracle.json b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/json_abi_oracle.json new file mode 100644 index 00000000000..f0586b752c7 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/json_abi_oracle.json @@ -0,0 +1,14 @@ +[ + { + "inputs": [], + "name": "main", + "outputs": [ + { + "components": null, + "name": "", + "type": "bool" + } + ], + "type": "function" + } +] \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/src/main.sw new file mode 100644 index 00000000000..3fa7807463a --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/identity_eq/src/main.sw @@ -0,0 +1,43 @@ +script; + +use std::{address::Address, assert::assert, contract_id::ContractId, identity::*}; + +fn main() -> bool { + let b1 = 0x0000000000000000000000000000000000000000000000000000000000000001; + let b2 = 0x0000000000000000000000000000000000000000000000000000000000000002; + + let address1 = Identity::Address(~Address::from(b1)); + let address2 = Identity::Address(~Address::from(b2)); + let contract1 = Identity::ContractId(~ContractId::from(b1)); + let contract2 = Identity::ContractId(~ContractId::from(b2)); + + // Eq is True + assert(address1 == address1); + assert(contract1 == contract1); + + // Eq is False + assert(!(address1 == address2)); + assert(!(address2 == address1)); + + assert(!(contract1 == contract2)); + assert(!(contract2 == contract1)); + + assert(!(address1 == contract1)); + assert(!(contract1 == address1)); + + // Neq is True + assert(address1 != address2); + assert(address2 != address1); + + assert(contract1 != contract2); + assert(contract2 != contract1); + + assert(address1 != contract1); + assert(contract1 != address1); + + // Neq is False + assert(!(address1 != address1)); + assert(!(contract1 != contract1)); + + true +}