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
Permutations, variations and combinations with/without repetition #776
Comments
Interesting idea! I'm not entirely sure I understand it correctly... How does this differ from |
A @CartesianTest combines the elements of two sets to pairs and tests them. This is very useful. |
This is a cool idea! 😎 What I didn't quite get so far is how exactly this leads to test cases. As far as I understand, the sequence is:
But we need more than one parameter, right? In the sorting example from earlier, [x, y, z] as the correct solution should also be passed to the test, right? This leads me to the question, which of these should it be:
We don't need to answer this question immediately. On the contrary, it may be interesting to pursue all two or three for a bit and see where that leads. |
I have created this github repo for you with a generator and a demo (called sample in that project): IMPORTANT: This project is a DRAFT and I am pretty sure you will immediately find a lot what can be improved. A short description:
The generators are LoopingGenerator, RecursionGenerator and BruteForceGenerator. The sample is about two counting sort implementations: If you can't remember what the differences were: I hope you find it interesting. |
/> This is a cool idea! 😎 Thanks. I have always wanted to contribute something wonderful to your great JUnit Pioneer project!
Yes, exactly. Those are permutations without repetition. Just look into BitSetCountingSorterTest and ByteArrayCountingSorterTest to watch them in action.
You always need a list of elements which can be numbers, strings or other objects. It seems to me "a generator for parameterized tests" might be a good way to integrate the combinatorial generator into JUnit Pioneer. It can look similar to a CartesianTest.
I am very confident we find a good way to integrate the combinatorial generator into JUnit Pioneer. |
We had a call and the demo looks great. I asked @mmirwaldt to go ahead with a PR. 😄 |
Some tests use permutations, variations and combinations with/without repetition as test data.
E.g. you want to test whether your customized sorting algorithm works correct:
Then you want to check if permutations of your test data always lead to the same result.
So [1,4,3,5,7,6,9,2,0,8], [3,1,7,2,9,0,8,6,4,5], ... must all result in [0,1,2,3,4,5,6,7,8,9].
You might think now creating permutations is easy. If you, however, implement a generator, you might end up with this primitive brute force algorithm:
Unfortunately, this solution doesn't scale very well.
E.g. if you want to generate all permutations of all 10 digits from 0 to 9, you end up trying out 10^10=10,000,000,000 combinations although only 10!=3,628,800 permutations exist.
You can, of course, lookup on wikipedia solutions for better permutation generators but a more efficient solution can just look like this:
This solution only produces the possible permutations without brute force.
Therefore, writing generators for permutations, variations and combinations with/without repetition can be very tricky.
And they can be a very nice future feature for testers who use JUnit-Pioneer.
(Please ignore for the moment that the result is a list of all permutations. The permutations can also be provided by a stream on demand. And yes, support for primitive types like ints is possible as well. This code is only a draft.)
What do you think?
P.S.: Permutations would be the start and variations and combinations would follow as they all three are the most common cases of combinatorics.
The text was updated successfully, but these errors were encountered: