2020-05-30 03:34:31 +03:00
|
|
|
use crate::{
|
|
|
|
compile_program, get_error, get_output, integer::u32::output_one, EdwardsConstrainedValue,
|
|
|
|
EdwardsTestCompiler,
|
|
|
|
};
|
2020-05-21 01:51:57 +03:00
|
|
|
use leo_compiler::{
|
|
|
|
errors::{CompilerError, ExpressionError, FunctionError, StatementError},
|
|
|
|
ConstrainedValue,
|
|
|
|
};
|
2020-06-02 04:35:43 +03:00
|
|
|
|
2020-05-21 01:51:57 +03:00
|
|
|
use snarkos_models::gadgets::utilities::boolean::Boolean;
|
|
|
|
|
|
|
|
const DIRECTORY_NAME: &str = "tests/function/";
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
pub(crate) fn output_empty(program: EdwardsTestCompiler) {
|
2020-05-21 01:51:57 +03:00
|
|
|
let output = get_output(program);
|
2020-05-30 03:34:31 +03:00
|
|
|
assert_eq!(
|
|
|
|
EdwardsConstrainedValue::Return(vec![]).to_string(),
|
|
|
|
output.to_string()
|
|
|
|
);
|
2020-05-21 01:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// (true, false)
|
2020-05-30 03:34:31 +03:00
|
|
|
pub(crate) fn output_multiple(program: EdwardsTestCompiler) {
|
2020-05-21 01:51:57 +03:00
|
|
|
let output = get_output(program);
|
|
|
|
assert_eq!(
|
2020-05-30 03:34:31 +03:00
|
|
|
EdwardsConstrainedValue::Return(vec![
|
2020-05-21 01:51:57 +03:00
|
|
|
ConstrainedValue::Boolean(Boolean::Constant(true)),
|
|
|
|
ConstrainedValue::Boolean(Boolean::Constant(false))
|
2020-05-30 03:34:31 +03:00
|
|
|
])
|
|
|
|
.to_string(),
|
|
|
|
output.to_string()
|
2020-05-21 01:51:57 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
fn fail_undefined_identifier(program: EdwardsTestCompiler) {
|
2020-05-21 01:51:57 +03:00
|
|
|
match get_error(program) {
|
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(
|
|
|
|
StatementError::ExpressionError(ExpressionError::UndefinedIdentifier(_)),
|
|
|
|
)) => {}
|
|
|
|
error => panic!("Expected function undefined, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inline function call
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "empty.leo").unwrap();
|
|
|
|
output_empty(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_return() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "return.leo").unwrap();
|
|
|
|
output_one(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_undefined() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "undefined.leo").unwrap();
|
|
|
|
fail_undefined_identifier(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function scope
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_global_scope_fail() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "scope_fail.leo").unwrap();
|
|
|
|
match get_error(program) {
|
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(
|
|
|
|
StatementError::ExpressionError(ExpressionError::FunctionError(value)),
|
|
|
|
)) => match *value {
|
|
|
|
FunctionError::StatementError(StatementError::ExpressionError(
|
|
|
|
ExpressionError::UndefinedIdentifier(_),
|
|
|
|
)) => {}
|
|
|
|
error => panic!("Expected function undefined, got {}", error),
|
|
|
|
},
|
|
|
|
error => panic!("Expected function undefined, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_value_unchanged() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "value_unchanged.leo").unwrap();
|
|
|
|
output_one(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multiple returns
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple_returns() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "multiple.leo").unwrap();
|
|
|
|
output_multiple(program);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_multiple_returns_main() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "multiple_main.leo").unwrap();
|
|
|
|
output_multiple(program);
|
|
|
|
}
|