leo/compiler/tests/array/mod.rs

134 lines
2.9 KiB
Rust
Raw Normal View History

use crate::{
get_error,
get_output,
integers::fail_integer,
parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
};
use leo_compiler::{
2020-06-08 07:26:49 +03:00
errors::{CompilerError, FunctionError},
ConstrainedValue,
Integer,
};
use leo_inputs::types::{IntegerType, U32Type};
use leo_types::InputValue;
2020-06-02 04:35:43 +03:00
2020-06-04 23:35:12 +03:00
use snarkos_models::gadgets::utilities::uint::UInt32;
// [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::Error(_string)) => {}
2020-06-23 08:32:57 +03:00
error => panic!("Expected function error, found {}", error),
}
}
2020-06-11 02:14:55 +03:00
pub(crate) fn input_value_u32_one() -> InputValue {
InputValue::Integer(IntegerType::U32Type(U32Type {}), 1)
}
// Expressions
#[test]
fn test_inline() {
2020-06-09 03:51:06 +03:00
let bytes = include_bytes!("inline.leo");
let program = parse_program(bytes).unwrap();
output_ones(program);
}
#[test]
fn test_initializer() {
2020-06-09 03:51:06 +03:00
let bytes = include_bytes!("initializer.leo");
let program = parse_program(bytes).unwrap();
output_ones(program);
}
#[test]
fn test_spread() {
2020-06-09 03:51:06 +03:00
let bytes = include_bytes!("spread.leo");
let program = parse_program(bytes).unwrap();
output_ones(program);
}
#[test]
fn test_slice() {
2020-06-09 03:51:06 +03:00
let bytes = include_bytes!("slice.leo");
let program = parse_program(bytes).unwrap();
output_ones(program);
}
#[test]
fn test_multi() {
2020-06-09 03:51:06 +03:00
let bytes = include_bytes!("multi.leo");
let program = parse_program(bytes).unwrap();
output_multi(program);
}
// Inputs
#[test]
fn test_input_array() {
2020-06-09 03:51:06 +03:00
let bytes = include_bytes!("input.leo");
let mut program = parse_program(bytes).unwrap();
2020-06-11 02:14:55 +03:00
program.set_inputs(vec![Some(InputValue::Array(vec![input_value_u32_one(); 3]))]);
2020-06-09 03:51:06 +03:00
output_ones(program)
}
#[test]
fn test_input_array_fail() {
2020-06-09 03:51:06 +03:00
let bytes = include_bytes!("input.leo");
let mut program = parse_program(bytes).unwrap();
2020-06-11 02:14:55 +03:00
program.set_inputs(vec![Some(input_value_u32_one())]);
2020-06-09 03:51:06 +03:00
fail_array(program);
}
#[test]
fn test_input_field_none() {
2020-06-09 03:51:06 +03:00
let bytes = include_bytes!("input.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![None]);
2020-06-09 03:51:06 +03:00
fail_integer(program)
}