fix array tests

This commit is contained in:
collin 2020-07-30 00:56:17 -07:00
parent 14498c4836
commit df9b94ef73
14 changed files with 85 additions and 112 deletions

View File

@ -1,3 +1,3 @@
function main() -> u32[3] {
return [1u32; 3]
function main(a: u8[3]) {
assert_eq!(a, [1u8; 3]);
}

View File

@ -1,3 +1,3 @@
function main() -> u32[3] {
return [1u32, 1u32, 1u32]
function main(a: u8[3]) {
assert_eq!(a, [1u8, 1u8, 1u8]);
}

View File

@ -1,3 +0,0 @@
function main(arr: u32[3]) -> u32[3] {
return arr
}

View File

@ -0,0 +1,3 @@
function main() {
let a = [1u8, bool];
}

View File

@ -0,0 +1,2 @@
[registers]
r: u8[3] = [1u8, 1u8, 1u8];

View File

@ -0,0 +1,2 @@
[registers]
r: u8[3] = [0u8, 0u8, 0u8];

View File

@ -0,0 +1,2 @@
[main]
a: u8[3][2] = [[0; 3]; 2];

View File

@ -0,0 +1,2 @@
[main]
a: u8[3] = [1u8, 1u8, 1u8];

View File

@ -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);
}

View File

@ -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);
}

View File

@ -0,0 +1,3 @@
function main(registers) -> u8[3] {
return registers.r
}

View File

@ -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]);
}

View File

@ -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]);
}

View File

@ -1,5 +1,5 @@
pub mod address;
// pub mod array;
pub mod array;
pub mod boolean;
// pub mod circuits;
pub mod field;