-
Notifications
You must be signed in to change notification settings - Fork 480
Support quickcheck based property testing #319
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 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c953fac
Support quickcheck tests
cmichi f3d2fd7
Introduce ink-fuzz feature
cmichi 02f6a90
Add quickcheck property testing for HashMap
cmichi 98d49b7
Make ink-fuzz imply std feature
cmichi 8032250
Support quickcheck tests
cmichi 6647c2b
Apply rustfmt
cmichi 927218d
Merge branch 'master' into cmichi-support-quickcheck-tests
cmichi 5c87d6f
Merge branch 'master' into cmichi-support-quickcheck-tests
cmichi 48c2488
Merge branch 'master' into cmichi-support-quickcheck-tests
cmichi a88a43b
[core] Remove unnecessary code due changes in master in the meantime
cmichi 0d1e7d0
[core] More removals of no longer necessary code
cmichi a0563fe
[core] Apply cargo fmt
cmichi f8c3a3c
[core] Rename ink-fuzz to ink-fuzz-tests
cmichi d7b9297
[core] Split fuzz tests into separate file
cmichi 1086aa0
[core] Improve naming
cmichi e88f27e
Apply suggestions from code review
cmichi 1b5b002
Merge branch 'master' into cmichi-support-quickcheck-tests
cmichi 0f0cdb4
[core] Replace for_each with for loop
cmichi 1897f43
[core] Replace for_each with for loop
cmichi 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright 2019-2020 Parity Technologies (UK) Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use super::HashMap as StorageHashMap; | ||
| use crate::env; | ||
|
|
||
| use itertools::Itertools; | ||
|
|
||
| /// Conducts repeated insert and remove operations into the map by iterating | ||
| /// over `xs`. For each odd `x` in `xs` a defined number of insert operations | ||
| /// (`inserts_each`) is executed. For each even `x` it's asserted that the | ||
| /// previously inserted elements are in the map and they are removed subsequently. | ||
| /// | ||
| /// The reasoning behind this even/odd sequence is to introduce some | ||
| /// randomness into when elements are inserted/removed. | ||
| /// | ||
| /// `inserts_each` was chosen as `u8` to keep the number of inserts per `x` in | ||
| /// a reasonable range. | ||
| fn insert_and_remove(xs: Vec<i32>, inserts_each: u8) { | ||
| let mut map = <StorageHashMap<i32, i32>>::new(); | ||
| let mut cnt_inserts = 0; | ||
| let mut previous_even_x = None; | ||
| let inserts_each = inserts_each as i32; | ||
|
|
||
| xs.into_iter().for_each(|x| { | ||
| if x % 2 == 0 { | ||
| // On even numbers we insert | ||
| for key in x..x + inserts_each { | ||
| let val = key * 10; | ||
| if map.insert(key, val).is_none() { | ||
| assert_eq!(map.get(&key), Some(&val)); | ||
| cnt_inserts += 1; | ||
| } | ||
| assert_eq!(map.len(), cnt_inserts); | ||
| } | ||
| if previous_even_x.is_none() { | ||
| previous_even_x = Some(x); | ||
| } | ||
| } else if previous_even_x.is_some() { | ||
| // If it's an odd number and we inserted in a previous run we assert | ||
| // that the last insert worked correctly and remove the elements again. | ||
| // | ||
| // It can happen that after one insert run there are many more | ||
| // insert runs (i.e. even `x` in `xs`) before we remove the numbers | ||
| // of the last run again. This is intentional, as to include testing | ||
| // if subsequent insert operations have an effect on already inserted | ||
| // items. | ||
| let x = previous_even_x.unwrap(); | ||
| for key in x..x + inserts_each { | ||
| let val = key * 10; | ||
| assert_eq!(map.get(&key), Some(&val)); | ||
| assert_eq!(map.take(&key), Some(val)); | ||
| assert_eq!(map.get(&key), None); | ||
| cnt_inserts -= 1; | ||
| assert_eq!(map.len(), cnt_inserts); | ||
| } | ||
| previous_even_x = None; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| #[quickcheck] | ||
| fn inserts_and_removes(xs: Vec<i32>, inserts_each: u8) { | ||
| env::test::run_test::<env::DefaultEnvTypes, _>(|_| { | ||
| insert_and_remove(xs, inserts_each); | ||
| Ok(()) | ||
| }) | ||
| .unwrap() | ||
| } | ||
|
|
||
| /// Inserts all elements from `xs`. Then removes each `xth` element from the map | ||
| /// and asserts that all non-`xth` elements are still in the map. | ||
| #[quickcheck] | ||
| fn removes(xs: Vec<i32>, xth: usize) { | ||
| env::test::run_test::<env::DefaultEnvTypes, _>(|_| { | ||
| // given | ||
| let xs: Vec<i32> = xs.into_iter().unique().collect(); | ||
| let xth = xth.max(1); | ||
| let mut map = <StorageHashMap<i32, i32>>::new(); | ||
| let mut len = map.len(); | ||
|
|
||
| // when | ||
| // 1) insert all | ||
| xs.iter().for_each(|i| { | ||
|
||
| assert_eq!(map.insert(*i, i * 10), None); | ||
| len += 1; | ||
| assert_eq!(map.len(), len); | ||
| }); | ||
|
|
||
| // 2) remove every `xth` element of `xs` from the map | ||
| for x in 0..xs.len() { | ||
| if x % xth == 0 { | ||
| let i = xs.get(x).expect( | ||
| "x is always in bounds since we iterate over the vec length; qed", | ||
| ); | ||
| assert_eq!(map.take(&i), Some(i * 10)); | ||
| len -= 1; | ||
| } | ||
| assert_eq!(map.len(), len); | ||
| } | ||
|
|
||
| // then | ||
| // everything else must still be get-able | ||
| for x in 0..xs.len() { | ||
| if x % xth != 0 { | ||
| let i = xs.get(x).expect( | ||
| "x is always in bounds since we iterate over the vec length; qed", | ||
| ); | ||
| assert_eq!(map.get(&i), Some(&(i * 10))); | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| }) | ||
| .unwrap() | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.