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

tests: Added tests for SerializableFelt252Dict #573

Merged
Merged
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
4 changes: 2 additions & 2 deletions src/utils/serializable_dict.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ impl ItemImpl<T> of ItemTrait<T> {
fn unwrap_single<impl TCopy: Copy<T>>(self: @Item<T>) -> T {
match self {
Item::Single(v) => (*v),
Item::Span(arr) => panic_with_felt252('should not be a span')
Item::Span(arr) => panic_with_felt252('should be single')
}
}

fn unwrap_span(self: @Item<T>) -> Span<T> {
match self {
Item::Single(v) => panic_with_felt252('should not be single'),
Item::Single(v) => panic_with_felt252('should be a span'),
Item::Span(arr) => *arr
}
}
Expand Down
101 changes: 101 additions & 0 deletions tests/utils/test_serializable_dict.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,74 @@ fn test_item_span() {
assert(item.len() == expected_len, 'incorrect len');
}

#[test]
fn test_item_comparison_single_equals() {
let item_a: Item<u128> = Item::Single(42);
let item_b: Item<u128> = Item::Single(42);
assert(item_a == item_b, 'u128 should be equals');

let item_a: Item<u128> = Item::Single(42);
let item_b: Item<u128> = Item::Single(69);
assert(item_a != item_b, 'u128 shouldnt be equals');

let item_a: Item<felt252> = Item::Single(69);
let item_b: Item<felt252> = Item::Single(69);
assert(item_a == item_b, 'felt252 should be equals');

let item_a: Item<felt252> = Item::Single(42);
let item_b: Item<felt252> = Item::Single(69);
assert(item_a != item_b, 'felt252 shouldnt be equals');
}

#[test]
fn test_item_comparison_spans() {
let item_a: Item<u128> = Item::Span(array![1, 2, 3].span());
let item_b: Item<u128> = Item::Span(array![1, 2, 3].span());
assert(item_a == item_b, 'u128 should be equals');

let item_a: Item<u128> = Item::Span(array![1, 2, 3].span());
let item_b: Item<u128> = Item::Span(array![4, 5].span());
assert(item_a != item_b, 'u128 shouldnt be equals');

let item_a: Item<felt252> = Item::Span(array![1, 2, 3].span());
let item_b: Item<felt252> = Item::Span(array![1, 2, 3].span());
assert(item_a == item_b, 'felt252 should be equals');

let item_a: Item<felt252> = Item::Span(array![1, 2, 3].span());
let item_b: Item<felt252> = Item::Span(array![1, 2, 9].span());
assert(item_a != item_b, 'felt252 shouldnt be equals');

let item_a: Item<ContractAddress> = Item::Span(
array![contract_address_const::<'satoshi'>(), contract_address_const::<'nakamoto'>()].span()
);
let item_b: Item<ContractAddress> = Item::Span(
array![contract_address_const::<'satoshi'>(), contract_address_const::<'nakamoto'>()].span()
);
assert(item_a == item_b, 'contract should be equals');

let item_a: Item<ContractAddress> = Item::Span(
array![contract_address_const::<'satoshi'>(), contract_address_const::<'nakamoto'>()].span()
);
let item_b: Item<ContractAddress> = Item::Span(
array![contract_address_const::<'nakamoto'>(), contract_address_const::<'satoshi'>()].span()
);
assert(item_a != item_b, 'contract shouldnt be equals');
}

#[test]
#[should_panic(expected: ('should be a span',))]
fn test_item_unwrap_single_as_span() {
let item: Item<u128> = Item::Single(8);
item.unwrap_span();
}

#[test]
#[should_panic(expected: ('should be single',))]
fn test_item_unwrap_span_as_single() {
let item: Item<u128> = Item::Span(array![1, 2].span());
item.unwrap_single();
}

// SerializableDict tests

#[test]
Expand All @@ -66,6 +134,8 @@ fn test_serializable_dict_insert_single() {
let retrieved_item: Item = dict.get(key).expect('key should be in dict');
let out_value: u128 = retrieved_item.unwrap_single();

assert(dict.contains(key), 'key should be in dict');
assert(dict.len() == 1, 'wrong dict len');
assert(out_value == expected_value, 'wrong value');
}

Expand All @@ -82,6 +152,7 @@ fn test_serializable_dict_insert_span() {
let out_span: Span<u128> = retrieved_item.unwrap_span();

assert(dict.contains(key), 'key should be in dict');
assert(dict.len() == 1, 'wrong dict len');
assert(out_span.at(0) == expected_array.at(0), 'wrong at idx 0');
assert(out_span.at(1) == expected_array.at(1), 'wrong at idx 1');
assert(out_span.at(2) == expected_array.at(2), 'wrong at idx 2');
Expand All @@ -91,6 +162,9 @@ fn test_serializable_dict_insert_span() {
fn test_serializable_dict_serialize() {
let mut dict: SerializableFelt252Dict<u128> = SerializableFelt252DictTrait::new();

assert(dict.is_empty(), 'dict should be empty');
assert(dict.len() == 0, 'wrong empty dict len');

let expected_value: u128 = 42;
let expected_array: Array<u128> = array![1, 2, 3];

Expand All @@ -115,7 +189,34 @@ fn test_serializable_dict_serialize() {
.get('test_span')
.expect('key should be in dict');
let out_span: Span<u128> = retrieved_item.unwrap_span();
assert(dict.len() == 2, 'wrong deserialized dict len');
assert(dict.contains('test'), 'test should be in dict');
assert(dict.contains('test_span'), 'test should be in dict');
assert(out_span.at(0) == expected_array.at(0), 'wrong at idx 0');
assert(out_span.at(1) == expected_array.at(1), 'wrong at idx 1');
assert(out_span.at(2) == expected_array.at(2), 'wrong at idx 2');
}

#[test]
#[should_panic(expected: ('err getting value',))]
fn test_error_deserialize_value() {
let serialized_dict: Array<felt252> = array!['key', 1, 1, 'key_2', 2, 1];
let mut span_serialized: Span<felt252> = serialized_dict.span();

match SerializableFelt252DictTrait::<u128>::deserialize(ref span_serialized) {
Option::Some(d) => panic_with_felt252('should have panicked'),
Option::None => ()
};
}

#[test]
#[should_panic(expected: ('err getting size',))]
fn test_error_deserialize_size() {
let serialized_dict: Array<felt252> = array!['key', 1, 1, 'key_2'];
let mut span_serialized: Span<felt252> = serialized_dict.span();

match SerializableFelt252DictTrait::<u128>::deserialize(ref span_serialized) {
Option::Some(d) => panic_with_felt252('should have panicked'),
Option::None => ()
};
}