diff --git a/frameworks/moveos-stdlib/doc/bcs.md b/frameworks/moveos-stdlib/doc/bcs.md index d2e2fd770..597f322c5 100644 --- a/frameworks/moveos-stdlib/doc/bcs.md +++ b/frameworks/moveos-stdlib/doc/bcs.md @@ -9,6 +9,7 @@ to values. This is the opposite of bcs::to_bytesfrom_bytes function and protected it with #[data_struct(T)]. +- [Struct `BCS`](#0x2_bcs_BCS) - [Constants](#@Constants_0) - [Function `to_bytes`](#0x2_bcs_to_bytes) - [Function `to_bool`](#0x2_bcs_to_bool) @@ -16,6 +17,8 @@ Note we provie a generic public from_bytes function and protected i - [Function `to_u64`](#0x2_bcs_to_u64) - [Function `to_u128`](#0x2_bcs_to_u128) - [Function `to_address`](#0x2_bcs_to_address) +- [Function `new`](#0x2_bcs_new) +- [Function `into_remainder_bytes`](#0x2_bcs_into_remainder_bytes) - [Function `peel_address`](#0x2_bcs_peel_address) - [Function `peel_bool`](#0x2_bcs_peel_bool) - [Function `peel_u8`](#0x2_bcs_peel_u8) @@ -49,6 +52,21 @@ Note we provie a generic public from_bytes function and protected i
use 0x1::bcs;
 use 0x1::option;
+use 0x1::vector;
+
+ + + + + +## Struct `BCS` + +A helper struct that saves resources on operations. For better +vector performance, it stores reversed bytes of the BCS and +enables use of vector::pop_back. + + +
struct BCS has copy, drop, store
 
@@ -179,6 +197,32 @@ The request Move type is not match with input Move type. + + +## Function `new` + +Creates a new instance of BCS wrapper that holds inversed +bytes for better performance. + + +
public fun new(bytes: vector<u8>): bcs::BCS
+
+ + + + + +## Function `into_remainder_bytes` + +Unpack the BCS struct returning the leftover bytes. +Useful for passing the data further after partial deserialization. + + +
public fun into_remainder_bytes(bcs: bcs::BCS): vector<u8>
+
+ + + ## Function `peel_address` @@ -186,7 +230,7 @@ The request Move type is not match with input Move type. Read address value from the bcs-serialized bytes. -
public fun peel_address(v: vector<u8>): address
+
public fun peel_address(bcs: &mut bcs::BCS): address
 
@@ -198,7 +242,7 @@ Read address value from the bcs-serialized bytes. Read a bool value from bcs-serialized bytes. -
public fun peel_bool(v: vector<u8>): bool
+
public fun peel_bool(bcs: &mut bcs::BCS): bool
 
@@ -210,7 +254,7 @@ Read a bool value from bcs-serialized bytes. Read u8 value from bcs-serialized bytes. -
public fun peel_u8(v: vector<u8>): u8
+
public fun peel_u8(bcs: &mut bcs::BCS): u8
 
@@ -222,7 +266,7 @@ Read u8 value from bcs-serialized bytes. Read u16 value from bcs-serialized bytes. -
public fun peel_u16(v: vector<u8>): u16
+
public fun peel_u16(bcs: &mut bcs::BCS): u16
 
@@ -234,7 +278,7 @@ Read u16 value from bcs-serialized bytes. Read u32 value from bcs-serialized bytes. -
public fun peel_u32(v: vector<u8>): u32
+
public fun peel_u32(bcs: &mut bcs::BCS): u32
 
@@ -246,7 +290,7 @@ Read u32 value from bcs-serialized bytes. Read u64 value from bcs-serialized bytes. -
public fun peel_u64(v: vector<u8>): u64
+
public fun peel_u64(bcs: &mut bcs::BCS): u64
 
@@ -258,7 +302,7 @@ Read u64 value from bcs-serialized bytes. Read u128 value from bcs-serialized bytes. -
public fun peel_u128(v: vector<u8>): u128
+
public fun peel_u128(bcs: &mut bcs::BCS): u128
 
@@ -270,7 +314,7 @@ Read u128 value from bcs-serialized bytes. Read u256 value from bcs-serialized bytes. -
public fun peel_u256(v: vector<u8>): u256
+
public fun peel_u256(bcs: &mut bcs::BCS): u256
 
@@ -286,7 +330,7 @@ In BCS vector length is implemented with ULEB128; See more here: https://en.wikipedia.org/wiki/LEB128 -
public fun peel_vec_length(v: vector<u8>): u64
+
public fun peel_vec_length(bcs: &mut bcs::BCS): u64
 
@@ -298,7 +342,7 @@ See more here: https://en.wikipedia.org/wiki/LEB128 Peel a vector of address from serialized bytes. -
public fun peel_vec_address(v: vector<u8>): vector<address>
+
public fun peel_vec_address(bcs: &mut bcs::BCS): vector<address>
 
@@ -310,7 +354,7 @@ Peel a vector of address from serialized bytes. Peel a vector of address from serialized bytes. -
public fun peel_vec_bool(v: vector<u8>): vector<bool>
+
public fun peel_vec_bool(bcs: &mut bcs::BCS): vector<bool>
 
@@ -322,7 +366,7 @@ Peel a vector of address from serialized bytes. Peel a vector of u8 (eg string) from serialized bytes. -
public fun peel_vec_u8(v: vector<u8>): vector<u8>
+
public fun peel_vec_u8(bcs: &mut bcs::BCS): vector<u8>
 
@@ -334,7 +378,7 @@ Peel a vector of u8 (eg string) from serialized bytes. Peel a vector<vector<u8>> (eg vec of string) from serialized bytes. -
public fun peel_vec_vec_u8(v: vector<u8>): vector<vector<u8>>
+
public fun peel_vec_vec_u8(bcs: &mut bcs::BCS): vector<vector<u8>>
 
@@ -346,7 +390,7 @@ Peel a vector<vector<u8>> Peel a vector of u16 from serialized bytes. -
public fun peel_vec_u16(v: vector<u8>): vector<u16>
+
public fun peel_vec_u16(bcs: &mut bcs::BCS): vector<u16>
 
@@ -358,7 +402,7 @@ Peel a vector of u16 from serialized bytes. Peel a vector of u32 from serialized bytes. -
public fun peel_vec_u32(v: vector<u8>): vector<u32>
+
public fun peel_vec_u32(bcs: &mut bcs::BCS): vector<u32>
 
@@ -370,7 +414,7 @@ Peel a vector of u32 from serialized bytes. Peel a vector of u64 from serialized bytes. -
public fun peel_vec_u64(v: vector<u8>): vector<u64>
+
public fun peel_vec_u64(bcs: &mut bcs::BCS): vector<u64>
 
@@ -382,7 +426,7 @@ Peel a vector of u64 from serialized bytes. Peel a vector of u128 from serialized bytes. -
public fun peel_vec_u128(v: vector<u8>): vector<u128>
+
public fun peel_vec_u128(bcs: &mut bcs::BCS): vector<u128>
 
@@ -394,7 +438,7 @@ Peel a vector of u128 from serialized bytes. Peel a vector of u256 from serialized bytes. -
public fun peel_vec_u256(v: vector<u8>): vector<u256>
+
public fun peel_vec_u256(bcs: &mut bcs::BCS): vector<u256>
 
@@ -406,7 +450,7 @@ Peel a vector of u256 from serialized bytes. Peel Option<address> from serialized bytes. -
public fun peel_option_address(v: vector<u8>): option::Option<address>
+
public fun peel_option_address(bcs: &mut bcs::BCS): option::Option<address>
 
@@ -418,7 +462,7 @@ Peel Option<address> from serialized bytes. Peel Option<bool> from serialized bytes. -
public fun peel_option_bool(v: vector<u8>): option::Option<bool>
+
public fun peel_option_bool(bcs: &mut bcs::BCS): option::Option<bool>
 
@@ -430,7 +474,7 @@ Peel Option<bool> from serialized bytes. Peel Option<u8> from serialized bytes. -
public fun peel_option_u8(v: vector<u8>): option::Option<u8>
+
public fun peel_option_u8(bcs: &mut bcs::BCS): option::Option<u8>
 
@@ -442,7 +486,7 @@ Peel Option<u8> from serialized bytes. Peel Option<u16> from serialized bytes. -
public fun peel_option_u16(v: vector<u8>): option::Option<u16>
+
public fun peel_option_u16(bcs: &mut bcs::BCS): option::Option<u16>
 
@@ -454,7 +498,7 @@ Peel Option<u16> from serialized bytes. Peel Option<u32> from serialized bytes. -
public fun peel_option_u32(v: vector<u8>): option::Option<u32>
+
public fun peel_option_u32(bcs: &mut bcs::BCS): option::Option<u32>
 
@@ -466,7 +510,7 @@ Peel Option<u32> from serialized bytes. Peel Option<u64> from serialized bytes. -
public fun peel_option_u64(v: vector<u8>): option::Option<u64>
+
public fun peel_option_u64(bcs: &mut bcs::BCS): option::Option<u64>
 
@@ -478,7 +522,7 @@ Peel Option<u64> from serialized bytes. Peel Option<u128> from serialized bytes. -
public fun peel_option_u128(v: vector<u8>): option::Option<u128>
+
public fun peel_option_u128(bcs: &mut bcs::BCS): option::Option<u128>
 
@@ -490,7 +534,7 @@ Peel Option<u128> from serialized bytes. Peel Option<u256> from serialized bytes. -
public fun peel_option_u256(v: vector<u8>): option::Option<u256>
+
public fun peel_option_u256(bcs: &mut bcs::BCS): option::Option<u256>
 
diff --git a/frameworks/moveos-stdlib/sources/bcs.move b/frameworks/moveos-stdlib/sources/bcs.move index 377a1aa13..bf17bf4c6 100644 --- a/frameworks/moveos-stdlib/sources/bcs.move +++ b/frameworks/moveos-stdlib/sources/bcs.move @@ -21,13 +21,6 @@ module moveos_std::bcs{ const ErrorInvalidBool: u64 = 4; const ErrorOutOfRange: u64 = 5; const ErrorLengthOutOfRange: u64 = 6; - - /// A helper struct that saves resources on operations. For better - /// vector performance, it stores reversed bytes of the BCS and - /// enables use of `vector::pop_back`. - public struct BCS has store, copy, drop { - bytes: vector - } public fun to_bytes(v: &MoveValue): vector{ std::bcs::to_bytes(v) @@ -53,25 +46,36 @@ module moveos_std::bcs{ from_bytes
(v) } + // === BCS struct === + + /// A helper struct that saves resources on operations. For better + /// vector performance, it stores reversed bytes of the BCS and + /// enables use of `vector::pop_back`. + struct BCS has store, copy, drop { + bytes: vector + } + /// Creates a new instance of BCS wrapper that holds inversed /// bytes for better performance. - public fun new(mut bytes: vector): BCS { - bytes.reverse(); + public fun new(bytes: vector): BCS { + vector::reverse(&mut bytes); BCS { bytes } } /// Unpack the `BCS` struct returning the leftover bytes. /// Useful for passing the data further after partial deserialization. public fun into_remainder_bytes(bcs: BCS): vector { - let BCS { mut bytes } = bcs; - bytes.reverse(); + let BCS { bytes } = bcs; + vector::reverse(&mut bytes); bytes } + // === Peel functions === + /// Read `address` value from the bcs-serialized bytes. public fun peel_address(bcs: &mut BCS): address { let bytes = bcs.bytes; - assert!(vector::length(bytes) >= 32, ErrorInvalidLength); + assert!(vector::length(&bytes) >= 32, ErrorInvalidLength); let i = 0; let addr_bytes = vector::empty(); while (i < 32) { @@ -84,8 +88,7 @@ module moveos_std::bcs{ /// Read a `bool` value from bcs-serialized bytes. public fun peel_bool(bcs: &mut BCS): bool { - let bytes = bcs.bytes; - let value = peel_u8(bytes); + let value = peel_u8(bcs); if (value == 0) { return false } else if (value == 1) { @@ -209,26 +212,24 @@ module moveos_std::bcs{ /// Peel a vector of `address` from serialized bytes. public fun peel_vec_address(bcs: &mut BCS): vector
{ - let bytes = bcs.bytes; - let len = peel_vec_length(bytes); + let len = peel_vec_length(bcs); let i = 0; let addresses = vector::empty
(); while (i < len) { - let address = peel_address(bytes); + let address = peel_address(bcs); vector::push_back(&mut addresses, address); i = i + 1; }; addresses } - /// Peel a vector of `address` from serialized bytes. + /// Peel a vector of `bool` from serialized bytes. public fun peel_vec_bool(bcs: &mut BCS): vector { - let bytes = bcs.bytes; - let len = peel_vec_length(bytes); + let len = peel_vec_length(bcs); let i = 0; let bools = vector::empty(); while (i < len) { - let bool = peel_bool(bytes); + let bool = peel_bool(bcs); vector::push_back(&mut bools, bool); i = i + 1; }; @@ -237,12 +238,11 @@ module moveos_std::bcs{ /// Peel a vector of `u8` (eg string) from serialized bytes. public fun peel_vec_u8(bcs: &mut BCS): vector { - let bytes = bcs.bytes; - let len = peel_vec_length(bytes); + let len = peel_vec_length(bcs); let i = 0; let u8s = vector::empty(); while (i < len) { - let u8 = peel_u8(bytes); + let u8 = peel_u8(bcs); vector::push_back(&mut u8s, u8); i = i + 1; }; @@ -251,12 +251,11 @@ module moveos_std::bcs{ /// Peel a `vector>` (eg vec of string) from serialized bytes. public fun peel_vec_vec_u8(bcs: &mut BCS): vector> { - let bytes = bcs.bytes; - let len = peel_vec_length(bytes); + let len = peel_vec_length(bcs); let i = 0; let vec_u8s = vector::empty>(); while (i < len) { - let vec_u8 = peel_vec_u8(bytes); + let vec_u8 = peel_vec_u8(bcs); vector::push_back(&mut vec_u8s, vec_u8); i = i + 1; }; @@ -265,12 +264,11 @@ module moveos_std::bcs{ /// Peel a vector of `u16` from serialized bytes. public fun peel_vec_u16(bcs: &mut BCS): vector { - let bytes = bcs.bytes; - let len = peel_vec_length(bytes); + let len = peel_vec_length(bcs); let i = 0; let u16s = vector::empty(); while (i < len) { - let u16 = peel_u16(bytes); + let u16 = peel_u16(bcs); vector::push_back(&mut u16s, u16); i = i + 1; }; @@ -279,12 +277,11 @@ module moveos_std::bcs{ /// Peel a vector of `u32` from serialized bytes. public fun peel_vec_u32(bcs: &mut BCS): vector { - let bytes = bcs.bytes; - let len = peel_vec_length(bytes); + let len = peel_vec_length(bcs); let i = 0; let u32s = vector::empty(); while (i < len) { - let u32 = peel_u32(bytes); + let u32 = peel_u32(bcs); vector::push_back(&mut u32s, u32); i = i + 1; }; @@ -293,12 +290,11 @@ module moveos_std::bcs{ /// Peel a vector of `u64` from serialized bytes. public fun peel_vec_u64(bcs: &mut BCS): vector { - let bytes = bcs.bytes; - let len = peel_vec_length(bytes); + let len = peel_vec_length(bcs); let i = 0; let u64s = vector::empty(); while (i < len) { - let u64 = peel_u64(bytes); + let u64 = peel_u64(bcs); vector::push_back(&mut u64s, u64); i = i + 1; }; @@ -307,12 +303,11 @@ module moveos_std::bcs{ /// Peel a vector of `u128` from serialized bytes. public fun peel_vec_u128(bcs: &mut BCS): vector { - let bytes = bcs.bytes; - let len = peel_vec_length(bytes); + let len = peel_vec_length(bcs); let i = 0; let u128s = vector::empty(); while (i < len) { - let u128 = peel_u128(bytes); + let u128 = peel_u128(bcs); vector::push_back(&mut u128s, u128); i = i + 1; }; @@ -321,12 +316,11 @@ module moveos_std::bcs{ /// Peel a vector of `u256` from serialized bytes. public fun peel_vec_u256(bcs: &mut BCS): vector { - let bytes = bcs.bytes; - let len = peel_vec_length(bytes); + let len = peel_vec_length(bcs); let i = 0; let u256s = vector::empty(); while (i < len) { - let u256 = peel_u256(bytes); + let u256 = peel_u256(bcs); vector::push_back(&mut u256s, u256); i = i + 1; }; @@ -337,9 +331,8 @@ module moveos_std::bcs{ /// Peel `Option
` from serialized bytes. public fun peel_option_address(bcs: &mut BCS): Option
{ - let bytes = bcs.bytes; - if (peel_bool(bytes)) { - option::some(peel_address(bytes)) + if (peel_bool(bcs)) { + option::some(peel_address(bcs)) } else { option::none() } @@ -347,9 +340,8 @@ module moveos_std::bcs{ /// Peel `Option` from serialized bytes. public fun peel_option_bool(bcs: &mut BCS): Option { - let bytes = bcs.bytes; - if (peel_bool(bytes)) { - option::some(peel_bool(bytes)) + if (peel_bool(bcs)) { + option::some(peel_bool(bcs)) } else { option::none() } @@ -357,9 +349,8 @@ module moveos_std::bcs{ /// Peel `Option` from serialized bytes. public fun peel_option_u8(bcs: &mut BCS): Option { - let bytes = bcs.bytes; - if (peel_bool(bytes)) { - option::some(peel_u8(bytes)) + if (peel_bool(bcs)) { + option::some(peel_u8(bcs)) } else { option::none() } @@ -367,9 +358,8 @@ module moveos_std::bcs{ /// Peel `Option` from serialized bytes. public fun peel_option_u16(bcs: &mut BCS): Option { - let bytes = bcs.bytes; - if (peel_bool(bytes)) { - option::some(peel_u16(bytes)) + if (peel_bool(bcs)) { + option::some(peel_u16(bcs)) } else { option::none() } @@ -377,9 +367,8 @@ module moveos_std::bcs{ /// Peel `Option` from serialized bytes. public fun peel_option_u32(bcs: &mut BCS): Option { - let bytes = bcs.bytes; - if (peel_bool(bytes)) { - option::some(peel_u32(bytes)) + if (peel_bool(bcs)) { + option::some(peel_u32(bcs)) } else { option::none() } @@ -387,9 +376,8 @@ module moveos_std::bcs{ /// Peel `Option` from serialized bytes. public fun peel_option_u64(bcs: &mut BCS): Option { - let bytes = bcs.bytes; - if (peel_bool(bytes)) { - option::some(peel_u64(bytes)) + if (peel_bool(bcs)) { + option::some(peel_u64(bcs)) } else { option::none() } @@ -397,9 +385,8 @@ module moveos_std::bcs{ /// Peel `Option` from serialized bytes. public fun peel_option_u128(bcs: &mut BCS): Option { - let bytes = bcs.bytes; - if (peel_bool(bytes)) { - option::some(peel_u128(bytes)) + if (peel_bool(bcs)) { + option::some(peel_u128(bcs)) } else { option::none() } @@ -407,9 +394,8 @@ module moveos_std::bcs{ /// Peel `Option` from serialized bytes. public fun peel_option_u256(bcs: &mut BCS): Option { - let bytes = bcs.bytes; - if (peel_bool(bytes)) { - option::some(peel_u256(bytes)) + if (peel_bool(bcs)) { + option::some(peel_u256(bcs)) } else { option::none() } @@ -433,7 +419,360 @@ module moveos_std::bcs{ } native public(friend) fun native_from_bytes(bytes: vector): Option; - - // TODO: add test cases for this module. + #[test] + fun test_peel_address_success() { + let bytes = x"7fe695faf7047ccfbc85f7dccb6c405d4e9b7b44788e71a71c3891a06ce0ca12"; + let bcs = new(bytes); + let address = peel_address(&mut bcs); + let expected_addr = @0x7fe695faf7047ccfbc85f7dccb6c405d4e9b7b44788e71a71c3891a06ce0ca12; + assert!(address == expected_addr, ErrorInvalidLength); + } + + #[test] + #[expected_failure(abort_code = ErrorInvalidLength, location = Self)] + fun test_peel_address_fail_with_length() { + let bytes = x"7fe695faf7047ccfbc85f7dccb6c405d4e9b7b44788e71a71c3891a06ce0"; + let bcs = new(bytes); + peel_address(&mut bcs); + } + + #[test] + fun test_peel_bool_success() { + let bytes = x"00"; + let bcs = new(bytes); + let bool = peel_bool(&mut bcs); + let expected_bool = false; + assert!(bool == expected_bool, ErrorInvalidBool); + } + + #[test] + #[expected_failure(abort_code = ErrorOutOfRange, location = Self)] + fun test_peel_bool_fail_with_range() { + let bytes = x""; + let bcs = new(bytes); + peel_bool(&mut bcs); + } + + #[test] + #[expected_failure(abort_code = ErrorInvalidBool, location = Self)] + fun test_peel_bool_fail_with_boolean() { + let bytes = x"22"; + let bcs = new(bytes); + peel_bool(&mut bcs); + } + + #[test] + fun test_peel_u8_success() { + let bytes = x"11"; + let bcs = new(bytes); + let u8 = peel_u8(&mut bcs); + let expected_u8 = 17; + assert!(u8 == expected_u8, ErrorOutOfRange); + } + + #[test] + #[expected_failure(abort_code = ErrorOutOfRange, location = Self)] + fun test_peel_u8_fail_with_range() { + let bytes = x""; + let bcs = new(bytes); + peel_u8(&mut bcs); + } + + #[test] + fun test_peel_u16_success() { + let bytes = x"0011"; + let bcs = new(bytes); + let u16 = peel_u16(&mut bcs); + let expected_u16 = 4352; + assert!(u16 == expected_u16, ErrorOutOfRange); + } + + #[test] + #[expected_failure(abort_code = ErrorOutOfRange, location = Self)] + fun test_peel_u16_fail_with_range() { + let bytes = x"11"; + let bcs = new(bytes); + peel_u16(&mut bcs); + } + + #[test] + fun test_peel_u32_success() { + let bytes = x"00001111"; + let bcs = new(bytes); + let u32 = peel_u32(&mut bcs); + let expected_u32 = 286326784; + assert!(u32 == expected_u32, ErrorOutOfRange); + } + + #[test] + #[expected_failure(abort_code = ErrorOutOfRange, location = Self)] + fun test_peel_u32_fail_with_range() { + let bytes = x"000011"; + let bcs = new(bytes); + peel_u32(&mut bcs); + } + + #[test] + fun test_peel_u64_success() { + let bytes = x"0000000011111111"; + let bcs = new(bytes); + let u64 = peel_u64(&mut bcs); + let expected_u64 = 1229782937960972288; + assert!(u64 == expected_u64, ErrorOutOfRange); + } + + #[test] + #[expected_failure(abort_code = ErrorOutOfRange, location = Self)] + fun test_peel_u64_fail_with_range() { + let bytes = x"00000000111111"; + let bcs = new(bytes); + peel_u64(&mut bcs); + } + + #[test] + fun test_peel_u128_success() { + let bytes = x"00000000111111110000000011111111"; + let bcs = new(bytes); + let u128 = peel_u128(&mut bcs); + let expected_u128 = 22685491122780686731170467593842589696; + assert!(u128 == expected_u128, ErrorOutOfRange); + } + + #[test] + #[expected_failure(abort_code = ErrorOutOfRange, location = Self)] + fun test_peel_u128_fail_with_range() { + let bytes = x"000000001111111100000000111111"; + let bcs = new(bytes); + peel_u128(&mut bcs); + } + + #[test] + fun test_peel_u256_success() { + let bytes = x"0000000011111111000000001111111100000000111111110000000011111111"; + let bcs = new(bytes); + let u256 = peel_u256(&mut bcs); + let expected_u256 = 7719472614023749917513163129863071459539701524256607433418042731035017347072; + assert!(u256 == expected_u256, ErrorOutOfRange); + } + + #[test] + #[expected_failure(abort_code = ErrorOutOfRange, location = Self)] + fun test_peel_u256_fail_with_range() { + let bytes = x"00000000111111110000000011111111000000001111111100000000111111"; + let bcs = new(bytes); + peel_u256(&mut bcs); + } + + #[test] + fun test_peel_vec_length_success() { + let bytes = x"11"; + let bcs = new(bytes); + let vec_len = peel_vec_length(&mut bcs); + let expected_vec_len = 17; + assert!(vec_len == expected_vec_len, ErrorLengthOutOfRange); + } + + #[test] + fun test_peel_vec_address_success() { + let bytes = x"7fe695faf7047ccfbc85f7dccb6c405d4e9b7b44788e71a71c3891a06ce0ca12"; + let bcs = new(bytes); + let vec_address = peel_vec_address(&mut bcs); + let expected_vec_address = vector::empty
(); + let expected_address = @0x7fe695faf7047ccfbc85f7dccb6c405d4e9b7b44788e71a71c3891a06ce0ca12; + let i = 0; + while (i < vector::length(&vec_address)) { + vector::push_back(&mut expected_vec_address, expected_address); + i = i + 1; + }; + assert!(vec_address == expected_vec_address, ErrorLengthOutOfRange); + } + + #[test] + fun test_peel_vec_bool_success() { + let bytes = x"01"; + let bcs = new(bytes); + let vec_bool = peel_vec_bool(&mut bcs); + let expected_vec_bool = vector::empty(); + let expected_bool = true; + vector::push_back(&mut expected_vec_bool, expected_bool); + assert!(vec_bool == expected_vec_bool, ErrorLengthOutOfRange); + } + + #[test] + fun test_peel_vec_u8_success() { + let bytes = x"11"; + let bcs = new(bytes); + let u8 = peel_vec_u8(&mut bcs); + let expected_vec_u8 = x"1111111111111111111111111111111111"; + assert!(u8 == expected_vec_u8, ErrorLengthOutOfRange); + } + + #[test] + fun test_peel_vec_vec_u8_success() { + let bytes = x"11"; + let bcs = new(bytes); + let vec_vec_u8 = peel_vec_vec_u8(&mut bcs); + let expected_vec_vec_u8 = vector::empty>(); + let expected_vec_u8 = x"1111111111111111111111111111111111"; + let i = 0; + while (i < vector::length(&vec_vec_u8)) { + vector::push_back(&mut expected_vec_vec_u8, expected_vec_u8); + i = i + 1; + }; + assert!(vec_vec_u8 == expected_vec_vec_u8, ErrorLengthOutOfRange); + } + + #[test] + fun test_peel_vec_u16_success() { + let bytes = x"1111"; + let bcs = new(bytes); + let vec_u16 = peel_vec_u16(&mut bcs); + let expected_vec_u16 = vector::empty(); + let expected_u16 = 4369u16; + let i = 0; + while (i < vector::length(&vec_u16)) { + vector::push_back(&mut expected_vec_u16, expected_u16); + i = i + 1; + }; + assert!(vec_u16 == expected_vec_u16, ErrorLengthOutOfRange); + } + + #[test] + fun test_peel_vec_u32_success() { + let bytes = x"11111111"; + let bcs = new(bytes); + let vec_u32 = peel_vec_u32(&mut bcs); + let expected_vec_u32 = vector::empty(); + let expected_u32 = 286331153u32; + let i = 0; + while (i < vector::length(&vec_u32)) { + vector::push_back(&mut expected_vec_u32, expected_u32); + i = i + 1; + }; + assert!(vec_u32 == expected_vec_u32, ErrorLengthOutOfRange); + } + + #[test] + fun test_peel_vec_u64_success() { + let bytes = x"1111111111111111"; + let bcs = new(bytes); + let vec_u64 = peel_vec_u64(&mut bcs); + let expected_vec_u64 = vector::empty(); + let expected_u64 = 1229782938247303441u64; + let i = 0; + while (i < vector::length(&vec_u64)) { + vector::push_back(&mut expected_vec_u64, expected_u64); + i = i + 1; + }; + assert!(vec_u64 == expected_vec_u64, ErrorLengthOutOfRange); + } + + #[test] + fun test_peel_vec_u128_success() { + let bytes = x"11111111111111111111111111111111"; + let bcs = new(bytes); + let vec_u128 = peel_vec_u128(&mut bcs); + let expected_vec_u128 = vector::empty(); + let expected_u128 = 22685491128062564230891640495451214097u128; + let i = 0; + while (i < vector::length(&vec_u128)) { + vector::push_back(&mut expected_vec_u128, expected_u128); + i = i + 1; + }; + assert!(vec_u128 == expected_vec_u128, ErrorLengthOutOfRange); + } + + #[test] + fun test_peel_vec_u256_success() { + let bytes = x"1111111111111111111111111111111111111111111111111111111111111111"; + let bcs = new(bytes); + let vec_u256 = peel_vec_u256(&mut bcs); + let expected_vec_u256 = vector::empty(); + let expected_u256 = 7719472615821079694904732333912527190217998977709370935963838933860875309329u256; + let i = 0; + while (i < vector::length(&vec_u256)) { + vector::push_back(&mut expected_vec_u256, expected_u256); + i = i + 1; + }; + assert!(vec_u256 == expected_vec_u256, ErrorLengthOutOfRange); + } + + #[test] + fun test_peel_option_address_success() { + let bytes = x"017fe695faf7047ccfbc85f7dccb6c405d4e9b7b44788e71a71c3891a06ce0ca12"; + let bcs = new(bytes); + let option_address = peel_option_address(&mut bcs); + let expected_option_addr = option::some(@0x17fe695faf7047ccfbc85f7dccb6c405d4e9b7b44788e71a71c3891a06ce0ca); + assert!(option_address == expected_option_addr, ErrorInvalidLength); + } + + #[test] + fun test_peel_option_bool_success() { + let bytes = x"01"; + let bcs = new(bytes); + let option_bool = peel_option_bool(&mut bcs); + let expected_option_bool = option::some(true); + assert!(option_bool == expected_option_bool, ErrorInvalidBool); + } + + #[test] + fun test_peel_option_u8_success() { + let bytes = x"0101"; + let bcs = new(bytes); + let option_u8 = peel_option_u8(&mut bcs); + std::debug::print(&option_u8); + let expected_option_u8 = option::some(1); + assert!(option_u8 == expected_option_u8, ErrorOutOfRange); + } + + #[test] + fun test_peel_option_u16_success() { + let bytes = x"010011"; + let bcs = new(bytes); + let option_u16 = peel_option_u16(&mut bcs); + std::debug::print(&option_u16); + let expected_option_u16 = option::some(1); + assert!(option_u16 == expected_option_u16, ErrorOutOfRange); + } + + #[test] + fun test_peel_option_u32_success() { + let bytes = x"0100001111"; + let bcs = new(bytes); + let option_u32 = peel_option_u32(&mut bcs); + std::debug::print(&option_u32); + let expected_option_u32 = option::some(285212673); + assert!(option_u32 == expected_option_u32, ErrorOutOfRange); + } + + #[test] + fun test_peel_option_u64_success() { + let bytes = x"010000000011111111"; + let bcs = new(bytes); + let option_u64 = peel_option_u64(&mut bcs); + std::debug::print(&option_u64); + let expected_option_u64 = option::some(1229782864946528257); + assert!(option_u64 == expected_option_u64, ErrorOutOfRange); + } + + #[test] + fun test_peel_option_u128_success() { + let bytes = x"0100000000111111110000000011111111"; + let bcs = new(bytes); + let option_u128 = peel_option_u128(&mut bcs); + std::debug::print(&option_u128); + let expected_option_u128 = option::some(22685489775901924302271377683643367425); + assert!(option_u128 == expected_option_u128, ErrorOutOfRange); + } + + #[test] + fun test_peel_option_u256_success() { + let bytes = x"010000000011111111000000001111111100000000111111110000000011111111"; + let bcs = new(bytes); + let option_u256 = peel_option_u256(&mut bcs); + std::debug::print(&option_u256); + let expected_option_u256 = option::some(7719472155704656682663016097251860136573850893801914284240011010441236971521); + assert!(option_u256 == expected_option_u256, ErrorOutOfRange); + } }