mirror of
https://github.com/AleoHQ/leo.git
synced 2025-01-04 07:59:02 +03:00
fix array tests
This commit is contained in:
parent
14498c4836
commit
df9b94ef73
@ -1,3 +1,3 @@
|
|||||||
function main() -> u32[3] {
|
function main(a: u8[3]) {
|
||||||
return [1u32; 3]
|
assert_eq!(a, [1u8; 3]);
|
||||||
}
|
}
|
@ -1,3 +1,3 @@
|
|||||||
function main() -> u32[3] {
|
function main(a: u8[3]) {
|
||||||
return [1u32, 1u32, 1u32]
|
assert_eq!(a, [1u8, 1u8, 1u8]);
|
||||||
}
|
}
|
@ -1,3 +0,0 @@
|
|||||||
function main(arr: u32[3]) -> u32[3] {
|
|
||||||
return arr
|
|
||||||
}
|
|
3
compiler/tests/array/inputs/inline_fail.leo
Normal file
3
compiler/tests/array/inputs/inline_fail.leo
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
function main() {
|
||||||
|
let a = [1u8, bool];
|
||||||
|
}
|
2
compiler/tests/array/inputs/registers_ones.in
Normal file
2
compiler/tests/array/inputs/registers_ones.in
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[registers]
|
||||||
|
r: u8[3] = [1u8, 1u8, 1u8];
|
2
compiler/tests/array/inputs/registers_zeros.in
Normal file
2
compiler/tests/array/inputs/registers_zeros.in
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[registers]
|
||||||
|
r: u8[3] = [0u8, 0u8, 0u8];
|
2
compiler/tests/array/inputs/six_zeros.in
Normal file
2
compiler/tests/array/inputs/six_zeros.in
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[main]
|
||||||
|
a: u8[3][2] = [[0; 3]; 2];
|
2
compiler/tests/array/inputs/three_ones.in
Normal file
2
compiler/tests/array/inputs/three_ones.in
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[main]
|
||||||
|
a: u8[3] = [1u8, 1u8, 1u8];
|
@ -1,133 +1,95 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
get_error,
|
assert_satisfied,
|
||||||
get_output,
|
get_compiler_error,
|
||||||
integers::fail_integer,
|
get_outputs,
|
||||||
parse_program,
|
parse_program,
|
||||||
EdwardsConstrainedValue,
|
parse_program_with_inputs,
|
||||||
EdwardsTestCompiler,
|
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]
|
assert!(expected.eq(actual.bytes().as_slice()));
|
||||||
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()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// [[0, 0, 0],
|
pub fn output_zeros(program: EdwardsTestCompiler) {
|
||||||
// [0, 0, 0]]
|
let expected = include_bytes!("outputs/registers_zeros.out");
|
||||||
fn output_multi(program: EdwardsTestCompiler) {
|
let actual = get_outputs(program);
|
||||||
let output = get_output(program);
|
|
||||||
assert_eq!(
|
assert!(expected.eq(actual.bytes().as_slice()));
|
||||||
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Array(vec![
|
|
||||||
ConstrainedValue::Array(
|
|
||||||
vec![ConstrainedValue::Integer(Integer::U32(UInt32::constant(0u32))); 3]
|
|
||||||
);
|
|
||||||
2
|
|
||||||
])])
|
|
||||||
.to_string(),
|
|
||||||
output.to_string()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_array(program: EdwardsTestCompiler) {
|
// Registers
|
||||||
match get_error(program) {
|
|
||||||
CompilerError::FunctionError(FunctionError::Error(_string)) => {}
|
|
||||||
error => panic!("Expected function error, found {}", error),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn input_value_u32_one() -> InputValue {
|
#[test]
|
||||||
InputValue::Integer(IntegerType::U32Type(U32Type {}), 1.to_string())
|
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
|
// Expressions
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_inline() {
|
fn test_inline() {
|
||||||
let bytes = include_bytes!("inline.leo");
|
let program_bytes = include_bytes!("inline.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
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]
|
#[test]
|
||||||
fn test_initializer() {
|
fn test_initializer() {
|
||||||
let bytes = include_bytes!("initializer.leo");
|
let program_bytes = include_bytes!("initializer.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
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]
|
#[test]
|
||||||
fn test_spread() {
|
fn test_spread() {
|
||||||
let bytes = include_bytes!("spread.leo");
|
let program_bytes = include_bytes!("spread.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
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]
|
#[test]
|
||||||
fn test_slice() {
|
fn test_slice() {
|
||||||
let bytes = include_bytes!("slice.leo");
|
let program_bytes = include_bytes!("slice.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
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]
|
#[test]
|
||||||
fn test_multi() {
|
fn test_multi() {
|
||||||
let bytes = include_bytes!("multi.leo");
|
let program_bytes = include_bytes!("multi.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
let program = parse_program(program_bytes).unwrap();
|
||||||
|
|
||||||
output_multi(program);
|
assert_satisfied(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)
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
// Multidimensional array syntax in leo
|
// Multidimensional array syntax in leo
|
||||||
function main() -> u32[3][2] {
|
function main() {
|
||||||
const m = [[0u32, 0u32, 0u32], [0u32, 0u32, 0u32]]; // inline
|
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);
|
||||||
}
|
}
|
3
compiler/tests/array/registers.leo
Normal file
3
compiler/tests/array/registers.leo
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
function main(registers) -> u8[3] {
|
||||||
|
return registers.r
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
// `{from}..{to}` copies the elements of one array into another exclusively
|
// `{from}..{to}` copies the elements of one array into another exclusively
|
||||||
function main() -> u32[3] {
|
function main(a: u8[3]) {
|
||||||
let a = [1u32; 4];
|
let b = [1u8; 4];
|
||||||
|
|
||||||
return a[0..3]
|
assert_eq!(a, b[0..3]);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// A spread operator `...` copies the elements of one array into another
|
// A spread operator `...` copies the elements of one array into another
|
||||||
function main() -> u32[3] {
|
function main(a: u8[3]) {
|
||||||
let a = [1u32, 1u32];
|
let b = [1u8, 1u8];
|
||||||
|
|
||||||
return [1u32, ...a]
|
assert_eq!(a, [1u8, ...b]);
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
pub mod address;
|
pub mod address;
|
||||||
// pub mod array;
|
pub mod array;
|
||||||
pub mod boolean;
|
pub mod boolean;
|
||||||
// pub mod circuits;
|
// pub mod circuits;
|
||||||
pub mod field;
|
pub mod field;
|
||||||
|
Loading…
Reference in New Issue
Block a user