leo/compiler/tests/array/mod.rs

111 lines
2.9 KiB
Rust
Raw Normal View History

2020-05-30 03:34:31 +03:00
use crate::{compile_program, get_error, get_output, EdwardsConstrainedValue, EdwardsTestCompiler};
use leo_compiler::{
2020-06-08 07:26:49 +03:00
errors::{CompilerError, FunctionError},
ConstrainedValue,
};
2020-06-08 07:26:49 +03:00
use leo_types::{InputValue, Integer, IntegerError};
2020-06-02 04:35:43 +03:00
2020-06-04 23:35:12 +03:00
use snarkos_models::gadgets::utilities::uint::UInt32;
const DIRECTORY_NAME: &str = "tests/array/";
// [1, 1, 1]
2020-05-30 03:34:31 +03:00
fn output_ones(program: EdwardsTestCompiler) {
let output = get_output(program);
assert_eq!(
2020-06-08 09:30:39 +03:00
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Array(vec![
ConstrainedValue::Integer(
Integer::U32(UInt32::constant(1u32))
);
3
])])
2020-05-30 03:34:31 +03:00
.to_string(),
output.to_string()
);
}
// [[0, 0, 0],
// [0, 0, 0]]
2020-05-30 03:34:31 +03:00
fn output_multi(program: EdwardsTestCompiler) {
let output = get_output(program);
assert_eq!(
2020-05-30 03:34:31 +03:00
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Array(vec![
ConstrainedValue::Array(
vec![ConstrainedValue::Integer(Integer::U32(UInt32::constant(0u32))); 3]
);
2
2020-05-30 03:34:31 +03:00
])])
.to_string(),
output.to_string()
)
}
2020-05-30 03:34:31 +03:00
fn fail_array(program: EdwardsTestCompiler) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::InvalidArray(_string)) => {}
error => panic!("Expected invalid array error, got {}", error),
}
}
2020-05-30 03:34:31 +03:00
fn fail_synthesis(program: EdwardsTestCompiler) {
match get_error(program) {
2020-06-08 09:30:39 +03:00
CompilerError::FunctionError(FunctionError::IntegerError(IntegerError::SynthesisError(_string))) => {}
error => panic!("Expected synthesis error, got {}", error),
}
}
// Expressions
#[test]
fn test_inline() {
let program = compile_program(DIRECTORY_NAME, "inline.leo").unwrap();
output_ones(program);
}
#[test]
fn test_initializer() {
let program = compile_program(DIRECTORY_NAME, "initializer.leo").unwrap();
output_ones(program);
}
#[test]
fn test_spread() {
let program = compile_program(DIRECTORY_NAME, "spread.leo").unwrap();
output_ones(program);
}
#[test]
fn test_slice() {
let program = compile_program(DIRECTORY_NAME, "slice.leo").unwrap();
output_ones(program);
}
#[test]
fn test_multi() {
let program = compile_program(DIRECTORY_NAME, "multi.leo").unwrap();
output_multi(program);
}
// Inputs
#[test]
fn test_input_array() {
let mut program = compile_program(DIRECTORY_NAME, "input_array.leo").unwrap();
2020-06-08 09:30:39 +03:00
program.set_inputs(vec![Some(InputValue::Array(vec![InputValue::Integer(1u128); 3]))]);
output_ones(program)
}
#[test]
fn test_input_array_fail() {
let mut program = compile_program(DIRECTORY_NAME, "input_array.leo").unwrap();
2020-06-06 02:51:26 +03:00
program.set_inputs(vec![Some(InputValue::Integer(1u128))]);
fail_array(program);
}
#[test]
fn test_input_field_none() {
let mut program = compile_program(DIRECTORY_NAME, "input_array.leo").unwrap();
program.set_inputs(vec![None]);
fail_synthesis(program)
}