leo/compiler/tests/mutability/mod.rs

100 lines
2.6 KiB
Rust
Raw Normal View History

2020-06-09 04:55:44 +03:00
use crate::{parse_program, EdwardsConstrainedValue, EdwardsTestCompiler};
use leo_compiler::{
errors::{CompilerError, FunctionError, StatementError},
ConstrainedValue,
};
2020-06-11 03:53:38 +03:00
use leo_types::Integer;
2020-06-02 04:35:43 +03:00
2020-06-11 02:14:55 +03:00
use crate::array::input_value_u32_one;
2020-05-30 03:34:31 +03:00
use snarkos_curves::edwards_bls12::Fq;
2020-06-04 23:35:12 +03:00
use snarkos_models::gadgets::{r1cs::TestConstraintSystem, utilities::uint::UInt32};
2020-05-30 03:34:31 +03:00
fn mut_success(program: EdwardsTestCompiler) {
2020-05-30 02:43:39 +03:00
let mut cs = TestConstraintSystem::<Fq>::new();
let output = program.compile_constraints(&mut cs).unwrap();
assert!(cs.is_satisfied());
assert_eq!(
2020-06-08 09:30:39 +03:00
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Integer(Integer::U32(UInt32::constant(0)))]).to_string(),
2020-05-30 03:34:31 +03:00
output.to_string()
);
}
2020-05-30 03:34:31 +03:00
fn mut_fail(program: EdwardsTestCompiler) {
2020-05-30 02:43:39 +03:00
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 {
2020-06-08 09:30:39 +03:00
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ImmutableAssign(_string))) => {}
err => panic!("Expected immutable assign error, got {}", err),
}
}
#[test]
fn test_let() {
2020-06-09 04:55:44 +03:00
let bytes = include_bytes!("let.leo");
let program = parse_program(bytes).unwrap();
mut_fail(program);
}
#[test]
fn test_let_mut() {
2020-06-09 04:55:44 +03:00
let bytes = include_bytes!("let_mut.leo");
let program = parse_program(bytes).unwrap();
mut_success(program);
}
#[test]
fn test_array() {
2020-06-09 04:55:44 +03:00
let bytes = include_bytes!("array.leo");
let program = parse_program(bytes).unwrap();
mut_fail(program);
}
#[test]
fn test_array_mut() {
2020-06-09 04:55:44 +03:00
let bytes = include_bytes!("array_mut.leo");
let program = parse_program(bytes).unwrap();
mut_success(program);
}
#[test]
fn test_circuit() {
2020-06-09 04:55:44 +03:00
let bytes = include_bytes!("circuit.leo");
let program = parse_program(bytes).unwrap();
mut_fail(program);
}
#[test]
fn test_circuit_mut() {
2020-06-09 04:55:44 +03:00
let bytes = include_bytes!("circuit_mut.leo");
let program = parse_program(bytes).unwrap();
mut_success(program);
}
#[test]
fn test_function_input() {
2020-06-09 04:55:44 +03:00
let bytes = include_bytes!("function_input.leo");
let mut program = parse_program(bytes).unwrap();
2020-06-11 02:14:55 +03:00
program.set_inputs(vec![Some(input_value_u32_one())]);
mut_fail(program);
}
#[test]
fn test_function_input_mut() {
2020-06-09 04:55:44 +03:00
let bytes = include_bytes!("function_input_mut.leo");
let mut program = parse_program(bytes).unwrap();
2020-06-11 02:14:55 +03:00
program.set_inputs(vec![Some(input_value_u32_one())]);
mut_success(program);
}