Skip to content

Commit

Permalink
Working sample tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanuppal committed Nov 10, 2024
1 parent a8db168 commit 8119c22
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
27 changes: 0 additions & 27 deletions tools/calyx-ffi/tests/fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,6 @@ calyx_ffi::declare_interface! {
)]
struct Fifo;

// struct State<T> {
// inner: T,
// }
//
// struct EquationalStrategy<T> {
// leaves: Vec<T>,
// builders:
// }
//
// impl<T> Strategy<T> for EquationalStrategy<T> {
// fn generate(&mut self) -> T {
// self.leaves.ra
// }
// }
//
// trait Strategy<T> {
// fn generate(&mut self) -> T;
//
// fn evolve(&mut self, value: &mut T) {
// *value = self.generate();
// }
// }
//
// struct FifoStrategy: Strategy<Fifo> {
//
// }

#[cfg(test)]
#[calyx_ffi_tests]
mod tests {
Expand Down
33 changes: 23 additions & 10 deletions tools/calyx-ffi/tests/stack.futil
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
}
}
}
Expand Down
22 changes: 15 additions & 7 deletions tools/calyx-ffi/tests/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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;
Expand All @@ -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());
}
}

0 comments on commit 8119c22

Please sign in to comment.