1
1
mirror of https://github.com/ProvableHQ/leo.git synced 2024-12-22 17:51:39 +03:00

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 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;
use leo_compiler::{ use leo_compiler::{

View File

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

View File

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

View File

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

View File

@ -1,11 +1,13 @@
function main(cond: bool) -> u32 { function main(a: bool) {
let mut a = 0u32; let mut b = 0u32;
if cond { if a {
for i in 0..4 { 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::{ use crate::{
get_output, assert_satisfied,
integers::u32::{output_number, output_one, output_zero}, expect_synthesis_error,
generate_main_inputs,
get_outputs,
parse_program, parse_program,
EdwardsConstrainedValue, parse_program_with_inputs,
EdwardsTestCompiler, EdwardsTestCompiler,
}; };
use leo_inputs::types::{IntegerType, U32Type}; use leo_inputs::types::{IntegerType, U32Type};
use leo_types::InputValue; 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] #[test]
fn test_assert() { fn test_assert() {
let bytes = include_bytes!("assert.leo"); let bytes = include_bytes!("assert.leo");
@ -35,52 +19,65 @@ fn test_assert() {
// Check that an input value of 1 satisfies the constraint system // Check that an input value of 1 satisfies the constraint system
program_1_pass.set_main_inputs(vec![Some(InputValue::Integer( let main_inputs = generate_main_inputs(vec![(
IntegerType::U32Type(U32Type {}), "a",
1.to_string(), Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
))]); )]);
empty_output_satisfied(program_1_pass);
program_1_pass.set_main_inputs(main_inputs);
assert_satisfied(program_1_pass);
// Check that an input value of 0 satisfies the constraint system // Check that an input value of 0 satisfies the constraint system
program_0_pass.set_main_inputs(vec![Some(InputValue::Integer( let main_inputs = generate_main_inputs(vec![(
IntegerType::U32Type(U32Type {}), "a",
0.to_string(), Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
))]); )]);
empty_output_satisfied(program_0_pass);
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 // Check that an input value of 2 does not satisfy the constraint system
program_2_fail.set_main_inputs(vec![Some(InputValue::Integer( let main_inputs = generate_main_inputs(vec![(
IntegerType::U32Type(U32Type {}), "a",
2.to_string(), 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(); program_2_fail.set_main_inputs(main_inputs);
assert!(!cs.is_satisfied());
expect_synthesis_error(program_2_fail);
} }
#[test] #[test]
fn test_mutate() { fn test_mutate() {
let bytes = include_bytes!("mutate.leo"); let bytes = include_bytes!("mutate.leo");
let mut program_1_true = parse_program(bytes).unwrap(); let mut program_1_pass = parse_program(bytes).unwrap();
let mut program_0_pass = program_1_true.clone(); let mut program_0_pass = program_1_pass.clone();
// Check that an input value of 1 satisfies the constraint system // Check that an input value of 1 satisfies the constraint system
program_1_true.set_main_inputs(vec![Some(InputValue::Integer( let main_inputs = generate_main_inputs(vec![(
IntegerType::U32Type(U32Type {}), "a",
1.to_string(), Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
))]); )]);
output_one(program_1_true);
program_1_pass.set_main_inputs(main_inputs);
assert_satisfied(program_1_pass);
// Check that an input value of 0 satisfies the constraint system // Check that an input value of 0 satisfies the constraint system
program_0_pass.set_main_inputs(vec![Some(InputValue::Integer( let main_inputs = generate_main_inputs(vec![(
IntegerType::U32Type(U32Type {}), "a",
0.to_string(), Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 0.to_string())),
))]); )]);
output_zero(program_0_pass);
program_0_pass.set_main_inputs(main_inputs);
assert_satisfied(program_0_pass);
} }
#[test] #[test]
@ -91,13 +88,19 @@ fn test_for_loop() {
// Check that an input value of true satisfies the constraint system // Check that an input value of true satisfies the constraint system
program_true_6.set_main_inputs(vec![Some(InputValue::Boolean(true))]); let main_inputs = generate_main_inputs(vec![("a", Some(InputValue::Boolean(true)))]);
output_number(program_true_6, 6u32);
program_true_6.set_main_inputs(main_inputs);
assert_satisfied(program_true_6);
// Check that an input value of false satisfies the constraint system // Check that an input value of false satisfies the constraint system
program_false_0.set_main_inputs(vec![Some(InputValue::Boolean(false))]); let main_inputs = generate_main_inputs(vec![("a", Some(InputValue::Boolean(false)))]);
output_zero(program_false_0);
program_false_0.set_main_inputs(main_inputs);
assert_satisfied(program_false_0);
} }
#[test] #[test]
@ -105,28 +108,58 @@ fn test_chain() {
let bytes = include_bytes!("chain.leo"); let bytes = include_bytes!("chain.leo");
let mut program_1_1 = parse_program(bytes).unwrap(); let mut program_1_1 = parse_program(bytes).unwrap();
let mut program_2_2 = program_1_1.clone(); 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 // Check that an input of 1 outputs 1
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 0 outputs true let main_inputs = generate_main_inputs(vec![
program_2_2.set_main_inputs(vec![Some(InputValue::Integer( (
IntegerType::U32Type(U32Type {}), "a",
2.to_string(), Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
))]); ),
output_number(program_2_2, 2u32); (
"b",
Some(InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())),
),
]);
// Check that an input of 0 outputs true program_1_1.set_main_inputs(main_inputs);
program_2_3.set_main_inputs(vec![Some(InputValue::Integer(
IntegerType::U32Type(U32Type {}), assert_satisfied(program_1_1);
5.to_string(),
))]); // Check that an input of 2 outputs 2
output_number(program_2_3, 3u32);
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] #[test]
@ -136,35 +169,81 @@ fn test_nested() {
let mut program_true_false_1 = program_true_true_3.clone(); let mut program_true_false_1 = program_true_true_3.clone();
let mut program_false_false_0 = 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]); let main_inputs = generate_main_inputs(vec![
output_number(program_true_true_3, 3u32); ("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))]); assert_satisfied(program_true_true_3);
output_number(program_true_false_1, 1u32);
// 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))]); let main_inputs = generate_main_inputs(vec![
output_number(program_false_false_0, 0u32); ("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] #[test]
fn test_multiple_returns() { fn test_multiple_returns() {
let bytes = include_bytes!("multiple_returns.leo"); let program_bytes = include_bytes!("multiple_returns.leo");
let mut program_true_1 = parse_program(bytes).unwrap();
let mut program_false_0 = program_true_1.clone();
// 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))]); let registers_one_bytes = include_bytes!("inputs/registers_one.in");
output_number(program_true_1, 1u32); 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))]); // Check that an input value of 0 writes 0 to the output registers
output_number(program_false_0, 0u32);
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 { function main(registers) -> u32 {
if cond { if registers.a == 0 {
return 1u32
} else {
return 0u32 return 0u32
} else {
return 1u32
} }
} }

View File

@ -1,11 +1,15 @@
function main(bit: u32) -> u32 { function main(a: u32) {
let mut a = 5u32; let mut b = 5u32;
if bit == 1 { if a == 1 {
a = 1; b = 1;
} else { } 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 { function main(a: bool, b: bool, c: u32) {
let mut result = 0u32; let mut d = 0u32;
if a { if a {
result += 1; d += 1;
if b { 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; let mut x = 4u32;
for i in 0..3 { for i in 0..3 {
x -= 1; x -= 1;
} }
return x assert_eq!(x, 1u32);
} }

View File

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