2020-06-09 03:28:09 +03:00
|
|
|
use crate::{get_error, get_output, parse_program, EdwardsConstrainedValue, EdwardsTestCompiler};
|
2020-05-20 01:45:40 +03:00
|
|
|
use leo_compiler::{
|
2020-06-02 04:35:43 +03:00
|
|
|
errors::{BooleanError, CompilerError, ExpressionError, FunctionError, StatementError},
|
2020-06-08 07:26:49 +03:00
|
|
|
ConstrainedValue,
|
2020-05-20 01:45:40 +03:00
|
|
|
};
|
2020-06-02 04:35:43 +03:00
|
|
|
|
2020-06-11 02:14:55 +03:00
|
|
|
use crate::array::input_value_u32_one;
|
2020-05-20 01:45:40 +03:00
|
|
|
use snarkos_models::gadgets::utilities::boolean::Boolean;
|
|
|
|
|
2020-06-03 02:56:11 +03:00
|
|
|
pub fn output_expected_boolean(program: EdwardsTestCompiler, boolean: bool) {
|
2020-05-20 01:45:40 +03:00
|
|
|
let output = get_output(program);
|
|
|
|
assert_eq!(
|
2020-06-08 09:30:39 +03:00
|
|
|
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Boolean(Boolean::Constant(boolean))]).to_string(),
|
2020-05-30 03:34:31 +03:00
|
|
|
output.to_string()
|
2020-05-20 01:45:40 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-03 02:56:11 +03:00
|
|
|
pub fn output_true(program: EdwardsTestCompiler) {
|
|
|
|
output_expected_boolean(program, true)
|
|
|
|
}
|
|
|
|
|
2020-06-01 22:15:49 +03:00
|
|
|
pub fn output_false(program: EdwardsTestCompiler) {
|
2020-06-03 02:56:11 +03:00
|
|
|
output_expected_boolean(program, false)
|
2020-05-20 01:45:40 +03:00
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
fn fail_evaluate(program: EdwardsTestCompiler) {
|
2020-05-20 01:45:40 +03:00
|
|
|
match get_error(program) {
|
2020-06-08 09:30:39 +03:00
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
|
|
|
ExpressionError::BooleanError(BooleanError::CannotEvaluate(_string)),
|
|
|
|
))) => {}
|
2020-05-20 01:45:40 +03:00
|
|
|
error => panic!("Expected evaluate error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
fn fail_enforce(program: EdwardsTestCompiler) {
|
2020-05-20 01:45:40 +03:00
|
|
|
match get_error(program) {
|
2020-06-08 09:30:39 +03:00
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
|
|
|
ExpressionError::BooleanError(BooleanError::CannotEnforce(_string)),
|
|
|
|
))) => {}
|
2020-05-20 01:45:40 +03:00
|
|
|
error => panic!("Expected evaluate error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
fn fail_boolean(program: EdwardsTestCompiler) {
|
2020-05-20 03:08:38 +03:00
|
|
|
match get_error(program) {
|
2020-06-08 09:30:39 +03:00
|
|
|
CompilerError::FunctionError(FunctionError::BooleanError(BooleanError::InvalidBoolean(_string))) => {}
|
2020-05-20 03:08:38 +03:00
|
|
|
error => panic!("Expected invalid boolean error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
fn fail_synthesis(program: EdwardsTestCompiler) {
|
2020-05-20 03:08:38 +03:00
|
|
|
match get_error(program) {
|
2020-06-08 09:30:39 +03:00
|
|
|
CompilerError::FunctionError(FunctionError::BooleanError(BooleanError::SynthesisError(_string))) => {}
|
2020-05-20 03:08:38 +03:00
|
|
|
error => panic!("Expected synthesis error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
#[test]
|
|
|
|
fn test_true() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("true.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_true(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_false() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("false.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_false(program);
|
|
|
|
}
|
|
|
|
|
2020-05-20 03:08:38 +03:00
|
|
|
#[test]
|
|
|
|
fn test_input_bool_field() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("input_bool.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:28:09 +03:00
|
|
|
|
2020-05-20 03:08:38 +03:00
|
|
|
fail_boolean(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_input_bool_none() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("input_bool.leo");
|
|
|
|
let mut program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 03:08:38 +03:00
|
|
|
program.set_inputs(vec![None]);
|
2020-06-09 03:28:09 +03:00
|
|
|
|
2020-05-20 03:08:38 +03:00
|
|
|
fail_synthesis(program);
|
|
|
|
}
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
// Boolean not !
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_true() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("not_true.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_false(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_false() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("not_false.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_true(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_u32() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("not_u32.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
fail_evaluate(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Boolean or ||
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_true_or_true() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("true_||_true.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_true(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_true_or_false() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("true_||_false.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_true(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_false_or_false() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("false_||_false.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_false(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_true_or_u32() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("true_||_u32.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
fail_enforce(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Boolean and &&
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_true_and_true() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("true_&&_true.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_true(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_true_and_false() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("true_&&_false.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_false(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_false_and_false() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("false_&&_false.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_false(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_true_and_u32() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("true_&&_u32.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
fail_enforce(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
// All
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_all() {
|
2020-06-09 03:28:09 +03:00
|
|
|
let bytes = include_bytes!("all.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
output_false(program);
|
|
|
|
}
|