Skip to content

Replace cairo programs in types tests #1037

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

Closed
wants to merge 2 commits into from
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
90 changes: 76 additions & 14 deletions src/types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,24 +404,86 @@ fn build_drop<'ctx>(

#[cfg(test)]
mod test {
use crate::{
utils::test::{load_cairo, run_program},
values::Value,
};
use crate::{utils::test::run_sierra_program, values::Value};
use cairo_lang_sierra::ProgramParser;
use pretty_assertions_sorted::assert_eq;

#[test]
fn test_array_snapshot_deep_clone() {
let program = load_cairo! {
fn run_test() -> @Array<Array<felt252>> {
let mut inputs: Array<Array<felt252>> = ArrayTrait::new();
inputs.append(array![1, 2, 3]);
inputs.append(array![4, 5, 6]);

@inputs
}
};
let result = run_program(&program, "run_test", &[]).return_value;
// fn run_test() -> @Array<Array<felt252>> {
// let mut inputs: Array<Array<felt252>> = ArrayTrait::new();
// inputs.append(array![1, 2, 3]);
// inputs.append(array![4, 5, 6]);

// @inputs
// }
let program = ProgramParser::new()
.parse(
r#"
type [2] = Array<[1]> [storable: true, drop: true, dup: false, zero_sized: false];
type [3] = Snapshot<[2]> [storable: true, drop: true, dup: true, zero_sized: false];
type [9] = Const<[0], 6> [storable: false, drop: false, dup: false, zero_sized: false];
type [8] = Const<[0], 5> [storable: false, drop: false, dup: false, zero_sized: false];
type [7] = Const<[0], 4> [storable: false, drop: false, dup: false, zero_sized: false];
type [6] = Const<[0], 3> [storable: false, drop: false, dup: false, zero_sized: false];
type [5] = Const<[0], 2> [storable: false, drop: false, dup: false, zero_sized: false];
type [4] = Const<[0], 1> [storable: false, drop: false, dup: false, zero_sized: false];
type [0] = felt252 [storable: true, drop: true, dup: true, zero_sized: false];
type [1] = Array<[0]> [storable: true, drop: true, dup: false, zero_sized: false];

libfunc [3] = array_new<[1]>;
libfunc [2] = array_new<[0]>;
libfunc [5] = const_as_immediate<[4]>;
libfunc [13] = store_temp<[0]>;
libfunc [1] = array_append<[0]>;
libfunc [6] = const_as_immediate<[5]>;
libfunc [7] = const_as_immediate<[6]>;
libfunc [14] = store_temp<[1]>;
libfunc [0] = array_append<[1]>;
libfunc [8] = const_as_immediate<[7]>;
libfunc [9] = const_as_immediate<[8]>;
libfunc [10] = const_as_immediate<[9]>;
libfunc [11] = snapshot_take<[2]>;
libfunc [12] = drop<[2]>;
libfunc [15] = store_temp<[3]>;

[3]() -> ([0]); // 0
[2]() -> ([1]); // 1
[5]() -> ([2]); // 2
[13]([2]) -> ([2]); // 3
[1]([1], [2]) -> ([3]); // 4
[6]() -> ([4]); // 5
[13]([4]) -> ([4]); // 6
[1]([3], [4]) -> ([5]); // 7
[7]() -> ([6]); // 8
[13]([6]) -> ([6]); // 9
[1]([5], [6]) -> ([7]); // 10
[14]([7]) -> ([7]); // 11
[0]([0], [7]) -> ([8]); // 12
[2]() -> ([9]); // 13
[8]() -> ([10]); // 14
[13]([10]) -> ([10]); // 15
[1]([9], [10]) -> ([11]); // 16
[9]() -> ([12]); // 17
[13]([12]) -> ([12]); // 18
[1]([11], [12]) -> ([13]); // 19
[10]() -> ([14]); // 20
[13]([14]) -> ([14]); // 21
[1]([13], [14]) -> ([15]); // 22
[14]([15]) -> ([15]); // 23
[0]([8], [15]) -> ([16]); // 24
[11]([16]) -> ([17], [18]); // 25
[12]([17]) -> (); // 26
[15]([18]) -> ([18]); // 27
return([18]); // 28

[0]@0() -> ([3]);
"#,
)
.map_err(|e| e.to_string())
.unwrap();

let result = run_sierra_program(program, &[]).return_value;

assert_eq!(
result,
Expand Down
29 changes: 19 additions & 10 deletions src/types/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,11 @@ pub fn get_type_for_variants<'ctx>(

#[cfg(test)]
mod test {
use crate::{metadata::MetadataStorage, types::TypeBuilder, utils::test::load_cairo};
use crate::{metadata::MetadataStorage, types::TypeBuilder};
use cairo_lang_sierra::{
extensions::core::{CoreLibfunc, CoreType},
program_registry::ProgramRegistry,
ProgramParser,
};
use melior::{
ir::{r#type::IntegerType, Location, Module},
Expand All @@ -805,15 +806,23 @@ mod test {

#[test]
fn enum_type_single_variant_no_i0() {
let (_, program) = load_cairo! {
enum MyEnum {
A: felt252,
}

fn run_program(x: MyEnum) -> MyEnum {
x
}
};
// enum MyEnum {
// A: felt252,
// }
// fn run_program(x: MyEnum) -> MyEnum {
// x
// }
let program = ProgramParser::new().parse(r#"
type [0] = felt252 [storable: true, drop: true, dup: true, zero_sized: false];
type [1] = Enum<ut@program::program::MyEnum, [0]> [storable: true, drop: true, dup: true, zero_sized: false];

libfunc [0] = store_temp<[1]>;

[0]([0]) -> ([0]); // 0
return([0]); // 1

[0]@0([0]: [1]) -> ([1]);
"#).map_err(|e| e.to_string()).unwrap();

let context = Context::new();
let registry = ProgramRegistry::<CoreType, CoreLibfunc>::new(&program).unwrap();
Expand Down
Loading
Loading