]> pd.if.org Git - pdclib/blob - functions/stdio/fscanf.c
Intermediate debugging work on *scanf().
[pdclib] / functions / stdio / fscanf.c
1 /* $Id$ */
2
3 /* fscanf( FILE *, const char *, ... )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10 #include <stdarg.h>
11
12 #ifndef REGTEST
13
14 int fscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... )
15 {
16     int rc;
17     va_list ap;
18     va_start( ap, format );
19     rc = vfscanf( stream, format, ap );
20     va_end( ap );
21     return rc;
22 }
23
24 #endif
25
26 #ifdef TEST
27 #include <_PDCLIB_test.h>
28
29 #include <string.h>
30 #include <limits.h>
31 #include <_PDCLIB_aux.h>
32
33 #if 0
34 #define CHECK_TRUE( a ) do { if ( a == 0 ) { fprintf( stderr, "Unexpected failure in " _PDCLIB_symbol2string( __LINE__ ) ": '" #a "' evaluated to false.\n" ); } } while ( 0 )
35 #define CHECK_FALSE( a ) do { if ( a != 0 ) { fprintf( stderr, "Unexpected failure in " _PDCLIB_symbol2string( __LINE__ ) ": '" #a "' evaluated to true.\n" ); } } while ( 0 )
36 #define CHECK_EQUAL( a, b ) do { int x = a; int y = b; if ( x != y ) { fprintf( stderr, "Mismatch in " _PDCLIB_symbol2string( __LINE__ ) ": result is %d, expected %d.\n", x, y ); } } while ( 0 )
37 #define CHECK_FEQUAL( a, b, T, F ) do { T x = a; T y = b; if ( x != y ) { fprintf( stderr, "Mismatch in " _PDCLIB_symbol2string( __LINE__ ) ": result is " F ", expected " F ".\n", x, y ); } } while ( 0 )
38 #endif
39
40 #define CHECK_TRUE( a ) TESTCASE( a != 0 )
41 #define CHECK_FALSE( a ) TESTCASE( a == 0 )
42 #define CHECK_EQUAL( a, b ) do { int x = a; int y = b; TESTCASE( x == y ); } while ( 0 )
43 #define CHECK_FEQUAL( a, b, T, F ) do { T x = a; T y = b; TESTCASE( x == y ); } while ( 0 )
44
45 // literal matches, character matches, and basic integer matches
46 void suite_one( void );
47 // decimal integer matches
48 void suite_two( void );
49 // hexadecimal integer matches
50 void suite_three( void );
51 // octal integer matches
52 void suite_four( void );
53 // string matches
54 void suite_five( void );
55
56 int main()
57 {
58     suite_one();
59     suite_two();
60     suite_three();
61     suite_four();
62     suite_five();
63 }
64
65 // literal matches, character matches, and basic integer matches
66 void suite_one()
67 {
68     FILE * file;
69     if ( ( file = fopen( "tmpfile", "wb+" ) ) == NULL )
70     {
71         puts( "Failed to open tmpfile for writing." );
72         return;
73     }
74     fprintf( file, "1234567890" );
75     fprintf( file, "1%c3-5+7%c9%c", 0, 0, -1 );
76     fprintf( file, "2 4 6 8 0%c", 255 );
77     fprintf( file, "1 \011 5%%%%  0" );
78     CHECK_EQUAL( ftell( file ), 40 );
79
80     // -----------------------------------------------------------------------
81     // Literal matching
82     // -----------------------------------------------------------------------
83     {
84     // matching six characters literally
85     // checking via %n
86     // should report six characters read
87     fseek( file, 0, SEEK_SET );
88     int n;
89     CHECK_EQUAL( fscanf( file, "123456%n", &n ), 0 );
90     CHECK_EQUAL( n, 6 );
91     CHECK_EQUAL( ftell( file ), 6 );
92     }
93     {
94     // matching a character, three whitespace chars, and another character
95     // checking via %n
96     // should report five characters read
97     fseek( file, 30, SEEK_SET );
98     int n;
99     CHECK_EQUAL( fscanf( file, "1 5%n", &n ), 0 );
100     CHECK_EQUAL( n, 5 );
101     CHECK_EQUAL( ftell( file ), 35 );
102     }
103     {
104     // matching three characters, not matching whitespaces, and matching another three characters
105     // checking via %n
106     // should report six characters matched
107     fseek( file, 0, SEEK_SET );
108     int n;
109     CHECK_EQUAL( fscanf( file, "123  456%n", &n ), 0 );
110     CHECK_EQUAL( n, 6 );
111     CHECK_EQUAL( ftell( file ), 6 );
112     }
113     {
114     // matching a character, two '%' characters, and two whitespaces
115     // checking via %n
116     // should report five characters matched
117     fseek( file, 34, SEEK_SET );
118     int n;
119     CHECK_EQUAL( fscanf( file, "5%%%% %n", &n ), 0 );
120     CHECK_EQUAL( n, 5 );
121     CHECK_EQUAL( ftell( file ), 39 );
122     }
123     {
124     // seeking to last character in file, trying to match that char and a whitespace
125     // checking via %n
126     // should report one character matched and EOF
127     fseek( file, -1, SEEK_END );
128     int n;
129     CHECK_EQUAL( fscanf( file, "0 %n", &n ), 0 );
130     CHECK_EQUAL( n, 1 );
131     CHECK_TRUE( feof( file ) );
132     CHECK_FALSE( ferror( file ) );
133     CHECK_EQUAL( ftell( file ), 40 );
134     }
135     {
136     // seeking to end of file, trying to match a -1
137     // checking via %n
138     // should report error, not executing %n
139     fseek( file, 0, SEEK_END );
140     int n = -1;
141     CHECK_EQUAL( fscanf( file, "\377%n", &n ), -1 );
142     CHECK_EQUAL( n, -1 );
143     CHECK_TRUE( feof( file ) );
144     CHECK_FALSE( ferror( file ) );
145     CHECK_EQUAL( ftell( file ), 40 );
146     }
147
148     // -----------------------------------------------------------------------
149     // Character matching ('%c')
150     // -----------------------------------------------------------------------
151     {
152     // reading a char array of specified width, including zero bytes
153     // should report the characters read, no zero termination of the array
154     fseek( file, 10, SEEK_SET );
155     char buffer[ 8 ];
156     memset( buffer, '\177', 8 );
157     CHECK_EQUAL( fscanf( file, "%7c", buffer ), 1 );
158     CHECK_FALSE( memcmp( buffer, "1\0003-5+7\177", 8 ) );
159     CHECK_EQUAL( ftell( file ), 17 );
160     }
161     {
162     // reading a char array of unspecified width when positioned at -1 value 
163     // should default to width one, read the -1 value, no zero termination of the array
164     fseek( file, 19, SEEK_SET );
165     char buffer[ 2 ];
166     memset( buffer, '\177', 2 );
167     CHECK_EQUAL( fscanf( file, "%c", buffer ), 1 );
168     CHECK_FALSE( memcmp( buffer, "\377\177", 2 ) );
169     CHECK_EQUAL( ftell( file ), 20 );
170     }
171     {
172     // reading a char array of specified width 1 when positioned at (non-space) whitespace
173     // should read the whitespace (literally), no zero termination of the array
174     fseek( file, 32, SEEK_SET );
175     char buffer[ 2 ];
176     memset( buffer, '\177', 2 );
177     CHECK_EQUAL( fscanf( file, "%1c", buffer ), 1 );
178     CHECK_FALSE( memcmp( buffer, "\011\177", 2 ) );
179     CHECK_EQUAL( ftell( file ), 33 );
180     }
181     {
182     // reading a char array of specified width 2 when positioned at last char of file
183     // should read the character, and report EOF
184     fseek( file, -1, SEEK_END );
185     char buffer[ 2 ];
186     memset( buffer, '\177', 2 );
187     CHECK_EQUAL( fscanf( file, "%2c", buffer ), 1 );
188     CHECK_FALSE( memcmp( buffer, "0\177", 2 ) );
189     CHECK_TRUE( feof( file ) );
190     CHECK_FALSE( ferror( file ) );
191     CHECK_EQUAL( ftell( file ), 40 );
192     }
193     {
194     // reading a char array of specified width 1 when positioned at last char of file
195     // should read the character, and NOT report EOF
196     fseek( file, -1, SEEK_END );
197     char buffer[ 2 ];
198     memset( buffer, '\177', 2 );
199     CHECK_EQUAL( fscanf( file, "%1c", buffer ), 1 );
200     CHECK_FALSE( memcmp( buffer, "0\177", 2 ) );
201     CHECK_FALSE( feof( file ) );
202     CHECK_EQUAL( ftell( file ), 40 );
203     }
204     {
205     // reading a char array of specified width 1 when positioned at EOF
206     // should report input error before any conversion (-1)
207     fseek( file, 0, SEEK_END );
208     char buffer[ 2 ];
209     memset( buffer, '\177', 2 );
210     CHECK_EQUAL( fscanf( file, "%1c", buffer ), -1 );
211     CHECK_FALSE( memcmp( buffer, "\177\177", 2 ) );
212     CHECK_TRUE( feof( file ) );
213     CHECK_FALSE( ferror( file ) );
214     CHECK_EQUAL( ftell( file ), 40 );
215     }
216
217     // -----------------------------------------------------------------------
218     // Integer matching ('%d')
219     // -----------------------------------------------------------------------
220     {
221     // reading a whitespace-terminated integer
222     fseek( file, 20, SEEK_SET );
223     int i;
224     int n;
225     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
226     CHECK_EQUAL( i, 2 );
227     CHECK_EQUAL( n, 1 );
228     CHECK_EQUAL( ftell( file ), 21 );
229     }
230     {
231     // reading a -1 terminated integer
232     fseek( file, 18, SEEK_SET );
233     int i;
234     int n;
235     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
236     CHECK_EQUAL( i, 9 );
237     CHECK_EQUAL( n, 1 );
238     CHECK_EQUAL( ftell( file ), 19 );
239     }
240     {
241     // reading a EOF terminated integer
242     fseek( file, -1, SEEK_END );
243     int i = -1;
244     int n;
245     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
246     CHECK_EQUAL( i, 0 );
247     CHECK_EQUAL( n, 1 );
248     CHECK_EQUAL( ftell( file ), 40 );
249     }
250     {
251     // trying to read an integer when positioned at whitespace
252     // should skip whitespaces
253     fseek( file, 32, SEEK_SET );
254     int i = -1;
255     int n;
256     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
257     CHECK_EQUAL( i, 5 );
258     CHECK_EQUAL( n, 3 );
259     CHECK_EQUAL( ftell( file ), 35 );
260     }
261     {
262     // trying to read an integer when positioned at -1 value
263     // should report matching failure
264     fseek( file, 19, SEEK_SET );
265     int i = 0;
266     int n = -1;
267     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 0 );
268     CHECK_EQUAL( i, 0 );
269     CHECK_EQUAL( n, -1 );
270     CHECK_EQUAL( ftell( file ), 19 );
271     }
272     {
273     // trying to read an integer when positioned at EOF
274     // should report reading failure
275     fseek( file, 0, SEEK_END );
276     int i = 0;
277     int n = -1;
278     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), -1 );
279     CHECK_EQUAL( i, 0 );
280     CHECK_EQUAL( n, -1 );
281     CHECK_EQUAL( ftell( file ), 40 );
282     }
283     {
284     // reading a '-'-prefixed integer
285     fseek( file, 13, SEEK_SET );
286     int i;
287     int n;
288     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
289     CHECK_EQUAL( i, -5 );
290     CHECK_EQUAL( n, 2 );
291     CHECK_EQUAL( ftell( file ), 15 );
292     }
293     {
294     // reading a '+'-prefixed integer
295     fseek( file, 15, SEEK_SET );
296     int i;
297     int n;
298     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
299     CHECK_EQUAL( i, 7 );
300     CHECK_EQUAL( n, 2 );
301     CHECK_EQUAL( ftell( file ), 17 ); 
302     }
303
304     fclose( file );
305     remove( "tmpfile" );
306 }
307
308 // decimal integer matches
309 void suite_two()
310 {
311     FILE * file;
312     if ( ( file = fopen( "tmpfile", "wb+" ) ) == NULL )
313     {
314         puts( "Failed to open tmpfile for writing." );
315         return;
316     }
317     fprintf( file, "-0 +0 -128 +127 +255 -32768 +32767 +65535\n" );
318     fprintf( file, "-2147483648 +2147483647 +4294967295\n" );
319     fprintf( file, "-9223372036854775808 +9223372036854775807 +18446744073709551615\n" );
320     CHECK_EQUAL( ftell( file ), 142 );
321     {
322     // reading 0, d
323     fseek( file, 1, SEEK_SET );
324     signed char i = -1;
325     int n;
326     CHECK_EQUAL( fscanf( file, "%hhd%n", &i, &n ), 1 );
327     CHECK_EQUAL( i, 0 );
328     CHECK_EQUAL( n, 1 );
329     CHECK_EQUAL( ftell( file ), 2 );
330     }
331     {
332     // reading -0, d
333     fseek( file, 0, SEEK_SET );
334     signed char i = -1;
335     int n;
336     CHECK_EQUAL( fscanf( file, "%hhd%n", &i, &n ), 1 );
337     CHECK_EQUAL( i, 0 );
338     CHECK_EQUAL( n, 2 );
339     CHECK_EQUAL( ftell( file ), 2 );
340     }
341     {
342     // reading +0, d
343     fseek( file, 3, SEEK_SET );
344     signed char i = -1;
345     int n;
346     CHECK_EQUAL( fscanf( file, "%hhd%n", &i, &n ), 1 );
347     CHECK_EQUAL( i, 0 );
348     CHECK_EQUAL( n, 2 );
349     CHECK_EQUAL( ftell( file ), 5 );
350     }
351     {
352     // reading -128, d
353     fseek( file, 6, SEEK_SET );
354     signed char i = -1;
355     int n;
356     CHECK_EQUAL( fscanf( file, "%hhd%n", &i, &n ), 1 );
357     CHECK_EQUAL( i, -128 );
358     CHECK_EQUAL( n, 4 );
359     CHECK_EQUAL( ftell( file ), 10 );
360     }
361     {
362     // reading 127, d
363     fseek( file, 12, SEEK_SET );
364     signed char i = -1;
365     int n;
366     CHECK_EQUAL( fscanf( file, "%hhd%n", &i, &n ), 1 );
367     CHECK_EQUAL( i, 127 );
368     CHECK_EQUAL( n, 3 );
369     CHECK_EQUAL( ftell( file ), 15 );
370     }
371     {
372     // reading +127, d
373     fseek( file, 11, SEEK_SET );
374     signed char i = -1;
375     int n;
376     CHECK_EQUAL( fscanf( file, "%hhd%n", &i, &n ), 1 );
377     CHECK_EQUAL( i, 127 );
378     CHECK_EQUAL( n, 4 );
379     CHECK_EQUAL( ftell( file ), 15 );
380     }
381     {
382     // reading 0, u
383     fseek( file, 1, SEEK_SET );
384     unsigned char i = -1;
385     int n;
386     CHECK_EQUAL( fscanf( file, "%hhu%n", &i, &n ), 1 );
387     CHECK_EQUAL( i, 0 );
388     CHECK_EQUAL( n, 1 );
389     CHECK_EQUAL( ftell( file ), 2 );
390     }
391     {
392     // reading -0, u
393     fseek( file, 0, SEEK_SET );
394     unsigned char i = -1;
395     int n;
396     CHECK_EQUAL( fscanf( file, "%hhu%n", &i, &n ), 1 );
397     CHECK_EQUAL( i, 0 );
398     CHECK_EQUAL( n, 2 );
399     CHECK_EQUAL( ftell( file ), 2 );
400     }
401     {
402     // reading +0, u
403     fseek( file, 3, SEEK_SET );
404     unsigned char i = -1;
405     int n;
406     CHECK_EQUAL( fscanf( file, "%hhu%n", &i, &n ), 1 );
407     CHECK_EQUAL( i, 0 );
408     CHECK_EQUAL( n, 2 );
409     CHECK_EQUAL( ftell( file ), 5 );
410     }
411     {
412     // reading 127, u
413     fseek( file, 12, SEEK_SET );
414     unsigned char i = -1;
415     int n;
416     CHECK_EQUAL( fscanf( file, "%hhu%n", &i, &n ), 1 );
417     CHECK_EQUAL( i, 127 );
418     CHECK_EQUAL( n, 3 );
419     CHECK_EQUAL( ftell( file ), 15 );
420     }
421     {
422     // reading +127, u
423     fseek( file, 11, SEEK_SET );
424     unsigned char i = -1;
425     int n;
426     CHECK_EQUAL( fscanf( file, "%hhu%n", &i, &n ), 1 );
427     CHECK_EQUAL( i, 127 );
428     CHECK_EQUAL( n, 4 );
429     CHECK_EQUAL( ftell( file ), 15 );
430     }
431     {
432     // reading 255, u
433     fseek( file, 17, SEEK_SET );
434     unsigned char i = 0;
435     int n;
436     CHECK_EQUAL( fscanf( file, "%hhu%n", &i, &n ), 1 );
437     CHECK_EQUAL( i, 255 );
438     CHECK_EQUAL( n, 3 );
439     CHECK_EQUAL( ftell( file ), 20 );
440     }
441     {
442     // reading +255, u
443     fseek( file, 16, SEEK_SET );
444     unsigned char i = 0;
445     int n;
446     CHECK_EQUAL( fscanf( file, "%hhu%n", &i, &n ), 1 );
447     CHECK_EQUAL( i, 255 );
448     CHECK_EQUAL( n, 4 );
449     CHECK_EQUAL( ftell( file ), 20 );
450     }
451     {
452     // reading 0, i
453     fseek( file, 1, SEEK_SET );
454     signed char i = -1;
455     int n;
456     CHECK_EQUAL( fscanf( file, "%hhi%n", &i, &n ), 1 );
457     CHECK_EQUAL( i, 0 );
458     CHECK_EQUAL( n, 1 );
459     CHECK_EQUAL( ftell( file ), 2 );
460     }
461     {
462     // reading -0, i
463     fseek( file, 0, SEEK_SET );
464     signed char i = -1;
465     int n;
466     CHECK_EQUAL( fscanf( file, "%hhi%n", &i, &n ), 1 );
467     CHECK_EQUAL( i, 0 );
468     CHECK_EQUAL( n, 2 );
469     CHECK_EQUAL( ftell( file ), 2 );
470     }
471     {
472     // reading +0, i
473     fseek( file, 3, SEEK_SET );
474     signed char i = -1;
475     int n;
476     CHECK_EQUAL( fscanf( file, "%hhi%n", &i, &n ), 1 );
477     CHECK_EQUAL( i, 0 );
478     CHECK_EQUAL( n, 2 );
479     CHECK_EQUAL( ftell( file ), 5 );
480     }
481     {
482     // reading -128, i
483     fseek( file, 6, SEEK_SET );
484     signed char i = -1;
485     int n;
486     CHECK_EQUAL( fscanf( file, "%hhi%n", &i, &n ), 1 );
487     CHECK_EQUAL( i, -128 );
488     CHECK_EQUAL( n, 4 );
489     CHECK_EQUAL( ftell( file ), 10 );
490     }
491     {
492     // reading 127, i
493     fseek( file, 12, SEEK_SET );
494     signed char i = -1;
495     int n;
496     CHECK_EQUAL( fscanf( file, "%hhi%n", &i, &n ), 1 );
497     CHECK_EQUAL( i, 127 );
498     CHECK_EQUAL( n, 3 );
499     CHECK_EQUAL( ftell( file ), 15 );
500     }
501     {
502     // reading +127, i
503     fseek( file, 11, SEEK_SET );
504     signed char i = -1;
505     int n;
506     CHECK_EQUAL( fscanf( file, "%hhi%n", &i, &n ), 1 );
507     CHECK_EQUAL( i, 127 );
508     CHECK_EQUAL( n, 4 );
509     CHECK_EQUAL( ftell( file ), 15 );
510     }
511     {
512     // reading 0, d
513     fseek( file, 1, SEEK_SET );
514     signed short i = -1;
515     int n;
516     CHECK_EQUAL( fscanf( file, "%hd%n", &i, &n ), 1 );
517     CHECK_EQUAL( i, 0 );
518     CHECK_EQUAL( n, 1 );
519     CHECK_EQUAL( ftell( file ), 2 );
520     }
521     {
522     // reading -0, d
523     fseek( file, 0, SEEK_SET );
524     signed short i = -1;
525     int n;
526     CHECK_EQUAL( fscanf( file, "%hd%n", &i, &n ), 1 );
527     CHECK_EQUAL( i, 0 );
528     CHECK_EQUAL( n, 2 );
529     CHECK_EQUAL( ftell( file ), 2 );
530     }
531     {
532     // reading +0, d
533     fseek( file, 3, SEEK_SET );
534     signed short i = -1;
535     int n;
536     CHECK_EQUAL( fscanf( file, "%hd%n", &i, &n ), 1 );
537     CHECK_EQUAL( i, 0 );
538     CHECK_EQUAL( n, 2 );
539     CHECK_EQUAL( ftell( file ), 5 );
540     }
541     {
542     // reading -32768, d
543     fseek( file, 21, SEEK_SET );
544     signed short i = -1;
545     int n;
546     CHECK_EQUAL( fscanf( file, "%hd%n", &i, &n ), 1 );
547     CHECK_EQUAL( i, -32768 );
548     CHECK_EQUAL( n, 6 );
549     CHECK_EQUAL( ftell( file ), 27 );
550     }
551     {
552     // reading 32767, d
553     fseek( file, 29, SEEK_SET );
554     signed short i = -1;
555     int n;
556     CHECK_EQUAL( fscanf( file, "%hd%n", &i, &n ), 1 );
557     CHECK_EQUAL( i, 32767 );
558     CHECK_EQUAL( n, 5 );
559     CHECK_EQUAL( ftell( file ), 34 );
560     }
561     {
562     // reading +32767, d
563     fseek( file, 28, SEEK_SET );
564     signed short i = -1;
565     int n;
566     CHECK_EQUAL( fscanf( file, "%hd%n", &i, &n ), 1 );
567     CHECK_EQUAL( i, 32767 );
568     CHECK_EQUAL( n, 6 );
569     CHECK_EQUAL( ftell( file ), 34 );
570     }
571     {
572     // reading 0, u
573     fseek( file, 1, SEEK_SET );
574     unsigned short i = -1;
575     int n;
576     CHECK_EQUAL( fscanf( file, "%hu%n", &i, &n ), 1 );
577     CHECK_EQUAL( i, 0 );
578     CHECK_EQUAL( n, 1 );
579     CHECK_EQUAL( ftell( file ), 2 );
580     }
581     {
582     // reading -0, u
583     fseek( file, 0, SEEK_SET );
584     unsigned short i = -1;
585     int n;
586     CHECK_EQUAL( fscanf( file, "%hu%n", &i, &n ), 1 );
587     CHECK_EQUAL( i, 0 );
588     CHECK_EQUAL( n, 2 );
589     CHECK_EQUAL( ftell( file ), 2 );
590     }
591     {
592     // reading +0, u
593     fseek( file, 3, SEEK_SET );
594     unsigned short i = -1;
595     int n;
596     CHECK_EQUAL( fscanf( file, "%hu%n", &i, &n ), 1 );
597     CHECK_EQUAL( i, 0 );
598     CHECK_EQUAL( n, 2 );
599     CHECK_EQUAL( ftell( file ), 5 );
600     }
601     {
602     // reading 32767, u
603     fseek( file, 29, SEEK_SET );
604     unsigned short i = -1;
605     int n;
606     CHECK_EQUAL( fscanf( file, "%hu%n", &i, &n ), 1 );
607     CHECK_EQUAL( i, 32767 );
608     CHECK_EQUAL( n, 5 );
609     CHECK_EQUAL( ftell( file ), 34 );
610     }
611     {
612     // reading +32767, u
613     fseek( file, 28, SEEK_SET );
614     unsigned short i = -1;
615     int n;
616     CHECK_EQUAL( fscanf( file, "%hu%n", &i, &n ), 1 );
617     CHECK_EQUAL( i, 32767 );
618     CHECK_EQUAL( n, 6 );
619     CHECK_EQUAL( ftell( file ), 34 );
620     }
621     {
622     // reading 65535, u
623     fseek( file, 36, SEEK_SET );
624     unsigned short i = 0;
625     int n;
626     CHECK_EQUAL( fscanf( file, "%hu%n", &i, &n ), 1 );
627     CHECK_EQUAL( i, 65535 );
628     CHECK_EQUAL( n, 5 );
629     CHECK_EQUAL( ftell( file ), 41 );
630     }
631     {
632     // reading +65535, u
633     fseek( file, 35, SEEK_SET );
634     unsigned short i = 0;
635     int n;
636     CHECK_EQUAL( fscanf( file, "%hu%n", &i, &n ), 1 );
637     CHECK_EQUAL( i, 65535 );
638     CHECK_EQUAL( n, 6 );
639     CHECK_EQUAL( ftell( file ), 41 );
640     }
641     {
642     // reading 0, i
643     fseek( file, 1, SEEK_SET );
644     signed short i = -1;
645     int n;
646     CHECK_EQUAL( fscanf( file, "%hi%n", &i, &n ), 1 );
647     CHECK_EQUAL( i, 0 );
648     CHECK_EQUAL( n, 1 );
649     CHECK_EQUAL( ftell( file ), 2 );
650     }
651     {
652     // reading -0, i
653     fseek( file, 0, SEEK_SET );
654     signed short i = -1;
655     int n;
656     CHECK_EQUAL( fscanf( file, "%hi%n", &i, &n ), 1 );
657     CHECK_EQUAL( i, 0 );
658     CHECK_EQUAL( n, 2 );
659     CHECK_EQUAL( ftell( file ), 2 );
660     }
661     {
662     // reading +0, i
663     fseek( file, 3, SEEK_SET );
664     signed short i = -1;
665     int n;
666     CHECK_EQUAL( fscanf( file, "%hi%n", &i, &n ), 1 );
667     CHECK_EQUAL( i, 0 );
668     CHECK_EQUAL( n, 2 );
669     CHECK_EQUAL( ftell( file ), 5 );
670     }
671     {
672     // reading -32768, i
673     fseek( file, 21, SEEK_SET );
674     signed short i = -1;
675     int n;
676     CHECK_EQUAL( fscanf( file, "%hi%n", &i, &n ), 1 );
677     CHECK_EQUAL( i, -32768 );
678     CHECK_EQUAL( n, 6 );
679     CHECK_EQUAL( ftell( file ), 27 );
680     }
681     {
682     // reading 32767, i
683     fseek( file, 29, SEEK_SET );
684     signed short i = -1;
685     int n;
686     CHECK_EQUAL( fscanf( file, "%hi%n", &i, &n ), 1 );
687     CHECK_EQUAL( i, 32767 );
688     CHECK_EQUAL( n, 5 );
689     CHECK_EQUAL( ftell( file ), 34 );
690     }
691     {
692     // reading +32767, i
693     fseek( file, 28, SEEK_SET );
694     signed short i = -1;
695     int n;
696     CHECK_EQUAL( fscanf( file, "%hi%n", &i, &n ), 1 );
697     CHECK_EQUAL( i, 32767 );
698     CHECK_EQUAL( n, 6 );
699     CHECK_EQUAL( ftell( file ), 34 );
700     }
701     {
702     // reading 0, d
703     fseek( file, 1, SEEK_SET );
704     signed int i = -1;
705     int n;
706     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
707     CHECK_EQUAL( i, 0 );
708     CHECK_EQUAL( n, 1 );
709     CHECK_EQUAL( ftell( file ), 2 );
710     }
711     {
712     // reading -0, d
713     fseek( file, 0, SEEK_SET );
714     signed int i = -1;
715     int n;
716     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
717     CHECK_EQUAL( i, 0 );
718     CHECK_EQUAL( n, 2 );
719     CHECK_EQUAL( ftell( file ), 2 );
720     }
721     {
722     // reading +0, d
723     fseek( file, 3, SEEK_SET );
724     signed int i = -1;
725     int n;
726     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
727     CHECK_EQUAL( i, 0 );
728     CHECK_EQUAL( n, 2 );
729     CHECK_EQUAL( ftell( file ), 5 );
730     }
731     {
732     // reading -2147483648, d
733     fseek( file, 42, SEEK_SET );
734     signed int i = -1;
735     int n;
736     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
737     CHECK_EQUAL( i, -2147483648 );
738     CHECK_EQUAL( n, 11 );
739     CHECK_EQUAL( ftell( file ), 53 );
740     }
741     {
742     // reading 2147483647, d
743     fseek( file, 55, SEEK_SET );
744     signed int i = -1;
745     int n;
746     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
747     CHECK_EQUAL( i, 2147483647 );
748     CHECK_EQUAL( n, 10 );
749     CHECK_EQUAL( ftell( file ), 65 );
750     }
751     {
752     // reading +2147483647, d
753     fseek( file, 54, SEEK_SET );
754     signed int i = -1;
755     int n;
756     CHECK_EQUAL( fscanf( file, "%d%n", &i, &n ), 1 );
757     CHECK_EQUAL( i, 2147483647 );
758     CHECK_EQUAL( n, 11 );
759     CHECK_EQUAL( ftell( file ), 65 );
760     }
761     {
762     // reading 0, u
763     fseek( file, 1, SEEK_SET );
764     unsigned int i = -1;
765     int n;
766     CHECK_EQUAL( fscanf( file, "%u%n", &i, &n ), 1 );
767     CHECK_EQUAL( i, 0 );
768     CHECK_EQUAL( n, 1 );
769     CHECK_EQUAL( ftell( file ), 2 );
770     }
771     {
772     // reading -0, u
773     fseek( file, 0, SEEK_SET );
774     unsigned int i = -1;
775     int n;
776     CHECK_EQUAL( fscanf( file, "%u%n", &i, &n ), 1 );
777     CHECK_EQUAL( i, 0 );
778     CHECK_EQUAL( n, 2 );
779     CHECK_EQUAL( ftell( file ), 2 );
780     }
781     {
782     // reading +0, u
783     fseek( file, 3, SEEK_SET );
784     unsigned int i = -1;
785     int n;
786     CHECK_EQUAL( fscanf( file, "%u%n", &i, &n ), 1 );
787     CHECK_EQUAL( i, 0 );
788     CHECK_EQUAL( n, 2 );
789     CHECK_EQUAL( ftell( file ), 5 );
790     }
791     {
792     // reading 2147483647, u
793     fseek( file, 55, SEEK_SET );
794     unsigned int i = -1;
795     int n;
796     CHECK_EQUAL( fscanf( file, "%u%n", &i, &n ), 1 );
797     CHECK_EQUAL( i, 2147483647 );
798     CHECK_EQUAL( n, 10 );
799     CHECK_EQUAL( ftell( file ), 65 );
800     }
801     {
802     // reading +2147483647, u
803     fseek( file, 54, SEEK_SET );
804     unsigned int i = -1;
805     int n;
806     CHECK_EQUAL( fscanf( file, "%u%n", &i, &n ), 1 );
807     CHECK_EQUAL( i, 2147483647 );
808     CHECK_EQUAL( n, 11 );
809     CHECK_EQUAL( ftell( file ), 65 );
810     }
811     {
812     // reading 4294967295, u
813     fseek( file, 67, SEEK_SET );
814     unsigned int i = 0;
815     int n;
816     CHECK_EQUAL( fscanf( file, "%u%n", &i, &n ), 1 );
817     CHECK_FEQUAL( i, 4294967295, unsigned int, "%u" );
818     CHECK_EQUAL( n, 10 );
819     CHECK_EQUAL( ftell( file ), 77 );
820     }
821     {
822     // reading +4294967295, u
823     fseek( file, 66, SEEK_SET );
824     unsigned int i = 0;
825     int n;
826     CHECK_EQUAL( fscanf( file, "%u%n", &i, &n ), 1 );
827     CHECK_FEQUAL( i, 4294967295, unsigned int, "%u" );
828     CHECK_EQUAL( n, 11 );
829     CHECK_EQUAL( ftell( file ), 77 );
830     }
831     {
832     // reading 0, i
833     fseek( file, 1, SEEK_SET );
834     signed int i = -1;
835     int n;
836     CHECK_EQUAL( fscanf( file, "%i%n", &i, &n ), 1 );
837     CHECK_EQUAL( i, 0 );
838     CHECK_EQUAL( n, 1 );
839     CHECK_EQUAL( ftell( file ), 2 );
840     }
841     {
842     // reading -0, i
843     fseek( file, 0, SEEK_SET );
844     signed int i = -1;
845     int n;
846     CHECK_EQUAL( fscanf( file, "%i%n", &i, &n ), 1 );
847     CHECK_EQUAL( i, 0 );
848     CHECK_EQUAL( n, 2 );
849     CHECK_EQUAL( ftell( file ), 2 );
850     }
851     {
852     // reading +0, i
853     fseek( file, 3, SEEK_SET );
854     signed int i = -1;
855     int n;
856     CHECK_EQUAL( fscanf( file, "%i%n", &i, &n ), 1 );
857     CHECK_EQUAL( i, 0 );
858     CHECK_EQUAL( n, 2 );
859     CHECK_EQUAL( ftell( file ), 5 );
860     }
861     {
862     // reading -2147483648, i
863     fseek( file, 42, SEEK_SET );
864     signed int i = -1;
865     int n;
866     CHECK_EQUAL( fscanf( file, "%i%n", &i, &n ), 1 );
867     CHECK_EQUAL( i, -2147483648 );
868     CHECK_EQUAL( n, 11 );
869     CHECK_EQUAL( ftell( file ), 53 );
870     }
871     {
872     // reading 2147483647, i
873     fseek( file, 55, SEEK_SET );
874     signed int i = -1;
875     int n;
876     CHECK_EQUAL( fscanf( file, "%i%n", &i, &n ), 1 );
877     CHECK_EQUAL( i, 2147483647 );
878     CHECK_EQUAL( n, 10 );
879     CHECK_EQUAL( ftell( file ), 65 );
880     }
881     {
882     // reading +2147483647, i
883     fseek( file, 54, SEEK_SET );
884     signed int i = -1;
885     int n;
886     CHECK_EQUAL( fscanf( file, "%i%n", &i, &n ), 1 );
887     CHECK_EQUAL( i, 2147483647 );
888     CHECK_EQUAL( n, 11 );
889     CHECK_EQUAL( ftell( file ), 65 );
890     }
891     {
892     // reading 0, d
893     fseek( file, 1, SEEK_SET );
894     signed long i = -1;
895     int n;
896     CHECK_EQUAL( fscanf( file, "%ld%n", &i, &n ), 1 );
897     CHECK_EQUAL( i, 0l );
898     CHECK_EQUAL( n, 1 );
899     CHECK_EQUAL( ftell( file ), 2 );
900     }
901     {
902     // reading -0, d
903     fseek( file, 0, SEEK_SET );
904     signed long i = -1;
905     int n;
906     CHECK_EQUAL( fscanf( file, "%ld%n", &i, &n ), 1 );
907     CHECK_EQUAL( i, 0l );
908     CHECK_EQUAL( n, 2 );
909     CHECK_EQUAL( ftell( file ), 2 );
910     }
911     {
912     // reading +0, d
913     fseek( file, 3, SEEK_SET );
914     signed long i = -1;
915     int n;
916     CHECK_EQUAL( fscanf( file, "%ld%n", &i, &n ), 1 );
917     CHECK_EQUAL( i, 0l );
918     CHECK_EQUAL( n, 2 );
919     CHECK_EQUAL( ftell( file ), 5 );
920     }
921     {
922     // reading -2147483648, d
923     fseek( file, 42, SEEK_SET );
924     signed long i = -1;
925     int n;
926     CHECK_EQUAL( fscanf( file, "%ld%n", &i, &n ), 1 );
927     CHECK_EQUAL( i, -2147483648l );
928     CHECK_EQUAL( n, 11 );
929     CHECK_EQUAL( ftell( file ), 53 );
930     }
931     {
932     // reading 2147483647, d
933     fseek( file, 55, SEEK_SET );
934     signed long i = -1;
935     int n;
936     CHECK_EQUAL( fscanf( file, "%ld%n", &i, &n ), 1 );
937     CHECK_EQUAL( i, 2147483647l );
938     CHECK_EQUAL( n, 10 );
939     CHECK_EQUAL( ftell( file ), 65 );
940     }
941     {
942     // reading +2147483647, d
943     fseek( file, 54, SEEK_SET );
944     signed long i = -1;
945     int n;
946     CHECK_EQUAL( fscanf( file, "%ld%n", &i, &n ), 1 );
947     CHECK_EQUAL( i, 2147483647l );
948     CHECK_EQUAL( n, 11 );
949     CHECK_EQUAL( ftell( file ), 65 );
950     }
951     {
952     // reading 0, u
953     fseek( file, 1, SEEK_SET );
954     unsigned long i = -1;
955     int n;
956     CHECK_EQUAL( fscanf( file, "%lu%n", &i, &n ), 1 );
957     CHECK_EQUAL( i, 0ul );
958     CHECK_EQUAL( n, 1 );
959     CHECK_EQUAL( ftell( file ), 2 );
960     }
961     {
962     // reading -0, u
963     fseek( file, 0, SEEK_SET );
964     unsigned long i = -1;
965     int n;
966     CHECK_EQUAL( fscanf( file, "%lu%n", &i, &n ), 1 );
967     CHECK_EQUAL( i, 0ul );
968     CHECK_EQUAL( n, 2 );
969     CHECK_EQUAL( ftell( file ), 2 );
970     }
971     {
972     // reading +0, u
973     fseek( file, 3, SEEK_SET );
974     unsigned long i = -1;
975     int n;
976     CHECK_EQUAL( fscanf( file, "%lu%n", &i, &n ), 1 );
977     CHECK_EQUAL( i, 0ul );
978     CHECK_EQUAL( n, 2 );
979     CHECK_EQUAL( ftell( file ), 5 );
980     }
981     {
982     // reading 2147483647, u
983     fseek( file, 55, SEEK_SET );
984     unsigned long i = -1;
985     int n;
986     CHECK_EQUAL( fscanf( file, "%lu%n", &i, &n ), 1 );
987     CHECK_EQUAL( i, 2147483647ul );
988     CHECK_EQUAL( n, 10 );
989     CHECK_EQUAL( ftell( file ), 65 );
990     }
991     {
992     // reading +2147483647, u
993     fseek( file, 54, SEEK_SET );
994     unsigned long i = -1;
995     int n;
996     CHECK_EQUAL( fscanf( file, "%lu%n", &i, &n ), 1 );
997     CHECK_EQUAL( i, 2147483647ul );
998     CHECK_EQUAL( n, 11 );
999     CHECK_EQUAL( ftell( file ), 65 );
1000     }
1001     {
1002     // reading 4294967295, u
1003     fseek( file, 67, SEEK_SET );
1004     unsigned long i = 0;
1005     int n;
1006     CHECK_EQUAL( fscanf( file, "%lu%n", &i, &n ), 1 );
1007     CHECK_FEQUAL( i, 4294967295ul, unsigned long, "%lu" );
1008     CHECK_EQUAL( n, 10 );
1009     CHECK_EQUAL( ftell( file ), 77 );
1010     }
1011     {
1012     // reading +4294967295, u
1013     fseek( file, 66, SEEK_SET );
1014     unsigned long i = 0;
1015     int n;
1016     CHECK_EQUAL( fscanf( file, "%lu%n", &i, &n ), 1 );
1017     CHECK_FEQUAL( i, 4294967295ul, unsigned long, "%lu" );
1018     CHECK_EQUAL( n, 11 );
1019     CHECK_EQUAL( ftell( file ), 77 );
1020     }
1021     {
1022     // reading 0, i
1023     fseek( file, 1, SEEK_SET );
1024     signed long i = -1;
1025     int n;
1026     CHECK_EQUAL( fscanf( file, "%li%n", &i, &n ), 1 );
1027     CHECK_EQUAL( i, 0l );
1028     CHECK_EQUAL( n, 1 );
1029     CHECK_EQUAL( ftell( file ), 2 );
1030     }
1031     {
1032     // reading -0, i
1033     fseek( file, 0, SEEK_SET );
1034     signed long i = -1;
1035     int n;
1036     CHECK_EQUAL( fscanf( file, "%li%n", &i, &n ), 1 );
1037     CHECK_EQUAL( i, 0l );
1038     CHECK_EQUAL( n, 2 );
1039     CHECK_EQUAL( ftell( file ), 2 );
1040     }
1041     {
1042     // reading +0, i
1043     fseek( file, 3, SEEK_SET );
1044     signed long i = -1;
1045     int n;
1046     CHECK_EQUAL( fscanf( file, "%li%n", &i, &n ), 1 );
1047     CHECK_EQUAL( i, 0l );
1048     CHECK_EQUAL( n, 2 );
1049     CHECK_EQUAL( ftell( file ), 5 );
1050     }
1051     {
1052     // reading -2147483648, i
1053     fseek( file, 42, SEEK_SET );
1054     signed long i = -1;
1055     int n;
1056     CHECK_EQUAL( fscanf( file, "%li%n", &i, &n ), 1 );
1057     CHECK_EQUAL( i, -2147483648l );
1058     CHECK_EQUAL( n, 11 );
1059     CHECK_EQUAL( ftell( file ), 53 );
1060     }
1061     {
1062     // reading 2147483647, i
1063     fseek( file, 55, SEEK_SET );
1064     signed long i = -1;
1065     int n;
1066     CHECK_EQUAL( fscanf( file, "%li%n", &i, &n ), 1 );
1067     CHECK_EQUAL( i, 2147483647l );
1068     CHECK_EQUAL( n, 10 );
1069     CHECK_EQUAL( ftell( file ), 65 );
1070     }
1071     {
1072     // reading +2147483647, i
1073     fseek( file, 54, SEEK_SET );
1074     signed long i = -1;
1075     int n;
1076     CHECK_EQUAL( fscanf( file, "%li%n", &i, &n ), 1 );
1077     CHECK_EQUAL( i, 2147483647l );
1078     CHECK_EQUAL( n, 11 );
1079     CHECK_EQUAL( ftell( file ), 65 );
1080     }
1081     {
1082     // reading 0, d
1083     fseek( file, 1, SEEK_SET );
1084     signed long long i = -1;
1085     int n;
1086     CHECK_EQUAL( fscanf( file, "%lld%n", &i, &n ), 1 );
1087     CHECK_EQUAL( i, 0ll );
1088     CHECK_EQUAL( n, 1 );
1089     CHECK_EQUAL( ftell( file ), 2 );
1090     }
1091     {
1092     // reading -0, d
1093     fseek( file, 0, SEEK_SET );
1094     signed long long i = -1;
1095     int n;
1096     CHECK_EQUAL( fscanf( file, "%lld%n", &i, &n ), 1 );
1097     CHECK_EQUAL( i, 0ll );
1098     CHECK_EQUAL( n, 2 );
1099     CHECK_EQUAL( ftell( file ), 2 );
1100     }
1101     {
1102     // reading +0, d
1103     fseek( file, 3, SEEK_SET );
1104     signed long long i = -1;
1105     int n;
1106     CHECK_EQUAL( fscanf( file, "%lld%n", &i, &n ), 1 );
1107     CHECK_EQUAL( i, 0ll );
1108     CHECK_EQUAL( n, 2 );
1109     CHECK_EQUAL( ftell( file ), 5 );
1110     }
1111     {
1112     // reading -9223372036854775808, d
1113     fseek( file, 78, SEEK_SET );
1114     signed long long i = -1;
1115     int n;
1116     CHECK_EQUAL( fscanf( file, "%lld%n", &i, &n ), 1 );
1117     CHECK_FEQUAL( i, LLONG_MIN, signed long long, "%lld" ); // should be literal -9223372036854775808ll but GCC balks.
1118     CHECK_EQUAL( i < 0ll, 1 );
1119     CHECK_EQUAL( n, 20 );
1120     CHECK_EQUAL( ftell( file ), 98 );
1121     }
1122     {
1123     // reading 9223372036854775807, d
1124     fseek( file, 100, SEEK_SET );
1125     signed long long i = -1;
1126     int n;
1127     CHECK_EQUAL( fscanf( file, "%lld%n", &i, &n ), 1 );
1128     CHECK_FEQUAL( i, 9223372036854775807ll, signed long long, "%lld" );
1129     CHECK_EQUAL( n, 19 );
1130     CHECK_EQUAL( ftell( file ), 119 );
1131     }
1132     {
1133     // reading +9223372036854775807, d
1134     fseek( file, 99, SEEK_SET );
1135     signed long long i = -1;
1136     int n;
1137     CHECK_EQUAL( fscanf( file, "%lld%n", &i, &n ), 1 );
1138     CHECK_FEQUAL( i, 9223372036854775807ll, signed long long, "%lld" );
1139     CHECK_EQUAL( n, 20 );
1140     CHECK_EQUAL( ftell( file ), 119 );
1141     }
1142     {
1143     // reading 0, u
1144     fseek( file, 1, SEEK_SET );
1145     unsigned long long i = -1;
1146     int n;
1147     CHECK_EQUAL( fscanf( file, "%llu%n", &i, &n ), 1 );
1148     CHECK_EQUAL( i, 0ull );
1149     CHECK_EQUAL( n, 1 );
1150     CHECK_EQUAL( ftell( file ), 2 );
1151     }
1152     {
1153     // reading -0, u
1154     fseek( file, 0, SEEK_SET );
1155     unsigned long long i = -1;
1156     int n;
1157     CHECK_EQUAL( fscanf( file, "%llu%n", &i, &n ), 1 );
1158     CHECK_EQUAL( i, 0ull );
1159     CHECK_EQUAL( n, 2 );
1160     CHECK_EQUAL( ftell( file ), 2 );
1161     }
1162     {
1163     // reading +0, u
1164     fseek( file, 3, SEEK_SET );
1165     unsigned long long i = -1;
1166     int n;
1167     CHECK_EQUAL( fscanf( file, "%llu%n", &i, &n ), 1 );
1168     CHECK_EQUAL( i, 0ull );
1169     CHECK_EQUAL( n, 2 );
1170     CHECK_EQUAL( ftell( file ), 5 );
1171     }
1172     {
1173     // reading 9223372036854775807, u
1174     fseek( file, 100, SEEK_SET );
1175     unsigned long long i = -1;
1176     int n;
1177     CHECK_EQUAL( fscanf( file, "%llu%n", &i, &n ), 1 );
1178     CHECK_FEQUAL( i, 9223372036854775807ull, unsigned long long, "%llu" );
1179     CHECK_EQUAL( n, 19 );
1180     CHECK_EQUAL( ftell( file ), 119 );
1181     }
1182     {
1183     // reading +9223372036854775807, u
1184     fseek( file, 99, SEEK_SET );
1185     unsigned long long i = -1;
1186     int n;
1187     CHECK_EQUAL( fscanf( file, "%llu%n", &i, &n ), 1 );
1188     CHECK_FEQUAL( i, 9223372036854775807ull, unsigned long long, "%llu" );
1189     CHECK_EQUAL( n, 20 );
1190     CHECK_EQUAL( ftell( file ), 119 );
1191     }
1192     {
1193     // reading 18446744073709551615, u
1194     fseek( file, 121, SEEK_SET );
1195     unsigned long long i = 0;
1196     int n;
1197     CHECK_EQUAL( fscanf( file, "%llu%n", &i, &n ), 1 );
1198     CHECK_FEQUAL( i, 18446744073709551615ull, unsigned long long, "%llu" );
1199     CHECK_EQUAL( n, 20 );
1200     CHECK_EQUAL( ftell( file ), 141 );
1201     }
1202     {
1203     // reading +18446744073709551615, u
1204     fseek( file, 120, SEEK_SET );
1205     unsigned long long i = 0;
1206     int n;
1207     CHECK_EQUAL( fscanf( file, "%llu%n", &i, &n ), 1 );
1208     CHECK_FEQUAL( i, 18446744073709551615ull, unsigned long long, "%llu" );
1209     CHECK_EQUAL( n, 21 );
1210     CHECK_EQUAL( ftell( file ), 141 );
1211     }
1212     {
1213     // reading 0, i
1214     fseek( file, 1, SEEK_SET );
1215     signed long long i = -1;
1216     int n;
1217     CHECK_EQUAL( fscanf( file, "%lli%n", &i, &n ), 1 );
1218     CHECK_EQUAL( i, 0ll );
1219     CHECK_EQUAL( n, 1 );
1220     CHECK_EQUAL( ftell( file ), 2 );
1221     }
1222     {
1223     // reading -0, i
1224     fseek( file, 0, SEEK_SET );
1225     signed long long i = -1;
1226     int n;
1227     CHECK_EQUAL( fscanf( file, "%lli%n", &i, &n ), 1 );
1228     CHECK_EQUAL( i, 0ll );
1229     CHECK_EQUAL( n, 2 );
1230     CHECK_EQUAL( ftell( file ), 2 );
1231     }
1232     {
1233     // reading +0, i
1234     fseek( file, 3, SEEK_SET );
1235     signed long long i = -1;
1236     int n;
1237     CHECK_EQUAL( fscanf( file, "%lli%n", &i, &n ), 1 );
1238     CHECK_EQUAL( i, 0ll );
1239     CHECK_EQUAL( n, 2 );
1240     CHECK_EQUAL( ftell( file ), 5 );
1241     }
1242     {
1243     // reading -9223372036854775808, i
1244     fseek( file, 78, SEEK_SET );
1245     signed long long i = -1;
1246     int n;
1247     CHECK_EQUAL( fscanf( file, "%lli%n", &i, &n ), 1 );
1248     CHECK_FEQUAL( i, LLONG_MIN, signed long long, "%lli" ); // should be literal -9223372036854775808ll but GCC balks.
1249     CHECK_EQUAL( i < 0ll, 1 );
1250     CHECK_EQUAL( n, 20 );
1251     CHECK_EQUAL( ftell( file ), 98 );
1252     }
1253     {
1254     // reading 9223372036854775807, i
1255     fseek( file, 100, SEEK_SET );
1256     signed long long i = -1;
1257     int n;
1258     CHECK_EQUAL( fscanf( file, "%lli%n", &i, &n ), 1 );
1259     CHECK_FEQUAL( i, 9223372036854775807ll, signed long long, "%lli" );
1260     CHECK_EQUAL( n, 19 );
1261     CHECK_EQUAL( ftell( file ), 119 );
1262     }
1263     {
1264     // reading +9223372036854775807, i
1265     fseek( file, 99, SEEK_SET );
1266     signed long long i = -1;
1267     int n;
1268     CHECK_EQUAL( fscanf( file, "%lli%n", &i, &n ), 1 );
1269     CHECK_FEQUAL( i, 9223372036854775807ll, signed long long, "%lli" );
1270     CHECK_EQUAL( n, 20 );
1271     CHECK_EQUAL( ftell( file ), 119 );
1272     }
1273     
1274     fclose( file );
1275     remove( "tmpfile" );
1276 }
1277
1278 // hexadecimal integer matches
1279 void suite_three()
1280 {
1281     FILE * file;
1282     if ( ( file = fopen( "tmpfile", "wb+" ) ) == NULL )
1283     {
1284         puts( "Failed to open tmpfile for writing." );
1285         return;
1286     }
1287     fprintf( file, "-0x0 -0x000 -0x7f 0x80 0xff -0x7fff 0x8000 0xffff -0x7fffffff 0x80000000 0xffffffff\n" );
1288     fprintf( file, "-0x7fffffffffffffff 0x8000000000000000 0xffffffffffffffff -0x\n" );
1289     CHECK_EQUAL( ftell( file ), 146 );
1290     {
1291     // reading 0, x
1292     fseek( file, 3, SEEK_SET );
1293     unsigned char i = -1;
1294     int n;
1295     CHECK_EQUAL( fscanf( file, "%hhx%n", &i, &n ), 1 );
1296     CHECK_EQUAL( i, 0 );
1297     CHECK_EQUAL( n, 1 );
1298     CHECK_EQUAL( ftell( file ), 4 );
1299     }
1300     {
1301     // reading -0x0, x
1302     fseek( file, 0, SEEK_SET );
1303     unsigned char i = -1;
1304     int n;
1305     CHECK_EQUAL( fscanf( file, "%hhx%n", &i, &n ), 1 );
1306     CHECK_EQUAL( i, 0 );
1307     CHECK_EQUAL( n, 4 );
1308     CHECK_EQUAL( ftell( file ), 4 );
1309     }
1310     {
1311     // reading -0x, x
1312     fseek( file, -4, SEEK_END );
1313     unsigned char i = -1;
1314     int n;
1315     CHECK_EQUAL( fscanf( file, "%hhx%n", &i, &n ), 1 );
1316     CHECK_EQUAL( i, 0 );
1317     CHECK_EQUAL( n, 3 );
1318     CHECK_EQUAL( ftell( file ), 145 );
1319     }
1320     {
1321     // reading 0x000, x
1322     fseek( file, 5, SEEK_SET );
1323     unsigned char i = -1;
1324     int n;
1325     CHECK_EQUAL( fscanf( file, "%hhx%n", &i, &n ), 1 );
1326     CHECK_EQUAL( i, 0 );
1327     CHECK_EQUAL( n, 6 );
1328     CHECK_EQUAL( ftell( file ), 11 );
1329     }
1330     {
1331     // reading 0x0, i
1332     fseek( file, 0, SEEK_SET );
1333     signed char i = -1;
1334     int n;
1335     CHECK_EQUAL( fscanf( file, "%hhi%n", &i, &n ), 1 );
1336     CHECK_EQUAL( i, 0 );
1337     CHECK_EQUAL( n, 4 );
1338     CHECK_EQUAL( ftell( file ), 4 );
1339     }
1340     {
1341     // reading 7f, x
1342     fseek( file, 15, SEEK_SET );
1343     unsigned char i = -1;
1344     int n;
1345     CHECK_EQUAL( fscanf( file, "%hhx%n", &i, &n ), 1 );
1346     CHECK_EQUAL( i, 127 );
1347     CHECK_EQUAL( n, 2 );
1348     CHECK_EQUAL( ftell( file ), 17 );
1349     }
1350     {
1351     // reading -0x7f, x
1352     fseek( file, 12, SEEK_SET );
1353     unsigned char i = -1;
1354     int n;
1355     CHECK_EQUAL( fscanf( file, "%hhx%n", &i, &n ), 1 );
1356     CHECK_FEQUAL( i, -127, unsigned char, "%hhu" );
1357     CHECK_EQUAL( n, 5 );
1358     CHECK_EQUAL( ftell( file ), 17 );
1359     }
1360     {
1361     // reading 0x80, i
1362     fseek( file, 18, SEEK_SET );
1363     signed char i = -1;
1364     int n;
1365     CHECK_EQUAL( fscanf( file, "%hhi%n", &i, &n ), 1 );
1366     CHECK_FEQUAL( i, -128, signed char, "%hd" );
1367     CHECK_EQUAL( n, 4 );
1368     CHECK_EQUAL( ftell( file ), 22 );
1369     }
1370     {
1371     // reading ff, x
1372     fseek( file, 25, SEEK_SET );
1373     unsigned char i = -1;
1374     int n;
1375     CHECK_EQUAL( fscanf( file, "%hhx%n", &i, &n ), 1 );
1376     CHECK_EQUAL( i, 0xff );
1377     CHECK_EQUAL( n, 2 );
1378     CHECK_EQUAL( ftell( file ), 27 );
1379     }
1380     {
1381     // reading 0xff, x
1382     fseek( file, 23, SEEK_SET );
1383     unsigned char i = -1;
1384     int n;
1385     CHECK_EQUAL( fscanf( file, "%hhx%n", &i, &n ), 1 );
1386     CHECK_EQUAL( i, 255 );
1387     CHECK_EQUAL( n, 4 );
1388     CHECK_EQUAL( ftell( file ), 27 );
1389     }
1390     {
1391     // reading 0xff, i
1392     fseek( file, 23, SEEK_SET );
1393     signed char i = 0;
1394     int n;
1395     CHECK_EQUAL( fscanf( file, "%hhi%n", &i, &n ), 1 );
1396     CHECK_EQUAL( i, -1 );
1397     CHECK_EQUAL( n, 4 );
1398     CHECK_EQUAL( ftell( file ), 27 );
1399     }
1400     {
1401     // reading 0, x
1402     fseek( file, 3, SEEK_SET );
1403     unsigned short i = -1;
1404     int n;
1405     CHECK_EQUAL( fscanf( file, "%hx%n", &i, &n ), 1 );
1406     CHECK_EQUAL( i, 0 );
1407     CHECK_EQUAL( n, 1 );
1408     CHECK_EQUAL( ftell( file ), 4 );
1409     }
1410     {
1411     // reading -0x0, x
1412     fseek( file, 0, SEEK_SET );
1413     unsigned short i = -1;
1414     int n;
1415     CHECK_EQUAL( fscanf( file, "%hx%n", &i, &n ), 1 );
1416     CHECK_EQUAL( i, 0 );
1417     CHECK_EQUAL( n, 4 );
1418     CHECK_EQUAL( ftell( file ), 4 );
1419     }
1420     {
1421     // reading -0x, x
1422     fseek( file, -4, SEEK_END );
1423     unsigned short i = -1;
1424     int n;
1425     CHECK_EQUAL( fscanf( file, "%hx%n", &i, &n ), 1 );
1426     CHECK_EQUAL( i, 0 );
1427     CHECK_EQUAL( n, 3 );
1428     CHECK_EQUAL( ftell( file ), 145 );
1429     }
1430     {
1431     // reading 0x000, x
1432     fseek( file, 5, SEEK_SET );
1433     unsigned short i = -1;
1434     int n;
1435     CHECK_EQUAL( fscanf( file, "%hx%n", &i, &n ), 1 );
1436     CHECK_EQUAL( i, 0 );
1437     CHECK_EQUAL( n, 6 );
1438     CHECK_EQUAL( ftell( file ), 11 );
1439     }
1440     {
1441     // reading 0x0, i
1442     fseek( file, 0, SEEK_SET );
1443     signed short i = -1;
1444     int n;
1445     CHECK_EQUAL( fscanf( file, "%hi%n", &i, &n ), 1 );
1446     CHECK_EQUAL( i, 0 );
1447     CHECK_EQUAL( n, 4 );
1448     CHECK_EQUAL( ftell( file ), 4 );
1449     }
1450     {
1451     // reading 7fff, x
1452     fseek( file, 31, SEEK_SET );
1453     unsigned short i = -1;
1454     int n;
1455     CHECK_EQUAL( fscanf( file, "%hx%n", &i, &n ), 1 );
1456     CHECK_EQUAL( i, 32767 );
1457     CHECK_EQUAL( n, 4 );
1458     CHECK_EQUAL( ftell( file ), 35 );
1459     }
1460     {
1461     // reading -0x7fff, x
1462     fseek( file, 28, SEEK_SET );
1463     unsigned short i = -1;
1464     int n;
1465     CHECK_EQUAL( fscanf( file, "%hx%n", &i, &n ), 1 );
1466     CHECK_FEQUAL( i, -32767, unsigned short, "%hu" );
1467     CHECK_EQUAL( n, 7 );
1468     CHECK_EQUAL( ftell( file ), 35 );
1469     }
1470     {
1471     // reading 0x8000, i
1472     fseek( file, 36, SEEK_SET );
1473     signed short i = -1;
1474     int n;
1475     CHECK_EQUAL( fscanf( file, "%hi%n", &i, &n ), 1 );
1476     CHECK_FEQUAL( i, -32768, signed short, "%hd" );
1477     CHECK_EQUAL( n, 6 );
1478     CHECK_EQUAL( ftell( file ), 42 );
1479     }
1480     {
1481     // reading ffff, x
1482     fseek( file, 45, SEEK_SET );
1483     unsigned short i = -1;
1484     int n;
1485     CHECK_EQUAL( fscanf( file, "%hx%n", &i, &n ), 1 );
1486     CHECK_EQUAL( i, 65535 );
1487     CHECK_EQUAL( n, 4 );
1488     CHECK_EQUAL( ftell( file ), 49 );
1489     }
1490     {
1491     // reading 0xffff, x
1492     fseek( file, 43, SEEK_SET );
1493     unsigned short i = -1;
1494     int n;
1495     CHECK_EQUAL( fscanf( file, "%hx%n", &i, &n ), 1 );
1496     CHECK_EQUAL( i, 65535 );
1497     CHECK_EQUAL( n, 6 );
1498     CHECK_EQUAL( ftell( file ), 49 );
1499     }
1500     {
1501     // reading 0xffff, i
1502     fseek( file, 43, SEEK_SET );
1503     signed short i = 0;
1504     int n;
1505     CHECK_EQUAL( fscanf( file, "%hi%n", &i, &n ), 1 );
1506     CHECK_FEQUAL( i, -1, signed short, "%hd" );
1507     CHECK_EQUAL( n, 6 );
1508     CHECK_EQUAL( ftell( file ), 49 );
1509     }
1510     {
1511     // reading 0, x
1512     fseek( file, 3, SEEK_SET );
1513     unsigned int i = -1;
1514     int n;
1515     CHECK_EQUAL( fscanf( file, "%x%n", &i, &n ), 1 );
1516     CHECK_EQUAL( i, 0 );
1517     CHECK_EQUAL( n, 1 );
1518     CHECK_EQUAL( ftell( file ), 4 );
1519     }
1520     {
1521     // reading -0x0, x
1522     fseek( file, 0, SEEK_SET );
1523     unsigned int i = -1;
1524     int n;
1525     CHECK_EQUAL( fscanf( file, "%x%n", &i, &n ), 1 );
1526     CHECK_EQUAL( i, 0 );
1527     CHECK_EQUAL( n, 4 );
1528     CHECK_EQUAL( ftell( file ), 4 );
1529     }
1530     {
1531     // reading -0x, x
1532     fseek( file, -4, SEEK_END );
1533     unsigned int i = -1;
1534     int n;
1535     CHECK_EQUAL( fscanf( file, "%x%n", &i, &n ), 1 );
1536     CHECK_EQUAL( i, 0 );
1537     CHECK_EQUAL( n, 3 );
1538     CHECK_EQUAL( ftell( file ), 145 );
1539     }
1540     {
1541     // reading 0x000, x
1542     fseek( file, 5, SEEK_SET );
1543     unsigned int i = -1;
1544     int n;
1545     CHECK_EQUAL( fscanf( file, "%x%n", &i, &n ), 1 );
1546     CHECK_EQUAL( i, 0 );
1547     CHECK_EQUAL( n, 6 );
1548     CHECK_EQUAL( ftell( file ), 11 );
1549     }
1550     {
1551     // reading 0x0, i
1552     fseek( file, 0, SEEK_SET );
1553     signed int i = -1;
1554     int n;
1555     CHECK_EQUAL( fscanf( file, "%i%n", &i, &n ), 1 );
1556     CHECK_EQUAL( i, 0 );
1557     CHECK_EQUAL( n, 4 );
1558     CHECK_EQUAL( ftell( file ), 4 );
1559     }
1560     {
1561     // reading 7fffffff, x
1562     fseek( file, 53, SEEK_SET );
1563     unsigned int i = -1;
1564     int n;
1565     CHECK_EQUAL( fscanf( file, "%x%n", &i, &n ), 1 );
1566     CHECK_EQUAL( i, 2147483647 );
1567     CHECK_EQUAL( n, 8 );
1568     CHECK_EQUAL( ftell( file ), 61 );
1569     }
1570     {
1571     // reading -0x7fffffff, x
1572     fseek( file, 50, SEEK_SET );
1573     unsigned int i = -1;
1574     int n;
1575     CHECK_EQUAL( fscanf( file, "%x%n", &i, &n ), 1 );
1576     CHECK_FEQUAL( i, -2147483647, unsigned int, "%u" );
1577     CHECK_EQUAL( n, 11 );
1578     CHECK_EQUAL( ftell( file ), 61 );
1579     }
1580     {
1581     // reading 0x80000000, i
1582     fseek( file, 62, SEEK_SET );
1583     signed int i = -1;
1584     int n;
1585     CHECK_EQUAL( fscanf( file, "%i%n", &i, &n ), 1 );
1586     CHECK_FEQUAL( i, 2147483647, signed int, "%d" ); // NOT overflowing, see strtol() specs.
1587     CHECK_EQUAL( n, 10 );
1588     CHECK_EQUAL( ftell( file ), 72 );
1589     }
1590     {
1591     // reading ffffffff, x
1592     fseek( file, 75, SEEK_SET );
1593     unsigned int i = -1;
1594     int n;
1595     CHECK_EQUAL( fscanf( file, "%x%n", &i, &n ), 1 );
1596     CHECK_FEQUAL( i, 4294967295, unsigned int, "%d" );
1597     CHECK_EQUAL( n, 8 );
1598     CHECK_EQUAL( ftell( file ), 83 );
1599     }
1600     {
1601     // reading 0xffffffff, x
1602     fseek( file, 73, SEEK_SET );
1603     unsigned int i = -1;
1604     int n;
1605     CHECK_EQUAL( fscanf( file, "%x%n", &i, &n ), 1 );
1606     CHECK_FEQUAL( i, 4294967295, unsigned int, "%d" );
1607     CHECK_EQUAL( n, 10 );
1608     CHECK_EQUAL( ftell( file ), 83 );
1609     }
1610     {
1611     // reading 0xffffffff, i
1612     fseek( file, 73, SEEK_SET );
1613     signed int i = 0;
1614     int n;
1615     CHECK_EQUAL( fscanf( file, "%i%n", &i, &n ), 1 );
1616     CHECK_FEQUAL( i, 2147483647, signed int, "%d" ); // NOT overflowing; see strtol() specs.
1617     CHECK_EQUAL( n, 10 );
1618     CHECK_EQUAL( ftell( file ), 83 );
1619     }
1620     fclose( file );
1621     remove( "tmpfile" );    
1622 }
1623
1624 // octal integer matches
1625 void suite_four()
1626 {
1627     FILE * file;
1628     if ( ( file = fopen( "tmpfile", "wb+" ) ) == NULL )
1629     {
1630         puts( "Failed to open tmpfile for writing." );
1631         return;
1632     }
1633     fprintf( file, "+0000 -0000 +0177 +0377 -0377 +077777 +0177777 -0177777\n" );
1634     fprintf( file, "+017777777777 +037777777777 -037777777777\n" );
1635     fprintf( file, "+0777777777777777777777 +01777777777777777777777\n" );
1636     fprintf( file, "-01777777777777777777777\n" );
1637     CHECK_EQUAL( ftell( file ), 172 );
1638     {
1639     // reading 0, o
1640     fseek( file, 4, SEEK_SET );
1641     unsigned char i = -1;
1642     int n;
1643     CHECK_EQUAL( fscanf( file, "%hho%n", &i, &n ), 1 );
1644     CHECK_EQUAL( i, 0u );
1645     CHECK_EQUAL( n, 1 );
1646     CHECK_EQUAL( ftell( file ), 5 );
1647     }
1648     {
1649     // reading +0000, o
1650     fseek( file, 0, SEEK_SET );
1651     unsigned char i = -1;
1652     int n;
1653     CHECK_EQUAL( fscanf( file, "%hho%n", &i, &n ), 1 );
1654     CHECK_EQUAL( i, 0u );
1655     CHECK_EQUAL( n, 5 );
1656     CHECK_EQUAL( ftell( file ), 5 );
1657     }
1658     {
1659     // reading -0000, o
1660     fseek( file, 6, SEEK_SET );
1661     unsigned char i = -1;
1662     int n;
1663     CHECK_EQUAL( fscanf( file, "%hho%n", &i, &n ), 1 );
1664     CHECK_EQUAL( i, 0u );
1665     CHECK_EQUAL( n, 5 );
1666     CHECK_EQUAL( ftell( file ), 11 );
1667     }
1668     {
1669     // reading 0177, o
1670     fseek( file, 13, SEEK_SET );
1671     unsigned char i = -1;
1672     int n;
1673     CHECK_EQUAL( fscanf( file, "%hho%n", &i, &n ), 1 );
1674     CHECK_EQUAL( i, 127u );
1675     CHECK_EQUAL( n, 4 );
1676     CHECK_EQUAL( ftell( file ), 17 );
1677     }
1678     {
1679     // reading +0177, o
1680     fseek( file, 12, SEEK_SET );
1681     unsigned char i = -1;
1682     int n;
1683     CHECK_EQUAL( fscanf( file, "%hho%n", &i, &n ), 1 );
1684     CHECK_EQUAL( i, 127u );
1685     CHECK_EQUAL( n, 5 );
1686     CHECK_EQUAL( ftell( file ), 17 );
1687     }
1688     {
1689     // reading 0377, o
1690     fseek( file, 19, SEEK_SET );
1691     unsigned char i = -1;
1692     int n;
1693     CHECK_EQUAL( fscanf( file, "%hho%n", &i, &n ), 1 );
1694     CHECK_FEQUAL( i, 255u, unsigned char, "%hhu" );
1695     CHECK_EQUAL( n, 4 );
1696     CHECK_EQUAL( ftell( file ), 23 );
1697     }
1698     {
1699     // reading +0377, o
1700     fseek( file, 18, SEEK_SET );
1701     unsigned char i = -1;
1702     int n;
1703     CHECK_EQUAL( fscanf( file, "%hho%n", &i, &n ), 1 );
1704     CHECK_FEQUAL( i, 255u, unsigned char, "%hhu" );
1705     CHECK_EQUAL( n, 5 );
1706     CHECK_EQUAL( ftell( file ), 23 );
1707     }
1708     {
1709     // reading -0377, o
1710     fseek( file, 24, SEEK_SET );
1711     unsigned char i = -1;
1712     int n;
1713     CHECK_EQUAL( fscanf( file, "%hho%n", &i, &n ), 1 );
1714     CHECK_FEQUAL( i, 1u, unsigned char, "%hhu" );
1715     CHECK_EQUAL( n, 5 );
1716     CHECK_EQUAL( ftell( file ), 29 );
1717     }
1718     {
1719     // reading 077777, o
1720     fseek( file, 31, SEEK_SET );
1721     unsigned short i = -1;
1722     int n;
1723     CHECK_EQUAL( fscanf( file, "%ho%n", &i, &n ), 1 );
1724     CHECK_EQUAL( i, 32767u );
1725     CHECK_EQUAL( n, 6 );
1726     CHECK_EQUAL( ftell( file ), 37 );
1727     }
1728     {
1729     // reading +077777, o
1730     fseek( file, 30, SEEK_SET );
1731     unsigned short i = -1;
1732     int n;
1733     CHECK_EQUAL( fscanf( file, "%ho%n", &i, &n ), 1 );
1734     CHECK_EQUAL( i, 32767u );
1735     CHECK_EQUAL( n, 7 );
1736     CHECK_EQUAL( ftell( file ), 37 );
1737     }
1738     {
1739     // reading 0177777, o
1740     fseek( file, 39, SEEK_SET );
1741     unsigned short i = -1;
1742     int n;
1743     CHECK_EQUAL( fscanf( file, "%ho%n", &i, &n ), 1 );
1744     CHECK_FEQUAL( i, 65535u, unsigned short, "%hu" );
1745     CHECK_EQUAL( n, 7 );
1746     CHECK_EQUAL( ftell( file ), 46 );
1747     }
1748     {
1749     // reading +0177777, o
1750     fseek( file, 38, SEEK_SET );
1751     unsigned short i = -1;
1752     int n;
1753     CHECK_EQUAL( fscanf( file, "%ho%n", &i, &n ), 1 );
1754     CHECK_FEQUAL( i, 65535u, unsigned short, "%hu" );
1755     CHECK_EQUAL( n, 8 );
1756     CHECK_EQUAL( ftell( file ), 46 );
1757     }
1758     {
1759     // reading -0177777, o
1760     fseek( file, 47, SEEK_SET );
1761     unsigned short i = -1;
1762     int n;
1763     CHECK_EQUAL( fscanf( file, "%ho%n", &i, &n ), 1 );
1764     CHECK_FEQUAL( i, 1u, unsigned short, "%hu" );
1765     CHECK_EQUAL( n, 8 );
1766     CHECK_EQUAL( ftell( file ), 55 );
1767     }
1768     {
1769     // reading 017777777777, o
1770     fseek( file, 57, SEEK_SET );
1771     unsigned int i = -1;
1772     int n;
1773     CHECK_EQUAL( fscanf( file, "%o%n", &i, &n ), 1 );
1774     CHECK_EQUAL( i, 2147483647u );
1775     CHECK_EQUAL( n, 12 );
1776     CHECK_EQUAL( ftell( file ), 69 );
1777     }
1778     {
1779     // reading +017777777777, o
1780     fseek( file, 56, SEEK_SET );
1781     unsigned int i = -1;
1782     int n;
1783     CHECK_EQUAL( fscanf( file, "%o%n", &i, &n ), 1 );
1784     CHECK_EQUAL( i, 2147483647u );
1785     CHECK_EQUAL( n, 13 );
1786     CHECK_EQUAL( ftell( file ), 69 );
1787     }
1788     {
1789     // reading 037777777777, o
1790     fseek( file, 71, SEEK_SET );
1791     unsigned int i = -1;
1792     int n;
1793     CHECK_EQUAL( fscanf( file, "%o%n", &i, &n ), 1 );
1794     CHECK_FEQUAL( i, 4294967295u, unsigned int, "%u" );
1795     CHECK_EQUAL( n, 12 );
1796     CHECK_EQUAL( ftell( file ), 83 );
1797     }
1798     {
1799     // reading +037777777777, o
1800     fseek( file, 70, SEEK_SET );
1801     unsigned int i = -1;
1802     int n;
1803     CHECK_EQUAL( fscanf( file, "%o%n", &i, &n ), 1 );
1804     CHECK_FEQUAL( i, 4294967295u, unsigned int, "%u" );
1805     CHECK_EQUAL( n, 13 );
1806     CHECK_EQUAL( ftell( file ), 83 );
1807     }
1808     {
1809     // reading -037777777777, o
1810     fseek( file, 84, SEEK_SET );
1811     unsigned int i = -1;
1812     int n;
1813     CHECK_EQUAL( fscanf( file, "%o%n", &i, &n ), 1 );
1814     CHECK_FEQUAL( i, 1u, unsigned int, "%u" );
1815     CHECK_EQUAL( n, 13 );
1816     CHECK_EQUAL( ftell( file ), 97 );
1817     }
1818     {
1819     // reading 017777777777, o
1820     fseek( file, 57, SEEK_SET );
1821     unsigned long i = -1;
1822     int n;
1823     CHECK_EQUAL( fscanf( file, "%lo%n", &i, &n ), 1 );
1824     CHECK_EQUAL( i, 2147483647lu );
1825     CHECK_EQUAL( n, 12 );
1826     CHECK_EQUAL( ftell( file ), 69 );
1827     }
1828     {
1829     // reading +017777777777, o
1830     fseek( file, 56, SEEK_SET );
1831     unsigned long i = -1;
1832     int n;
1833     CHECK_EQUAL( fscanf( file, "%lo%n", &i, &n ), 1 );
1834     CHECK_EQUAL( i, 2147483647lu );
1835     CHECK_EQUAL( n, 13 );
1836     CHECK_EQUAL( ftell( file ), 69 );
1837     }
1838     {
1839     // reading 037777777777, o
1840     fseek( file, 71, SEEK_SET );
1841     unsigned long i = -1;
1842     int n;
1843     CHECK_EQUAL( fscanf( file, "%lo%n", &i, &n ), 1 );
1844     CHECK_FEQUAL( i, 4294967295lu, unsigned long, "%lu" );
1845     CHECK_EQUAL( n, 12 );
1846     CHECK_EQUAL( ftell( file ), 83 );
1847     }
1848     {
1849     // reading +037777777777, o
1850     fseek( file, 70, SEEK_SET );
1851     unsigned long i = -1;
1852     int n;
1853     CHECK_EQUAL( fscanf( file, "%lo%n", &i, &n ), 1 );
1854     CHECK_FEQUAL( i, 4294967295lu, unsigned long, "%lu" );
1855     CHECK_EQUAL( n, 13 );
1856     CHECK_EQUAL( ftell( file ), 83 );
1857     }
1858     {
1859     // reading -037777777777, o
1860     fseek( file, 84, SEEK_SET );
1861     unsigned long i = -1;
1862     int n;
1863     CHECK_EQUAL( fscanf( file, "%lo%n", &i, &n ), 1 );
1864     CHECK_FEQUAL( i, 1lu, unsigned long, "%lu" );
1865     CHECK_EQUAL( n, 13 );
1866     CHECK_EQUAL( ftell( file ), 97 );
1867     }
1868     {
1869     // reading 0777777777777777777777, o
1870     fseek( file, 99, SEEK_SET );
1871     unsigned long long i = -1;
1872     int n;
1873     CHECK_EQUAL( fscanf( file, "%llo%n", &i, &n ), 1 );
1874     CHECK_FEQUAL( i, 9223372036854775807llu, unsigned long long, "%llu" );
1875     CHECK_EQUAL( n, 22 );
1876     CHECK_EQUAL( ftell( file ), 121 );
1877     }
1878     {
1879     // reading +0777777777777777777777, o
1880     fseek( file, 98, SEEK_SET );
1881     unsigned long long i = -1;
1882     int n;
1883     CHECK_EQUAL( fscanf( file, "%llo%n", &i, &n ), 1 );
1884     CHECK_FEQUAL( i, 9223372036854775807llu, unsigned long long, "%llu" );
1885     CHECK_EQUAL( n, 23 );
1886     CHECK_EQUAL( ftell( file ), 121 );
1887     }
1888     {
1889     // reading 01777777777777777777777, o
1890     fseek( file, 123, SEEK_SET );
1891     unsigned long long i = -1;
1892     int n;
1893     CHECK_EQUAL( fscanf( file, "%llo%n", &i, &n ), 1 );
1894     CHECK_FEQUAL( i, 18446744073709551615llu, unsigned long long, "%llu" );
1895     CHECK_EQUAL( n, 23 );
1896     CHECK_EQUAL( ftell( file ), 146 );
1897     }
1898     {
1899     // reading +01777777777777777777777, o
1900     fseek( file, 122, SEEK_SET );
1901     unsigned long long i = -1;
1902     int n;
1903     CHECK_EQUAL( fscanf( file, "%llo%n", &i, &n ), 1 );
1904     CHECK_FEQUAL( i, 18446744073709551615llu, unsigned long long, "%llu" );
1905     CHECK_EQUAL( n, 24 );
1906     CHECK_EQUAL( ftell( file ), 146 );
1907     }
1908     {
1909     // reading -01777777777777777777777, o
1910     fseek( file, 147, SEEK_SET );
1911     unsigned long long i = -1;
1912     int n;
1913     CHECK_EQUAL( fscanf( file, "%llo%n", &i, &n ), 1 );
1914     CHECK_FEQUAL( i, 1llu, unsigned long long, "%llu" );
1915     CHECK_EQUAL( n, 24 );
1916     CHECK_EQUAL( ftell( file ), 171 );
1917     }
1918     fclose( file );
1919     remove( "tmpfile" );    
1920 }
1921
1922 // string matches
1923 void suite_five()
1924 {
1925     int const BUFSIZE = 1000;
1926     FILE * file;
1927     if ( ( file = fopen( "tmpfile", "wb+" ) ) == NULL )
1928     {
1929         puts( "Failed to open tmpfile for writing." );
1930         return;
1931     }
1932     fprintf( file, "abcdefgh-ijklmnop[qrs%%uvw]xyz" );
1933     CHECK_EQUAL( ftell( file ), 29 );
1934     char buffer[ BUFSIZE ];
1935     {
1936     // reading abc
1937     fseek( file, 0, SEEK_SET );
1938     memset( buffer, '\0', BUFSIZE );
1939     int n;
1940     CHECK_EQUAL( fscanf( file, "%[abc]%n", buffer, &n ), 1 );
1941     CHECK_EQUAL( n, 3 );
1942     CHECK_FALSE( memcmp( buffer, "abc", n + 1 ) );
1943     CHECK_EQUAL( ftell( file ), 3 );
1944     }
1945     {
1946     // reading a-c
1947     fseek( file, 0, SEEK_SET );
1948     memset( buffer, '\0', BUFSIZE );
1949     int n;
1950     CHECK_EQUAL( fscanf( file, "%[a-c]%n", buffer, &n ), 1 );
1951     CHECK_EQUAL( n, 3 );
1952     CHECK_FALSE( memcmp( buffer, "abc", n + 1 ) );
1953     CHECK_EQUAL( ftell( file ), 3 );
1954     }
1955     {
1956     // reading a-h
1957     fseek( file, 0, SEEK_SET );
1958     memset( buffer, '\0', BUFSIZE );
1959     int n;
1960     CHECK_EQUAL( fscanf( file, "%[a-h]%n", buffer, &n ), 1 );
1961     CHECK_EQUAL( n, 8 );
1962     CHECK_FALSE( memcmp( buffer, "abcdefgh", n + 1 ) );
1963     CHECK_EQUAL( ftell( file ), 8 );
1964     }
1965     {
1966     // reading o-r, including [, seperate char
1967     fseek( file, 15, SEEK_SET );
1968     memset( buffer, '\0', BUFSIZE );
1969     int n;
1970     CHECK_EQUAL( fscanf( file, "%[[o-qr]%n", buffer, &n ), 1 );
1971     CHECK_EQUAL( n, 5 );
1972     CHECK_FALSE( memcmp( buffer, "op[qr", n + 1 ) );
1973     CHECK_EQUAL( ftell( file ), 20 );
1974     }
1975     {
1976     // reading v-y, including ], two groups
1977     fseek( file, 23, SEEK_SET );
1978     memset( buffer, '\0', BUFSIZE );
1979     int n;
1980     CHECK_EQUAL( fscanf( file, "%[]v-wx-y]%n", buffer, &n ), 1 );
1981     CHECK_EQUAL( n, 5 );
1982     CHECK_FALSE( memcmp( buffer, "vw]xy", n + 1 ) );
1983     CHECK_EQUAL( ftell( file ), 28 );
1984     }
1985     {
1986     // missing on first character
1987     fseek( file, 0, SEEK_SET );
1988     memset( buffer, '\0', BUFSIZE );
1989     int n;
1990     CHECK_EQUAL( fscanf( file, "%[b]%n", buffer, &n ), 0 );
1991     CHECK_FALSE( memcmp( buffer, "", 1 ) );
1992     CHECK_EQUAL( ftell( file ), 0 );
1993     }
1994     {
1995     // eof while reading, two groups
1996     fseek( file, 27, SEEK_SET );
1997     memset( buffer, '\0', BUFSIZE );
1998     int n;
1999     CHECK_EQUAL( fscanf( file, "%[a-zA-Z]%n", buffer, &n ), 1 );
2000     CHECK_EQUAL( n, 2 );
2001     CHECK_FALSE( memcmp( buffer, "yz", n + 1 ) );
2002     CHECK_TRUE( feof( file ) );
2003     CHECK_FALSE( ferror( file ) );
2004     CHECK_EQUAL( ftell( file ), 29 );
2005     }
2006     {
2007     // eof before reading
2008     fseek( file, 29, SEEK_SET );
2009     memset( buffer, '\0', BUFSIZE );
2010     int n;
2011     CHECK_EQUAL( fscanf( file, "%[a-z]%n", buffer, &n ), -1 );
2012     CHECK_FALSE( memcmp( buffer, "", 1 ) );
2013     CHECK_TRUE( feof( file ) );
2014     CHECK_FALSE( ferror( file ) );
2015     CHECK_EQUAL( ftell( file ), 29 );
2016     }
2017     {
2018     // negation - [^...]
2019     fseek( file, 0, SEEK_SET );
2020     memset( buffer, '\0', BUFSIZE );
2021     int n;
2022     CHECK_EQUAL( fscanf( file, "%[^d-f]%n", buffer, &n ), 1 );
2023     CHECK_EQUAL( n, 3 );
2024     CHECK_FALSE( memcmp( buffer, "abc", 4 ) );
2025     CHECK_EQUAL( ftell( file ), 3 );
2026     }
2027     fclose( file );
2028     remove( "tmpfile" );    
2029 }
2030
2031 #if 0
2032 char scanstring[] = "  1 23\00045\000\00067 ";
2033
2034 void scantest( int testnr, FILE * fh, size_t position, char const * format, 
2035                int expected_fscan_rc, char const * expected_fscan_output, size_t expected_fscan_length, 
2036                int expected_sscan_rc, char const * expected_sscan_output, size_t expected_sscan_length )
2037 {
2038     char buffer[15];
2039     printf( "Test %d\n", testnr );
2040     TESTCASE( memset( buffer, -1, 15 ) == buffer );
2041     TESTCASE( fseek( fh, position, SEEK_SET ) == 0 );
2042     TESTCASE( fscanf( fh, format, buffer ) == expected_fscan_rc );
2043     TESTCASE( memcmp( buffer, expected_fscan_output, expected_fscan_length ) == 0 );
2044     TESTCASE( memset( buffer, -1, 15 ) == buffer );
2045     TESTCASE( sscanf( scanstring + position, format, buffer ) == expected_sscan_rc );
2046     TESTCASE( memcmp( buffer, expected_sscan_output, expected_sscan_length ) == 0 );
2047 }
2048
2049 int main( void )
2050 {
2051     FILE * fh;
2052     TESTCASE( ( fh = fopen( "testfile", "w+" ) ) != NULL );
2053     TESTCASE( fwrite( scanstring, 14, 1, fh ) == 1 );
2054     rewind( fh );
2055
2056     /* %14c - full scan */
2057     scantest( 1, fh, 0, "%14c",
2058               1, "  1 23\00045\000\00067 \377", 15,
2059               1, "  1 23\377", 7 );
2060
2061     /* %c - default to one, reading whitespace */
2062     scantest( 2, fh, 0, "%c",
2063               1, " \377", 2,
2064               1, " \377", 2 );
2065
2066     /* %1c - reading zero byte */
2067     scantest( 3, fh, 9, "%1c",
2068               1, "\000\377", 2,
2069               -1, "\377", 1 );
2070
2071     /* %0c - NOT reading EOF */
2072     scantest( 4, fh, 13, "%0c",
2073               0, "\377", 1,
2074               0, "\377", 1 );
2075               
2076     TESTCASE( fclose( fh ) == 0 );
2077     //TESTCASE( remove( "testfile" ) == 0 );
2078
2079     return TEST_RESULTS;
2080 }
2081 #endif
2082
2083 #endif