From df9b94ef7312c8523336aea9974538ed0b3d2d2e Mon Sep 17 00:00:00 2001 From: collin Date: Thu, 30 Jul 2020 00:56:17 -0700 Subject: [PATCH] fix array tests --- compiler/tests/array/initializer.leo | 4 +- compiler/tests/array/inline.leo | 4 +- compiler/tests/array/input.leo | 3 - compiler/tests/array/inputs/inline_fail.leo | 3 + compiler/tests/array/inputs/registers_ones.in | 2 + .../tests/array/inputs/registers_zeros.in | 2 + compiler/tests/array/inputs/six_zeros.in | 2 + compiler/tests/array/inputs/three_ones.in | 2 + compiler/tests/array/mod.rs | 150 +++++++----------- compiler/tests/array/multi.leo | 8 +- compiler/tests/array/registers.leo | 3 + compiler/tests/array/slice.leo | 6 +- compiler/tests/array/spread.leo | 6 +- compiler/tests/mod.rs | 2 +- 14 files changed, 85 insertions(+), 112 deletions(-) delete mode 100644 compiler/tests/array/input.leo create mode 100644 compiler/tests/array/inputs/inline_fail.leo create mode 100644 compiler/tests/array/inputs/registers_ones.in create mode 100644 compiler/tests/array/inputs/registers_zeros.in create mode 100644 compiler/tests/array/inputs/six_zeros.in create mode 100644 compiler/tests/array/inputs/three_ones.in create mode 100644 compiler/tests/array/registers.leo diff --git a/compiler/tests/array/initializer.leo b/compiler/tests/array/initializer.leo index d129bd704d..109c003fd3 100644 --- a/compiler/tests/array/initializer.leo +++ b/compiler/tests/array/initializer.leo @@ -1,3 +1,3 @@ -function main() -> u32[3] { - return [1u32; 3] +function main(a: u8[3]) { + assert_eq!(a, [1u8; 3]); } \ No newline at end of file diff --git a/compiler/tests/array/inline.leo b/compiler/tests/array/inline.leo index 3a77aa5546..649ec73262 100644 --- a/compiler/tests/array/inline.leo +++ b/compiler/tests/array/inline.leo @@ -1,3 +1,3 @@ -function main() -> u32[3] { - return [1u32, 1u32, 1u32] +function main(a: u8[3]) { + assert_eq!(a, [1u8, 1u8, 1u8]); } \ No newline at end of file diff --git a/compiler/tests/array/input.leo b/compiler/tests/array/input.leo deleted file mode 100644 index eade0fae25..0000000000 --- a/compiler/tests/array/input.leo +++ /dev/null @@ -1,3 +0,0 @@ -function main(arr: u32[3]) -> u32[3] { - return arr -} \ No newline at end of file diff --git a/compiler/tests/array/inputs/inline_fail.leo b/compiler/tests/array/inputs/inline_fail.leo new file mode 100644 index 0000000000..ef826e9f0c --- /dev/null +++ b/compiler/tests/array/inputs/inline_fail.leo @@ -0,0 +1,3 @@ +function main() { + let a = [1u8, bool]; +} \ No newline at end of file diff --git a/compiler/tests/array/inputs/registers_ones.in b/compiler/tests/array/inputs/registers_ones.in new file mode 100644 index 0000000000..47f8b756d1 --- /dev/null +++ b/compiler/tests/array/inputs/registers_ones.in @@ -0,0 +1,2 @@ +[registers] +r: u8[3] = [1u8, 1u8, 1u8]; diff --git a/compiler/tests/array/inputs/registers_zeros.in b/compiler/tests/array/inputs/registers_zeros.in new file mode 100644 index 0000000000..bda1f40229 --- /dev/null +++ b/compiler/tests/array/inputs/registers_zeros.in @@ -0,0 +1,2 @@ +[registers] +r: u8[3] = [0u8, 0u8, 0u8]; diff --git a/compiler/tests/array/inputs/six_zeros.in b/compiler/tests/array/inputs/six_zeros.in new file mode 100644 index 0000000000..f91102b28f --- /dev/null +++ b/compiler/tests/array/inputs/six_zeros.in @@ -0,0 +1,2 @@ +[main] +a: u8[3][2] = [[0; 3]; 2]; \ No newline at end of file diff --git a/compiler/tests/array/inputs/three_ones.in b/compiler/tests/array/inputs/three_ones.in new file mode 100644 index 0000000000..96bcc4ef51 --- /dev/null +++ b/compiler/tests/array/inputs/three_ones.in @@ -0,0 +1,2 @@ +[main] +a: u8[3] = [1u8, 1u8, 1u8]; \ No newline at end of file diff --git a/compiler/tests/array/mod.rs b/compiler/tests/array/mod.rs index 06dd2f95cc..2da4897601 100644 --- a/compiler/tests/array/mod.rs +++ b/compiler/tests/array/mod.rs @@ -1,133 +1,95 @@ use crate::{ - get_error, - get_output, - integers::fail_integer, + assert_satisfied, + get_compiler_error, + get_outputs, parse_program, - EdwardsConstrainedValue, + parse_program_with_inputs, EdwardsTestCompiler, }; -use leo_compiler::{ - errors::{CompilerError, FunctionError}, - ConstrainedValue, - Integer, -}; -use leo_inputs::types::{IntegerType, U32Type}; -use leo_types::InputValue; -use snarkos_models::gadgets::utilities::uint::UInt32; +pub fn output_ones(program: EdwardsTestCompiler) { + let expected = include_bytes!("outputs/registers_ones.out"); + let actual = get_outputs(program); -// [1, 1, 1] -fn output_ones(program: EdwardsTestCompiler) { - let output = get_output(program); - assert_eq!( - EdwardsConstrainedValue::Return(vec![ConstrainedValue::Array(vec![ - ConstrainedValue::Integer( - Integer::U32(UInt32::constant(1u32)) - ); - 3 - ])]) - .to_string(), - output.to_string() - ); + assert!(expected.eq(actual.bytes().as_slice())); } -// [[0, 0, 0], -// [0, 0, 0]] -fn output_multi(program: EdwardsTestCompiler) { - let output = get_output(program); - assert_eq!( - EdwardsConstrainedValue::Return(vec![ConstrainedValue::Array(vec![ - ConstrainedValue::Array( - vec![ConstrainedValue::Integer(Integer::U32(UInt32::constant(0u32))); 3] - ); - 2 - ])]) - .to_string(), - output.to_string() - ) +pub fn output_zeros(program: EdwardsTestCompiler) { + let expected = include_bytes!("outputs/registers_zeros.out"); + let actual = get_outputs(program); + + assert!(expected.eq(actual.bytes().as_slice())); } -fn fail_array(program: EdwardsTestCompiler) { - match get_error(program) { - CompilerError::FunctionError(FunctionError::Error(_string)) => {} - error => panic!("Expected function error, found {}", error), - } -} +// Registers -pub(crate) fn input_value_u32_one() -> InputValue { - InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string()) +#[test] +fn test_registers() { + let program_bytes = include_bytes!("registers.leo"); + let ones_input_bytes = include_bytes!("inputs/registers_ones.in"); + let zeros_input_bytes = include_bytes!("inputs/registers_zeros.in"); + + // test ones input register => ones output register + let program = parse_program_with_inputs(program_bytes, ones_input_bytes).unwrap(); + + output_ones(program); + + // test zeros input register => zeros output register + let program = parse_program_with_inputs(program_bytes, zeros_input_bytes).unwrap(); + + output_zeros(program); } // Expressions #[test] fn test_inline() { - let bytes = include_bytes!("inline.leo"); - let program = parse_program(bytes).unwrap(); + let program_bytes = include_bytes!("inline.leo"); + let input_bytes = include_bytes!("inputs/three_ones.in"); + let program = parse_program_with_inputs(program_bytes, input_bytes).unwrap(); - output_ones(program); + assert_satisfied(program); +} + +#[test] +fn test_inline_fail() { + let program_bytes = include_bytes!("inline.leo"); + let program = parse_program(program_bytes).unwrap(); + + let _err = get_compiler_error(program); } #[test] fn test_initializer() { - let bytes = include_bytes!("initializer.leo"); - let program = parse_program(bytes).unwrap(); + let program_bytes = include_bytes!("initializer.leo"); + let input_bytes = include_bytes!("inputs/three_ones.in"); + let program = parse_program_with_inputs(program_bytes, input_bytes).unwrap(); - output_ones(program); + assert_satisfied(program); } #[test] fn test_spread() { - let bytes = include_bytes!("spread.leo"); - let program = parse_program(bytes).unwrap(); + let program_bytes = include_bytes!("spread.leo"); + let input_bytes = include_bytes!("inputs/three_ones.in"); + let program = parse_program_with_inputs(program_bytes, input_bytes).unwrap(); - output_ones(program); + assert_satisfied(program); } #[test] fn test_slice() { - let bytes = include_bytes!("slice.leo"); - let program = parse_program(bytes).unwrap(); + let program_bytes = include_bytes!("slice.leo"); + let input_bytes = include_bytes!("inputs/three_ones.in"); + let program = parse_program_with_inputs(program_bytes, input_bytes).unwrap(); - output_ones(program); + assert_satisfied(program); } #[test] fn test_multi() { - let bytes = include_bytes!("multi.leo"); - let program = parse_program(bytes).unwrap(); + let program_bytes = include_bytes!("multi.leo"); + let program = parse_program(program_bytes).unwrap(); - output_multi(program); -} - -// Inputs - -#[test] -fn test_input_array() { - let bytes = include_bytes!("input.leo"); - let mut program = parse_program(bytes).unwrap(); - - program.set_main_inputs(vec![Some(InputValue::Array(vec![input_value_u32_one(); 3]))]); - - output_ones(program) -} - -#[test] -fn test_input_array_fail() { - let bytes = include_bytes!("input.leo"); - let mut program = parse_program(bytes).unwrap(); - - program.set_main_inputs(vec![Some(input_value_u32_one())]); - - fail_array(program); -} - -#[test] -fn test_input_field_none() { - let bytes = include_bytes!("input.leo"); - let mut program = parse_program(bytes).unwrap(); - - program.set_main_inputs(vec![None]); - - fail_integer(program) + assert_satisfied(program); } diff --git a/compiler/tests/array/multi.leo b/compiler/tests/array/multi.leo index baf94b2842..f5079b8615 100644 --- a/compiler/tests/array/multi.leo +++ b/compiler/tests/array/multi.leo @@ -1,8 +1,8 @@ // Multidimensional array syntax in leo -function main() -> u32[3][2] { - const m = [[0u32, 0u32, 0u32], [0u32, 0u32, 0u32]]; // inline +function main() { + const a = [[0u32, 0u32, 0u32], [0u32, 0u32, 0u32]]; // inline - const m: u32[3][2] = [[0; 3]; 2]; // initializer + const b: u32[3][2] = [[0; 3]; 2]; // initializer - return m + assert_eq!(a, b); } \ No newline at end of file diff --git a/compiler/tests/array/registers.leo b/compiler/tests/array/registers.leo new file mode 100644 index 0000000000..42d0fa4530 --- /dev/null +++ b/compiler/tests/array/registers.leo @@ -0,0 +1,3 @@ +function main(registers) -> u8[3] { + return registers.r +} \ No newline at end of file diff --git a/compiler/tests/array/slice.leo b/compiler/tests/array/slice.leo index 95390d0583..eeca972e26 100644 --- a/compiler/tests/array/slice.leo +++ b/compiler/tests/array/slice.leo @@ -1,6 +1,6 @@ // `{from}..{to}` copies the elements of one array into another exclusively -function main() -> u32[3] { - let a = [1u32; 4]; +function main(a: u8[3]) { + let b = [1u8; 4]; - return a[0..3] + assert_eq!(a, b[0..3]); } diff --git a/compiler/tests/array/spread.leo b/compiler/tests/array/spread.leo index 86d5612171..6a7922122f 100644 --- a/compiler/tests/array/spread.leo +++ b/compiler/tests/array/spread.leo @@ -1,6 +1,6 @@ // A spread operator `...` copies the elements of one array into another -function main() -> u32[3] { - let a = [1u32, 1u32]; +function main(a: u8[3]) { + let b = [1u8, 1u8]; - return [1u32, ...a] + assert_eq!(a, [1u8, ...b]); } \ No newline at end of file diff --git a/compiler/tests/mod.rs b/compiler/tests/mod.rs index e3077cfbfa..e9ec9d15f3 100644 --- a/compiler/tests/mod.rs +++ b/compiler/tests/mod.rs @@ -1,5 +1,5 @@ pub mod address; -// pub mod array; +pub mod array; pub mod boolean; // pub mod circuits; pub mod field;