Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let mut housekeeping #270

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/bytes/src/bytes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,8 @@ impl BytesImpl of BytesTrait {
// if value in two elements, read them and join them
let (element_index, element_offset) = BytesTrait::locate(offset);
let value_in_one_element = element_offset + size <= BYTES_PER_ELEMENT;
let mut value = 0;
if value_in_one_element {
value =
read_sub_u128(*self.data[element_index], BYTES_PER_ELEMENT, element_offset, size);
let value = if value_in_one_element {
read_sub_u128(*self.data[element_index], BYTES_PER_ELEMENT, element_offset, size)
} else {
let (_, end_element_offset) = BytesTrait::locate(offset + size);
let left = read_sub_u128(
Expand All @@ -207,8 +205,8 @@ impl BytesImpl of BytesTrait {
let right = read_sub_u128(
*self.data[element_index + 1], BYTES_PER_ELEMENT, 0, end_element_offset
);
value = u128_join(left, right, end_element_offset);
}
u128_join(left, right, end_element_offset)
};
(offset + size, value)
}

Expand Down
77 changes: 39 additions & 38 deletions src/bytes/src/tests/test_bytes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn test_bytes_update() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_u128_packed() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand Down Expand Up @@ -111,7 +111,7 @@ fn test_bytes_read_u128_packed() {
#[available_gas(20000000)]
#[should_panic(expected: ('out of bound',))]
fn test_bytes_read_u128_packed_out_of_bound() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -126,7 +126,7 @@ fn test_bytes_read_u128_packed_out_of_bound() {
#[available_gas(20000000)]
#[should_panic(expected: ('too large',))]
fn test_bytes_read_u128_packed_too_large() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -140,7 +140,7 @@ fn test_bytes_read_u128_packed_too_large() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_u128_array_packed() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -166,7 +166,7 @@ fn test_bytes_read_u128_array_packed() {
#[available_gas(20000000)]
#[should_panic(expected: ('out of bound',))]
fn test_bytes_read_u128_array_packed_out_of_bound() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -181,7 +181,7 @@ fn test_bytes_read_u128_array_packed_out_of_bound() {
#[available_gas(20000000)]
#[should_panic(expected: ('too large',))]
fn test_bytes_read_u128_array_packed_too_large() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -195,7 +195,7 @@ fn test_bytes_read_u128_array_packed_too_large() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_felt252_packed() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -212,7 +212,7 @@ fn test_bytes_read_felt252_packed() {
#[available_gas(20000000)]
#[should_panic(expected: ('out of bound',))]
fn test_bytes_read_felt252_packed_out_of_bound() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -227,7 +227,7 @@ fn test_bytes_read_felt252_packed_out_of_bound() {
#[available_gas(20000000)]
#[should_panic(expected: ('too large',))]
fn test_bytes_read_felt252_packed_too_large() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -241,7 +241,7 @@ fn test_bytes_read_felt252_packed_too_large() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_u8() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -257,7 +257,7 @@ fn test_bytes_read_u8() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_u16() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -273,7 +273,7 @@ fn test_bytes_read_u16() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_u32() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -289,7 +289,7 @@ fn test_bytes_read_u32() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_usize() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -305,7 +305,7 @@ fn test_bytes_read_usize() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_u64() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -321,7 +321,7 @@ fn test_bytes_read_u64() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_u128() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -337,7 +337,7 @@ fn test_bytes_read_u128() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_u256() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x01020304050607080910111213141516,
0x01020304050607080910000000000000
Expand All @@ -354,7 +354,7 @@ fn test_bytes_read_u256() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_u256_array() {
let mut array = array![
let array = array![
0x01020304050607080910111213141516,
0x16151413121110090807060504030201,
0x16151413121110090807060504030201,
Expand All @@ -378,10 +378,11 @@ fn test_bytes_read_u256_array() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_address() {
let mut array = array![];
array.append(0x01020304050607080910111213140154);
array.append(0x01855d7796176b05d160196ff92381eb);
array.append(0x7910f5446c2e0e04e13db2194a4f0000);
let array = array![
0x01020304050607080910111213140154,
0x01855d7796176b05d160196ff92381eb,
0x7910f5446c2e0e04e13db2194a4f0000,
];

let bytes = BytesTrait::new(46, array);
let address = 0x015401855d7796176b05d160196ff92381eb7910f5446c2e0e04e13db2194a4f;
Expand All @@ -394,10 +395,11 @@ fn test_bytes_read_address() {
#[test]
#[available_gas(20000000)]
fn test_bytes_read_bytes() {
let mut array = array![];
array.append(0x01020304050607080910111213140154);
array.append(0x01855d7796176b05d160196ff92381eb);
array.append(0x7910f5446c2e0e04e13db2194a4f0000);
let array = array![
0x01020304050607080910111213140154,
0x01855d7796176b05d160196ff92381eb,
0x7910f5446c2e0e04e13db2194a4f0000,
];

let bytes = BytesTrait::new(46, array);

Expand Down Expand Up @@ -544,7 +546,7 @@ fn test_bytes_concat() {
];
let mut bytes = BytesTrait::new(117, array);

let mut array: Array<u128> = array![
let array: Array<u128> = array![
0x01020304050607080910111213140154,
0x01855d7796176b05d160196ff92381eb,
0x7910f5446c2e0e04e13db2194a4f0000
Expand Down Expand Up @@ -656,24 +658,23 @@ fn test_bytes_sha256() {

// u256{low: 1, high: 0}
// 0x0000000000000000000000000000000000000000000000000000000000000001
let mut array = array![];
array.append(0);
array.append(1);
let array = array![0, 1];
let bytes: Bytes = BytesTrait::new(32, array);
let res = bytes.sha256();
let hash: u256 = 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5;
assert(res == hash, 'bytes_sha256_1');

// test_bytes_append bytes
let mut array = array![];
array.append(0x10111213141516171810111213141516);
array.append(0x17180101020102030400000001000003);
array.append(0x04050607080000000000000010111213);
array.append(0x14151617180000000000000001020304);
array.append(0x05060708090000000000000000000102);
array.append(0x0304050607015401855d7796176b05d1);
array.append(0x60196ff92381eb7910f5446c2e0e04e1);
array.append(0x3db2194a4f0000000000000000000000);
let array = array![
0x10111213141516171810111213141516,
0x17180101020102030400000001000003,
0x04050607080000000000000010111213,
0x14151617180000000000000001020304,
0x05060708090000000000000000000102,
0x0304050607015401855d7796176b05d1,
0x60196ff92381eb7910f5446c2e0e04e1,
0x3db2194a4f0000000000000000000000,
];

let bytes: Bytes = BytesTrait::new(117, array);

Expand Down
Loading
Loading