免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1290 | 回复: 0
打印 上一主题 下一主题

用于测试php函数的代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-07-16 20:09 |只看该作者 |倒序浏览

今天忽然想到的,就写了一段测试php函数的代码。
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 "";
                print_r($Param);
            }
            elseif(is_bool($Param))
            {
                echo "参数" . $ParamKey . ",类型为boll:";
                if($Param)
                {
                    echo "true";
                }
                else
                {
                    echo "false";
                }
            }
            else
            {
                echo "参数" . $ParamKey . ",类型为字符串或数字:";
                echo $Param;
            }
            echo "
";
        }
        $Params = join(", ", $TempParamList);
        unset($TempParamList);
        eval("$TestReturnResult = " . $FunctionName . "(" . $Params . ");");
        if(is_array($TestReturnResult))
        {
            echo "函数返回数组:";
            print_r($TestReturnResult);
        }
        elseif(is_bool($TestReturnResult))
        {
            if($TestReturnResult)
            {
                echo "函数返回true";
            }
            else
            {
                echo "函数返回false";
            }
        }
        else
        {
            echo "函数返回数字或字符串:" . $TestReturnResult;
        }
        echo "
";
    }
}
/**
* 计算组合的函数
*
* @author                   李玉鹏
* @param  array $CombinList 待排列组合的2维数组
* @return array             组合后的数组
*/
function sysCombineArray($CombinList)
{
    if(!is_array(current($CombinList)))
    {
        echo "参数不是2维数组";
        return false;
    }
    /* 计算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中的元素在拆分成组合后纵向出现的最大重复次数
        $RepeatTime = $RepeatTime / count($ParamList);
        $StartPosition = 1;
        foreach($ParamList as $Param)
        {
            $TempStartPosition = $StartPosition;
            $SpaceCount = $CombineCount / count($ParamList) / $RepeatTime;
            for($J = 1; $J


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/1777/showart_35977.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP