leo/compiler/tests/statements/conditional/mod.rs

250 lines
7.0 KiB
Rust
Raw Normal View History

use crate::{
2020-07-31 03:11:58 +03:00
assert_satisfied,
expect_synthesis_error,
2020-08-01 05:39:30 +03:00
generate_main_input,
2020-07-31 03:11:58 +03:00
get_outputs,
parse_program,
2020-08-01 05:39:30 +03:00
parse_program_with_input,
EdwardsTestCompiler,
};
2020-08-01 04:49:01 +03:00
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
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![(
2020-07-31 03:11:58 +03:00
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
)]);
2020-08-01 05:39:30 +03:00
program_1_pass.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_1_pass);
// Check that an input value of 0 satisfies the constraint system
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![(
2020-07-31 03:11:58 +03:00
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
)]);
2020-08-01 05:39:30 +03:00
program_0_pass.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_0_pass);
// Check that an input value of 2 does not satisfy the constraint system
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![(
2020-07-31 03:11:58 +03:00
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 2.to_string())),
)]);
2020-08-01 05:39:30 +03:00
program_2_fail.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
expect_synthesis_error(program_2_fail);
}
#[test]
fn test_mutate() {
let bytes = include_bytes!("mutate.leo");
2020-07-31 03:11:58 +03:00
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
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![(
2020-07-31 03:11:58 +03:00
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
)]);
2020-08-01 05:39:30 +03:00
program_1_pass.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_1_pass);
// Check that an input value of 0 satisfies the constraint system
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![(
2020-07-31 03:11:58 +03:00
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
)]);
2020-08-01 05:39:30 +03:00
program_0_pass.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
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();
2020-06-13 13:39:51 +03:00
// Check that an input value of true satisfies the constraint system
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![("a", Some(InputValue::Boolean(true)))]);
2020-07-31 03:11:58 +03:00
2020-08-01 05:39:30 +03:00
program_true_6.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_true_6);
2020-06-13 13:39:51 +03:00
// Check that an input value of false satisfies the constraint system
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![("a", Some(InputValue::Boolean(false)))]);
2020-07-31 03:11:58 +03:00
2020-08-01 05:39:30 +03:00
program_false_0.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_false_0);
}
2020-06-13 13:39:51 +03:00
#[test]
fn test_chain() {
2020-06-13 13:39:51 +03:00
let bytes = include_bytes!("chain.leo");
let mut program_1_1 = parse_program(bytes).unwrap();
let mut program_2_2 = program_1_1.clone();
2020-07-31 03:11:58 +03:00
let mut program_4_3 = program_1_1.clone();
// Check that an input of 1 outputs 1
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![
2020-07-31 03:11:58 +03:00
(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
),
(
"b",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
),
]);
2020-08-01 05:39:30 +03:00
program_1_1.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_1_1);
// Check that an input of 2 outputs 2
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![
2020-07-31 03:11:58 +03:00
(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 2.to_string())),
),
(
"b",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 2.to_string())),
),
]);
2020-08-01 05:39:30 +03:00
program_2_2.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_2_2);
// Check that an input of 4 outputs 3
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![
2020-07-31 03:11:58 +03:00
(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 4.to_string())),
),
(
"b",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 3.to_string())),
),
]);
2020-08-01 05:39:30 +03:00
program_4_3.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_4_3);
2020-06-13 13:39:51 +03:00
}
#[test]
fn test_nested() {
2020-06-13 13:39:51 +03:00
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();
2020-07-31 03:11:58 +03:00
// Check that an input value of true true outputs 3
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![
2020-07-31 03:11:58 +03:00
("a", Some(InputValue::Boolean(true))),
("b", Some(InputValue::Boolean(true))),
(
"c",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 3.to_string())),
),
]);
2020-08-01 05:39:30 +03:00
program_true_true_3.set_main_input(main_inputs);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_true_true_3);
// Check that an input value of true false outputs 1
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![
2020-07-31 03:11:58 +03:00
("a", Some(InputValue::Boolean(true))),
("b", Some(InputValue::Boolean(false))),
(
"c",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
),
]);
2020-08-01 05:39:30 +03:00
program_true_false_1.set_main_input(main_inputs);
2020-06-13 13:39:51 +03:00
2020-07-31 03:11:58 +03:00
assert_satisfied(program_true_false_1);
2020-06-13 13:39:51 +03:00
2020-07-31 03:11:58 +03:00
// Check that an input value of false false outputs 0
2020-06-13 13:39:51 +03:00
2020-08-01 05:39:30 +03:00
let main_inputs = generate_main_input(vec![
2020-07-31 03:11:58 +03:00
("a", Some(InputValue::Boolean(false))),
("b", Some(InputValue::Boolean(false))),
(
"c",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
),
]);
2020-06-13 13:39:51 +03:00
2020-08-01 05:39:30 +03:00
program_false_false_0.set_main_input(main_inputs);
2020-06-13 13:39:51 +03:00
2020-07-31 03:11:58 +03:00
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());
2020-06-13 13:39:51 +03:00
}
2020-06-24 09:56:51 +03:00
#[test]
fn test_multiple_returns() {
2020-07-31 03:11:58 +03:00
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");
2020-08-01 05:39:30 +03:00
let program = parse_program_with_input(program_bytes, registers_one_bytes).unwrap();
2020-06-24 09:56:51 +03:00
2020-07-31 03:11:58 +03:00
output_one(program);
2020-06-24 09:56:51 +03:00
2020-07-31 03:11:58 +03:00
// Check that an input value of 0 writes 0 to the output registers
2020-06-24 09:56:51 +03:00
2020-07-31 03:11:58 +03:00
let registers_zero_bytes = include_bytes!("inputs/registers_zero.in");
2020-08-01 05:39:30 +03:00
let program = parse_program_with_input(program_bytes, registers_zero_bytes).unwrap();
2020-06-24 09:56:51 +03:00
2020-07-31 03:11:58 +03:00
output_zero(program);
2020-06-24 09:56:51 +03:00
}