Skip to content

Commit

Permalink
Add tests on hint_utils.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoletta committed Jan 4, 2023
1 parent ee751fa commit 510ae02
Showing 1 changed file with 112 additions and 1 deletion.
113 changes: 112 additions & 1 deletion src/hint_processor/builtin_hint_processor/hint_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ pub fn get_reference_from_var_name<'a>(
mod tests {
use super::*;
use crate::{
bigint,
hint_processor::hint_processor_definition::HintReference,
relocatable,
serde::deserialize_program::OffsetValue,
utils::test_utils::*,
vm::{
errors::memory_errors::MemoryError, vm_core::VirtualMachine, vm_memory::memory::Memory,
errors::{memory_errors::MemoryError, vm_errors::VirtualMachineError},
vm_core::VirtualMachine,
vm_memory::memory::Memory,
},
};
use num_bigint::Sign;
Expand All @@ -140,4 +143,112 @@ mod tests {
Ok(relocatable!(0, 2))
);
}

#[test]
fn get_maybe_relocatable_from_var_name_valid() {
let mut vm = vm!();
vm.memory = memory![((1, 0), (0, 0))];
let hint_ref = HintReference::new_simple(0);
let ids_data = HashMap::from([("value".to_string(), hint_ref)]);

assert_eq!(
get_maybe_relocatable_from_var_name("value", &vm, &ids_data, &ApTracking::new()),
Ok(mayberelocatable!(0, 0))
);
}

#[test]
fn get_maybe_relocatable_from_var_name_invalid() {
let mut vm = vm!();
vm.memory = Memory::new();
let hint_ref = HintReference::new_simple(0);
let ids_data = HashMap::from([("value".to_string(), hint_ref)]);

assert_eq!(
get_maybe_relocatable_from_var_name("value", &vm, &ids_data, &ApTracking::new()),
Err(HintError::FailedToGetIds)
);
}

#[test]
fn get_ptr_from_var_name_valid() {
let mut vm = vm!();
vm.memory = memory![((1, 0), (0, 0))];
let hint_ref = HintReference::new_simple(0);
let ids_data = HashMap::from([("value".to_string(), hint_ref)]);

assert_eq!(
get_ptr_from_var_name("value", &vm, &ids_data, &ApTracking::new()),
Ok(relocatable!(0, 0))
);
}

#[test]
fn get_ptr_from_var_name_invalid() {
let mut vm = vm!();
vm.memory = memory![((1, 0), 0)];
let hint_ref = HintReference::new_simple(0);
let ids_data = HashMap::from([("value".to_string(), hint_ref)]);

assert_eq!(
get_ptr_from_var_name("value", &vm, &ids_data, &ApTracking::new()),
Err(HintError::Internal(
VirtualMachineError::ExpectedRelocatable(MaybeRelocatable::from((1, 0)))
))
);
}

#[test]
fn get_relocatable_from_var_name_valid() {
let mut vm = vm!();
vm.memory = memory![((1, 0), (0, 0))];
let hint_ref = HintReference::new_simple(0);
let ids_data = HashMap::from([("value".to_string(), hint_ref)]);

assert_eq!(
get_relocatable_from_var_name("value", &vm, &ids_data, &ApTracking::new()),
Ok(relocatable!(1, 0))
);
}

#[test]
fn get_relocatable_from_var_name_invalid() {
let mut vm = vm!();
vm.memory = Memory::new();
let hint_ref = HintReference::new_simple(-8);
let ids_data = HashMap::from([("value".to_string(), hint_ref)]);

assert_eq!(
get_relocatable_from_var_name("value", &vm, &ids_data, &ApTracking::new()),
Err(HintError::FailedToGetIds)
);
}

#[test]
fn get_integer_from_var_name_valid() {
let mut vm = vm!();
vm.memory = memory![((1, 0), 1)];
let hint_ref = HintReference::new_simple(0);
let ids_data = HashMap::from([("value".to_string(), hint_ref)]);

assert_eq!(
get_integer_from_var_name("value", &vm, &ids_data, &ApTracking::new()),
Ok(Cow::Borrowed(&bigint!(1)))
);
}

#[test]
fn get_integer_from_var_name_invalid() {
let mut vm = vm!();
vm.memory = memory![((1, 0), (0, 0))];
let hint_ref = HintReference::new_simple(0);
let ids_data = HashMap::from([("value".to_string(), hint_ref)]);

assert_eq!(
get_integer_from_var_name("value", &vm, &ids_data, &ApTracking::new()),
Err(HintError::Internal(VirtualMachineError::ExpectedInteger(
MaybeRelocatable::from((1, 0))
)))
);
}
}

1 comment on commit 510ae02

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: 510ae02 Previous: e29e000 Ratio
cairo_run(cairo_programs/benchmarks/integration_builtins.json 755875855 ns/iter (± 18744640) 579460496 ns/iter (± 17020111) 1.30
cairo_run(cairo_programs/benchmarks/math_cmp_and_pow_integration_benchmark.json 31326343 ns/iter (± 2086335) 23961720 ns/iter (± 1440243) 1.31
cairo_run(cairo_programs/benchmarks/operations_with_data_structures_benchmarks.json 3198391876 ns/iter (± 80005675) 2390532478 ns/iter (± 61947297) 1.34

This comment was automatically generated by workflow using github-action-benchmark.

CC: @unbalancedparentheses

Please sign in to comment.