測(cè)試php函數(shù)的方法
今天忽然想到的,就寫了一段測(cè)試php函數(shù)的代碼。
<?php
/**
* 參數(shù)數(shù)組$ParamList說明
*
* 數(shù)組的第一維索引是需要測(cè)試的函數(shù)的參數(shù)名,第二維的每個(gè)元素是該參數(shù)需要測(cè)試的可能值,元素值可以為數(shù)組。
*/
$ParamList = array("Param1" => array(3,4,3,2,1),
"Param2" => array(3,2,5),
"Param3" => array(0,0.5,1,1.5));
// 測(cè)試函數(shù)
sysTestFunction("Test", $ParamList);
// 待測(cè)試的函數(shù)
function Test($Param1, $Param2, $Param3)
{
return $Param1 . "|" . $Param2 . "|" . $Param3;
}
/**
* 自動(dòng)測(cè)試
*
* @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 "開始測(cè)試函數(shù)" . $FunctionName . "<br />";
foreach($TestParamList as $Key => $TestParamInfo)
{
echo "開始測(cè)試第" . $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;
}
?>
相關(guān)文章
Thinkphp5.0 框架視圖view的比較標(biāo)簽用法分析
這篇文章主要介紹了Thinkphp5.0 框架視圖view的比較標(biāo)簽用法,結(jié)合實(shí)例形式分析了thinkPHP5框架eq、equal、neq、notequal、egt及switch、range、between等標(biāo)簽相關(guān)用法,需要的朋友可以參考下2019-10-10WordPress中對(duì)訪客評(píng)論功能的一些優(yōu)化方法
這篇文章主要介紹了WordPress中對(duì)訪客評(píng)論功能的一些優(yōu)化,包括顯示評(píng)論上的歡迎信息等功能,需要的朋友可以參考下2015-11-11使用PHPStudy搭建Cloudreve網(wǎng)盤服務(wù)的流程步驟
自云存儲(chǔ)概念興起已經(jīng)有段時(shí)間了,各互聯(lián)網(wǎng)大廠也紛紛加入戰(zhàn)局,一時(shí)間公有云盤遍地開花,今天我們就為大家介紹,如何使用Cpolar與Cloudreve,在個(gè)人Windows電腦上搭建一個(gè)強(qiáng)大的PHP云盤系統(tǒng),需要的朋友可以參考下2024-02-02laravel 5.1下php artisan migrate的使用注意事項(xiàng)總結(jié)
這篇文章主要給大家總結(jié)介紹了在laravel 5.1下php artisan migrate的使用注意事項(xiàng),文中介紹的非常詳細(xì),對(duì)大家使用php artisan migrate具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06