Skip to content

Commit 17cc6f3

Browse files
adding fibonacci hints tests (#203)
1 parent 00f9572 commit 17cc6f3

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

crates/cairo-program-runner-lib/src/hints/fibonacci_hints.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,75 @@ pub fn fibonacci_load_claim_idx(
2727
let fibonacci_claim_index: usize = exec_scopes.get(FIBONACCI_CLAIM_INDEX)?;
2828
insert_value_into_ap(vm, fibonacci_claim_index)
2929
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
use cairo_vm::types::relocatable::Relocatable;
35+
use cairo_vm::vm::vm_core::VirtualMachine;
36+
use cairo_vm::Felt252;
37+
38+
fn prepare_vm_for_fibonacci_test(
39+
fibonacci_input: &FibonacciInput,
40+
) -> (VirtualMachine, ExecutionScopes) {
41+
let mut vm = VirtualMachine::new(false, true);
42+
vm.segments.add();
43+
vm.segments.add();
44+
vm.set_ap(0);
45+
vm.set_fp(0);
46+
let mut exec_scopes = ExecutionScopes::new();
47+
exec_scopes.insert_value(
48+
PROGRAM_INPUT,
49+
serde_json::to_string(&fibonacci_input).unwrap(),
50+
);
51+
(vm, exec_scopes)
52+
}
53+
54+
#[test]
55+
fn test_fibonacci_load_second_element() {
56+
// Runs fibonacci_load_second_element hint and asserts that the second element is loaded
57+
// correctly into ap.
58+
59+
let fibonacci_input = FibonacciInput {
60+
second_element: 5,
61+
fibonacci_claim_index: 3,
62+
};
63+
64+
let (mut vm, mut exec_scopes) = prepare_vm_for_fibonacci_test(&fibonacci_input);
65+
let result = fibonacci_load_second_element(&mut vm, &mut exec_scopes);
66+
assert!(result.is_ok());
67+
assert_eq!(vm.get_ap(), Relocatable::from((1, 0)));
68+
assert_eq!(
69+
vm.segments
70+
.memory
71+
.get_integer(vm.get_ap())
72+
.unwrap()
73+
.as_ref(),
74+
&Felt252::from(fibonacci_input.second_element)
75+
);
76+
}
77+
78+
#[test]
79+
fn test_fibonacci_load_claim_idx() {
80+
// Runs fibonacci_load_second_element and afterwards fibonacci_load_claim_idx hint and
81+
// asserts that the claim index is loaded correctly into ap.
82+
let fibonacci_input = FibonacciInput {
83+
second_element: 5,
84+
fibonacci_claim_index: 3,
85+
};
86+
87+
let (mut vm, mut exec_scopes) = prepare_vm_for_fibonacci_test(&fibonacci_input);
88+
fibonacci_load_second_element(&mut vm, &mut exec_scopes).unwrap();
89+
vm.set_ap(1); // Set AP to 1 to simulate the hint's behavior
90+
fibonacci_load_claim_idx(&mut vm, &mut exec_scopes).unwrap();
91+
assert_eq!(vm.get_ap(), Relocatable::from((1, 1)));
92+
assert_eq!(
93+
vm.segments
94+
.memory
95+
.get_integer(vm.get_ap())
96+
.unwrap()
97+
.as_ref(),
98+
&Felt252::from(fibonacci_input.fibonacci_claim_index)
99+
);
100+
}
101+
}

crates/cairo-program-runner-lib/src/hints/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ pub struct FlexibleBuiltinUsageInput {
562562
pub n_blake2s: usize,
563563
}
564564

565-
#[derive(Debug, Clone, Deserialize)]
565+
#[derive(Debug, Clone, Deserialize, Serialize)]
566566
pub struct FibonacciInput {
567567
pub fibonacci_claim_index: usize,
568568
pub second_element: usize,

0 commit comments

Comments
 (0)