forked from starkware-libs/cairo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.rs
423 lines (407 loc) · 15 KB
/
examples_test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use assert_matches::assert_matches;
use cairo_felt::{felt_str as felt252_str, Felt252};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
use cairo_lang_compiler::project::setup_project;
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_filesystem::db::FilesGroupEx;
use cairo_lang_filesystem::flag::Flag;
use cairo_lang_filesystem::ids::{CrateId, FlagId};
use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId;
use cairo_lang_runner::{token_gas_cost, Arg, RunResultValue, SierraCasmRunner};
use cairo_lang_sierra::extensions::gas::CostTokenType;
use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug;
use cairo_lang_sierra_generator::replace_ids::replace_sierra_ids_in_program;
use cairo_lang_sierra_to_casm::compiler::SierraToCasmConfig;
use cairo_lang_sierra_to_casm::metadata::{calc_metadata, calc_metadata_ap_change_only};
use cairo_lang_test_utils::compare_contents_or_fix_with_path;
use cairo_lang_utils::{extract_matches, Upcast};
use itertools::Itertools;
use rstest::{fixture, rstest};
type ExampleDirData = (Mutex<RootDatabase>, Vec<CrateId>);
/// Setups the cairo lowering to sierra db for the examples crate.
#[fixture]
#[once]
fn example_dir_data() -> ExampleDirData {
let mut db = RootDatabase::builder().detect_corelib().build().unwrap();
let dir = env!("CARGO_MANIFEST_DIR");
// Pop the "/tests" suffix.
let mut path = PathBuf::from(dir).parent().unwrap().to_owned();
path.push("examples");
let crate_ids = setup_project(&mut db, path.as_path()).expect("Project setup failed.");
DiagnosticsReporter::stderr().with_crates(&crate_ids).ensure(&db).unwrap();
(db.into(), crate_ids)
}
#[rstest]
#[allow(unused_variables)]
fn lowering_test(example_dir_data: &ExampleDirData) {}
/// Returns the path of the relevant test file.
fn get_test_data_path(name: &str, test_type: &str) -> PathBuf {
[env!("CARGO_MANIFEST_DIR"), "test_data", &format!("{name}.{test_type}")].into_iter().collect()
}
/// Compares content to examples content, or overrides it if the `CAIRO_FIX_TESTS` environment
/// value is set to `1`.
fn compare_contents_or_fix(name: &str, test_type: &str, content: String) {
let path = get_test_data_path(name, test_type);
compare_contents_or_fix_with_path(&path, content)
}
/// Compiles the Cairo code for submodule `name` of the examples crates to a Sierra program.
fn checked_compile_to_sierra(
name: &str,
(db, crate_ids): &ExampleDirData,
auto_add_withdraw_gas: bool,
) -> cairo_lang_sierra::program::Program {
let mut locked_db = db.lock().unwrap();
let add_withdraw_gas_flag_id = FlagId::new(locked_db.snapshot().upcast(), "add_withdraw_gas");
locked_db.set_flag(
add_withdraw_gas_flag_id,
Some(Arc::new(Flag::AddWithdrawGas(auto_add_withdraw_gas))),
);
let db = locked_db.snapshot();
let mut requested_function_ids = vec![];
for crate_id in crate_ids {
for module_id in db.crate_modules(*crate_id).iter() {
if module_id.full_path(&db) != format!("examples::{name}") {
continue;
}
for (free_func_id, _) in db.module_free_functions(*module_id).unwrap().iter() {
if let Some(function) =
ConcreteFunctionWithBodyId::from_no_generics_free(db.upcast(), *free_func_id)
{
requested_function_ids.push(function)
}
}
}
}
let SierraProgramWithDebug { program: sierra_program, .. } =
Arc::unwrap_or_clone(db.get_sierra_program_for_functions(requested_function_ids).unwrap());
replace_sierra_ids_in_program(&db, &sierra_program)
}
/// Tests lowering from Cairo to Sierra.
#[rstest]
#[case::fib("fib")]
#[case::fib_box("fib_box")]
#[case::fib_array("fib_array")]
#[case::fib_counter("fib_counter")]
#[case::fib_match("fib_match")]
#[case::fib_struct("fib_struct")]
#[case::fib_u128("fib_u128")]
#[case::fib_u128_checked("fib_u128_checked")]
#[case::fib_local("fib_local")]
#[case::fib_loop("fib_loop")]
#[case::fib_unary("fib_unary")]
#[case::enum_flow("enum_flow")]
#[case::corelib_usage("corelib_usage")]
#[case::hash_chain("hash_chain")]
#[case::hash_chain_gas("hash_chain_gas")]
#[case::pedersen_test("pedersen_test")]
#[case::match_or("match_or")]
fn cairo_to_sierra(#[case] name: &str, example_dir_data: &ExampleDirData) {
compare_contents_or_fix(
name,
"sierra",
checked_compile_to_sierra(name, example_dir_data, false).to_string(),
);
}
/// Tests lowering from Cairo to Sierra, with automatic addition of `withdraw_gas` calls.
#[rstest]
#[case::fib("fib")]
fn cairo_to_sierra_auto_gas(#[case] name: &str, example_dir_data: &ExampleDirData) {
compare_contents_or_fix(
&format!("{name}_gas"),
"sierra",
checked_compile_to_sierra(name, example_dir_data, true).to_string(),
);
}
/// Tests lowering from Cairo to casm.
#[rstest]
#[case::fib("fib", false)]
#[case::fib_box("fib_box", false)]
#[case::fib_array("fib_array", false)]
#[case::fib_counter("fib_counter", false)]
#[case::fib_match("fib_match", false)]
#[case::fib_struct("fib_struct", false)]
#[case::fib_u128("fib_u128", false)]
#[case::fib_u128_checked("fib_u128_checked", false)]
#[case::fib_local("fib_local", false)]
#[case::fib_loop("fib_loop", false)]
#[case::fib_unary("fib_unary", false)]
#[case::enum_flow("enum_flow", false)]
#[case::corelib_usage("corelib_usage", false)]
#[case::hash_chain("hash_chain", false)]
#[case::hash_chain_gas("hash_chain_gas", true)]
#[case::pedersen_test("pedersen_test", false)]
#[case::match_or("match_or", false)]
fn cairo_to_casm(
#[case] name: &str,
#[case] gas_usage_check: bool,
example_dir_data: &ExampleDirData,
) {
let program = checked_compile_to_sierra(name, example_dir_data, false);
compare_contents_or_fix(
name,
"casm",
cairo_lang_sierra_to_casm::compiler::compile(
&program,
&if gas_usage_check {
calc_metadata(&program, Default::default()).unwrap()
} else {
calc_metadata_ap_change_only(&program).unwrap()
},
SierraToCasmConfig { gas_usage_check, max_bytecode_size: usize::MAX },
)
.unwrap()
.to_string(),
);
}
/// Tests lowering from Cairo to casm, with automatic addition of `withdraw_gas` calls.
#[rstest]
#[case::fib("fib")]
fn cairo_to_casm_auto_gas(#[case] name: &str, example_dir_data: &ExampleDirData) {
let program = checked_compile_to_sierra(name, example_dir_data, true);
compare_contents_or_fix(
&format!("{name}_gas"),
"casm",
cairo_lang_sierra_to_casm::compiler::compile(
&program,
&calc_metadata(&program, Default::default()).unwrap(),
SierraToCasmConfig { gas_usage_check: true, max_bytecode_size: usize::MAX },
)
.unwrap()
.to_string(),
);
}
fn run_function(
name: &str,
params: &[Felt252],
available_gas: Option<usize>,
expected_cost: Option<usize>,
example_dir_data: &ExampleDirData,
auto_add_withdraw_gas: bool,
) -> RunResultValue {
let runner = SierraCasmRunner::new(
checked_compile_to_sierra(name, example_dir_data, auto_add_withdraw_gas),
if available_gas.is_some() { Some(Default::default()) } else { None },
Default::default(),
None,
)
.expect("Failed setting up runner.");
let result = runner
.run_function_with_starknet_context(
// find first
runner.find_function("").expect("Failed finding the function."),
¶ms.iter().cloned().map(Arg::Value).collect_vec(),
available_gas,
Default::default(),
)
.expect("Failed running the function.");
if let Some(expected_cost) = expected_cost {
assert_eq!(
available_gas.unwrap() - result.gas_counter.as_ref().unwrap(),
Felt252::from(expected_cost)
);
}
result.value
}
#[rstest]
#[case::fib(
"fib",
&[1, 1, 7].map(Felt252::from), None, None,
RunResultValue::Success(vec![Felt252::from(21)])
)]
#[case::fib(
"fib_loop",
&[1, 1, 7].map(Felt252::from), None, None,
RunResultValue::Success(vec![Felt252::from(21)])
)]
#[case::fib_counter(
"fib_counter",
&[1, 1, 8].map(Felt252::from), None, None,
RunResultValue::Success([34, 8].map(Felt252::from).into_iter().collect())
)]
#[case::fib_match(
"fib_match",
&[9].map(Felt252::from), None, None,
RunResultValue::Success([55].map(Felt252::from).into_iter().collect())
)]
#[case::fib_struct(
"fib_struct",
&[1, 1, 9].map(Felt252::from), None, None,
RunResultValue::Success([55, 9].map(Felt252::from).into_iter().collect())
)]
#[case::fib_u128_checked_pass(
"fib_u128_checked",
&[1, 1, 10].map(Felt252::from), None, None,
RunResultValue::Success([/*ok*/0, /*fib*/89].map(Felt252::from).into_iter().collect())
)]
#[case::fib_u128_checked_fail(
"fib_u128_checked",
&[1, 1, 200].map(Felt252::from), None, None,
RunResultValue::Success([/*err*/1, /*padding*/0].map(Felt252::from).into_iter().collect())
)]
#[case::fib_u128_pass(
"fib_u128",
&[1, 1, 10].map(Felt252::from), None, None,
RunResultValue::Success(vec![Felt252::from(89)])
)]
#[case::fib_u128_fail(
"fib_u128",
&[1, 1, 200].map(Felt252::from), None, None,
RunResultValue::Panic(vec![Felt252::from_bytes_be(b"u128_add Overflow")])
)]
#[case::fib_local(
"fib_local",
&[6].map(Felt252::from), None, None,
RunResultValue::Success(vec![Felt252::from(13)])
)]
#[case::fib_unary(
"fib_unary",
&[7].map(Felt252::from), None, None,
RunResultValue::Success(vec![Felt252::from(21)])
)]
#[case::hash_chain(
"hash_chain",
&[3].map(Felt252::from), None, None,
RunResultValue::Success(vec![felt252_str!(
"2dca1ad81a6107a9ef68c69f791bcdbda1df257aab76bd43ded73d96ed6227d", 16)]))]
#[case::hash_chain_gas(
"hash_chain_gas",
&[3].map(Felt252::from), Some(100000), Some(9880 + 3 * token_gas_cost(CostTokenType::Pedersen)),
RunResultValue::Success(vec![felt252_str!(
"2dca1ad81a6107a9ef68c69f791bcdbda1df257aab76bd43ded73d96ed6227d", 16)]))]
fn run_function_test(
#[case] name: &str,
#[case] params: &[Felt252],
#[case] available_gas: Option<usize>,
#[case] expected_cost: Option<usize>,
#[case] expected_result: RunResultValue,
example_dir_data: &ExampleDirData,
) {
pretty_assertions::assert_eq!(
run_function(name, params, available_gas, expected_cost, example_dir_data, false),
expected_result
);
}
#[rstest]
#[case::fib_pass(
"fib",
&[1, 1, 10].map(Felt252::from), Some(200000), None,
RunResultValue::Success([89].map(Felt252::from).into_iter().collect())
)]
#[case::fib_fail(
"fib",
&[1, 1, 10].map(Felt252::from), Some(10000), None,
RunResultValue::Panic(vec![Felt252::from_bytes_be(b"Out of gas")])
)]
fn run_function_auto_gas_test(
#[case] name: &str,
#[case] params: &[Felt252],
#[case] available_gas: Option<usize>,
#[case] expected_cost: Option<usize>,
#[case] expected_result: RunResultValue,
example_dir_data: &ExampleDirData,
) {
pretty_assertions::assert_eq!(
run_function(name, params, available_gas, expected_cost, example_dir_data, true),
expected_result
);
}
#[rstest]
#[case::size_2(2, 1)]
#[case::size_3(3, 2)]
#[case::size_4(4, 3)]
#[case::size_5(5, 5)]
#[case::size_6(6, 8)]
#[case::size_7(7, 13)]
#[case::size_8(8, 21)]
#[case::size_9(9, 34)]
#[case::size_10(10, 55)]
fn run_fib_array_len(#[case] n: usize, #[case] last: usize, example_dir_data: &ExampleDirData) {
assert_matches!(
&extract_matches!(
run_function("fib_array", &[n].map(Felt252::from), None, None, example_dir_data, false),
RunResultValue::Success
)[..],
[_, _, actual_last, actual_len] if actual_last == &Felt252::from(last) && actual_len == &Felt252::from(n)
);
}
#[rstest]
fn complex_input_test(example_dir_data: &ExampleDirData) {
let runner = SierraCasmRunner::new(
checked_compile_to_sierra("complex_input", example_dir_data, false),
None,
Default::default(),
None,
)
.expect("Failed setting up runner.");
let result = runner
.run_function_with_starknet_context(
// find first
runner.find_function("").expect("Failed finding the function."),
&[
// `felt_input`
Arg::Value(Felt252::from(1)),
// `felt_arr_input`
Arg::Array(vec![Arg::Value(Felt252::from(2)), Arg::Value(Felt252::from(3))]),
// `a_input.val.low`
Arg::Value(Felt252::from(4)),
// `a_input.val.high`
Arg::Value(Felt252::from(5)),
// `a_input.arr`
Arg::Array(vec![
// `a_input.arr[0].low`
Arg::Value(Felt252::from(6)),
// `a_input.arr[0].high`
Arg::Value(Felt252::from(7)),
]),
// `a_arr_input`
Arg::Array(vec![
// `a_arr_input[0].val.low`
Arg::Value(Felt252::from(8)),
// `a_arr_input[0].val.high`
Arg::Value(Felt252::from(9)),
// `a_arr_input[0].arr`
Arg::Array(vec![
// `a_arr_input[0].arr[0].low`
Arg::Value(Felt252::from(10)),
// `a_arr_input[0].arr[0].high`
Arg::Value(Felt252::from(11)),
// `a_arr_input[0].arr[1].low`
Arg::Value(Felt252::from(12)),
// `a_arr_input[0].arr[1].high`
Arg::Value(Felt252::from(13)),
// `a_arr_input[0].arr[2].low`
Arg::Value(Felt252::from(14)),
// `a_arr_input[0].arr[2].high`
Arg::Value(Felt252::from(15)),
]),
// `a_arr_input[1].val.low`
Arg::Value(Felt252::from(16)),
// `a_arr_input[1].val.high`
Arg::Value(Felt252::from(17)),
// `a_arr_input[1].arr`
Arg::Array(vec![
// `a_arr_input[1].arr[0].low`
Arg::Value(Felt252::from(18)),
// `a_arr_input[1].arr[0].high`
Arg::Value(Felt252::from(19)),
]),
]),
],
None,
Default::default(),
)
.expect("Failed running the function.");
assert_eq!(
result.value,
RunResultValue::Success(vec![
// `r.low`
Felt252::from(1 + 2 + 3 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18),
// `r.high`
Felt252::from(5 + 7 + 9 + 11 + 13 + 15 + 17 + 19)
])
);
}