From c1f05eec75a9d171157a5907ed508508153b37f1 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 3 Aug 2022 11:23:16 -0400 Subject: [PATCH 01/46] Inital Commit --- sway_libs/.gitignore | 2 ++ sway_libs/Forc.toml | 7 +++++++ sway_libs/src/lib.sw | 3 +++ sway_libs/src/string.sw | 1 + tests/src/.gitignore | 2 ++ tests/src/Cargo.toml | 15 +++++++++++++++ tests/src/test_projects/harness.rs | 3 +++ tests/src/test_projects/string/.gitignore | 2 ++ tests/src/test_projects/string/Forc.toml | 8 ++++++++ tests/src/test_projects/string/mod.rs | 0 tests/src/test_projects/string/src/main.sw | 1 + 11 files changed, 44 insertions(+) create mode 100644 sway_libs/.gitignore create mode 100644 sway_libs/Forc.toml create mode 100644 sway_libs/src/lib.sw create mode 100644 sway_libs/src/string.sw create mode 100644 tests/src/.gitignore create mode 100644 tests/src/Cargo.toml create mode 100644 tests/src/test_projects/harness.rs create mode 100644 tests/src/test_projects/string/.gitignore create mode 100644 tests/src/test_projects/string/Forc.toml create mode 100644 tests/src/test_projects/string/mod.rs create mode 100644 tests/src/test_projects/string/src/main.sw diff --git a/sway_libs/.gitignore b/sway_libs/.gitignore new file mode 100644 index 00000000..5e7f2c02 --- /dev/null +++ b/sway_libs/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/sway_libs/Forc.toml b/sway_libs/Forc.toml new file mode 100644 index 00000000..13bf5958 --- /dev/null +++ b/sway_libs/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "lib.sw" +license = "Apache-2.0" +name = "sway_libs" + +[dependencies] diff --git a/sway_libs/src/lib.sw b/sway_libs/src/lib.sw new file mode 100644 index 00000000..9f08544e --- /dev/null +++ b/sway_libs/src/lib.sw @@ -0,0 +1,3 @@ +library sway_libs; + +dep string; diff --git a/sway_libs/src/string.sw b/sway_libs/src/string.sw new file mode 100644 index 00000000..3a0a4f3f --- /dev/null +++ b/sway_libs/src/string.sw @@ -0,0 +1 @@ +library string; \ No newline at end of file diff --git a/tests/src/.gitignore b/tests/src/.gitignore new file mode 100644 index 00000000..5e7f2c02 --- /dev/null +++ b/tests/src/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/tests/src/Cargo.toml b/tests/src/Cargo.toml new file mode 100644 index 00000000..b06aecbc --- /dev/null +++ b/tests/src/Cargo.toml @@ -0,0 +1,15 @@ +[project] +name = "tests" +version = "0.0.0" +authors = ["Fuel Labs "] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +fuels = { version = "0.19" } +tokio = { version = "1.12", features = ["rt", "macros"] } + +[[test]] +harness = true +name = "integration_tests" +path = "test_projects/harness.rs" diff --git a/tests/src/test_projects/harness.rs b/tests/src/test_projects/harness.rs new file mode 100644 index 00000000..ddf9bf13 --- /dev/null +++ b/tests/src/test_projects/harness.rs @@ -0,0 +1,3 @@ +// Add test modules here: + +mod string; \ No newline at end of file diff --git a/tests/src/test_projects/string/.gitignore b/tests/src/test_projects/string/.gitignore new file mode 100644 index 00000000..5e7f2c02 --- /dev/null +++ b/tests/src/test_projects/string/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/tests/src/test_projects/string/Forc.toml b/tests/src/test_projects/string/Forc.toml new file mode 100644 index 00000000..874ec67f --- /dev/null +++ b/tests/src/test_projects/string/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "string" + +[dependencies] +sway_libs = { path = "../../../../sway_libs"} diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs new file mode 100644 index 00000000..e69de29b diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw new file mode 100644 index 00000000..b5fdc857 --- /dev/null +++ b/tests/src/test_projects/string/src/main.sw @@ -0,0 +1 @@ +contract; From 1dbabccaeadcfde3fe4129a134d5e3d4a8d6529e Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 3 Aug 2022 11:51:09 -0400 Subject: [PATCH 02/46] Add base functions --- sway_libs/src/string.sw | 78 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/sway_libs/src/string.sw b/sway_libs/src/string.sw index 3a0a4f3f..0b35ed0c 100644 --- a/sway_libs/src/string.sw +++ b/sway_libs/src/string.sw @@ -1 +1,77 @@ -library string; \ No newline at end of file +library string; + +use std::{ + vec::Vec, +}; + +struct String { + bytes: Vec +} + +impl String { + + /// Appends an element to the back of the `String`. + pub fn append(mut self, value: u8) { + bytes.push(value); + } + + /// Returns the bytes stored for the `String`. + pub fn as_bytes(self) -> Vec { + self.vec + } + + // Not possible? + // pub fn as_str(self) -> S { + // self + // } + + /// Gets the capacity of the allocation. + pub fn capacity(self) -> u64 { + self.vec.capacity() + } + + /// Truncates this `String`, removing all contents. + /// + /// While this means the `String` will have a length of zero, it does not + /// touch its capacity. + pub fn clear(mut self) { + self.vec.clear() + } + + /// Attempts to convert a static `str` to a `String` + pub fn from_str(mut self, value: S) { + let len = size_of::(); + let mut ptr = addr_of(value); + let mut iterator = 0; + + while iterator < len { + self.bytes.push(read(ptr + iterator)); + iterator += 1; + } + } + + /// Returns `true` if the vector contains no elements. + pub fn is_empty(self) -> bool { + self.vec.is_empty() + } + + /// Returns the number of elements in the vector, also referred to + /// as its 'length'. + pub fn len(self) -> u64 { + self.bytes.len() + } + + /// Constructs a new, empty `String`. + pub fn new() -> Self { + Self { + bytes: ~Vec::new() + } + } + + /// Constructs a new, empty `String` with the specified capacity. + pub fn with_capacity(capacity: u64) -> Self { + Self { + bytes:: ~Vec::with_capacity(capacity) + } + } +} From 8a24ca7edf332250e4d5ba39af6d496acc5b3c65 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 3 Aug 2022 15:43:48 -0400 Subject: [PATCH 03/46] Refactor and Rename to match Rust --- sway_libs/Forc.lock | 14 +++++++++++++ sway_libs/src/string.sw | 45 +++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 sway_libs/Forc.lock diff --git a/sway_libs/Forc.lock b/sway_libs/Forc.lock new file mode 100644 index 00000000..59758a0e --- /dev/null +++ b/sway_libs/Forc.lock @@ -0,0 +1,14 @@ +[[package]] +name = 'core' +source = 'path+from-root-603BAB4590C454D3' +dependencies = [] + +[[package]] +name = 'std' +source = 'git+https://github.com/fuellabs/sway?tag=v0.19.0#5c716e1ba55d755555ed5aa186c883f73c4f90dc' +dependencies = ['core'] + +[[package]] +name = 'sway_libs' +source = 'root' +dependencies = ['std'] diff --git a/sway_libs/src/string.sw b/sway_libs/src/string.sw index 0b35ed0c..1aa16c00 100644 --- a/sway_libs/src/string.sw +++ b/sway_libs/src/string.sw @@ -1,6 +1,7 @@ library string; use std::{ + mem::read, vec::Vec, }; @@ -10,14 +11,9 @@ struct String { impl String { - /// Appends an element to the back of the `String`. - pub fn append(mut self, value: u8) { - bytes.push(value); - } - /// Returns the bytes stored for the `String`. pub fn as_bytes(self) -> Vec { - self.vec + self.bytes } // Not possible? @@ -27,7 +23,7 @@ impl String { /// Gets the capacity of the allocation. pub fn capacity(self) -> u64 { - self.vec.capacity() + self.bytes.capacity() } /// Truncates this `String`, removing all contents. @@ -35,24 +31,12 @@ impl String { /// While this means the `String` will have a length of zero, it does not /// touch its capacity. pub fn clear(mut self) { - self.vec.clear() - } - - /// Attempts to convert a static `str` to a `String` - pub fn from_str(mut self, value: S) { - let len = size_of::(); - let mut ptr = addr_of(value); - let mut iterator = 0; - - while iterator < len { - self.bytes.push(read(ptr + iterator)); - iterator += 1; - } + self.bytes.clear() } /// Returns `true` if the vector contains no elements. pub fn is_empty(self) -> bool { - self.vec.is_empty() + self.bytes.is_empty() } /// Returns the number of elements in the vector, also referred to @@ -68,10 +52,27 @@ impl String { } } + /// Appends an element to the back of the `String`. + pub fn push(mut self, value: u8) { + self.bytes.push(value); + } + + /// Attempts to convert a static `str` to a `String` + pub fn push_str(mut self, ptr: u64, len: u64) { + let mut ptr = ptr; + let mut iterator = 0; + + // This will probably need to change + while iterator < len { + self.bytes.push(read(ptr + iterator)); + iterator += 1; + } + } + /// Constructs a new, empty `String` with the specified capacity. pub fn with_capacity(capacity: u64) -> Self { Self { - bytes:: ~Vec::with_capacity(capacity) + bytes: ~Vec::with_capacity(capacity) } } } From 80471e3b657e53883c8b491d5a4fe6ae2fc7c288 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 3 Aug 2022 16:13:32 -0400 Subject: [PATCH 04/46] Tests outline --- sway_libs/src/string.sw | 2 +- tests/src/test_projects/string/Forc.lock | 22 +++++++++ tests/src/test_projects/string/mod.rs | 49 ++++++++++++++++++++ tests/src/test_projects/string/src/main.sw | 53 ++++++++++++++++++++++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 tests/src/test_projects/string/Forc.lock diff --git a/sway_libs/src/string.sw b/sway_libs/src/string.sw index 1aa16c00..628aa083 100644 --- a/sway_libs/src/string.sw +++ b/sway_libs/src/string.sw @@ -5,7 +5,7 @@ use std::{ vec::Vec, }; -struct String { +pub struct String { bytes: Vec } diff --git a/tests/src/test_projects/string/Forc.lock b/tests/src/test_projects/string/Forc.lock new file mode 100644 index 00000000..7a7c6cbd --- /dev/null +++ b/tests/src/test_projects/string/Forc.lock @@ -0,0 +1,22 @@ +[[package]] +name = 'core' +source = 'path+from-root-603BAB4590C454D3' +dependencies = [] + +[[package]] +name = 'std' +source = 'git+https://github.com/fuellabs/sway?tag=v0.19.0#5c716e1ba55d755555ed5aa186c883f73c4f90dc' +dependencies = ['core'] + +[[package]] +name = 'string' +source = 'root' +dependencies = [ + 'std', + 'sway_libs', +] + +[[package]] +name = 'sway_libs' +source = 'path+from-root-245F7BDB5423C0B5' +dependencies = ['std'] diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index e69de29b..187e9dc2 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -0,0 +1,49 @@ +use fuels::{prelude::*, tx::ContractId}; + +abigen!( + TestStringLib, + "test_projects/string/out/debug/string-abi.json" +); + +async fn test_merkle_proof_instance() -> TestStringLib { + // Launch a local network and deploy the contract + let wallet = launch_provider_and_get_wallet().await; + + let id = Contract::deploy( + "test_projects/merkle_proof/out/debug/string.bin", + &wallet, + TxParameters::default(), + StorageConfiguration::default(), + ) + .await + .unwrap(); + + let instance = TestStringLibBuilder::new(id.to_string(), wallet).build(); + + instance +} + +// mod merkle_proof { + +// use super::*; + +// mod success { + +// use super::*; + +// #[tokio::test] +// async fn verifies_merkle_proof() { +// let _instance = test_merkle_proof_instance().await; + +// // Test here once https://github.com/FuelLabs/fuels-rs/issues/353 is closed +// } + +// } + +// mod reverts { + +// use super::*; + +// } + +// } \ No newline at end of file diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index b5fdc857..2192222c 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -1 +1,54 @@ contract; + +use std::vec::Vec; +use sway_libs::string::String; + +abi StringTest { + fn as_bytes(); + fn capacity(); + fn clear(); + fn is_empty(); + fn len(); + fn new(); + fn push(); + fn push_str(); + fn with_capacity(); +} + +impl StringTest for Contract { + fn as_bytes() { + + } + + fn capacity() { + + } + + fn clear() { + + } + + fn is_empty() { + + } + + fn len() { + + } + + fn new() { + + } + + fn push() { + + } + + fn push_str() { + + } + + fn with_capacity() { + + } +} From 2838a0ddb6c3a9d43e375fd2e0430d0d0e7875ae Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 3 Aug 2022 16:14:58 -0400 Subject: [PATCH 05/46] Update to string --- tests/src/test_projects/string/mod.rs | 29 +++++++++++++-------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index 187e9dc2..53a131f1 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -5,7 +5,7 @@ abigen!( "test_projects/string/out/debug/string-abi.json" ); -async fn test_merkle_proof_instance() -> TestStringLib { +async fn test_string_instance() -> TestStringLib { // Launch a local network and deploy the contract let wallet = launch_provider_and_get_wallet().await; @@ -23,27 +23,26 @@ async fn test_merkle_proof_instance() -> TestStringLib { instance } -// mod merkle_proof { +mod as_bytes { -// use super::*; + use super::*; -// mod success { + mod success { -// use super::*; + use super::*; -// #[tokio::test] -// async fn verifies_merkle_proof() { -// let _instance = test_merkle_proof_instance().await; + #[tokio::test] + async fn gets_bytes() { + let _instance = test_string_instance().await; -// // Test here once https://github.com/FuelLabs/fuels-rs/issues/353 is closed -// } + } -// } + } -// mod reverts { + mod reverts { -// use super::*; + use super::*; -// } + } -// } \ No newline at end of file +} \ No newline at end of file From de3d3bf567bb66fc5f6b3264f86e07db06ae6d4d Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 3 Aug 2022 16:48:26 -0400 Subject: [PATCH 06/46] Add len function test --- tests/src/test_projects/string/mod.rs | 45 ++++++++---- tests/src/test_projects/string/src/main.sw | 82 +++++++++++++++++----- 2 files changed, 94 insertions(+), 33 deletions(-) diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index 53a131f1..b47a8593 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -1,4 +1,4 @@ -use fuels::{prelude::*, tx::ContractId}; +use fuels::{prelude::*}; abigen!( TestStringLib, @@ -10,7 +10,7 @@ async fn test_string_instance() -> TestStringLib { let wallet = launch_provider_and_get_wallet().await; let id = Contract::deploy( - "test_projects/merkle_proof/out/debug/string.bin", + "test_projects/string/out/debug/string.bin", &wallet, TxParameters::default(), StorageConfiguration::default(), @@ -23,26 +23,43 @@ async fn test_string_instance() -> TestStringLib { instance } -mod as_bytes { +// mod as_bytes { - use super::*; +// use super::*; - mod success { +// mod success { - use super::*; +// use super::*; - #[tokio::test] - async fn gets_bytes() { - let _instance = test_string_instance().await; +// #[tokio::test] +// async fn gets_bytes() { +// let _instance = test_string_instance().await; - } +// } - } +// } + +// mod reverts { + +// use super::*; + +// } + +// } - mod reverts { +mod len { + + use super::*; + + mod success { use super::*; - } + #[tokio::test] + async fn gets_length() { + let instance = test_string_instance().await; -} \ No newline at end of file + let _result = instance.test_len().call().await.unwrap(); + } + } +} diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 2192222c..bb707cb9 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -1,54 +1,98 @@ contract; -use std::vec::Vec; +use std::{ + assert::assert, + vec::Vec +}; use sway_libs::string::String; abi StringTest { - fn as_bytes(); - fn capacity(); - fn clear(); - fn is_empty(); - fn len(); - fn new(); - fn push(); - fn push_str(); - fn with_capacity(); + fn test_as_bytes(); + fn test_capacity(); + fn test_clear(); + fn test_is_empty(); + fn test_len(); + fn test_new(); + fn test_push(); + fn test_push_str(); + fn test_with_capacity(); } impl StringTest for Contract { - fn as_bytes() { + fn test_as_bytes() { } - fn capacity() { + fn test_capacity() { } - fn clear() { + fn test_clear() { } - fn is_empty() { + fn test_is_empty() { + let mut string = ~String::new(); + assert(string.is_empty()); } - fn len() { + fn test_len() { + let mut string = ~String::new(); + assert(string.len() == 0); + + let number0 = 0u8; + let number1 = 1u8; + let number2 = 2u8; + let number3 = 3u8; + let number4 = 4u8; + let number5 = 5u8; + let number6 = 6u8; + let number7 = 7u8; + let number8 = 8u8; + + string.push(number0); + assert(string.len() == 1); + + string.push(number1); + assert(string.len() == 2); + + string.push(number2); + assert(string.len() == 3); + + string.push(number3); + assert(string.len() == 4); + + string.push(number4); + assert(string.len() == 5); + + string.push(number5); + assert(string.len() == 6); + + string.push(number6); + assert(string.len() == 7); + + string.push(number7); + assert(string.len() == 8); + + string.push(number8); + assert(string.len() == 9); } - fn new() { + fn test_new() { } - fn push() { + fn test_push() { } - fn push_str() { + fn test_push_str() { } - fn with_capacity() { + fn test_with_capacity() { } } From 4b338c7c7949354a57a2cf06d3eba410a379a0d7 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 3 Aug 2022 16:55:14 -0400 Subject: [PATCH 07/46] Add is_empty tests --- tests/src/test_projects/string/mod.rs | 17 +++++++++++++++++ tests/src/test_projects/string/src/main.sw | 19 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index b47a8593..689b7e94 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -47,6 +47,23 @@ async fn test_string_instance() -> TestStringLib { // } +mod is_empty { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn returns_if_empty() { + let instance = test_string_instance().await; + + let _result = instance.test_is_empty().call().await.unwrap(); + } + } +} + mod len { use super::*; diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index bb707cb9..da14e638 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -34,7 +34,24 @@ impl StringTest for Contract { fn test_is_empty() { let mut string = ~String::new(); - assert(string.is_empty()); + assert(string.is_empty()); + + string.push(0u8); + assert(!string.is_empty()); + + string.push(1u8); + assert(!string.is_empty()); + + string.clear(); + assert(string.is_empty()); + + string.push(0u8); + assert(!string.is_empty()); + + string.push(1u8); + assert(!string.is_empty()); + + // TODO: Pop and check if empty } fn test_len() { From e16aeb98f7be2c4938c2ce733d59529144e8b103 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 4 Aug 2022 11:50:41 -0400 Subject: [PATCH 08/46] Add clear function tests --- tests/src/test_projects/string/mod.rs | 17 ++++++++ tests/src/test_projects/string/src/main.sw | 46 +++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index 689b7e94..aa701ca0 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -47,6 +47,23 @@ async fn test_string_instance() -> TestStringLib { // } +mod clear { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn clears_string() { + let instance = test_string_instance().await; + + let _result = instance.test_clear().call().await.unwrap(); + } + } +} + mod is_empty { use super::*; diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index da14e638..6b9b3fbc 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -24,11 +24,55 @@ impl StringTest for Contract { } fn test_capacity() { - + } fn test_clear() { + let mut string = ~String::new(); + + let number0 = 0u8; + let number1 = 1u8; + let number2 = 2u8; + let number3 = 3u8; + let number4 = 4u8; + let number5 = 5u8; + let number6 = 6u8; + let number7 = 7u8; + let number8 = 8u8; + + assert(string.is_empty()); + + string.clear(); + assert(string.is_empty()); + + string.push(number0); + assert(!string.is_empty()); + + string.clear(); + assert(string.is_empty()); + + string.push(number0); + string.push(number1); + string.push(number2); + string.push(number3); + string.push(number4); + string.push(number5); + string.push(number6); + string.push(number7); + string.push(number8); + assert(!string.is_empty()); + string.clear(); + assert(string.is_empty()); + + string.clear(); + assert(string.is_empty()); + + string.push(number0); + assert(!string.is_empty()); + + string.clear(); + assert(string.is_empty()); } fn test_is_empty() { From 6caec295a8658d9c94588e72c31ead8a270f2d41 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 4 Aug 2022 11:51:46 -0400 Subject: [PATCH 09/46] Add capacity function tests --- tests/src/test_projects/string/mod.rs | 17 +++++++++ tests/src/test_projects/string/src/main.sw | 44 ++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index aa701ca0..f12abcf3 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -47,6 +47,23 @@ async fn test_string_instance() -> TestStringLib { // } +mod capacity { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn returns_capacity() { + let instance = test_string_instance().await; + + let _result = instance.test_capacity().call().await.unwrap(); + } + } +} + mod clear { use super::*; diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 6b9b3fbc..5118a65a 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -24,7 +24,51 @@ impl StringTest for Contract { } fn test_capacity() { + let mut string = ~String::new(); + + let number0 = 0u8; + let number1 = 1u8; + let number2 = 2u8; + let number3 = 3u8; + let number4 = 4u8; + let number5 = 5u8; + let number6 = 6u8; + let number7 = 7u8; + let number8 = 8u8; + + assert(string.capacity() == 0); + + string.push(number0); + assert(string.capacity() == 1); + + string.push(number1); + assert(string.capacity() == 2); + + string.push(number2); + assert(string.capacity() == 4); + + string.push(number3); + assert(string.capacity() == 4); + + string.push(number4); + assert(string.capacity() == 8); + + string.push(number5); + assert(string.capacity() == 8); + + string.push(number6); + string.push(number7); + string.push(number8); + assert(string.capacity() == 8); + + string.push(number0); + assert(string.capacity() == 16); + string.clear(); + assert(string.capacity() == 16); + + string.push(number0); + assert(string.capacity() == 16); } fn test_clear() { From b109dfb1581e537ff908a9f9dc03f2becf127a0c Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 4 Aug 2022 11:58:49 -0400 Subject: [PATCH 10/46] Add new function tests --- tests/src/test_projects/string/mod.rs | 17 +++++++++++++++++ tests/src/test_projects/string/src/main.sw | 6 +++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index f12abcf3..8e715422 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -114,3 +114,20 @@ mod len { } } } + +mod new { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn creates_string() { + let instance = test_string_instance().await; + + let _result = instance.test_new().call().await.unwrap(); + } + } +} diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 5118a65a..621cd000 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -186,7 +186,11 @@ impl StringTest for Contract { } fn test_new() { - + let mut string = ~String::new(); + + assert(string.len() == 0); + assert(string.is_empty()); + assert(string.capacity() == 0); } fn test_push() { From e3ab49c7ad100033bb231f3c097795664610b89f Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 4 Aug 2022 12:25:28 -0400 Subject: [PATCH 11/46] Add push function tests --- tests/src/test_projects/string/mod.rs | 17 +++++ tests/src/test_projects/string/src/main.sw | 72 ++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index 8e715422..45c40e84 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -131,3 +131,20 @@ mod new { } } } + +mod push { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn pushes_to_string() { + let instance = test_string_instance().await; + + let _result = instance.test_push().call().await.unwrap(); + } + } +} diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 621cd000..9442b5de 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -194,7 +194,79 @@ impl StringTest for Contract { } fn test_push() { + let mut string = ~String::new(); + + // TODO: Eq will be needed for Vec + //assert(string.as_bytes() == ~Vec::new()); + assert(string.len() == 0); + assert(string.is_empty()); + assert(string.capacity() == 0); + + let number0 = 0u8; + let number1 = 1u8; + let number2 = 2u8; + let number3 = 3u8; + let number4 = 4u8; + let number5 = 5u8; + let number6 = 6u8; + let number7 = 7u8; + let number8 = 8u8; + + string.push(number0); + // assert(string.get(0) == number0); + assert(string.len() == 1); + + string.push(number1); + // assert(string.get(1) == number1); + assert(string.len() == 2); + + string.push(number2); + // assert(string.get(2) == number2); + assert(string.len() == 3); + + string.push(number3); + // assert(string.get(3) == number3); + assert(string.len() == 4); + + string.push(number4); + // assert(string.get(4) == number4); + assert(string.len() == 5); + + string.push(number5); + // assert(string.get(5) == number5); + assert(string.len() == 6); + + string.push(number6); + // assert(string.get(6) == number6); + assert(string.len() == 7); + + string.push(number7); + // assert(string.get(7) == number7); + assert(string.len() == 8); + + string.push(number8); + // assert(string.get(8) == number8); + assert(string.len() == 9); + string.push(number1); + // assert(string.get(9) == number1); + assert(string.len() == 10); + + string.clear(); + assert(string.len() == 0); + assert(string.is_empty()); + + string.push(number1); + // assert(string.get(0) == number1); + assert(string.len() == 1); + + string.push(number1); + // assert(string.get(1) == number1); + assert(string.len() == 2); + + string.push(number0); + // assert(string.get(2) == number0); + assert(string.len() == 3); } fn test_push_str() { From 352c9970f641b46d9e032b115fee4c076f490c06 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 4 Aug 2022 12:36:14 -0400 Subject: [PATCH 12/46] Fix capacity test --- tests/src/test_projects/string/src/main.sw | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 9442b5de..e3a2a7af 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -58,10 +58,9 @@ impl StringTest for Contract { string.push(number6); string.push(number7); - string.push(number8); assert(string.capacity() == 8); - string.push(number0); + string.push(number8); assert(string.capacity() == 16); string.clear(); From 2830e6b468f7cb3666ff63219d08e715bf04164e Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 4 Aug 2022 12:44:57 -0400 Subject: [PATCH 13/46] Add with_capacity function tests --- tests/src/test_projects/string/mod.rs | 17 +++++++++ tests/src/test_projects/string/src/main.sw | 40 ++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index 45c40e84..33a20da7 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -148,3 +148,20 @@ mod push { } } } + +mod with_capacity { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn creates_string_with_capacity() { + let instance = test_string_instance().await; + + let _result = instance.test_with_capacity().call().await.unwrap(); + } + } +} diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index e3a2a7af..df3e28f7 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -274,5 +274,45 @@ impl StringTest for Contract { fn test_with_capacity() { + let mut iterator = 0; + + while iterator < 16 { + let mut string = ~String::with_capacity(iterator); + assert(string.capacity() == iterator); + + iterator += 1; + } + + let number0 = 0u8; + let number1 = 1u8; + let number2 = 2u8; + let number3 = 3u8; + let number4 = 4u8; + + let mut string = ~String::with_capacity(0); + assert(string.capacity() == 0); + + string.push(number0); + assert(string.capacity() == 1); + string.push(number1); + assert(string.capacity() == 2); + string.push(number2); + assert(string.capacity() == 4); + string.clear(); + assert(string.capacity() == 4); + + let mut string = ~String::with_capacity(4); + assert(string.capacity() == 4); + + string.push(number0); + assert(string.capacity() == 4); + string.push(number1); + assert(string.capacity() == 4); + string.push(number2); + assert(string.capacity() == 4); + string.push(number3); + assert(string.capacity() == 4); + string.push(number4); + assert(string.capacity() == 8); } } From cf8608f98e62000ddbee83af0823ef1016494ce1 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 4 Aug 2022 16:36:03 -0400 Subject: [PATCH 14/46] Add some more functions --- sway_libs/src/string.sw | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/sway_libs/src/string.sw b/sway_libs/src/string.sw index 628aa083..43556b80 100644 --- a/sway_libs/src/string.sw +++ b/sway_libs/src/string.sw @@ -2,6 +2,7 @@ library string; use std::{ mem::read, + option::Option, vec::Vec, }; @@ -34,6 +35,19 @@ impl String { self.bytes.clear() } + /// Converts a vector of bytes to a `String`. + pub fn from_utf8(vec: Vec) -> Self { + // TODO: Implement + Self { + bytes: ~Vec::new() + } + } + + // Inserts a character at the index within the string. + pub fn insert(mut self, index: u64, character: u8) { + self.bytes.insert(index, character); + } + /// Returns `true` if the vector contains no elements. pub fn is_empty(self) -> bool { self.bytes.is_empty() @@ -52,6 +66,16 @@ impl String { } } + /// Returns the element found at the index of given. + pub fn nth(self, index: u64) -> Option { + self.bytes.get(index) + } + + /// Removes the last character from the string buffer and returns it. + pub fn pop(mut self) -> Option { + self.bytes.pop() + } + /// Appends an element to the back of the `String`. pub fn push(mut self, value: u8) { self.bytes.push(value); @@ -59,7 +83,8 @@ impl String { /// Attempts to convert a static `str` to a `String` pub fn push_str(mut self, ptr: u64, len: u64) { - let mut ptr = ptr; + // TODO: This will probably need some compliler work + let mut ptr: u64 = ptr; let mut iterator = 0; // This will probably need to change @@ -69,6 +94,11 @@ impl String { } } + /// Removes and returns the element at position `index` within the string. + pub fn remove(mut self, index: u64) -> u8 { + self.bytes.remove(index) + } + /// Constructs a new, empty `String` with the specified capacity. pub fn with_capacity(capacity: u64) -> Self { Self { From f3f195b1bd56a2edd462ac574fcabf52fed75952 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Fri, 5 Aug 2022 16:39:23 -0400 Subject: [PATCH 15/46] First iteration of push_str --- sway_libs/src/string.sw | 7 ++-- tests/src/test_projects/string/mod.rs | 19 ++++++++++ tests/src/test_projects/string/src/main.sw | 41 +++++++++++++++++++++- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/sway_libs/src/string.sw b/sway_libs/src/string.sw index 43556b80..84b89e09 100644 --- a/sway_libs/src/string.sw +++ b/sway_libs/src/string.sw @@ -81,15 +81,16 @@ impl String { self.bytes.push(value); } + // TODO: This will probably need some compliler work /// Attempts to convert a static `str` to a `String` pub fn push_str(mut self, ptr: u64, len: u64) { - // TODO: This will probably need some compliler work let mut ptr: u64 = ptr; - let mut iterator = 0; + let mut iterator: u64 = 0; // This will probably need to change while iterator < len { - self.bytes.push(read(ptr + iterator)); + let byte: u8 = read(ptr + iterator); + self.bytes.push(byte); iterator += 1; } } diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index 33a20da7..9463f51b 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -1,4 +1,6 @@ use fuels::{prelude::*}; +use std::io::stdout; +use std::io::Write; abigen!( TestStringLib, @@ -149,6 +151,23 @@ mod push { } } +mod push_str { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn pushes_string() { + let instance = test_string_instance().await; + + let _result = instance.test_push_str().call().await.unwrap(); + } + } +} + mod with_capacity { use super::*; diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index df3e28f7..1b2ba582 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -2,7 +2,10 @@ contract; use std::{ assert::assert, - vec::Vec + intrinsics::size_of, + mem::{addr_of, read}, + option::Option, + vec::Vec, }; use sway_libs::string::String; @@ -269,7 +272,43 @@ impl StringTest for Contract { } fn test_push_str() { + let mut string = ~String::new(); + + let fuel_str: str[4] = "fuel"; + let ptr: u64 = addr_of(fuel_str); + + string.push_str(ptr, 4); + let byte1: u8 = read(ptr); + let byte2: u8 = read(ptr + 1); + let byte3: u8 = read(ptr + 2); + let byte4: u8 = read(ptr + 3); + + assert(string.len() == 4); + assert(string.nth(0).unwrap() == byte1); + assert(string.nth(1).unwrap() == byte2); + assert(string.nth(2).unwrap() == byte3); + assert(string.nth(3).unwrap() == byte4); + + // let f: str[1] = "f"; + // let u: str[1] = "u"; + // let e: str[1] = "e"; + // let l: str[1] = "l"; + + // let ptr_f: u64 = addr_of(f); + // let ptr_u: u64 = addr_of(u); + // let ptr_e: u64 = addr_of(e); + // let ptr_l: u64 = addr_of(l); + + // let byte_f: u8 = read(ptr_f); + // let byte_u: u8 = read(ptr_u); + // let byte_e: u8 = read(ptr_e); + // let byte_l: u8 = read(ptr_l); + + // assert(string.nth(0).unwrap() == byte_f); + // assert(string.nth(1).unwrap() == byte_u); + // assert(string.nth(2).unwrap() == byte_e); + // assert(string.nth(3).unwrap() == byte_l); } fn test_with_capacity() { From eafb587e0fcfc65bcea3634974fc2c0571452e86 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 4 Oct 2022 15:17:10 -0400 Subject: [PATCH 16/46] Remove functions with relation to str --- sway_libs/src/string.sw | 191 +++++++++++++++++----------------------- 1 file changed, 82 insertions(+), 109 deletions(-) diff --git a/sway_libs/src/string.sw b/sway_libs/src/string.sw index 84b89e09..e4a643d9 100644 --- a/sway_libs/src/string.sw +++ b/sway_libs/src/string.sw @@ -1,109 +1,82 @@ -library string; - -use std::{ - mem::read, - option::Option, - vec::Vec, -}; - -pub struct String { - bytes: Vec -} - -impl String { - - /// Returns the bytes stored for the `String`. - pub fn as_bytes(self) -> Vec { - self.bytes - } - - // Not possible? - // pub fn as_str(self) -> S { - // self - // } - - /// Gets the capacity of the allocation. - pub fn capacity(self) -> u64 { - self.bytes.capacity() - } - - /// Truncates this `String`, removing all contents. - /// - /// While this means the `String` will have a length of zero, it does not - /// touch its capacity. - pub fn clear(mut self) { - self.bytes.clear() - } - - /// Converts a vector of bytes to a `String`. - pub fn from_utf8(vec: Vec) -> Self { - // TODO: Implement - Self { - bytes: ~Vec::new() - } - } - - // Inserts a character at the index within the string. - pub fn insert(mut self, index: u64, character: u8) { - self.bytes.insert(index, character); - } - - /// Returns `true` if the vector contains no elements. - pub fn is_empty(self) -> bool { - self.bytes.is_empty() - } - - /// Returns the number of elements in the vector, also referred to - /// as its 'length'. - pub fn len(self) -> u64 { - self.bytes.len() - } - - /// Constructs a new, empty `String`. - pub fn new() -> Self { - Self { - bytes: ~Vec::new() - } - } - - /// Returns the element found at the index of given. - pub fn nth(self, index: u64) -> Option { - self.bytes.get(index) - } - - /// Removes the last character from the string buffer and returns it. - pub fn pop(mut self) -> Option { - self.bytes.pop() - } - - /// Appends an element to the back of the `String`. - pub fn push(mut self, value: u8) { - self.bytes.push(value); - } - - // TODO: This will probably need some compliler work - /// Attempts to convert a static `str` to a `String` - pub fn push_str(mut self, ptr: u64, len: u64) { - let mut ptr: u64 = ptr; - let mut iterator: u64 = 0; - - // This will probably need to change - while iterator < len { - let byte: u8 = read(ptr + iterator); - self.bytes.push(byte); - iterator += 1; - } - } - - /// Removes and returns the element at position `index` within the string. - pub fn remove(mut self, index: u64) -> u8 { - self.bytes.remove(index) - } - - /// Constructs a new, empty `String` with the specified capacity. - pub fn with_capacity(capacity: u64) -> Self { - Self { - bytes: ~Vec::with_capacity(capacity) - } - } -} +library string; + +use std::{option::Option, vec::Vec}; + +pub struct String { + bytes: Vec, +} + +impl String { + /// Returns the bytes stored for the `String`. + pub fn as_bytes(self) -> Vec { + self.bytes + } + + /// Gets the capacity of the allocation. + pub fn capacity(self) -> u64 { + self.bytes.capacity() + } + + /// Truncates this `String`, removing all contents. + /// + /// While this means the `String` will have a length of zero, it does not + /// touch its capacity. + pub fn clear(self) { + self.bytes.clear() + } + + /// Converts a vector of bytes to a `String`. + pub fn from_utf8(vec: Vec) -> String { + String { bytes: vec } + } + + // Inserts a character at the index within the string. + pub fn insert(self, index: u64, character: u8) { + self.bytes.insert(index, character); + } + + /// Returns `true` if the vector contains no elements. + pub fn is_empty(self) -> bool { + self.bytes.is_empty() + } + + /// Returns the number of elements in the vector, also referred to + /// as its 'length'. + pub fn len(self) -> u64 { + self.bytes.len() + } + + /// Constructs a new, empty `String`. + pub fn new() -> Self { + Self { + bytes: ~Vec::new(), + } + } + + /// Returns the element found at the index of given. + pub fn nth(self, index: u64) -> Option { + self.bytes.get(index) + } + + /// Removes the last character from the string buffer and returns it. + pub fn pop(self) -> Option { + self.bytes.pop() + } + + /// Appends an element to the back of the `String`. + pub fn push(self, value: u8) { + self.bytes.push(value); + } + + /// Removes and returns the element at position `index` within the string. + pub fn remove(self, index: u64) -> u8 { + self.bytes.remove(index) + } + + /// Constructs a new, empty `String` with the specified capacity. + pub fn with_capacity(capacity: u64) -> Self { + Self { + bytes: ~Vec::with_capacity(capacity), + } + } +} From d7bf98d08fa41fa63b137badbc27266fdbeb130b Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 4 Oct 2022 15:22:49 -0400 Subject: [PATCH 17/46] Update function test list to match new string methods --- tests/src/test_projects/string/src/main.sw | 58 +++++++--------------- 1 file changed, 19 insertions(+), 39 deletions(-) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 1b2ba582..68423fed 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -13,11 +13,15 @@ abi StringTest { fn test_as_bytes(); fn test_capacity(); fn test_clear(); + fn test_from_utf8(); + fn test_insert(); fn test_is_empty(); fn test_len(); fn test_new(); + fn test_nth(); + fn test_pop(); fn test_push(); - fn test_push_str(); + fn test_remove(); fn test_with_capacity(); } @@ -121,6 +125,10 @@ impl StringTest for Contract { assert(string.is_empty()); } + fn test_from_utf8() { + + } + fn test_is_empty() { let mut string = ~String::new(); @@ -195,6 +203,14 @@ impl StringTest for Contract { assert(string.capacity() == 0); } + fn test_nth() { + + } + + fn test_pop() { + + } + fn test_push() { let mut string = ~String::new(); @@ -271,44 +287,8 @@ impl StringTest for Contract { assert(string.len() == 3); } - fn test_push_str() { - let mut string = ~String::new(); - - let fuel_str: str[4] = "fuel"; - let ptr: u64 = addr_of(fuel_str); - - string.push_str(ptr, 4); - - let byte1: u8 = read(ptr); - let byte2: u8 = read(ptr + 1); - let byte3: u8 = read(ptr + 2); - let byte4: u8 = read(ptr + 3); - - assert(string.len() == 4); - assert(string.nth(0).unwrap() == byte1); - assert(string.nth(1).unwrap() == byte2); - assert(string.nth(2).unwrap() == byte3); - assert(string.nth(3).unwrap() == byte4); - - // let f: str[1] = "f"; - // let u: str[1] = "u"; - // let e: str[1] = "e"; - // let l: str[1] = "l"; - - // let ptr_f: u64 = addr_of(f); - // let ptr_u: u64 = addr_of(u); - // let ptr_e: u64 = addr_of(e); - // let ptr_l: u64 = addr_of(l); - - // let byte_f: u8 = read(ptr_f); - // let byte_u: u8 = read(ptr_u); - // let byte_e: u8 = read(ptr_e); - // let byte_l: u8 = read(ptr_l); - - // assert(string.nth(0).unwrap() == byte_f); - // assert(string.nth(1).unwrap() == byte_u); - // assert(string.nth(2).unwrap() == byte_e); - // assert(string.nth(3).unwrap() == byte_l); + fn test_remove() { + } fn test_with_capacity() { From c37c295777369ff403d5e60005fcacc94b77510e Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 4 Oct 2022 15:24:00 -0400 Subject: [PATCH 18/46] Update empty tests --- tests/src/test_projects/string/src/main.sw | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 68423fed..db509518 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -129,6 +129,10 @@ impl StringTest for Contract { } + fn test_insert() { + + } + fn test_is_empty() { let mut string = ~String::new(); @@ -149,7 +153,11 @@ impl StringTest for Contract { string.push(1u8); assert(!string.is_empty()); - // TODO: Pop and check if empty + string.pop(); + assert(!string.is_empty()); + + string.pop(); + assert(string.is_empty()); } fn test_len() { @@ -288,7 +296,7 @@ impl StringTest for Contract { } fn test_remove() { - + } fn test_with_capacity() { From 05299540e665a90045299fbc1f445c9d5213fde4 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 4 Oct 2022 15:25:10 -0400 Subject: [PATCH 19/46] Update len tests --- tests/src/test_projects/string/src/main.sw | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index db509518..6d5b9b0a 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -201,6 +201,9 @@ impl StringTest for Contract { string.push(number8); assert(string.len() == 9); + + string.pop(); + assert(string.len() == 8); } fn test_new() { From 503364927b651fd2ba6e0b08ee59ab22046ec11b Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 4 Oct 2022 15:26:57 -0400 Subject: [PATCH 20/46] Update push test --- tests/src/test_projects/string/src/main.sw | 31 +++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 6d5b9b0a..ee2317a4 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -204,6 +204,9 @@ impl StringTest for Contract { string.pop(); assert(string.len() == 8); + + string.clear(); + assert(string.len() == 0); } fn test_new() { @@ -225,8 +228,6 @@ impl StringTest for Contract { fn test_push() { let mut string = ~String::new(); - // TODO: Eq will be needed for Vec - //assert(string.as_bytes() == ~Vec::new()); assert(string.len() == 0); assert(string.is_empty()); assert(string.capacity() == 0); @@ -242,43 +243,43 @@ impl StringTest for Contract { let number8 = 8u8; string.push(number0); - // assert(string.get(0) == number0); + assert(string.nth(0) == number0); assert(string.len() == 1); string.push(number1); - // assert(string.get(1) == number1); + assert(string.nth(1) == number1); assert(string.len() == 2); string.push(number2); - // assert(string.get(2) == number2); + assert(string.nth(2) == number2); assert(string.len() == 3); string.push(number3); - // assert(string.get(3) == number3); + assert(string.nth(3) == number3); assert(string.len() == 4); string.push(number4); - // assert(string.get(4) == number4); + assert(string.nth(4) == number4); assert(string.len() == 5); string.push(number5); - // assert(string.get(5) == number5); + assert(string.nth(5) == number5); assert(string.len() == 6); string.push(number6); - // assert(string.get(6) == number6); + assert(string.nth(6) == number6); assert(string.len() == 7); string.push(number7); - // assert(string.get(7) == number7); + assert(string.nth(7) == number7); assert(string.len() == 8); string.push(number8); - // assert(string.get(8) == number8); + assert(string.nth(8) == number8); assert(string.len() == 9); string.push(number1); - // assert(string.get(9) == number1); + assert(string.nth(9) == number1); assert(string.len() == 10); string.clear(); @@ -286,15 +287,15 @@ impl StringTest for Contract { assert(string.is_empty()); string.push(number1); - // assert(string.get(0) == number1); + assert(string.nth(0) == number1); assert(string.len() == 1); string.push(number1); - // assert(string.get(1) == number1); + assert(string.nth(1) == number1); assert(string.len() == 2); string.push(number0); - // assert(string.get(2) == number0); + assert(string.nth(2) == number0); assert(string.len() == 3); } From b2089bf23dce34a7e19a0dd728ddc8008a72e839 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 4 Oct 2022 15:29:39 -0400 Subject: [PATCH 21/46] Update to use constants for numbers --- tests/src/test_projects/string/src/main.sw | 192 +++++++++------------ 1 file changed, 78 insertions(+), 114 deletions(-) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index ee2317a4..8f44a6f6 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -9,6 +9,16 @@ use std::{ }; use sway_libs::string::String; +const NUMBER0 = 0u8; +const NUMBER1 = 1u8; +const NUMBER2 = 2u8; +const NUMBER3 = 3u8; +const NUMBER4 = 4u8; +const NUMBER5 = 5u8; +const NUMBER6 = 6u8; +const NUMBER7 = 7u8; +const NUMBER8 = 8u8; + abi StringTest { fn test_as_bytes(); fn test_capacity(); @@ -33,83 +43,63 @@ impl StringTest for Contract { fn test_capacity() { let mut string = ~String::new(); - let number0 = 0u8; - let number1 = 1u8; - let number2 = 2u8; - let number3 = 3u8; - let number4 = 4u8; - let number5 = 5u8; - let number6 = 6u8; - let number7 = 7u8; - let number8 = 8u8; - assert(string.capacity() == 0); - string.push(number0); + string.push(NUMBER0); assert(string.capacity() == 1); - string.push(number1); + string.push(NUMBER1); assert(string.capacity() == 2); - string.push(number2); + string.push(NUMBER2); assert(string.capacity() == 4); - string.push(number3); + string.push(NUMBER3); assert(string.capacity() == 4); - string.push(number4); + string.push(NUMBER4); assert(string.capacity() == 8); - string.push(number5); + string.push(NUMBER5); assert(string.capacity() == 8); - string.push(number6); - string.push(number7); + string.push(NUMBER6); + string.push(NUMBER7); assert(string.capacity() == 8); - string.push(number8); + string.push(NUMBER8); assert(string.capacity() == 16); string.clear(); assert(string.capacity() == 16); - string.push(number0); + string.push(NUMBER0); assert(string.capacity() == 16); } fn test_clear() { let mut string = ~String::new(); - let number0 = 0u8; - let number1 = 1u8; - let number2 = 2u8; - let number3 = 3u8; - let number4 = 4u8; - let number5 = 5u8; - let number6 = 6u8; - let number7 = 7u8; - let number8 = 8u8; - assert(string.is_empty()); string.clear(); assert(string.is_empty()); - string.push(number0); + string.push(NUMBER0); assert(!string.is_empty()); string.clear(); assert(string.is_empty()); - string.push(number0); - string.push(number1); - string.push(number2); - string.push(number3); - string.push(number4); - string.push(number5); - string.push(number6); - string.push(number7); - string.push(number8); + string.push(NUMBER0); + string.push(NUMBER1); + string.push(NUMBER2); + string.push(NUMBER3); + string.push(NUMBER4); + string.push(NUMBER5); + string.push(NUMBER6); + string.push(NUMBER7); + string.push(NUMBER8); assert(!string.is_empty()); string.clear(); @@ -118,7 +108,7 @@ impl StringTest for Contract { string.clear(); assert(string.is_empty()); - string.push(number0); + string.push(NUMBER0); assert(!string.is_empty()); string.clear(); @@ -138,19 +128,19 @@ impl StringTest for Contract { assert(string.is_empty()); - string.push(0u8); + string.push(NUMBER0); assert(!string.is_empty()); - string.push(1u8); + string.push(NUMBER1); assert(!string.is_empty()); string.clear(); assert(string.is_empty()); - string.push(0u8); + string.push(NUMBER0); assert(!string.is_empty()); - string.push(1u8); + string.push(NUMBER1); assert(!string.is_empty()); string.pop(); @@ -165,41 +155,31 @@ impl StringTest for Contract { assert(string.len() == 0); - let number0 = 0u8; - let number1 = 1u8; - let number2 = 2u8; - let number3 = 3u8; - let number4 = 4u8; - let number5 = 5u8; - let number6 = 6u8; - let number7 = 7u8; - let number8 = 8u8; - - string.push(number0); + string.push(NUMBER0); assert(string.len() == 1); - string.push(number1); + string.push(NUMBER1); assert(string.len() == 2); - string.push(number2); + string.push(NUMBER2); assert(string.len() == 3); - string.push(number3); + string.push(NUMBER3); assert(string.len() == 4); - string.push(number4); + string.push(NUMBER4); assert(string.len() == 5); - string.push(number5); + string.push(NUMBER5); assert(string.len() == 6); - string.push(number6); + string.push(NUMBER6); assert(string.len() == 7); - string.push(number7); + string.push(NUMBER7); assert(string.len() == 8); - string.push(number8); + string.push(NUMBER8); assert(string.len() == 9); string.pop(); @@ -232,70 +212,60 @@ impl StringTest for Contract { assert(string.is_empty()); assert(string.capacity() == 0); - let number0 = 0u8; - let number1 = 1u8; - let number2 = 2u8; - let number3 = 3u8; - let number4 = 4u8; - let number5 = 5u8; - let number6 = 6u8; - let number7 = 7u8; - let number8 = 8u8; - - string.push(number0); - assert(string.nth(0) == number0); + string.push(NUMBER0); + assert(string.nth(0).unwrap() == NUMBER0); assert(string.len() == 1); - string.push(number1); - assert(string.nth(1) == number1); + string.push(NUMBER1); + assert(string.nth(1).unwrap() == NUMBER1); assert(string.len() == 2); - string.push(number2); - assert(string.nth(2) == number2); + string.push(NUMBER2); + assert(string.nth(2).unwrap() == NUMBER2); assert(string.len() == 3); - string.push(number3); - assert(string.nth(3) == number3); + string.push(NUMBER3); + assert(string.nth(3).unwrap() == NUMBER3); assert(string.len() == 4); - string.push(number4); - assert(string.nth(4) == number4); + string.push(NUMBER4); + assert(string.nth(4).unwrap() == NUMBER4); assert(string.len() == 5); - string.push(number5); - assert(string.nth(5) == number5); + string.push(NUMBER5); + assert(string.nth(5).unwrap() == NUMBER5); assert(string.len() == 6); - string.push(number6); - assert(string.nth(6) == number6); + string.push(NUMBER6); + assert(string.nth(6).unwrap() == NUMBER6); assert(string.len() == 7); - string.push(number7); - assert(string.nth(7) == number7); + string.push(NUMBER7); + assert(string.nth(7).unwrap() == NUMBER7); assert(string.len() == 8); - string.push(number8); - assert(string.nth(8) == number8); + string.push(NUMBER8); + assert(string.nth(8).unwrap() == NUMBER8); assert(string.len() == 9); - string.push(number1); - assert(string.nth(9) == number1); + string.push(NUMBER1); + assert(string.nth(9).unwrap() == NUMBER1); assert(string.len() == 10); string.clear(); assert(string.len() == 0); assert(string.is_empty()); - string.push(number1); - assert(string.nth(0) == number1); + string.push(NUMBER1); + assert(string.nth(0).unwrap() == NUMBER1); assert(string.len() == 1); - string.push(number1); - assert(string.nth(1) == number1); + string.push(NUMBER1); + assert(string.nth(1).unwrap() == NUMBER1); assert(string.len() == 2); - string.push(number0); - assert(string.nth(2) == number0); + string.push(NUMBER0); + assert(string.nth(2).unwrap() == NUMBER0); assert(string.len() == 3); } @@ -314,20 +284,14 @@ impl StringTest for Contract { iterator += 1; } - let number0 = 0u8; - let number1 = 1u8; - let number2 = 2u8; - let number3 = 3u8; - let number4 = 4u8; - let mut string = ~String::with_capacity(0); assert(string.capacity() == 0); - string.push(number0); + string.push(NUMBER0); assert(string.capacity() == 1); - string.push(number1); + string.push(NUMBER1); assert(string.capacity() == 2); - string.push(number2); + string.push(NUMBER2); assert(string.capacity() == 4); string.clear(); assert(string.capacity() == 4); @@ -335,15 +299,15 @@ impl StringTest for Contract { let mut string = ~String::with_capacity(4); assert(string.capacity() == 4); - string.push(number0); + string.push(NUMBER0); assert(string.capacity() == 4); - string.push(number1); + string.push(NUMBER1); assert(string.capacity() == 4); - string.push(number2); + string.push(NUMBER2); assert(string.capacity() == 4); - string.push(number3); + string.push(NUMBER3); assert(string.capacity() == 4); - string.push(number4); + string.push(NUMBER4); assert(string.capacity() == 8); } } From 83ce059dbfcad57c75e7690c6b57bd084b9200de Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 4 Oct 2022 15:38:22 -0400 Subject: [PATCH 22/46] Add as_bytes test --- tests/src/test_projects/string/src/main.sw | 28 +++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 8f44a6f6..6419ab7c 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -37,7 +37,29 @@ abi StringTest { impl StringTest for Contract { fn test_as_bytes() { + let mut string = ~String::new(); + let bytes: Vec = string.as_bytes(); + assert(bytes.len() == string.len()); + assert(bytes.capacity() == string.capacity()); + + let _result = string.push(NUMBER0); + let bytes: Vec = string.as_bytes(); + assert(bytes.len() == string.len()); + assert(bytes.capacity() == string.capacity()); + assert(bytes.get(0).unwrap() == string.nth(0).unwrap()); + + let _result = string.push(NUMBER1); + let mut bytes: Vec = string.as_bytes(); + assert(bytes.len() == string.len()); + assert(bytes.capacity() == string.capacity()); + assert(bytes.get(1).unwrap() == string.nth(1).unwrap()); + + let _result = string.pop(); + let _result = bytes.pop(); + assert(bytes.len() == string.len()); + assert(bytes.capacity() == string.capacity()); + assert(bytes.get(0).unwrap() == string.nth(0).unwrap()); } fn test_capacity() { @@ -143,10 +165,10 @@ impl StringTest for Contract { string.push(NUMBER1); assert(!string.is_empty()); - string.pop(); + let _result = string.pop(); assert(!string.is_empty()); - string.pop(); + let _result = string.pop(); assert(string.is_empty()); } @@ -182,7 +204,7 @@ impl StringTest for Contract { string.push(NUMBER8); assert(string.len() == 9); - string.pop(); + let _result = string.pop(); assert(string.len() == 8); string.clear(); From 54406dce1ae58976caefdac0aca21ae0c230e10a Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 4 Oct 2022 16:21:16 -0400 Subject: [PATCH 23/46] Add from_utf8 test --- tests/src/test_projects/string/src/main.sw | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 6419ab7c..205b0bd9 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -138,7 +138,21 @@ impl StringTest for Contract { } fn test_from_utf8() { + let mut vec: Vec = ~Vec::new(); + vec.push(NUMBER0); + vec.push(NUMBER1); + vec.push(NUMBER2); + vec.push(NUMBER3); + vec.push(NUMBER4); + + let string_from_uft8 = ~String::from_utf8(vec); + + assert(vec.len() == string_from_uft8.len()); + assert(vec.capacity() == string_from_uft8.capacity()); + assert(vec.get(0).unwrap() == string_from_uft8.nth(0).unwrap()); + assert(vec.get(1).unwrap() == string_from_uft8.nth(1).unwrap()); + assert(vec.get(2).unwrap() == string_from_uft8.nth(2).unwrap()); } fn test_insert() { From ab7f0a1a059d8ed7b2dcf8c4e54f19797c2d837a Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 11:33:25 -0400 Subject: [PATCH 24/46] Add insert test --- tests/src/test_projects/string/src/main.sw | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 205b0bd9..249f8280 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -156,7 +156,26 @@ impl StringTest for Contract { } fn test_insert() { + let mut string = ~String::new(); + + assert(string.len() == 0); + + string.insert(0, NUMBER0); + assert(string.len() == 1); + assert(string.nth(0).unwrap() == NUMBER0); + + string.push(NUMBER1); + string.push(NUMBER2); + + string.insert(0, NUMBER3); + assert(string.len() == 4); + assert(string.nth(0).unwrap() == NUMBER3); + + string.insert(1, NUMBER4); + assert(string.nth(1).unwrap() == NUMBER4); + string.insert(string.len() - 1, NUMBER5); + assert(string.nth(string.len() - 1).unwrap() == NUMBER5); } fn test_is_empty() { From 31dfbeb65b6345dc0fc796951a75217a83d6d2b1 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 11:35:39 -0400 Subject: [PATCH 25/46] Add nth test --- tests/src/test_projects/string/src/main.sw | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 249f8280..a622646e 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -253,7 +253,32 @@ impl StringTest for Contract { } fn test_nth() { + let mut string = ~String::new(); + + string.push(NUMBER0); + assert(string.nth(0).unwrap() == NUMBER0); + + string.push(NUMBER1); + assert(string.nth(0).unwrap() == NUMBER0); + assert(string.nth(1).unwrap() == NUMBER1); + + string.push(NUMBER2); + assert(string.nth(0).unwrap() == NUMBER0); + assert(string.nth(1).unwrap() == NUMBER1); + assert(string.nth(2).unwrap() == NUMBER2); + string.push(NUMBER3); + assert(string.nth(0).unwrap() == NUMBER0); + assert(string.nth(1).unwrap() == NUMBER1); + assert(string.nth(2).unwrap() == NUMBER2); + assert(string.nth(3).unwrap() == NUMBER3); + + string.push(NUMBER4); + assert(string.nth(0).unwrap() == NUMBER0); + assert(string.nth(1).unwrap() == NUMBER1); + assert(string.nth(2).unwrap() == NUMBER2); + assert(string.nth(3).unwrap() == NUMBER3); + assert(string.nth(4).unwrap() == NUMBER4); } fn test_pop() { From bd7f72dda5904d43393a247f3ec452a7d95bd133 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 11:51:14 -0400 Subject: [PATCH 26/46] Add test for pop --- tests/src/test_projects/string/src/main.sw | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index a622646e..d7fa78e9 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -279,10 +279,44 @@ impl StringTest for Contract { assert(string.nth(2).unwrap() == NUMBER2); assert(string.nth(3).unwrap() == NUMBER3); assert(string.nth(4).unwrap() == NUMBER4); + + string.clear(); + string.push(NUMBER5); + string.push(NUMBER6); + assert(string.nth(0).unwrap() == NUMBER5); + assert(string.nth(1).unwrap() == NUMBER6); } fn test_pop() { + let mut string = ~String::new(); + + string.push(NUMBER0); + string.push(NUMBER1); + string.push(NUMBER2); + string.push(NUMBER3); + string.push(NUMBER4); + + assert(string.len() == 5); + assert(string.pop().unwrap() == NUMBER4); + assert(string.len() == 4); + + assert(string.pop().unwrap() == NUMBER3); + assert(string.len() == 3); + + assert(string.pop().unwrap() == NUMBER2); + assert(string.len() == 2); + + assert(string.pop().unwrap() == NUMBER1); + assert(string.len() == 1); + + assert(string.pop().unwrap() == NUMBER0); + assert(string.len() == 0); + + assert(string.pop().is_none()); + + string.push(NUMBER5); + assert(string.pop().unwrap() == NUMBER5); } fn test_push() { From 11ea781ba526997445f319af75ada1b78c6b5c4e Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 11:55:07 -0400 Subject: [PATCH 27/46] Add remove test --- tests/src/test_projects/string/src/main.sw | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index d7fa78e9..4f8b7679 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -384,7 +384,38 @@ impl StringTest for Contract { } fn test_remove() { + let mut string = ~String::new(); + + string.push(NUMBER0); + string.push(NUMBER1); + string.push(NUMBER2); + string.push(NUMBER3); + string.push(NUMBER4); + string.push(NUMBER5); + + assert(string.len() == 6); + + assert(string.remove(0) == NUMBER0); + assert(string.len() == 5); + assert(string.remove(0) == NUMBER1); + assert(string.len() == 4); + + assert(string.remove(1) == NUMBER3); + assert(string.len() == 3); + + assert(string.remove(string.len() - 1) == NUMBER5); + assert(string.len() == 2); + + assert(string.remove(1) == NUMBER4); + assert(string.len() == 1); + + assert(string.remove(0) == NUMBER2); + assert(string.len() == 0); + + string.push(NUMBER6); + assert(string.remove(0) == NUMBER6); + assert(string.len() == 0); } fn test_with_capacity() { From 73c397b5e27a5f345b1df34bbb936405c4edcf44 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 12:24:35 -0400 Subject: [PATCH 28/46] Update rust SDK for tests --- tests/src/test_projects/string/Forc.lock | 4 +- tests/src/test_projects/string/mod.rs | 123 ++++++++++++++++----- tests/src/test_projects/string/src/main.sw | 2 +- 3 files changed, 99 insertions(+), 30 deletions(-) diff --git a/tests/src/test_projects/string/Forc.lock b/tests/src/test_projects/string/Forc.lock index 7a7c6cbd..111cc5d2 100644 --- a/tests/src/test_projects/string/Forc.lock +++ b/tests/src/test_projects/string/Forc.lock @@ -1,11 +1,11 @@ [[package]] name = 'core' -source = 'path+from-root-603BAB4590C454D3' +source = 'path+from-root-3851086A5CA5C88E' dependencies = [] [[package]] name = 'std' -source = 'git+https://github.com/fuellabs/sway?tag=v0.19.0#5c716e1ba55d755555ed5aa186c883f73c4f90dc' +source = 'git+https://github.com/fuellabs/sway?tag=v0.24.1#54e1a340e6c0c9a675e8a66aab03f579d5d6b39b' dependencies = ['core'] [[package]] diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index 9463f51b..30c09a53 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -1,7 +1,6 @@ -use fuels::{prelude::*}; -use std::io::stdout; -use std::io::Write; +use fuels::{prelude::*, tx::ContractId}; +// Load abi from json abigen!( TestStringLib, "test_projects/string/out/debug/string-abi.json" @@ -9,7 +8,16 @@ abigen!( async fn test_string_instance() -> TestStringLib { // Launch a local network and deploy the contract - let wallet = launch_provider_and_get_wallet().await; + let mut wallets = launch_custom_provider_and_get_wallets( + WalletsConfig::new( + Some(1), /* Single wallet */ + Some(1), /* Single coin (UTXO) */ + Some(1_000_000_000), /* Amount per coin */ + ), + None, + ) + .await; + let wallet = wallets.pop().unwrap(); let id = Contract::deploy( "test_projects/string/out/debug/string.bin", @@ -25,29 +33,22 @@ async fn test_string_instance() -> TestStringLib { instance } -// mod as_bytes { - -// use super::*; - -// mod success { - -// use super::*; - -// #[tokio::test] -// async fn gets_bytes() { -// let _instance = test_string_instance().await; +mod as_bytes { -// } - -// } + use super::*; -// mod reverts { + mod success { -// use super::*; + use super::*; -// } + #[tokio::test] + async fn returns_bytes() { + let instance = test_string_instance().await; -// } + let _result = instance.test_as_bytes().call().await.unwrap(); + } + } +} mod capacity { @@ -83,6 +84,40 @@ mod clear { } } +mod from_utf8 { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn converts_to_string() { + let instance = test_string_instance().await; + + let _result = instance.test_from_utf8().call().await.unwrap(); + } + } +} + +mod insert { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn inserts_into_string() { + let instance = test_string_instance().await; + + let _result = instance.test_insert().call().await.unwrap(); + } + } +} + mod is_empty { use super::*; @@ -109,7 +144,7 @@ mod len { use super::*; #[tokio::test] - async fn gets_length() { + async fn returns_string_length() { let instance = test_string_instance().await; let _result = instance.test_len().call().await.unwrap(); @@ -126,7 +161,7 @@ mod new { use super::*; #[tokio::test] - async fn creates_string() { + async fn creates_empty_string() { let instance = test_string_instance().await; let _result = instance.test_new().call().await.unwrap(); @@ -134,6 +169,40 @@ mod new { } } +mod nth { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn returns_nth_element_in_string() { + let instance = test_string_instance().await; + + let _result = instance.test_nth().call().await.unwrap(); + } + } +} + +mod pop { + + use super::*; + + mod success { + + use super::*; + + #[tokio::test] + async fn pops_last_element_in_string() { + let instance = test_string_instance().await; + + let _result = instance.test_pop().call().await.unwrap(); + } + } +} + mod push { use super::*; @@ -151,7 +220,7 @@ mod push { } } -mod push_str { +mod remove { use super::*; @@ -160,10 +229,10 @@ mod push_str { use super::*; #[tokio::test] - async fn pushes_string() { + async fn removes_element_in_string() { let instance = test_string_instance().await; - let _result = instance.test_push_str().call().await.unwrap(); + let _result = instance.test_remove().call().await.unwrap(); } } } diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 4f8b7679..3615b91a 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -175,7 +175,7 @@ impl StringTest for Contract { assert(string.nth(1).unwrap() == NUMBER4); string.insert(string.len() - 1, NUMBER5); - assert(string.nth(string.len() - 1).unwrap() == NUMBER5); + assert(string.nth(string.len() - 2).unwrap() == NUMBER5); } fn test_is_empty() { From 3c9df9ff820981822dfc515569c439567ba8d6ce Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 12:25:59 -0400 Subject: [PATCH 29/46] Run formatter --- sway_libs/src/string.sw | 2 +- tests/src/test_projects/string/Forc.toml | 16 +- tests/src/test_projects/string/src/main.sw | 794 +++++++++------------ 3 files changed, 345 insertions(+), 467 deletions(-) diff --git a/sway_libs/src/string.sw b/sway_libs/src/string.sw index e4a643d9..30fe260a 100644 --- a/sway_libs/src/string.sw +++ b/sway_libs/src/string.sw @@ -72,7 +72,7 @@ impl String { pub fn remove(self, index: u64) -> u8 { self.bytes.remove(index) } - + /// Constructs a new, empty `String` with the specified capacity. pub fn with_capacity(capacity: u64) -> Self { Self { diff --git a/tests/src/test_projects/string/Forc.toml b/tests/src/test_projects/string/Forc.toml index 874ec67f..dd15a4ea 100644 --- a/tests/src/test_projects/string/Forc.toml +++ b/tests/src/test_projects/string/Forc.toml @@ -1,8 +1,8 @@ -[project] -authors = ["Fuel Labs "] -entry = "main.sw" -license = "Apache-2.0" -name = "string" - -[dependencies] -sway_libs = { path = "../../../../sway_libs"} +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "string" + +[dependencies] +sway_libs = { path = "../../../../sway_libs" } diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 3615b91a..40480724 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -1,458 +1,336 @@ -contract; - -use std::{ - assert::assert, - intrinsics::size_of, - mem::{addr_of, read}, - option::Option, - vec::Vec, -}; -use sway_libs::string::String; - -const NUMBER0 = 0u8; -const NUMBER1 = 1u8; -const NUMBER2 = 2u8; -const NUMBER3 = 3u8; -const NUMBER4 = 4u8; -const NUMBER5 = 5u8; -const NUMBER6 = 6u8; -const NUMBER7 = 7u8; -const NUMBER8 = 8u8; - -abi StringTest { - fn test_as_bytes(); - fn test_capacity(); - fn test_clear(); - fn test_from_utf8(); - fn test_insert(); - fn test_is_empty(); - fn test_len(); - fn test_new(); - fn test_nth(); - fn test_pop(); - fn test_push(); - fn test_remove(); - fn test_with_capacity(); -} - -impl StringTest for Contract { - fn test_as_bytes() { - let mut string = ~String::new(); - - let bytes: Vec = string.as_bytes(); - assert(bytes.len() == string.len()); - assert(bytes.capacity() == string.capacity()); - - let _result = string.push(NUMBER0); - let bytes: Vec = string.as_bytes(); - assert(bytes.len() == string.len()); - assert(bytes.capacity() == string.capacity()); - assert(bytes.get(0).unwrap() == string.nth(0).unwrap()); - - let _result = string.push(NUMBER1); - let mut bytes: Vec = string.as_bytes(); - assert(bytes.len() == string.len()); - assert(bytes.capacity() == string.capacity()); - assert(bytes.get(1).unwrap() == string.nth(1).unwrap()); - - let _result = string.pop(); - let _result = bytes.pop(); - assert(bytes.len() == string.len()); - assert(bytes.capacity() == string.capacity()); - assert(bytes.get(0).unwrap() == string.nth(0).unwrap()); - } - - fn test_capacity() { - let mut string = ~String::new(); - - assert(string.capacity() == 0); - - string.push(NUMBER0); - assert(string.capacity() == 1); - - string.push(NUMBER1); - assert(string.capacity() == 2); - - string.push(NUMBER2); - assert(string.capacity() == 4); - - string.push(NUMBER3); - assert(string.capacity() == 4); - - string.push(NUMBER4); - assert(string.capacity() == 8); - - string.push(NUMBER5); - assert(string.capacity() == 8); - - string.push(NUMBER6); - string.push(NUMBER7); - assert(string.capacity() == 8); - - string.push(NUMBER8); - assert(string.capacity() == 16); - - string.clear(); - assert(string.capacity() == 16); - - string.push(NUMBER0); - assert(string.capacity() == 16); - } - - fn test_clear() { - let mut string = ~String::new(); - - assert(string.is_empty()); - - string.clear(); - assert(string.is_empty()); - - string.push(NUMBER0); - assert(!string.is_empty()); - - string.clear(); - assert(string.is_empty()); - - string.push(NUMBER0); - string.push(NUMBER1); - string.push(NUMBER2); - string.push(NUMBER3); - string.push(NUMBER4); - string.push(NUMBER5); - string.push(NUMBER6); - string.push(NUMBER7); - string.push(NUMBER8); - assert(!string.is_empty()); - - string.clear(); - assert(string.is_empty()); - - string.clear(); - assert(string.is_empty()); - - string.push(NUMBER0); - assert(!string.is_empty()); - - string.clear(); - assert(string.is_empty()); - } - - fn test_from_utf8() { - let mut vec: Vec = ~Vec::new(); - - vec.push(NUMBER0); - vec.push(NUMBER1); - vec.push(NUMBER2); - vec.push(NUMBER3); - vec.push(NUMBER4); - - let string_from_uft8 = ~String::from_utf8(vec); - - assert(vec.len() == string_from_uft8.len()); - assert(vec.capacity() == string_from_uft8.capacity()); - assert(vec.get(0).unwrap() == string_from_uft8.nth(0).unwrap()); - assert(vec.get(1).unwrap() == string_from_uft8.nth(1).unwrap()); - assert(vec.get(2).unwrap() == string_from_uft8.nth(2).unwrap()); - } - - fn test_insert() { - let mut string = ~String::new(); - - assert(string.len() == 0); - - string.insert(0, NUMBER0); - assert(string.len() == 1); - assert(string.nth(0).unwrap() == NUMBER0); - - string.push(NUMBER1); - string.push(NUMBER2); - - string.insert(0, NUMBER3); - assert(string.len() == 4); - assert(string.nth(0).unwrap() == NUMBER3); - - string.insert(1, NUMBER4); - assert(string.nth(1).unwrap() == NUMBER4); - - string.insert(string.len() - 1, NUMBER5); - assert(string.nth(string.len() - 2).unwrap() == NUMBER5); - } - - fn test_is_empty() { - let mut string = ~String::new(); - - assert(string.is_empty()); - - string.push(NUMBER0); - assert(!string.is_empty()); - - string.push(NUMBER1); - assert(!string.is_empty()); - - string.clear(); - assert(string.is_empty()); - - string.push(NUMBER0); - assert(!string.is_empty()); - - string.push(NUMBER1); - assert(!string.is_empty()); - - let _result = string.pop(); - assert(!string.is_empty()); - - let _result = string.pop(); - assert(string.is_empty()); - } - - fn test_len() { - let mut string = ~String::new(); - - assert(string.len() == 0); - - string.push(NUMBER0); - assert(string.len() == 1); - - string.push(NUMBER1); - assert(string.len() == 2); - - string.push(NUMBER2); - assert(string.len() == 3); - - string.push(NUMBER3); - assert(string.len() == 4); - - string.push(NUMBER4); - assert(string.len() == 5); - - string.push(NUMBER5); - assert(string.len() == 6); - - string.push(NUMBER6); - assert(string.len() == 7); - - string.push(NUMBER7); - assert(string.len() == 8); - - string.push(NUMBER8); - assert(string.len() == 9); - - let _result = string.pop(); - assert(string.len() == 8); - - string.clear(); - assert(string.len() == 0); - } - - fn test_new() { - let mut string = ~String::new(); - - assert(string.len() == 0); - assert(string.is_empty()); - assert(string.capacity() == 0); - } - - fn test_nth() { - let mut string = ~String::new(); - - string.push(NUMBER0); - assert(string.nth(0).unwrap() == NUMBER0); - - string.push(NUMBER1); - assert(string.nth(0).unwrap() == NUMBER0); - assert(string.nth(1).unwrap() == NUMBER1); - - string.push(NUMBER2); - assert(string.nth(0).unwrap() == NUMBER0); - assert(string.nth(1).unwrap() == NUMBER1); - assert(string.nth(2).unwrap() == NUMBER2); - - string.push(NUMBER3); - assert(string.nth(0).unwrap() == NUMBER0); - assert(string.nth(1).unwrap() == NUMBER1); - assert(string.nth(2).unwrap() == NUMBER2); - assert(string.nth(3).unwrap() == NUMBER3); - - string.push(NUMBER4); - assert(string.nth(0).unwrap() == NUMBER0); - assert(string.nth(1).unwrap() == NUMBER1); - assert(string.nth(2).unwrap() == NUMBER2); - assert(string.nth(3).unwrap() == NUMBER3); - assert(string.nth(4).unwrap() == NUMBER4); - - string.clear(); - string.push(NUMBER5); - string.push(NUMBER6); - assert(string.nth(0).unwrap() == NUMBER5); - assert(string.nth(1).unwrap() == NUMBER6); - } - - fn test_pop() { - let mut string = ~String::new(); - - string.push(NUMBER0); - string.push(NUMBER1); - string.push(NUMBER2); - string.push(NUMBER3); - string.push(NUMBER4); - - assert(string.len() == 5); - - assert(string.pop().unwrap() == NUMBER4); - assert(string.len() == 4); - - assert(string.pop().unwrap() == NUMBER3); - assert(string.len() == 3); - - assert(string.pop().unwrap() == NUMBER2); - assert(string.len() == 2); - - assert(string.pop().unwrap() == NUMBER1); - assert(string.len() == 1); - - assert(string.pop().unwrap() == NUMBER0); - assert(string.len() == 0); - - assert(string.pop().is_none()); - - string.push(NUMBER5); - assert(string.pop().unwrap() == NUMBER5); - } - - fn test_push() { - let mut string = ~String::new(); - - assert(string.len() == 0); - assert(string.is_empty()); - assert(string.capacity() == 0); - - string.push(NUMBER0); - assert(string.nth(0).unwrap() == NUMBER0); - assert(string.len() == 1); - - string.push(NUMBER1); - assert(string.nth(1).unwrap() == NUMBER1); - assert(string.len() == 2); - - string.push(NUMBER2); - assert(string.nth(2).unwrap() == NUMBER2); - assert(string.len() == 3); - - string.push(NUMBER3); - assert(string.nth(3).unwrap() == NUMBER3); - assert(string.len() == 4); - - string.push(NUMBER4); - assert(string.nth(4).unwrap() == NUMBER4); - assert(string.len() == 5); - - string.push(NUMBER5); - assert(string.nth(5).unwrap() == NUMBER5); - assert(string.len() == 6); - - string.push(NUMBER6); - assert(string.nth(6).unwrap() == NUMBER6); - assert(string.len() == 7); - - string.push(NUMBER7); - assert(string.nth(7).unwrap() == NUMBER7); - assert(string.len() == 8); - - string.push(NUMBER8); - assert(string.nth(8).unwrap() == NUMBER8); - assert(string.len() == 9); - - string.push(NUMBER1); - assert(string.nth(9).unwrap() == NUMBER1); - assert(string.len() == 10); - - string.clear(); - assert(string.len() == 0); - assert(string.is_empty()); - - string.push(NUMBER1); - assert(string.nth(0).unwrap() == NUMBER1); - assert(string.len() == 1); - - string.push(NUMBER1); - assert(string.nth(1).unwrap() == NUMBER1); - assert(string.len() == 2); - - string.push(NUMBER0); - assert(string.nth(2).unwrap() == NUMBER0); - assert(string.len() == 3); - } - - fn test_remove() { - let mut string = ~String::new(); - - string.push(NUMBER0); - string.push(NUMBER1); - string.push(NUMBER2); - string.push(NUMBER3); - string.push(NUMBER4); - string.push(NUMBER5); - - assert(string.len() == 6); - - assert(string.remove(0) == NUMBER0); - assert(string.len() == 5); - - assert(string.remove(0) == NUMBER1); - assert(string.len() == 4); - - assert(string.remove(1) == NUMBER3); - assert(string.len() == 3); - - assert(string.remove(string.len() - 1) == NUMBER5); - assert(string.len() == 2); - - assert(string.remove(1) == NUMBER4); - assert(string.len() == 1); - - assert(string.remove(0) == NUMBER2); - assert(string.len() == 0); - - string.push(NUMBER6); - assert(string.remove(0) == NUMBER6); - assert(string.len() == 0); - } - - fn 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.push(NUMBER0); - assert(string.capacity() == 1); - string.push(NUMBER1); - assert(string.capacity() == 2); - string.push(NUMBER2); - assert(string.capacity() == 4); - string.clear(); - assert(string.capacity() == 4); - - let mut string = ~String::with_capacity(4); - assert(string.capacity() == 4); - - string.push(NUMBER0); - assert(string.capacity() == 4); - string.push(NUMBER1); - assert(string.capacity() == 4); - string.push(NUMBER2); - assert(string.capacity() == 4); - string.push(NUMBER3); - assert(string.capacity() == 4); - string.push(NUMBER4); - assert(string.capacity() == 8); - } -} +contract; +use std::{assert::assert, intrinsics::size_of, mem::{addr_of, read}, option::Option, vec::Vec}; +use sway_libs::string::String; +const NUMBER0 = 0u8; +const NUMBER1 = 1u8; +const NUMBER2 = 2u8; +const NUMBER3 = 3u8; +const NUMBER4 = 4u8; +const NUMBER5 = 5u8; +const NUMBER6 = 6u8; +const NUMBER7 = 7u8; +const NUMBER8 = 8u8; +abi StringTest { + fn test_as_bytes(); + fn test_capacity(); + fn test_clear(); + fn test_from_utf8(); + fn test_insert(); + fn test_is_empty(); + fn test_len(); + fn test_new(); + fn test_nth(); + fn test_pop(); + fn test_push(); + fn test_remove(); + fn test_with_capacity(); +} +impl StringTest for Contract { + fn test_as_bytes() { + let mut string = ~String::new(); + let bytes: Vec = string.as_bytes(); + assert(bytes.len() == string.len()); + assert(bytes.capacity() == string.capacity()); + let _result = string.push(NUMBER0); + let bytes: Vec = string.as_bytes(); + assert(bytes.len() == string.len()); + assert(bytes.capacity() == string.capacity()); + assert(bytes.get(0).unwrap() == string.nth(0).unwrap()); + let _result = string.push(NUMBER1); + let mut bytes: Vec = string.as_bytes(); + assert(bytes.len() == string.len()); + assert(bytes.capacity() == string.capacity()); + assert(bytes.get(1).unwrap() == string.nth(1).unwrap()); + let _result = string.pop(); + let _result = bytes.pop(); + assert(bytes.len() == string.len()); + assert(bytes.capacity() == string.capacity()); + assert(bytes.get(0).unwrap() == string.nth(0).unwrap()); + } + fn test_capacity() { + let mut string = ~String::new(); + assert(string.capacity() == 0); + string.push(NUMBER0); + assert(string.capacity() == 1); + string.push(NUMBER1); + assert(string.capacity() == 2); + string.push(NUMBER2); + assert(string.capacity() == 4); + string.push(NUMBER3); + assert(string.capacity() == 4); + string.push(NUMBER4); + assert(string.capacity() == 8); + string.push(NUMBER5); + assert(string.capacity() == 8); + string.push(NUMBER6); + string.push(NUMBER7); + assert(string.capacity() == 8); + string.push(NUMBER8); + assert(string.capacity() == 16); + string.clear(); + assert(string.capacity() == 16); + string.push(NUMBER0); + assert(string.capacity() == 16); + } + fn test_clear() { + let mut string = ~String::new(); + assert(string.is_empty()); + string.clear(); + assert(string.is_empty()); + string.push(NUMBER0); + assert(!string.is_empty()); + string.clear(); + assert(string.is_empty()); + string.push(NUMBER0); + string.push(NUMBER1); + string.push(NUMBER2); + string.push(NUMBER3); + string.push(NUMBER4); + string.push(NUMBER5); + string.push(NUMBER6); + string.push(NUMBER7); + string.push(NUMBER8); + assert(!string.is_empty()); + string.clear(); + assert(string.is_empty()); + string.clear(); + assert(string.is_empty()); + string.push(NUMBER0); + assert(!string.is_empty()); + string.clear(); + assert(string.is_empty()); + } + fn test_from_utf8() { + let mut vec: Vec = ~Vec::new(); + vec.push(NUMBER0); + vec.push(NUMBER1); + vec.push(NUMBER2); + vec.push(NUMBER3); + vec.push(NUMBER4); + let string_from_uft8 = ~String::from_utf8(vec); + assert(vec.len() == string_from_uft8.len()); + assert(vec.capacity() == string_from_uft8.capacity()); + assert(vec.get(0).unwrap() == string_from_uft8.nth(0).unwrap()); + assert(vec.get(1).unwrap() == string_from_uft8.nth(1).unwrap()); + assert(vec.get(2).unwrap() == string_from_uft8.nth(2).unwrap()); + } + fn test_insert() { + let mut string = ~String::new(); + assert(string.len() == 0); + string.insert(0, NUMBER0); + assert(string.len() == 1); + assert(string.nth(0).unwrap() == NUMBER0); + string.push(NUMBER1); + string.push(NUMBER2); + string.insert(0, NUMBER3); + assert(string.len() == 4); + assert(string.nth(0).unwrap() == NUMBER3); + string.insert(1, NUMBER4); + assert(string.nth(1).unwrap() == NUMBER4); + string.insert(string.len() - 1, NUMBER5); + assert(string.nth(string.len() - 2).unwrap() == NUMBER5); + } + fn test_is_empty() { + let mut string = ~String::new(); + assert(string.is_empty()); + string.push(NUMBER0); + assert(!string.is_empty()); + string.push(NUMBER1); + assert(!string.is_empty()); + string.clear(); + assert(string.is_empty()); + string.push(NUMBER0); + assert(!string.is_empty()); + string.push(NUMBER1); + assert(!string.is_empty()); + let _result = string.pop(); + assert(!string.is_empty()); + let _result = string.pop(); + assert(string.is_empty()); + } + fn test_len() { + let mut string = ~String::new(); + assert(string.len() == 0); + string.push(NUMBER0); + assert(string.len() == 1); + string.push(NUMBER1); + assert(string.len() == 2); + string.push(NUMBER2); + assert(string.len() == 3); + string.push(NUMBER3); + assert(string.len() == 4); + string.push(NUMBER4); + assert(string.len() == 5); + string.push(NUMBER5); + assert(string.len() == 6); + string.push(NUMBER6); + assert(string.len() == 7); + string.push(NUMBER7); + assert(string.len() == 8); + string.push(NUMBER8); + assert(string.len() == 9); + let _result = string.pop(); + assert(string.len() == 8); + string.clear(); + assert(string.len() == 0); + } + fn test_new() { + let mut string = ~String::new(); + assert(string.len() == 0); + assert(string.is_empty()); + assert(string.capacity() == 0); + } + fn test_nth() { + let mut string = ~String::new(); + string.push(NUMBER0); + assert(string.nth(0).unwrap() == NUMBER0); + string.push(NUMBER1); + assert(string.nth(0).unwrap() == NUMBER0); + assert(string.nth(1).unwrap() == NUMBER1); + string.push(NUMBER2); + assert(string.nth(0).unwrap() == NUMBER0); + assert(string.nth(1).unwrap() == NUMBER1); + assert(string.nth(2).unwrap() == NUMBER2); + string.push(NUMBER3); + assert(string.nth(0).unwrap() == NUMBER0); + assert(string.nth(1).unwrap() == NUMBER1); + assert(string.nth(2).unwrap() == NUMBER2); + assert(string.nth(3).unwrap() == NUMBER3); + string.push(NUMBER4); + assert(string.nth(0).unwrap() == NUMBER0); + assert(string.nth(1).unwrap() == NUMBER1); + assert(string.nth(2).unwrap() == NUMBER2); + assert(string.nth(3).unwrap() == NUMBER3); + assert(string.nth(4).unwrap() == NUMBER4); + string.clear(); + string.push(NUMBER5); + string.push(NUMBER6); + assert(string.nth(0).unwrap() == NUMBER5); + assert(string.nth(1).unwrap() == NUMBER6); + } + fn test_pop() { + let mut string = ~String::new(); + string.push(NUMBER0); + string.push(NUMBER1); + string.push(NUMBER2); + string.push(NUMBER3); + string.push(NUMBER4); + assert(string.len() == 5); + assert(string.pop().unwrap() == NUMBER4); + assert(string.len() == 4); + assert(string.pop().unwrap() == NUMBER3); + assert(string.len() == 3); + assert(string.pop().unwrap() == NUMBER2); + assert(string.len() == 2); + assert(string.pop().unwrap() == NUMBER1); + assert(string.len() == 1); + assert(string.pop().unwrap() == NUMBER0); + assert(string.len() == 0); + assert(string.pop().is_none()); + string.push(NUMBER5); + assert(string.pop().unwrap() == NUMBER5); + } + fn test_push() { + let mut string = ~String::new(); + assert(string.len() == 0); + assert(string.is_empty()); + assert(string.capacity() == 0); + string.push(NUMBER0); + assert(string.nth(0).unwrap() == NUMBER0); + assert(string.len() == 1); + string.push(NUMBER1); + assert(string.nth(1).unwrap() == NUMBER1); + assert(string.len() == 2); + string.push(NUMBER2); + assert(string.nth(2).unwrap() == NUMBER2); + assert(string.len() == 3); + string.push(NUMBER3); + assert(string.nth(3).unwrap() == NUMBER3); + assert(string.len() == 4); + string.push(NUMBER4); + assert(string.nth(4).unwrap() == NUMBER4); + assert(string.len() == 5); + string.push(NUMBER5); + assert(string.nth(5).unwrap() == NUMBER5); + assert(string.len() == 6); + string.push(NUMBER6); + assert(string.nth(6).unwrap() == NUMBER6); + assert(string.len() == 7); + string.push(NUMBER7); + assert(string.nth(7).unwrap() == NUMBER7); + assert(string.len() == 8); + string.push(NUMBER8); + assert(string.nth(8).unwrap() == NUMBER8); + assert(string.len() == 9); + string.push(NUMBER1); + assert(string.nth(9).unwrap() == NUMBER1); + assert(string.len() == 10); + string.clear(); + assert(string.len() == 0); + assert(string.is_empty()); + string.push(NUMBER1); + assert(string.nth(0).unwrap() == NUMBER1); + assert(string.len() == 1); + string.push(NUMBER1); + assert(string.nth(1).unwrap() == NUMBER1); + assert(string.len() == 2); + string.push(NUMBER0); + assert(string.nth(2).unwrap() == NUMBER0); + assert(string.len() == 3); + } + fn test_remove() { + let mut string = ~String::new(); + string.push(NUMBER0); + string.push(NUMBER1); + string.push(NUMBER2); + string.push(NUMBER3); + string.push(NUMBER4); + string.push(NUMBER5); + assert(string.len() == 6); + assert(string.remove(0) == NUMBER0); + assert(string.len() == 5); + assert(string.remove(0) == NUMBER1); + assert(string.len() == 4); + assert(string.remove(1) == NUMBER3); + assert(string.len() == 3); + assert(string.remove(string.len() - 1) == NUMBER5); + assert(string.len() == 2); + assert(string.remove(1) == NUMBER4); + assert(string.len() == 1); + assert(string.remove(0) == NUMBER2); + assert(string.len() == 0); + string.push(NUMBER6); + assert(string.remove(0) == NUMBER6); + assert(string.len() == 0); + } + fn 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.push(NUMBER0); + assert(string.capacity() == 1); + string.push(NUMBER1); + assert(string.capacity() == 2); + string.push(NUMBER2); + assert(string.capacity() == 4); + string.clear(); + assert(string.capacity() == 4); + let mut string = ~String::with_capacity(4); + assert(string.capacity() == 4); + string.push(NUMBER0); + assert(string.capacity() == 4); + string.push(NUMBER1); + assert(string.capacity() == 4); + string.push(NUMBER2); + assert(string.capacity() == 4); + string.push(NUMBER3); + assert(string.capacity() == 4); + string.push(NUMBER4); + assert(string.capacity() == 8); + } +} From 2db5115b9aa9d1480a8b8b8bbdf8a05a77b4e274 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 15:23:31 -0400 Subject: [PATCH 30/46] Fix bizarre formatting --- tests/src/test_projects/string/src/main.sw | 141 +++++++++++++++++++-- 1 file changed, 132 insertions(+), 9 deletions(-) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 40480724..875e4c86 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -1,6 +1,14 @@ contract; -use std::{assert::assert, intrinsics::size_of, mem::{addr_of, read}, option::Option, vec::Vec}; + +use std::{ + assert::assert, + intrinsics::size_of, + mem::{addr_of, read}, + option::Option, + vec::Vec +}; use sway_libs::string::String; + const NUMBER0 = 0u8; const NUMBER1 = 1u8; const NUMBER2 = 2u8; @@ -10,6 +18,7 @@ const NUMBER5 = 5u8; const NUMBER6 = 6u8; const NUMBER7 = 7u8; const NUMBER8 = 8u8; + abi StringTest { fn test_as_bytes(); fn test_capacity(); @@ -25,39 +34,51 @@ abi StringTest { fn test_remove(); fn test_with_capacity(); } + impl StringTest for Contract { fn test_as_bytes() { let mut string = ~String::new(); + let bytes: Vec = string.as_bytes(); assert(bytes.len() == string.len()); assert(bytes.capacity() == string.capacity()); - let _result = string.push(NUMBER0); + + string.push(NUMBER0); let bytes: Vec = string.as_bytes(); assert(bytes.len() == string.len()); assert(bytes.capacity() == string.capacity()); assert(bytes.get(0).unwrap() == string.nth(0).unwrap()); - let _result = string.push(NUMBER1); + + string.push(NUMBER1); let mut bytes: Vec = string.as_bytes(); assert(bytes.len() == string.len()); assert(bytes.capacity() == string.capacity()); assert(bytes.get(1).unwrap() == string.nth(1).unwrap()); - let _result = string.pop(); - let _result = bytes.pop(); + + let result_string = string.pop().unwrap(); + let result_bytes = bytes.pop().unwrap(); + assert(result_bytes == result_string); assert(bytes.len() == string.len()); assert(bytes.capacity() == string.capacity()); assert(bytes.get(0).unwrap() == string.nth(0).unwrap()); } + fn test_capacity() { let mut string = ~String::new(); + assert(string.capacity() == 0); + string.push(NUMBER0); assert(string.capacity() == 1); + string.push(NUMBER1); assert(string.capacity() == 2); + string.push(NUMBER2); assert(string.capacity() == 4); string.push(NUMBER3); assert(string.capacity() == 4); + string.push(NUMBER4); assert(string.capacity() == 8); string.push(NUMBER5); @@ -65,22 +86,31 @@ impl StringTest for Contract { string.push(NUMBER6); string.push(NUMBER7); assert(string.capacity() == 8); + string.push(NUMBER8); assert(string.capacity() == 16); + string.clear(); assert(string.capacity() == 16); + string.push(NUMBER0); assert(string.capacity() == 16); } + fn test_clear() { let mut string = ~String::new(); + assert(string.is_empty()); + string.clear(); assert(string.is_empty()); + string.push(NUMBER0); assert(!string.is_empty()); + string.clear(); assert(string.is_empty()); + string.push(NUMBER0); string.push(NUMBER1); string.push(NUMBER2); @@ -91,22 +121,29 @@ impl StringTest for Contract { string.push(NUMBER7); string.push(NUMBER8); assert(!string.is_empty()); + string.clear(); assert(string.is_empty()); + string.clear(); assert(string.is_empty()); + string.push(NUMBER0); assert(!string.is_empty()); + string.clear(); assert(string.is_empty()); } + fn test_from_utf8() { let mut vec: Vec = ~Vec::new(); + vec.push(NUMBER0); vec.push(NUMBER1); vec.push(NUMBER2); vec.push(NUMBER3); vec.push(NUMBER4); + let string_from_uft8 = ~String::from_utf8(vec); assert(vec.len() == string_from_uft8.len()); assert(vec.capacity() == string_from_uft8.capacity()); @@ -114,222 +151,308 @@ impl StringTest for Contract { assert(vec.get(1).unwrap() == string_from_uft8.nth(1).unwrap()); assert(vec.get(2).unwrap() == string_from_uft8.nth(2).unwrap()); } + fn test_insert() { let mut string = ~String::new(); + assert(string.len() == 0); - string.insert(0, NUMBER0); + + string.insert(NUMBER0, 0); assert(string.len() == 1); assert(string.nth(0).unwrap() == NUMBER0); + string.push(NUMBER1); string.push(NUMBER2); - string.insert(0, NUMBER3); + string.insert(NUMBER3, 0); assert(string.len() == 4); assert(string.nth(0).unwrap() == NUMBER3); - string.insert(1, NUMBER4); + + string.insert(NUMBER4, 1); assert(string.nth(1).unwrap() == NUMBER4); - string.insert(string.len() - 1, NUMBER5); + + string.insert(NUMBER5, string.len() - 1); assert(string.nth(string.len() - 2).unwrap() == NUMBER5); } + fn test_is_empty() { let mut string = ~String::new(); + assert(string.is_empty()); + string.push(NUMBER0); assert(!string.is_empty()); + string.push(NUMBER1); assert(!string.is_empty()); + string.clear(); assert(string.is_empty()); + string.push(NUMBER0); assert(!string.is_empty()); + string.push(NUMBER1); assert(!string.is_empty()); + let _result = string.pop(); assert(!string.is_empty()); + let _result = string.pop(); assert(string.is_empty()); } + fn test_len() { let mut string = ~String::new(); + assert(string.len() == 0); + string.push(NUMBER0); assert(string.len() == 1); + string.push(NUMBER1); assert(string.len() == 2); + string.push(NUMBER2); assert(string.len() == 3); + string.push(NUMBER3); assert(string.len() == 4); + string.push(NUMBER4); assert(string.len() == 5); + string.push(NUMBER5); assert(string.len() == 6); + string.push(NUMBER6); assert(string.len() == 7); + string.push(NUMBER7); assert(string.len() == 8); + string.push(NUMBER8); assert(string.len() == 9); + let _result = string.pop(); assert(string.len() == 8); + string.clear(); assert(string.len() == 0); } + fn test_new() { let mut string = ~String::new(); + assert(string.len() == 0); assert(string.is_empty()); assert(string.capacity() == 0); } + fn test_nth() { let mut string = ~String::new(); + string.push(NUMBER0); assert(string.nth(0).unwrap() == NUMBER0); + string.push(NUMBER1); assert(string.nth(0).unwrap() == NUMBER0); assert(string.nth(1).unwrap() == NUMBER1); + string.push(NUMBER2); assert(string.nth(0).unwrap() == NUMBER0); assert(string.nth(1).unwrap() == NUMBER1); assert(string.nth(2).unwrap() == NUMBER2); + string.push(NUMBER3); assert(string.nth(0).unwrap() == NUMBER0); assert(string.nth(1).unwrap() == NUMBER1); assert(string.nth(2).unwrap() == NUMBER2); assert(string.nth(3).unwrap() == NUMBER3); + string.push(NUMBER4); assert(string.nth(0).unwrap() == NUMBER0); assert(string.nth(1).unwrap() == NUMBER1); assert(string.nth(2).unwrap() == NUMBER2); assert(string.nth(3).unwrap() == NUMBER3); assert(string.nth(4).unwrap() == NUMBER4); + string.clear(); string.push(NUMBER5); string.push(NUMBER6); assert(string.nth(0).unwrap() == NUMBER5); assert(string.nth(1).unwrap() == NUMBER6); } + fn test_pop() { let mut string = ~String::new(); + string.push(NUMBER0); string.push(NUMBER1); string.push(NUMBER2); string.push(NUMBER3); string.push(NUMBER4); + assert(string.len() == 5); assert(string.pop().unwrap() == NUMBER4); + assert(string.len() == 4); assert(string.pop().unwrap() == NUMBER3); + assert(string.len() == 3); assert(string.pop().unwrap() == NUMBER2); + assert(string.len() == 2); assert(string.pop().unwrap() == NUMBER1); + assert(string.len() == 1); assert(string.pop().unwrap() == NUMBER0); + assert(string.len() == 0); assert(string.pop().is_none()); + string.push(NUMBER5); assert(string.pop().unwrap() == NUMBER5); } + fn test_push() { let mut string = ~String::new(); + assert(string.len() == 0); assert(string.is_empty()); assert(string.capacity() == 0); + string.push(NUMBER0); assert(string.nth(0).unwrap() == NUMBER0); assert(string.len() == 1); + string.push(NUMBER1); assert(string.nth(1).unwrap() == NUMBER1); assert(string.len() == 2); + string.push(NUMBER2); assert(string.nth(2).unwrap() == NUMBER2); assert(string.len() == 3); + string.push(NUMBER3); assert(string.nth(3).unwrap() == NUMBER3); assert(string.len() == 4); + string.push(NUMBER4); assert(string.nth(4).unwrap() == NUMBER4); assert(string.len() == 5); + string.push(NUMBER5); assert(string.nth(5).unwrap() == NUMBER5); assert(string.len() == 6); + string.push(NUMBER6); assert(string.nth(6).unwrap() == NUMBER6); assert(string.len() == 7); + string.push(NUMBER7); assert(string.nth(7).unwrap() == NUMBER7); assert(string.len() == 8); + string.push(NUMBER8); assert(string.nth(8).unwrap() == NUMBER8); assert(string.len() == 9); + string.push(NUMBER1); assert(string.nth(9).unwrap() == NUMBER1); assert(string.len() == 10); + string.clear(); assert(string.len() == 0); assert(string.is_empty()); + string.push(NUMBER1); assert(string.nth(0).unwrap() == NUMBER1); assert(string.len() == 1); + string.push(NUMBER1); assert(string.nth(1).unwrap() == NUMBER1); assert(string.len() == 2); + string.push(NUMBER0); assert(string.nth(2).unwrap() == NUMBER0); assert(string.len() == 3); } + fn test_remove() { let mut string = ~String::new(); + string.push(NUMBER0); string.push(NUMBER1); string.push(NUMBER2); string.push(NUMBER3); string.push(NUMBER4); string.push(NUMBER5); + assert(string.len() == 6); + assert(string.remove(0) == NUMBER0); assert(string.len() == 5); + assert(string.remove(0) == NUMBER1); assert(string.len() == 4); + assert(string.remove(1) == NUMBER3); assert(string.len() == 3); + assert(string.remove(string.len() - 1) == NUMBER5); assert(string.len() == 2); + assert(string.remove(1) == NUMBER4); assert(string.len() == 1); + assert(string.remove(0) == NUMBER2); assert(string.len() == 0); + string.push(NUMBER6); assert(string.remove(0) == NUMBER6); assert(string.len() == 0); } + fn 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.push(NUMBER0); assert(string.capacity() == 1); + string.push(NUMBER1); assert(string.capacity() == 2); + string.push(NUMBER2); assert(string.capacity() == 4); + string.clear(); assert(string.capacity() == 4); + let mut string = ~String::with_capacity(4); + assert(string.capacity() == 4); + string.push(NUMBER0); assert(string.capacity() == 4); + string.push(NUMBER1); assert(string.capacity() == 4); + string.push(NUMBER2); assert(string.capacity() == 4); + string.push(NUMBER3); assert(string.capacity() == 4); + string.push(NUMBER4); assert(string.capacity() == 8); } From 2863cd59a7fb0b0622a585ddfd7099def9d94fb2 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 15:25:16 -0400 Subject: [PATCH 31/46] Update inline documentation --- sway_libs/src/string.sw | 47 +++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/sway_libs/src/string.sw b/sway_libs/src/string.sw index 30fe260a..f6924ab3 100644 --- a/sway_libs/src/string.sw +++ b/sway_libs/src/string.sw @@ -26,13 +26,22 @@ impl String { } /// Converts a vector of bytes to a `String`. + /// + /// # Arguments + /// + /// * `vec` - The vector of `u8` bytes which should be converted into a `String`. pub fn from_utf8(vec: Vec) -> String { String { bytes: vec } } - // Inserts a character at the index within the string. - pub fn insert(self, index: u64, character: u8) { - self.bytes.insert(index, character); + /// Inserts a byte at the index within the `String`. + /// + /// # Arguments + /// + /// * `byte` - The element which is to be added to the `String`. + /// * `index` - The position in the `String` where the element is to be inserted. + pub fn insert(self, byte: u8, index: u64) { + self.bytes.insert(index, byte); } /// Returns `true` if the vector contains no elements. @@ -40,13 +49,13 @@ impl String { self.bytes.is_empty() } - /// Returns the number of elements in the vector, also referred to + /// Returns the number of elements in the `String`, also referred to /// as its 'length'. pub fn len(self) -> u64 { self.bytes.len() } - /// Constructs a new, empty `String`. + /// Constructs a new, empty `String`. pub fn new() -> Self { Self { bytes: ~Vec::new(), @@ -54,26 +63,42 @@ impl String { } /// Returns the element found at the index of given. + /// + /// # Arguments + /// + /// * `index` - The position of the element to be returned from the `String`. pub fn nth(self, index: u64) -> Option { self.bytes.get(index) } - /// Removes the last character from the string buffer and returns it. + /// Removes the last character from the `String` buffer and returns it. pub fn pop(self) -> Option { self.bytes.pop() } - /// Appends an element to the back of the `String`. - pub fn push(self, value: u8) { - self.bytes.push(value); + /// Appends an element to the back of the `String`. + /// + /// # Arguments + /// + /// * `byte` - The element to be appended to the end of the `String`. + pub fn push(self, byte: u8) { + self.bytes.push(byte); } - /// Removes and returns the element at position `index` within the string. + /// Removes and returns the element at the specified index within the `String`. + /// + /// # Arguments + /// + /// * `index` - The position of the element in the `String` to be removed. pub fn remove(self, index: u64) -> u8 { self.bytes.remove(index) } - /// Constructs a new, empty `String` with the specified capacity. + /// Constructs a new, empty `String` with the specified capacity. + /// + /// # Arguments + /// + /// * `capacity` - The specified amount of memory on the heap to be allocated for the `String`. pub fn with_capacity(capacity: u64) -> Self { Self { bytes: ~Vec::with_capacity(capacity), From d003e41caf7968fa3dbca20834db1e5459357d32 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 15:27:45 -0400 Subject: [PATCH 32/46] Update formatting --- sway_libs/Forc.lock | 14 -------------- sway_libs/src/string.sw | 24 ++++++++++++------------ 2 files changed, 12 insertions(+), 26 deletions(-) delete mode 100644 sway_libs/Forc.lock diff --git a/sway_libs/Forc.lock b/sway_libs/Forc.lock deleted file mode 100644 index 59758a0e..00000000 --- a/sway_libs/Forc.lock +++ /dev/null @@ -1,14 +0,0 @@ -[[package]] -name = 'core' -source = 'path+from-root-603BAB4590C454D3' -dependencies = [] - -[[package]] -name = 'std' -source = 'git+https://github.com/fuellabs/sway?tag=v0.19.0#5c716e1ba55d755555ed5aa186c883f73c4f90dc' -dependencies = ['core'] - -[[package]] -name = 'sway_libs' -source = 'root' -dependencies = ['std'] diff --git a/sway_libs/src/string.sw b/sway_libs/src/string.sw index f6924ab3..e5bc092a 100644 --- a/sway_libs/src/string.sw +++ b/sway_libs/src/string.sw @@ -26,18 +26,18 @@ impl String { } /// Converts a vector of bytes to a `String`. - /// + /// /// # Arguments - /// + /// /// * `vec` - The vector of `u8` bytes which should be converted into a `String`. pub fn from_utf8(vec: Vec) -> String { String { bytes: vec } } /// Inserts a byte at the index within the `String`. - /// + /// /// # Arguments - /// + /// /// * `byte` - The element which is to be added to the `String`. /// * `index` - The position in the `String` where the element is to be inserted. pub fn insert(self, byte: u8, index: u64) { @@ -63,9 +63,9 @@ impl String { } /// Returns the element found at the index of given. - /// + /// /// # Arguments - /// + /// /// * `index` - The position of the element to be returned from the `String`. pub fn nth(self, index: u64) -> Option { self.bytes.get(index) @@ -77,27 +77,27 @@ impl String { } /// Appends an element to the back of the `String`. - /// + /// /// # Arguments - /// + /// /// * `byte` - The element to be appended to the end of the `String`. pub fn push(self, byte: u8) { self.bytes.push(byte); } /// Removes and returns the element at the specified index within the `String`. - /// + /// /// # Arguments - /// + /// /// * `index` - The position of the element in the `String` to be removed. pub fn remove(self, index: u64) -> u8 { self.bytes.remove(index) } /// Constructs a new, empty `String` with the specified capacity. - /// + /// /// # Arguments - /// + /// /// * `capacity` - The specified amount of memory on the heap to be allocated for the `String`. pub fn with_capacity(capacity: u64) -> Self { Self { From 63880ab52a5a24bd647d3c6d5b653c2fe3c0e422 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 15:28:54 -0400 Subject: [PATCH 33/46] Remove Forc.lock --- tests/src/test_projects/string/Forc.lock | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 tests/src/test_projects/string/Forc.lock diff --git a/tests/src/test_projects/string/Forc.lock b/tests/src/test_projects/string/Forc.lock deleted file mode 100644 index 111cc5d2..00000000 --- a/tests/src/test_projects/string/Forc.lock +++ /dev/null @@ -1,22 +0,0 @@ -[[package]] -name = 'core' -source = 'path+from-root-3851086A5CA5C88E' -dependencies = [] - -[[package]] -name = 'std' -source = 'git+https://github.com/fuellabs/sway?tag=v0.24.1#54e1a340e6c0c9a675e8a66aab03f579d5d6b39b' -dependencies = ['core'] - -[[package]] -name = 'string' -source = 'root' -dependencies = [ - 'std', - 'sway_libs', -] - -[[package]] -name = 'sway_libs' -source = 'path+from-root-245F7BDB5423C0B5' -dependencies = ['std'] From 00a417b2ae65166d939f6e347c20fb52dfdbfb2a Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 16:14:48 -0400 Subject: [PATCH 34/46] Add README.md and SPECIFICATION.md --- README.md | 1 + sway_libs/src/lib.sw | 2 +- sway_libs/src/string/README.md | 41 ++++++++++++++++++ sway_libs/src/string/SPECIFICATION.md | 61 +++++++++++++++++++++++++++ sway_libs/src/{ => string}/string.sw | 0 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 sway_libs/src/string/README.md create mode 100644 sway_libs/src/string/SPECIFICATION.md rename sway_libs/src/{ => string}/string.sw (100%) diff --git a/README.md b/README.md index 9e1ae9ac..8f611ba5 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ These libraries contain helper functions, generalized standards, and other tools ### Libraries - [Binary Merkle Proof](./sway_libs/src/merkle_proof/) is used to verify Binary Merkle Trees computed off-chain. +- [String](./sway_libs/src/string.sw) is a Rust-like implementation of the utf-8 based String library. ## Using a library diff --git a/sway_libs/src/lib.sw b/sway_libs/src/lib.sw index 4ddaee32..656f16d1 100644 --- a/sway_libs/src/lib.sw +++ b/sway_libs/src/lib.sw @@ -1,4 +1,4 @@ library sway_libs; dep merkle_proof/binary_merkle_proof; -dep string; +dep string/string; diff --git a/sway_libs/src/string/README.md b/sway_libs/src/string/README.md new file mode 100644 index 00000000..edf49846 --- /dev/null +++ b/sway_libs/src/string/README.md @@ -0,0 +1,41 @@ +# Overview + +The String library allows for developers to use valid UTF-8 encoded strings in Sway. This is differs from Sway's `str` as the size must not be known at compile time. `String` is heap allocated, growable, and not null terminated. + +The `String` is stored as vector of bytes. + +For more information please see the [specification](./SPECIFICATION.md). + +> **Note** There is currently no way to convert a `str` to a `String`. + +# Using the Library + +## Using the String Library in Sway + +Once imported, using the String library is as simple as defining a new variable and calling the `new` function. + +```rust +let mut string = ~String::new(); +``` + +Appending or adding to the `String` can be done by calling the `push` and `insert` functions. + +```rust +string.push(0u8); +string.insert(0u8, 0); +``` + +Removing from the `String` can be done with either the `pop` or `remove` functions. + +```rust +let last_element = string.pop().unwrap(); +let nth_element = string.remove(0); +``` + +To retrieve an element in the `String`, use the `nth` function. + +```rust +let nth_element = string.nth(0); +``` + +For more information please see the [specification](./SPECIFICATION.md). diff --git a/sway_libs/src/string/SPECIFICATION.md b/sway_libs/src/string/SPECIFICATION.md new file mode 100644 index 00000000..8ce5420e --- /dev/null +++ b/sway_libs/src/string/SPECIFICATION.md @@ -0,0 +1,61 @@ +# Overview + +This document provides an overview of the String library. + +It outlines the use cases, i.e. specification, and describes how to implement the library. + +# Use Cases + +The String library can be used anytime a string's length is unknown at compile time and as such lives on the heap. Further methods can then be implemented to provide additional features building off of the `String` struct. + +## Public Functions + +### `as_bytes` + +The `as_bytes` function is used to convert the `String` struct back to a `Vec` of `u8` bytes. + +### `capacity` + +The `capacity` function will return the current amount of memory on the heap allocated to the `String`. + +> **Note** Capacity and length are not the same. A `String` may have a length of 0 but any arbitrary capacity. + +### `clear` + +The `clear` function will truncate the `String` to a length of 0. This does not clear the capacity of the `String`. + +### `from_utf8` + +The `from_utf8` function will return a `String` from a `Vec` of `u8` bytes. + +### `insert` + +The `insert` function will insert a new element at the specified index in the `String`. + +### `is_empty` + +The `is_emtpy` function will return a `bool` as to whether there are elements in the `String`. This does not check the capacity of the `String`. + +### `len` + +The `len` function will return the length or number of elements in the `String`. This does not check the capacity of the `String`. + +### `new` + +The `new` function will construct an empty `String` struct. + +### `nth` + +The `nth` function will return the element at the specified index in the `String`. If the index is out of bounds, `None` is returned. + +### `pop` + +The `pop` function will both return the last element in the `String` and remove it. If the `String` does not have any elements, `None` is returned. This does not change it's capacity. + +### `remove` + +The `remove` function with both return and remove the specified element in the `String`. This does not change it's capacity. + +### `with_capacity` + +The `with_capacity` function will construct an empty `String` struct with a specified amount of memory allocated on the heap. diff --git a/sway_libs/src/string.sw b/sway_libs/src/string/string.sw similarity index 100% rename from sway_libs/src/string.sw rename to sway_libs/src/string/string.sw From 83b5602df75823c530c3b1d72d8db53df6be8f7a Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 16:17:12 -0400 Subject: [PATCH 35/46] Fix README link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8f611ba5..4526895a 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ These libraries contain helper functions, generalized standards, and other tools ### Libraries - [Binary Merkle Proof](./sway_libs/src/merkle_proof/) is used to verify Binary Merkle Trees computed off-chain. -- [String](./sway_libs/src/string.sw) is a Rust-like implementation of the utf-8 based String library. +- [String](./sway_libs/src/string/) is a UTF-8 encoded String library used for dynamically lengthed strings. ## Using a library From 68afba37915de0443be906e926e28a63a8226dda Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 5 Oct 2022 16:37:53 -0400 Subject: [PATCH 36/46] Add how to import to README --- sway_libs/src/string/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sway_libs/src/string/README.md b/sway_libs/src/string/README.md index edf49846..00b43b4c 100644 --- a/sway_libs/src/string/README.md +++ b/sway_libs/src/string/README.md @@ -12,6 +12,12 @@ For more information please see the [specification](./SPECIFICATION.md). ## Using the String Library in Sway +Import `String` by adding the Sway-Libs to your Forc.toml and appending the following to your Sway file. + +```rust +use sway_libs::string::String; +``` + Once imported, using the String library is as simple as defining a new variable and calling the `new` function. ```rust From e128a7f3d682e837c77394ef784d50f69ea475b0 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 6 Oct 2022 15:30:09 -0400 Subject: [PATCH 37/46] Update code based on suggestions --- sway_libs/src/string/string.sw | 37 ++++++++++++--------------- tests/src/test_projects/string/mod.rs | 6 ++--- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/sway_libs/src/string/string.sw b/sway_libs/src/string/string.sw index e5bc092a..16134e42 100644 --- a/sway_libs/src/string/string.sw +++ b/sway_libs/src/string/string.sw @@ -2,25 +2,22 @@ library string; use std::{option::Option, vec::Vec}; -pub struct String { +pub struct String { bytes: Vec, } -impl String { - /// Returns the bytes stored for the `String`. +impl String { + /// Returns the bytes stored for the `String`. pub fn as_bytes(self) -> Vec { self.bytes } - /// Gets the capacity of the allocation. + /// Gets the amount of memory on the heap allocated to the `String`. pub fn capacity(self) -> u64 { self.bytes.capacity() } - /// Truncates this `String`, removing all contents. - /// - /// While this means the `String` will have a length of zero, it does not - /// touch its capacity. + /// Truncates this `String` to a length of zero, removing all contents. pub fn clear(self) { self.bytes.clear() } @@ -29,17 +26,17 @@ impl String { /// /// # Arguments /// - /// * `vec` - The vector of `u8` bytes which should be converted into a `String`. - pub fn from_utf8(vec: Vec) -> String { - String { bytes: vec } + /// * `bytes` - The vector of `u8` bytes which will be converted into a `String`. + pub fn from_utf8(bytes: Vec) -> String { + String { bytes } } /// Inserts a byte at the index within the `String`. /// /// # Arguments /// - /// * `byte` - The element which is to be added to the `String`. - /// * `index` - The position in the `String` where the element is to be inserted. + /// * `byte` - The element which will be added to the `String`. + /// * `index` - The position in the `String` where the element will be inserted. pub fn insert(self, byte: u8, index: u64) { self.bytes.insert(index, byte); } @@ -55,18 +52,18 @@ impl String { self.bytes.len() } - /// Constructs a new, empty `String`. + /// Constructs a new instance of the `String` type. pub fn new() -> Self { Self { bytes: ~Vec::new(), } } - /// Returns the element found at the index of given. + /// Returns the element at the specified index. /// /// # Arguments /// - /// * `index` - The position of the element to be returned from the `String`. + /// * `index` - The position of the element that will be returned. pub fn nth(self, index: u64) -> Option { self.bytes.get(index) } @@ -76,7 +73,7 @@ impl String { self.bytes.pop() } - /// Appends an element to the back of the `String`. + /// Appends an element to the end of the `String`. /// /// # Arguments /// @@ -85,16 +82,16 @@ impl String { self.bytes.push(byte); } - /// Removes and returns the element at the specified index within the `String`. + /// Removes and returns the element at the specified index. /// /// # Arguments /// - /// * `index` - The position of the element in the `String` to be removed. + /// * `index` - The position of the element that will be removed. pub fn remove(self, index: u64) -> u8 { self.bytes.remove(index) } - /// Constructs a new, empty `String` with the specified capacity. + /// Constructs a new instance of the `String` type with the specified capacity. /// /// # Arguments /// diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index 30c09a53..197fc36b 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -2,11 +2,11 @@ use fuels::{prelude::*, tx::ContractId}; // Load abi from json abigen!( - TestStringLib, + StringTestLib, "test_projects/string/out/debug/string-abi.json" ); -async fn test_string_instance() -> TestStringLib { +async fn string_test_instance() -> StringTestLib { // Launch a local network and deploy the contract let mut wallets = launch_custom_provider_and_get_wallets( WalletsConfig::new( @@ -28,7 +28,7 @@ async fn test_string_instance() -> TestStringLib { .await .unwrap(); - let instance = TestStringLibBuilder::new(id.to_string(), wallet).build(); + let instance = StringTestLibBuilder::new(id.to_string(), wallet).build(); instance } From d3b500686cac0e943dbd6107133179c076ca28e1 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 6 Oct 2022 15:30:34 -0400 Subject: [PATCH 38/46] Update documentation based on suggestions --- README.md | 2 +- sway_libs/src/string/README.md | 12 ++++--- sway_libs/src/string/SPECIFICATION.md | 50 +++++++++++++-------------- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 33ed8473..f6581f98 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ These libraries contain helper functions, generalized standards, and other tools ### Libraries - [Binary Merkle Proof](./sway_libs/src/merkle_proof/) is used to verify Binary Merkle Trees computed off-chain. -- [String](./sway_libs/src/string/) is a UTF-8 encoded String library used for dynamically lengthed strings. +- [String](./sway_libs/src/string/) is used for dynamic length strings that are UTF-8 encoded. ## Using a library diff --git a/sway_libs/src/string/README.md b/sway_libs/src/string/README.md index 00b43b4c..5fedc00b 100644 --- a/sway_libs/src/string/README.md +++ b/sway_libs/src/string/README.md @@ -1,8 +1,8 @@ # Overview -The String library allows for developers to use valid UTF-8 encoded strings in Sway. This is differs from Sway's `str` as the size must not be known at compile time. `String` is heap allocated, growable, and not null terminated. +The String library allows for developers to use valid UTF-8 encoded strings of dynamic length in Sway. The `String` is heap allocated, growable, and not null terminated. -The `String` is stored as vector of bytes. +The `String` is stored as vector of bytes. This differs from Sway's built in `str` as the size must not be known at compile time and is not static. For more information please see the [specification](./SPECIFICATION.md). @@ -10,20 +10,22 @@ For more information please see the [specification](./SPECIFICATION.md). # Using the Library -## Using the String Library in Sway +## Getting Started -Import `String` by adding the Sway-Libs to your Forc.toml and appending the following to your Sway file. +In order to use the `String` library it must be added to the Forc.toml file and then imported into your Sway project. To add Sway-libs as a dependency to the Forc.toml in your project, please see the [source README.md](./README.md). ```rust use sway_libs::string::String; ``` -Once imported, using the String library is as simple as defining a new variable and calling the `new` function. +Once imported, a `String` can be instantiated defining a new variable and calling the `new` function. ```rust let mut string = ~String::new(); ``` +## Basic Functionality + Appending or adding to the `String` can be done by calling the `push` and `insert` functions. ```rust diff --git a/sway_libs/src/string/SPECIFICATION.md b/sway_libs/src/string/SPECIFICATION.md index 8ce5420e..2cfe005c 100644 --- a/sway_libs/src/string/SPECIFICATION.md +++ b/sway_libs/src/string/SPECIFICATION.md @@ -4,58 +4,58 @@ This document provides an overview of the String library. It outlines the use cases, i.e. specification, and describes how to implement the library. -# Use Cases +## Use Cases The String library can be used anytime a string's length is unknown at compile time and as such lives on the heap. Further methods can then be implemented to provide additional features building off of the `String` struct. ## Public Functions -### `as_bytes` +### `as_bytes()` -The `as_bytes` function is used to convert the `String` struct back to a `Vec` of `u8` bytes. +Used to convert the `String` struct to a `Vec` of `u8` bytes. -### `capacity` +### `capacity()` -The `capacity` function will return the current amount of memory on the heap allocated to the `String`. +Returns the total amount of memory on the heap allocated to the `String` which can be filled with elements. > **Note** Capacity and length are not the same. A `String` may have a length of 0 but any arbitrary capacity. -### `clear` +### `clear()` -The `clear` function will truncate the `String` to a length of 0. This does not clear the capacity of the `String`. +Truncates the `String` to a length of 0 and will appear empty. This does not clear the capacity of the `String`. -### `from_utf8` +### `from_utf8()` -The `from_utf8` function will return a `String` from a `Vec` of `u8` bytes. +Given a vector of `u8`'s a new `String` instance will be returned. -### `insert` +### `insert()` -The `insert` function will insert a new element at the specified index in the `String`. +Inserts a new element at the specified index in the `String`. -### `is_empty` +### `is_empty()` -The `is_emtpy` function will return a `bool` as to whether there are elements in the `String`. This does not check the capacity of the `String`. +Returns a boolean indicating whether the length of the `String` is zero. -### `len` +### `len()` -The `len` function will return the length or number of elements in the `String`. This does not check the capacity of the `String`. +Returns the total number of elements in the `String`. -### `new` +### `new()` -The `new` function will construct an empty `String` struct. +Creates a new instance of the `String` struct. -### `nth` +### `nth()` -The `nth` function will return the element at the specified index in the `String`. If the index is out of bounds, `None` is returned. +Returns the element at the specified index in the `String`. If the index is out of bounds, `None` is returned. -### `pop` +### `pop()` -The `pop` function will both return the last element in the `String` and remove it. If the `String` does not have any elements, `None` is returned. This does not change it's capacity. +Removes the last element in the `String` and returns it. If the `String` does not have any elements, `None` is returned. -### `remove` +### `remove()` -The `remove` function with both return and remove the specified element in the `String`. This does not change it's capacity. +Will both return and remove the specified element in the `String`. -### `with_capacity` +### `with_capacity()` -The `with_capacity` function will construct an empty `String` struct with a specified amount of memory allocated on the heap. +Creates a new instance of the `String` struct with a specified amount of memory allocated on the heap. From 0080b060b88db18cc0af437db6138ce4c88e8771 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 6 Oct 2022 15:34:22 -0400 Subject: [PATCH 39/46] Remove warnings and fix instance calls --- tests/src/test_projects/string/mod.rs | 28 +++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index 197fc36b..b5e8b418 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -1,4 +1,4 @@ -use fuels::{prelude::*, tx::ContractId}; +use fuels::{prelude::*}; // Load abi from json abigen!( @@ -43,7 +43,7 @@ mod as_bytes { #[tokio::test] async fn returns_bytes() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_as_bytes().call().await.unwrap(); } @@ -60,7 +60,7 @@ mod capacity { #[tokio::test] async fn returns_capacity() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_capacity().call().await.unwrap(); } @@ -77,7 +77,7 @@ mod clear { #[tokio::test] async fn clears_string() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_clear().call().await.unwrap(); } @@ -94,7 +94,7 @@ mod from_utf8 { #[tokio::test] async fn converts_to_string() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_from_utf8().call().await.unwrap(); } @@ -111,7 +111,7 @@ mod insert { #[tokio::test] async fn inserts_into_string() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_insert().call().await.unwrap(); } @@ -128,7 +128,7 @@ mod is_empty { #[tokio::test] async fn returns_if_empty() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_is_empty().call().await.unwrap(); } @@ -145,7 +145,7 @@ mod len { #[tokio::test] async fn returns_string_length() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_len().call().await.unwrap(); } @@ -162,7 +162,7 @@ mod new { #[tokio::test] async fn creates_empty_string() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_new().call().await.unwrap(); } @@ -179,7 +179,7 @@ mod nth { #[tokio::test] async fn returns_nth_element_in_string() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_nth().call().await.unwrap(); } @@ -196,7 +196,7 @@ mod pop { #[tokio::test] async fn pops_last_element_in_string() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_pop().call().await.unwrap(); } @@ -213,7 +213,7 @@ mod push { #[tokio::test] async fn pushes_to_string() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_push().call().await.unwrap(); } @@ -230,7 +230,7 @@ mod remove { #[tokio::test] async fn removes_element_in_string() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_remove().call().await.unwrap(); } @@ -247,7 +247,7 @@ mod with_capacity { #[tokio::test] async fn creates_string_with_capacity() { - let instance = test_string_instance().await; + let instance = string_test_instance().await; let _result = instance.test_with_capacity().call().await.unwrap(); } From b7e61fdf914122a9b4162194cdb0f60fe4783530 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Fri, 7 Oct 2022 11:42:49 -0400 Subject: [PATCH 40/46] Update wording of documentation to inform of String's usage of unsafe UTF-8 encoding --- README.md | 2 +- sway_libs/src/string/README.md | 20 ++++++++++++-------- sway_libs/src/string/SPECIFICATION.md | 14 ++++++++------ sway_libs/src/string/string.sw | 18 +++++++++--------- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 97e03d62..700830db 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ These libraries contain helper functions, generalized standards, and other tools ### Libraries - [Binary Merkle Proof](./sway_libs/src/merkle_proof/) is used to verify Binary Merkle Trees computed off-chain. -- [String](./sway_libs/src/string/) is used for dynamic length strings that are UTF-8 encoded. +- [String](./sway_libs/src/string/) is an interface to implement dynamic length strings that are UTF-8 encoded. ## Using a library diff --git a/sway_libs/src/string/README.md b/sway_libs/src/string/README.md index 5fedc00b..1de84dae 100644 --- a/sway_libs/src/string/README.md +++ b/sway_libs/src/string/README.md @@ -1,13 +1,17 @@ # Overview -The String library allows for developers to use valid UTF-8 encoded strings of dynamic length in Sway. The `String` is heap allocated, growable, and not null terminated. +The String library provides an interface to use UTF-8 encoded strings of dynamic length in Sway. The `String` is heap allocated, growable, and not null terminated. -The `String` is stored as vector of bytes. This differs from Sway's built in `str` as the size must not be known at compile time and is not static. +The `String` is stored as vector of bytes. This differs from Sway's built in `str` as the size must not be known at compile time and is not static. For more information please see the [specification](./SPECIFICATION.md). > **Note** There is currently no way to convert a `str` to a `String`. +## Known Issues + +It is important to note that unlike Rust's `String`, this `String` library does **not** guarantee a valid UTF-8 string. The `String` currently behaves only as a `vec` and does not perform any validation. This intended to be supported in the future with the introduction of [`char`](https://github.com/FuelLabs/sway/issues/2937) to the Sway language. + # Using the Library ## Getting Started @@ -26,24 +30,24 @@ let mut string = ~String::new(); ## Basic Functionality -Appending or adding to the `String` can be done by calling the `push` and `insert` functions. +Appending or adding bytes to the `String` can be done by calling the `push` and `insert` functions. ```rust string.push(0u8); string.insert(0u8, 0); ``` -Removing from the `String` can be done with either the `pop` or `remove` functions. +Removing bytes from the `String` can be done with either the `pop` or `remove` functions. ```rust -let last_element = string.pop().unwrap(); -let nth_element = string.remove(0); +let last_byte = string.pop().unwrap(); +let nth_byte = string.remove(0); ``` -To retrieve an element in the `String`, use the `nth` function. +To retrieve a byte in the `String`, use the `nth` function. ```rust -let nth_element = string.nth(0); +let nth_byte = string.nth(0); ``` For more information please see the [specification](./SPECIFICATION.md). diff --git a/sway_libs/src/string/SPECIFICATION.md b/sway_libs/src/string/SPECIFICATION.md index 2cfe005c..d64be445 100644 --- a/sway_libs/src/string/SPECIFICATION.md +++ b/sway_libs/src/string/SPECIFICATION.md @@ -8,6 +8,8 @@ It outlines the use cases, i.e. specification, and describes how to implement th The String library can be used anytime a string's length is unknown at compile time and as such lives on the heap. Further methods can then be implemented to provide additional features building off of the `String` struct. +> **Note** There is no guarantee in the validity of the UTF-8 encoded `String` and should be used with caution. For more information, please see the [known issues](./README.md#known-issues). + ## Public Functions ### `as_bytes()` @@ -16,7 +18,7 @@ Used to convert the `String` struct to a `Vec` of `u8` bytes. ### `capacity()` -Returns the total amount of memory on the heap allocated to the `String` which can be filled with elements. +Returns the total amount of memory on the heap allocated to the `String` which can be filled with bytes. > **Note** Capacity and length are not the same. A `String` may have a length of 0 but any arbitrary capacity. @@ -30,7 +32,7 @@ Given a vector of `u8`'s a new `String` instance will be returned. ### `insert()` -Inserts a new element at the specified index in the `String`. +Inserts a new byte at the specified index in the `String`. ### `is_empty()` @@ -38,7 +40,7 @@ Returns a boolean indicating whether the length of the `String` is zero. ### `len()` -Returns the total number of elements in the `String`. +Returns the total number of bytes in the `String`. ### `new()` @@ -46,15 +48,15 @@ Creates a new instance of the `String` struct. ### `nth()` -Returns the element at the specified index in the `String`. If the index is out of bounds, `None` is returned. +Returns the byte at the specified index in the `String`. If the index is out of bounds, `None` is returned. ### `pop()` -Removes the last element in the `String` and returns it. If the `String` does not have any elements, `None` is returned. +Removes the last byte in the `String` and returns it. If the `String` does not have any bytes, `None` is returned. ### `remove()` -Will both return and remove the specified element in the `String`. +Will both remove and return the specified byte in the `String`. ### `with_capacity()` diff --git a/sway_libs/src/string/string.sw b/sway_libs/src/string/string.sw index 16134e42..1a0b50de 100644 --- a/sway_libs/src/string/string.sw +++ b/sway_libs/src/string/string.sw @@ -36,17 +36,17 @@ impl String { /// # Arguments /// /// * `byte` - The element which will be added to the `String`. - /// * `index` - The position in the `String` where the element will be inserted. + /// * `index` - The position in the `String` where the byte will be inserted. pub fn insert(self, byte: u8, index: u64) { self.bytes.insert(index, byte); } - /// Returns `true` if the vector contains no elements. + /// Returns `true` if the vector contains no bytes. pub fn is_empty(self) -> bool { self.bytes.is_empty() } - /// Returns the number of elements in the `String`, also referred to + /// Returns the number of bytes in the `String`, also referred to /// as its 'length'. pub fn len(self) -> u64 { self.bytes.len() @@ -59,21 +59,21 @@ impl String { } } - /// Returns the element at the specified index. + /// Returns the byte at the specified index. /// /// # Arguments /// - /// * `index` - The position of the element that will be returned. + /// * `index` - The position of the byte that will be returned. pub fn nth(self, index: u64) -> Option { self.bytes.get(index) } - /// Removes the last character from the `String` buffer and returns it. + /// Removes the last byte from the `String` buffer and returns it. pub fn pop(self) -> Option { self.bytes.pop() } - /// Appends an element to the end of the `String`. + /// Appends a byte to the end of the `String`. /// /// # Arguments /// @@ -82,11 +82,11 @@ impl String { self.bytes.push(byte); } - /// Removes and returns the element at the specified index. + /// Removes and returns the byte at the specified index. /// /// # Arguments /// - /// * `index` - The position of the element that will be removed. + /// * `index` - The position of the byte that will be removed. pub fn remove(self, index: u64) -> u8 { self.bytes.remove(index) } From 7db876113355550680997604aab9ed7029389e98 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Fri, 7 Oct 2022 11:49:24 -0400 Subject: [PATCH 41/46] Update incorrect link to source README --- sway_libs/src/string/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway_libs/src/string/README.md b/sway_libs/src/string/README.md index 1de84dae..c9da56b3 100644 --- a/sway_libs/src/string/README.md +++ b/sway_libs/src/string/README.md @@ -16,7 +16,7 @@ It is important to note that unlike Rust's `String`, this `String` library does ## Getting Started -In order to use the `String` library it must be added to the Forc.toml file and then imported into your Sway project. To add Sway-libs as a dependency to the Forc.toml in your project, please see the [source README.md](./README.md). +In order to use the `String` library it must be added to the Forc.toml file and then imported into your Sway project. To add Sway-libs as a dependency to the Forc.toml in your project, please see the [source README.md](../../../README.md). ```rust use sway_libs::string::String; From ae7a296c233899489b01461c79e136dbb9c419ec Mon Sep 17 00:00:00 2001 From: bitzoic Date: Fri, 7 Oct 2022 17:09:49 -0400 Subject: [PATCH 42/46] Refactor SDK tests into multiple files --- tests/src/test_projects/string/mod.rs | 256 +----------------- .../string/tests/functions/as_bytes.rs | 16 ++ .../string/tests/functions/capacity.rs | 16 ++ .../string/tests/functions/clear.rs | 16 ++ .../string/tests/functions/from_utf8.rs | 16 ++ .../string/tests/functions/insert.rs | 16 ++ .../string/tests/functions/is_empty.rs | 16 ++ .../string/tests/functions/len.rs | 16 ++ .../string/tests/functions/mod.rs | 13 + .../string/tests/functions/new.rs | 16 ++ .../string/tests/functions/nth.rs | 16 ++ .../string/tests/functions/pop.rs | 16 ++ .../string/tests/functions/push.rs | 16 ++ .../string/tests/functions/remove.rs | 16 ++ .../string/tests/functions/with_capacity.rs | 16 ++ tests/src/test_projects/string/tests/mod.rs | 2 + .../test_projects/string/tests/utils/mod.rs | 96 +++++++ 17 files changed, 320 insertions(+), 255 deletions(-) create mode 100644 tests/src/test_projects/string/tests/functions/as_bytes.rs create mode 100644 tests/src/test_projects/string/tests/functions/capacity.rs create mode 100644 tests/src/test_projects/string/tests/functions/clear.rs create mode 100644 tests/src/test_projects/string/tests/functions/from_utf8.rs create mode 100644 tests/src/test_projects/string/tests/functions/insert.rs create mode 100644 tests/src/test_projects/string/tests/functions/is_empty.rs create mode 100644 tests/src/test_projects/string/tests/functions/len.rs create mode 100644 tests/src/test_projects/string/tests/functions/mod.rs create mode 100644 tests/src/test_projects/string/tests/functions/new.rs create mode 100644 tests/src/test_projects/string/tests/functions/nth.rs create mode 100644 tests/src/test_projects/string/tests/functions/pop.rs create mode 100644 tests/src/test_projects/string/tests/functions/push.rs create mode 100644 tests/src/test_projects/string/tests/functions/remove.rs create mode 100644 tests/src/test_projects/string/tests/functions/with_capacity.rs create mode 100644 tests/src/test_projects/string/tests/mod.rs create mode 100644 tests/src/test_projects/string/tests/utils/mod.rs diff --git a/tests/src/test_projects/string/mod.rs b/tests/src/test_projects/string/mod.rs index b5e8b418..dc39c4cc 100644 --- a/tests/src/test_projects/string/mod.rs +++ b/tests/src/test_projects/string/mod.rs @@ -1,255 +1 @@ -use fuels::{prelude::*}; - -// Load abi from json -abigen!( - StringTestLib, - "test_projects/string/out/debug/string-abi.json" -); - -async fn string_test_instance() -> StringTestLib { - // Launch a local network and deploy the contract - let mut wallets = launch_custom_provider_and_get_wallets( - WalletsConfig::new( - Some(1), /* Single wallet */ - Some(1), /* Single coin (UTXO) */ - Some(1_000_000_000), /* Amount per coin */ - ), - None, - ) - .await; - let wallet = wallets.pop().unwrap(); - - let id = Contract::deploy( - "test_projects/string/out/debug/string.bin", - &wallet, - TxParameters::default(), - StorageConfiguration::default(), - ) - .await - .unwrap(); - - let instance = StringTestLibBuilder::new(id.to_string(), wallet).build(); - - instance -} - -mod as_bytes { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn returns_bytes() { - let instance = string_test_instance().await; - - let _result = instance.test_as_bytes().call().await.unwrap(); - } - } -} - -mod capacity { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn returns_capacity() { - let instance = string_test_instance().await; - - let _result = instance.test_capacity().call().await.unwrap(); - } - } -} - -mod clear { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn clears_string() { - let instance = string_test_instance().await; - - let _result = instance.test_clear().call().await.unwrap(); - } - } -} - -mod from_utf8 { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn converts_to_string() { - let instance = string_test_instance().await; - - let _result = instance.test_from_utf8().call().await.unwrap(); - } - } -} - -mod insert { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn inserts_into_string() { - let instance = string_test_instance().await; - - let _result = instance.test_insert().call().await.unwrap(); - } - } -} - -mod is_empty { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn returns_if_empty() { - let instance = string_test_instance().await; - - let _result = instance.test_is_empty().call().await.unwrap(); - } - } -} - -mod len { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn returns_string_length() { - let instance = string_test_instance().await; - - let _result = instance.test_len().call().await.unwrap(); - } - } -} - -mod new { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn creates_empty_string() { - let instance = string_test_instance().await; - - let _result = instance.test_new().call().await.unwrap(); - } - } -} - -mod nth { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn returns_nth_element_in_string() { - let instance = string_test_instance().await; - - let _result = instance.test_nth().call().await.unwrap(); - } - } -} - -mod pop { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn pops_last_element_in_string() { - let instance = string_test_instance().await; - - let _result = instance.test_pop().call().await.unwrap(); - } - } -} - -mod push { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn pushes_to_string() { - let instance = string_test_instance().await; - - let _result = instance.test_push().call().await.unwrap(); - } - } -} - -mod remove { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn removes_element_in_string() { - let instance = string_test_instance().await; - - let _result = instance.test_remove().call().await.unwrap(); - } - } -} - -mod with_capacity { - - use super::*; - - mod success { - - use super::*; - - #[tokio::test] - async fn creates_string_with_capacity() { - let instance = string_test_instance().await; - - let _result = instance.test_with_capacity().call().await.unwrap(); - } - } -} +mod tests; diff --git a/tests/src/test_projects/string/tests/functions/as_bytes.rs b/tests/src/test_projects/string/tests/functions/as_bytes.rs new file mode 100644 index 00000000..4fa04176 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/as_bytes.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{as_bytes}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn returns_bytes() { + let instance = setup().await; + + as_bytes(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/capacity.rs b/tests/src/test_projects/string/tests/functions/capacity.rs new file mode 100644 index 00000000..92eca7e9 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/capacity.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{capacity}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn get_capacity() { + let instance = setup().await; + + capacity(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/clear.rs b/tests/src/test_projects/string/tests/functions/clear.rs new file mode 100644 index 00000000..85171df5 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/clear.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{clear}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn clears_bytes() { + let instance = setup().await; + + clear(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/from_utf8.rs b/tests/src/test_projects/string/tests/functions/from_utf8.rs new file mode 100644 index 00000000..804cd72f --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/from_utf8.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{from_utf8}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn converts_from_utf8_vec() { + let instance = setup().await; + + from_utf8(&instance).await; + } +} \ No newline at end of file diff --git a/tests/src/test_projects/string/tests/functions/insert.rs b/tests/src/test_projects/string/tests/functions/insert.rs new file mode 100644 index 00000000..d77ba5be --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/insert.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{insert}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn inserts_bytes_into_string() { + let instance = setup().await; + + insert(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/is_empty.rs b/tests/src/test_projects/string/tests/functions/is_empty.rs new file mode 100644 index 00000000..d365d2f7 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/is_empty.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{is_empty}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn checks_if_empty() { + let instance = setup().await; + + is_empty(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/len.rs b/tests/src/test_projects/string/tests/functions/len.rs new file mode 100644 index 00000000..f24a4fac --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/len.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{len}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn returns_string_length() { + let instance = setup().await; + + len(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/mod.rs b/tests/src/test_projects/string/tests/functions/mod.rs new file mode 100644 index 00000000..f4272047 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/mod.rs @@ -0,0 +1,13 @@ +mod as_bytes; +mod capacity; +mod clear; +mod from_utf8; +mod insert; +mod is_empty; +mod len; +mod new; +mod nth; +mod pop; +mod push; +mod remove; +mod with_capacity; diff --git a/tests/src/test_projects/string/tests/functions/new.rs b/tests/src/test_projects/string/tests/functions/new.rs new file mode 100644 index 00000000..09696842 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/new.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{new}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn creates_new_string() { + let instance = setup().await; + + new(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/nth.rs b/tests/src/test_projects/string/tests/functions/nth.rs new file mode 100644 index 00000000..09694fb6 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/nth.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{nth}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn returns_nth_index() { + let instance = setup().await; + + nth(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/pop.rs b/tests/src/test_projects/string/tests/functions/pop.rs new file mode 100644 index 00000000..2020f7d0 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/pop.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{pop}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn pops_end_of_string() { + let instance = setup().await; + + pop(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/push.rs b/tests/src/test_projects/string/tests/functions/push.rs new file mode 100644 index 00000000..ebc54460 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/push.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{push}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn pushes_to_end_of_string() { + let instance = setup().await; + + push(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/remove.rs b/tests/src/test_projects/string/tests/functions/remove.rs new file mode 100644 index 00000000..ab841463 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/remove.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{remove}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn removes_at_index() { + let instance = setup().await; + + remove(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/functions/with_capacity.rs b/tests/src/test_projects/string/tests/functions/with_capacity.rs new file mode 100644 index 00000000..16559b65 --- /dev/null +++ b/tests/src/test_projects/string/tests/functions/with_capacity.rs @@ -0,0 +1,16 @@ +use crate::string::tests::utils::{ + abi_calls::{with_capacity}, + test_helpers::setup, +}; + +mod success { + + use super::*; + + #[tokio::test] + async fn creates_new_string_with_capacity() { + let instance = setup().await; + + with_capacity(&instance).await; + } +} diff --git a/tests/src/test_projects/string/tests/mod.rs b/tests/src/test_projects/string/tests/mod.rs new file mode 100644 index 00000000..6c951a35 --- /dev/null +++ b/tests/src/test_projects/string/tests/mod.rs @@ -0,0 +1,2 @@ +mod functions; +mod utils; diff --git a/tests/src/test_projects/string/tests/utils/mod.rs b/tests/src/test_projects/string/tests/utils/mod.rs new file mode 100644 index 00000000..b17c2679 --- /dev/null +++ b/tests/src/test_projects/string/tests/utils/mod.rs @@ -0,0 +1,96 @@ +use fuels::{contract::contract::CallResponse, prelude::*}; + +// Load abi from json +abigen!( + StringTestLib, + "test_projects/string/out/debug/string-abi.json" +); + +pub mod abi_calls { + + use super::*; + + pub async fn as_bytes(contract: &StringTestLib) -> CallResponse<()> { + contract.test_as_bytes().call().await.unwrap() + } + + pub async fn capacity(contract: &StringTestLib) -> CallResponse<()> { + contract.test_capacity().call().await.unwrap() + } + + pub async fn clear(contract: &StringTestLib) -> CallResponse<()> { + contract.test_clear().call().await.unwrap() + } + + pub async fn from_utf8(contract: &StringTestLib) -> CallResponse<()> { + contract.test_from_utf8().call().await.unwrap() + } + + pub async fn insert(contract: &StringTestLib) -> CallResponse<()> { + contract.test_insert().call().await.unwrap() + } + + pub async fn is_empty(contract: &StringTestLib) -> CallResponse<()> { + contract.test_is_empty().call().await.unwrap() + } + + pub async fn len(contract: &StringTestLib) -> CallResponse<()> { + contract.test_len().call().await.unwrap() + } + + pub async fn new(contract: &StringTestLib) -> CallResponse<()> { + contract.test_new().call().await.unwrap() + } + + pub async fn nth(contract: &StringTestLib) -> CallResponse<()> { + contract.test_nth().call().await.unwrap() + } + + pub async fn pop(contract: &StringTestLib) -> CallResponse<()> { + contract.test_pop().call().await.unwrap() + } + + pub async fn push(contract: &StringTestLib) -> CallResponse<()> { + contract.test_push().call().await.unwrap() + } + + pub async fn remove(contract: &StringTestLib) -> CallResponse<()> { + contract.test_remove().call().await.unwrap() + } + + pub async fn with_capacity(contract: &StringTestLib) -> CallResponse<()> { + contract.test_with_capacity().call().await.unwrap() + } +} + +pub mod test_helpers { + + use super::*; + + pub async fn setup() -> StringTestLib { + // Launch a local network and deploy the contract + let mut wallets = launch_custom_provider_and_get_wallets( + WalletsConfig::new( + Some(1), /* Single wallet */ + Some(1), /* Single coin (UTXO) */ + Some(1_000_000_000), /* Amount per coin */ + ), + None, + ) + .await; + let wallet = wallets.pop().unwrap(); + + let id = Contract::deploy( + "test_projects/string/out/debug/string.bin", + &wallet, + TxParameters::default(), + StorageConfiguration::default(), + ) + .await + .unwrap(); + + let instance = StringTestLibBuilder::new(id.to_string(), wallet).build(); + + instance + } +} From 89d06d62631df1bca3f9556d60a75c95c76d4fdd Mon Sep 17 00:00:00 2001 From: bitzoic Date: Mon, 10 Oct 2022 10:36:13 -0400 Subject: [PATCH 43/46] Add None for nth test --- tests/src/test_projects/string/src/main.sw | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/src/test_projects/string/src/main.sw b/tests/src/test_projects/string/src/main.sw index 875e4c86..c03fe804 100644 --- a/tests/src/test_projects/string/src/main.sw +++ b/tests/src/test_projects/string/src/main.sw @@ -281,6 +281,8 @@ impl StringTest for Contract { string.push(NUMBER6); assert(string.nth(0).unwrap() == NUMBER5); assert(string.nth(1).unwrap() == NUMBER6); + + assert(string.nth(2).is_none()); } fn test_pop() { From c79457c51ce906fb7e242c8282632839bcd16340 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Mon, 10 Oct 2022 10:39:04 -0400 Subject: [PATCH 44/46] Add EOD to test file --- tests/src/test_projects/string/tests/functions/from_utf8.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/test_projects/string/tests/functions/from_utf8.rs b/tests/src/test_projects/string/tests/functions/from_utf8.rs index 804cd72f..4df1f6b4 100644 --- a/tests/src/test_projects/string/tests/functions/from_utf8.rs +++ b/tests/src/test_projects/string/tests/functions/from_utf8.rs @@ -13,4 +13,4 @@ mod success { from_utf8(&instance).await; } -} \ No newline at end of file +} From 446f6a741fbbafdce14afcff556be8f96c0534aa Mon Sep 17 00:00:00 2001 From: bitzoic Date: Mon, 10 Oct 2022 11:59:56 -0400 Subject: [PATCH 45/46] Update Documentation based on suggestions --- sway_libs/src/string/README.md | 13 ++++++++++--- sway_libs/src/string/SPECIFICATION.md | 10 +++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/sway_libs/src/string/README.md b/sway_libs/src/string/README.md index c9da56b3..878a6a25 100644 --- a/sway_libs/src/string/README.md +++ b/sway_libs/src/string/README.md @@ -2,11 +2,11 @@ The String library provides an interface to use UTF-8 encoded strings of dynamic length in Sway. The `String` is heap allocated, growable, and not null terminated. -The `String` is stored as vector of bytes. This differs from Sway's built in `str` as the size must not be known at compile time and is not static. +The `String` is stored as vector of bytes. This differs from Sway's built in `str` because the size cannot be known at compile time and the length is dynamic. For more information please see the [specification](./SPECIFICATION.md). -> **Note** There is currently no way to convert a `str` to a `String`. +> **Note** There is no way to convert a `str` to a `String`. ## Known Issues @@ -16,7 +16,7 @@ It is important to note that unlike Rust's `String`, this `String` library does ## Getting Started -In order to use the `String` library it must be added to the Forc.toml file and then imported into your Sway project. To add Sway-libs as a dependency to the Forc.toml in your project, please see the [source README.md](../../../README.md). +In order to use the `String` library it must be added to the Forc.toml file and then imported into your Sway project. To add Sway-libs as a dependency to the Forc.toml in your project, please see the [README.md](../../../README.md). ```rust use sway_libs::string::String; @@ -33,20 +33,27 @@ let mut string = ~String::new(); Appending or adding bytes to the `String` can be done by calling the `push` and `insert` functions. ```rust +// Append to the end string.push(0u8); + +// Insert at index 0 string.insert(0u8, 0); ``` Removing bytes from the `String` can be done with either the `pop` or `remove` functions. ```rust +// Remove the last byte from the string, return the option that wraps the value and unwrap the byte let last_byte = string.pop().unwrap(); + +// Remove and return the byte at index 0 let nth_byte = string.remove(0); ``` To retrieve a byte in the `String`, use the `nth` function. ```rust +// Retrieve the byte at index 0 let nth_byte = string.nth(0); ``` diff --git a/sway_libs/src/string/SPECIFICATION.md b/sway_libs/src/string/SPECIFICATION.md index d64be445..c3736112 100644 --- a/sway_libs/src/string/SPECIFICATION.md +++ b/sway_libs/src/string/SPECIFICATION.md @@ -6,7 +6,7 @@ It outlines the use cases, i.e. specification, and describes how to implement th ## Use Cases -The String library can be used anytime a string's length is unknown at compile time and as such lives on the heap. Further methods can then be implemented to provide additional features building off of the `String` struct. +The String library can be used anytime a string's length is unknown at compile time. Further methods can then be implemented to provide additional features building off of the `String` struct. > **Note** There is no guarantee in the validity of the UTF-8 encoded `String` and should be used with caution. For more information, please see the [known issues](./README.md#known-issues). @@ -14,13 +14,13 @@ The String library can be used anytime a string's length is unknown at compile t ### `as_bytes()` -Used to convert the `String` struct to a `Vec` of `u8` bytes. +Convert the `String` struct to a `Vec` of `u8` bytes. ### `capacity()` Returns the total amount of memory on the heap allocated to the `String` which can be filled with bytes. -> **Note** Capacity and length are not the same. A `String` may have a length of 0 but any arbitrary capacity. +> **Note** Capacity and length are not the same. A `String` may have a length of 0 but any positive capacity. ### `clear()` @@ -28,7 +28,7 @@ Truncates the `String` to a length of 0 and will appear empty. This does not cle ### `from_utf8()` -Given a vector of `u8`'s a new `String` instance will be returned. +A new instance of a `String` will be created from a vector of `u8`'s ### `insert()` @@ -56,7 +56,7 @@ Removes the last byte in the `String` and returns it. If the `String` does not h ### `remove()` -Will both remove and return the specified byte in the `String`. +Remove and return the specified byte in the `String`. ### `with_capacity()` From ab466102153d7732628561ed0c42f9d9b3b8bbe5 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Mon, 10 Oct 2022 12:00:25 -0400 Subject: [PATCH 46/46] Remove unneeded braces from tests --- tests/src/test_projects/string/tests/functions/as_bytes.rs | 2 +- tests/src/test_projects/string/tests/functions/capacity.rs | 2 +- tests/src/test_projects/string/tests/functions/clear.rs | 2 +- tests/src/test_projects/string/tests/functions/from_utf8.rs | 2 +- tests/src/test_projects/string/tests/functions/insert.rs | 2 +- tests/src/test_projects/string/tests/functions/is_empty.rs | 2 +- tests/src/test_projects/string/tests/functions/len.rs | 2 +- tests/src/test_projects/string/tests/functions/new.rs | 2 +- tests/src/test_projects/string/tests/functions/nth.rs | 2 +- tests/src/test_projects/string/tests/functions/pop.rs | 2 +- tests/src/test_projects/string/tests/functions/push.rs | 2 +- tests/src/test_projects/string/tests/functions/remove.rs | 2 +- tests/src/test_projects/string/tests/functions/with_capacity.rs | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/src/test_projects/string/tests/functions/as_bytes.rs b/tests/src/test_projects/string/tests/functions/as_bytes.rs index 4fa04176..f2db0e06 100644 --- a/tests/src/test_projects/string/tests/functions/as_bytes.rs +++ b/tests/src/test_projects/string/tests/functions/as_bytes.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{as_bytes}, + abi_calls::as_bytes, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/capacity.rs b/tests/src/test_projects/string/tests/functions/capacity.rs index 92eca7e9..b8c9f3e8 100644 --- a/tests/src/test_projects/string/tests/functions/capacity.rs +++ b/tests/src/test_projects/string/tests/functions/capacity.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{capacity}, + abi_calls::capacity, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/clear.rs b/tests/src/test_projects/string/tests/functions/clear.rs index 85171df5..906568d5 100644 --- a/tests/src/test_projects/string/tests/functions/clear.rs +++ b/tests/src/test_projects/string/tests/functions/clear.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{clear}, + abi_calls::clear, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/from_utf8.rs b/tests/src/test_projects/string/tests/functions/from_utf8.rs index 4df1f6b4..23595fd1 100644 --- a/tests/src/test_projects/string/tests/functions/from_utf8.rs +++ b/tests/src/test_projects/string/tests/functions/from_utf8.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{from_utf8}, + abi_calls::from_utf8, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/insert.rs b/tests/src/test_projects/string/tests/functions/insert.rs index d77ba5be..57a78edd 100644 --- a/tests/src/test_projects/string/tests/functions/insert.rs +++ b/tests/src/test_projects/string/tests/functions/insert.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{insert}, + abi_calls::insert, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/is_empty.rs b/tests/src/test_projects/string/tests/functions/is_empty.rs index d365d2f7..804245fc 100644 --- a/tests/src/test_projects/string/tests/functions/is_empty.rs +++ b/tests/src/test_projects/string/tests/functions/is_empty.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{is_empty}, + abi_calls::is_empty, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/len.rs b/tests/src/test_projects/string/tests/functions/len.rs index f24a4fac..db4883d6 100644 --- a/tests/src/test_projects/string/tests/functions/len.rs +++ b/tests/src/test_projects/string/tests/functions/len.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{len}, + abi_calls::len, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/new.rs b/tests/src/test_projects/string/tests/functions/new.rs index 09696842..54cdf86f 100644 --- a/tests/src/test_projects/string/tests/functions/new.rs +++ b/tests/src/test_projects/string/tests/functions/new.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{new}, + abi_calls::new, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/nth.rs b/tests/src/test_projects/string/tests/functions/nth.rs index 09694fb6..86526e7c 100644 --- a/tests/src/test_projects/string/tests/functions/nth.rs +++ b/tests/src/test_projects/string/tests/functions/nth.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{nth}, + abi_calls::nth, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/pop.rs b/tests/src/test_projects/string/tests/functions/pop.rs index 2020f7d0..a143ebe4 100644 --- a/tests/src/test_projects/string/tests/functions/pop.rs +++ b/tests/src/test_projects/string/tests/functions/pop.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{pop}, + abi_calls::pop, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/push.rs b/tests/src/test_projects/string/tests/functions/push.rs index ebc54460..5c07e237 100644 --- a/tests/src/test_projects/string/tests/functions/push.rs +++ b/tests/src/test_projects/string/tests/functions/push.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{push}, + abi_calls::push, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/remove.rs b/tests/src/test_projects/string/tests/functions/remove.rs index ab841463..b9799e07 100644 --- a/tests/src/test_projects/string/tests/functions/remove.rs +++ b/tests/src/test_projects/string/tests/functions/remove.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{remove}, + abi_calls::remove, test_helpers::setup, }; diff --git a/tests/src/test_projects/string/tests/functions/with_capacity.rs b/tests/src/test_projects/string/tests/functions/with_capacity.rs index 16559b65..679d1a9d 100644 --- a/tests/src/test_projects/string/tests/functions/with_capacity.rs +++ b/tests/src/test_projects/string/tests/functions/with_capacity.rs @@ -1,5 +1,5 @@ use crate::string::tests::utils::{ - abi_calls::{with_capacity}, + abi_calls::with_capacity, test_helpers::setup, };