fix mutability tests

This commit is contained in:
collin 2020-07-30 16:37:01 -07:00
parent d4eae685a3
commit f334ba646d
7 changed files with 32 additions and 56 deletions

View File

@ -9,7 +9,7 @@ pub mod import;
pub mod inputs; pub mod inputs;
pub mod integers; pub mod integers;
pub mod macros; pub mod macros;
// pub mod mutability; pub mod mutability;
// pub mod statements; // pub mod statements;
// pub mod syntax; // pub mod syntax;

View File

@ -1,7 +1,7 @@
// Adding the `mut` keyword makes an array variable mutable. // Adding the `mut` keyword makes an array variable mutable.
function main() -> u32 { function main() {
let mut a = [1u32]; let mut a = [1u32];
a[0] = 0; a[0] = 0;
return a[0] assert_eq!(a[0], 0u32);
} }

View File

@ -3,9 +3,9 @@ circuit Foo {
x: u32 x: u32
} }
function main() -> u32 { function main() {
let mut a = Foo { x: 1 }; let mut a = Foo { x: 1 };
a.x = 0; a.x = 0;
return a.x assert_eq!(a.x, 0u32);
} }

View File

@ -1,4 +1,4 @@
// Function inputs are immutable by default. // Function inputs are immutable by default.
function main(a: u32) { function main(a: bool) {
a = 0; a = false;
} }

View File

@ -1,6 +1,6 @@
// Adding the `mut` keyword makes a function variable mutable. // Adding the `mut` keyword makes a function variable mutable.
function main(mut a: u32) -> u32 { function main(mut a: bool) {
a = 0; a = true;
return a assert_eq!(a, true);
} }

View File

@ -1,7 +1,7 @@
// Adding the `mut` keyword makes a variable mutable. // Adding the `mut` keyword makes a variable mutable.
function main() -> u32 { function main() {
let mut a = 1u32; let mut a = 1u32;
a = 0; a = 0;
return a assert_eq!(a, 0u32);
} }

View File

@ -1,42 +1,12 @@
use crate::{array::input_value_u32_one, parse_program, EdwardsConstrainedValue, EdwardsTestCompiler}; use crate::{assert_satisfied, expect_compiler_error, generate_main_inputs, parse_program};
use leo_compiler::{ use leo_types::InputValue;
errors::{CompilerError, FunctionError, StatementError},
ConstrainedValue,
Integer,
};
use snarkos_curves::edwards_bls12::Fq;
use snarkos_models::gadgets::{r1cs::TestConstraintSystem, utilities::uint::UInt32};
fn mut_success(program: EdwardsTestCompiler) {
let mut cs = TestConstraintSystem::<Fq>::new();
let output = program.compile_constraints(&mut cs).unwrap();
assert!(cs.is_satisfied());
assert_eq!(
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Integer(Integer::U32(UInt32::constant(0)))]).to_string(),
output.to_string()
);
}
fn mut_fail(program: EdwardsTestCompiler) {
let mut cs = TestConstraintSystem::<Fq>::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::Error(_string))) => {}
err => panic!("Expected immutable assign error, got {}", err),
}
}
#[test] #[test]
fn test_let() { fn test_let() {
let bytes = include_bytes!("let.leo"); let bytes = include_bytes!("let.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
mut_fail(program); expect_compiler_error(program);
} }
#[test] #[test]
@ -44,7 +14,7 @@ fn test_let_mut() {
let bytes = include_bytes!("let_mut.leo"); let bytes = include_bytes!("let_mut.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
mut_success(program); assert_satisfied(program);
} }
#[test] #[test]
@ -52,7 +22,7 @@ fn test_const_fail() {
let bytes = include_bytes!("const.leo"); let bytes = include_bytes!("const.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
mut_fail(program); expect_compiler_error(program);
} }
#[test] #[test]
@ -60,7 +30,7 @@ fn test_const_mut_fail() {
let bytes = include_bytes!("const_mut.leo"); let bytes = include_bytes!("const_mut.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
mut_fail(program); expect_compiler_error(program);
} }
#[test] #[test]
@ -68,7 +38,7 @@ fn test_array() {
let bytes = include_bytes!("array.leo"); let bytes = include_bytes!("array.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
mut_fail(program); expect_compiler_error(program);
} }
#[test] #[test]
@ -76,7 +46,7 @@ fn test_array_mut() {
let bytes = include_bytes!("array_mut.leo"); let bytes = include_bytes!("array_mut.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
mut_success(program); assert_satisfied(program);
} }
#[test] #[test]
@ -84,7 +54,7 @@ fn test_circuit() {
let bytes = include_bytes!("circuit.leo"); let bytes = include_bytes!("circuit.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
mut_fail(program); expect_compiler_error(program);
} }
#[test] #[test]
@ -92,7 +62,7 @@ fn test_circuit_mut() {
let bytes = include_bytes!("circuit_mut.leo"); let bytes = include_bytes!("circuit_mut.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
mut_success(program); assert_satisfied(program);
} }
#[test] #[test]
@ -100,8 +70,11 @@ fn test_function_input() {
let bytes = include_bytes!("function_input.leo"); let bytes = include_bytes!("function_input.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_main_inputs(vec![Some(input_value_u32_one())]); let main_inputs = generate_main_inputs(vec![("a", Some(InputValue::Boolean(true)))]);
mut_fail(program);
program.set_main_inputs(main_inputs);
expect_compiler_error(program);
} }
#[test] #[test]
@ -109,6 +82,9 @@ fn test_function_input_mut() {
let bytes = include_bytes!("function_input_mut.leo"); let bytes = include_bytes!("function_input_mut.leo");
let mut program = parse_program(bytes).unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_main_inputs(vec![Some(input_value_u32_one())]); let main_inputs = generate_main_inputs(vec![("a", Some(InputValue::Boolean(true)))]);
mut_success(program);
program.set_main_inputs(main_inputs);
assert_satisfied(program);
} }