From 8119c22a6ebbb9d07ef9f2da5d132c1491c6aba2 Mon Sep 17 00:00:00 2001 From: Ethan Uppal <113849268+ethanuppal@users.noreply.github.com> Date: Sun, 10 Nov 2024 17:15:32 -0500 Subject: [PATCH] Working sample tests --- tools/calyx-ffi/tests/fifo.rs | 27 ------------------------- tools/calyx-ffi/tests/stack.futil | 33 +++++++++++++++++++++---------- tools/calyx-ffi/tests/stack.rs | 22 ++++++++++++++------- 3 files changed, 38 insertions(+), 44 deletions(-) diff --git a/tools/calyx-ffi/tests/fifo.rs b/tools/calyx-ffi/tests/fifo.rs index c3d1cc45e..76ffd1159 100644 --- a/tools/calyx-ffi/tests/fifo.rs +++ b/tools/calyx-ffi/tests/fifo.rs @@ -50,33 +50,6 @@ calyx_ffi::declare_interface! { )] struct Fifo; -// struct State { -// inner: T, -// } -// -// struct EquationalStrategy { -// leaves: Vec, -// builders: -// } -// -// impl Strategy for EquationalStrategy { -// fn generate(&mut self) -> T { -// self.leaves.ra -// } -// } -// -// trait Strategy { -// fn generate(&mut self) -> T; -// -// fn evolve(&mut self, value: &mut T) { -// *value = self.generate(); -// } -// } -// -// struct FifoStrategy: Strategy { -// -// } - #[cfg(test)] #[calyx_ffi_tests] mod tests { diff --git a/tools/calyx-ffi/tests/stack.futil b/tools/calyx-ffi/tests/stack.futil index 1eb9029b8..11575c4c0 100644 --- a/tools/calyx-ffi/tests/stack.futil +++ b/tools/calyx-ffi/tests/stack.futil @@ -2,27 +2,36 @@ import "primitives/core.futil"; import "primitives/memories/seq.futil"; import "primitives/binary_operators.futil"; -component main(cmd: 1, value: 32) -> (out: 32) { +component main(cmd: 1, value: 32) -> (out: 32, length: 4) { cells { store = seq_mem_d1(32, 16, 4); next = std_reg( 4); incr = std_add( 4); decr = std_sub( 4); last = std_reg(32); + test = std_eq(1); } wires { out = last.out; + length = next.out; + + comb group check_cmd { + test.left = cmd; + test.right = 1'b0; + } group write_at_next { store.addr0 = next.out; store.write_data = value; store.write_en = 1'b1; + store.content_en = 1'b1; write_at_next[done] = store.done; } group read_from_next { store.addr0 = next.out; + store.content_en = 1'b1; last.in = store.read_data; - last.write_en = 1'b1; + last.write_en = store.done; read_from_next[done] = last.done; } group increment_next { @@ -41,15 +50,19 @@ component main(cmd: 1, value: 32) -> (out: 32) { } } control { - if cmd { - seq { - write_at_next; - increment_next; + // if-else is buggy in cider2 + par { + if test.out with check_cmd { + seq { + write_at_next; + increment_next; + } } - } else { - seq { - decrement_next; - read_from_next; + if cmd { + seq { + decrement_next; + read_from_next; + } } } } diff --git a/tools/calyx-ffi/tests/stack.rs b/tools/calyx-ffi/tests/stack.rs index a6301c7a3..8009f5143 100644 --- a/tools/calyx-ffi/tests/stack.rs +++ b/tools/calyx-ffi/tests/stack.rs @@ -6,19 +6,27 @@ enum StackCommand { Pop = 1, } +const STACK_CAPACITY: u64 = 16; + calyx_ffi::declare_interface! { - Stack(cmd: 1, value: 32) -> (out: 32) impl { + Stack(cmd: 1, value: 32) -> (out: 32, length: 4) impl { fn push(&mut self, value: u32) { - self.reset(); + assert!(self.length() < STACK_CAPACITY, "tried to push when length={}", STACK_CAPACITY); + println!("stack has length {} before push", self.length()); + let old_length = self.length(); self.set_cmd(StackCommand::Push as u64); self.set_value(value as u64); self.go(); + assert_eq!(old_length + 1, self.length(), "stack length should increase by 1 on push"); } fn pop(&mut self) -> u32 { - self.reset(); + assert!(self.length() > 0, "tried to pop when stack empty"); + println!("stack has length {} before pop", self.length()); + let old_length = self.length(); self.set_cmd(StackCommand::Pop as u64); self.go(); + assert_eq!(old_length - 1, self.length(), "stack length should decrease by 1 on pop"); self.out() as u32 } } @@ -29,7 +37,7 @@ calyx_ffi::declare_interface! { comp = "main", backend = cider_ffi_backend, derive = [ - Stack(cmd: 1, value: 32) -> (out: 32) + Stack(cmd: 1, value: 32) -> (out: 32, length: 4) ] )] struct ReallyBadStack; @@ -41,13 +49,13 @@ mod tests { #[calyx_ffi_test] fn test_stack(stack: &mut ReallyBadStack) { - println!("testing fifo"); + println!("testing stack"); stack.push(1); stack.push(2); assert_eq!(2, stack.pop()); - // fifo.push(3); - // assert_eq!(3, fifo.pop()); + stack.push(3); + assert_eq!(3, stack.pop()); assert_eq!(1, stack.pop()); } }