-
Notifications
You must be signed in to change notification settings - Fork 599
test: add public context txe + state var tests #15706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 162 additions & 29 deletions
191
noir-projects/aztec-nr/aztec/src/state_vars/public_immutable/test.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,186 @@ | ||
| use crate::{context::PublicContext, state_vars::public_immutable::PublicImmutable}; | ||
| use crate::{ | ||
| context::{PrivateContext, PublicContext, UtilityContext}, | ||
| state_vars::public_immutable::PublicImmutable, | ||
| }; | ||
| use crate::test::{helpers::test_environment::TestEnvironment, mocks::mock_struct::MockStruct}; | ||
| use dep::protocol_types::traits::Serialize; | ||
| use std::mem::zeroed; | ||
|
|
||
| global storage_slot = 7; | ||
| global storage_slot: Field = 7; | ||
|
|
||
| fn setup() -> TestEnvironment { | ||
| TestEnvironment::new() | ||
| fn in_public(context: &mut PublicContext) -> PublicImmutable<MockStruct, &mut PublicContext> { | ||
| PublicImmutable::new(context, storage_slot) | ||
| } | ||
|
|
||
| fn in_public(env: TestEnvironment) -> PublicImmutable<MockStruct, &mut PublicContext> { | ||
| PublicImmutable::new(&mut env.public(), storage_slot) | ||
| fn in_private(context: &mut PrivateContext) -> PublicImmutable<MockStruct, &mut PrivateContext> { | ||
| PublicImmutable::new(context, storage_slot) | ||
| } | ||
|
|
||
| fn in_utility(context: UtilityContext) -> PublicImmutable<MockStruct, UtilityContext> { | ||
| PublicImmutable::new(context, storage_slot) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_uninitialized_by_default() { | ||
| let env = setup(); | ||
| let state_var = in_public(env); | ||
| unconstrained fn is_uninitialized_by_default() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| assert_eq(state_var.is_initialized(), false); | ||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| assert_eq(state_var.is_initialized(), false); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_initialize_uninitialized() { | ||
| let env = setup(); | ||
| let state_var = in_public(env); | ||
| unconstrained fn initialize_uninitialized_same_tx() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| let value = MockStruct::new(5, 6); | ||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
|
|
||
| let value = MockStruct::new(5, 6); | ||
| state_var.initialize(value); | ||
|
|
||
| assert(state_var.is_initialized()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn initialize_uninitialized_other_tx() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
|
|
||
| let value = MockStruct::new(5, 6); | ||
| state_var.initialize(value); | ||
| }); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| assert(state_var.is_initialized()); | ||
| }); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| unconstrained fn initialize_already_initialized_same_tx() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
|
|
||
| let value = MockStruct::new(5, 6); | ||
| state_var.initialize(value); | ||
|
|
||
| let other_value = MockStruct::new(7, 8); | ||
| state_var.initialize(other_value); | ||
| }); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| unconstrained fn initialize_already_initialized_other_tx() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| let value = MockStruct::new(5, 6); | ||
| state_var.initialize(value); | ||
| }); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| let other_value = MockStruct::new(7, 8); | ||
| state_var.initialize(other_value); | ||
| }); | ||
| } | ||
|
|
||
| #[test(should_fail_with = "Trying to read from uninitialized PublicImmutable")] | ||
| unconstrained fn read_uninitialized_public_fails() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| state_var.initialize(value); | ||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| let _ = state_var.read(); | ||
| }); | ||
| } | ||
|
|
||
| // TODO(#15703): this test should be made to fail | ||
| #[test] | ||
| unconstrained fn read_uninitialized_private_returns_default() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| env.private_context(|context| { | ||
| let state_var = in_private(context); | ||
| assert_eq(state_var.read(), zeroed()); | ||
| }); | ||
| } | ||
|
|
||
| // TODO(#15703): this test should be made to fail | ||
| #[test] | ||
| unconstrained fn read_uninitialized_utility_returns_default() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| env.utility_context(|context| { | ||
| let state_var = in_utility(context); | ||
| assert_eq(state_var.read(), zeroed()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn read_uninitialized_public_same_tx() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| assert(state_var.is_initialized()); | ||
| assert(state_var.read() == value); | ||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| let value = MockStruct::new(5, 6); | ||
| state_var.initialize(value); | ||
|
|
||
| assert_eq(state_var.read(), value); | ||
| }); | ||
| } | ||
|
|
||
| #[test(should_fail_with = "PublicImmutable already initialized")] | ||
| fn test_initialize_already_initialized() { | ||
| let env = setup(); | ||
| let state_var = in_public(env); | ||
| #[test] | ||
| unconstrained fn read_uninitialized_public_other_tx() { | ||
| let mut env = TestEnvironment::_new(); | ||
| let value = MockStruct::new(5, 6); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| state_var.initialize(value); | ||
| }); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| assert_eq(state_var.read(), value); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn read_uninitialized_private() { | ||
| let mut env = TestEnvironment::_new(); | ||
| let value = MockStruct::new(5, 6); | ||
|
|
||
| state_var.initialize(value); | ||
| state_var.initialize(value); | ||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| state_var.initialize(value); | ||
| }); | ||
|
|
||
| env.private_context(|context| { | ||
| let state_var = in_private(context); | ||
| assert_eq(state_var.read(), value); | ||
| }); | ||
| } | ||
|
|
||
| #[test(should_fail_with = "PublicImmutable not initialized")] | ||
| fn test_read_uninitialized() { | ||
| let env = setup(); | ||
| let state_var = in_public(env); | ||
| #[test] | ||
| unconstrained fn read_uninitialized_utility() { | ||
| let mut env = TestEnvironment::_new(); | ||
| let value = MockStruct::new(5, 6); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| state_var.initialize(value); | ||
| }); | ||
|
|
||
| let _ = state_var.read(); | ||
| env.utility_context(|context| { | ||
| let state_var = in_utility(context); | ||
| assert_eq(state_var.read(), value); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
noir-projects/aztec-nr/aztec/src/state_vars/public_mutable/test.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| use crate::{context::{PublicContext, UtilityContext}, state_vars::public_mutable::PublicMutable}; | ||
| use crate::test::{helpers::test_environment::TestEnvironment, mocks::mock_struct::MockStruct}; | ||
| use std::mem::zeroed; | ||
|
|
||
| global storage_slot: Field = 7; | ||
|
|
||
| fn in_public(context: &mut PublicContext) -> PublicMutable<MockStruct, &mut PublicContext> { | ||
| PublicMutable::new(context, storage_slot) | ||
| } | ||
|
|
||
| fn in_utility(context: UtilityContext) -> PublicMutable<MockStruct, UtilityContext> { | ||
| PublicMutable::new(context, storage_slot) | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn read_uninitialized_public_returns_zero() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| assert_eq(state_var.read(), zeroed()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn read_uninitialized_utility_returns_default() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| env.utility_context(|context| { | ||
| let state_var = in_utility(context); | ||
| assert_eq(state_var.read(), zeroed()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn write_read_public_same_tx() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| let value = MockStruct::new(5, 6); | ||
|
|
||
| state_var.write(value); | ||
| assert_eq(state_var.read(), value); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn write_read_public_other_tx() { | ||
| let mut env = TestEnvironment::_new(); | ||
|
|
||
| let value = MockStruct::new(5, 6); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| state_var.write(value); | ||
| }); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| assert_eq(state_var.read(), value); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn write_read_utility() { | ||
| let mut env = TestEnvironment::_new(); | ||
| let value = MockStruct::new(5, 6); | ||
|
|
||
| env.public_context(|context| { | ||
| let state_var = in_public(context); | ||
| state_var.write(value); | ||
| }); | ||
|
|
||
| env.utility_context(|context| { | ||
| let state_var = in_utility(context); | ||
| assert_eq(state_var.read(), value); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error message?