PHP code to test sscanf()

This commit is contained in:
Clayton Collie 2000-06-06 19:15:26 +00:00
parent 98bf43f229
commit 24b26065e0
2 changed files with 141 additions and 0 deletions

28
tests/scan_cases Normal file
View File

@ -0,0 +1,28 @@
# Test file used by testscanf.php. Feel free to add additional cases
# sscanf test cases. formatted to be slurped and split by explode
%d|48| valid decimal (positive)
%d|-98| valid signed decimal (negative)
%d|56a| integer scan : decimal digit followed by alpha
%4d|558071|decimal integer with width specification
%i|-5489|valid signed integer (negative)
%s| the rain in spain | plain ole string matched with %s
%10s|jabberwocky| string with width specifier
%f|289.071| valid float (%f)
%f|-0.403| valid negative (%f)
%3f|76.4|Float with width specifier ending at decimal point
%3f"|789.4|Float with width specifier
%o|0321|octal with leading 0
%o|327| valid octal digits
%o|380| octal scan with octal digit followed by non-octal
%x|fe| valid hex|
%x|0xfe| "c" style hex with leading 0x|
%x|455| hex with all single digits < f
%x|-98| hex (negative signed int)
%c|y| single char
%4c|tulips|Character with width specification (4)
%e|10e-9| signed floating point with negative exponent
%e|10e+9| signed floating point with explicit positive exponent
%e|10e9| signed floating point with positive exponent (no + sign)
# next we test multiple cases
%d %i %o %u %x %s %c %e %f %g | 19 84 0666 2000 0xface your x 31e+9 0.912 2.4 |multiple specifiers

113
tests/testscanf.php Normal file
View File

@ -0,0 +1,113 @@
<?php
function print_value($val,$postfix="<br>") {
if (is_array($val)) {
for ($i = 0;$i< count($val);$i++) {
echo $val[$i] . $postfix;
}
} else {
echo $val . $postfix;
}
}
function do_sscanf($string, $format) {
$s = "sscanf(\"" . $string . ",\"" . $format ."\").";
echo "$s<br>";
$s = str_repeat("-", strlen($s));
echo "$s<br>";
$output = sscanf($string,$format);
echo "Result : ";
print_value( $output );
echo "$s<br><br>";
}
function run_sscanf_test_cases($filename="scan_cases")
{
echo "<h3><em><br>Running Test Cases from $filename<br></em></h3>";
$arr = file($filename);
for ($i=0;$i < count($arr);$i++) {
$line_arr = explode("|",$arr[$i]);
$format = $line_arr[0];
$string = $line_arr[1];
if (count($arr) > 2) {
$comment = $line_arr[2];
} else {
$comment = "";
}
if ( empty($format) || empty($string) ) {
continue;
}
print("<h4>** Case : $comment ******************************</h4>");
do_sscanf($string,$format);
}
}
function simple_tests() {
echo "Testing sscanf with standard ANSI syntax (values returned by
reference)-<br>";
$decimal = -1;
$string = "";
$hex = 0;
$float = 0.0;
$octal = 0.0;
$int = -1;
echo "<h3><em><br>Simple Test<br></em></h3>";
echo "sscanf('10','%d',&\$decimal) <br>";
echo "<br>BEFORE : <br> decimal = $decimal.";
$foo = sscanf("10","%d",&$decimal);
echo "<br>AFTER : <br> decimal = $decimal <br>";
echo "<h3><em><br>Simple Test 2<br></em></h3>";
echo "sscanf(\"ghost 0xface\",\"%s %x\",&\$string, &\$int)<br>";
echo "<br>BEFORE : <br> string = $string, int = $int<br>";
$foo = sscanf("ghost 0xface","%s %x",&$string, &$int);
echo "<br>AFTER : <br> string = $string, int = $int<br>";
echo " sscan reports : ";
print_value( $foo,"");
echo " conversions <br>";
echo "<h3><em><br>Multiple specifiers<br></em></h3>";
echo "sscanf(\"jabberwocky 1024 0xFF 1.024 644 10\",
\"%s %d %x %f %o %i\",
&\$string,&\$decimal,&\$hex,&\$float,&\$octal,&\$int);<br>";
echo "<br>BEFORE : <br>";
echo "Decimal = $decimal, String = $string, Hex = $hex<br>";
echo "Octal = $octal , Float = $float, Int = $int<br>";
$foo = sscanf( "jabberwocky 1024 0xFF 1.024 644 10",
"%s %d %x %f %o %i",
&$string,&$decimal,&$hex,&$float,&$octal,&$int);
echo "<br>AFTER :<br>";
echo "decimal = $decimal, string = $string, hex = $hex<br>";
echo "octal = $octal , float = $float, int = $int<br>";
echo " sscan reports : ";
print_value( $foo,"");
echo " conversions <br>";
echo "----------------------------------------<br>";
}
?>
<html>
<head>
<title>Test of sscanf()</title>
</head>
<body>
<strong><h1>Testing sscanf() support in PHP</h1></strong><br>
<?php
if (!function_exists('sscanf')) {
echo "<strong>I'm sorry but sscanf() does not exist !i</strong><br>";
} else {
simple_tests();
run_sscanf_test_cases();
}
?>
</body>
</html>