php-src/ext/standard/tests/file/fscanf_variation7.phpt
Peter Kokot b746e69887 Sync leading and final newlines in *.phpt sections
This patch adds missing newlines, trims multiple redundant final
newlines into a single one, and trims redundant leading newlines in all
*.phpt sections.

According to POSIX, a line is a sequence of zero or more non-' <newline>'
characters plus a terminating '<newline>' character. [1] Files should
normally have at least one final newline character.

C89 [2] and later standards [3] mention a final newline:
"A source file that is not empty shall end in a new-line character,
which shall not be immediately preceded by a backslash character."

Although it is not mandatory for all files to have a final newline
fixed, a more consistent and homogeneous approach brings less of commit
differences issues and a better development experience in certain text
editors and IDEs.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
[2] https://port70.net/~nsz/c/c89/c89-draft.html#2.1.1.2
[3] https://port70.net/~nsz/c/c99/n1256.html#5.1.1.2
2018-10-15 04:32:30 +02:00

246 lines
3.3 KiB
PHP

--TEST--
Test fscanf() function: usage variations - integer formats with boolean
--FILE--
<?php
/*
Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] );
Description: Parses input from a file according to a format
*/
/* Test fscanf() to scan boolean data using different integer format types */
$file_path = dirname(__FILE__);
echo "*** Test fscanf(): different integer format types with boolean data ***\n";
// create a file
$filename = "$file_path/fscanf_variation7.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of boolean types
$bool_types = array (
true,
false,
TRUE,
FALSE,
);
$int_formats = array( "%d", "%hd", "%ld", "%Ld", " %d", "%d ", "% d", "\t%d", "\n%d", "%4d", "%30d", "%[0-9]", "%*d");
$counter = 1;
// writing to the file
foreach($bool_types as $value) {
@fprintf($file_handle, $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using different integer formats
foreach($int_formats as $int_format) {
// rewind the file so that for every foreach iteration the file pointer starts from bof
rewind($file_handle);
echo "\n-- iteration $counter --\n";
while( !feof($file_handle) ) {
var_dump( fscanf($file_handle,$int_format) );
}
$counter++;
}
echo "\n*** Done ***";
?>
--CLEAN--
<?php
$file_path = dirname(__FILE__);
$filename = "$file_path/fscanf_variation7.tmp";
unlink($filename);
?>
--EXPECTF--
*** Test fscanf(): different integer format types with boolean data ***
-- iteration 1 --
array(1) {
[0]=>
int(1)
}
NULL
array(1) {
[0]=>
int(1)
}
NULL
bool(false)
-- iteration 2 --
array(1) {
[0]=>
int(1)
}
NULL
array(1) {
[0]=>
int(1)
}
NULL
bool(false)
-- iteration 3 --
array(1) {
[0]=>
int(1)
}
NULL
array(1) {
[0]=>
int(1)
}
NULL
bool(false)
-- iteration 4 --
array(1) {
[0]=>
int(1)
}
NULL
array(1) {
[0]=>
int(1)
}
NULL
bool(false)
-- iteration 5 --
array(1) {
[0]=>
int(1)
}
NULL
array(1) {
[0]=>
int(1)
}
NULL
bool(false)
-- iteration 6 --
array(1) {
[0]=>
int(1)
}
NULL
array(1) {
[0]=>
int(1)
}
NULL
bool(false)
-- iteration 7 --
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL
Warning: fscanf(): Bad scan conversion character " " in %s on line %d
NULL
bool(false)
-- iteration 8 --
array(1) {
[0]=>
int(1)
}
NULL
array(1) {
[0]=>
int(1)
}
NULL
bool(false)
-- iteration 9 --
array(1) {
[0]=>
int(1)
}
NULL
array(1) {
[0]=>
int(1)
}
NULL
bool(false)
-- iteration 10 --
array(1) {
[0]=>
int(1)
}
NULL
array(1) {
[0]=>
int(1)
}
NULL
bool(false)
-- iteration 11 --
array(1) {
[0]=>
int(1)
}
NULL
array(1) {
[0]=>
int(1)
}
NULL
bool(false)
-- iteration 12 --
array(1) {
[0]=>
string(1) "1"
}
array(1) {
[0]=>
NULL
}
array(1) {
[0]=>
string(1) "1"
}
array(1) {
[0]=>
NULL
}
bool(false)
-- iteration 13 --
array(0) {
}
NULL
array(0) {
}
NULL
bool(false)
*** Done ***