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/scan_cases | 28 +++++++++++++ tests/testscanf.php | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 tests/scan_cases create mode 100644 tests/testscanf.php (limited to 'tests') 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 @@ +") { + 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