]> granicus.if.org Git - php/blob
e9fc0a4383
[php] /
1 --TEST--
2 Test printf() function : error conditions
3 --FILE--
4 <?php
5 /* Prototype  : int printf  ( string $format  [, mixed $args  [, mixed $...  ]] )
6  * Description: Produces output according to format .
7  * Source code: ext/standard/formatted_print.c
8  */
9
10 echo "*** Testing printf() : error conditions ***\n";
11
12 // Zero arguments
13 echo "\n-- Testing printf() function with Zero arguments --\n";
14 try {
15     var_dump( printf() );
16 } catch (TypeError $e) {
17     echo $e->getMessage(), "\n";
18 }
19
20 echo "\n-- Testing printf() function with less than expected no. of arguments --\n";
21 $format1 = '%s';
22 $format2 = '%s%s';
23 $format3 = '%s%s%s';
24 $arg1 = 'one';
25 $arg2 = 'two';
26
27 echo "\n-- Call printf with one argument less than expected --\n";
28 try {
29     var_dump( printf($format1) );
30 } catch (\ArgumentCountError $e) {
31     echo $e->getMessage(), "\n";
32 }
33 try {
34     var_dump( printf($format2,$arg1) );
35 } catch (\ArgumentCountError $e) {
36     echo $e->getMessage(), "\n";
37 }
38 try {
39     var_dump( printf($format3,$arg1,$arg2) );
40 } catch (\ArgumentCountError $e) {
41     echo $e->getMessage(), "\n";
42 }
43
44 echo "\n-- Call printf with two argument less than expected --\n";
45 try {
46     var_dump( printf($format2) );
47 } catch (\ArgumentCountError $e) {
48     echo $e->getMessage(), "\n";
49 }
50 try {
51     var_dump( printf($format3,$arg1) );
52 } catch (\ArgumentCountError $e) {
53     echo $e->getMessage(), "\n";
54 }
55
56 echo "\n-- Call printf with three argument less than expected --\n";
57 try {
58     var_dump( printf($format3) );
59 } catch (\ArgumentCountError $e) {
60     echo $e->getMessage(), "\n";
61 }
62
63 ?>
64 --EXPECT--
65 *** Testing printf() : error conditions ***
66
67 -- Testing printf() function with Zero arguments --
68 printf() expects at least 1 parameter, 0 given
69
70 -- Testing printf() function with less than expected no. of arguments --
71
72 -- Call printf with one argument less than expected --
73 2 parameters are required, 1 given
74 3 parameters are required, 2 given
75 4 parameters are required, 3 given
76
77 -- Call printf with two argument less than expected --
78 3 parameters are required, 1 given
79 4 parameters are required, 2 given
80
81 -- Call printf with three argument less than expected --
82 4 parameters are required, 1 given