Skip to content

Commit

Permalink
feat: expose data_len accessor in Program (lambdaclass#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oppen authored and kariy committed Jun 23, 2023
1 parent f0f1981 commit 23817a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 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

* Add methor `Program::data_len(&self) -> usize` to get the number of data cells in a given program [#1022](https://github.com/lambdaclass/cairo-rs/pull/1022)

* Add missing hint on uint256_improvements lib [#1013](https://github.com/lambdaclass/cairo-rs/pull/1013):

`BuiltinHintProcessor` now supports the following hint:
Expand Down
36 changes: 36 additions & 0 deletions src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ impl Program {
pub fn iter_data(&self) -> impl Iterator<Item = &MaybeRelocatable> {
self.shared_program_data.data.iter()
}

pub fn data_len(&self) -> usize {
self.shared_program_data.data.len()
}
}

impl Default for Program {
Expand Down Expand Up @@ -323,6 +327,38 @@ mod tests {
assert_eq!(program.iter_data().cloned().collect::<Vec<_>>(), data);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn data_len() {
let reference_manager = ReferenceManager {
references: Vec::new(),
};

let builtins: Vec<BuiltinName> = Vec::new();
let data: Vec<MaybeRelocatable> = vec![
mayberelocatable!(5189976364521848832),
mayberelocatable!(1000),
mayberelocatable!(5189976364521848832),
mayberelocatable!(2000),
mayberelocatable!(5201798304953696256),
mayberelocatable!(2345108766317314046),
];

let program = Program::new(
builtins,
data.clone(),
None,
HashMap::new(),
reference_manager,
HashMap::new(),
Vec::new(),
None,
)
.unwrap();

assert_eq!(program.data_len(), data.len());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn new_program_with_invalid_identifiers() {
Expand Down

0 comments on commit 23817a9

Please sign in to comment.