Skip to content

Commit

Permalink
Feature: make test_utils macros public
Browse files Browse the repository at this point in the history
Fixes #1595.
  • Loading branch information
odesenfans committed Feb 5, 2024
1 parent dea2723 commit bebb3ed
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* feat: Make test_utils macros public [#1599](https://github.com/lambdaclass/cairo-vm/pull/1599).

* feat(BREAKING): Remove unecessary conversion functions between `Felt` & `BigUint`/`BigInt` [#1562](https://github.com/lambdaclass/cairo-vm/pull/1562)
* Remove the following functions:
* felt_from_biguint
Expand Down
95 changes: 61 additions & 34 deletions vm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn from_relocatable_to_indexes(relocatable: Relocatable) -> (usize, usize) {
}
}

#[cfg(test)]
#[cfg(any(test, feature = "test_utils"))]
#[macro_use]
pub mod test_utils {
use crate::types::exec_scope::ExecutionScopes;
Expand All @@ -76,7 +76,7 @@ pub mod test_utils {
Into::<num_bigint::BigInt>::into($val)
};
}
pub(crate) use bigint;
pub use bigint;

#[macro_export]
macro_rules! bigint_str {
Expand All @@ -87,15 +87,15 @@ pub mod test_utils {
num_bigint::BigInt::parse_bytes($val.as_bytes(), $opt).expect("Couldn't parse bytes")
};
}
pub(crate) use bigint_str;
pub use bigint_str;

#[macro_export]
macro_rules! biguint {
($val : expr) => {
Into::<num_bigint::BigUint>::into($val as u128)
};
}
pub(crate) use biguint;
pub use biguint;

#[macro_export]
macro_rules! biguint_str {
Expand All @@ -106,7 +106,7 @@ pub mod test_utils {
num_bigint::BigUint::parse_bytes($val.as_bytes(), $opt).expect("Couldn't parse bytes")
};
}
pub(crate) use biguint_str;
pub use biguint_str;

impl From<(&str, u8)> for MaybeRelocatable {
fn from((string, radix): (&str, u8)) -> Self {
Expand All @@ -118,6 +118,7 @@ pub mod test_utils {
}
}

#[macro_export]
macro_rules! segments {
($( (($si:expr, $off:expr), $val:tt) ),* $(,)? ) => {
{
Expand All @@ -133,8 +134,9 @@ pub mod test_utils {

};
}
pub(crate) use segments;
pub use segments;

#[macro_export]
macro_rules! memory {
( $( (($si:expr, $off:expr), $val:tt) ),* ) => {
{
Expand All @@ -144,8 +146,9 @@ pub mod test_utils {
}
};
}
pub(crate) use memory;
pub use memory;

#[macro_export]
macro_rules! memory_from_memory {
($mem: expr, ( $( (($si:expr, $off:expr), $val:tt) ),* )) => {
{
Expand All @@ -155,8 +158,9 @@ pub mod test_utils {
}
};
}
pub(crate) use memory_from_memory;
pub use memory_from_memory;

#[macro_export]
macro_rules! memory_inner {
($mem:expr, ($si:expr, $off:expr), ($sival:expr, $offval: expr)) => {
let (k, v) = (($si, $off).into(), &mayberelocatable!($sival, $offval));
Expand Down Expand Up @@ -189,17 +193,19 @@ pub mod test_utils {
}
};
}
pub(crate) use memory_inner;
pub use memory_inner;

#[macro_export]
macro_rules! check_memory {
( $mem: expr, $( (($si:expr, $off:expr), $val:tt) ),* $(,)? ) => {
$(
check_memory_address!($mem, ($si, $off), $val);
)*
};
}
pub(crate) use check_memory;
pub use check_memory;

#[macro_export]
macro_rules! check_memory_address {
($mem:expr, ($si:expr, $off:expr), ($sival:expr, $offval: expr)) => {
assert_eq!(
Expand All @@ -214,8 +220,9 @@ pub mod test_utils {
)
};
}
pub(crate) use check_memory_address;
pub use check_memory_address;

#[macro_export]
macro_rules! mayberelocatable {
($val1 : expr, $val2 : expr) => {
$crate::types::relocatable::MaybeRelocatable::from(($val1, $val2))
Expand All @@ -224,8 +231,9 @@ pub mod test_utils {
$crate::types::relocatable::MaybeRelocatable::from(crate::Felt252::from($val1 as i128))
};
}
pub(crate) use mayberelocatable;
pub use mayberelocatable;

#[macro_export]
macro_rules! references {
($num: expr) => {{
let mut references = crate::stdlib::collections::HashMap::<usize, HintReference>::new();
Expand All @@ -235,8 +243,9 @@ pub mod test_utils {
references
}};
}
pub(crate) use references;
pub use references;

#[macro_export]
macro_rules! vm_with_range_check {
() => {{
let mut vm = VirtualMachine::new(false);
Expand All @@ -247,8 +256,9 @@ pub mod test_utils {
vm
}};
}
pub(crate) use vm_with_range_check;
pub use vm_with_range_check;

#[macro_export]
macro_rules! cairo_runner {
($program:expr) => {
CairoRunner::new(&$program, "all_cairo", false).unwrap()
Expand All @@ -263,12 +273,14 @@ pub mod test_utils {
CairoRunner::new(&program, $layout.to_string(), proof_mode).unwrap()
};
}
pub(crate) use cairo_runner;
pub use cairo_runner;

pub(crate) use crate::stdlib::{collections::BTreeMap, sync::Arc};
pub use crate::stdlib::{collections::BTreeMap, sync::Arc};
pub(crate) use crate::types::program::HintsCollection;
pub(crate) use crate::types::program::Program;
pub use crate::types::program::Program;
pub(crate) use crate::types::program::SharedProgramData;

#[macro_export]
macro_rules! program {
//Empty program
() => {
Expand Down Expand Up @@ -308,7 +320,7 @@ pub mod test_utils {
}};
}

pub(crate) use program;
pub use program;

pub(crate) struct ProgramFlat {
pub(crate) data: crate::utils::Vec<MaybeRelocatable>,
Expand Down Expand Up @@ -381,6 +393,7 @@ pub mod test_utils {
}
}

#[macro_export]
macro_rules! vm {
() => {{
VirtualMachine::new(false)
Expand All @@ -390,17 +403,19 @@ pub mod test_utils {
VirtualMachine::new($use_trace)
}};
}
pub(crate) use vm;
pub use vm;

#[macro_export]
macro_rules! run_context {
( $vm: expr, $pc: expr, $ap: expr, $fp: expr ) => {
$vm.run_context.pc = Relocatable::from((0, $pc));
$vm.run_context.ap = $ap;
$vm.run_context.fp = $fp;
};
}
pub(crate) use run_context;
pub use run_context;

#[macro_export]
macro_rules! ids_data {
( $( $name: expr ),* ) => {
{
Expand All @@ -414,8 +429,9 @@ pub mod test_utils {
}
};
}
pub(crate) use ids_data;
pub use ids_data;

#[macro_export]
macro_rules! non_continuous_ids_data {
( $( ($name: expr, $offset:expr) ),* $(,)? ) => {
{
Expand All @@ -427,10 +443,10 @@ pub mod test_utils {
}
};
}
pub(crate) use non_continuous_ids_data;
pub use non_continuous_ids_data;

#[track_caller]
pub(crate) fn trace_check(
pub fn trace_check(
actual: &[TraceEntry],
expected: &[(crate::utils::Relocatable, usize, usize)],
) {
Expand All @@ -440,13 +456,15 @@ pub mod test_utils {
}
}

#[macro_export]
macro_rules! exec_scopes_ref {
() => {
&mut ExecutionScopes::new()
};
}
pub(crate) use exec_scopes_ref;
pub use exec_scopes_ref;

#[macro_export]
macro_rules! run_hint {
($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr) => {{
let hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data);
Expand Down Expand Up @@ -480,26 +498,29 @@ pub mod test_utils {
)
}};
}
pub(crate) use run_hint;
pub use run_hint;

#[macro_export]
macro_rules! add_segments {
($vm:expr, $n:expr) => {
for _ in 0..$n {
$vm.segments.add();
}
};
}
pub(crate) use add_segments;
pub use add_segments;

#[macro_export]
macro_rules! check_scope {
( $exec_scope: expr, [ $( ($name: expr, $val: expr)),*$(,)? ] $(,)? ) => {
$(
check_scope_value($exec_scope, $name, $val);
)*
};
}
pub(crate) use check_scope;
pub use check_scope;

#[macro_export]
macro_rules! scope {
() => { ExecutionScopes::new() };
( $( ($name: expr, $val: expr)),* $(,)? ) => {
Expand All @@ -515,8 +536,9 @@ pub mod test_utils {
}
};
}
pub(crate) use scope;
pub use scope;

#[macro_export]
macro_rules! check_dictionary {
( $exec_scopes: expr, $tracker_num:expr, $( ($key:expr, $val:expr )),* ) => {
$(
Expand All @@ -534,8 +556,9 @@ pub mod test_utils {
*
};
}
pub(crate) use check_dictionary;
pub use check_dictionary;

#[macro_export]
macro_rules! check_dict_ptr {
($exec_scopes: expr, $tracker_num: expr, ($i:expr, $off:expr)) => {
assert_eq!(
Expand All @@ -551,8 +574,9 @@ pub mod test_utils {
);
};
}
pub(crate) use check_dict_ptr;
pub use check_dict_ptr;

#[macro_export]
macro_rules! dict_manager {
($exec_scopes:expr, $tracker_num:expr, $( ($key:expr, $val:expr )),* ) => {
let mut tracker = DictTracker::new_empty(relocatable!($tracker_num, 0));
Expand All @@ -571,8 +595,9 @@ pub mod test_utils {
};

}
pub(crate) use dict_manager;
pub use dict_manager;

#[macro_export]
macro_rules! dict_manager_default {
($exec_scopes:expr, $tracker_num:expr,$default:expr, $( ($key:expr, $val:expr )),* ) => {
let mut tracker = DictTracker::new_default_dict(relocatable!($tracker_num, 0), &MaybeRelocatable::from($default), None);
Expand All @@ -590,15 +615,17 @@ pub mod test_utils {
$exec_scopes.insert_value("dict_manager", crate::stdlib::rc::Rc::new(core::cell::RefCell::new(dict_manager)))
};
}
pub(crate) use dict_manager_default;
pub use dict_manager_default;

#[macro_export]
macro_rules! vec_data {
( $( ($val:tt) ),* ) => {
vec![$( vec_data_inner!($val) ),*]
};
}
pub(crate) use vec_data;
pub use vec_data;

#[macro_export]
macro_rules! vec_data_inner {
(( $val1:expr, $val2:expr )) => {
mayberelocatable!($val1, $val2)
Expand All @@ -607,7 +634,7 @@ pub mod test_utils {
mayberelocatable!($val)
};
}
pub(crate) use vec_data_inner;
pub use vec_data_inner;

pub fn check_scope_value<T: core::fmt::Debug + core::cmp::PartialEq + 'static>(
scopes: &ExecutionScopes,
Expand Down

0 comments on commit bebb3ed

Please sign in to comment.