Skip to content

Commit 3621b76

Browse files
committed
Add second test for join loops
1 parent 993c3a2 commit 3621b76

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/circuit.rs

+5
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ pub enum CircuitError {
101101
InvalidGate(usize),
102102
/// The specified output gate does not exist in the circuit.
103103
InvalidOutput(usize),
104+
/// The circuit does not specify any input gates.
105+
EmptyInputs,
104106
/// The circuit does not specify any output gates.
105107
EmptyOutputs,
106108
/// The provided circuit has too many gates to be processed.
@@ -141,6 +143,9 @@ impl Circuit {
141143
pub fn validate(&self) -> Result<(), CircuitError> {
142144
let mut num_and_gates = 0;
143145
let wires = self.wires();
146+
if self.input_gates.iter().all(|i| *i == 0) {
147+
return Err(CircuitError::EmptyInputs);
148+
}
144149
for (i, g) in wires.iter().enumerate() {
145150
match g {
146151
Wire::Input(_) => {}

tests/compile.rs

+62
Original file line numberDiff line numberDiff line change
@@ -2135,3 +2135,65 @@ pub fn main(rows1: [([u8; 3], u16); 4], rows2: [([u8; 3], u16, u16); 3]) -> u16
21352135
);
21362136
Ok(())
21372137
}
2138+
2139+
#[test]
2140+
fn compile_multiple_join_loops() -> Result<(), Error> {
2141+
for joined in 0..4 {
2142+
for only_a in 0..4 {
2143+
for only_b in 1..4 {
2144+
println!("Testing join loops for {joined} / {only_a} / {only_b} elements");
2145+
let a = joined + only_a;
2146+
let b = joined + only_b;
2147+
let prg = format!(
2148+
"
2149+
pub fn main(rows1: [(u8, u16); {a}], rows2: [(u8, u16, u16); {b}]) -> u16 {{
2150+
let mut result = 0u16;
2151+
for row in join(rows1, rows2) {{
2152+
let ((_, field1), (_, field2, field3)) = row;
2153+
result = result + field1 + field2 + field3;
2154+
}}
2155+
result
2156+
}}
2157+
"
2158+
);
2159+
let compiled = compile(&prg).map_err(|e| pretty_print(e, &prg))?;
2160+
compiled.circuit.validate().unwrap();
2161+
let mut eval = compiled.evaluator();
2162+
let mut rows_a = vec![];
2163+
let mut rows_b = vec![];
2164+
for i in 0..joined {
2165+
rows_a.push(Literal::Tuple(vec![
2166+
Literal::NumUnsigned(i, UnsignedNumType::U8),
2167+
Literal::NumUnsigned(2, UnsignedNumType::U16),
2168+
]));
2169+
rows_b.push(Literal::Tuple(vec![
2170+
Literal::NumUnsigned(i, UnsignedNumType::U8),
2171+
Literal::NumUnsigned(1, UnsignedNumType::U16),
2172+
Literal::NumUnsigned(1, UnsignedNumType::U16),
2173+
]));
2174+
}
2175+
for i in joined..joined + only_a {
2176+
rows_a.push(Literal::Tuple(vec![
2177+
Literal::NumUnsigned(i, UnsignedNumType::U8),
2178+
Literal::NumUnsigned(2, UnsignedNumType::U16),
2179+
]));
2180+
}
2181+
for i in joined + only_a..joined + only_a + only_b {
2182+
rows_b.push(Literal::Tuple(vec![
2183+
Literal::NumUnsigned(i, UnsignedNumType::U8),
2184+
Literal::NumUnsigned(1, UnsignedNumType::U16),
2185+
Literal::NumUnsigned(1, UnsignedNumType::U16),
2186+
]));
2187+
}
2188+
eval.set_literal(Literal::Array(rows_a)).unwrap();
2189+
eval.set_literal(Literal::Array(rows_b)).unwrap();
2190+
let output = eval.run().map_err(|e| pretty_print(e, &prg))?;
2191+
assert_eq!(
2192+
u16::try_from(output).map_err(|e| pretty_print(e, &prg))?,
2193+
joined as u16 * 4
2194+
);
2195+
}
2196+
}
2197+
}
2198+
Ok(())
2199+
}

0 commit comments

Comments
 (0)