Skip to content

Commit

Permalink
Add second test for join loops
Browse files Browse the repository at this point in the history
  • Loading branch information
fkettelhoit committed Jul 24, 2024
1 parent 993c3a2 commit 3621b76
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub enum CircuitError {
InvalidGate(usize),
/// The specified output gate does not exist in the circuit.
InvalidOutput(usize),
/// The circuit does not specify any input gates.
EmptyInputs,
/// The circuit does not specify any output gates.
EmptyOutputs,
/// The provided circuit has too many gates to be processed.
Expand Down Expand Up @@ -141,6 +143,9 @@ impl Circuit {
pub fn validate(&self) -> Result<(), CircuitError> {
let mut num_and_gates = 0;
let wires = self.wires();
if self.input_gates.iter().all(|i| *i == 0) {
return Err(CircuitError::EmptyInputs);
}
for (i, g) in wires.iter().enumerate() {
match g {
Wire::Input(_) => {}
Expand Down
62 changes: 62 additions & 0 deletions tests/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2135,3 +2135,65 @@ pub fn main(rows1: [([u8; 3], u16); 4], rows2: [([u8; 3], u16, u16); 3]) -> u16
);
Ok(())
}

#[test]
fn compile_multiple_join_loops() -> Result<(), Error> {
for joined in 0..4 {
for only_a in 0..4 {
for only_b in 1..4 {
println!("Testing join loops for {joined} / {only_a} / {only_b} elements");
let a = joined + only_a;
let b = joined + only_b;
let prg = format!(
"
pub fn main(rows1: [(u8, u16); {a}], rows2: [(u8, u16, u16); {b}]) -> u16 {{
let mut result = 0u16;
for row in join(rows1, rows2) {{
let ((_, field1), (_, field2, field3)) = row;
result = result + field1 + field2 + field3;
}}
result
}}
"
);
let compiled = compile(&prg).map_err(|e| pretty_print(e, &prg))?;
compiled.circuit.validate().unwrap();
let mut eval = compiled.evaluator();
let mut rows_a = vec![];
let mut rows_b = vec![];
for i in 0..joined {
rows_a.push(Literal::Tuple(vec![
Literal::NumUnsigned(i, UnsignedNumType::U8),
Literal::NumUnsigned(2, UnsignedNumType::U16),
]));
rows_b.push(Literal::Tuple(vec![
Literal::NumUnsigned(i, UnsignedNumType::U8),
Literal::NumUnsigned(1, UnsignedNumType::U16),
Literal::NumUnsigned(1, UnsignedNumType::U16),
]));
}
for i in joined..joined + only_a {
rows_a.push(Literal::Tuple(vec![
Literal::NumUnsigned(i, UnsignedNumType::U8),
Literal::NumUnsigned(2, UnsignedNumType::U16),
]));
}
for i in joined + only_a..joined + only_a + only_b {
rows_b.push(Literal::Tuple(vec![
Literal::NumUnsigned(i, UnsignedNumType::U8),
Literal::NumUnsigned(1, UnsignedNumType::U16),
Literal::NumUnsigned(1, UnsignedNumType::U16),
]));
}
eval.set_literal(Literal::Array(rows_a)).unwrap();
eval.set_literal(Literal::Array(rows_b)).unwrap();
let output = eval.run().map_err(|e| pretty_print(e, &prg))?;
assert_eq!(
u16::try_from(output).map_err(|e| pretty_print(e, &prg))?,
joined as u16 * 4
);
}
}
}
Ok(())
}

0 comments on commit 3621b76

Please sign in to comment.