Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test reproducing bug #808 #810

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions tests/cases/stack.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use core::nullable::{NullableTrait};
use core::num::traits::Bounded;
use core::starknet::EthAddress;
use core::dict::{Felt252Dict, Felt252DictTrait};

#[derive(Destruct, Default)]
pub struct Stack {
pub items: Felt252Dict<Nullable<u256>>,
pub len: usize,
}

pub trait StackTrait {
fn new() -> Stack;
fn push(ref self: Stack, item: u256) -> Result<(), ()>;
fn pop(ref self: Stack) -> Result<u256, ()>;
fn peek_at(ref self: Stack, index: usize) -> Result<u256, ()>;
fn len(self: @Stack) -> usize;
}

impl StackImpl of StackTrait {
#[inline(always)]
fn new() -> Stack {
Default::default()
}

/// Pushes a new bytes32 word onto the stack.
///
/// When pushing an item to the stack, we will compute
/// an index which corresponds to the index in the dict the item will be stored at.
/// The internal index is computed as follows:
///
/// index = len(Stack_i) + i * STACK_SEGMENT_SIZE
///
/// # Errors
///
/// If the stack is full, returns with a StackOverflow error.
#[inline(always)]
fn push(ref self: Stack, item: u256) -> Result<(), ()> {
let length = self.len();

self.items.insert(length.into(), NullableTrait::new(item));
self.len += 1;
println!("stack_push top {:?}", self.peek_at(0).unwrap());
Result::Ok(())
}

/// Pops the top item off the stack.
///
/// # Errors
///
/// If the stack is empty, returns with a StackOverflow error.
#[inline(always)]
fn pop(ref self: Stack) -> Result<u256, ()> {
self.len -= 1;
let item = self.items.get(self.len().into());
println!("stack_pop top {:?}", item.deref());
Result::Ok(item.deref())
}

#[inline(always)]
fn peek_at(ref self: Stack, index: usize) -> Result<u256, ()> {
let position = self.len() - 1 - index;
let item = self.items.get(position.into());

Result::Ok(item.deref())
}

#[inline(always)]
fn len(self: @Stack) -> usize {
*self.len
}
}

pub fn main() -> u256 {
let mut stack = StackImpl::new();

stack.push(0).unwrap();
stack.push(0).unwrap();
stack.push(0).unwrap();
stack.push(0).unwrap();
stack.push(0).unwrap();
stack.push(4).unwrap();
stack.pop().unwrap()
}
2 changes: 2 additions & 0 deletions tests/tests/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ use test_case::test_case;
#[test_case("tests/cases/cairo_vm/programs/struct_span_return.cairo")]
#[test_case("tests/cases/cairo_vm/programs/tensor_new.cairo")]
#[test_case("tests/cases/brainfuck.cairo")]
// EVM related
#[test_case("tests/cases/stack.cairo")]
fn test_program_cases(program_path: &str) {
compare_inputless_program(program_path)
}
Expand Down
Loading