diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c2c93f5ebab..4c1995aff32 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -462,7 +462,7 @@ jobs: - name: Install Forc run: cargo install --locked --debug --path ./forc - name: Run Unit Tests - run: forc build --path sway-lib-core && forc test --path sway-lib-core && forc build --path sway-lib-std && forc test --path sway-lib-std + run: forc build --path sway-lib-core && forc test --path sway-lib-core && forc build --path sway-lib-std && forc test --path sway-lib-std && forc build --path test/src/in_language_tests & forc test --path test/src/in_language_tests forc-pkg-fuels-deps-check: runs-on: ubuntu-latest diff --git a/sway-lib-std/src/address.sw b/sway-lib-std/src/address.sw index 9f2ff990e1e..d2c82e5026c 100644 --- a/sway-lib-std/src/address.sw +++ b/sway-lib-std/src/address.sw @@ -125,35 +125,3 @@ impl Hash for Address { bits.hash(state); } } - -#[test] -fn test_address_from_b256() { - use ::assert::assert; - - let my_address = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert( - my_address - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn test_address_into_b256() { - use ::assert::assert; - use ::convert::Into; - - let address = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_data: b256 = address.into(); - assert(b256_data == 0x0000000000000000000000000000000000000000000000000000000000000001); -} - -#[test] -fn test_address_zero() { - use ::assert::assert; - - let address = Address::zero(); - assert(address.is_zero()); - - let other_address = Address::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert(!other_address.is_zero()); -} diff --git a/sway-lib-std/src/array_conversions/b256.sw b/sway-lib-std/src/array_conversions/b256.sw index 0aef04b27aa..15d53dbc5dd 100644 --- a/sway-lib-std/src/array_conversions/b256.sw +++ b/sway-lib-std/src/array_conversions/b256.sw @@ -173,55 +173,3 @@ impl b256 { } } } - -#[test] -fn test_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 test_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 test_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 test_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/sway-lib-std/src/array_conversions/u16.sw b/sway-lib-std/src/array_conversions/u16.sw index 06372c55e12..ad6a11f8774 100644 --- a/sway-lib-std/src/array_conversions/u16.sw +++ b/sway-lib-std/src/array_conversions/u16.sw @@ -122,37 +122,3 @@ impl u16 { } } } - -#[test] -fn test_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 test_u16_from_le_bytes() { - let bytes = [1_u8, 2_u8]; - let result = u16::from_le_bytes(bytes); - - assert(result == 513_u16); -} - -#[test] -fn test_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 test_u16_from_be_bytes() { - let bytes = [2_u8, 1_u8]; - let result = u16::from_be_bytes(bytes); - - assert(result == 513_u16); -} diff --git a/sway-lib-std/src/array_conversions/u256.sw b/sway-lib-std/src/array_conversions/u256.sw index 9691f44892d..daa8fa94de9 100644 --- a/sway-lib-std/src/array_conversions/u256.sw +++ b/sway-lib-std/src/array_conversions/u256.sw @@ -172,55 +172,3 @@ impl u256 { } } } - -#[test] -fn test_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 test_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 test_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 test_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/sway-lib-std/src/array_conversions/u32.sw b/sway-lib-std/src/array_conversions/u32.sw index 5b202500c12..2831fea8f16 100644 --- a/sway-lib-std/src/array_conversions/u32.sw +++ b/sway-lib-std/src/array_conversions/u32.sw @@ -189,41 +189,3 @@ impl u32 { } } } - -#[test] -fn test_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 test_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 test_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 test_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/sway-lib-std/src/array_conversions/u64.sw b/sway-lib-std/src/array_conversions/u64.sw index d08b87f32ff..5e6aa1e74f5 100644 --- a/sway-lib-std/src/array_conversions/u64.sw +++ b/sway-lib-std/src/array_conversions/u64.sw @@ -257,49 +257,3 @@ impl u64 { } } } - -#[test] -fn test_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 test_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 test_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 test_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/sway-lib-std/src/asset_id.sw b/sway-lib-std/src/asset_id.sw index aa86a84258d..53fadeeaa9d 100644 --- a/sway-lib-std/src/asset_id.sw +++ b/sway-lib-std/src/asset_id.sw @@ -97,6 +97,11 @@ impl AssetId { /// Creates a new AssetId with the default SubId for the current contract. /// + /// # Additional Information + /// + /// **WARNING** If called in an external context, this will **not** return a correct AssetId. + /// If called externally, will actually use the Transaction Id as a the ContractId. + /// /// # Returns /// /// * [AssetId] - The AssetId of the asset. Computed by hashing the ContractId and the default SubId. @@ -240,67 +245,3 @@ impl From for b256 { id.bits() } } - -#[test()] -fn test_hasher_sha256_asset_id() { - use ::assert::assert; - let mut hasher = Hasher::new(); - AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000) - .hash(hasher); - let s256 = hasher.sha256(); - assert(s256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let mut hasher = Hasher::new(); - AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001) - .hash(hasher); - let s256 = hasher.sha256(); - assert(s256 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); -} - -#[test()] -fn test_hasher_sha256_contract_id() { - use ::assert::assert; - let mut hasher = Hasher::new(); - ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000) - .hash(hasher); - let s256 = hasher.sha256(); - assert(s256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let mut hasher = Hasher::new(); - ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001) - .hash(hasher); - let s256 = hasher.sha256(); - assert(s256 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); -} - -#[test] -fn test_asset_id_from_b256() { - use ::assert::assert; - - let my_asset = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert( - my_asset - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn test_asset_id_into_b256() { - use ::assert::assert; - use ::convert::Into; - - let asset = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_data: b256 = asset.into(); - assert(b256_data == 0x0000000000000000000000000000000000000000000000000000000000000001); -} - -#[test] -fn test_asset_id_zero() { - use ::assert::assert; - - let asset = AssetId::zero(); - assert(asset.is_zero()); - - let other_assert = AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert(!other_assert.is_zero()); -} diff --git a/sway-lib-std/src/b512.sw b/sway-lib-std/src/b512.sw index 6e0adea5732..96e4c5d68cd 100644 --- a/sway-lib-std/src/b512.sw +++ b/sway-lib-std/src/b512.sw @@ -16,8 +16,27 @@ impl core::ops::Eq for B512 { } } -/// Functions for casting between `B512` and an array of two `b256`s. impl From<(b256, b256)> for B512 { + /// Converts from a `b256` tuple to a `B512`. + /// + /// # Arguments + /// + /// * `components`: [(b256, b256)] - The `(b256, b256)` tuple to convert to a `B512`. + /// + /// # Returns + /// + /// * [B512] - The newly created `B512`. + /// + /// # Examples + /// + /// ```sway + /// use std::b512::B512; + /// + /// fn foo() { + /// let tuple: (b256, b256) = (b256::zero(), b256::zero()); + /// let b512 = B512::from(tuple); + /// } + /// ``` fn from(components: (b256, b256)) -> Self { Self { bits: [components.0, components.1], @@ -26,6 +45,30 @@ impl From<(b256, b256)> for B512 { } impl From for (b256, b256) { + /// Converts from a `B512` to a `b256` tuple. + /// + /// # Additional Information + /// + /// **NOTE:** To import, use the glob operator i.e. `use std::b512::*;` + /// + /// # Arguments + /// + /// * `val`: [B512] - The `B512` to convert to a tuple. + /// + /// # Returns + /// + /// * [(b256, b256)] - The newly created tuple. + /// + /// # Examples + /// + /// ```sway + /// use std::b512::*; + /// + /// fn foo() { + /// let b512 = B512::zero(); + /// let tuple: (b256, b256) = (b256, b256)::from(b512); + /// } + /// ``` fn from(val: B512) -> (b256, b256) { ((val.bits)[0], (val.bits)[1]) } @@ -116,27 +159,3 @@ impl B512 { (self.bits)[0] == b256::zero() && (self.bits)[1] == b256::zero() } } - -#[test] -fn test_b512_zero() { - use ::assert::assert; - - let zero_b512 = B512::zero(); - assert(zero_b512.is_zero()); - - let other1_b512 = B512::from(( - b256::zero(), - 0x0000000000000000000000000000000000000000000000000000000000000001, - )); - assert(!other1_b512.is_zero()); - let other2_b512 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - b256::zero(), - )); - assert(!other2_b512.is_zero()); - let other3_b512 = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - 0x0000000000000000000000000000000000000000000000000000000000000001, - )); - assert(!other3_b512.is_zero()); -} diff --git a/sway-lib-std/src/block.sw b/sway-lib-std/src/block.sw index 98a8ee035cc..dc9e597962e 100644 --- a/sway-lib-std/src/block.sw +++ b/sway-lib-std/src/block.sw @@ -6,7 +6,7 @@ use ::result::Result::{self, *}; use ::logging::log; /// Error type for when the block hash cannot be found. -enum BlockHashError { +pub enum BlockHashError { /// Error returned when the block hash cannot be found. BlockHeightTooHigh: (), } @@ -123,32 +123,3 @@ pub fn block_header_hash(block_height: u32) -> Result { Ok(header_hash) } } - -//////////////////////////////////////////////////////////////////// -// Tests -//////////////////////////////////////////////////////////////////// - -#[test(should_revert)] -fn test_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 test_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/sway-lib-std/src/bytes.sw b/sway-lib-std/src/bytes.sw index 4ecfff2c7b0..b3efba7f4b5 100644 --- a/sway-lib-std/src/bytes.sw +++ b/sway-lib-std/src/bytes.sw @@ -522,11 +522,6 @@ impl Bytes { /// Clears the `Bytes`, removing all values. /// - /// # Additional Information - /// - /// Note that this method has no effect on the allocated capacity - /// of the Bytes. - /// /// # Examples /// /// ```sway @@ -946,454 +941,3 @@ impl AbiDecode for Bytes { } } } - -// Tests -// -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 test_new_bytes() { - let bytes = Bytes::new(); - assert(bytes.len() == 0); -} - -#[test()] -fn test_push() { - let (_, a, b, c) = setup(); - let mut bytes = Bytes::new(); - bytes.push(a); - assert(bytes.len() == 1); - bytes.push(b); - assert(bytes.len() == 2); - bytes.push(c); - assert(bytes.len() == 3); -} - -#[test()] -fn test_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); - - let first = bytes.pop(); - assert(first.unwrap() == 19u8); - assert(bytes.len() == 11); - - let second = bytes.pop(); - assert(second.unwrap() == 17u8); - assert(bytes.len() == 10); - - 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); - assert(bytes.pop().is_none() == true); - assert(bytes.len() == 0); -} - -#[test()] -fn test_len() { - let (mut bytes, _, _, _) = setup(); - assert(bytes.len() == 3); -} - -#[test()] -fn test_clear() { - let (mut bytes, _, _, _) = setup(); - assert(bytes.len() == 3); - - bytes.clear(); - - assert(bytes.len() == 0); -} - -#[test()] -fn 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); - assert(size_of_val(bytes.buf) == 16); -} - -#[test()] -fn test_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); - assert(bytes.len() == 4); - bytes.push(3u8); - assert(bytes.capacity() == 8); - assert(bytes.len() == 5); -} - -#[test()] -fn test_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); -} - -#[test()] -fn test_remove() { - let (mut bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - - let item = bytes.remove(1); - - assert(bytes.len() == 2); - assert(item == b); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == c); - assert(bytes.get(2).is_none()); -} - -#[test()] -fn test_insert() { - let (mut bytes, a, b, c) = setup(); - let d = 11u8; - assert(bytes.len() == 3); - - 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); -} - -#[test()] -fn test_swap() { - let (mut bytes, a, b, c) = setup(); - assert(bytes.len() == 3); - - bytes.swap(0, 1); - - assert(bytes.len() == 3); - assert(bytes.get(0).unwrap() == b); - assert(bytes.get(1).unwrap() == a); - assert(bytes.get(2).unwrap() == c); -} - -#[test()] -fn test_set() { - let (mut bytes, a, _b, c) = setup(); - assert(bytes.len() == 3); - let d = 11u8; - - bytes.set(1, d); - - assert(bytes.len() == 3); - assert(bytes.get(0).unwrap() == a); - assert(bytes.get(1).unwrap() == d); - assert(bytes.get(2).unwrap() == c); -} - -#[test()] -fn test_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 test_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 test_bytes_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); -} - -#[test()] -fn test_split_at() { - let (mut original, _a, _b, _c) = setup(); - assert(original.len() == 3); - let index = 1; - let (left, right) = original.split_at(index); - assert(original.capacity() == 4); - assert(right.capacity() == 2); - assert(left.len() == 1); - assert(right.len() == 2); -} - -#[test()] -fn test_split_at_0() { - let (mut original, _a, _b, _c) = setup(); - assert(original.len() == 3); - let index = 0; - let (left, right) = original.split_at(index); - assert(original.capacity() == 4); - assert(right.capacity() == 3); - assert(left.len() == 0); - assert(right.len() == 3); -} - -#[test()] -fn test_split_at_len() { - let (mut original, _a, _b, _c) = setup(); - assert(original.len() == 3); - let index = 3; - let (left, right) = original.split_at(index); - assert(original.capacity() == 4); - assert(right.capacity() == 0); - assert(left.len() == 3); - assert(right.len() == 0); -} - -#[test()] -fn test_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(); - let _first_cap = bytes.capacity(); - let _second_cap = bytes2.capacity(); - bytes.append(bytes2); - assert(bytes.len() == first_length + second_length); - assert(bytes.capacity() == first_length + first_length); - 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 test_append_empty_bytes() { - // nothing is appended or modified when appending an empty bytes. - 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(); - assert(bytes2.len() == 0); - let first_length = bytes.len(); - let first_cap = bytes.capacity(); - bytes.append(bytes2); - assert(bytes.len() == first_length); - assert(bytes.capacity() == first_cap); -} - -#[test()] -fn test_append_to_empty_bytes() { - let mut bytes = Bytes::new(); - assert(bytes.len() == 0); - let (mut bytes2, a, b, c) = setup(); - assert(bytes2.len() == 3); - - let _first_length = bytes.len(); - let _first_cap = bytes.capacity(); - let second_length = bytes2.len(); - let second_cap = bytes2.capacity(); - bytes.append(bytes2); - assert(bytes.len() == second_length); - assert(bytes.capacity() == second_cap); - let values = [a, b, c]; - let mut i = 0; - while i < 3 { - assert(bytes.get(i).unwrap() == values[i]); - i += 1; - }; - - assert(bytes2.len() == 0); - assert(bytes2.capacity() == 0); -} - -#[test()] -fn test_eq() { - let (mut bytes, _a, _b, _c) = setup(); - let (mut bytes2, _a, _b, _c) = setup(); - assert(bytes == bytes2); - - 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); - assert(bytes != other); - - bytes.push(42u8); - assert(bytes == other); - - other.swap(0, 1); - assert(bytes != other); -} - -#[test()] -fn test_as_raw_slice() { - let val = 0x3497297632836282349729763283628234972976328362823497297632836282; - let slice_1 = asm(ptr: (__addr_of(val), 32)) { - ptr: raw_slice - }; - let mut bytes = Bytes::from(slice_1); - let slice_2 = bytes.as_raw_slice(); - assert(slice_1.ptr() == slice_2.ptr()); - assert(slice_1.number_of_bytes() == slice_2.number_of_bytes()); -} - -// This test will need to be updated once https://github.com/FuelLabs/sway/pull/3882 is resolved -#[test()] -fn test_from_raw_slice() { - let val = 0x3497297632836282349729763283628234972976328362823497297632836282; - let slice_1 = asm(ptr: (__addr_of(val), 32)) { - ptr: raw_slice - }; - let mut bytes = Bytes::from(slice_1); - let slice_2 = bytes.as_raw_slice(); - assert(slice_1.ptr() == slice_2.ptr()); - assert(slice_1.number_of_bytes() == slice_2.number_of_bytes()); -} - -#[test] -fn test_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 test_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] -pub fn test_encode_decode() { - let initial = 0x3333333333333333333333333333333333333333333333333333333333333333; - let initial: Bytes = Bytes::from(initial); - let decoded = abi_decode::(encode(initial)); - - assert_eq(decoded, initial); -} diff --git a/sway-lib-std/src/bytes_conversions/b256.sw b/sway-lib-std/src/bytes_conversions/b256.sw index 055eb82a006..09ac108e9bd 100644 --- a/sway-lib-std/src/bytes_conversions/b256.sw +++ b/sway-lib-std/src/bytes_conversions/b256.sw @@ -146,58 +146,3 @@ impl b256 { bytes.into() } } - -#[test] -fn test_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 test_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 test_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 test_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/sway-lib-std/src/bytes_conversions/u16.sw b/sway-lib-std/src/bytes_conversions/u16.sw index 63bdb281dc8..0c699540de8 100644 --- a/sway-lib-std/src/bytes_conversions/u16.sw +++ b/sway-lib-std/src/bytes_conversions/u16.sw @@ -158,41 +158,3 @@ impl u16 { } } } - -#[test] -fn test_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 test_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 test_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 test_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/sway-lib-std/src/bytes_conversions/u256.sw b/sway-lib-std/src/bytes_conversions/u256.sw index fa117446280..c1eff97145b 100644 --- a/sway-lib-std/src/bytes_conversions/u256.sw +++ b/sway-lib-std/src/bytes_conversions/u256.sw @@ -152,58 +152,3 @@ impl u256 { } } } - -#[test] -fn test_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 test_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 test_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 test_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/sway-lib-std/src/bytes_conversions/u32.sw b/sway-lib-std/src/bytes_conversions/u32.sw index 25267bce425..03b3c9a8476 100644 --- a/sway-lib-std/src/bytes_conversions/u32.sw +++ b/sway-lib-std/src/bytes_conversions/u32.sw @@ -216,49 +216,3 @@ impl u32 { } } } - -#[test] -fn test_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 test_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 test_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 test_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/sway-lib-std/src/bytes_conversions/u64.sw b/sway-lib-std/src/bytes_conversions/u64.sw index f8957eb42b0..d2d80687e74 100644 --- a/sway-lib-std/src/bytes_conversions/u64.sw +++ b/sway-lib-std/src/bytes_conversions/u64.sw @@ -304,65 +304,3 @@ impl u64 { } } } - -#[test] -fn test_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 test_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 test_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 test_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/sway-lib-std/src/contract_id.sw b/sway-lib-std/src/contract_id.sw index 2e1416fb6b2..5ec994dffab 100644 --- a/sway-lib-std/src/contract_id.sw +++ b/sway-lib-std/src/contract_id.sw @@ -36,7 +36,6 @@ impl core::ops::Eq for ContractId { } } -/// Functions for casting between the `b256` and `ContractId` types. impl From for ContractId { /// Casts raw `b256` data to a `ContractId`. /// @@ -93,8 +92,6 @@ impl ContractId { /// /// # Additional Information /// - /// This is equivalent to std::callframes::contract_id(). - /// /// **_Note:_** If called in an external context, this will **not** return a ContractId. /// If called externally, will actually return a pointer to the Transaction Id (Wrapped in the ContractId struct). /// @@ -157,35 +154,3 @@ impl ContractId { self.bits == b256::zero() } } - -#[test] -fn test_contract_id_from_b256() { - use ::assert::assert; - - let my_contract_id = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert( - my_contract_id - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn test_contract_id_into_b256() { - use ::assert::assert; - use ::convert::Into; - - let contract_id = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_data: b256 = contract_id.into(); - assert(b256_data == 0x0000000000000000000000000000000000000000000000000000000000000001); -} - -#[test] -fn test_contract_id_zero() { - use ::assert::assert; - - let contract_id = ContractId::zero(); - assert(contract_id.is_zero()); - - let other_contract_id = ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert(!other_contract_id.is_zero()); -} diff --git a/sway-lib-std/src/ecr.sw b/sway-lib-std/src/ecr.sw index 3b4cf38eaaf..1bff86d3be9 100644 --- a/sway-lib-std/src/ecr.sw +++ b/sway-lib-std/src/ecr.sw @@ -264,86 +264,3 @@ pub fn ec_recover_address_r1(signature: B512, msg_hash: b256) -> Result for b256 { } } } - -#[test] -fn test_b256_try_from_bytes() { - use ::assert::assert; - - 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 res = b256::try_from(initial_bytes); - let expected = 0x3333333333333333333333333333333333333333333333333333333333333333; - - assert(res.unwrap() == expected); - - 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 res = b256::try_from(second_bytes); - assert(res.is_none()); - - // bytes is still available to use: - assert(second_bytes.len() == 33); - assert(second_bytes.capacity() == 33); -} - -#[test] -fn test_b256_from_u256() { - use ::assert::assert; - - let val = 0x0000000000000000000000000000000000000000000000000000000000000000_u256; - let res = b256::from(val); - assert(res == 0x0000000000000000000000000000000000000000000000000000000000000000); -} - -#[test] -fn test_b256_from_u128() { - use ::assert::assert; - - let b256_value = >::from(U128::from((18446744073709551615_u64, 18446744073709551615_u64))); - assert( - b256_value == 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff, - ); -} - -#[test] -fn test_b256_from_tuple() { - use ::assert::assert; - - let b256_value = >::from((1, 2, 3, 4)); - assert( - b256_value == 0x0000000000000001000000000000000200000000000000030000000000000004, - ); -} - -#[test] -fn test_b256_try_from_b512() { - use ::assert::assert; - - 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()); -} diff --git a/sway-lib-std/src/primitive_conversions/str.sw b/sway-lib-std/src/primitive_conversions/str.sw index bf0728a2eee..27f75b41d92 100644 --- a/sway-lib-std/src/primitive_conversions/str.sw +++ b/sway-lib-std/src/primitive_conversions/str.sw @@ -30,17 +30,3 @@ impl str { } } } - -#[test] -fn str_slice_to_str_array() { - use ::assert::*; - use core::str::*; - - 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/sway-lib-std/src/primitive_conversions/u16.sw b/sway-lib-std/src/primitive_conversions/u16.sw index 82424b83b34..bced8a770a5 100644 --- a/sway-lib-std/src/primitive_conversions/u16.sw +++ b/sway-lib-std/src/primitive_conversions/u16.sw @@ -91,81 +91,3 @@ impl TryFrom for u16 { } } } - -#[test] -fn test_u16_from_u8() { - use ::assert::assert; - - let u8_1: u8 = 0u8; - let u8_2: u8 = 255u8; - - let u16_1 = u16::from(u8_1); - let u16_2 = u16::from(u8_2); - - assert(u16_1 == 0u16); - assert(u16_2 == 255u16); -} - -#[test] -fn test_u16_try_from_u32() { - use ::assert::assert; - - let u32_1: u32 = 2u32; - let u32_2: u32 = u16::max().as_u32() + 1; - - let u16_1 = >::try_from(u32_1); - let u16_2 = >::try_from(u32_2); - - assert(u16_1.is_some()); - assert(u16_1.unwrap() == 2u16); - - assert(u16_2.is_none()); -} - -#[test] -fn test_u16_try_from_u64() { - use ::assert::assert; - - let u64_1: u64 = 2; - let u64_2: u64 = u16::max().as_u64() + 1; - - let u16_1 = >::try_from(u64_1); - let u16_2 = >::try_from(u64_2); - - assert(u16_1.is_some()); - assert(u16_1.unwrap() == 2u16); - - assert(u16_2.is_none()); -} - -#[test] -fn test_u16_try_from_u256() { - use ::assert::assert; - - let u256_1: u256 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; - let u256_2: u256 = 0x1000000000000000000000000000000000000000000000000000000000000000u256; - - let u16_1 = >::try_from(u256_1); - let u16_2 = >::try_from(u256_2); - - assert(u16_1.is_some()); - assert(u16_1.unwrap() == 2u16); - - assert(u16_2.is_none()); -} - -#[test] -fn test_u16_try_from_u128() { - use ::assert::assert; - - let u128_1: U128 = U128::new(); - let u128_2: U128 = U128::from((0, u16::max().as_u64() + 1)); - - let u16_1 = >::try_from(u128_1); - let u16_2 = >::try_from(u128_2); - - assert(u16_1.is_some()); - assert(u16_1.unwrap() == 0u16); - - assert(u16_2.is_none()); -} diff --git a/sway-lib-std/src/primitive_conversions/u256.sw b/sway-lib-std/src/primitive_conversions/u256.sw index 4204e523f63..b4d14755092 100644 --- a/sway-lib-std/src/primitive_conversions/u256.sw +++ b/sway-lib-std/src/primitive_conversions/u256.sw @@ -214,95 +214,3 @@ impl From<(u64, u64, u64, u64)> for u256 { } } } - -// TODO: Replace > with u256::from when https://github.com/FuelLabs/sway/issues/5798 is resolved. -#[test] -fn test_u256_from_u8() { - use ::assert::assert; - - let u256_value = >::from(255_u8); - assert( - u256_value == 0x00000000000000000000000000000000000000000000000000000000000000ff_u256, - ); -} - -#[test] -fn test_u256_from_u16() { - use ::assert::assert; - - let u256_value = >::from(65535_u16); - assert( - u256_value == 0x000000000000000000000000000000000000000000000000000000000000ffff_u256, - ); -} - -#[test] -fn test_u256_from_u32() { - use ::assert::assert; - - let u256_value = >::from(4294967295_u32); - assert( - u256_value == 0x00000000000000000000000000000000000000000000000000000000ffffffff_u256, - ); -} - -#[test] -fn test_u256_from_u64() { - use ::assert::assert; - - let u256_value = >::from(18446744073709551615_u64); - assert( - u256_value == 0x000000000000000000000000000000000000000000000000ffffffffffffffff_u256, - ); -} - -#[test] -fn test_u256_from_b256() { - use ::assert::assert; - - let u256_value = >::from(0x0000000000000000000000000000000000000000000000000000000000000000); - assert( - u256_value == 0x0000000000000000000000000000000000000000000000000000000000000000_u256, - ); - - let u256_value = >::from(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff); - assert( - u256_value == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_u256, - ); -} - -#[test] -fn test_u256_from_u128() { - use ::assert::assert; - - let u256_value = >::from(U128::from((18446744073709551615_u64, 18446744073709551615_u64))); - assert( - u256_value == 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff_u256, - ); -} - -#[test] -fn test_u256_from_tuple() { - use ::assert::assert; - - let u256_value = >::from((1, 2, 3, 4)); - assert( - u256_value == 0x0000000000000001000000000000000200000000000000030000000000000004_u256, - ); -} - -#[test] -fn test_u256_try_from_b512() { - use ::assert::assert; - - let b512_value = B512::new(); - let u256_value = u256::try_from(b512_value); - assert(u256_value.is_some()); - - let b512_value = B512::from(( - 0x0000000000000000000000000000000000000000000000000000000000000001, - b256::zero(), - )); - let u256_value = u256::try_from(b512_value); - assert(u256_value.is_none()); -} diff --git a/sway-lib-std/src/primitive_conversions/u32.sw b/sway-lib-std/src/primitive_conversions/u32.sw index d602353c5ce..ca5a0722680 100644 --- a/sway-lib-std/src/primitive_conversions/u32.sw +++ b/sway-lib-std/src/primitive_conversions/u32.sw @@ -111,80 +111,3 @@ impl TryFrom for u32 { } } } - -// TODO: Replace > with u32::from when https://github.com/FuelLabs/sway/issues/5798 is resolved. -#[test] -fn test_u32_from_u8() { - use ::assert::assert; - - let u8_1: u8 = 0u8; - let u8_2: u8 = 255u8; - - let u32_1 = >::from(u8_1); - let u32_2 = >::from(u8_2); - - assert(u32_1 == 0u32); - assert(u32_2 == 255u32); -} - -#[test] -fn test_u32_from_u16() { - use ::assert::assert; - - let u16_1: u16 = 0u16; - let u16_2: u16 = 65535u16; - - let u32_1 = >::from(u16_1); - let u32_2 = >::from(u16_2); - - assert(u32_1 == 0u32); - assert(u32_2 == 65535u32); -} - -#[test] -fn test_u32_try_from_u64() { - use ::assert::assert; - - let u64_1: u64 = 2; - let u64_2: u64 = u32::max().as_u64() + 1; - - let u32_1 = >::try_from(u64_1); - let u32_2 = >::try_from(u64_2); - - assert(u32_1.is_some()); - assert(u32_1.unwrap() == 2u32); - - assert(u32_2.is_none()); -} - -#[test] -fn test_u32_try_from_u256() { - use ::assert::assert; - - let u256_1: u256 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; - let u256_2: u256 = 0x1000000000000000000000000000000000000000000000000000000000000000u256; - - let u32_1 = >::try_from(u256_1); - let u32_2 = >::try_from(u256_2); - - assert(u32_1.is_some()); - assert(u32_1.unwrap() == 2u32); - - assert(u32_2.is_none()); -} - -#[test] -fn test_u32_try_from_u128() { - use ::assert::assert; - - let u128_1: U128 = U128::new(); - let u128_2: U128 = U128::from((0, u32::max().as_u64() + 1)); - - let u32_1 = >::try_from(u128_1); - let u32_2 = >::try_from(u128_2); - - assert(u32_1.is_some()); - assert(u32_1.unwrap() == 0u32); - - assert(u32_2.is_none()); -} diff --git a/sway-lib-std/src/primitive_conversions/u64.sw b/sway-lib-std/src/primitive_conversions/u64.sw index c87e36be6f0..b373bb85ae9 100644 --- a/sway-lib-std/src/primitive_conversions/u64.sw +++ b/sway-lib-std/src/primitive_conversions/u64.sw @@ -125,78 +125,3 @@ impl TryFrom for u64 { } } } - -// TODO: Replace > with u64::from when https://github.com/FuelLabs/sway/issues/5798 is resolved. -#[test] -fn test_u64_from_u8() { - use ::assert::assert; - - let u8_1: u8 = 0u8; - let u8_2: u8 = 255u8; - - let u64_1 = >::from(u8_1); - let u64_2 = >::from(u8_2); - - assert(u64_1 == 0u64); - assert(u64_2 == 255u64); -} - -#[test] -fn test_u64_from_u16() { - use ::assert::assert; - - let u16_1: u16 = 0u16; - let u16_2: u16 = 65535u16; - - let u64_1 = >::from(u16_1); - let u64_2 = >::from(u16_2); - - assert(u64_1 == 0u64); - assert(u64_2 == 65535u64); -} - -#[test] -fn test_u64_from_u32() { - use ::assert::assert; - - let u32_1: u32 = 0u32; - let u32_2: u32 = 4294967295u32; - - let u64_1 = >::from(u32_1); - let u64_2 = >::from(u32_2); - - assert(u64_1 == 0u64); - assert(u64_2 == 4294967295u64); -} - -#[test] -fn test_u64_try_from_u256() { - use ::assert::assert; - - let u256_1 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; - let u256_2 = 0x1000000000000000000000000000000000000000000000000000000000000000u256; - - let u64_1 = u64::try_from(u256_1); - let u64_2 = u64::try_from(u256_2); - - assert(u64_1.is_some()); - assert(u64_1.unwrap() == 2); - - assert(u64_2.is_none()); -} - -#[test] -fn test_u64_try_from_u128() { - use ::assert::assert; - - let u128_1: U128 = U128::new(); - let u128_2: U128 = U128::from((1, 0)); - - let u64_1 = >::try_from(u128_1); - let u64_2 = >::try_from(u128_2); - - assert(u64_1.is_some()); - assert(u64_1.unwrap() == 0u64); - - assert(u64_2.is_none()); -} diff --git a/sway-lib-std/src/primitive_conversions/u8.sw b/sway-lib-std/src/primitive_conversions/u8.sw index 75f6daf592b..70f10614ae7 100644 --- a/sway-lib-std/src/primitive_conversions/u8.sw +++ b/sway-lib-std/src/primitive_conversions/u8.sw @@ -69,83 +69,3 @@ impl TryFrom for u8 { } } } - -#[test] -fn test_u8_try_from_u16() { - use ::assert::assert; - - let u16_1: u16 = 2u16; - let u16_2: u16 = u8::max().as_u16() + 1; - - let u8_1 = >::try_from(u16_1); - let u8_2 = >::try_from(u16_2); - - assert(u8_1.is_some()); - assert(u8_1.unwrap() == 2u8); - - assert(u8_2.is_none()); -} - -#[test] -fn test_u8_try_from_u32() { - use ::assert::assert; - - let u32_1: u32 = 2u32; - let u32_2: u32 = u16::max().as_u32() + 1; - - let u8_1 = >::try_from(u32_1); - let u8_2 = >::try_from(u32_2); - - assert(u8_1.is_some()); - assert(u8_1.unwrap() == 2u8); - - assert(u8_2.is_none()); -} - -#[test] -fn test_u8_try_from_u64() { - use ::assert::assert; - - let u64_1: u64 = 2; - let u64_2: u64 = u16::max().as_u64() + 1; - - let u8_1 = >::try_from(u64_1); - let u8_2 = >::try_from(u64_2); - - assert(u8_1.is_some()); - assert(u8_1.unwrap() == 2u8); - - assert(u8_2.is_none()); -} - -#[test] -fn test_u8_try_from_u256() { - use ::assert::assert; - - let u256_1: u256 = 0x0000000000000000000000000000000000000000000000000000000000000002u256; - let u256_2: u256 = 0x1000000000000000000000000000000000000000000000000000000000000000u256; - - let u8_1 = >::try_from(u256_1); - let u8_2 = >::try_from(u256_2); - - assert(u8_1.is_some()); - assert(u8_1.unwrap() == 2u8); - - assert(u8_2.is_none()); -} - -#[test] -fn test_u8_try_from_u128() { - use ::assert::assert; - - let u128_1: U128 = U128::new(); - let u128_2: U128 = U128::from((0, u8::max().as_u64() + 1)); - - let u8_1 = >::try_from(u128_1); - let u8_2 = >::try_from(u128_2); - - assert(u8_1.is_some()); - assert(u8_1.unwrap() == 0u8); - - assert(u8_2.is_none()); -} diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index dfe575a629a..97b6cf76df3 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -254,6 +254,27 @@ impl AsRawSlice for String { } impl From for String { + /// Converts a `raw_slice` to a `String`. + /// + /// # Arguments + /// + /// * `slice`: [raw_slice] - The `raw_slice` to convert to a `String`. + /// + /// # Returns + /// + /// * [String] - The newly created `String`. + /// + /// # Examples + /// + /// ```sway + /// use std::{alloc::alloc, string::*}; + /// + /// fn foo() { + /// let ptr = alloc::(1); + /// let slice = raw_slice::from_parts::(ptr, 1); + /// let string: String = String::from(slice); + /// } + /// ``` fn from(slice: raw_slice) -> Self { Self { bytes: Bytes::from(slice), @@ -262,6 +283,30 @@ impl From for String { } impl From for raw_slice { + /// Converts a `String` to a `raw_slice`. + /// + /// # Additional Information + /// + /// **NOTE:** To import, use the glob operator i.e. `use std::string::*;` + /// + /// # Arguments + /// + /// * `s`: [String] - The `String` to convert to a `raw_slice`. + /// + /// # Returns + /// + /// * [raw_slice] - The newly created `raw_slice`. + /// + /// # Examples + /// + /// ```sway + /// use std::string::*; + /// + /// fn foo() { + /// let string = String::from_ascii_str("Fuel"); + /// let string_slice: raw_slice = string.into(); + /// } + /// ``` fn from(s: String) -> raw_slice { raw_slice::from(s.as_bytes()) } @@ -307,261 +352,3 @@ impl AbiDecode for String { } } } - -// Tests - -#[test] -fn string_test_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_test_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_test_clear() { - let mut string = String::new(); - - assert(string.is_empty()); - - string.clear(); - assert(string.is_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_test_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 = 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_test_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_test_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.capacity() == 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); -} - -#[test] -fn string_test_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_test_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_test_into_raw_slice() { - 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_test_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_test_new() { - let mut string = String::new(); - - assert(string.is_empty()); - assert(string.capacity() == 0); -} - -#[test] -fn string_test_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_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 ::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/sway-lib-std/src/u128.sw b/sway-lib-std/src/u128.sw index 375feb2d9b6..94a27c98027 100644 --- a/sway-lib-std/src/u128.sw +++ b/sway-lib-std/src/u128.sw @@ -130,6 +130,7 @@ impl From<(u64, u64)> for U128 { } } +// NOTE: To import, use the glob operator i.e. `use std::u128::*;` impl From for (u64, u64) { fn from(val: U128) -> (u64, u64) { (val.upper, val.lower) @@ -730,66 +731,3 @@ impl Logarithm for U128 { self_log2 / base_log2 } } - -#[test] -fn test_u128_from_u8() { - let u8_1: u8 = 0u8; - let u8_2: u8 = 255u8; - - let u128_1 = >::from(u8_1); - let u128_2 = >::from(u8_2); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 255u64); -} - -#[test] -fn test_u128_from_u16() { - let u16_1: u16 = 0u16; - let u16_2: u16 = 65535u16; - - let u128_1 = >::from(u16_1); - let u128_2 = >::from(u16_2); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 65535u64); -} - -#[test] -fn test_u128_from_u32() { - let u32_1: u32 = 0u32; - let u32_2: u32 = 4294967295u32; - - let u128_1 = >::from(u32_1); - let u128_2 = >::from(u32_2); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 4294967295u64); -} - -#[test] -fn test_u128_from_u64() { - let u64_1: u64 = 0u64; - let u64_2: u64 = 18446744073709551615u64; - - let u128_1 = >::from(u64_1); - let u128_2 = >::from(u64_2); - - assert(u128_1.as_u64().unwrap() == 0u64); - assert(u128_2.as_u64().unwrap() == 18446744073709551615u64); -} - -#[test] -fn test_u128_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()); -} diff --git a/sway-lib-std/src/vec.sw b/sway-lib-std/src/vec.sw index 07df2059567..491e8a728e5 100644 --- a/sway-lib-std/src/vec.sw +++ b/sway-lib-std/src/vec.sw @@ -702,29 +702,3 @@ impl Iterator for VecIter { self.values.get(self.index - 1) } } - -#[test()] -fn test_vec_with_len_1() { - let mut ve: Vec = Vec::new(); - assert(ve.len == 0); - ve.push(1); - assert(ve.len == 1); - let _ = ve.remove(0); - assert(ve.len == 0); -} - -#[test()] -fn encode_and_decode_vec() { - 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/sway-lib-std/src/vm/evm/ecr.sw b/sway-lib-std/src/vm/evm/ecr.sw index 09bbd65a45c..f66c148063e 100644 --- a/sway-lib-std/src/vm/evm/ecr.sw +++ b/sway-lib-std/src/vm/evm/ecr.sw @@ -2,7 +2,6 @@ library; use ::b512::B512; -use ::registers::error; use ::ecr::{ec_recover, EcRecoverError}; use ::hash::*; use ::result::Result::{self, *}; diff --git a/sway-lib-std/src/vm/evm/evm_address.sw b/sway-lib-std/src/vm/evm/evm_address.sw index 4b548853d51..554a44204a9 100644 --- a/sway-lib-std/src/vm/evm/evm_address.sw +++ b/sway-lib-std/src/vm/evm/evm_address.sw @@ -145,35 +145,3 @@ impl Hash for EvmAddress { bits.hash(state); } } - -#[test] -fn test_evm_address_from_b256() { - use ::assert::assert; - - let evm_address = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert( - evm_address - .bits() == 0x0000000000000000000000000000000000000000000000000000000000000001, - ); -} - -#[test] -fn test_evm_address_into_b256() { - use ::assert::assert; - use ::convert::Into; - - let evm_address = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - let b256_data: b256 = evm_address.into(); - assert(b256_data == 0x0000000000000000000000000000000000000000000000000000000000000001); -} - -#[test] -fn test_evm_address_zero() { - use ::assert::assert; - - let evm_address = EvmAddress::zero(); - assert(evm_address.is_zero()); - - let other_evm_address = EvmAddress::from(0x0000000000000000000000000000000000000000000000000000000000000001); - assert(!other_evm_address.is_zero()); -} diff --git a/test/src/in_language_tests/.gitignore b/test/src/in_language_tests/.gitignore new file mode 100644 index 00000000000..85059217cde --- /dev/null +++ b/test/src/in_language_tests/.gitignore @@ -0,0 +1 @@ +Forc.lock diff --git a/test/src/in_language_tests/Forc.toml b/test/src/in_language_tests/Forc.toml new file mode 100644 index 00000000000..e015fae4ff0 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/address_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/address_inline_tests/Forc.toml new file mode 100644 index 00000000000..b4b0a53ec38 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/address_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/address_inline_tests/src/main.sw new file mode 100644 index 00000000000..f86f63e5a16 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/alloc_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/alloc_inline_tests/Forc.toml new file mode 100644 index 00000000000..aa8391756f5 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/alloc_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/alloc_inline_tests/src/main.sw new file mode 100644 index 00000000000..54c9536290f --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/array_conversions_b256_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/array_conversions_b256_inline_tests/Forc.toml new file mode 100644 index 00000000000..5ef3222bb22 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/array_conversions_b256_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/array_conversions_u16_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/array_conversions_u16_inline_tests/Forc.toml new file mode 100644 index 00000000000..97f65521625 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/array_conversions_u16_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/array_conversions_u256_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/array_conversions_u256_inline_tests/Forc.toml new file mode 100644 index 00000000000..2632e8c5fdc --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/array_conversions_u256_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/array_conversions_u32_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/array_conversions_u32_inline_tests/Forc.toml new file mode 100644 index 00000000000..c695b8533fe --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/array_conversions_u32_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/array_conversions_u64_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/array_conversions_u64_inline_tests/Forc.toml new file mode 100644 index 00000000000..556899c47c4 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/array_conversions_u64_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/assert_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/assert_inline_tests/Forc.toml new file mode 100644 index 00000000000..71bac6ffeaf --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/assert_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/assert_inline_tests/src/main.sw new file mode 100644 index 00000000000..916bd7fea4a --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/asset_id_contract_tests/Forc.toml b/test/src/in_language_tests/test_programs/asset_id_contract_tests/Forc.toml new file mode 100644 index 00000000000..646e9e12e7f --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/asset_id_contract_tests/src/main.sw b/test/src/in_language_tests/test_programs/asset_id_contract_tests/src/main.sw new file mode 100644 index 00000000000..16e895c99c9 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/asset_id_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/asset_id_inline_tests/Forc.toml new file mode 100644 index 00000000000..6f1fff646d1 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/asset_id_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/asset_id_inline_tests/src/main.sw new file mode 100644 index 00000000000..057bf1fe5a5 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/b512_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/b512_inline_tests/Forc.toml new file mode 100644 index 00000000000..3eb4a563a83 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/b512_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/b512_inline_tests/src/main.sw new file mode 100644 index 00000000000..8c5ef0c4381 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/block_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/block_inline_tests/Forc.toml new file mode 100644 index 00000000000..4d1f61ac902 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/block_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/block_inline_tests/src/main.sw new file mode 100644 index 00000000000..c89fb10d65a --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/Forc.toml new file mode 100644 index 00000000000..58a5ac2c835 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/bytes_conversions_b256_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/Forc.toml new file mode 100644 index 00000000000..7e52a229ec8 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/bytes_conversions_u16_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/Forc.toml new file mode 100644 index 00000000000..ea3ff0fb77f --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/bytes_conversions_u256_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/Forc.toml new file mode 100644 index 00000000000..48f9066f8ec --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/bytes_conversions_u32_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/Forc.toml new file mode 100644 index 00000000000..3f51fa9d573 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/bytes_conversions_u64_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/bytes_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/bytes_inline_tests/Forc.toml new file mode 100644 index 00000000000..ada0818b7ae --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/bytes_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/bytes_inline_tests/src/main.sw new file mode 100644 index 00000000000..3d69727a4a2 --- /dev/null +++ b/test/src/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()); + 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()); + 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/in_language_tests/test_programs/contract_id_contract_tests/Forc.toml b/test/src/in_language_tests/test_programs/contract_id_contract_tests/Forc.toml new file mode 100644 index 00000000000..de4d6a7ee46 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/contract_id_contract_tests/src/main.sw b/test/src/in_language_tests/test_programs/contract_id_contract_tests/src/main.sw new file mode 100644 index 00000000000..4ad5554b355 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/contract_id_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/contract_id_inline_tests/Forc.toml new file mode 100644 index 00000000000..a17cad1235c --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/contract_id_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/contract_id_inline_tests/src/main.sw new file mode 100644 index 00000000000..cfcc216a8d4 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/ecr_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/ecr_inline_tests/Forc.toml new file mode 100644 index 00000000000..0a8e7ef6ca2 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/ecr_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/ecr_inline_tests/src/main.sw new file mode 100644 index 00000000000..e11471b2cbe --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/flags_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/flags_inline_tests/Forc.toml new file mode 100644 index 00000000000..18fc492db04 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/flags_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/flags_inline_tests/src/main.sw new file mode 100644 index 00000000000..dc8d7dac430 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/hash_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/hash_inline_tests/Forc.toml new file mode 100644 index 00000000000..b0e33a7ead7 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/hash_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/hash_inline_tests/src/main.sw new file mode 100644 index 00000000000..ccef01df70f --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/identity_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/identity_inline_tests/Forc.toml new file mode 100644 index 00000000000..e124feb3c7a --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/identity_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/identity_inline_tests/src/main.sw new file mode 100644 index 00000000000..b6f5d148622 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/math_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/math_inline_tests/Forc.toml new file mode 100644 index 00000000000..6ac992f8d23 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/math_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/math_inline_tests/src/main.sw new file mode 100644 index 00000000000..02e0cc2458b --- /dev/null +++ b/test/src/in_language_tests/test_programs/math_inline_tests/src/main.sw @@ -0,0 +1,369 @@ +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()); +} diff --git a/test/src/in_language_tests/test_programs/option_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/option_inline_tests/Forc.toml new file mode 100644 index 00000000000..4a79bcec8cd --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/option_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/option_inline_tests/src/main.sw new file mode 100644 index 00000000000..7ca19c1d4d7 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/Forc.toml new file mode 100644 index 00000000000..7df7ff1fc55 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw new file mode 100644 index 00000000000..bcb1c1b7c24 --- /dev/null +++ b/test/src/in_language_tests/test_programs/primitive_conversions_b256_inline_tests/src/main.sw @@ -0,0 +1,158 @@ +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 res = b256::try_from(initial_bytes); + let expected = 0x3333333333333333333333333333333333333333333333333333333333333333; + + assert(res.unwrap() == expected); + + 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 res = b256::try_from(second_bytes); + assert(res.is_none()); + + // bytes is still available to use: + assert(second_bytes.len() == 33); + assert(second_bytes.capacity() == 33); +} + +#[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/in_language_tests/test_programs/primitive_conversions_str_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/primitive_conversions_str_inline_tests/Forc.toml new file mode 100644 index 00000000000..40441657520 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/primitive_conversions_str_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/Forc.toml new file mode 100644 index 00000000000..c1aec88c9bb --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/primitive_conversions_u16_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/Forc.toml new file mode 100644 index 00000000000..7aba4002607 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/primitive_conversions_u256_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/Forc.toml new file mode 100644 index 00000000000..f49eef53b36 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/primitive_conversions_u32_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/Forc.toml new file mode 100644 index 00000000000..e44d33356eb --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/primitive_conversions_u64_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/Forc.toml new file mode 100644 index 00000000000..11ba8b72886 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/primitive_conversions_u8_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/result_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/result_inline_tests/Forc.toml new file mode 100644 index 00000000000..be492595367 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/result_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/result_inline_tests/src/main.sw new file mode 100644 index 00000000000..2166d4b643b --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/revert_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/revert_inline_tests/Forc.toml new file mode 100644 index 00000000000..4d01e7378cb --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/revert_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/revert_inline_tests/src/main.sw new file mode 100644 index 00000000000..cff1e49fc0e --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/string_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/string_inline_tests/Forc.toml new file mode 100644 index 00000000000..fc32fcd089e --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/string_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/string_inline_tests/src/main.sw new file mode 100644 index 00000000000..ec1313fa345 --- /dev/null +++ b/test/src/in_language_tests/test_programs/string_inline_tests/src/main.sw @@ -0,0 +1,379 @@ +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.capacity() == 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); +} + +#[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/in_language_tests/test_programs/u128_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/u128_inline_tests/Forc.toml new file mode 100644 index 00000000000..c218c1f64cd --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/u128_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/u128_inline_tests/src/main.sw new file mode 100644 index 00000000000..c7fc8405215 --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/vec_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/vec_inline_tests/Forc.toml new file mode 100644 index 00000000000..3481c693acf --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/vec_inline_tests/src/main.sw b/test/src/in_language_tests/test_programs/vec_inline_tests/src/main.sw new file mode 100644 index 00000000000..5ac6a8fef32 --- /dev/null +++ b/test/src/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()); + 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()); + 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/in_language_tests/test_programs/vm_evm_ecr_inline_tests/Forc.toml b/test/src/in_language_tests/test_programs/vm_evm_ecr_inline_tests/Forc.toml new file mode 100644 index 00000000000..ecdc1cddaad --- /dev/null +++ b/test/src/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/in_language_tests/test_programs/vm_evm_ecr_inline_tests/src/main.sw b/test/src/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/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/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/Forc.toml b/test/src/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/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/in_language_tests/test_programs/vm_evm_evm_address_inline_tests/src/main.sw b/test/src/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/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); +}