From 17784ffb23e481777ae8f84b8884c371cef07226 Mon Sep 17 00:00:00 2001 From: collin Date: Mon, 7 Sep 2020 13:47:50 -0700 Subject: [PATCH] add tests for mutable circuit variables using self keyword --- compiler/tests/circuits/mod.rs | 65 +++++++++++++++++++ compiler/tests/circuits/mut_function_fail.leo | 9 +++ .../tests/circuits/mut_self_function_fail.leo | 15 +++++ .../mut_self_static_function_fail.leo | 15 +++++ compiler/tests/circuits/mut_self_variable.leo | 21 ++++++ .../tests/circuits/mut_self_variable_fail.leo | 13 ++++ .../circuits/mut_static_function_fail.leo | 9 +++ compiler/tests/circuits/mut_variable.leo | 17 +++++ compiler/tests/circuits/mut_variable_fail.leo | 9 +++ 9 files changed, 173 insertions(+) create mode 100644 compiler/tests/circuits/mut_function_fail.leo create mode 100644 compiler/tests/circuits/mut_self_function_fail.leo create mode 100644 compiler/tests/circuits/mut_self_static_function_fail.leo create mode 100644 compiler/tests/circuits/mut_self_variable.leo create mode 100644 compiler/tests/circuits/mut_self_variable_fail.leo create mode 100644 compiler/tests/circuits/mut_static_function_fail.leo create mode 100644 compiler/tests/circuits/mut_variable.leo create mode 100644 compiler/tests/circuits/mut_variable_fail.leo diff --git a/compiler/tests/circuits/mod.rs b/compiler/tests/circuits/mod.rs index 912e462a15..54e44f49b9 100644 --- a/compiler/tests/circuits/mod.rs +++ b/compiler/tests/circuits/mod.rs @@ -134,6 +134,71 @@ fn test_member_static_function_undefined() { expect_fail(program) } +// Mutability +#[test] +fn test_mutate_function_fail() { + let bytes = include_bytes!("mut_function_fail.leo"); + let program = parse_program(bytes).unwrap(); + + expect_compiler_error(program); +} + +#[test] +fn test_mutate_self_variable() { + let bytes = include_bytes!("mut_self_variable.leo"); + let program = parse_program(bytes).unwrap(); + + assert_satisfied(program); +} + +#[test] +fn test_mutate_self_variable_fail() { + let bytes = include_bytes!("mut_self_variable_fail.leo"); + let program = parse_program(bytes).unwrap(); + + expect_compiler_error(program); +} + +#[test] +fn test_mutate_self_function_fail() { + let bytes = include_bytes!("mut_self_function_fail.leo"); + let program = parse_program(bytes).unwrap(); + + expect_compiler_error(program); +} + +#[test] +fn test_mutate_self_static_function_fail() { + let bytes = include_bytes!("mut_self_static_function_fail.leo"); + let program = parse_program(bytes).unwrap(); + + expect_compiler_error(program); +} + +#[test] +fn test_mutate_static_function_fail() { + let bytes = include_bytes!("mut_static_function_fail.leo"); + let program = parse_program(bytes).unwrap(); + + expect_compiler_error(program); +} + +#[test] +fn test_mutate_variable() { + let bytes = include_bytes!("mut_variable.leo"); + let program = parse_program(bytes).unwrap(); + + assert_satisfied(program); +} + +#[test] +fn test_mutate_variable_fail() { + let bytes = include_bytes!("mut_variable_fail.leo"); + let program = parse_program(bytes).unwrap(); + + expect_compiler_error(program); +} + // Self #[test] fn test_self_member_pass() { diff --git a/compiler/tests/circuits/mut_function_fail.leo b/compiler/tests/circuits/mut_function_fail.leo new file mode 100644 index 0000000000..ed3092c656 --- /dev/null +++ b/compiler/tests/circuits/mut_function_fail.leo @@ -0,0 +1,9 @@ +circuit Foo { + function bar() {} +} + +function main() { + let mut f = Foo { a: 0u8 }; + + f.bar = 1u8; +} \ No newline at end of file diff --git a/compiler/tests/circuits/mut_self_function_fail.leo b/compiler/tests/circuits/mut_self_function_fail.leo new file mode 100644 index 0000000000..18da32c5fb --- /dev/null +++ b/compiler/tests/circuits/mut_self_function_fail.leo @@ -0,0 +1,15 @@ +circuit Foo { + a: u8, + + function bar() {} + + function set_a(new: u8) { + self.bar = new; + } +} + +function main() { + let mut f = Foo { a: 0u8 }; + + f.set_a(1u8); +} \ No newline at end of file diff --git a/compiler/tests/circuits/mut_self_static_function_fail.leo b/compiler/tests/circuits/mut_self_static_function_fail.leo new file mode 100644 index 0000000000..de87c5fadf --- /dev/null +++ b/compiler/tests/circuits/mut_self_static_function_fail.leo @@ -0,0 +1,15 @@ +circuit Foo { + a: u8, + + static function bar() {} + + function set_a(new: u8) { + self.bar = new; + } +} + +function main() { + let mut f = Foo { a: 0u8 }; + + f.set_a(1u8); +} \ No newline at end of file diff --git a/compiler/tests/circuits/mut_self_variable.leo b/compiler/tests/circuits/mut_self_variable.leo new file mode 100644 index 0000000000..abc8ca56e0 --- /dev/null +++ b/compiler/tests/circuits/mut_self_variable.leo @@ -0,0 +1,21 @@ +circuit Foo { + mut a: u8, + + function set_a(new: u8) { + self.a = new; + } +} + +function main() { + let mut f = Foo { a: 0u8 }; + + console.assert(f.a == 0u8); + + f.set_a(1u8); + + console.assert(f.a == 1u8); + + f.set_a(2u8); + + console.assert(f.a == 2u8); +} \ No newline at end of file diff --git a/compiler/tests/circuits/mut_self_variable_fail.leo b/compiler/tests/circuits/mut_self_variable_fail.leo new file mode 100644 index 0000000000..551b041c9e --- /dev/null +++ b/compiler/tests/circuits/mut_self_variable_fail.leo @@ -0,0 +1,13 @@ +circuit Foo { + a: u8, + + function set_a(new: u8) { + self.a = new; + } +} + +function main() { + let mut f = Foo { a: 0u8 }; + + f.set_a(1u8); +} \ No newline at end of file diff --git a/compiler/tests/circuits/mut_static_function_fail.leo b/compiler/tests/circuits/mut_static_function_fail.leo new file mode 100644 index 0000000000..f1bccaaf89 --- /dev/null +++ b/compiler/tests/circuits/mut_static_function_fail.leo @@ -0,0 +1,9 @@ +circuit Foo { + static function bar() {} +} + +function main() { + let mut f = Foo { a: 0u8 }; + + f.bar = 1u8; +} \ No newline at end of file diff --git a/compiler/tests/circuits/mut_variable.leo b/compiler/tests/circuits/mut_variable.leo new file mode 100644 index 0000000000..eaf61ddf02 --- /dev/null +++ b/compiler/tests/circuits/mut_variable.leo @@ -0,0 +1,17 @@ +circuit Foo { + mut a: u8, +} + +function main() { + let mut f = Foo { a: 0u8 }; + + console.assert(f.a == 0u8); + + f.a = 1u8; + + console.assert(f.a == 1u8); + + f.a = 2u8; + + console.assert(f.a == 2u8); +} \ No newline at end of file diff --git a/compiler/tests/circuits/mut_variable_fail.leo b/compiler/tests/circuits/mut_variable_fail.leo new file mode 100644 index 0000000000..bb2e653ee3 --- /dev/null +++ b/compiler/tests/circuits/mut_variable_fail.leo @@ -0,0 +1,9 @@ +circuit Foo { + a: u8, +} + +function main() { + let mut f = Foo { a: 0u8 }; + + f.a = 1u8; +} \ No newline at end of file