leo/compiler/tests/integer/u32/mod.rs

107 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};
2020-06-02 04:35:43 +03:00
use leo_compiler::{
errors::{CompilerError, FunctionError, IntegerError},
types::Integer,
ConstrainedValue, InputValue,
};
2020-06-04 23:35:12 +03:00
use snarkos_models::gadgets::utilities::uint::UInt32;
const DIRECTORY_NAME: &str = "tests/integer/u32/";
2020-05-30 03:34:31 +03:00
pub(crate) fn output_zero(program: EdwardsTestCompiler) {
let output = get_output(program);
assert_eq!(
2020-05-30 03:34:31 +03:00
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Integer(Integer::U32(
UInt32::constant(0u32)
2020-05-30 03:34:31 +03:00
))])
.to_string(),
output.to_string()
)
}
2020-05-30 03:34:31 +03:00
pub(crate) fn output_one(program: EdwardsTestCompiler) {
let output = get_output(program);
assert_eq!(
2020-05-30 03:34:31 +03:00
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Integer(Integer::U32(
UInt32::constant(1u32)
2020-05-30 03:34:31 +03:00
))])
.to_string(),
output.to_string()
)
}
2020-05-30 03:34:31 +03:00
fn output_two(program: EdwardsTestCompiler) {
let output = get_output(program);
assert_eq!(
2020-05-30 03:34:31 +03:00
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Integer(Integer::U32(
UInt32::constant(2u32)
2020-05-30 03:34:31 +03:00
))])
.to_string(),
output.to_string()
)
}
2020-05-30 03:34:31 +03:00
fn fail_integer(program: EdwardsTestCompiler) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::IntegerError(
IntegerError::InvalidInteger(_string),
)) => {}
error => panic!("Expected invalid boolean error, got {}", error),
}
}
2020-05-30 03:34:31 +03:00
fn fail_synthesis(program: EdwardsTestCompiler) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::IntegerError(
IntegerError::SynthesisError(_string),
)) => {}
error => panic!("Expected synthesis error, got {}", error),
}
}
#[test]
fn test_input_u32_bool() {
let mut program = compile_program(DIRECTORY_NAME, "input_u32.leo").unwrap();
program.set_inputs(vec![Some(InputValue::Boolean(true))]);
fail_integer(program);
}
#[test]
fn test_input_u32_none() {
let mut program = compile_program(DIRECTORY_NAME, "input_u32.leo").unwrap();
program.set_inputs(vec![None]);
fail_synthesis(program);
}
#[test]
fn test_zero() {
let program = compile_program(DIRECTORY_NAME, "zero.leo").unwrap();
output_zero(program);
}
#[test]
fn test_one() {
let program = compile_program(DIRECTORY_NAME, "one.leo").unwrap();
output_one(program);
}
#[test]
fn test_1_plus_1() {
let program = compile_program(DIRECTORY_NAME, "1+1.leo").unwrap();
output_two(program);
}
#[test]
fn test_1_minus_1() {
let program = compile_program(DIRECTORY_NAME, "1-1.leo").unwrap();
output_zero(program)
}
// #[test] // Todo: Catch subtraction overflow error in gadget
// fn test_1_minus_2() {
// let program = compile_program(DIRECTORY_NAME, "1-2.leo").unwrap();
// let error = get_error(program);
// println!("{}", error);
// }