2020-05-19 22:01:19 +03:00
|
|
|
use crate::compile_program;
|
|
|
|
|
|
|
|
use leo_compiler::{types::Integer, ConstrainedValue, InputValue};
|
|
|
|
|
2020-05-19 22:41:21 +03:00
|
|
|
use leo_compiler::compiler::Compiler;
|
|
|
|
use leo_compiler::errors::CompilerError;
|
|
|
|
use leo_compiler::errors::FunctionError;
|
|
|
|
use leo_compiler::errors::StatementError;
|
2020-05-19 22:01:19 +03:00
|
|
|
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
|
|
|
use snarkos_models::gadgets::r1cs::TestConstraintSystem;
|
|
|
|
use snarkos_models::gadgets::utilities::uint32::UInt32;
|
|
|
|
|
|
|
|
const DIRECTORY_NAME: &str = "tests/mutability/";
|
|
|
|
|
2020-05-19 22:41:21 +03:00
|
|
|
fn mut_success(program: Compiler<Fr, EdwardsProjective>) {
|
2020-05-19 22:01:19 +03:00
|
|
|
let mut cs = TestConstraintSystem::<Fr>::new();
|
|
|
|
let output = program.compile_constraints(&mut cs).unwrap();
|
|
|
|
println!("{}", output);
|
|
|
|
|
|
|
|
assert!(cs.is_satisfied());
|
|
|
|
assert_eq!(
|
|
|
|
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Integer(
|
|
|
|
Integer::U32(UInt32::constant(0))
|
|
|
|
)]),
|
|
|
|
output
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-19 22:41:21 +03:00
|
|
|
fn mut_fail(program: Compiler<Fr, EdwardsProjective>) {
|
|
|
|
let mut cs = TestConstraintSystem::<Fr>::new();
|
|
|
|
let err = program.compile_constraints(&mut cs).unwrap_err();
|
|
|
|
|
|
|
|
// It would be ideal if assert_eq!(Error1, Error2) were possible but unfortunately it is not due to
|
|
|
|
// https://github.com/rust-lang/rust/issues/34158#issuecomment-224910299
|
|
|
|
match err {
|
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(
|
|
|
|
StatementError::ImmutableAssign(_string),
|
|
|
|
)) => {}
|
|
|
|
err => panic!("Expected immutable assign error, got {}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_let() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "let.leo").unwrap();
|
|
|
|
mut_fail(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_let_mut() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "let_mut.leo").unwrap();
|
|
|
|
mut_success(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_array() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "array.leo").unwrap();
|
|
|
|
mut_fail(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_array_mut() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "array_mut.leo").unwrap();
|
|
|
|
mut_success(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_circuit() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "circuit.leo").unwrap();
|
|
|
|
mut_fail(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_circuit_mut() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "circuit_mut.leo").unwrap();
|
|
|
|
mut_success(program);
|
|
|
|
}
|
|
|
|
|
2020-05-19 22:01:19 +03:00
|
|
|
#[test]
|
|
|
|
fn test_function_input() {
|
|
|
|
let mut program = compile_program(DIRECTORY_NAME, "function_input.leo").unwrap();
|
|
|
|
program.set_inputs(vec![Some(InputValue::Integer(1))]);
|
2020-05-19 22:41:21 +03:00
|
|
|
mut_fail(program);
|
2020-05-19 22:01:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function_input_mut() {
|
|
|
|
let mut program = compile_program(DIRECTORY_NAME, "function_input_mut.leo").unwrap();
|
|
|
|
program.set_inputs(vec![Some(InputValue::Integer(1))]);
|
2020-05-19 22:41:21 +03:00
|
|
|
mut_success(program);
|
2020-05-19 22:01:19 +03:00
|
|
|
}
|