summaryrefslogtreecommitdiff
path: root/tests/testscanf.php
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 /tests/testscanf.php
parent98bf43f22903c3deb6e06942f93adff7c7af640e (diff)
downloadphp-git-24b26065e00f702537a6aae53eab22b968a7f8bc.tar.gz
PHP code to test sscanf()
Diffstat (limited to 'tests/testscanf.php')
-rw-r--r--tests/testscanf.php113
1 files changed, 113 insertions, 0 deletions
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>