>php教程 >php手册 >用于测试php函数的代码

用于测试php函数的代码

WBOY
WBOY원래의
2016-06-21 09:16:02946검색

函数


今天忽然想到的,就写了一段测试php函数的代码。

/**
 * 参数数组$ParamList说明
 *
 * 数组的第一维索引是需要测试的函数的参数名,第二维的每个元素是该参数需要测试的可能值,元素值可以为数组。
 */

$ParamList = array("Param1" => array(3,4,3,2,1),
                   "Param2" => array(3,2,5),
                   "Param3" => array(0,0.5,1,1.5));

// 测试函数
sysTestFunction("Test", $ParamList);


// 待测试的函数
function Test($Param1, $Param2, $Param3)
{
    return $Param1 . "|" . $Param2 . "|" . $Param3;
}


/**
 * 自动测试
 *
 * @author         李玉鹏
 * @param  string  $FunctionName  函数名称
 * @param  array   $ParamList     参数列表
 * @return array
 */
function sysTestFunction($FunctionName, $ParamList)
{
    if(empty($FunctionName))
    {
        echo "函数名不能为空";
        return false;
    }

    if(!is_array(current($ParamList)))
    {
        echo "参数不是2维数组";
        return false;
    }

    $TestParamList = sysCombineArray($ParamList);

    echo "开始测试函数" . $FunctionName . "
";
    foreach($TestParamList as $Key => $TestParamInfo)
    {
        echo "开始测试第" . $Key . "组参数:
";

        foreach($TestParamInfo as $ParamKey => $Param)
        {
            ${"Param" . $ParamKey} = $Param;
            $TempParamList[] = "$Param" . $ParamKey;
            if(is_array($Param))
            {
                echo "参数" . $ParamKey . ",类型为数组:";
                echo "

";<br>                print_r($Param);<br>            }<br>            elseif(is_bool($Param))<br>            {<br>                echo "参数" . $ParamKey . ",类型为boll:";<br>                if($Param)<br>                {<br>                    echo "true";<br>                }<br>                else<br>                {<br>                    echo "false";<br>                }<br>            }<br>            else<br>            {<br>                echo "参数" . $ParamKey . ",类型为字符串或数字:";<br>                echo $Param;<br>            }<br>            echo "<br>";<br>        }<p>        $Params = join(", ", $TempParamList);<br>        unset($TempParamList);</p><p>        eval("$TestReturnResult = " . $FunctionName . "(" . $Params . ");");<br>        if(is_array($TestReturnResult))<br>        {<br>            echo "函数返回数组:</p><pre class="brush:php;toolbar:false">";<br>            print_r($TestReturnResult);<br>        }<br>        elseif(is_bool($TestReturnResult))<br>        {<br>            if($TestReturnResult)<br>            {<br>                echo "函数返回true";<br>            }<br>            else<br>            {<br>                echo "函数返回false";<br>            }<br>        }<br>        else<br>        {<br>            echo "函数返回数字或字符串:" . $TestReturnResult;<br>        }<br>        echo "<br><br>";<br>    }<br>}<p>/**<br> * 计算组合的函数<br> *<br> * @author                   李玉鹏<leeyupeng><br> * @param  array $CombinList 待排列组合的2维数组<br> * @return array             组合后的数组<br> */<br>function sysCombineArray($CombinList)<br>{<br>    if(!is_array(current($CombinList)))<br>    {<br>        echo "参数不是2维数组";<br>        return false;<br>    }</leeyupeng></p><p>    /* 计算C(a,1) * C(b, 1) * ... * C(n, 1)的值 */<br>    $CombineCount = 1;<br>    foreach($CombinList as $Key => $Value)<br>    {<br>        $CombineCount *= count($Value);<br>    }</p><p>    $RepeatTime = $CombineCount;<br>    foreach($CombinList as $ClassNo => $ParamList)<br>    {<br>        // $ParamList中的元素在拆分成组合后纵向出现的最大重复次数<br>        $RepeatTime = $RepeatTime / count($ParamList);</p><p>        $StartPosition = 1;</p><p>        foreach($ParamList as $Param)<br>        {<br>            $TempStartPosition = $StartPosition;</p><p>            $SpaceCount = $CombineCount / count($ParamList) / $RepeatTime;</p><p>            for($J = 1; $J             {<br>                for($I = 0; $I                 {<br>                   $Result[$TempStartPosition + $I][$ClassNo] = $Param;<br>                }<br>                $TempStartPosition += $RepeatTime * count($ParamList);<br>            }<br>            $StartPosition += $RepeatTime;<br>        }<br>    }</p><p>    return $Result;<br>}<br>?></p>                   <center>                     </center>
            <p style="width:100%;text-align:center;margin:10px 0">

<br>
<br>

</p><p style="width:100%;text-align:center;margin:10px 0">

            </p><p class="clear"></p>

           
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:php的pconnect()다음 기사:做个站内搜索引擎