summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClayton Collie <ccollie@php.net>2000-06-06 19:15:26 +0000
committerClayton Collie <ccollie@php.net>2000-06-06 19:15:26 +0000
commit24b26065e00f702537a6aae53eab22b968a7f8bc (patch)
tree9679e847978912bdbdf94d3df5cde11686556649
parent98bf43f22903c3deb6e06942f93adff7c7af640e (diff)
downloadphp-git-24b26065e00f702537a6aae53eab22b968a7f8bc.tar.gz
PHP code to test sscanf()
-rw-r--r--tests/scan_cases28
-rw-r--r--tests/testscanf.php113
2 files changed, 141 insertions, 0 deletions
diff --git a/tests/scan_cases b/tests/scan_cases
new file mode 100644
index 0000000000..d562230fde
--- /dev/null
+++ b/tests/scan_cases
@@ -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
+
diff --git a/tests/testscanf.php b/tests/testscanf.php
new file mode 100644
index 0000000000..ad530978c5
--- /dev/null
+++ b/tests/testscanf.php
@@ -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>