Skip to content

Commit 21338b4

Browse files
committed
tests: add dict manager temp segments test
1 parent 99f0ae5 commit 21338b4

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

vm/src/hint_processor/cairo_1_hint_processor/dict_manager.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,98 @@ impl DictSquashExecScope {
239239
self.current_access_indices()?.pop()
240240
}
241241
}
242+
243+
#[cfg(test)]
244+
mod tests {
245+
use super::*;
246+
use crate::stdlib::collections::HashMap;
247+
use crate::types::relocatable::Relocatable;
248+
use crate::vm::vm_core::VirtualMachine;
249+
250+
/// Test for new_default_dict error case:
251+
/// Attempting to create a dictionary on a segment that's already taken
252+
#[test]
253+
fn test_new_default_dict_segment_taken_error() {
254+
let mut vm = VirtualMachine::new(false, false);
255+
let mut dict_manager = DictManagerExecScope::new(true);
256+
257+
// First dictionary creation should work
258+
let _first_dict_start = dict_manager.new_default_dict(&mut vm).unwrap();
259+
260+
// Simulate a scenario where the same segment index is attempted to be used again
261+
// let conflicting_segment = Relocatable::from((first_dict_start.segment_index, 0));
262+
263+
// Attempt to create a new dictionary should return an error
264+
let result = dict_manager.new_default_dict(&mut vm);
265+
assert!(matches!(
266+
result,
267+
Err(HintError::CantCreateDictionaryOnTakenSegment(_))
268+
));
269+
}
270+
271+
/// Test for relocate_all_dictionaries error cases
272+
#[test]
273+
fn test_relocate_all_dictionaries_errors() {
274+
let mut vm = VirtualMachine::new(false, false);
275+
276+
// Test 1: First segment is a temporary segment (should error)
277+
{
278+
let mut dict_manager = DictManagerExecScope::new(true);
279+
let first_dict_start = Relocatable::from((-1, 0)); // Temporary segment
280+
281+
dict_manager.trackers.push(DictTrackerExecScope {
282+
data: HashMap::default(),
283+
start: first_dict_start,
284+
end: Some(Relocatable::from((-1, 10))),
285+
});
286+
287+
let result = dict_manager.relocate_all_dictionaries(&mut vm);
288+
assert!(matches!(
289+
result,
290+
Err(HintError::CustomHint(_)) if result.unwrap_err().to_string().contains("First dict segment should not be temporary")
291+
));
292+
}
293+
294+
// Test 2: Non-temporary dictionary segment
295+
{
296+
let mut dict_manager = DictManagerExecScope::new(true);
297+
let first_dict_start = Relocatable::from((0, 0)); // Non-temporary segment
298+
let second_dict_start = Relocatable::from((1, 0)); // Non-temporary segment
299+
300+
dict_manager.trackers.push(DictTrackerExecScope {
301+
data: HashMap::default(),
302+
start: first_dict_start,
303+
end: Some(Relocatable::from((0, 10))),
304+
});
305+
dict_manager.trackers.push(DictTrackerExecScope {
306+
data: HashMap::default(),
307+
start: second_dict_start,
308+
end: Some(Relocatable::from((1, 10))),
309+
});
310+
311+
let result = dict_manager.relocate_all_dictionaries(&mut vm);
312+
assert!(matches!(
313+
result,
314+
Err(HintError::CustomHint(_)) if result.unwrap_err().to_string().contains("Dict segment should be temporary")
315+
));
316+
}
317+
}
318+
319+
/// Test for relocate_all_dictionaries when no temporary segments
320+
#[test]
321+
fn test_relocate_all_dictionaries_no_temporary_segments() {
322+
let mut vm = VirtualMachine::new(false, false);
323+
let mut dict_manager = DictManagerExecScope::new(false);
324+
325+
// Adding some trackers should not cause any errors
326+
dict_manager.trackers.push(DictTrackerExecScope {
327+
data: HashMap::default(),
328+
start: Relocatable::from((0, 0)),
329+
end: Some(Relocatable::from((0, 10))),
330+
});
331+
332+
// Should not error and essentially do nothing
333+
let result = dict_manager.relocate_all_dictionaries(&mut vm);
334+
assert!(result.is_ok());
335+
}
336+
}

0 commit comments

Comments
 (0)