Skip to content

Commit 21dec96

Browse files
committed
Generalized test functions
- created a test function to generate add/calyx_go_done transactions from their corresponding source code
1 parent 59403d5 commit 21dec96

File tree

3 files changed

+84
-182
lines changed

3 files changed

+84
-182
lines changed

src/diagnostic.rs

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl DiagnosticHandler {
166166

167167
#[cfg(test)]
168168
mod tests {
169-
use crate::typecheck::*;
169+
use crate::{serialize::tests::create_calyx_go_down_transaction, typecheck::*};
170170

171171
use super::*;
172172
use baa::BitVecValue;
@@ -198,86 +198,8 @@ mod tests {
198198

199199
#[test]
200200
fn serialize_calyx_go_down_transaction() {
201-
// Manually create the expected result of parsing `calyx_go_down`.
202-
// Note that the order in which things are created will be different in the parser.
203-
204-
// 1) declare symbols
205-
let mut symbols = SymbolTable::default();
206-
let mut handler: DiagnosticHandler = DiagnosticHandler::new();
207-
let ii = symbols.add_without_parent("ii".to_string(), Type::BitVec(32));
208-
let oo = symbols.add_without_parent("oo".to_string(), Type::BitVec(32));
209-
assert_eq!(symbols["oo"], symbols[oo]);
210-
211-
// declare Calyx struct
212-
let dut_struct = symbols.add_struct(
213-
"Calyx".to_string(),
214-
vec![
215-
Field::new("ii".to_string(), Dir::In, Type::BitVec(32)),
216-
Field::new("go".to_string(), Dir::In, Type::BitVec(1)),
217-
Field::new("done".to_string(), Dir::Out, Type::BitVec(1)),
218-
Field::new("oo".to_string(), Dir::Out, Type::BitVec(32)),
219-
],
220-
);
221-
222-
let dut = symbols.add_without_parent("dut".to_string(), Type::Struct(dut_struct));
223-
let dut_ii = symbols.add_with_parent("ii".to_string(), dut);
224-
let dut_go = symbols.add_with_parent("go".to_string(), dut);
225-
let dut_done = symbols.add_with_parent("done".to_string(), dut);
226-
let dut_oo = symbols.add_with_parent("oo".to_string(), dut);
227-
assert_eq!(symbols["dut.oo"], symbols[dut_oo]);
228-
assert_eq!(symbols["oo"], symbols[oo]);
229-
230-
// create fileid and read file
231-
let input =
232-
std::fs::read_to_string("tests/calyx_go_doneStruct.prot").expect("failed to load");
233-
let calyx_fileid = handler.add_file("calyx_go_done.prot".to_string(), input);
234-
235-
// 2) create transaction
236-
let mut calyx_go_done = Transaction::new("calyx_go_done".to_string());
237-
calyx_go_done.args = vec![Arg::new(ii, Dir::In), Arg::new(oo, Dir::Out)];
238-
calyx_go_done.type_args = vec![dut];
239-
240-
// 3) create expressions
241-
let ii_expr = calyx_go_done.e(Expr::Sym(ii));
242-
calyx_go_done.add_expr_loc(ii_expr, 153, 155, calyx_fileid);
243-
let dut_oo_expr = calyx_go_done.e(Expr::Sym(dut_oo));
244-
calyx_go_done.add_expr_loc(dut_oo_expr, 260, 266, calyx_fileid);
245-
let one_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(1, 1)));
246-
calyx_go_done.add_expr_loc(one_expr, 170, 171, calyx_fileid);
247-
let zero_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(0, 1)));
248-
calyx_go_done.add_expr_loc(zero_expr, 232, 233, calyx_fileid);
249-
let dut_done_expr = calyx_go_done.e(Expr::Sym(dut_done));
250-
calyx_go_done.add_expr_loc(dut_done_expr, 184, 192, calyx_fileid);
251-
let cond_expr = calyx_go_done.e(Expr::Equal(dut_done_expr, one_expr));
252-
calyx_go_done.add_expr_loc(cond_expr, 183, 198, calyx_fileid);
253-
let not_expr = calyx_go_done.e(Expr::Not(cond_expr));
254-
calyx_go_done.add_expr_loc(not_expr, 182, 198, calyx_fileid);
255-
256-
// 4) create statements
257-
let while_body = vec![calyx_go_done.s(Stmt::Step)];
258-
let wbody = calyx_go_done.s(Stmt::Block(while_body));
259-
260-
let dut_ii_assign = calyx_go_done.s(Stmt::Assign(dut_ii, ii_expr));
261-
calyx_go_done.add_stmt_loc(dut_ii_assign, 143, 157, calyx_fileid);
262-
let dut_go_assign = calyx_go_done.s(Stmt::Assign(dut_go, one_expr));
263-
calyx_go_done.add_stmt_loc(dut_go_assign, 160, 172, calyx_fileid);
264-
let dut_while = calyx_go_done.s(Stmt::While(not_expr, wbody));
265-
calyx_go_done.add_stmt_loc(dut_while, 175, 219, calyx_fileid);
266-
let dut_go_reassign = calyx_go_done.s(Stmt::Assign(dut_go, zero_expr));
267-
calyx_go_done.add_stmt_loc(dut_go_reassign, 222, 234, calyx_fileid);
268-
let dut_ii_dontcare = calyx_go_done.s(Stmt::Assign(dut_ii, calyx_go_done.expr_dont_care()));
269-
calyx_go_done.add_stmt_loc(dut_ii_dontcare, 238, 250, calyx_fileid);
270-
let oo_assign = calyx_go_done.s(Stmt::Assign(oo, dut_oo_expr));
271-
calyx_go_done.add_stmt_loc(oo_assign, 254, 268, calyx_fileid);
272-
let body = vec![
273-
dut_ii_assign,
274-
dut_go_assign,
275-
dut_while,
276-
dut_go_reassign,
277-
dut_ii_dontcare,
278-
oo_assign,
279-
];
280-
calyx_go_done.body = calyx_go_done.s(Stmt::Block(body));
201+
let mut handler = DiagnosticHandler::new();
202+
let (calyx_go_done, symbols) = create_calyx_go_down_transaction(&mut handler);
281203
type_check(&calyx_go_done, &symbols, &mut handler);
282204
}
283205
}

src/serialize.rs

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ pub mod tests {
179179

180180
use super::*;
181181

182-
#[test]
183-
fn serialize_add_transaction() {
182+
pub fn create_add_transaction(handler: &mut DiagnosticHandler) -> (Transaction, SymbolTable) {
184183
// Manually create the expected result of parsing `add.prot`.
185184
// Note that the order in which things are created will be different in the parser.
186185

@@ -209,6 +208,10 @@ pub mod tests {
209208
assert_eq!(symbols["DUT.s"], symbols[dut_s]);
210209
assert_eq!(symbols["s"], symbols[s]);
211210

211+
// create fileid and read file
212+
let input = std::fs::read_to_string("tests/addStruct.prot").expect("failed to load");
213+
let add_fileid = handler.add_file("add.prot".to_string(), input);
214+
212215
// 2) create transaction
213216
let mut add = Transaction::new("add".to_string());
214217
add.args = vec![
@@ -220,31 +223,47 @@ pub mod tests {
220223

221224
// 3) create expressions
222225
let a_expr = add.e(Expr::Sym(a));
226+
add.add_expr_loc(a_expr, 193, 194, add_fileid);
223227
let b_expr = add.e(Expr::Sym(b));
228+
add.add_expr_loc(b_expr, 208, 209, add_fileid);
224229
let dut_s_expr = add.e(Expr::Sym(dut_s));
230+
add.add_expr_loc(dut_s_expr, 271, 276, add_fileid);
225231

226232
// 4) create statements
233+
let a_assign = add.s(Stmt::Assign(dut_a, a_expr));
234+
add.add_stmt_loc(a_assign, 184, 195, add_fileid);
235+
let b_assign = add.s(Stmt::Assign(dut_b, b_expr));
236+
add.add_stmt_loc(b_assign, 199, 210, add_fileid);
237+
let step = add.s(Stmt::Step);
238+
add.add_stmt_loc(step, 214, 221, add_fileid);
239+
let fork = add.s(Stmt::Fork);
240+
add.add_stmt_loc(fork, 225, 232, add_fileid);
241+
let dut_a_assign = add.s(Stmt::Assign(dut_a, add.expr_dont_care()));
242+
add.add_stmt_loc(dut_a_assign, 236, 247, add_fileid);
243+
let dut_b_assign = add.s(Stmt::Assign(dut_b, add.expr_dont_care()));
244+
add.add_stmt_loc(dut_b_assign, 251, 262, add_fileid);
245+
let s_assign = add.s(Stmt::Assign(s, dut_s_expr));
246+
add.add_stmt_loc(s_assign, 266, 277, add_fileid);
227247
let body = vec![
228-
add.s(Stmt::Assign(dut_a, a_expr)),
229-
add.s(Stmt::Assign(dut_b, b_expr)),
230-
add.s(Stmt::Step),
231-
add.s(Stmt::Fork),
232-
add.s(Stmt::Assign(dut_a, add.expr_dont_care())),
233-
add.s(Stmt::Assign(dut_b, add.expr_dont_care())),
234-
add.s(Stmt::Assign(s, dut_s_expr)),
248+
a_assign,
249+
b_assign,
250+
step,
251+
fork,
252+
dut_a_assign,
253+
dut_b_assign,
254+
s_assign,
235255
];
236256
add.body = add.s(Stmt::Block(body));
237257

238-
println!("{}", serialize_to_string(&add, &symbols).unwrap());
258+
(add, symbols)
239259
}
240260

241-
#[test]
242-
fn serialize_calyx_go_down_transaction() {
261+
pub fn create_calyx_go_down_transaction(
262+
handler: &mut DiagnosticHandler,
263+
) -> (Transaction, SymbolTable) {
243264
// Manually create the expected result of parsing `calyx_go_down`.
244265
// Note that the order in which things are created will be different in the parser.
245266

246-
// TODO: create this into function that factors our the code to put src code into IR
247-
248267
// 1) declare symbols
249268
let mut symbols = SymbolTable::default();
250269
let ii = symbols.add_without_parent("ii".to_string(), Type::BitVec(32));
@@ -256,7 +275,7 @@ pub mod tests {
256275
"Calyx".to_string(),
257276
vec![
258277
Field::new("ii".to_string(), Dir::In, Type::BitVec(32)),
259-
Field::new("go".to_string(), Dir::In, Type::BitVec(1)),
278+
Field::new("go".to_string(), Dir::In, Type::BitVec(32)),
260279
Field::new("done".to_string(), Dir::Out, Type::BitVec(1)),
261280
Field::new("oo".to_string(), Dir::Out, Type::BitVec(32)),
262281
],
@@ -270,30 +289,48 @@ pub mod tests {
270289
assert_eq!(symbols["dut.oo"], symbols[dut_oo]);
271290
assert_eq!(symbols["oo"], symbols[oo]);
272291

292+
// create fileid and read file
293+
let input =
294+
std::fs::read_to_string("tests/calyx_go_doneStruct.prot").expect("failed to load");
295+
let calyx_fileid = handler.add_file("calyx_go_done.prot".to_string(), input);
296+
273297
// 2) create transaction
274298
let mut calyx_go_done = Transaction::new("calyx_go_done".to_string());
275299
calyx_go_done.args = vec![Arg::new(ii, Dir::In), Arg::new(oo, Dir::Out)];
276300
calyx_go_done.type_args = vec![dut];
277301

278302
// 3) create expressions
279303
let ii_expr = calyx_go_done.e(Expr::Sym(ii));
304+
calyx_go_done.add_expr_loc(ii_expr, 153, 155, calyx_fileid);
280305
let dut_oo_expr = calyx_go_done.e(Expr::Sym(dut_oo));
306+
calyx_go_done.add_expr_loc(dut_oo_expr, 260, 266, calyx_fileid);
281307
let one_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(1, 1)));
308+
calyx_go_done.add_expr_loc(one_expr, 170, 171, calyx_fileid);
282309
let zero_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(0, 1)));
310+
calyx_go_done.add_expr_loc(zero_expr, 232, 233, calyx_fileid);
283311
let dut_done_expr = calyx_go_done.e(Expr::Sym(dut_done));
312+
calyx_go_done.add_expr_loc(dut_done_expr, 184, 192, calyx_fileid);
284313
let cond_expr = calyx_go_done.e(Expr::Equal(dut_done_expr, one_expr));
314+
calyx_go_done.add_expr_loc(cond_expr, 183, 198, calyx_fileid);
285315
let not_expr = calyx_go_done.e(Expr::Not(cond_expr));
316+
calyx_go_done.add_expr_loc(not_expr, 182, 198, calyx_fileid);
286317

287318
// 4) create statements
288319
let while_body = vec![calyx_go_done.s(Stmt::Step)];
289320
let wbody = calyx_go_done.s(Stmt::Block(while_body));
290321

291322
let dut_ii_assign = calyx_go_done.s(Stmt::Assign(dut_ii, ii_expr));
292-
let dut_go_assign = calyx_go_done.s(Stmt::Assign(dut_go, one_expr));
323+
calyx_go_done.add_stmt_loc(dut_ii_assign, 143, 157, calyx_fileid);
324+
let dut_go_assign = calyx_go_done.s(Stmt::Assign(dut_go, one_expr)); // Should cause type mismatch error
325+
calyx_go_done.add_stmt_loc(dut_go_assign, 160, 172, calyx_fileid);
293326
let dut_while = calyx_go_done.s(Stmt::While(not_expr, wbody));
294-
let dut_go_reassign = calyx_go_done.s(Stmt::Assign(dut_go, zero_expr));
327+
calyx_go_done.add_stmt_loc(dut_while, 175, 219, calyx_fileid);
328+
let dut_go_reassign = calyx_go_done.s(Stmt::Assign(dut_go, zero_expr)); // Should cause type mismatch error
329+
calyx_go_done.add_stmt_loc(dut_go_reassign, 222, 234, calyx_fileid);
295330
let dut_ii_dontcare = calyx_go_done.s(Stmt::Assign(dut_ii, calyx_go_done.expr_dont_care()));
331+
calyx_go_done.add_stmt_loc(dut_ii_dontcare, 238, 250, calyx_fileid);
296332
let oo_assign = calyx_go_done.s(Stmt::Assign(oo, dut_oo_expr));
333+
calyx_go_done.add_stmt_loc(oo_assign, 254, 268, calyx_fileid);
297334
let body = vec![
298335
dut_ii_assign,
299336
dut_go_assign,
@@ -303,6 +340,22 @@ pub mod tests {
303340
oo_assign,
304341
];
305342
calyx_go_done.body = calyx_go_done.s(Stmt::Block(body));
343+
344+
(calyx_go_done, symbols)
345+
}
346+
347+
#[test]
348+
fn serialize_add_transaction() {
349+
let mut handler = DiagnosticHandler::new();
350+
let (add, symbols) = create_add_transaction(&mut handler);
351+
352+
println!("{}", serialize_to_string(&add, &symbols).unwrap());
353+
}
354+
355+
#[test]
356+
fn serialize_calyx_go_down_transaction() {
357+
let mut handler = DiagnosticHandler::new();
358+
let (calyx_go_done, symbols) = create_calyx_go_down_transaction(&mut handler);
306359
println!("{}", serialize_to_string(&calyx_go_done, &symbols).unwrap());
307360
}
308361

src/typecheck.rs

Lines changed: 11 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -142,96 +142,23 @@ pub fn type_check(tr: &Transaction, st: &SymbolTable, handler: &mut DiagnosticHa
142142
}
143143
}
144144

145+
#[cfg(test)]
145146
mod tests {
146-
use baa::BitVecValue;
147+
use crate::serialize::tests::{create_add_transaction, create_calyx_go_down_transaction};
147148

148149
use super::*;
149150

150151
#[test]
151-
fn serialize_calyx_go_down_transaction() {
152-
// Manually create the expected result of parsing `calyx_go_down`.
153-
// Note that the order in which things are created will be different in the parser.
154-
155-
// TODO: create this into function that factors our the code to put src code into IR
156-
157-
// 1) declare symbols
158-
let mut symbols = SymbolTable::default();
159-
let mut handler: DiagnosticHandler = DiagnosticHandler::new();
160-
161-
let ii = symbols.add_without_parent("ii".to_string(), Type::BitVec(32));
162-
let oo = symbols.add_without_parent("oo".to_string(), Type::BitVec(32));
163-
assert_eq!(symbols["oo"], symbols[oo]);
164-
165-
// declare Calyx struct
166-
let dut_struct = symbols.add_struct(
167-
"Calyx".to_string(),
168-
vec![
169-
Field::new("ii".to_string(), Dir::In, Type::BitVec(32)),
170-
Field::new("go".to_string(), Dir::In, Type::BitVec(32)),
171-
Field::new("done".to_string(), Dir::Out, Type::BitVec(1)),
172-
Field::new("oo".to_string(), Dir::Out, Type::BitVec(32)),
173-
],
174-
);
175-
176-
let dut = symbols.add_without_parent("dut".to_string(), Type::Struct(dut_struct));
177-
let dut_ii = symbols.add_with_parent("ii".to_string(), dut);
178-
let dut_go = symbols.add_with_parent("go".to_string(), dut);
179-
let dut_done = symbols.add_with_parent("done".to_string(), dut);
180-
let dut_oo = symbols.add_with_parent("oo".to_string(), dut);
181-
assert_eq!(symbols["dut.oo"], symbols[dut_oo]);
182-
assert_eq!(symbols["oo"], symbols[oo]);
183-
184-
// create fileid and read file
185-
let input =
186-
std::fs::read_to_string("tests/calyx_go_doneStruct.prot").expect("failed to load");
187-
let calyx_fileid = handler.add_file("calyx_go_done.prot".to_string(), input);
188-
189-
// 2) create transaction
190-
let mut calyx_go_done = Transaction::new("calyx_go_done".to_string());
191-
calyx_go_done.args = vec![Arg::new(ii, Dir::In), Arg::new(oo, Dir::Out)];
192-
calyx_go_done.type_args = vec![dut];
193-
194-
// 3) create expressions
195-
let ii_expr = calyx_go_done.e(Expr::Sym(ii));
196-
calyx_go_done.add_expr_loc(ii_expr, 153, 155, calyx_fileid);
197-
let dut_oo_expr = calyx_go_done.e(Expr::Sym(dut_oo));
198-
calyx_go_done.add_expr_loc(dut_oo_expr, 260, 266, calyx_fileid);
199-
let one_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(1, 1)));
200-
calyx_go_done.add_expr_loc(one_expr, 170, 171, calyx_fileid);
201-
let zero_expr = calyx_go_done.e(Expr::Const(BitVecValue::from_u64(0, 1)));
202-
calyx_go_done.add_expr_loc(zero_expr, 232, 233, calyx_fileid);
203-
let dut_done_expr = calyx_go_done.e(Expr::Sym(dut_done));
204-
calyx_go_done.add_expr_loc(dut_done_expr, 184, 192, calyx_fileid);
205-
let cond_expr = calyx_go_done.e(Expr::Equal(dut_done_expr, one_expr));
206-
calyx_go_done.add_expr_loc(cond_expr, 183, 198, calyx_fileid);
207-
let not_expr = calyx_go_done.e(Expr::Not(cond_expr));
208-
calyx_go_done.add_expr_loc(not_expr, 182, 198, calyx_fileid);
209-
210-
// 4) create statements
211-
let while_body = vec![calyx_go_done.s(Stmt::Step)];
212-
let wbody = calyx_go_done.s(Stmt::Block(while_body));
152+
fn typecheck_add_transaction() {
153+
let mut handler = DiagnosticHandler::new();
154+
let (add, symbols) = create_add_transaction(&mut handler);
155+
type_check(&add, &symbols, &mut handler);
156+
}
213157

214-
let dut_ii_assign = calyx_go_done.s(Stmt::Assign(dut_ii, ii_expr));
215-
calyx_go_done.add_stmt_loc(dut_ii_assign, 143, 157, calyx_fileid);
216-
let dut_go_assign = calyx_go_done.s(Stmt::Assign(dut_go, one_expr));
217-
calyx_go_done.add_stmt_loc(dut_go_assign, 160, 172, calyx_fileid);
218-
let dut_while = calyx_go_done.s(Stmt::While(not_expr, wbody));
219-
calyx_go_done.add_stmt_loc(dut_while, 175, 219, calyx_fileid);
220-
let dut_go_reassign = calyx_go_done.s(Stmt::Assign(dut_go, zero_expr));
221-
calyx_go_done.add_stmt_loc(dut_go_reassign, 222, 234, calyx_fileid);
222-
let dut_ii_dontcare = calyx_go_done.s(Stmt::Assign(dut_ii, calyx_go_done.expr_dont_care()));
223-
calyx_go_done.add_stmt_loc(dut_ii_dontcare, 238, 250, calyx_fileid);
224-
let oo_assign = calyx_go_done.s(Stmt::Assign(oo, dut_oo_expr));
225-
calyx_go_done.add_stmt_loc(oo_assign, 254, 268, calyx_fileid);
226-
let body = vec![
227-
dut_ii_assign,
228-
dut_go_assign,
229-
dut_while,
230-
dut_go_reassign,
231-
dut_ii_dontcare,
232-
oo_assign,
233-
];
234-
calyx_go_done.body = calyx_go_done.s(Stmt::Block(body));
158+
#[test]
159+
fn typecheck_calyx_go_down_transaction() {
160+
let mut handler = DiagnosticHandler::new();
161+
let (calyx_go_done, symbols) = create_calyx_go_down_transaction(&mut handler);
235162
type_check(&calyx_go_done, &symbols, &mut handler);
236163
}
237164
}

0 commit comments

Comments
 (0)