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.

12
  • 2
    This has nothing to do with recursion. Search for "permutations", if all you want is the combinations of your possible argument lists. Commented Sep 20, 2011 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. Commented Sep 20, 2011 at 23:12
  • stackoverflow.com/questions/2516599/… Commented Sep 20, 2011 at 23:19
  • 1
    @glowcoder Yes, through func_num_args(), func_get_arg() or simply func_get_args(). Commented Sep 20, 2011 at 23:33
  • 2
    You have a recursive problem, you say? You should look at this question : stackoverflow.com/questions/7492846/php-recursive-problem Commented Sep 20, 2011 at 23:47

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.