|
今天忽然想到的,就寫了一段測試php函數(shù)的代碼。
復(fù)制代碼 代碼如下:
<?php
/**
* 參數(shù)數(shù)組$ParamList說明
*
* 數(shù)組的第一維索引是需要測試的函數(shù)的參數(shù)名,第二維的每個(gè)元素是該參數(shù)需要測試的可能值,元素值可以為數(shù)組。
*/
$ParamList = array("Param1" => array(3,4,3,2,1),
"Param2" => array(3,2,5),
"Param3" => array(0,0.5,1,1.5));
// 測試函數(shù)
sysTestFunction("Test", $ParamList);
// 待測試的函數(shù)
function Test($Param1, $Param2, $Param3)
{
return $Param1 . "|" . $Param2 . "|" . $Param3;
}
/**
* 自動(dòng)測試
*
* @param string $FunctionName 函數(shù)名稱
* @param array $ParamList 參數(shù)列表
* @return array
*/
function sysTestFunction($FunctionName, $ParamList)
{
if(empty($FunctionName))
{
echo "函數(shù)名不能為空";
return false;
}
if(!is_array(current($ParamList)))
{
echo "參數(shù)不是2維數(shù)組";
return false;
}
$TestParamList = sysCombineArray($ParamList);
echo "開始測試函數(shù)" . $FunctionName . "<br />";
foreach($TestParamList as $Key => $TestParamInfo)
{
echo "開始測試第" . $Key . "組參數(shù):<br />";
foreach($TestParamInfo as $ParamKey => $Param)
{
${"Param" . $ParamKey} = $Param;
$TempParamList[] = "$Param" . $ParamKey;
if(is_array($Param))
{
echo "參數(shù)" . $ParamKey . ",類型為數(shù)組:";
echo "<pre>";
print_r($Param);
}
elseif(is_bool($Param))
{
echo "參數(shù)" . $ParamKey . ",類型為boll:";
if($Param)
{
echo "true";
}
else
{
echo "false";
}
}
else
{
echo "參數(shù)" . $ParamKey . ",類型為字符串或數(shù)字:";
echo $Param;
}
echo "<br />";
}
$Params = join(", ", $TempParamList);
unset($TempParamList);
eval("$TestReturnResult = " . $FunctionName . "(" . $Params . ");");
if(is_array($TestReturnResult))
{
echo "函數(shù)返回?cái)?shù)組:<pre>";
print_r($TestReturnResult);
}
elseif(is_bool($TestReturnResult))
{
if($TestReturnResult)
{
echo "函數(shù)返回true";
}
else
{
echo "函數(shù)返回false";
}
}
else
{
echo "函數(shù)返回?cái)?shù)字或字符串:" . $TestReturnResult;
}
echo "<br /><br />";
}
}
/**
* 計(jì)算組合的函數(shù)
*
* @param array $CombinList 待排列組合的2維數(shù)組
* @return array 組合后的數(shù)組
*/
function sysCombineArray($CombinList)
{
if(!is_array(current($CombinList)))
{
echo "參數(shù)不是2維數(shù)組";
return false;
}
/* 計(jì)算C(a,1) * C(b, 1) * ... * C(n, 1)的值 */
$CombineCount = 1;
foreach($CombinList as $Key => $Value)
{
$CombineCount *= count($Value);
}
$RepeatTime = $CombineCount;
foreach($CombinList as $ClassNo => $ParamList)
{
// $ParamList中的元素在拆分成組合后縱向出現(xiàn)的最大重復(fù)次數(shù)
$RepeatTime = $RepeatTime / count($ParamList);
$StartPosition = 1;
foreach($ParamList as $Param)
{
$TempStartPosition = $StartPosition;
$SpaceCount = $CombineCount / count($ParamList) / $RepeatTime;
for($J = 1; $J <= $SpaceCount; $J ++)
{
for($I = 0; $I < $RepeatTime; $I ++)
{
$Result[$TempStartPosition + $I][$ClassNo] = $Param;
}
$TempStartPosition += $RepeatTime * count($ParamList);
}
$StartPosition += $RepeatTime;
}
}
return $Result;
}
?>
php技術(shù):測試php函數(shù)的方法,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。