finish mutability tests. add testing helpers

This commit is contained in:
collin 2020-05-19 12:41:21 -07:00
parent 1c733b5dd9
commit e5fea1e468
7 changed files with 93 additions and 30 deletions

View File

@ -477,7 +477,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
array: Box<Expression<F, G>>,
index: RangeOrExpression<F, G>,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
let array = match self.enforce_expression(
let array = match self.enforce_branch(
cs,
file_scope.clone(),
function_scope.clone(),

View File

@ -1,6 +1,6 @@
//! An in memory store to keep track of defined names when constraining a Leo program.
use crate::{constraints::ConstrainedValue, types::Identifier};
use crate::constraints::ConstrainedValue;
use snarkos_models::{
curves::{Field, Group, PrimeField},
@ -26,7 +26,6 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
}
pub(crate) fn store(&mut self, name: String, value: ConstrainedValue<F, G>) {
println!("storing {}", name);
self.identifiers.insert(name, value);
}

View File

@ -0,0 +1,5 @@
// Arrays are immutable by default.
function main() {
let a = [1u32];
a[0] = 0;
}

View File

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

View File

@ -0,0 +1,9 @@
// Circuits are immutable by default.
circuit Circ {
x: u32
}
function main() {
let a = Circ { x: 1 };
a.x = 0;
}

View File

@ -0,0 +1,11 @@
// Adding the `mut` keyword makes a circuit variable mutable.
circuit Circ {
x: u32
}
function main() -> u32 {
let mut a = Circ { x: 1 };
a.x = 0;
return a.x
}

View File

@ -2,58 +2,90 @@ use crate::compile_program;
use leo_compiler::{types::Integer, ConstrainedValue, InputValue};
use leo_compiler::compiler::Compiler;
use leo_compiler::errors::CompilerError;
use leo_compiler::errors::FunctionError;
use leo_compiler::errors::StatementError;
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/";
fn mut_success(program: Compiler<Fr, EdwardsProjective>) {
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
);
}
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 mut cs = TestConstraintSystem::<Fr>::new();
let program = compile_program(DIRECTORY_NAME, "let.leo").unwrap();
let output = program.compile_constraints(&mut cs).is_err();
assert!(output);
mut_fail(program);
}
#[test]
fn test_let_mut() {
let mut cs = TestConstraintSystem::<Fr>::new();
let program = compile_program(DIRECTORY_NAME, "let_mut.leo").unwrap();
let output = program.compile_constraints(&mut cs).unwrap();
println!("{}", output);
mut_success(program);
}
assert!(cs.is_satisfied());
assert_eq!(
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Integer(
Integer::U32(UInt32::constant(0))
)]),
output
);
#[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);
}
#[test]
fn test_function_input() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut program = compile_program(DIRECTORY_NAME, "function_input.leo").unwrap();
program.set_inputs(vec![Some(InputValue::Integer(1))]);
let output = program.compile_constraints(&mut cs).is_err();
assert!(output);
mut_fail(program);
}
#[test]
fn test_function_input_mut() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut program = compile_program(DIRECTORY_NAME, "function_input_mut.leo").unwrap();
program.set_inputs(vec![Some(InputValue::Integer(1))]);
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
);
mut_success(program);
}