From 6eb9ef41c3673c3c3de5c43cf4fdecd6f22f010d Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 30 Jul 2020 17:11:58 -0700 Subject: [PATCH] fix statement tests --- compiler/tests/mod.rs | 2 +- compiler/tests/statements/assertion_basic.leo | 4 +- .../tests/statements/conditional/assert.leo | 8 +- .../tests/statements/conditional/chain.leo | 16 +- .../tests/statements/conditional/for_loop.leo | 12 +- .../conditional/inputs/registers_one.in | 2 + .../conditional/inputs/registers_zero.in | 2 + compiler/tests/statements/conditional/mod.rs | 261 ++++++++++++------ .../conditional/multiple_returns.leo | 8 +- .../tests/statements/conditional/mutate.leo | 16 +- .../tests/statements/conditional/nested.leo | 10 +- .../conditional/outputs_/registers_one.out | 2 + .../conditional/outputs_/registers_zero.out | 2 + compiler/tests/statements/iteration_basic.leo | 4 +- compiler/tests/statements/mod.rs | 57 ++-- compiler/tests/statements/ternary_basic.leo | 6 +- 16 files changed, 255 insertions(+), 157 deletions(-) create mode 100644 compiler/tests/statements/conditional/inputs/registers_one.in create mode 100644 compiler/tests/statements/conditional/inputs/registers_zero.in create mode 100644 compiler/tests/statements/conditional/outputs_/registers_one.out create mode 100644 compiler/tests/statements/conditional/outputs_/registers_zero.out diff --git a/compiler/tests/mod.rs b/compiler/tests/mod.rs index b6b0ad76c0..fb6dd0144a 100644 --- a/compiler/tests/mod.rs +++ b/compiler/tests/mod.rs @@ -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::{ diff --git a/compiler/tests/statements/assertion_basic.leo b/compiler/tests/statements/assertion_basic.leo index 6c1fe116d0..8a8ab02f28 100644 --- a/compiler/tests/statements/assertion_basic.leo +++ b/compiler/tests/statements/assertion_basic.leo @@ -1,3 +1,3 @@ -function main(b: bool) { - assert_eq!(b, true); +function main(a: bool) { + assert_eq!(a, true); } \ No newline at end of file diff --git a/compiler/tests/statements/conditional/assert.leo b/compiler/tests/statements/conditional/assert.leo index c0c24db693..c6025b26de 100644 --- a/compiler/tests/statements/conditional/assert.leo +++ b/compiler/tests/statements/conditional/assert.leo @@ -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); } } diff --git a/compiler/tests/statements/conditional/chain.leo b/compiler/tests/statements/conditional/chain.leo index 65b391c1e8..6b789fb1c1 100644 --- a/compiler/tests/statements/conditional/chain.leo +++ b/compiler/tests/statements/conditional/chain.leo @@ -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); } \ No newline at end of file diff --git a/compiler/tests/statements/conditional/for_loop.leo b/compiler/tests/statements/conditional/for_loop.leo index b8228333a2..3c123e6fe4 100644 --- a/compiler/tests/statements/conditional/for_loop.leo +++ b/compiler/tests/statements/conditional/for_loop.leo @@ -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); } diff --git a/compiler/tests/statements/conditional/inputs/registers_one.in b/compiler/tests/statements/conditional/inputs/registers_one.in new file mode 100644 index 0000000000..be39f343fa --- /dev/null +++ b/compiler/tests/statements/conditional/inputs/registers_one.in @@ -0,0 +1,2 @@ +[registers] +a: u32 = 1; diff --git a/compiler/tests/statements/conditional/inputs/registers_zero.in b/compiler/tests/statements/conditional/inputs/registers_zero.in new file mode 100644 index 0000000000..ae20e35199 --- /dev/null +++ b/compiler/tests/statements/conditional/inputs/registers_zero.in @@ -0,0 +1,2 @@ +[registers] +a: u32 = 0; diff --git a/compiler/tests/statements/conditional/mod.rs b/compiler/tests/statements/conditional/mod.rs index 3d8beae280..d1907445b4 100644 --- a/compiler/tests/statements/conditional/mod.rs +++ b/compiler/tests/statements/conditional/mod.rs @@ -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::::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); } diff --git a/compiler/tests/statements/conditional/multiple_returns.leo b/compiler/tests/statements/conditional/multiple_returns.leo index 434bd8e458..3eda7d7063 100644 --- a/compiler/tests/statements/conditional/multiple_returns.leo +++ b/compiler/tests/statements/conditional/multiple_returns.leo @@ -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 } } \ No newline at end of file diff --git a/compiler/tests/statements/conditional/mutate.leo b/compiler/tests/statements/conditional/mutate.leo index 67ec956874..3bb7cb1839 100644 --- a/compiler/tests/statements/conditional/mutate.leo +++ b/compiler/tests/statements/conditional/mutate.leo @@ -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); + } } diff --git a/compiler/tests/statements/conditional/nested.leo b/compiler/tests/statements/conditional/nested.leo index b4570077fc..b903aad345 100644 --- a/compiler/tests/statements/conditional/nested.leo +++ b/compiler/tests/statements/conditional/nested.leo @@ -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); } \ No newline at end of file diff --git a/compiler/tests/statements/conditional/outputs_/registers_one.out b/compiler/tests/statements/conditional/outputs_/registers_one.out new file mode 100644 index 0000000000..663014ef39 --- /dev/null +++ b/compiler/tests/statements/conditional/outputs_/registers_one.out @@ -0,0 +1,2 @@ +[registers] +a: u32 = 1u32; diff --git a/compiler/tests/statements/conditional/outputs_/registers_zero.out b/compiler/tests/statements/conditional/outputs_/registers_zero.out new file mode 100644 index 0000000000..fb12d4d334 --- /dev/null +++ b/compiler/tests/statements/conditional/outputs_/registers_zero.out @@ -0,0 +1,2 @@ +[registers] +a: u32 = 0u32; diff --git a/compiler/tests/statements/iteration_basic.leo b/compiler/tests/statements/iteration_basic.leo index 19460cc543..3dae3d3761 100644 --- a/compiler/tests/statements/iteration_basic.leo +++ b/compiler/tests/statements/iteration_basic.leo @@ -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); } \ No newline at end of file diff --git a/compiler/tests/statements/mod.rs b/compiler/tests/statements/mod.rs index 84e1febd74..c178b501d9 100644 --- a/compiler/tests/statements/mod.rs +++ b/compiler/tests/statements/mod.rs @@ -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::::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::::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); } diff --git a/compiler/tests/statements/ternary_basic.leo b/compiler/tests/statements/ternary_basic.leo index 26909325e0..8ba6a71283 100644 --- a/compiler/tests/statements/ternary_basic.leo +++ b/compiler/tests/statements/ternary_basic.leo @@ -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); } \ No newline at end of file