Code used to test php functions

Source: Internet
Author: User
Today, I suddenly thought of writing a piece of code to test the php function .? Php *** parameter array $ ParamList description ** the first-dimension index of the array is the parameter name of the function to be tested. each element of the second-dimension is the possible value to be tested for this parameter, element values can be arrays. * $ ParamListarray (Param1array (3, 4, 3, 2, 1), Param2 suddenly came to mind today and wrote a piece of code to test the php function.

/**
* Parameter array $ ParamList description
*
* The first-dimension index of the array is the parameter name of the function to be tested. each element in the second-dimension is the possible value to be tested for this parameter. 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 the function
SysTestFunction ("Test", $ ParamList );


// Function to be tested
Function Test ($ Param1, $ Param2, $ Param3)
{
Return $ Param1. "|". $ Param2. "|". $ Param3;
}


/**
* Automatic test
*
* @ Author Li Yupeng
* @ Param string $ FunctionName function name
* @ Param array $ ParamList parameter list
* @ Return array
*/
Function sysTestFunction ($ FunctionName, $ ParamList)
{
If (empty ($ FunctionName ))
{
Echo "function name cannot be blank ";
Return false;
}

If (! Is_array (current ($ ParamList )))
{
Echo "the parameter is not a two-dimensional array ";
Return false;
}

$ TestParamList = sysCombineArray ($ ParamList );

Echo "start test function". $ FunctionName ."
";
Foreach ($ TestParamList as $ Key => $ TestParamInfo)
{
Echo "start to test the". $ Key. "Group parameters:
";

Foreach ($ TestParamInfo as $ ParamKey => $ Param)
{
$ {"Param". $ ParamKey} = $ Param;
$ TempParamList [] = "$ Param". $ ParamKey;
If (is_array ($ Param ))
{
Echo "parameter". $ ParamKey. ", type: Array :";
Echo"

";
Print_r ($ Param );
}
Elseif (is_bool ($ Param ))
{
Echo "parameter". $ ParamKey. ", type: boll :";
If ($ Param)
{
Echo "true ";
}
Else
{
Echo "false ";
}
}
Else
{
Echo "parameter". $ ParamKey. ", Type: string or number :";
Echo $ Param;
}
Echo"
";
}

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

Eval ("$ TestReturnResult =". $ FunctionName. "(". $ Params .");");
If (is_array ($ TestReturnResult ))
{
Echo "function returns an array:

";
Print_r ($ TestReturnResult );
}
Elseif (is_bool ($ TestReturnResult ))
{
If ($ TestReturnResult)
{
Echo "the function returns true ";
}
Else
{
Echo "the function returns false ";
}
}
Else
{
Echo "function returns a number or string:". $ TestReturnResult;
}
Echo"

";
}
}

/**
* Compute the combined functions
*
* @ Author Li Yupeng
* @ Param array $ CombinList the two-dimensional array to be sorted and combined
* @ Return array
*/
Function sysCombineArray ($ CombinList)
{
If (! Is_array (current ($ CombinList )))
{
Echo "the parameter is not a two-dimensional array ";
Return false;
}

/* Calculate 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)
{
// $ Maximum number of times that an element in the ParamList appears vertically after being 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.