0

Possible Duplicate:
PHP 2D Array output all combinations

I need to test the behavior of a certain function with a significant number of possible inputs. Say the function signatures are as follows:

foo ($a)
foo ($a, $b, $c)

Say $a can have the following values: 1, 2.

Say $b can have the following values: 'hello', 'world'.

Say $c can have the following values: TRUE, FALSE

How to write a function that returns the following combinations:

1
2
1,hello,TRUE
1,hello,FALSE
2,hello,TRUE
2,hello,FALSE
1,world,TRUE
1,world,FALSE
...

Note that the number of function's parameters are unknown and so are their possible values.

Community
  • 1
  • 1
StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441
  • 2
    This has nothing to do with recursion. Search for "permutations", if all you want is the combinations of your possible argument lists. – mario Sep 20 '11 at 23:10
  • 2
    *"Note that the number of function's parameters are unknown and so are their possible values."* - No, you seem to have explicitly listed three possible parameters, two of which are optional, all of which have two possible values. Doesn't seem very unknown to me. – animuson Sep 20 '11 at 23:12
  • http://stackoverflow.com/questions/2516599/php-2d-array-output-all-combinations – user187291 Sep 20 '11 at 23:19
  • hakre: my question, as stated in the original post: "How to write a function that returns the following combinations:" – StackOverflowNewbie Sep 20 '11 at 23:22
  • mario: isn't this a "combinations" problem? In any case, I think the problem is solved by recursion. No? – StackOverflowNewbie Sep 20 '11 at 23:23
  • animuson: the function `foo` and the values for `$a`, `$b`, and `$c` are for illustration purposes only. – StackOverflowNewbie Sep 20 '11 at 23:24
  • Does php have something analogous to Java's varargs? – corsiKa Sep 20 '11 at 23:30
  • @glowcoder: it does, and this is a duplicate, the original solves this with varargs. – Karoly Horvath Sep 20 '11 at 23:32
  • 1
    @glowcoder Yes, through `func_num_args()`, `func_get_arg()` or simply `func_get_args()`. – Zecc Sep 20 '11 at 23:33
  • If we don't know the possible values for any of the function's parameters, then each argument could either take on any of the possible values for a floating point number (which, technically, is system-dependent) or any possible string up to the largest length of a string allowable in memory (which is very much system-dependent, and probably dependent on what else is stored in memory at any particular time). So, while some version of this might theoretically be possible, it would be a very, very bad idea. –  Sep 20 '11 at 23:37
  • 2
    You have a recursive problem, you say? You should look at this question : http://stackoverflow.com/questions/7492846/php-recursive-problem – nickf Sep 20 '11 at 23:47
  • I can't tell who's trolling who. – Incognito Sep 21 '11 at 00:11

1 Answers1

0

This question doesn't seem to have anything to do with recursion.

From what you've written, it seems that you want to test the function foo() with that list of arguments, generated from the possible permutations of each? The following code will generate that list.

//Generate the list
$arg_list = array();
foreach(array(1,2) as $a) //Build the foo($a) cases
   $arg_list[] = array($a); 
foreach(array(1,2) as $a) //Build the foo($a, $b, $c) cases
   foreach(array('hello','world') as $b)
       foreach(array(true,false) as $c)
           $arg_list[] = array($a,$b,$c);

//Test each possible case
foreach($arg_list as $args) {
   ...
   $result = call_user_func_array('foo', $args);
   ...
   //Is result what was expected? Check and aggregate
}

Is this the sort of thing you're after?

MrTrick
  • 1,927
  • 12
  • 11
  • I need to do this for a lot of functions. The number of parameters and their valid values will all differ. I can specify the the parameters and their values, but I don't want to have nested loops to handle each function. Instead, I want one function to handle any possible number of parameters and their possible values. – StackOverflowNewbie Sep 21 '11 at 12:29
  • What about the expected output or side-effects for each function call that you're testing? For unit testing code (which this seems to be), you may have to accept that it tends to be quite verbose. – MrTrick Sep 21 '11 at 13:10
  • You *could* design a schema for running tests, define the set of inputs and the result matrix according to that schema, and implement code to run said schema. However, with every layer of abstraction you add in the test harness, you risk introducing bugs... and if the test harness is broken, how would you know? – MrTrick Sep 21 '11 at 13:14