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/>.
|
|
|
|
|
2020-11-12 21:28:24 +03:00
|
|
|
use crate::{
|
|
|
|
assert_satisfied,
|
|
|
|
expect_compiler_error,
|
|
|
|
expect_type_inference_error,
|
|
|
|
get_output,
|
|
|
|
parse_program,
|
|
|
|
parse_program_with_input,
|
|
|
|
};
|
2020-07-30 22:10:33 +03:00
|
|
|
use leo_compiler::errors::{CompilerError, ExpressionError, FunctionError, StatementError};
|
2020-05-21 01:51:57 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("empty.leo");
|
2020-06-09 04:31:21 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:10:33 +03:00
|
|
|
assert_satisfied(program);
|
2020-05-21 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-30 22:10:33 +03:00
|
|
|
fn test_iteration() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("iteration.leo");
|
2020-06-09 04:31:21 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:10:33 +03:00
|
|
|
assert_satisfied(program);
|
2020-05-21 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-30 22:10:33 +03:00
|
|
|
fn test_iteration_repeated() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("iteration_repeated.leo");
|
2020-06-09 04:31:21 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:10:33 +03:00
|
|
|
assert_satisfied(program);
|
2020-05-21 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
2020-08-15 10:25:11 +03:00
|
|
|
#[test]
|
|
|
|
fn test_newlines() {
|
2020-12-04 23:20:59 +03:00
|
|
|
let input_string = include_str!("input/newlines.in");
|
|
|
|
let program_string = include_str!("newlines.leo");
|
|
|
|
let program = parse_program_with_input(program_string, input_string).unwrap();
|
2020-08-15 10:25:11 +03:00
|
|
|
|
2020-12-04 23:20:59 +03:00
|
|
|
let expected_string = include_str!("output/newlines.out");
|
2020-08-15 10:25:11 +03:00
|
|
|
let actual_bytes = get_output(program);
|
2020-12-04 23:20:59 +03:00
|
|
|
let actual_string = std::str::from_utf8(actual_bytes.bytes().as_slice()).unwrap();
|
2020-08-15 10:25:11 +03:00
|
|
|
|
2020-12-04 23:20:59 +03:00
|
|
|
assert_eq!(expected_string, actual_string);
|
2020-08-15 10:25:11 +03:00
|
|
|
}
|
|
|
|
|
2020-05-21 01:51:57 +03:00
|
|
|
#[test]
|
2020-07-30 22:10:33 +03:00
|
|
|
fn test_multiple_returns() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("multiple.leo");
|
2020-06-09 04:31:21 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:10:33 +03:00
|
|
|
assert_satisfied(program);
|
2020-05-21 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-30 22:10:33 +03:00
|
|
|
fn test_multiple_returns_main() {
|
2020-12-04 23:20:59 +03:00
|
|
|
let program_string = include_str!("multiple_main.leo");
|
|
|
|
let input_string = include_str!("input/registers.in");
|
2020-06-09 04:31:21 +03:00
|
|
|
|
2020-12-04 23:20:59 +03:00
|
|
|
let program = parse_program_with_input(program_string, input_string).unwrap();
|
2020-07-30 22:10:33 +03:00
|
|
|
|
2020-12-04 23:20:59 +03:00
|
|
|
let expected_string = include_str!("output/registers.out");
|
2020-08-01 07:15:33 +03:00
|
|
|
let actual_bytes = get_output(program);
|
2020-12-04 23:20:59 +03:00
|
|
|
let actual_string = std::str::from_utf8(actual_bytes.bytes().as_slice()).unwrap();
|
2020-05-21 01:51:57 +03:00
|
|
|
|
2020-12-04 23:20:59 +03:00
|
|
|
assert_eq!(expected_string, actual_string);
|
2020-07-30 22:10:33 +03:00
|
|
|
}
|
2020-05-21 01:51:57 +03:00
|
|
|
|
|
|
|
#[test]
|
2020-07-30 22:10:33 +03:00
|
|
|
fn test_repeated_function_call() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("repeated.leo");
|
2020-06-09 04:31:21 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:10:33 +03:00
|
|
|
assert_satisfied(program);
|
2020-05-21 01:51:57 +03:00
|
|
|
}
|
2020-06-20 08:07:02 +03:00
|
|
|
|
2020-05-21 01:51:57 +03:00
|
|
|
#[test]
|
2020-07-30 22:10:33 +03:00
|
|
|
fn test_return() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("return.leo");
|
2020-06-09 04:31:21 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:10:33 +03:00
|
|
|
assert_satisfied(program);
|
2020-05-21 01:51:57 +03:00
|
|
|
}
|
2020-06-23 01:42:46 +03:00
|
|
|
|
|
|
|
#[test]
|
2020-07-30 22:10:33 +03:00
|
|
|
fn test_scope_fail() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("scope_fail.leo");
|
2020-06-23 01:42:46 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:10:33 +03:00
|
|
|
match expect_compiler_error(program) {
|
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
|
|
|
ExpressionError::FunctionError(value),
|
|
|
|
))) => match *value {
|
|
|
|
FunctionError::StatementError(StatementError::ExpressionError(ExpressionError::Error(_))) => {}
|
|
|
|
error => panic!("Expected function undefined, got {}", error),
|
|
|
|
},
|
|
|
|
error => panic!("Expected function undefined, got {}", error),
|
|
|
|
}
|
2020-06-23 01:42:46 +03:00
|
|
|
}
|
2020-07-30 22:10:33 +03:00
|
|
|
|
2020-11-12 21:28:24 +03:00
|
|
|
#[test]
|
|
|
|
fn test_undefined() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("undefined.leo");
|
2020-11-12 21:28:24 +03:00
|
|
|
let error = parse_program(bytes).err().unwrap();
|
|
|
|
|
|
|
|
expect_type_inference_error(error);
|
|
|
|
}
|
2020-06-23 04:28:30 +03:00
|
|
|
|
|
|
|
#[test]
|
2020-07-30 22:10:33 +03:00
|
|
|
fn test_value_unchanged() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("value_unchanged.leo");
|
2020-06-23 04:28:30 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-07-30 22:10:33 +03:00
|
|
|
assert_satisfied(program);
|
2020-06-23 04:28:30 +03:00
|
|
|
}
|
2020-09-17 22:46:55 +03:00
|
|
|
|
2020-11-12 21:28:24 +03:00
|
|
|
#[test]
|
|
|
|
fn test_array_input() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("array_input.leo");
|
2020-11-12 21:28:24 +03:00
|
|
|
let error = parse_program(bytes).err().unwrap();
|
|
|
|
|
|
|
|
expect_type_inference_error(error)
|
|
|
|
}
|
2020-10-26 23:14:36 +03:00
|
|
|
|
2020-09-17 22:46:55 +03:00
|
|
|
// Test return multidimensional arrays
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_return_array_nested_fail() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("return_array_nested_fail.leo");
|
2020-11-10 06:26:34 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
2020-09-17 22:46:55 +03:00
|
|
|
|
2020-11-10 06:26:34 +03:00
|
|
|
let _err = expect_compiler_error(program);
|
2020-09-17 22:46:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_return_array_nested_pass() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("return_array_nested_pass.leo");
|
2020-09-17 22:46:55 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_return_array_tuple_fail() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("return_array_tuple_fail.leo");
|
2020-11-10 06:26:34 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
2020-09-17 22:46:55 +03:00
|
|
|
|
2020-11-10 06:26:34 +03:00
|
|
|
let _err = expect_compiler_error(program);
|
2020-09-17 22:46:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_return_array_tuple_pass() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("return_array_tuple_pass.leo");
|
2020-09-17 22:46:55 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
|
|
|
}
|
2020-11-19 05:28:41 +03:00
|
|
|
|
2020-11-19 05:13:49 +03:00
|
|
|
// Test return tuples
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_return_tuple() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("return_tuple.leo");
|
2020-11-19 05:13:49 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
|
|
|
}
|
2020-11-24 00:57:22 +03:00
|
|
|
|
2020-11-19 05:28:41 +03:00
|
|
|
#[test]
|
|
|
|
fn test_return_tuple_conditional() {
|
2020-12-04 23:34:51 +03:00
|
|
|
let bytes = include_str!("return_tuple_conditional.leo");
|
2020-11-19 05:28:41 +03:00
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
assert_satisfied(program);
|
|
|
|
}
|