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

hotfix: list example #129

Merged
merged 6 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
added list exmaples and tests
  • Loading branch information
mohiiit authored and julio4 committed Nov 27, 2023
commit 747de239f62cb60304896894fd609802df9d27d2
4 changes: 4 additions & 0 deletions listings/ch00-getting-started/cairo_cheatsheet/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ mod loop_example;
mod match_example;
mod struct_example;
mod type_casting_example;
mod list_example;

#[cfg(test)]
mod tests;
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#[starknet::contract]
mod listExample {
use alexandria_storage::list::{List, ListTrait};

#[storage]
struct Storage {
amount: List<u128>,
tasks: List<Task>
}

#[derive(Copy, Drop, Serde, starknet::Store)]
struct Task {
description: felt252,
status: felt252
}


#[external(v0)]
#[generate_trait]
impl external of externalTrait {
fn add_in_amount(ref self: ContractState, number: u128) {
let mut current_amount_list = self.amount.read();
current_amount_list.append(number);
}

fn add_in_task(ref self: ContractState, description: felt252, status: felt252) {
let new_task = Task { description: description, status: status };
let mut current_tasks_list = self.tasks.read();
current_tasks_list.append(new_task);
}

fn is_empty_list(ref self: ContractState) -> bool {
let mut current_amount_list = self.amount.read();
current_amount_list.is_empty()
}

fn list_length(ref self: ContractState) -> u32 {
let mut current_amount_list = self.amount.read();
current_amount_list.len()
}

fn get_from_index(ref self: ContractState, index: u32) -> u128 {
self.amount.read()[index]
}

fn set_from_index(ref self: ContractState, index: u32, number: u128) {
let mut current_amount_list = self.amount.read();
current_amount_list.set(index, number);
}

fn pop_front_list(ref self: ContractState) {
let mut current_amount_list = self.amount.read();
current_amount_list.pop_front();
}

fn array_conversion(ref self: ContractState) -> Array<u128> {
let mut current_amount_list = self.amount.read();
current_amount_list.array()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod test_list_example;
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use core::array::ArrayTrait;
use cairo_cheatsheet::list_example::listExample::{
externalTrait, Task, amountContractMemberStateTrait, tasksContractMemberStateTrait
};
use cairo_cheatsheet::list_example::listExample;

fn STATE() -> listExample::ContractState {
listExample::contract_state_for_testing()
}

#[test]
#[available_gas(2000000)]
fn test_add_in_amount() {
let mut state = STATE();
state.add_in_amount(200);
assert(state.amount.read()[0] == 200, 'should be 200');
}

#[test]
#[available_gas(2000000)]
fn test_add_in_task() {
let mut state = STATE();
state.add_in_task('test_description', 'test_status');
let current_task: Task = state.tasks.read()[0];
assert(current_task.description == 'test_description', 'should be test_description');
assert(current_task.status == 'test_status', 'should be test_status');
}

#[test]
#[available_gas(2000000)]
fn test_is_empty_list() {
let mut state = STATE();

let pre_addition = state.is_empty_list();
assert(pre_addition==true, 'should be true');

state.add_in_amount(200);
let post_addition = state.is_empty_list();
assert(post_addition==false, 'should be false');
}

#[test]
#[available_gas(2000000)]
fn test_list_length() {
let mut state = STATE();

let pre_addition = state.list_length();
assert(pre_addition==0, 'should be zero');

state.add_in_amount(200);
let post_addition = state.list_length();
assert(post_addition==1, 'should be 1');
}

#[test]
#[available_gas(2000000)]
fn test_get_from_index() {
let mut state = STATE();
state.add_in_amount(200);
let output = state.get_from_index(0);
assert(output == 200, 'should be 200');
}

#[test]
#[available_gas(2000000)]
fn test_set_from_index() {
let mut state = STATE();
state.add_in_amount(200);
state.set_from_index(0, 400);
assert(state.amount.read()[0] == 400, 'should be 400');
}

#[test]
#[available_gas(2000000)]
fn test_pop_front_list() {
let mut state = STATE();

state.add_in_amount(200);
let pre_pop_front = state.list_length();
assert(pre_pop_front==1, 'should be 1');

state.pop_front_list();
let post_pop_front = state.list_length();
assert(post_pop_front==0, 'should be zero');
}

#[test]
#[available_gas(2000000)]
fn test_array_conversion() {
let mut ideal_array = ArrayTrait::<u128>::new();
ideal_array.append(200);
ideal_array.append(400);

let mut state = STATE();

state.add_in_amount(200);
state.add_in_amount(400);
let output: Array<u128> = state.array_conversion();

assert(output==ideal_array, 'should be equal');
}