<p>I would like to test a function with a tuple from a set of fringe cases and normal values. For example, while testing a function which returns <code>true</code> whenever given three lengths that form a valid triangle, I would have specific cases, negative / small / large numbers, values close-to being overflowed, etc.; what is more, main aim is to generate combinations of these values, <em>with</em> or <em>without</em> repetition, in order to get a set of test data.</p>

<pre><code>(inf,0,-1), (5,10,1000), (10,5,5), (0,-1,5), (1000,inf,inf),
...
</code></pre>

<blockquote>
  <p><em>As a note: I actually know the answer to this, but it might be helpful for others, and a challenge for people here! --will post my answer later on.</em></p>
</blockquote>

[{'answer_id': 589, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 2, 'selected': False, 'text': "<p>Interesting question!</p>\n\n<p>I would do this by picking combinations, something like the following in python.  The hardest part is probably first pass verification, i.e. <code>if f(1,2,3) returns true</code>, is that a correct result?  Once you have verified that, then this is a good basis for regression testing.</p>\n\n<p>Probably it's a good idea to make a set of test cases that you know will be all true (e.g. 3,4,5 for this triangle case), and a set of test cases that you know will be all false (e.g. 0,1,inf).  Then you can more easily verify the tests are correct.</p>\n\n<pre>\n# xpermutations from http://code.activestate.com/recipes/190465\nfrom xpermutations import *\n\nlengths=[-1,0,1,5,10,0,1000,'inf']\nfor c in xselections(lengths,3):        # or xuniqueselections\n    print c\n</pre>\n\n<pre>\n(-1,-1,-1);\n(-1,-1,0);\n(-1,-1,1);\n(-1,-1,5);\n(-1,-1,10);\n(-1,-1,0);\n(-1,-1,1000);\n(-1,-1,inf);\n(-1,0,-1);\n(-1,0,0);\n...\n</pre>\n"}, {'answer_id': 794, 'author': 'nlucaroni', 'author_id': 157, 'author_profile': 'https://Stackoverflow.com/users/157', 'pm_score': 5, 'selected': True, 'text': '<p>Absolutely, especially dealing with lots of these permutations/combinations I can definitely see that the first pass would be an issue.</p>\n\n<p>Interesting implementation in python, though I wrote a nice one in C and Ocaml based on "Algorithm 515" (see below). He wrote his in Fortran as it was common back then for all the "Algorithm XX" papers, well, that assembly or c. I had to re-write it and make some small improvements to work with arrays not ranges of numbers. This one does random access, I\'m still working on getting some nice implementations of the ones mentioned in Knuth 4th volume fascicle 2. I\'ll an explanation of how this works to the reader. Though if someone is curious, I wouldn\'t object to writing something up.</p>\n\n<pre><code>/** [combination c n p x]\n * get the [x]th lexicographically ordered set of [p] elements in [n]\n * output is in [c], and should be sizeof(int)*[p] */\nvoid combination(int* c,int n,int p, int x){\n    int i,r,k = 0;\n    for(i=0;i&lt;p-1;i++){\n        c[i] = (i != 0) ? c[i-1] : 0;\n        do {\n            c[i]++;\n            r = choose(n-c[i],p-(i+1));\n            k = k + r;\n        } while(k &lt; x);\n        k = k - r;\n    }\n    c[p-1] = c[p-2] + x - k;\n}\n</code></pre>\n\n<p>~"Algorithm 515: Generation of a Vector from the Lexicographical Index"; Buckles, B. P., and Lybanon, M. ACM Transactions on Mathematical Software, Vol. 3, No. 2, June 1977.</p>\n'}, {'answer_id': 13123, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': '<p>I think you can do this with the <a href="http://blog.donnfelker.com/2008/04/01/NUnit247AndTheRowTestAttributeWithExample.aspx" rel="nofollow noreferrer">Row Test Attribute</a> (available in MbUnit and later versions of NUnit) where you could specify several sets to populate one unit test.</p>\n'}, {'answer_id': 79583, 'author': 'not-bob', 'author_id': 14770, 'author_profile': 'https://Stackoverflow.com/users/14770', 'pm_score': 0, 'selected': False, 'text': "<p>While it's possible to create lots of test data and see what happens, it's more efficient to try to minimize the data being used.</p>\n\n<p>From a typical QA perspective, you would want to identify different classifications of inputs. Produce a set of input values for each classification and determine the appropriate outputs. </p>\n\n<p>Here's a sample of classes of input values</p>\n\n<ul>\n<li>valid triangles with small numbers such as (1 billion, 2, billion, 2 billion)</li>\n<li>valid triangles with large numbers such as (0.000001, 0.00002, 0.00003)</li>\n<li>valid obtuse triangles that are 'almost'flat such as (10, 10, 19.9999)</li>\n<li>valid acute triangles that are 'almost' flat such as (10, 10, 0000001)</li>\n<li>invalid triangles with at least one negative value</li>\n<li>invalid triangles where the sum of two sides equals the third</li>\n<li>invalid triangles where the sum of two sides is greater than the third</li>\n<li>input values that are non-numeric</li>\n</ul>\n\n<p>...</p>\n\n<p>Once you are satisfied with the list of input classifications for this function, then you can create the actual test data.  Likely, it would be helpful to test all permutations of each item.  (e.g. (2,3,4), (2,4,3), (3,2,4), (3,4,2), (4,2,3), (4,3,2)) Typically, you'll find there are some classifications you missed (such as the concept of inf as an input parameter).</p>\n\n<p>Random data for some period of time may be helpful as well, that can find strange bugs in the code, but is generally not productive.</p>\n\n<p>More likely, this function is being used in some specific context where additional rules are applied.(e.g. only integer values or values must be in 0.01 increments, etc.) These add to the list of classifications of input parameters.</p>\n"}, {'answer_id': 170010, 'author': 'e-satis', 'author_id': 9951, 'author_profile': 'https://Stackoverflow.com/users/9951', 'pm_score': 2, 'selected': False, 'text': '<p>With the brand new Python 2.6, you have a standard solution with the itertools module that returns the Cartesian product of iterables :</p>\n\n<pre><code>import itertools\n\nprint list(itertools.product([1,2,3], [4,5,6]))\n   [(1, 4), (1, 5), (1, 6),\n   (2, 4), (2, 5), (2, 6),\n   (3, 4), (3, 5), (3, 6)]\n</code></pre>\n\n<p>You can provide a "repeat" argument to perform the product with an iterable and itself:</p>\n\n<pre><code>print list(itertools.product([1,2], repeat=3))\n[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),\n(2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]\n</code></pre>\n\n<p>You can also tweak something with combinations as well :</p>\n\n<pre><code>print list(itertools.combinations(\'123\', 2))\n[(\'1\', \'2\'), (\'1\', \'3\'), (\'2\', \'3\')]\n</code></pre>\n\n<p>And if order matters, there are permutations :</p>\n\n<pre><code>print list(itertools.permutations([1,2,3,4], 2))\n[(1, 2), (1, 3), (1, 4),\n   (2, 1), (2, 3), (2, 4),\n   (3, 1), (3, 2), (3, 4),\n   (4, 1), (4, 2), (4, 3)]\n</code></pre>\n\n<p>Of course all that cool stuff don\'t exactly do the same thing, but you can use them in a way or another to solve you problem. </p>\n\n<p>Just remember that you can convert a tuple or a list to a set and vice versa using list(), tuple() and set().</p>\n'}]
['https://Stackoverflow.com/questions/561', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/157/']