fix statement tests

This commit is contained in:
collin 2020-07-30 17:11:58 -07:00
parent f334ba646d
commit 6eb9ef41c3
16 changed files with 255 additions and 157 deletions

View File

@ -10,7 +10,7 @@ pub mod inputs;
pub mod integers;
pub mod macros;
pub mod mutability;
// pub mod statements;
pub mod statements;
// pub mod syntax;
use leo_compiler::{

View File

@ -1,3 +1,3 @@
function main(b: bool) {
assert_eq!(b, true);
function main(a: bool) {
assert_eq!(a, true);
}

View File

@ -1,7 +1,7 @@
function main(bit: u32) {
if bit == 1 {
assert_eq!(bit, 1);
function main(a: u32) {
if a == 1 {
assert_eq!(a, 1);
} else {
assert_eq!(bit, 0);
assert_eq!(a, 0);
}
}

View File

@ -1,13 +1,13 @@
function main(bit: u32) -> u32 {
let mut result = 0u32;
function main(a: u32, b: u32) {
let mut c = 0u32;
if bit == 1 {
result = 1;
} else if bit == 2 {
result = 2;
if a == 1 {
c = 1;
} else if a == 2 {
c = 2;
} else {
result = 3;
c = 3;
}
return result
assert_eq!(c, b);
}

View File

@ -1,11 +1,13 @@
function main(cond: bool) -> u32 {
let mut a = 0u32;
function main(a: bool) {
let mut b = 0u32;
if cond {
if a {
for i in 0..4 {
a += i;
b += i;
}
}
return a
let r: u32 = if a ? 6 : 0;
assert_eq!(r, b);
}

View File

@ -0,0 +1,2 @@
[registers]
a: u32 = 1;

View File

@ -0,0 +1,2 @@
[registers]
a: u32 = 0;

View File

@ -1,31 +1,15 @@
use crate::{
get_output,
integers::u32::{output_number, output_one, output_zero},
assert_satisfied,
expect_synthesis_error,
generate_main_inputs,
get_outputs,
parse_program,
EdwardsConstrainedValue,
parse_program_with_inputs,
EdwardsTestCompiler,
};
use leo_inputs::types::{IntegerType, U32Type};
use leo_types::InputValue;
use snarkos_curves::edwards_bls12::Fq;
use snarkos_models::gadgets::r1cs::TestConstraintSystem;
fn empty_output_satisfied(program: EdwardsTestCompiler) {
let output = get_output(program);
assert_eq!(EdwardsConstrainedValue::Return(vec![]).to_string(), output.to_string());
}
// Tests a statements.conditional enforceBit() program
//
// function main(bit: u8) {
// if bit == 1u8 {
// assert_eq!(bit, 1u8);
// } else {
// assert_eq!(bit, 0u8);
// }
// }
#[test]
fn test_assert() {
let bytes = include_bytes!("assert.leo");
@ -35,52 +19,65 @@ fn test_assert() {
// Check that an input value of 1 satisfies the constraint system
program_1_pass.set_main_inputs(vec![Some(InputValue::Integer(
IntegerType::U32Type(U32Type {}),
1.to_string(),
))]);
empty_output_satisfied(program_1_pass);
let main_inputs = generate_main_inputs(vec![(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
)]);
program_1_pass.set_main_inputs(main_inputs);
assert_satisfied(program_1_pass);
// Check that an input value of 0 satisfies the constraint system
program_0_pass.set_main_inputs(vec![Some(InputValue::Integer(
IntegerType::U32Type(U32Type {}),
0.to_string(),
))]);
empty_output_satisfied(program_0_pass);
let main_inputs = generate_main_inputs(vec![(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
)]);
program_0_pass.set_main_inputs(main_inputs);
assert_satisfied(program_0_pass);
// Check that an input value of 2 does not satisfy the constraint system
program_2_fail.set_main_inputs(vec![Some(InputValue::Integer(
IntegerType::U32Type(U32Type {}),
2.to_string(),
))]);
let mut cs = TestConstraintSystem::<Fq>::new();
let _output = program_2_fail.compile_constraints(&mut cs).unwrap();
assert!(!cs.is_satisfied());
let main_inputs = generate_main_inputs(vec![(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 2.to_string())),
)]);
program_2_fail.set_main_inputs(main_inputs);
expect_synthesis_error(program_2_fail);
}
#[test]
fn test_mutate() {
let bytes = include_bytes!("mutate.leo");
let mut program_1_true = parse_program(bytes).unwrap();
let mut program_0_pass = program_1_true.clone();
let mut program_1_pass = parse_program(bytes).unwrap();
let mut program_0_pass = program_1_pass.clone();
// Check that an input value of 1 satisfies the constraint system
program_1_true.set_main_inputs(vec![Some(InputValue::Integer(
IntegerType::U32Type(U32Type {}),
1.to_string(),
))]);
output_one(program_1_true);
let main_inputs = generate_main_inputs(vec![(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
)]);
program_1_pass.set_main_inputs(main_inputs);
assert_satisfied(program_1_pass);
// Check that an input value of 0 satisfies the constraint system
program_0_pass.set_main_inputs(vec![Some(InputValue::Integer(
IntegerType::U32Type(U32Type {}),
0.to_string(),
))]);
output_zero(program_0_pass);
let main_inputs = generate_main_inputs(vec![(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
)]);
program_0_pass.set_main_inputs(main_inputs);
assert_satisfied(program_0_pass);
}
#[test]
@ -91,13 +88,19 @@ fn test_for_loop() {
// Check that an input value of true satisfies the constraint system
program_true_6.set_main_inputs(vec![Some(InputValue::Boolean(true))]);
output_number(program_true_6, 6u32);
let main_inputs = generate_main_inputs(vec![("a", Some(InputValue::Boolean(true)))]);
program_true_6.set_main_inputs(main_inputs);
assert_satisfied(program_true_6);
// Check that an input value of false satisfies the constraint system
program_false_0.set_main_inputs(vec![Some(InputValue::Boolean(false))]);
output_zero(program_false_0);
let main_inputs = generate_main_inputs(vec![("a", Some(InputValue::Boolean(false)))]);
program_false_0.set_main_inputs(main_inputs);
assert_satisfied(program_false_0);
}
#[test]
@ -105,28 +108,58 @@ fn test_chain() {
let bytes = include_bytes!("chain.leo");
let mut program_1_1 = parse_program(bytes).unwrap();
let mut program_2_2 = program_1_1.clone();
let mut program_2_3 = program_1_1.clone();
let mut program_4_3 = program_1_1.clone();
// Check that an input of 1 outputs true
program_1_1.set_main_inputs(vec![Some(InputValue::Integer(
IntegerType::U32Type(U32Type {}),
1.to_string(),
))]);
output_number(program_1_1, 1u32);
// Check that an input of 1 outputs 1
// Check that an input of 0 outputs true
program_2_2.set_main_inputs(vec![Some(InputValue::Integer(
IntegerType::U32Type(U32Type {}),
2.to_string(),
))]);
output_number(program_2_2, 2u32);
let main_inputs = generate_main_inputs(vec![
(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
),
(
"b",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
),
]);
// Check that an input of 0 outputs true
program_2_3.set_main_inputs(vec![Some(InputValue::Integer(
IntegerType::U32Type(U32Type {}),
5.to_string(),
))]);
output_number(program_2_3, 3u32);
program_1_1.set_main_inputs(main_inputs);
assert_satisfied(program_1_1);
// Check that an input of 2 outputs 2
let main_inputs = generate_main_inputs(vec![
(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 2.to_string())),
),
(
"b",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 2.to_string())),
),
]);
program_2_2.set_main_inputs(main_inputs);
assert_satisfied(program_2_2);
// Check that an input of 4 outputs 3
let main_inputs = generate_main_inputs(vec![
(
"a",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 4.to_string())),
),
(
"b",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 3.to_string())),
),
]);
program_4_3.set_main_inputs(main_inputs);
assert_satisfied(program_4_3);
}
#[test]
@ -136,35 +169,81 @@ fn test_nested() {
let mut program_true_false_1 = program_true_true_3.clone();
let mut program_false_false_0 = program_true_true_3.clone();
// Check that an input value of true true satisfies the constraint system
// Check that an input value of true true outputs 3
program_true_true_3.set_main_inputs(vec![Some(InputValue::Boolean(true)); 2]);
output_number(program_true_true_3, 3u32);
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Boolean(true))),
("b", Some(InputValue::Boolean(true))),
(
"c",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 3.to_string())),
),
]);
// Check that an input value of true false satisfies the constraint system
program_true_true_3.set_main_inputs(main_inputs);
program_true_false_1.set_main_inputs(vec![Some(InputValue::Boolean(true)), Some(InputValue::Boolean(false))]);
output_number(program_true_false_1, 1u32);
assert_satisfied(program_true_true_3);
// Check that an input value of false false satisfies the constraint system
// Check that an input value of true false outputs 1
program_false_false_0.set_main_inputs(vec![Some(InputValue::Boolean(false)), Some(InputValue::Boolean(false))]);
output_number(program_false_false_0, 0u32);
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Boolean(true))),
("b", Some(InputValue::Boolean(false))),
(
"c",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
),
]);
program_true_false_1.set_main_inputs(main_inputs);
assert_satisfied(program_true_false_1);
// Check that an input value of false false outputs 0
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Boolean(false))),
("b", Some(InputValue::Boolean(false))),
(
"c",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
),
]);
program_false_false_0.set_main_inputs(main_inputs);
assert_satisfied(program_false_false_0);
}
fn output_one(program: EdwardsTestCompiler) {
let expected = include_bytes!("outputs_/registers_one.out");
let actual = get_outputs(program);
assert_eq!(expected, actual.bytes().as_slice());
}
fn output_zero(program: EdwardsTestCompiler) {
let expected = include_bytes!("outputs_/registers_zero.out");
let actual = get_outputs(program);
assert_eq!(expected, actual.bytes().as_slice());
}
#[test]
fn test_multiple_returns() {
let bytes = include_bytes!("multiple_returns.leo");
let mut program_true_1 = parse_program(bytes).unwrap();
let mut program_false_0 = program_true_1.clone();
let program_bytes = include_bytes!("multiple_returns.leo");
// Check that an input value of true returns 1 and satisfies the constraint system
// Check that an input value of 1 writes 1 to the output registers
program_true_1.set_main_inputs(vec![Some(InputValue::Boolean(true))]);
output_number(program_true_1, 1u32);
let registers_one_bytes = include_bytes!("inputs/registers_one.in");
let program = parse_program_with_inputs(program_bytes, registers_one_bytes).unwrap();
// Check that an input value of false returns 0 and satisfies the constraint system
output_one(program);
program_false_0.set_main_inputs(vec![Some(InputValue::Boolean(false))]);
output_number(program_false_0, 0u32);
// Check that an input value of 0 writes 0 to the output registers
let registers_zero_bytes = include_bytes!("inputs/registers_zero.in");
let program = parse_program_with_inputs(program_bytes, registers_zero_bytes).unwrap();
output_zero(program);
}

View File

@ -1,7 +1,7 @@
function main(cond: bool) -> u32 {
if cond {
return 1u32
} else {
function main(registers) -> u32 {
if registers.a == 0 {
return 0u32
} else {
return 1u32
}
}

View File

@ -1,11 +1,15 @@
function main(bit: u32) -> u32 {
let mut a = 5u32;
function main(a: u32) {
let mut b = 5u32;
if bit == 1 {
a = 1;
if a == 1 {
b = 1;
} else {
a = 0;
b = 0;
}
return a
if a == 1 {
assert_eq!(b, 1);
} else {
assert_eq!(b, 0);
}
}

View File

@ -1,12 +1,12 @@
function main(a: bool, b: bool) -> u32 {
let mut result = 0u32;
function main(a: bool, b: bool, c: u32) {
let mut d = 0u32;
if a {
result += 1;
d += 1;
if b {
result += 2;
d += 2;
}
}
return result
assert_eq!(d, c);
}

View File

@ -0,0 +1,2 @@
[registers]
a: u32 = 1u32;

View File

@ -0,0 +1,2 @@
[registers]
a: u32 = 0u32;

View File

@ -1,8 +1,8 @@
function main() -> u32 {
function main() {
let mut x = 4u32;
for i in 0..3 {
x -= 1;
}
return x
assert_eq!(x, 1u32);
}

View File

@ -1,13 +1,6 @@
use crate::{
get_error,
integers::u32::{output_one, output_zero},
parse_program,
};
use crate::{assert_satisfied, expect_compiler_error, expect_synthesis_error, generate_main_inputs, parse_program};
use leo_types::InputValue;
use snarkos_curves::edwards_bls12::Fq;
use snarkos_models::gadgets::r1cs::TestConstraintSystem;
pub mod conditional;
// Ternary if {bool}? {expression} : {expression};
@ -15,15 +8,27 @@ pub mod conditional;
#[test]
fn test_ternary_basic() {
let bytes = include_bytes!("ternary_basic.leo");
let mut program_input_true = parse_program(bytes).unwrap();
let mut program = parse_program(bytes).unwrap();
let mut program_input_false = program_input_true.clone();
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Boolean(true))),
("b", Some(InputValue::Boolean(true))),
]);
program_input_true.set_main_inputs(vec![Some(InputValue::Boolean(true))]);
output_one(program_input_true);
program.set_main_inputs(main_inputs);
program_input_false.set_main_inputs(vec![Some(InputValue::Boolean(false))]);
output_zero(program_input_false);
assert_satisfied(program);
let mut program = parse_program(bytes).unwrap();
let main_inputs = generate_main_inputs(vec![
("a", Some(InputValue::Boolean(false))),
("b", Some(InputValue::Boolean(false))),
]);
program.set_main_inputs(main_inputs);
assert_satisfied(program);
}
// Iteration for i {start}..{stop} { statements }
@ -33,7 +38,7 @@ fn test_iteration_basic() {
let bytes = include_bytes!("iteration_basic.leo");
let program = parse_program(bytes).unwrap();
output_one(program);
assert_satisfied(program);
}
// Assertion
@ -41,23 +46,21 @@ fn test_iteration_basic() {
#[test]
fn test_assertion_basic() {
let bytes = include_bytes!("assertion_basic.leo");
let program = parse_program(bytes).unwrap();
let mut program = parse_program(bytes).unwrap();
let mut program_input_true = program.clone();
let mut cs_satisfied = TestConstraintSystem::<Fq>::new();
let main_inputs = generate_main_inputs(vec![("a", Some(InputValue::Boolean(true)))]);
program_input_true.set_main_inputs(vec![Some(InputValue::Boolean(true))]);
let _output = program_input_true.compile_constraints(&mut cs_satisfied).unwrap();
program.set_main_inputs(main_inputs);
assert!(cs_satisfied.is_satisfied());
assert_satisfied(program);
let mut program_input_false = program.clone();
let mut cs_unsatisfied = TestConstraintSystem::<Fq>::new();
let mut program = parse_program(bytes).unwrap();
program_input_false.set_main_inputs(vec![Some(InputValue::Boolean(false))]);
let _output = program_input_false.compile_constraints(&mut cs_unsatisfied).unwrap();
let main_inputs = generate_main_inputs(vec![("a", Some(InputValue::Boolean(false)))]);
assert!(!cs_unsatisfied.is_satisfied());
program.set_main_inputs(main_inputs);
expect_synthesis_error(program);
}
#[test]
@ -65,5 +68,5 @@ fn test_num_returns_fail() {
let bytes = include_bytes!("num_returns_fail.leo");
let program = parse_program(bytes).unwrap();
let _ = get_error(program);
expect_compiler_error(program);
}

View File

@ -1,3 +1,5 @@
function main(b: bool) -> u32 {
return if b ? 1 : 0
function main(a: bool, b: bool) {
let c = if a ? true : false;
assert_eq!(c, b);
}