Skip to content

Commit

Permalink
feat: Add clean method for list (#182)
Browse files Browse the repository at this point in the history
## Pull Request type

Please check the type of change your PR introduces:

- [ ] Bugfix
- [X] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no API changes)
- [ ] Build-related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

Issue Number:
[#177](#177)

## What is the new behavior?

- Added a clean component to List 
- Added unit test related to this component

## Does this introduce a breaking change?

- [ ] Yes
- [X] No

## Other information
  • Loading branch information
azurwastaken authored Oct 2, 2023
1 parent 7bc7b8a commit 92c3c1b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/storage/src/list.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ trait ListTrait<T> {
fn append(ref self: List<T>, value: T) -> u32;
fn get(self: @List<T>, index: u32) -> Option<T>;
fn set(ref self: List<T>, index: u32, value: T);
fn clean(ref self: List<T>);
fn pop_front(ref self: List<T>) -> Option<T>;
fn array(self: @List<T>) -> Array<T>;
}
Expand Down Expand Up @@ -71,6 +72,11 @@ impl ListImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>, impl TStore: Store<T>
Store::write_at_offset(self.address_domain, base, offset, value).unwrap_syscall();
}

fn clean(ref self: List<T>) {
self.len = 0;
Store::write(self.address_domain, self.base, self.len);
}

fn pop_front(ref self: List<T>) -> Option<T> {
if self.len == 0 {
return Option::None;
Expand Down
56 changes: 56 additions & 0 deletions src/storage/src/tests/list_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trait IAListHolder<TContractState> {
fn do_set(
ref self: TContractState, index: u32, addrs_value: ContractAddress, numbers_value: u256
);
fn do_clean(ref self: TContractState);
fn do_pop_front(ref self: TContractState) -> (Option<ContractAddress>, Option<u256>);
fn do_array(self: @TContractState) -> (Array<ContractAddress>, Array<u256>);
}
Expand Down Expand Up @@ -67,6 +68,13 @@ mod AListHolder {
n.set(index, numbers_value);
}

fn do_clean(ref self: ContractState) {
let mut a = self.addrs.read();
let mut n = self.numbers.read();
a.clean();
n.clean();
}

fn do_pop_front(ref self: ContractState) -> (Option<ContractAddress>, Option<u256>) {
let mut a = self.addrs.read();
let mut n = self.numbers.read();
Expand Down Expand Up @@ -359,4 +367,52 @@ mod tests {
let (array_addr, array_number) = contract.do_array();
assert((array_addr.len(), array_number.len()) == (0, 0), 'lens must be null');
}

#[test]
#[available_gas(100000000)]
fn test_array_clean() {
let contract = deploy_mock();
let mock_addr = mock_addr();

contract.do_append(mock_addr, 100); // idx 0
contract.do_append(mock_addr, 200); // idx 1
contract.do_append(mock_addr, 300); // idx 2
contract.do_clean();
assert(contract.do_get_len() == (0, 0), 'is empty');
}

#[test]
#[available_gas(100000000)]
fn test_array_clean_with_empty_array() {
let contract = deploy_mock();
let mock_addr = mock_addr();

assert(contract.do_get_len() == (0, 0), 'is empty');

contract.do_clean();

assert(contract.do_get_len() == (0, 0), 'is still empty');
}

#[test]
#[available_gas(100000000)]
fn test_array_get_value_after_clean() {
let contract = deploy_mock();
let mock_addr = mock_addr();

contract.do_append(mock_addr, 100); // idx 0
let (addr, number) = contract.do_get(0);
assert(addr.is_some(), 'addr is some');
assert(addr.unwrap() == mock_addr, 'should be mock_addr');
assert(number.is_some(), 'number is some');
assert(number.unwrap() == 100, 'should be 100');

contract.do_clean();

assert(contract.do_get_len() == (0, 0), 'len');

let (addr, number) = contract.do_get(0);
assert(addr.is_none(), 'addr is none');
assert(number.is_none(), 'number is none');
}
}

0 comments on commit 92c3c1b

Please sign in to comment.