1--TEST--
2Test fscanf() function: usage variations - integer formats with float values
3--SKIPIF--
4<?php
5if (PHP_INT_SIZE != 4) {
6    die("skip this test is for 32bit platform only");
7}
8?>
9--FILE--
10<?php
11
12/* Test fscanf() to scan float values using different integer format types */
13
14$file_path = __DIR__;
15
16echo "*** Test fscanf(): different integer format types with float values ***\n";
17
18// create a file
19$filename = "$file_path/fscanf_variation3.tmp";
20$file_handle = fopen($filename, "w");
21if($file_handle == false)
22  exit("Error:failed to open file $filename");
23
24// array of float type values
25
26$float_values = array (
27  -2147483649,
28  2147483648,
29  -0x80000001, // float value, beyond max negative int
30  0x800000001, // float value, beyond max positive int
31  020000000001, // float value, beyond max positive int
32  -020000000001, // float value, beyond max negative int
33  0.0,
34  -0.1,
35  1.0,
36  1e5,
37  -1e6,
38  1E8,
39  -1E9,
40  10.0000000000000000005,
41  10.5e+5
42);
43
44$int_formats = array( "%d", "%hd", "%ld", "%Ld", " %d", "%d ", "% d", "\t%d", "\n%d", "%4d", "%30d", "%[0-9]", "%*d");
45
46$counter = 1;
47
48// writing to the file
49foreach($float_values as $value) {
50  @fprintf($file_handle, $value);
51  @fprintf($file_handle, "\n");
52}
53// closing the file
54fclose($file_handle);
55
56// opening the file for reading
57$file_handle = fopen($filename, "r");
58if($file_handle == false) {
59  exit("Error:failed to open file $filename");
60}
61
62$counter = 1;
63// reading the values from file using different integer formats
64foreach($int_formats as $int_format) {
65  // rewind the file so that for every foreach iteration the file pointer starts from bof
66  rewind($file_handle);
67  echo "\n-- iteration $counter --\n";
68  while( !feof($file_handle) ) {
69    try {
70      var_dump( fscanf($file_handle,$int_format) );
71    } catch (ValueError $exception) {
72      echo $exception->getMessage() . "\n";
73    }
74  }
75  $counter++;
76}
77
78echo "\n*** Done ***";
79?>
80--CLEAN--
81<?php
82$file_path = __DIR__;
83$filename = "$file_path/fscanf_variation3.tmp";
84unlink($filename);
85?>
86--EXPECT--
87*** Test fscanf(): different integer format types with float values ***
88
89-- iteration 1 --
90array(1) {
91  [0]=>
92  int(-2147483648)
93}
94array(1) {
95  [0]=>
96  int(2147483647)
97}
98array(1) {
99  [0]=>
100  int(-2147483648)
101}
102array(1) {
103  [0]=>
104  int(2147483647)
105}
106array(1) {
107  [0]=>
108  int(2147483647)
109}
110array(1) {
111  [0]=>
112  int(-2147483648)
113}
114array(1) {
115  [0]=>
116  int(0)
117}
118array(1) {
119  [0]=>
120  int(0)
121}
122array(1) {
123  [0]=>
124  int(1)
125}
126array(1) {
127  [0]=>
128  int(100000)
129}
130array(1) {
131  [0]=>
132  int(-1000000)
133}
134array(1) {
135  [0]=>
136  int(100000000)
137}
138array(1) {
139  [0]=>
140  int(-1000000000)
141}
142array(1) {
143  [0]=>
144  int(10)
145}
146array(1) {
147  [0]=>
148  int(1050000)
149}
150bool(false)
151
152-- iteration 2 --
153array(1) {
154  [0]=>
155  int(-2147483648)
156}
157array(1) {
158  [0]=>
159  int(2147483647)
160}
161array(1) {
162  [0]=>
163  int(-2147483648)
164}
165array(1) {
166  [0]=>
167  int(2147483647)
168}
169array(1) {
170  [0]=>
171  int(2147483647)
172}
173array(1) {
174  [0]=>
175  int(-2147483648)
176}
177array(1) {
178  [0]=>
179  int(0)
180}
181array(1) {
182  [0]=>
183  int(0)
184}
185array(1) {
186  [0]=>
187  int(1)
188}
189array(1) {
190  [0]=>
191  int(100000)
192}
193array(1) {
194  [0]=>
195  int(-1000000)
196}
197array(1) {
198  [0]=>
199  int(100000000)
200}
201array(1) {
202  [0]=>
203  int(-1000000000)
204}
205array(1) {
206  [0]=>
207  int(10)
208}
209array(1) {
210  [0]=>
211  int(1050000)
212}
213bool(false)
214
215-- iteration 3 --
216array(1) {
217  [0]=>
218  int(-2147483648)
219}
220array(1) {
221  [0]=>
222  int(2147483647)
223}
224array(1) {
225  [0]=>
226  int(-2147483648)
227}
228array(1) {
229  [0]=>
230  int(2147483647)
231}
232array(1) {
233  [0]=>
234  int(2147483647)
235}
236array(1) {
237  [0]=>
238  int(-2147483648)
239}
240array(1) {
241  [0]=>
242  int(0)
243}
244array(1) {
245  [0]=>
246  int(0)
247}
248array(1) {
249  [0]=>
250  int(1)
251}
252array(1) {
253  [0]=>
254  int(100000)
255}
256array(1) {
257  [0]=>
258  int(-1000000)
259}
260array(1) {
261  [0]=>
262  int(100000000)
263}
264array(1) {
265  [0]=>
266  int(-1000000000)
267}
268array(1) {
269  [0]=>
270  int(10)
271}
272array(1) {
273  [0]=>
274  int(1050000)
275}
276bool(false)
277
278-- iteration 4 --
279array(1) {
280  [0]=>
281  int(-2147483648)
282}
283array(1) {
284  [0]=>
285  int(2147483647)
286}
287array(1) {
288  [0]=>
289  int(-2147483648)
290}
291array(1) {
292  [0]=>
293  int(2147483647)
294}
295array(1) {
296  [0]=>
297  int(2147483647)
298}
299array(1) {
300  [0]=>
301  int(-2147483648)
302}
303array(1) {
304  [0]=>
305  int(0)
306}
307array(1) {
308  [0]=>
309  int(0)
310}
311array(1) {
312  [0]=>
313  int(1)
314}
315array(1) {
316  [0]=>
317  int(100000)
318}
319array(1) {
320  [0]=>
321  int(-1000000)
322}
323array(1) {
324  [0]=>
325  int(100000000)
326}
327array(1) {
328  [0]=>
329  int(-1000000000)
330}
331array(1) {
332  [0]=>
333  int(10)
334}
335array(1) {
336  [0]=>
337  int(1050000)
338}
339bool(false)
340
341-- iteration 5 --
342array(1) {
343  [0]=>
344  int(-2147483648)
345}
346array(1) {
347  [0]=>
348  int(2147483647)
349}
350array(1) {
351  [0]=>
352  int(-2147483648)
353}
354array(1) {
355  [0]=>
356  int(2147483647)
357}
358array(1) {
359  [0]=>
360  int(2147483647)
361}
362array(1) {
363  [0]=>
364  int(-2147483648)
365}
366array(1) {
367  [0]=>
368  int(0)
369}
370array(1) {
371  [0]=>
372  int(0)
373}
374array(1) {
375  [0]=>
376  int(1)
377}
378array(1) {
379  [0]=>
380  int(100000)
381}
382array(1) {
383  [0]=>
384  int(-1000000)
385}
386array(1) {
387  [0]=>
388  int(100000000)
389}
390array(1) {
391  [0]=>
392  int(-1000000000)
393}
394array(1) {
395  [0]=>
396  int(10)
397}
398array(1) {
399  [0]=>
400  int(1050000)
401}
402bool(false)
403
404-- iteration 6 --
405array(1) {
406  [0]=>
407  int(-2147483648)
408}
409array(1) {
410  [0]=>
411  int(2147483647)
412}
413array(1) {
414  [0]=>
415  int(-2147483648)
416}
417array(1) {
418  [0]=>
419  int(2147483647)
420}
421array(1) {
422  [0]=>
423  int(2147483647)
424}
425array(1) {
426  [0]=>
427  int(-2147483648)
428}
429array(1) {
430  [0]=>
431  int(0)
432}
433array(1) {
434  [0]=>
435  int(0)
436}
437array(1) {
438  [0]=>
439  int(1)
440}
441array(1) {
442  [0]=>
443  int(100000)
444}
445array(1) {
446  [0]=>
447  int(-1000000)
448}
449array(1) {
450  [0]=>
451  int(100000000)
452}
453array(1) {
454  [0]=>
455  int(-1000000000)
456}
457array(1) {
458  [0]=>
459  int(10)
460}
461array(1) {
462  [0]=>
463  int(1050000)
464}
465bool(false)
466
467-- iteration 7 --
468Bad scan conversion character " "
469Bad scan conversion character " "
470Bad scan conversion character " "
471Bad scan conversion character " "
472Bad scan conversion character " "
473Bad scan conversion character " "
474Bad scan conversion character " "
475Bad scan conversion character " "
476Bad scan conversion character " "
477Bad scan conversion character " "
478Bad scan conversion character " "
479Bad scan conversion character " "
480Bad scan conversion character " "
481Bad scan conversion character " "
482Bad scan conversion character " "
483bool(false)
484
485-- iteration 8 --
486array(1) {
487  [0]=>
488  int(-2147483648)
489}
490array(1) {
491  [0]=>
492  int(2147483647)
493}
494array(1) {
495  [0]=>
496  int(-2147483648)
497}
498array(1) {
499  [0]=>
500  int(2147483647)
501}
502array(1) {
503  [0]=>
504  int(2147483647)
505}
506array(1) {
507  [0]=>
508  int(-2147483648)
509}
510array(1) {
511  [0]=>
512  int(0)
513}
514array(1) {
515  [0]=>
516  int(0)
517}
518array(1) {
519  [0]=>
520  int(1)
521}
522array(1) {
523  [0]=>
524  int(100000)
525}
526array(1) {
527  [0]=>
528  int(-1000000)
529}
530array(1) {
531  [0]=>
532  int(100000000)
533}
534array(1) {
535  [0]=>
536  int(-1000000000)
537}
538array(1) {
539  [0]=>
540  int(10)
541}
542array(1) {
543  [0]=>
544  int(1050000)
545}
546bool(false)
547
548-- iteration 9 --
549array(1) {
550  [0]=>
551  int(-2147483648)
552}
553array(1) {
554  [0]=>
555  int(2147483647)
556}
557array(1) {
558  [0]=>
559  int(-2147483648)
560}
561array(1) {
562  [0]=>
563  int(2147483647)
564}
565array(1) {
566  [0]=>
567  int(2147483647)
568}
569array(1) {
570  [0]=>
571  int(-2147483648)
572}
573array(1) {
574  [0]=>
575  int(0)
576}
577array(1) {
578  [0]=>
579  int(0)
580}
581array(1) {
582  [0]=>
583  int(1)
584}
585array(1) {
586  [0]=>
587  int(100000)
588}
589array(1) {
590  [0]=>
591  int(-1000000)
592}
593array(1) {
594  [0]=>
595  int(100000000)
596}
597array(1) {
598  [0]=>
599  int(-1000000000)
600}
601array(1) {
602  [0]=>
603  int(10)
604}
605array(1) {
606  [0]=>
607  int(1050000)
608}
609bool(false)
610
611-- iteration 10 --
612array(1) {
613  [0]=>
614  int(-214)
615}
616array(1) {
617  [0]=>
618  int(2147)
619}
620array(1) {
621  [0]=>
622  int(-214)
623}
624array(1) {
625  [0]=>
626  int(3435)
627}
628array(1) {
629  [0]=>
630  int(2147)
631}
632array(1) {
633  [0]=>
634  int(-214)
635}
636array(1) {
637  [0]=>
638  int(0)
639}
640array(1) {
641  [0]=>
642  int(0)
643}
644array(1) {
645  [0]=>
646  int(1)
647}
648array(1) {
649  [0]=>
650  int(1000)
651}
652array(1) {
653  [0]=>
654  int(-100)
655}
656array(1) {
657  [0]=>
658  int(1000)
659}
660array(1) {
661  [0]=>
662  int(-100)
663}
664array(1) {
665  [0]=>
666  int(10)
667}
668array(1) {
669  [0]=>
670  int(1050)
671}
672bool(false)
673
674-- iteration 11 --
675array(1) {
676  [0]=>
677  int(-2147483648)
678}
679array(1) {
680  [0]=>
681  int(2147483647)
682}
683array(1) {
684  [0]=>
685  int(-2147483648)
686}
687array(1) {
688  [0]=>
689  int(2147483647)
690}
691array(1) {
692  [0]=>
693  int(2147483647)
694}
695array(1) {
696  [0]=>
697  int(-2147483648)
698}
699array(1) {
700  [0]=>
701  int(0)
702}
703array(1) {
704  [0]=>
705  int(0)
706}
707array(1) {
708  [0]=>
709  int(1)
710}
711array(1) {
712  [0]=>
713  int(100000)
714}
715array(1) {
716  [0]=>
717  int(-1000000)
718}
719array(1) {
720  [0]=>
721  int(100000000)
722}
723array(1) {
724  [0]=>
725  int(-1000000000)
726}
727array(1) {
728  [0]=>
729  int(10)
730}
731array(1) {
732  [0]=>
733  int(1050000)
734}
735bool(false)
736
737-- iteration 12 --
738array(1) {
739  [0]=>
740  NULL
741}
742array(1) {
743  [0]=>
744  string(10) "2147483648"
745}
746array(1) {
747  [0]=>
748  NULL
749}
750array(1) {
751  [0]=>
752  string(11) "34359738369"
753}
754array(1) {
755  [0]=>
756  string(10) "2147483649"
757}
758array(1) {
759  [0]=>
760  NULL
761}
762array(1) {
763  [0]=>
764  string(1) "0"
765}
766array(1) {
767  [0]=>
768  NULL
769}
770array(1) {
771  [0]=>
772  string(1) "1"
773}
774array(1) {
775  [0]=>
776  string(6) "100000"
777}
778array(1) {
779  [0]=>
780  NULL
781}
782array(1) {
783  [0]=>
784  string(9) "100000000"
785}
786array(1) {
787  [0]=>
788  NULL
789}
790array(1) {
791  [0]=>
792  string(2) "10"
793}
794array(1) {
795  [0]=>
796  string(7) "1050000"
797}
798bool(false)
799
800-- iteration 13 --
801array(0) {
802}
803array(0) {
804}
805array(0) {
806}
807array(0) {
808}
809array(0) {
810}
811array(0) {
812}
813array(0) {
814}
815array(0) {
816}
817array(0) {
818}
819array(0) {
820}
821array(0) {
822}
823array(0) {
824}
825array(0) {
826}
827array(0) {
828}
829array(0) {
830}
831bool(false)
832
833*** Done ***
834