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

用于测试php函数的代码

WBOY
Freigeben: 2016-06-21 09:16:02
Original
945 Leute haben es durchsucht

函数


今天忽然想到的,就写了一段测试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>

           
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage