1
1
mirror of https://github.com/ProvableHQ/leo.git synced 2024-12-25 11:12:48 +03:00
leo/compiler/tests/array/mod.rs

692 lines
19 KiB
Rust
Raw Normal View History

2021-02-02 07:26:56 +03:00
// Copyright (C) 2019-2021 Aleo Systems Inc.
2020-08-18 13:50:26 +03:00
// 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-30 10:56:17 +03:00
assert_satisfied,
2020-12-17 00:02:31 +03:00
expect_asg_error,
2020-07-30 21:11:54 +03:00
expect_compiler_error,
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-07-30 10:56:17 +03:00
pub fn output_ones(program: EdwardsTestCompiler) {
2020-08-17 05:14:26 +03:00
let expected = include_bytes!("output/registers_ones.out");
2020-08-01 07:15:33 +03:00
let actual = get_output(program);
2020-07-30 10:56:17 +03:00
assert!(expected.eq(actual.bytes().as_slice()));
}
2020-07-30 10:56:17 +03:00
pub fn output_zeros(program: EdwardsTestCompiler) {
2020-08-17 05:14:26 +03:00
let expected = include_bytes!("output/registers_zeros.out");
2020-08-01 07:15:33 +03:00
let actual = get_output(program);
2020-07-30 10:56:17 +03:00
assert!(expected.eq(actual.bytes().as_slice()));
2020-06-11 02:14:55 +03:00
}
2020-07-30 10:56:17 +03:00
// Registers
#[test]
2020-07-30 10:56:17 +03:00
fn test_registers() {
let program_string = include_str!("registers.leo");
let ones_input_string = include_str!("input/registers_ones.in");
let zeros_input_string = include_str!("input/registers_zeros.in");
2020-07-30 10:56:17 +03:00
// test ones input register => ones output register
let program = parse_program_with_input(program_string, ones_input_string).unwrap();
2020-06-09 03:51:06 +03:00
output_ones(program);
2020-07-30 10:56:17 +03:00
// test zeros input register => zeros output register
let program = parse_program_with_input(program_string, zeros_input_string).unwrap();
2020-06-09 03:51:06 +03:00
2020-07-30 10:56:17 +03:00
output_zeros(program);
}
2020-07-30 10:56:17 +03:00
// Expressions
#[test]
2020-07-30 10:56:17 +03:00
fn test_inline() {
let program_string = include_str!("inline.leo");
let input_string = include_str!("input/three_ones.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
2020-06-09 03:51:06 +03:00
2020-07-30 10:56:17 +03:00
assert_satisfied(program);
}
2020-12-17 00:02:31 +03:00
#[test]
fn test_nested() {
let program_string = include_str!("nested.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
2020-07-30 10:56:17 +03:00
fn test_inline_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("inline.leo");
let program = parse_program(program_string).unwrap();
2020-06-09 03:51:06 +03:00
2020-07-30 21:11:54 +03:00
let _err = expect_compiler_error(program);
}
#[test]
2020-07-30 10:56:17 +03:00
fn test_initializer() {
let program_string = include_str!("initializer.leo");
let input_string = include_str!("input/three_ones.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
2020-06-09 03:51:06 +03:00
2020-07-30 10:56:17 +03:00
assert_satisfied(program);
}
#[test]
fn test_initializer_fail() {
let program_string = include_str!("initializer_fail.leo");
let input_string = include_str!("input/three_ones.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
assert!(syntax_error);
}
2020-09-03 02:04:41 +03:00
#[test]
fn test_initializer_input() {
let program_string = include_str!("initializer_input.leo");
let input_string = include_str!("input/six_zeros.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
2020-09-03 02:04:41 +03:00
assert_satisfied(program);
}
#[test]
fn test_initializer_input_fail() {
let program_string = include_str!("initializer_input.leo");
let input_string = include_str!("input/initializer_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
2020-09-03 02:04:41 +03:00
assert!(syntax_error);
}
#[test]
fn test_input_nested_3x2() {
let program_string = include_str!("input_nested_3x2.leo");
let input_string = include_str!("input/input_nested_3x2.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
2020-06-09 03:51:06 +03:00
2020-07-30 10:56:17 +03:00
assert_satisfied(program);
}
#[test]
fn test_input_nested_3x2_fail() {
let program_string = include_str!("input_nested_3x2_fail.leo");
let input_string = include_str!("input/input_nested_3x2_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
2020-06-09 03:51:06 +03:00
assert!(syntax_error);
}
#[test]
fn test_input_tuple_3x2() {
let program_string = include_str!("input_tuple_3x2.leo");
let input_string = include_str!("input/input_tuple_3x2.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
2020-06-09 03:51:06 +03:00
2020-07-30 10:56:17 +03:00
assert_satisfied(program);
}
2020-09-02 21:40:58 +03:00
#[test]
fn test_input_tuple_3x2_fail() {
let program_string = include_str!("input_tuple_3x2_fail.leo");
let input_string = include_str!("input/input_tuple_3x2_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
2020-09-03 10:30:57 +03:00
assert!(syntax_error);
2020-09-03 10:30:57 +03:00
}
#[test]
fn test_multi_fail_initializer() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("multi_fail_initializer.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-09-02 21:40:58 +03:00
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
2020-09-02 21:40:58 +03:00
}
#[test]
fn test_multi_inline_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("multi_fail_inline.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-09-02 21:40:58 +03:00
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
2020-09-02 21:40:58 +03:00
}
2020-09-03 02:49:06 +03:00
#[test]
fn test_multi_initializer() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("multi_initializer.leo");
let program = parse_program(program_string).unwrap();
2020-09-03 02:49:06 +03:00
assert_satisfied(program);
}
#[test]
fn test_multi_initializer_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("multi_initializer_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-09-03 02:49:06 +03:00
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
2020-09-03 02:49:06 +03:00
}
#[test]
fn test_nested_3x2_value() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("nested_3x2_value.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_nested_3x2_value_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("nested_3x2_value_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
#[test]
fn test_tuple_3x2_value() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("tuple_3x2_value.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_tuple_3x2_value_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("tuple_3x2_value_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
#[test]
fn test_spread() {
let program_string = include_str!("spread.leo");
let input_string = include_str!("input/three_ones.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_slice() {
let program_string = include_str!("slice.leo");
let input_string = include_str!("input/three_ones.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
2020-12-10 01:42:15 +03:00
#[test]
fn test_slice_lower() {
let program_string = include_str!("slice_lower.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
// Array type tests
#[test]
fn test_type_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_fail.leo");
let syntax_error = parse_program(program_string).is_err();
assert!(syntax_error);
}
#[test]
fn test_type_nested_value_nested_3x2() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_nested_value_nested_3x2.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_type_nested_value_nested_3x2_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_nested_value_nested_3x2_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
#[test]
fn test_type_nested_value_nested_4x3x2() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_nested_value_nested_4x3x2.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_type_nested_value_nested_4x3x2_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_nested_value_nested_4x3x2_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
#[test]
fn test_type_nested_value_tuple_3x2() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_nested_value_tuple_3x2.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_type_nested_value_tuple_3x2_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_nested_value_tuple_3x2_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
#[test]
fn test_type_nested_value_tuple_4x3x2() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_nested_value_tuple_4x3x2.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_type_nested_value_tuple_4x3x2_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_nested_value_tuple_4x3x2_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
#[test]
fn test_type_tuple_value_nested_3x2() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_tuple_value_nested_3x2.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_type_tuple_value_nested_3x2_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_tuple_value_nested_3x2_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
#[test]
fn test_type_tuple_value_nested_4x3x2() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_tuple_value_nested_4x3x2.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_type_tuple_value_nested_4x3x2_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_tuple_value_nested_4x3x2_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
#[test]
fn test_type_tuple_value_tuple_3x2() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_tuple_value_tuple_3x2.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_type_tuple_value_tuple_3x2_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_tuple_value_tuple_3x2_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
#[test]
fn test_type_tuple_value_tuple_4x3x2() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_tuple_value_tuple_4x3x2.leo");
let program = parse_program(program_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_type_tuple_value_tuple_4x3x2_fail() {
2020-12-04 23:34:51 +03:00
let program_string = include_str!("type_tuple_value_tuple_4x3x2_fail.leo");
2020-12-17 00:02:31 +03:00
let error = parse_program(program_string).err().unwrap();
2020-12-17 00:02:31 +03:00
expect_asg_error(error);
}
// Tests for nested multi-dimensional arrays as input to the program
#[test]
fn test_input_type_nested_value_nested_3x2() {
let program_string = include_str!("type_input_3x2.leo");
let input_string = include_str!("input/type_nested_value_nested_3x2.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_input_type_nested_value_nested_3x2_fail() {
let program_string = include_str!("type_input_3x2.leo");
let input_string = include_str!("input/type_nested_value_nested_3x2_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
assert!(syntax_error);
}
#[test]
fn test_input_type_nested_value_nested_4x3x2() {
let program_string = include_str!("type_input_4x3x2.leo");
let input_string = include_str!("input/type_nested_value_nested_4x3x2.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_input_type_nested_value_nested_4x3x2_fail() {
let program_string = include_str!("type_input_4x3x2.leo");
let input_string = include_str!("input/type_nested_value_nested_4x3x2_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
assert!(syntax_error);
}
#[test]
fn test_input_type_nested_value_tuple_3x2() {
let program_string = include_str!("type_input_3x2.leo");
let input_string = include_str!("input/type_nested_value_tuple_3x2.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_input_type_nested_value_tuple_3x2_fail() {
let program_string = include_str!("type_input_3x2.leo");
let input_string = include_str!("input/type_nested_value_tuple_3x2_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
assert!(syntax_error);
}
#[test]
fn test_input_type_nested_value_tuple_4x3x2() {
let program_string = include_str!("type_input_4x3x2.leo");
let input_string = include_str!("input/type_nested_value_tuple_4x3x2.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program)
}
#[test]
fn test_input_type_nested_value_tuple_4x3x2_fail() {
let program_string = include_str!("type_input_4x3x2.leo");
let input_string = include_str!("input/type_nested_value_tuple_4x3x2_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
assert!(syntax_error);
}
// Tests for multi-dimensional arrays using tuple syntax as input to the program
#[test]
fn test_input_type_tuple_value_nested_3x2() {
let program_string = include_str!("type_input_3x2.leo");
let input_string = include_str!("input/type_tuple_value_nested_3x2.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_input_type_tuple_value_nested_3x2_fail() {
let program_string = include_str!("type_input_3x2.leo");
let input_string = include_str!("input/type_tuple_value_nested_3x2_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
assert!(syntax_error);
}
#[test]
fn test_input_type_tuple_value_nested_4x3x2() {
let program_string = include_str!("type_input_4x3x2.leo");
let input_string = include_str!("input/type_tuple_value_nested_4x3x2.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_input_type_tuple_value_nested_4x3x2_fail() {
let program_string = include_str!("type_input_4x3x2.leo");
let input_string = include_str!("input/type_tuple_value_nested_4x3x2_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
assert!(syntax_error);
}
#[test]
fn test_input_type_tuple_value_tuple_3x2() {
let program_string = include_str!("type_input_3x2.leo");
let input_string = include_str!("input/type_tuple_value_tuple_3x2.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_input_type_tuple_value_tuple_3x2_fail() {
let program_string = include_str!("type_input_3x2.leo");
let input_string = include_str!("input/type_tuple_value_tuple_3x2_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
assert!(syntax_error);
}
#[test]
fn test_input_type_tuple_value_tuple_4x3x2() {
let program_string = include_str!("type_input_4x3x2.leo");
let input_string = include_str!("input/type_tuple_value_tuple_4x3x2.in");
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_input_type_tuple_value_tuple_4x3x2_fail() {
let program_string = include_str!("type_input_4x3x2.leo");
let input_string = include_str!("input/type_tuple_value_tuple_4x3x2_fail.in");
let syntax_error = parse_program_with_input(program_string, input_string).is_err();
assert!(syntax_error);
}
2020-12-17 00:02:31 +03:00
#[test]
fn test_variable_slice_fail() {
let program_string = include_str!("variable_slice_fail.leo");
let error = parse_program(program_string).err().unwrap();
expect_asg_error(error);
}
2021-03-22 19:38:01 +03:00
#[test]
fn test_array_index() {
let program_string = r#"
function main(i: u32) {
let b = [1u8, 2, 3, 4];
console.assert(2 == b[i]);
console.assert(3 == b[2]);
}
"#;
let input_string = r#"
[main]
i: u32 = 1;
"#;
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_array_index_bounds_fail() {
let program_string = r#"
function main(i: u32) {
let b = [1u8, 2, 3, 4];
console.assert(2 == b[i]);
}
"#;
let input_string = r#"
[main]
i: u32 = 4;
"#;
let program = parse_program_with_input(program_string, input_string).unwrap();
expect_compiler_error(program);
}
#[test]
fn test_array_range_index() {
let program_string = r#"
function main(i: u32) {
let b = [1u8, 2, 3, 4];
console.assert([1u8, 2] == b[0..i]);
console.assert([3u8, 4] == b[i..4]);
}
"#;
let input_string = r#"
[main]
i: u32 = 2;
"#;
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_array_range_index_dyn() {
let program_string = r#"
function main(i: u32) {
let b = [1u8, 2, 3, 4];
console.assert([1u8, 2] == b[..i]);
console.assert([3u8, 4] == b[i..]);
}
"#;
let input_string = r#"
[main]
i: u32 = 2;
"#;
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_array_range_index_full_dyn() {
let program_string = r#"
function main(i: u32, y: u32) {
let b = [1u8, 2, 3, 4];
console.assert([3u8, 4] == b[i..y]);
}
"#;
let input_string = r#"
[main]
i: u32 = 2;
y: u32 = 4;
"#;
let program = parse_program_with_input(program_string, input_string).unwrap();
assert_satisfied(program);
}
#[test]
fn test_array_range_index_fail_bounds() {
let program_string = r#"
function main(i: u32, y: u32) {
let b = [1u8, 2, 3, 4];
console.assert([1, 2] == b[3..5]);
}
"#;
let input_string = r#"
[main]
i: u32 = 2;
"#;
let err = parse_program_with_input(program_string, input_string).is_err();
assert!(err);
}
#[test]
fn test_array_range_index_full_dyn_resized_fail() {
let program_string = r#"
function main(i: u32, y: u32) {
let b = [1u8, 2, 3, 4];
console.assert([3u8, 4] == b[i..y]);
}
"#;
let input_string = r#"
[main]
i: u32 = 1;
y: u32 = 4;
"#;
let program = parse_program_with_input(program_string, input_string).unwrap();
expect_compiler_error(program);
}
#[test]
fn test_array_range_index_full_dyn_bounds_fail() {
let program_string = r#"
function main(i: u32, y: u32) {
let b = [1u8, 2, 3, 4];
console.assert([3u8, 4] == b[i..y]);
}
"#;
let input_string = r#"
[main]
i: u32 = 3;
y: u32 = 5;
"#;
let program = parse_program_with_input(program_string, input_string).unwrap();
expect_compiler_error(program);
}