Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-递归问题_Php_Algorithm_Recursion - Fatal编程技术网

PHP-递归问题

PHP-递归问题,php,algorithm,recursion,Php,Algorithm,Recursion,可能重复: 我需要用大量可能的输入测试某个函数的行为。假设函数签名如下所示: foo ($a) foo ($a, $b, $c) 假设$a可以有以下值:1,2 假设$b可以有以下值:“hello”,“world” 假设$c可以有以下值:TRUE、FALSE 如何编写返回以下组合的函数: 1 2 1,hello,TRUE 1,hello,FALSE 2,hello,TRUE 2,hello,FALSE 1,world,TRUE 1,world,FALSE ... 请注意,函数参数的数量是未知

可能重复:

我需要用大量可能的输入测试某个函数的行为。假设函数签名如下所示:

foo ($a)
foo ($a, $b, $c)
假设$a可以有以下值:1,2

假设$b可以有以下值:“hello”,“world”

假设$c可以有以下值:TRUE、FALSE

如何编写返回以下组合的函数:

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

请注意,函数参数的数量是未知的,它们的可能值也是未知的。

这个问题似乎与递归无关

从您所写的内容来看,您似乎想要使用参数列表测试函数foo,这些参数列表是由每个参数的可能排列生成的? 下面的代码将生成该列表

//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
}

这就是您所追求的吗?

这与递归无关。搜索置换,如果您只需要可能的参数列表的组合。请注意,函数的参数数量未知,它们的可能值也是未知的。-不,您似乎明确列出了三个可能的参数,其中两个是可选的,所有参数都有两个可能的值。对我来说似乎不是很陌生。@glowcoder是的,通过func_num_args、func_get_arg或只是func_get_args。你说你有递归问题吗?你应该看看这个问题:我需要为很多函数做这个。参数的数量及其有效值都将不同。我可以指定参数及其值,但我不想用嵌套循环来处理每个函数。相反,我想要一个函数来处理任意数量的参数及其可能的值。那么您正在测试的每个函数调用的预期输出或副作用呢?对于看似如此的单元测试代码,您可能不得不承认它往往非常冗长。您可以设计一个运行测试的模式,根据该模式定义输入集和结果矩阵,并实现代码以运行所述模式。然而,随着您在测试工具中添加的每一层抽象,您都有引入bug的风险。。。如果测试线束断了,你怎么知道?