1--TEST--
2Test sprintf() function : usage variations - unexpected values for format argument
3--FILE--
4<?php
5/*
6* Testing sprintf() : with different unexpected values for format argument other than the strings
7*/
8
9echo "*** Testing sprintf() : with unexpected values for format argument ***\n";
10
11// initialing required variables
12$arg1 = "second arg";
13$arg2 = "third arg";
14
15// declaring class
16class sample
17{
18  public function __toString() {
19    return "Object";
20  }
21}
22
23// creating a file resource
24$file_handle = fopen(__FILE__, 'r');
25
26//array of values to iterate over
27$values = array(
28
29      // int data
30      0,
31      1,
32      12345,
33      -2345,
34
35      // float data
36      10.5,
37      -10.5,
38      10.1234567e10,
39      10.7654321E-10,
40      .5,
41
42      // array data
43      array(),
44      array(0),
45      array(1),
46      array(1, 2),
47      array('color' => 'red', 'item' => 'pen'),
48
49      // boolean data
50      true,
51      false,
52      TRUE,
53      FALSE,
54
55      // empty data
56      "",
57      '',
58
59      // object data
60      new sample(),
61
62      // resource data
63      $file_handle
64);
65
66// loop through each element of the array for format
67
68$count = 1;
69foreach($values as $value) {
70  echo "\n-- Iteration $count --\n";
71
72  // with default argument
73  try {
74    var_dump(sprintf($value));
75  } catch (TypeError $exception) {
76    echo $exception->getMessage() . "\n";
77  }
78
79  // with two arguments
80  try {
81    var_dump(sprintf($value, $arg1));
82  } catch (TypeError $exception) {
83    echo $exception->getMessage() . "\n";
84  }
85
86  // with three arguments
87  try {
88    var_dump(sprintf($value, $arg1, $arg2));
89  } catch (TypeError $exception) {
90    echo $exception->getMessage() . "\n";
91  }
92
93  $count++;
94}
95
96// close the resource
97fclose($file_handle);
98
99echo "Done";
100?>
101--EXPECT--
102*** Testing sprintf() : with unexpected values for format argument ***
103
104-- Iteration 1 --
105string(1) "0"
106string(1) "0"
107string(1) "0"
108
109-- Iteration 2 --
110string(1) "1"
111string(1) "1"
112string(1) "1"
113
114-- Iteration 3 --
115string(5) "12345"
116string(5) "12345"
117string(5) "12345"
118
119-- Iteration 4 --
120string(5) "-2345"
121string(5) "-2345"
122string(5) "-2345"
123
124-- Iteration 5 --
125string(4) "10.5"
126string(4) "10.5"
127string(4) "10.5"
128
129-- Iteration 6 --
130string(5) "-10.5"
131string(5) "-10.5"
132string(5) "-10.5"
133
134-- Iteration 7 --
135string(12) "101234567000"
136string(12) "101234567000"
137string(12) "101234567000"
138
139-- Iteration 8 --
140string(13) "1.07654321E-9"
141string(13) "1.07654321E-9"
142string(13) "1.07654321E-9"
143
144-- Iteration 9 --
145string(3) "0.5"
146string(3) "0.5"
147string(3) "0.5"
148
149-- Iteration 10 --
150sprintf(): Argument #1 ($format) must be of type string, array given
151sprintf(): Argument #1 ($format) must be of type string, array given
152sprintf(): Argument #1 ($format) must be of type string, array given
153
154-- Iteration 11 --
155sprintf(): Argument #1 ($format) must be of type string, array given
156sprintf(): Argument #1 ($format) must be of type string, array given
157sprintf(): Argument #1 ($format) must be of type string, array given
158
159-- Iteration 12 --
160sprintf(): Argument #1 ($format) must be of type string, array given
161sprintf(): Argument #1 ($format) must be of type string, array given
162sprintf(): Argument #1 ($format) must be of type string, array given
163
164-- Iteration 13 --
165sprintf(): Argument #1 ($format) must be of type string, array given
166sprintf(): Argument #1 ($format) must be of type string, array given
167sprintf(): Argument #1 ($format) must be of type string, array given
168
169-- Iteration 14 --
170sprintf(): Argument #1 ($format) must be of type string, array given
171sprintf(): Argument #1 ($format) must be of type string, array given
172sprintf(): Argument #1 ($format) must be of type string, array given
173
174-- Iteration 15 --
175string(1) "1"
176string(1) "1"
177string(1) "1"
178
179-- Iteration 16 --
180string(0) ""
181string(0) ""
182string(0) ""
183
184-- Iteration 17 --
185string(1) "1"
186string(1) "1"
187string(1) "1"
188
189-- Iteration 18 --
190string(0) ""
191string(0) ""
192string(0) ""
193
194-- Iteration 19 --
195string(0) ""
196string(0) ""
197string(0) ""
198
199-- Iteration 20 --
200string(0) ""
201string(0) ""
202string(0) ""
203
204-- Iteration 21 --
205string(6) "Object"
206string(6) "Object"
207string(6) "Object"
208
209-- Iteration 22 --
210sprintf(): Argument #1 ($format) must be of type string, resource given
211sprintf(): Argument #1 ($format) must be of type string, resource given
212sprintf(): Argument #1 ($format) must be of type string, resource given
213Done
214