fix circuit tests

This commit is contained in:
collin 2020-07-30 11:11:54 -07:00
parent df9b94ef73
commit 41559ba3a0
30 changed files with 154 additions and 241 deletions

View File

@ -1,4 +1,4 @@
use crate::{assert_satisfied, generate_main_inputs, get_compiler_error, parse_program};
use crate::{assert_satisfied, expect_compiler_error, generate_main_inputs, parse_program};
use leo_types::InputValue;
static TEST_ADDRESS_1: &'static str = "aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8";
@ -17,7 +17,7 @@ fn test_invalid() {
let bytes = include_bytes!("invalid.leo");
let program = parse_program(bytes).unwrap();
let _output = get_compiler_error(program);
let _output = expect_compiler_error(program);
}
#[test]
@ -33,7 +33,7 @@ fn test_implicit_invalid() {
let bytes = include_bytes!("implicit_invalid.leo");
let program = parse_program(bytes).unwrap();
let _output = get_compiler_error(program);
let _output = expect_compiler_error(program);
}
#[test]
@ -49,7 +49,7 @@ fn test_assert_eq_fail() {
let bytes = include_bytes!("assert_eq_fail.leo");
let program = parse_program(bytes).unwrap();
let _output = get_compiler_error(program);
let _output = expect_compiler_error(program);
}
#[test]

View File

@ -1,6 +1,6 @@
use crate::{
assert_satisfied,
get_compiler_error,
expect_compiler_error,
get_outputs,
parse_program,
parse_program_with_inputs,
@ -8,14 +8,14 @@ use crate::{
};
pub fn output_ones(program: EdwardsTestCompiler) {
let expected = include_bytes!("outputs/registers_ones.out");
let expected = include_bytes!("outputs_/registers_ones.out");
let actual = get_outputs(program);
assert!(expected.eq(actual.bytes().as_slice()));
}
pub fn output_zeros(program: EdwardsTestCompiler) {
let expected = include_bytes!("outputs/registers_zeros.out");
let expected = include_bytes!("outputs_/registers_zeros.out");
let actual = get_outputs(program);
assert!(expected.eq(actual.bytes().as_slice()));
@ -56,7 +56,7 @@ fn test_inline_fail() {
let program_bytes = include_bytes!("inline.leo");
let program = parse_program(program_bytes).unwrap();
let _err = get_compiler_error(program);
let _err = expect_compiler_error(program);
}
#[test]

View File

@ -0,0 +1,2 @@
[registers]
r: u8[3] = [1u8, 1u8, 1u8];

View File

@ -0,0 +1,2 @@
[registers]
r: u8[3] = [0u8, 0u8, 0u8];

View File

@ -1,8 +1,8 @@
use crate::{
assert_satisfied,
get_compiler_error,
expect_compiler_error,
expect_synthesis_error,
get_outputs,
get_synthesis_error,
parse_program,
parse_program_with_inputs,
EdwardsTestCompiler,
@ -10,21 +10,21 @@ use crate::{
use leo_compiler::errors::{BooleanError, CompilerError, ExpressionError, FunctionError, StatementError};
pub fn output_true(program: EdwardsTestCompiler) {
let expected = include_bytes!("outputs/register_true.out");
let expected = include_bytes!("outputs_/register_true.out");
let actual = get_outputs(program);
assert_eq!(expected, actual.bytes().as_slice());
}
pub fn output_false(program: EdwardsTestCompiler) {
let expected = include_bytes!("outputs/register_false.out");
let expected = include_bytes!("outputs_/register_false.out");
let actual = get_outputs(program);
assert_eq!(expected, actual.bytes().as_slice());
}
fn fail_boolean_statement(program: EdwardsTestCompiler) {
match get_compiler_error(program) {
match expect_compiler_error(program) {
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::BooleanError(BooleanError::Error(_)),
))) => {}
@ -49,7 +49,7 @@ fn test_input_fail() {
let program = parse_program_with_inputs(program_bytes, input_bytes).unwrap();
get_synthesis_error(program);
expect_synthesis_error(program);
}
#[test]

View File

@ -0,0 +1,2 @@
[registers]
r: bool = false;

View File

@ -0,0 +1,2 @@
[registers]
r: bool = true;

View File

@ -2,6 +2,6 @@ circuit Foo {
x: u32
}
function main() -> Foo {
return Foo { x: 1u32 }
function main() {
let a = Foo { x: 1u32 };
}

View File

@ -3,5 +3,5 @@ circuit Foo {
}
function main() {
let c = Foo { y: 0u32 };
let a = Foo { y: 0u32 };
}

View File

@ -1,3 +1,3 @@
function main() {
let c = Foo { };
let a = Foo { };
}

View File

@ -2,8 +2,8 @@ circuit Foo {
x: u32,
}
function main() -> u32 {
let c = Foo { x: 1u32 };
function main() {
let a = Foo { x: 1u32 };
return c.x
assert_eq!(a.x, 1u32);
}

View File

@ -2,11 +2,14 @@ circuit Foo {
foo: u32,
static function bar() -> u32 {
return 0
return 1u32
}
}
function main() -> u32 {
let f = Foo { foo: 1 };
return f.foo + Foo::bar()
function main() {
let a = Foo { foo: 1 };
let b = a.foo + Foo::bar();
assert_eq!(b, 2u32);
}

View File

@ -2,8 +2,8 @@ circuit Foo {
x: u32
}
function main() -> u32 {
let c = Foo { x: 1u32 };
function main() {
let a = Foo { x: 1u32 };
return c.y
let err = a.y;
}

View File

@ -4,7 +4,8 @@ circuit Foo {
}
}
function main() -> u32 {
let c = Foo { };
return c.echo(1u32)
function main() {
let a = Foo { };
assert_eq!(a.echo(1u32), 1u32);
}

View File

@ -4,7 +4,7 @@ circuit Foo {
}
}
function main() -> u32 {
let c = Foo { };
return c.echoed(1u32)
function main() {
let a = Foo { };
let err = a.echoed(1u32);
}

View File

@ -4,7 +4,7 @@ circuit Foo {
}
}
function main() -> u32 {
let c = Foo { };
return c.echo(1u32) // echo is a static function and must be accessed using `::`
function main() {
let a = Foo { };
let err = a.echo(1u32); // echo is a static function and must be accessed using `::`
}

View File

@ -10,8 +10,9 @@ circuit Foo {
}
}
function main() -> u32 {
let f = Foo { x: 1u32 };
function main() {
let a = Foo { x: 1u32 };
let b = a.call_add_x(1u32);
return f.call_add_x(1u32)
assert_eq!(b, 2u32);
}

View File

@ -4,6 +4,8 @@ circuit Foo {
}
}
function main() -> u32 {
return Foo::echo(1u32)
function main() {
let a = Foo::echo(1u32);
assert_eq!(a, 1u32);
}

View File

@ -4,6 +4,6 @@ circuit Foo {
}
}
function main() -> u32 {
return Foo::echo(1u32) // echo is a non-static function and must be accessed using `.`
function main() {
let err = Foo::echo(1u32); // echo is a non-static function and must be accessed using `.`
}

View File

@ -4,6 +4,6 @@ circuit Foo {
}
}
function main() -> u32 {
return Foo::echoed(1u32)
function main() {
let err = Foo::echoed(1u32);
}

View File

@ -1,55 +1,49 @@
use crate::{
get_error,
get_output,
integers::u32::{output_number, output_one},
parse_program,
EdwardsConstrainedValue,
EdwardsTestCompiler,
};
use leo_compiler::{
errors::{CompilerError, ExpressionError, FunctionError, StatementError},
ConstrainedCircuitMember,
ConstrainedValue,
Integer,
};
use leo_types::{Expression, Function, Identifier, Span, Statement, Type};
use snarkos_models::gadgets::utilities::uint::UInt32;
// Foo { x: 1u32 }
fn output_circuit(program: EdwardsTestCompiler) {
let output = get_output(program);
assert_eq!(
EdwardsConstrainedValue::Return(vec![ConstrainedValue::CircuitExpression(
Identifier {
name: "Foo".to_string(),
span: Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0
}
},
vec![ConstrainedCircuitMember(
Identifier {
name: "x".to_string(),
span: Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0
}
},
ConstrainedValue::Integer(Integer::U32(UInt32::constant(1u32)))
)]
)])
.to_string(),
output.to_string()
);
}
use crate::{assert_satisfied, expect_compiler_error, parse_program, EdwardsTestCompiler};
use leo_compiler::errors::{CompilerError, ExpressionError, FunctionError, StatementError};
// use leo_compiler::{
// errors::{CompilerError, ExpressionError, FunctionError, StatementError},
// ConstrainedCircuitMember,
// ConstrainedValue,
// Integer,
// };
// use leo_types::{Expression, Function, Identifier, Span, Statement, Type};
// use snarkos_models::gadgets::utilities::uint::UInt32;
//
// // Foo { x: 1u32 }
// fn output_circuit(program: EdwardsTestCompiler) {
// let output = get_output(program);
// assert_eq!(
// EdwardsConstrainedValue::Return(vec![ConstrainedValue::CircuitExpression(
// Identifier {
// name: "Foo".to_string(),
// span: Span {
// text: "".to_string(),
// line: 0,
// start: 0,
// end: 0
// }
// },
// vec![ConstrainedCircuitMember(
// Identifier {
// name: "x".to_string(),
// span: Span {
// text: "".to_string(),
// line: 0,
// start: 0,
// end: 0
// }
// },
// ConstrainedValue::Integer(Integer::U32(UInt32::constant(1u32)))
// )]
// )])
// .to_string(),
// output.to_string()
// );
// }
//
fn expect_fail(program: EdwardsTestCompiler) {
match get_error(program) {
match expect_compiler_error(program) {
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::Error(_string),
))) => {}
@ -64,7 +58,7 @@ fn test_inline() {
let bytes = include_bytes!("inline.leo");
let program = parse_program(bytes).unwrap();
output_circuit(program);
assert_satisfied(program);
}
#[test]
@ -72,7 +66,7 @@ fn test_inline_fail() {
let bytes = include_bytes!("inline_fail.leo");
let program = parse_program(bytes).unwrap();
expect_fail(program)
expect_fail(program);
}
#[test]
@ -80,12 +74,7 @@ fn test_inline_undefined() {
let bytes = include_bytes!("inline_undefined.leo");
let program = parse_program(bytes).unwrap();
match get_error(program) {
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::Error(_),
))) => {}
error => panic!("Expected undefined circuit error, got {}", error),
}
expect_fail(program);
}
// Members
@ -95,7 +84,7 @@ fn test_member_field() {
let bytes = include_bytes!("member_field.leo");
let program = parse_program(bytes).unwrap();
output_one(program);
assert_satisfied(program);
}
#[test]
@ -111,7 +100,7 @@ fn test_member_field_and_function() {
let bytes = include_bytes!("member_field_and_function.leo");
let program = parse_program(bytes).unwrap();
output_one(program);
assert_satisfied(program);
}
#[test]
@ -119,7 +108,7 @@ fn test_member_function() {
let bytes = include_bytes!("member_function.leo");
let program = parse_program(bytes).unwrap();
output_one(program);
assert_satisfied(program);
}
#[test]
@ -143,7 +132,7 @@ fn test_member_function_nested() {
let bytes = include_bytes!("member_function_nested.leo");
let program = parse_program(bytes).unwrap();
output_number(program, 2u32);
assert_satisfied(program);
}
#[test]
@ -151,15 +140,7 @@ fn test_member_static_function() {
let bytes = include_bytes!("member_static_function.leo");
let program = parse_program(bytes).unwrap();
output_one(program);
}
#[test]
fn test_member_static_function_undefined() {
let bytes = include_bytes!("member_static_function_undefined.leo");
let program = parse_program(bytes).unwrap();
expect_fail(program)
assert_satisfied(program);
}
#[test]
@ -170,135 +151,45 @@ fn test_member_static_function_invalid() {
expect_fail(program)
}
#[test]
fn test_member_static_function_undefined() {
let bytes = include_bytes!("member_static_function_undefined.leo");
let program = parse_program(bytes).unwrap();
expect_fail(program)
}
// Self
#[test]
fn test_self_member() {
fn test_self_member_pass() {
let bytes = include_bytes!("self_member.leo");
let program = parse_program(bytes).unwrap();
output_one(program);
assert_satisfied(program);
}
#[test]
fn test_self_no_member_fail() {
let bytes = include_bytes!("self_no_member_fail.leo");
fn test_self_member_invalid() {
let bytes = include_bytes!("self_member_invalid.leo");
let program = parse_program(bytes).unwrap();
let _err = get_error(program);
let _err = expect_compiler_error(program);
}
#[test]
fn test_self_member_fail() {
let bytes = include_bytes!("self_member_fail.leo");
fn test_self_member_undefined() {
let bytes = include_bytes!("self_member_undefined.leo");
let program = parse_program(bytes).unwrap();
let _err = get_error(program);
}
#[test]
fn test_self_circuit() {
let bytes = include_bytes!("self_circuit.leo");
let program = parse_program(bytes).unwrap();
let output = get_output(program);
// circuit Foo {
// static function new() -> Self {
// return Self { }
// }
// }
assert_eq!(
EdwardsConstrainedValue::Return(vec![ConstrainedValue::CircuitExpression(
Identifier {
name: "Foo".to_string(),
span: Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0
}
},
vec![ConstrainedCircuitMember(
Identifier {
name: "new".to_string(),
span: Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0
}
},
ConstrainedValue::Static(Box::new(ConstrainedValue::Function(
Some(Identifier {
name: "new".to_string(),
span: Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0
}
}),
Function {
function_name: Identifier {
name: "new".to_string(),
span: Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0
}
},
inputs: vec![],
returns: vec![Type::SelfType],
statements: vec![Statement::Return(
vec![Expression::Circuit(
Identifier {
name: "Self".to_string(),
span: Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0
}
},
vec![],
Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0
}
)],
Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0
}
)],
span: Span {
text: "".to_string(),
line: 0,
start: 0,
end: 0
}
}
)))
)]
)])
.to_string(),
output.to_string()
);
let _err = expect_compiler_error(program);
}
// All
#[test]
fn test_pedersen_mock() {
use crate::integers::u32::output_zero;
let bytes = include_bytes!("pedersen_mock.leo");
let program = parse_program(bytes).unwrap();
output_zero(program);
assert_satisfied(program);
}

View File

@ -16,9 +16,10 @@ circuit PedersenHash {
}
// The 'pedersen_hash' main function.
function main() -> u32 {
function main() {
let parameters = [0u32; 512];
let pedersen = PedersenHash::new(parameters);
let input: bool[512] = [true; 512];
return pedersen.hash(input)
assert_eq!(pedersen.hash(input), 0u32);
}

View File

@ -4,6 +4,6 @@ circuit Foo {
}
}
function main() -> Foo {
return Foo::new()
function main() {
let a = Foo::new();
}

View File

@ -6,7 +6,9 @@ circuit Foo {
}
}
function main() -> u32 {
let foo = Foo { f: 1u32 };
return foo.bar()
function main() {
let a = Foo { f: 1u32 };
let b = a.bar();
assert_eq!(b, 1u32);
}

View File

@ -8,5 +8,5 @@ circuit Foo {
function main() -> u32 {
let foo = Foo { f: 1u32 };
return foo.bar()
let err = foo.bar();
}

View File

@ -4,7 +4,7 @@ circuit Foo {
}
}
function main() -> u32 {
function main() {
let foo = Foo { };
return foo.bar()
let err = foo.bar();
}

View File

@ -1,4 +1,4 @@
use crate::{assert_satisfied, generate_main_inputs, get_synthesis_error, parse_program};
use crate::{assert_satisfied, expect_synthesis_error, generate_main_inputs, parse_program};
use leo_types::InputValue;
use snarkos_curves::edwards_bls12::Fq;
@ -232,7 +232,7 @@ fn test_assert_eq_fail() {
program.set_main_inputs(main_inputs);
get_synthesis_error(program);
expect_synthesis_error(program);
}
}
@ -278,14 +278,14 @@ fn test_ternary() {
//
// pub fn output_one(program: EdwardsTestCompiler) {
// let expected = include_bytes!("outputs/register_one.out");
// let expected = include_bytes!("outputs_/register_one.out");
// let actual = get_outputs(program);
//
// assert_eq!(expected, actual.bytes().as_slice());
// }
//
// pub fn output_zero(program: EdwardsTestCompiler) {
// let expected = include_bytes!("outputs/register_zero.out");
// let expected = include_bytes!("outputs_/register_zero.out");
// let actual = get_outputs(program);
//
// assert_eq!(expected, actual.bytes().as_slice());

View File

@ -0,0 +1,2 @@
[registers]
r: field = 1;

View File

@ -0,0 +1,2 @@
[registers]
r: field = 0;

View File

@ -1,7 +1,7 @@
pub mod address;
pub mod array;
pub mod boolean;
// pub mod circuits;
pub mod circuits;
pub mod field;
// pub mod function;
// pub mod group;
@ -94,12 +94,12 @@ pub(crate) fn assert_satisfied(program: EdwardsTestCompiler) {
assert_eq!(empty_output_bytes, res.bytes().as_slice());
}
pub(crate) fn get_compiler_error(program: EdwardsTestCompiler) -> CompilerError {
pub(crate) fn expect_compiler_error(program: EdwardsTestCompiler) -> CompilerError {
let mut cs = TestConstraintSystem::<Fq>::new();
program.generate_constraints_helper(&mut cs).unwrap_err()
}
pub(crate) fn get_synthesis_error(program: EdwardsTestCompiler) {
pub(crate) fn expect_synthesis_error(program: EdwardsTestCompiler) {
let mut cs = TestConstraintSystem::<Fq>::new();
let _output = program.generate_constraints_helper(&mut cs).unwrap();