Code to test the PHP function

Source: Internet
Author: User
Tags foreach array count functions key return string
Function

Today suddenly thought of, wrote a test PHP function code.

<?php

/**
* Parameter Array $paramlist description
*
* The first-dimensional index of the array is the parameter name of the function that needs to be tested, and each element of the second dimension is the possible value that the parameter needs to be tested, and the element value can be an array.
*/

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

Test function
Systestfunction ("Test", $ParamList);


Functions to be tested
function Test ($Param 1, $Param 2, $Param 3)
{
Return $Param 1. "|" . $Param 2. "|" . $Param 3;
}


/**
* Automatic Testing
*
* @author confined <leeyupeng@163.com>
* @param string $FunctionName function name
* @param array $ParamList parameter list
* @return Array
*/
function Systestfunction ($FunctionName, $ParamList)
{
if (empty ($FunctionName))
{
echo "function name cannot be null";
return false;
}

if (!is_array ($ParamList))
{
echo "parameter is not a 2-D array";
return false;
}

$TestParamList = Syscombinearray ($ParamList);

echo "Start test function." $FunctionName. "<br/>";
foreach ($TestParamList as $Key => $TestParamInfo)
{
echo "Start test First". $Key. "Group parameters: <br/>";

foreach ($TestParamInfo as $ParamKey => $Param)
{
${"Param". $ParamKey} = $Param;
$TempParamList [] = "$Param". $ParamKey;
if (Is_array ($Param))
{
echo "Parameters". $ParamKey. ", the type is an array:";
echo "<pre>";
Print_r ($Param);
}
ElseIf (Is_bool ($Param))
{
echo "Parameters". $ParamKey. ", the type is boll:";
if ($Param)
{
echo "true";
}
Else
{
echo "false";
}
}
Else
{
echo "Parameters". $ParamKey. ", type is a string or number:";
Echo $Param;
}
echo "<br/>";
}

$Params = Join (",", $TempParamList);
Unset ($TempParamList);

Eval ("$TestReturnResult =". $FunctionName. "(" . $Params. ");");
if (Is_array ($TestReturnResult))
{
echo "function returns array:<pre>";
Print_r ($TestReturnResult);
}
ElseIf (Is_bool ($TestReturnResult))
{
if ($TestReturnResult)
{
echo "function returns True";
}
Else
{
echo "function returns false";
}
}
Else
{
echo function returns a number or string: ". $TestReturnResult;
}
echo "<br/><br/>";
}
}

/**
* Compute a combination of functions
*
* @author confined <leeyupeng@163.com>
* @param array $CombinList 2-D arrays to be arranged
* The array after the combination of @return array
*/
function Syscombinearray ($CombinList)
{
if (!is_array ($CombinList))
{
echo "parameter is not a 2-D array";
return false;
}

/* Compute the value of 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)
{
The maximum number of repetitions of an element in a $ParamList that appears vertically after it is split into a combination
$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;
}
?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.