2020-06-09 04:55:44 +03:00
|
|
|
use crate::{parse_program, EdwardsConstrainedValue, EdwardsTestCompiler};
|
2020-05-20 01:45:40 +03:00
|
|
|
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-19 22:01:19 +03:00
|
|
|
|
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();
|
2020-05-19 22:01:19 +03:00
|
|
|
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-19 22:01:19 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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();
|
2020-05-19 22:41:21 +03:00
|
|
|
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))) => {}
|
2020-05-19 22:41:21 +03:00
|
|
|
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();
|
|
|
|
|
2020-05-19 22:41:21 +03:00
|
|
|
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();
|
|
|
|
|
2020-05-19 22:41:21 +03:00
|
|
|
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();
|
|
|
|
|
2020-05-19 22:41:21 +03:00
|
|
|
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();
|
|
|
|
|
2020-05-19 22:41:21 +03:00
|
|
|
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();
|
|
|
|
|
2020-05-19 22:41:21 +03:00
|
|
|
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();
|
|
|
|
|
2020-05-19 22:41:21 +03:00
|
|
|
mut_success(program);
|
|
|
|
}
|
|
|
|
|
2020-05-19 22:01:19 +03:00
|
|
|
#[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())]);
|
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() {
|
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())]);
|
2020-05-19 22:41:21 +03:00
|
|
|
mut_success(program);
|
2020-05-19 22:01:19 +03:00
|
|
|
}
|