leo/compiler/tests/statements/conditional/mod.rs
2020-07-31 18:49:01 -07:00

250 lines
7.0 KiB
Rust

use crate::{
assert_satisfied,
expect_synthesis_error,
generate_main_inputs,
get_outputs,
parse_program,
parse_program_with_inputs,
EdwardsTestCompiler,
};
use leo_input::types::{IntegerType, U32Type};
use leo_types::InputValue;
#[test]
fn test_assert() {
let bytes = include_bytes!("assert.leo");
let mut program_1_pass = parse_program(bytes).unwrap();
let mut program_0_pass = program_1_pass.clone();
let mut program_2_fail = program_1_pass.clone();
// Check that an input value of 1 satisfies the constraint system
let main_inputs = generate_main_inputs(vec![(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
)]);
program_1_pass.set_main_inputs(main_inputs);
assert_satisfied(program_1_pass);
// Check that an input value of 0 satisfies the constraint system
let main_inputs = generate_main_inputs(vec![(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
)]);
program_0_pass.set_main_inputs(main_inputs);
assert_satisfied(program_0_pass);
// Check that an input value of 2 does not satisfy the constraint system
let main_inputs = generate_main_inputs(vec![(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 2.to_string())),
)]);
program_2_fail.set_main_inputs(main_inputs);
expect_synthesis_error(program_2_fail);
}
#[test]
fn test_mutate() {
let bytes = include_bytes!("mutate.leo");
let mut program_1_pass = parse_program(bytes).unwrap();
let mut program_0_pass = program_1_pass.clone();
// Check that an input value of 1 satisfies the constraint system
let main_inputs = generate_main_inputs(vec![(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
)]);
program_1_pass.set_main_inputs(main_inputs);
assert_satisfied(program_1_pass);
// Check that an input value of 0 satisfies the constraint system
let main_inputs = generate_main_inputs(vec![(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
)]);
program_0_pass.set_main_inputs(main_inputs);
assert_satisfied(program_0_pass);
}
#[test]
fn test_for_loop() {
let bytes = include_bytes!("for_loop.leo");
let mut program_true_6 = parse_program(bytes).unwrap();
let mut program_false_0 = program_true_6.clone();
// Check that an input value of true satisfies the constraint system
let main_inputs = generate_main_inputs(vec![("a", Some(InputValue::Boolean(true)))]);
program_true_6.set_main_inputs(main_inputs);
assert_satisfied(program_true_6);
// Check that an input value of false satisfies the constraint system
let main_inputs = generate_main_inputs(vec![("a", Some(InputValue::Boolean(false)))]);
program_false_0.set_main_inputs(main_inputs);
assert_satisfied(program_false_0);
}
#[test]
fn test_chain() {
let bytes = include_bytes!("chain.leo");
let mut program_1_1 = parse_program(bytes).unwrap();
let mut program_2_2 = program_1_1.clone();
let mut program_4_3 = program_1_1.clone();
// Check that an input of 1 outputs 1
let main_inputs = generate_main_inputs(vec![
(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
),
(
"b",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
),
]);
program_1_1.set_main_inputs(main_inputs);
assert_satisfied(program_1_1);
// Check that an input of 2 outputs 2
let main_inputs = generate_main_inputs(vec![
(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 2.to_string())),
),
(
"b",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 2.to_string())),
),
]);
program_2_2.set_main_inputs(main_inputs);
assert_satisfied(program_2_2);
// Check that an input of 4 outputs 3
let main_inputs = generate_main_inputs(vec![
(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 4.to_string())),
),
(
"b",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 3.to_string())),
),
]);
program_4_3.set_main_inputs(main_inputs);
assert_satisfied(program_4_3);
}
#[test]
fn test_nested() {
let bytes = include_bytes!("nested.leo");
let mut program_true_true_3 = parse_program(bytes).unwrap();
let mut program_true_false_1 = program_true_true_3.clone();
let mut program_false_false_0 = program_true_true_3.clone();
// Check that an input value of true true outputs 3
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Boolean(true))),
("b", Some(InputValue::Boolean(true))),
(
"c",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 3.to_string())),
),
]);
program_true_true_3.set_main_inputs(main_inputs);
assert_satisfied(program_true_true_3);
// Check that an input value of true false outputs 1
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Boolean(true))),
("b", Some(InputValue::Boolean(false))),
(
"c",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
),
]);
program_true_false_1.set_main_inputs(main_inputs);
assert_satisfied(program_true_false_1);
// Check that an input value of false false outputs 0
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Boolean(false))),
("b", Some(InputValue::Boolean(false))),
(
"c",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
),
]);
program_false_false_0.set_main_inputs(main_inputs);
assert_satisfied(program_false_false_0);
}
fn output_one(program: EdwardsTestCompiler) {
let expected = include_bytes!("outputs_/registers_one.out");
let actual = get_outputs(program);
assert_eq!(expected, actual.bytes().as_slice());
}
fn output_zero(program: EdwardsTestCompiler) {
let expected = include_bytes!("outputs_/registers_zero.out");
let actual = get_outputs(program);
assert_eq!(expected, actual.bytes().as_slice());
}
#[test]
fn test_multiple_returns() {
let program_bytes = include_bytes!("multiple_returns.leo");
// Check that an input value of 1 writes 1 to the output registers
let registers_one_bytes = include_bytes!("inputs/registers_one.in");
let program = parse_program_with_inputs(program_bytes, registers_one_bytes).unwrap();
output_one(program);
// Check that an input value of 0 writes 0 to the output registers
let registers_zero_bytes = include_bytes!("inputs/registers_zero.in");
let program = parse_program_with_inputs(program_bytes, registers_zero_bytes).unwrap();
output_zero(program);
}