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