New sscanf() tests. Tested on Windows. Linux and Linux 64 bit

This commit is contained in:
andy wharmby 2009-01-18 14:40:29 +00:00
parent cdef2ee23b
commit b819f60eed
11 changed files with 757 additions and 0 deletions

View File

@ -0,0 +1,44 @@
--TEST--
Test sscanf() function : basic functionality - string format
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
/*
* Testing sscanf() : basic functionality
*/
echo "*** Testing sscanf() : basic functionality - using string format ***\n";
$str = "Part: Widget Serial Number: 1234789 Stock: 25";
$format = "Part: %s Serial Number: %s Stock: %s";
echo "\n-- Try sccanf() WITHOUT optional args --\n";
// extract details using short format
list($part, $number, $stock) = sscanf($str, $format);
var_dump($part, $number, $stock);
echo "\n-- Try sccanf() WITH optional args --\n";
// extract details using long format
$res = sscanf($str, $format, $part, $number, $stock);
var_dump($res, $part, $number, $stock);
?>
===DONE===
--EXPECT--
*** Testing sscanf() : basic functionality - using string format ***
-- Try sccanf() WITHOUT optional args --
string(6) "Widget"
string(7) "1234789"
string(2) "25"
-- Try sccanf() WITH optional args --
int(3)
string(6) "Widget"
string(7) "1234789"
string(2) "25"
===DONE===

View File

@ -0,0 +1,44 @@
--TEST--
Test sscanf() function : basic functionality - integer format
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
/*
* Testing sscanf() : basic functionality
*/
echo "*** Testing sscanf() : basic functionality - using integer format ***\n";
$str = "Part: Widget Serial Number: 1234789 Stock: 25";
$format = "Part: %s Serial Number: %d Stock: %d";
echo "\n-- Try sccanf() WITHOUT optional args --\n";
// extract details using short format
list($part, $number, $stock) = sscanf($str, $format);
var_dump($part, $number, $stock);
echo "\n-- Try sccanf() WITH optional args --\n";
// extract details using long format
$res = sscanf($str, $format, $part, $number, $stock);
var_dump($res, $part, $number, $stock);
?>
===DONE===
--EXPECT--
*** Testing sscanf() : basic functionality - using integer format ***
-- Try sccanf() WITHOUT optional args --
string(6) "Widget"
int(1234789)
int(25)
-- Try sccanf() WITH optional args --
int(3)
string(6) "Widget"
int(1234789)
int(25)
===DONE===

View File

@ -0,0 +1,43 @@
--TEST--
Test sscanf() function : basic functionality - float format
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
echo "*** Testing sscanf() : basic functionality -- using float format ***\n";
$str = "Part: Widget Length: 111.53 Width: 22.345 Depth: 12.4";
$format = "Part: %s Length: %f Width: %f Depth: %f";
echo "\n-- Try sccanf() WITHOUT optional args --\n";
// extract details using short format
list($part, $length, $width, $depth) = sscanf($str, $format);
var_dump($part, $length, $width, $depth);
echo "\n-- Try sccanf() WITH optional args --\n";
// extract details using long format
$res = sscanf($str, $format, $part, $length, $width, $depth);
var_dump($res, $part, $length, $width, $depth);
?>
===DONE===
--EXPECT--
*** Testing sscanf() : basic functionality -- using float format ***
-- Try sccanf() WITHOUT optional args --
string(6) "Widget"
float(111.53)
float(22.345)
float(12.4)
-- Try sccanf() WITH optional args --
int(4)
string(6) "Widget"
float(111.53)
float(22.345)
float(12.4)
===DONE===

View File

@ -0,0 +1,43 @@
--TEST--
Test sscanf() function : basic functionality - char format
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
echo "*** Testing sscanf() : basic functionality - using char format ***\n";
$str = "X = A + B - C";
$format = "%c = %c + %c - %c";
echo "\n-- Try sccanf() WITHOUT optional args --\n";
// extract details using short format
list($arg1, $arg2, $arg3, $arg4) = sscanf($str, $format);
var_dump($arg1, $arg2, $arg3, $arg4);
echo "\n-- Try sccanf() WITH optional args --\n";
// extract details using long format
$res = sscanf($str, $format, $arg1, $arg2, $arg3, $arg4);
var_dump($res, $arg1, $arg2, $arg3, $arg4);
?>
===DONE===
--EXPECT--
*** Testing sscanf() : basic functionality - using char format ***
-- Try sccanf() WITHOUT optional args --
string(1) "X"
string(1) "A"
string(1) "B"
string(1) "C"
-- Try sccanf() WITH optional args --
int(4)
string(1) "X"
string(1) "A"
string(1) "B"
string(1) "C"
===DONE===

View File

@ -0,0 +1,58 @@
--TEST--
Test sscanf() function : basic functionality - exponential format
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
echo "*** Testing sscanf() : basic functionality -using exponential format ***\n";
$str = "10.12345 10.12345E3 10.12345e3 -10.12345e4" ;
$format1 = "%e %e %e %e";
$format2 = "%E %E %E %E";
echo "\n-- Try sccanf() WITHOUT optional args --\n";
// extract details using short format
list($arg1, $arg2, $arg3, $arg4) = sscanf($str, $format1);
var_dump($arg1, $arg2, $arg3, $arg4);
list($arg1, $arg2, $arg3, $arg4) = sscanf($str, $format2);
var_dump($arg1, $arg2, $arg3, $arg4);
echo "\n-- Try sccanf() WITH optional args --\n";
// extract details using long format
$res = sscanf($str, $format1, $arg1, $arg2, $arg3, $arg4);
var_dump($res, $arg1, $arg2, $arg3, $arg4);
$res = sscanf($str, $format2,$arg1, $arg2, $arg3, $arg4);
var_dump($res, $arg1, $arg2, $arg3, $arg4);
?>
===DONE===
--EXPECT--
*** Testing sscanf() : basic functionality -using exponential format ***
-- Try sccanf() WITHOUT optional args --
float(10.12345)
float(10123.45)
float(10123.45)
float(-101234.5)
float(10.12345)
float(10123.45)
float(10123.45)
float(-101234.5)
-- Try sccanf() WITH optional args --
int(4)
float(10.12345)
float(10123.45)
float(10123.45)
float(-101234.5)
int(4)
float(10.12345)
float(10123.45)
float(10123.45)
float(-101234.5)
===DONE===

View File

@ -0,0 +1,47 @@
--TEST--
Test sscanf() function : basic functionality - unsigned format
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
echo "*** Testing sscanf() : basic functionality - using unsigned format ***\n";
$str = "-11 +11 11 -11.234 +11.234 11.234";
$format = "%u %u %u %u %u %u";
echo "\n-- Try sccanf() WITHOUT optional args --\n";
// extract details using short format
list($arg1, $arg2, $arg3, $arg4, $arg5, $arg6) = sscanf($str, $format);
var_dump($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
echo "\n-- Try sccanf() WITH optional args --\n";
// extract details using long format
$res = sscanf($str, $format, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
var_dump($res, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
?>
===DONE===
--EXPECT--
*** Testing sscanf() : basic functionality - using unsigned format ***
-- Try sccanf() WITHOUT optional args --
string(10) "4294967285"
int(11)
int(11)
string(10) "4294967285"
NULL
NULL
-- Try sccanf() WITH optional args --
int(4)
string(10) "4294967285"
int(11)
int(11)
string(10) "4294967285"
NULL
NULL
===DONE===

View File

@ -0,0 +1,47 @@
--TEST--
Test sscanf() function : basic functionality - octal format
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
echo "*** Testing sscanf() : basic functionality - using octal format ***\n";
$str = "0123 -0123 +0123 0129 -0129 +0129";
$format = "%o %o %o %o %o %o";
echo "\n-- Try sccanf() WITHOUT optional args --\n";
// extract details using short format
list($arg1, $arg2, $arg3, $arg4, $arg5, $arg6) = sscanf($str, $format);
var_dump($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
echo "\n-- Try sccanf() WITH optional args --\n";
// extract details using long format
$res = sscanf($str, $format, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
var_dump($res, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
?>
===DONE===
--EXPECT--
*** Testing sscanf() : basic functionality - using octal format ***
-- Try sccanf() WITHOUT optional args --
int(83)
int(-83)
int(83)
int(10)
NULL
NULL
-- Try sccanf() WITH optional args --
int(4)
int(83)
int(-83)
int(83)
int(10)
NULL
NULL
===DONE===

View File

@ -0,0 +1,65 @@
--TEST--
Test sscanf() function : basic functionality - hexadecimal format
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
echo "*** Testing sscanf() : basic functionality - - using hexadecimal format ***\n";
$str = "129 12F 123B -123B 01ABC 1G";
$format1 = "%x %x %x %x %x %x";
$format2 = "%X %X %X %X %X %X";
echo "\n-- Try sccanf() WITHOUT optional args --\n";
// extract details using short format
list($arg1, $arg2, $arg3, $arg4, $arg5, $arg6) = sscanf($str, $format1);
var_dump($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
list($arg1, $arg2, $arg3, $arg4, $arg5, $arg6) = sscanf($str, $format2);
var_dump($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
echo "\n-- Try sccanf() WITH optional args --\n";
// extract details using long format
$res = sscanf($str, $format1, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
var_dump($res, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
$res = sscanf($str, $format2, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
var_dump($res, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
?>
===DONE===
--EXPECT--
*** Testing sscanf() : basic functionality - - using hexadecimal format ***
-- Try sccanf() WITHOUT optional args --
int(297)
int(303)
int(4667)
int(-4667)
int(6844)
int(1)
int(297)
int(303)
int(4667)
int(-4667)
int(6844)
int(1)
-- Try sccanf() WITH optional args --
int(6)
int(297)
int(303)
int(4667)
int(-4667)
int(6844)
int(1)
int(6)
int(297)
int(303)
int(4667)
int(-4667)
int(6844)
int(1)
===DONE===

View File

@ -0,0 +1,44 @@
--TEST--
Test sscanf() function : error conditions
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
echo "*** Testing sscanf() : error conditions ***\n";
$str = "Hello World";
$format = "%s %s";
echo "\n-- Testing sscanf() function with no arguments --\n";
var_dump( sscanf() );
echo "\n-- Testing sscanf() function with one argument --\n";
var_dump( sscanf($str) );
echo "\n-- Testing sscanf() function with more than expected no. of arguments --\n";
var_dump( sscanf($str, $format, $str1, $str2, $extra_str) );
?>
===DONE===
--EXPECTF--
*** Testing sscanf() : error conditions ***
-- Testing sscanf() function with no arguments --
Warning: sscanf() expects at least 2 parameters, 0 given in %s on line %d
NULL
-- Testing sscanf() function with one argument --
Warning: sscanf() expects at least 2 parameters, 1 given in %s on line %d
NULL
-- Testing sscanf() function with more than expected no. of arguments --
Warning: sscanf(): Variable is not assigned by any conversion specifiers in %s on line %d
int(-1)
===DONE===

View File

@ -0,0 +1,169 @@
--TEST--
Test sscanf() function : usage variations - unexpected inputs for '$str' argument
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
echo "*** Testing sscanf() function: with unexpected inputs for 'str' argument ***\n";
//get an unset variable
$unset_var = 'string_val';
unset($unset_var);
//defining a class
class sample {
public function __toString() {
return "sample object";
}
}
//getting the resource
$file_handle = fopen(__FILE__, "r");
// array with different values for $input
$inputs = array (
// integer values
/*1*/ 0,
1,
-2,
2147483647,
-2147483648,
// float values
/*6*/ 10.5,
-20.5,
10.1234567e10,
// array values
/*9*/ array(),
array(0),
array(1, 2),
// boolean values
/*12*/ true,
false,
TRUE,
FALSE,
// null values
/*16*/ NULL,
null,
// objects
/*18*/ new sample(),
// resource
/*19*/ $file_handle,
// undefined variable
/*20*/ @$undefined_var,
// unset variable
/*21*/ @$unset_var
);
//defining '$pad_length' argument
$format = "%s";
// loop through with each element of the $inputs array to test sscanf() function
$count = 1;
foreach($inputs as $input) {
echo "-- Iteration $count --\n";
var_dump( sscanf($input, $format) );
$count ++;
}
fclose($file_handle); //closing the file handle
?>
===DONE===
--EXPECTF--
*** Testing sscanf() function: with unexpected inputs for 'str' argument ***
-- Iteration 1 --
array(1) {
[0]=>
string(1) "0"
}
-- Iteration 2 --
array(1) {
[0]=>
string(1) "1"
}
-- Iteration 3 --
array(1) {
[0]=>
string(2) "-2"
}
-- Iteration 4 --
array(1) {
[0]=>
string(10) "2147483647"
}
-- Iteration 5 --
array(1) {
[0]=>
string(11) "-2147483648"
}
-- Iteration 6 --
array(1) {
[0]=>
string(4) "10.5"
}
-- Iteration 7 --
array(1) {
[0]=>
string(5) "-20.5"
}
-- Iteration 8 --
array(1) {
[0]=>
string(12) "101234567000"
}
-- Iteration 9 --
Warning: sscanf() expects parameter 1 to be string, array given in %s on line %d
NULL
-- Iteration 10 --
Warning: sscanf() expects parameter 1 to be string, array given in %s on line %d
NULL
-- Iteration 11 --
Warning: sscanf() expects parameter 1 to be string, array given in %s on line %d
NULL
-- Iteration 12 --
array(1) {
[0]=>
string(1) "1"
}
-- Iteration 13 --
NULL
-- Iteration 14 --
array(1) {
[0]=>
string(1) "1"
}
-- Iteration 15 --
NULL
-- Iteration 16 --
NULL
-- Iteration 17 --
NULL
-- Iteration 18 --
array(1) {
[0]=>
string(6) "sample"
}
-- Iteration 19 --
Warning: sscanf() expects parameter 1 to be string, resource given in %s on line %d
NULL
-- Iteration 20 --
NULL
-- Iteration 21 --
NULL
===DONE===

View File

@ -0,0 +1,153 @@
--TEST--
Test sscanf() function : usage variations - unexpected inputs for '$format' argument
--FILE--
<?php
/* Prototype : mixed sscanf ( string $str , string $format [, mixed &$... ] )
* Description: Parses input from a string according to a format
* Source code: ext/standard/string.c
*/
echo "*** Testing sscanf() function: with unexpected inputs for 'format' argument ***\n";
//get an unset variable
$unset_var = 'string_val';
unset($unset_var);
//defining a class
class sample {
public function __toString() {
return "sample object";
}
}
//getting the resource
$file_handle = fopen(__FILE__, "r");
// array with different values for $input
$inputs = array (
// integer values
/*1*/ 0,
1,
-2,
2147483647,
-2147483648,
// float values
/*6*/ 10.5,
-20.5,
10.1234567e10,
// array values
/*9*/ array(),
array(0),
array(1, 2),
// boolean values
/*12*/ true,
false,
TRUE,
FALSE,
// null values
/*16*/ NULL,
null,
// objects
/*18*/ new sample(),
// resource
/*19*/ $file_handle,
// undefined variable
/*20*/ @$undefined_var,
// unset variable
/*21*/ @$unset_var
);
//defining '$pad_length' argument
$str = "Hello World";
// loop through with each element of the $inputs array to test sscanf() function
$count = 1;
foreach($inputs as $input) {
echo "-- Iteration $count --\n";
var_dump( sscanf($str, $input) );
$count ++;
}
fclose($file_handle); //closing the file handle
?>
===DONE===
--EXPECTF--
*** Testing sscanf() function: with unexpected inputs for 'format' argument ***
-- Iteration 1 --
array(0) {
}
-- Iteration 2 --
array(0) {
}
-- Iteration 3 --
array(0) {
}
-- Iteration 4 --
array(0) {
}
-- Iteration 5 --
array(0) {
}
-- Iteration 6 --
array(0) {
}
-- Iteration 7 --
array(0) {
}
-- Iteration 8 --
array(0) {
}
-- Iteration 9 --
Warning: sscanf() expects parameter 2 to be string, array given in %s on line %d
NULL
-- Iteration 10 --
Warning: sscanf() expects parameter 2 to be string, array given in %s on line %d
NULL
-- Iteration 11 --
Warning: sscanf() expects parameter 2 to be string, array given in %s on line %d
NULL
-- Iteration 12 --
array(0) {
}
-- Iteration 13 --
array(0) {
}
-- Iteration 14 --
array(0) {
}
-- Iteration 15 --
array(0) {
}
-- Iteration 16 --
array(0) {
}
-- Iteration 17 --
array(0) {
}
-- Iteration 18 --
array(0) {
}
-- Iteration 19 --
Warning: sscanf() expects parameter 2 to be string, resource given in %s on line %d
NULL
-- Iteration 20 --
array(0) {
}
-- Iteration 21 --
array(0) {
}
===DONE===