From 24b26065e00f702537a6aae53eab22b968a7f8bc Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Tue, 6 Jun 2000 19:15:26 +0000 Subject: PHP code to test sscanf() --- tests/testscanf.php | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/testscanf.php (limited to 'tests/testscanf.php') 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 @@ +") { + 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
"; + $s = str_repeat("-", strlen($s)); + echo "$s
"; + $output = sscanf($string,$format); + echo "Result : "; + print_value( $output ); + echo "$s

"; +} + + +function run_sscanf_test_cases($filename="scan_cases") +{ + + echo "


Running Test Cases from $filename

"; + $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("

** Case : $comment ******************************

"); + do_sscanf($string,$format); + } +} + +function simple_tests() { + echo "Testing sscanf with standard ANSI syntax (values returned by +reference)-
"; + $decimal = -1; + $string = ""; + $hex = 0; + $float = 0.0; + $octal = 0.0; + $int = -1; + + echo "


Simple Test

"; + echo "sscanf('10','%d',&\$decimal)
"; + echo "
BEFORE :
decimal = $decimal."; + $foo = sscanf("10","%d",&$decimal); + echo "
AFTER :
decimal = $decimal
"; + + + echo "


Simple Test 2

"; + echo "sscanf(\"ghost 0xface\",\"%s %x\",&\$string, &\$int)
"; + echo "
BEFORE :
string = $string, int = $int
"; + $foo = sscanf("ghost 0xface","%s %x",&$string, &$int); + echo "
AFTER :
string = $string, int = $int
"; + echo " sscan reports : "; + print_value( $foo,""); + echo " conversions
"; + + echo "


Multiple specifiers

"; + echo "sscanf(\"jabberwocky 1024 0xFF 1.024 644 10\", + \"%s %d %x %f %o %i\", + &\$string,&\$decimal,&\$hex,&\$float,&\$octal,&\$int);
"; + echo "
BEFORE :
"; + echo "Decimal = $decimal, String = $string, Hex = $hex
"; + echo "Octal = $octal , Float = $float, Int = $int
"; + $foo = sscanf( "jabberwocky 1024 0xFF 1.024 644 10", + "%s %d %x %f %o %i", + &$string,&$decimal,&$hex,&$float,&$octal,&$int); + echo "
AFTER :
"; + echo "decimal = $decimal, string = $string, hex = $hex
"; + echo "octal = $octal , float = $float, int = $int
"; + + echo " sscan reports : "; + print_value( $foo,""); + echo " conversions
"; + echo "----------------------------------------
"; +} + + + +?> + + + Test of sscanf() + + +

Testing sscanf() support in PHP


+ I'm sorry but sscanf() does not exist !i
"; + } else { + simple_tests(); + run_sscanf_test_cases(); + } + ?> + + -- cgit v1.2.1