本文提供的方法是用于測(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(emptyempty($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;
- } //Vevb.com
- $TempStartPosition += $RepeatTime * count($ParamList);
- }
- $StartPosition += $RepeatTime;
- }
- }
- return $Result;
- }
- ?>
新聞熱點(diǎn)
疑難解答