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