Skip to content

Commit

Permalink
improve dict test
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Oct 17, 2024
1 parent fcc8769 commit 8237005
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,9 @@ mod tests {
use starknet_types_core::felt::Felt;

use crate::{
cairo_native__dict_drop, cairo_native__dict_get, cairo_native__dict_new,
cairo_native__libfunc__debug__print, cairo_native__libfunc__hades_permutation,
cairo_native__libfunc__pedersen,
cairo_native__dict_drop, cairo_native__dict_dup, cairo_native__dict_get,
cairo_native__dict_new, cairo_native__libfunc__debug__print,
cairo_native__libfunc__hades_permutation, cairo_native__libfunc__pedersen,
};

pub fn felt252_short_str(value: &str) -> Felt {
Expand Down Expand Up @@ -751,6 +751,13 @@ mod tests {
drop(b);
}

pub extern "C" fn dup_fn_test(ptr: *mut c_void) -> *mut c_void {
assert!(!ptr.is_null());
let ptr: *mut u64 = ptr.cast();
let dup = unsafe { Box::into_raw(Box::new(*ptr)) };
dup.cast()
}

#[test]
fn test_dict() {
let dict = unsafe { cairo_native__dict_new(free_fn_test) };
Expand All @@ -769,6 +776,18 @@ mod tests {
assert_eq!(unsafe { **ptr }, 2);
}

let cloned_dict = unsafe { cairo_native__dict_dup(dict, dup_fn_test) };

unsafe { cairo_native__dict_drop(dict, None) };

{
let ptr: *mut *mut u64 =
unsafe { cairo_native__dict_get(&mut *cloned_dict, &key) }.cast();
assert!(!ptr.is_null());
assert!(!unsafe { *ptr }.is_null());
assert_eq!(unsafe { **ptr }, 2);
}

unsafe { cairo_native__dict_drop(cloned_dict, None) };
}
}

0 comments on commit 8237005

Please sign in to comment.