parent
72b7ebdfae
commit
f072f944b2
8 changed files with 493 additions and 16 deletions
@ -0,0 +1,93 @@ |
|||||||
|
--TEST-- |
||||||
|
User-defined and Built-in Functions |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
// Test user-defined function |
||||||
|
function add($a, $b) { |
||||||
|
return $a + $b; |
||||||
|
} |
||||||
|
|
||||||
|
function multiply($x, $y) { |
||||||
|
return $x * $y; |
||||||
|
} |
||||||
|
|
||||||
|
function greet($name) { |
||||||
|
return "Hello, " . $name . "!"; |
||||||
|
} |
||||||
|
|
||||||
|
function main() { |
||||||
|
// Test function calls |
||||||
|
$result1 = add(5, 3); |
||||||
|
var_dump($result1); |
||||||
|
|
||||||
|
$result2 = multiply(4, 7); |
||||||
|
var_dump($result2); |
||||||
|
|
||||||
|
$result3 = greet("World"); |
||||||
|
var_dump($result3); |
||||||
|
|
||||||
|
// Test built-in functions |
||||||
|
$test_string = " Hello World "; |
||||||
|
$trimmed = trim($test_string); |
||||||
|
var_dump($trimmed); |
||||||
|
|
||||||
|
$upper = strtoupper($test_string); |
||||||
|
var_dump($upper); |
||||||
|
|
||||||
|
$len = strlen($trimmed); |
||||||
|
var_dump($len); |
||||||
|
|
||||||
|
// Test array functions |
||||||
|
$numbers = [1, 2, 3, 4, 5]; |
||||||
|
$reversed = array_reverse($numbers); |
||||||
|
var_dump($reversed); |
||||||
|
|
||||||
|
$count = count($numbers); |
||||||
|
var_dump($count); |
||||||
|
$fact5 = factorial(5); |
||||||
|
var_dump($fact5); |
||||||
|
|
||||||
|
$greeting1 = greet_with_title("Smith"); |
||||||
|
var_dump($greeting1); |
||||||
|
|
||||||
|
$greeting2 = greet_with_title("Jane", "Ms."); |
||||||
|
var_dump($greeting2); |
||||||
|
} |
||||||
|
|
||||||
|
// Test recursive function |
||||||
|
function factorial($n) { |
||||||
|
if ($n <= 1) { |
||||||
|
return 1; |
||||||
|
} |
||||||
|
return $n * factorial($n - 1); |
||||||
|
} |
||||||
|
|
||||||
|
// Test function with default parameters |
||||||
|
function greet_with_title($name, $title = "Mr.") { |
||||||
|
return $title . " " . $name; |
||||||
|
} |
||||||
|
|
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
int(8) |
||||||
|
int(28) |
||||||
|
string(13) "Hello, World!" |
||||||
|
string(11) "Hello World" |
||||||
|
string(15) " HELLO WORLD " |
||||||
|
int(11) |
||||||
|
array(5) { |
||||||
|
[0]=> |
||||||
|
int(5) |
||||||
|
[1]=> |
||||||
|
int(4) |
||||||
|
[2]=> |
||||||
|
int(3) |
||||||
|
[3]=> |
||||||
|
int(2) |
||||||
|
[4]=> |
||||||
|
int(1) |
||||||
|
} |
||||||
|
int(5) |
||||||
|
int(120) |
||||||
|
string(9) "Mr. Smith" |
||||||
|
string(8) "Ms. Jane" |
||||||
@ -0,0 +1,140 @@ |
|||||||
|
--TEST-- |
||||||
|
Mathematical Functions |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
// Test basic math functions |
||||||
|
$abs = abs(-5); |
||||||
|
var_dump($abs); |
||||||
|
|
||||||
|
$abs_float = abs(-3.7); |
||||||
|
var_dump($abs_float); |
||||||
|
|
||||||
|
// Test ceil and floor |
||||||
|
$ceil = ceil(4.2); |
||||||
|
var_dump($ceil); |
||||||
|
|
||||||
|
$floor = floor(4.8); |
||||||
|
var_dump($floor); |
||||||
|
|
||||||
|
$round = round(4.567, 2); |
||||||
|
var_dump($round); |
||||||
|
|
||||||
|
// Test min and max |
||||||
|
$min = min(3, 7, 1, 9, 4); |
||||||
|
var_dump($min); |
||||||
|
|
||||||
|
$max = max(3, 7, 1, 9, 4); |
||||||
|
var_dump($max); |
||||||
|
|
||||||
|
// Test with arrays |
||||||
|
$numbers = [5, 2, 8, 1, 9]; |
||||||
|
$min_arr = min($numbers); |
||||||
|
var_dump($min_arr); |
||||||
|
|
||||||
|
$max_arr = max($numbers); |
||||||
|
var_dump($max_arr); |
||||||
|
|
||||||
|
// Test pow function |
||||||
|
$power = pow(2, 8); |
||||||
|
var_dump($power); |
||||||
|
|
||||||
|
// Test sqrt |
||||||
|
$sqrt = sqrt(64); |
||||||
|
var_dump($sqrt); |
||||||
|
|
||||||
|
$sqrt_float = sqrt(2); |
||||||
|
var_dump($sqrt_float); |
||||||
|
|
||||||
|
// Test rand and mt_rand (within a range for predictability) |
||||||
|
srand(12345); // For predictable results in tests |
||||||
|
$rand1 = rand(1, 100); |
||||||
|
var_dump($rand1); |
||||||
|
|
||||||
|
mt_srand(12345); // For predictable results in tests |
||||||
|
$rand2 = mt_rand(1, 100); |
||||||
|
var_dump($rand2); |
||||||
|
|
||||||
|
// Test number format |
||||||
|
$number = 1234.5678; |
||||||
|
$formatted = number_format($number, 2); |
||||||
|
var_dump($formatted); |
||||||
|
|
||||||
|
// Test fmod |
||||||
|
$fmod = fmod(10.5, 3.2); |
||||||
|
var_dump($fmod); |
||||||
|
|
||||||
|
// Test trigonometric functions |
||||||
|
$sin = sin(pi()/2); // Should be 1 |
||||||
|
var_dump($sin); |
||||||
|
|
||||||
|
$cos = cos(0); // Should be 1 |
||||||
|
var_dump($cos); |
||||||
|
|
||||||
|
$tan = tan(pi()/4); // Should be close to 1 |
||||||
|
var_dump($tan); |
||||||
|
|
||||||
|
// Test logarithmic functions |
||||||
|
$log = log(M_E); // Natural log of e should be 1 |
||||||
|
var_dump($log); |
||||||
|
|
||||||
|
$log10 = log10(100); // Base-10 log of 100 should be 2 |
||||||
|
var_dump($log10); |
||||||
|
|
||||||
|
// Test exponential |
||||||
|
$exp = exp(1); // e^1 should be e |
||||||
|
var_dump($exp); |
||||||
|
|
||||||
|
// Test is_* functions |
||||||
|
$int_val = 42; |
||||||
|
$float_val = 3.14; |
||||||
|
$str_val = "hello"; |
||||||
|
|
||||||
|
var_dump(is_int($int_val)); |
||||||
|
var_dump(is_float($float_val)); |
||||||
|
var_dump(is_numeric("123")); |
||||||
|
var_dump(is_numeric("abc")); |
||||||
|
|
||||||
|
// Test conversion functions |
||||||
|
$hex = dechex(255); |
||||||
|
var_dump($hex); |
||||||
|
|
||||||
|
$bin = decbin(8); |
||||||
|
var_dump($bin); |
||||||
|
|
||||||
|
$oct = decoct(8); |
||||||
|
var_dump($oct); |
||||||
|
|
||||||
|
$back_to_dec = hexdec($hex); |
||||||
|
var_dump($back_to_dec); |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
int(5) |
||||||
|
float(3.7) |
||||||
|
float(5) |
||||||
|
float(4) |
||||||
|
float(4.57) |
||||||
|
int(1) |
||||||
|
int(9) |
||||||
|
int(1) |
||||||
|
int(9) |
||||||
|
int(256) |
||||||
|
float(8) |
||||||
|
float(1.4142135623730951) |
||||||
|
int(91) |
||||||
|
int(91) |
||||||
|
string(8) "1,234.57" |
||||||
|
float(0.8999999999999995) |
||||||
|
float(1) |
||||||
|
float(1) |
||||||
|
float(0.9999999999999999) |
||||||
|
float(1) |
||||||
|
float(2) |
||||||
|
float(2.718281828459045) |
||||||
|
bool(true) |
||||||
|
bool(true) |
||||||
|
bool(true) |
||||||
|
bool(false) |
||||||
|
string(2) "ff" |
||||||
|
string(4) "1000" |
||||||
|
string(2) "10" |
||||||
|
int(255) |
||||||
@ -0,0 +1,156 @@ |
|||||||
|
--TEST-- |
||||||
|
String Functions |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
// Test basic string functions |
||||||
|
$str = "Hello, World!"; |
||||||
|
|
||||||
|
$len = strlen($str); |
||||||
|
var_dump($len); |
||||||
|
|
||||||
|
$upper = strtoupper($str); |
||||||
|
var_dump($upper); |
||||||
|
|
||||||
|
$lower = strtolower($str); |
||||||
|
var_dump($lower); |
||||||
|
|
||||||
|
// Test substr |
||||||
|
$substr = substr($str, 0, 5); |
||||||
|
var_dump($substr); |
||||||
|
|
||||||
|
$substr2 = substr($str, 7); |
||||||
|
var_dump($substr2); |
||||||
|
|
||||||
|
// Test strpos |
||||||
|
$pos = strpos($str, "World"); |
||||||
|
var_dump($pos); |
||||||
|
|
||||||
|
// Test str_replace |
||||||
|
$replaced = str_replace("World", "PHP", $str); |
||||||
|
var_dump($replaced); |
||||||
|
|
||||||
|
// Test trim functions |
||||||
|
$spaced = " Hello, World! "; |
||||||
|
$trimmed = trim($spaced); |
||||||
|
var_dump($trimmed); |
||||||
|
|
||||||
|
$ltrimmed = ltrim($spaced); |
||||||
|
var_dump($ltrimmed); |
||||||
|
|
||||||
|
$rtrimmed = rtrim($spaced); |
||||||
|
var_dump($rtrimmed); |
||||||
|
|
||||||
|
// Test string concatenation |
||||||
|
$part1 = "Hello"; |
||||||
|
$part2 = "World"; |
||||||
|
$concatenated = $part1 . " " . $part2 . "!"; |
||||||
|
var_dump($concatenated); |
||||||
|
|
||||||
|
// Test explode and implode |
||||||
|
$csv = "apple,banana,orange"; |
||||||
|
$fruits = explode(",", $csv); |
||||||
|
var_dump($fruits); |
||||||
|
|
||||||
|
$joined = implode("-", $fruits); |
||||||
|
var_dump($joined); |
||||||
|
|
||||||
|
// Test comparison functions |
||||||
|
$cmp1 = strcmp("abc", "abc"); |
||||||
|
var_dump($cmp1); |
||||||
|
|
||||||
|
$cmp2 = strcmp("abc", "def"); |
||||||
|
var_dump($cmp2); |
||||||
|
|
||||||
|
$cmp3 = strcasecmp("ABC", "abc"); |
||||||
|
var_dump($cmp3); |
||||||
|
|
||||||
|
// Test string formatting |
||||||
|
$formatted = sprintf("Value: %d, Text: %s", 42, "hello"); |
||||||
|
var_dump($formatted); |
||||||
|
|
||||||
|
// Test chunk split |
||||||
|
$chunked = str_split("abcdefghij", 3); |
||||||
|
var_dump($chunked); |
||||||
|
|
||||||
|
// Test strrev |
||||||
|
$reversed = strrev("hello"); |
||||||
|
var_dump($reversed); |
||||||
|
|
||||||
|
// Test word functions |
||||||
|
$word_count = str_word_count("Hello world!"); |
||||||
|
var_dump($word_count); |
||||||
|
|
||||||
|
// Test ucfirst and ucwords |
||||||
|
$mixed_case = "hello world from PHP"; |
||||||
|
$first_upper = ucfirst($mixed_case); |
||||||
|
var_dump($first_upper); |
||||||
|
|
||||||
|
$title_case = ucwords($mixed_case); |
||||||
|
var_dump($title_case); |
||||||
|
|
||||||
|
// Test number conversion from string |
||||||
|
$num_str = "123"; |
||||||
|
$num = (int)$num_str; |
||||||
|
var_dump($num); |
||||||
|
|
||||||
|
$float_str = "45.67"; |
||||||
|
$float = (float)$float_str; |
||||||
|
var_dump($float); |
||||||
|
|
||||||
|
// Test string padding |
||||||
|
$padded = str_pad("Hello", 10, "0", STR_PAD_LEFT); |
||||||
|
var_dump($padded); |
||||||
|
|
||||||
|
// Test string repeat |
||||||
|
$repeated = str_repeat("xy", 4); |
||||||
|
var_dump($repeated); |
||||||
|
|
||||||
|
// Test strip_tags |
||||||
|
$html = "<p>Hello <b>World</b>!</p>"; |
||||||
|
$stripped = strip_tags($html); |
||||||
|
var_dump($stripped); |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
int(13) |
||||||
|
string(13) "HELLO, WORLD!" |
||||||
|
string(13) "hello, world!" |
||||||
|
string(5) "Hello" |
||||||
|
string(6) "World!" |
||||||
|
int(7) |
||||||
|
string(11) "Hello, PHP!" |
||||||
|
string(13) "Hello, World!" |
||||||
|
string(15) "Hello, World! " |
||||||
|
string(15) " Hello, World!" |
||||||
|
string(12) "Hello World!" |
||||||
|
array(3) { |
||||||
|
[0]=> |
||||||
|
string(5) "apple" |
||||||
|
[1]=> |
||||||
|
string(6) "banana" |
||||||
|
[2]=> |
||||||
|
string(6) "orange" |
||||||
|
} |
||||||
|
string(19) "apple-banana-orange" |
||||||
|
int(0) |
||||||
|
int(-3) |
||||||
|
int(0) |
||||||
|
string(22) "Value: 42, Text: hello" |
||||||
|
array(4) { |
||||||
|
[0]=> |
||||||
|
string(3) "abc" |
||||||
|
[1]=> |
||||||
|
string(3) "def" |
||||||
|
[2]=> |
||||||
|
string(3) "ghi" |
||||||
|
[3]=> |
||||||
|
string(1) "j" |
||||||
|
} |
||||||
|
string(5) "olleh" |
||||||
|
int(2) |
||||||
|
string(20) "Hello world from PHP" |
||||||
|
string(20) "Hello World From PHP" |
||||||
|
int(123) |
||||||
|
float(45.67) |
||||||
|
string(10) "00000Hello" |
||||||
|
string(8) "xyxyxyxy" |
||||||
|
string(12) "Hello World!" |
||||||
Loading…
Reference in new issue