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

224 lines
6.6 KiB
Rust
Raw Normal View History

2020-08-18 13:50:26 +03:00
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
2020-07-31 03:11:58 +03:00
assert_satisfied,
2020-08-17 03:20:47 +03:00
expect_compiler_error,
2020-08-01 05:39:30 +03:00
generate_main_input,
generate_test_input_u32,
2020-08-01 07:15:33 +03:00
get_output,
parse_program,
2020-08-01 05:39:30 +03:00
parse_program_with_input,
EdwardsTestCompiler,
};
2020-10-31 03:31:09 +03:00
use leo_ast::InputValue;
#[test]
fn test_assert() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("assert.leo");
let mut program_1_pass = parse_program(program_string).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_input = generate_main_input(vec![("a", generate_test_input_u32(1))]);
2020-07-31 03:11:58 +03:00
2020-08-01 06:59:50 +03:00
program_1_pass.set_main_input(main_input);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_1_pass);
// Check that an input value of 0 satisfies the constraint system
let main_input = generate_main_input(vec![("a", generate_test_input_u32(0))]);
2020-07-31 03:11:58 +03:00
2020-08-01 06:59:50 +03:00
program_0_pass.set_main_input(main_input);
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
let main_input = generate_main_input(vec![("a", generate_test_input_u32(2))]);
2020-07-31 03:11:58 +03:00
2020-08-01 06:59:50 +03:00
program_2_fail.set_main_input(main_input);
2020-07-31 03:11:58 +03:00
2020-08-17 03:20:47 +03:00
expect_compiler_error(program_2_fail);
}
#[test]
fn test_mutate() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("mutate.leo");
let mut program_1_pass = parse_program(program_string).unwrap();
2020-07-31 03:11:58 +03:00
let mut program_0_pass = program_1_pass.clone();
// Check that an input value of 1 satisfies the constraint system
let main_input = generate_main_input(vec![("a", generate_test_input_u32(1))]);
2020-07-31 03:11:58 +03:00
2020-08-01 06:59:50 +03:00
program_1_pass.set_main_input(main_input);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_1_pass);
// Check that an input value of 0 satisfies the constraint system
let main_input = generate_main_input(vec![("a", generate_test_input_u32(0))]);
2020-07-31 03:11:58 +03:00
2020-08-01 06:59:50 +03:00
program_0_pass.set_main_input(main_input);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_0_pass);
}
#[test]
fn test_for_loop() {
2020-12-04 23:48:43 +03:00
let program_string = include_str!("for_loop.leo");
let mut program_true_6 = parse_program(program_string).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 06:59:50 +03:00
let main_input = generate_main_input(vec![("a", Some(InputValue::Boolean(true)))]);
2020-07-31 03:11:58 +03:00
2020-08-01 06:59:50 +03:00
program_true_6.set_main_input(main_input);
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 06:59:50 +03:00
let main_input = generate_main_input(vec![("a", Some(InputValue::Boolean(false)))]);
2020-07-31 03:11:58 +03:00
2020-08-01 06:59:50 +03:00
program_false_0.set_main_input(main_input);
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-12-04 23:48:43 +03:00
let program_string = include_str!("chain.leo");
let mut program_1_1 = parse_program(program_string).unwrap();
2020-06-13 13:39:51 +03:00
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 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", generate_test_input_u32(1)),
("b", generate_test_input_u32(1)),
2020-07-31 03:11:58 +03:00
]);
2020-08-01 06:59:50 +03:00
program_1_1.set_main_input(main_input);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_1_1);
// Check that an input of 2 outputs 2
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", generate_test_input_u32(2)),
("b", generate_test_input_u32(2)),
2020-07-31 03:11:58 +03:00
]);
2020-08-01 06:59:50 +03:00
program_2_2.set_main_input(main_input);
2020-07-31 03:11:58 +03:00
assert_satisfied(program_2_2);
// Check that an input of 4 outputs 3
2020-08-01 06:59:50 +03:00
let main_input = generate_main_input(vec![
("a", generate_test_input_u32(4)),
("b", generate_test_input_u32(3)),
2020-07-31 03:11:58 +03:00
]);
2020-08-01 06:59:50 +03:00
program_4_3.set_main_input(main_input);
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-12-04 23:48:43 +03:00
let program_string = include_str!("nested.leo");
let mut program_true_true_3 = parse_program(program_string).unwrap();
2020-06-13 13:39:51 +03:00
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 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 03:11:58 +03:00
("a", Some(InputValue::Boolean(true))),
("b", Some(InputValue::Boolean(true))),
("c", generate_test_input_u32(3)),
2020-07-31 03:11:58 +03:00
]);
2020-08-01 06:59:50 +03:00
program_true_true_3.set_main_input(main_input);
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 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 03:11:58 +03:00
("a", Some(InputValue::Boolean(true))),
("b", Some(InputValue::Boolean(false))),
("c", generate_test_input_u32(1)),
2020-07-31 03:11:58 +03:00
]);
2020-08-01 06:59:50 +03:00
program_true_false_1.set_main_input(main_input);
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 06:59:50 +03:00
let main_input = generate_main_input(vec![
2020-07-31 03:11:58 +03:00
("a", Some(InputValue::Boolean(false))),
("b", Some(InputValue::Boolean(false))),
("c", generate_test_input_u32(0)),
2020-07-31 03:11:58 +03:00
]);
2020-06-13 13:39:51 +03:00
2020-08-01 06:59:50 +03:00
program_false_false_0.set_main_input(main_input);
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) {
2020-09-10 21:18:49 +03:00
let expected = include_bytes!("output/registers_one.out");
2020-08-01 07:15:33 +03:00
let actual = get_output(program);
2020-07-31 03:11:58 +03:00
assert_eq!(expected, actual.bytes().as_slice());
}
fn output_zero(program: EdwardsTestCompiler) {
2020-09-10 21:18:49 +03:00
let expected = include_bytes!("output/registers_zero.out");
2020-08-01 07:15:33 +03:00
let actual = get_output(program);
2020-07-31 03:11:58 +03:00
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() {
let program_string = include_str!("multiple_returns.leo");
2020-07-31 03:11:58 +03:00
// Check that an input value of 1 writes 1 to the output registers
let registers_one_string = include_str!("input/registers_one.in");
let program = parse_program_with_input(program_string, registers_one_string).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
let registers_zero_string = include_str!("input/registers_zero.in");
let program = parse_program_with_input(program_string, registers_zero_string).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
}