File: stdio.c

package info (click to toggle)
c2go 0.26.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,052 kB
  • sloc: ansic: 6,037; sh: 82; makefile: 5
file content (655 lines) | stat: -rw-r--r-- 12,820 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// This program actually still works without including stdio.h but it should be
// here for consistency.

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
#include "tests.h"

#define START_TEST(t) \
    diag(#t);         \
    test_##t();

// size of that file
int filesize = 12820;

void test_putchar()
{
    putchar('#');
    char c;
    for (c = 'A'; c <= 'Z'; c++)
        putchar(c);
    putchar('\n');

    pass("%s", "putchar");
}

void test_puts()
{
    puts("#c2go");

    pass("%s", "puts");
}

void test_printf()
{
    // TODO: printf() has a different syntax to Go
    // https://github.com/elliotchance/c2go/issues/94

    printf("# Characters: %c %c \n", 'a', 65);
    //printf("Decimals: %d %ld\n", 1977, 650000L);
    printf("# Preceding with blanks: %10d \n", 1977);
    printf("# Preceding with zeros: %010d \n", 1977);
    printf("# Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
    printf("# floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
    printf("# Width trick: %*d \n", 5, 10);
    printf("# %s \n", "A string");


    int magnitude = 4;
    char printfFormat[30] = "%0";
    char magnitudeString[10];
    sprintf(magnitudeString, "%d", magnitude);
    strcat(printfFormat, magnitudeString);
    strcat(printfFormat, "d  ");
    printf("# ");
    printf(printfFormat, 120);
    printf(" \n");

    pass("%s", "printf");
}

void test_remove()
{
    // TODO: This does not actually test successfully deleting a file.
    if (remove("myfile.txt") != 0)
    {
        pass("%s", "error deleting file");
    }
    else
    {
        fail("%s", "file successfully deleted");
    }
}

void test_rename()
{
    // TODO: This does not actually test successfully renaming a file.
    int result;
    char oldname[] = "oldname.txt";
    char newname[] = "newname.txt";
    result = rename(oldname, newname);
    if (result == 0)
    {
        fail("%s", "File successfully renamed");
    }
    else
    {
        pass("%s", "Error renaming file");
    }
}

void test_fopen()
{
    FILE *pFile;
    pFile = fopen("/tmp/myfile.txt", "w");
    if (pFile != NULL)
    {
        is_not_null(pFile);
        fclose(pFile);
    }
}

void test_tmpfile()
{
    char buffer[256];
    FILE *pFile;
    pFile = tmpfile();

    fputs("hello world", pFile);
    rewind(pFile);
    fputs(fgets(buffer, 256, pFile), stdout);
    fclose(pFile);
}

void test_strerror()
{
    FILE *pFile;
    pFile = fopen("/tmp/nonexistantfile.dfjisz985bed9ztszosvep98zwibvezgrxdizbseiu.txt", "r");
    is_true(pFile == NULL);
    is_eq(errno, ENOENT);
    char *error = strerror(errno);
    is_streq(error, "No such file or directory");
    errno = 0;
    is_eq(errno, 0);
}

void test_tmpnam()
{
    // TODO: This is a tricky one to test because the output of tmpnam() in C
    // and Go will be different. I will keep the test here so at least it tries
    // to run the code but the test itself is not actually proving anything.

    char *pointer;

    // FIXME: We cannot pass variables by reference yet, which is a legitimate
    // way to use tmpnam(). I have to leave this disabled for now.
    //
    //     char buffer[L_tmpnam];
    //     tmpnam(buffer);
    //     assert(buffer != NULL);

    pointer = tmpnam(NULL);
    is_not_null(pointer);
}

void test_fclose()
{
	remove("/tmp/myfile.txt");
    FILE *pFile;
    pFile = fopen("/tmp/myfile.txt", "w");
    fputs("fclose example", pFile);
    fclose(pFile);
    // remove temp file
    is_eq(remove("/tmp/myfile.txt"),0)
}

void test_fflush()
{
    char mybuffer[80];
    FILE *pFile;
    pFile = fopen("/tmp/example.txt", "w+");
    is_not_null(pFile) or_return();

    fputs("test", pFile);
    fflush(pFile); // flushing or repositioning required
    fgets(mybuffer, 80, pFile);
    fclose(pFile);

    // check correct value written to file
    pFile = fopen("/tmp/example.txt", "r");
    is_not_null(pFile) or_return();
    fgets(mybuffer, 80, pFile);
    is_streq(mybuffer, "test");
    fclose(pFile);

    // check that fopen with w flag truncates the file
    pFile = fopen("/tmp/example.txt", "w");
    is_not_null(pFile) or_return();
    fputs(".", pFile);
    fclose(pFile);
    pFile = fopen("/tmp/example.txt", "r");
    is_not_null(pFile) or_return();
    fgets(mybuffer, 80, pFile);
    is_streq(mybuffer, ".");
    fclose(pFile);

    // remove temp file
    is_eq(remove("/tmp/example.txt"),0)
}

void test_fprintf()
{
	remove("/tmp/myfile1.txt");
    FILE *pFile;
    int n;
    char *name = "John Smith";

    pFile = fopen("/tmp/myfile1.txt", "w");
    is_not_null(pFile);

    for (n = 0; n < 3; n++)
    {
        fprintf(pFile, "Name %d [%-10.10s]\n", n + 1, name);
    }

    fclose(pFile);
    // remove temp file
    is_eq(remove("/tmp/myfile1.txt"),0)
}

void test_fscanf()
{
	remove("/tmp/myfile2.txt");

    char str[80];
    char end[80];
    float f;
	int i;
    FILE *pFile;

    pFile = fopen("/tmp/myfile2.txt", "w+");
    is_not_null(pFile);

    fprintf(pFile, "%f \r\n %s %d %s", 3.1416, "PI", 42, "end");
    rewind(pFile);
    fscanf(pFile, "%f", &f);
    fscanf(pFile, "%s", str);
    fscanf(pFile, "%d", &i);
    fscanf(pFile, "%s", end);
    fclose(pFile);
	pFile = NULL;

    is_eq(f, 3.1416);
    is_streq(str, "PI");
	is_eq(i,42);
    is_streq(end,"end");

	// read again
    FILE *pFile2;
    pFile2 = fopen("/tmp/myfile2.txt", "r");
    is_not_null(pFile2);

    fscanf(pFile2, "%f", &f);
    fscanf(pFile2, "%s", str);
    fscanf(pFile2, "%d", &i);
    fscanf(pFile2, "%s", end);
    fclose(pFile2);
	pFile2 = NULL;

    is_eq(f, 3.1416);
    is_streq(str, "PI");
	is_eq(i,42);
    is_streq(end,"end");
    
	// remove temp file
    is_eq(remove("/tmp/myfile2.txt"),0)
}

void test_fgetc()
{
    FILE *pFile;
    int c;
    int n = 0;
    pFile = fopen("tests/stdio.c", "r");
    is_not_null(pFile);

    do
    {
        c = fgetc(pFile);
        if (c == '$')
            n++;
    } while (c != EOF);
    fclose(pFile);

    is_eq(n, 2);
}

void test_fgets()
{
    FILE *pFile;
    char *mystring;
    char dummy[20];

    pFile = fopen("tests/stdio.c", "r");
    is_not_null(pFile);

    mystring = fgets(dummy, 20, pFile);
    is_not_null(mystring);

    fclose(pFile);
}

void test_fgets2()
{
    FILE *pFile;
    char *mystring;
    char buffer[5];

    pFile = fopen("/tmp/mylog.txt", "w");
    fputs("asdfgh\nijk\n", pFile);
    fclose(pFile);

    pFile = fopen("/tmp/mylog.txt", "r");
    is_not_null(pFile);

    mystring = fgets(buffer, 5, pFile);
    is_not_null(mystring);
    is_streq(buffer, "asdf");
    is_streq(mystring, "asdf");
    is_true(buffer == mystring);
    is_eq(strlen(buffer), 4)
    mystring = fgets(buffer, 5, pFile);
    is_streq(buffer, "gh\n");
    is_streq(mystring, "gh\n");
    is_eq(strlen(buffer), 3)
    mystring = fgets(buffer, 5, pFile);
    is_streq(buffer, "ijk\n");
    is_streq(mystring, "ijk\n");
    is_eq(strlen(buffer), 4);
    is_eq(buffer[4], 0);
    is_false(feof(pFile));
    is_false(ferror(pFile));
    mystring = fgets(buffer, 5, pFile);
    is_streq(buffer, "ijk\n");
    is_true(NULL == mystring);
    is_true(feof(pFile));
    is_false(ferror(pFile));

    fclose(pFile);

    // remove temp file
    is_eq(remove("/tmp/mylog.txt"),0)
}

void test_fputc()
{
    char c;

    fputc('#', stdout);
    for (c = 'A'; c <= 'Z'; c++)
        fputc(c, stdout);
    fputc('\n', stdout);
}

void test_fputs()
{
    FILE *pFile;
    char *sentence = "Hello, World";

    pFile = fopen("/tmp/mylog.txt", "w");
    fputs(sentence, pFile);
    fclose(pFile);
	// remove temp file
    is_eq(remove("/tmp/mylog.txt"),0)
}

void test_getc()
{
    FILE *pFile;
    int c;
    int n = 0;
    pFile = fopen("tests/stdio.c", "r");
    is_not_null(pFile);

    do
    {
        c = getc(pFile);
        if (c == '$')
            n++;
    } while (c != EOF);
    fclose(pFile);

    is_eq(n, 2);
}

void test_putc()
{
    FILE *pFile;
    char c;

    pFile = fopen("/tmp/whatever.txt", "w");
    for (c = 'A'; c <= 'Z'; c++)
    {
        putc(c, pFile);
    }
    fclose(pFile);
	// remove temp file
    is_eq(remove("/tmp/whatever.txt"),0)
}

void test_fseek()
{
    FILE *pFile;
    pFile = fopen("/tmp/example.txt", "w");
    fputs("This is an apple.", pFile);
    fseek(pFile, 9, SEEK_SET);
    fputs(" sam", pFile);
    fclose(pFile);
	// remove temp file
    is_eq(remove("/tmp/example.txt"),0)
}

void test_ftell()
{
    FILE *pFile;
    long size;

    pFile = fopen("tests/stdio.c", "r");
    is_not_null(pFile);

    fseek(pFile, 0, SEEK_END); // non-portable
    size = ftell(pFile);
    fclose(pFile);

    is_eq(size, filesize);
}

void test_fread()
{
    FILE *pFile;
    int lSize;
    char buffer[1024];
    int result;

    pFile = fopen("tests/getchar.c", "r");
    is_not_null(pFile);

    // obtain file size:
    fseek(pFile, 0, SEEK_END);
    lSize = ftell(pFile);
    is_eq(lSize, 216);

    rewind(pFile);

    // copy the file into the buffer:
    result = fread(buffer, 1, lSize, pFile);
    is_eq(result, lSize);

    // See issue #107
    buffer[lSize - 1] = 0;

    is_eq(strlen(buffer), 215);

    // terminate
    fclose(pFile);
}

void test_fwrite()
{
    FILE *pFile;
    pFile = fopen("/tmp/myfile.bin", "w");
    fwrite("xyz", 1, 3, pFile);
    fclose(pFile);
	// remove temp file
    is_eq(remove("/tmp/myfile.bin"),0)
}

void test_fgetpos()
{
    FILE *pFile;
    int c;
    int n;
    fpos_t pos;

    pFile = fopen("tests/stdio.c", "r");
    is_not_null(pFile);

    c = fgetc(pFile);
    is_eq(c, '/');

    fgetpos(pFile, &pos);
    for (n = 0; n < 3; n++)
    {
        fsetpos(pFile, &pos);
        c = fgetc(pFile);
        is_eq(c, '/');
    }

    fclose(pFile);
}

void test_fsetpos()
{
    FILE *pFile;
    fpos_t position;

    pFile = fopen("/tmp/myfile.txt", "w");
    fgetpos(pFile, &position);
    fputs("That is a sample", pFile);
    fsetpos(pFile, &position);
    fputs("This", pFile);
    fclose(pFile);
	// remove temp file
    is_eq(remove("/tmp/myfile.txt"),0)
}

void test_rewind()
{
    int n;
    FILE *pFile;
    char buffer[27];

    pFile = fopen("/tmp/myfile.txt", "w+");
    for (n = 'A'; n <= 'Z'; n++)
        fputc(n, pFile);
    rewind(pFile);
    fread(buffer, 1, 26, pFile);
    fclose(pFile);
    buffer[26] = '\0';

    is_eq(strlen(buffer), 26);
	
	// remove temp file
    is_eq(remove("/tmp/myfile.txt"),0)
}

void test_feof()
{
    FILE *pFile;
    int n = 0;
    pFile = fopen("tests/stdio.c", "r");
    is_not_null(pFile);

    while (fgetc(pFile) != EOF)
    {
        ++n;
    }
    if (feof(pFile))
    {
        pass("%s", "End-of-File reached.");
        is_eq(n, filesize);
    }
    else
    {
        fail("%s", "End-of-File was not reached.");
    }

    fclose(pFile);
}

void test_sprintf()
{
	char buffer [100];
	int cx;
	cx = snprintf ( buffer, 100, "The half of %d is %d", 60, 60/2 );
	is_streq(buffer,"The half of 60 is 30");
	is_eq(cx,20);
}

void test_snprintf()
{
	char buffer [50];
	int n, a=5, b=3;
	n = sprintf (buffer, "%d plus %d is %d", a, b, a+b);
	is_streq(buffer, "5 plus 3 is 8")
	is_eq(n,13)
}

int PrintFError(const char * format, ... )
{
	char buffer[256];
	va_list args;
	va_start (args, format);
	int s = vsprintf (buffer,format, args);
	va_end (args);
	return s;
}

void test_vsprintf()
{
	int s = PrintFError("Success function '%s' %.2f","vsprintf",3.1415926);
	is_eq(s,19+8+5);
}

int PrintFError2(const char * format, ... )
{
	char buffer[256];
	va_list args;
	va_start (args, format);
	int s = vsnprintf (buffer,256,format, args);
	va_end (args);
	return s;
}

void test_vsnprintf()
{
	int s = PrintFError2("Success function '%s' %.2f","vsprintf",3.1415926);
	is_eq(s,19+8+5);
}

void test_eof()
{
	if ( (int)(EOF) == -1 ) {
		pass("ok");
	}
	char c = EOF;
	if ( c == (char)(EOF) ) {
		pass("ok");
	}
	char a[1];
	a[0] = 's';
	if ( a[0] != EOF ) {
		pass("ok");
	}
	a[0] = EOF;
	if ( a[0] != EOF ) {
		fail("EOF == EOF - fail");
	} else {
		pass("ok");
	}
}

int main()
{
    plan(90);

    START_TEST(putchar)
    START_TEST(puts)
    START_TEST(printf)
    START_TEST(remove)
    START_TEST(rename)
    START_TEST(fopen)
    START_TEST(tmpfile)
    START_TEST(tmpnam)
    START_TEST(fclose)
    START_TEST(fflush)
    START_TEST(printf)
    START_TEST(fprintf)
    START_TEST(fscanf)
    START_TEST(fgetc)
    START_TEST(fgets)
    START_TEST(fgets2)
    START_TEST(fputc)
    START_TEST(fputs)
    START_TEST(getc)
    START_TEST(putc)
    START_TEST(fseek)
    START_TEST(ftell)
    START_TEST(fread)
    START_TEST(fwrite)
    START_TEST(fgetpos)
    START_TEST(fsetpos)
    START_TEST(rewind)
    START_TEST(feof)
    START_TEST(sprintf)
    START_TEST(snprintf)
    START_TEST(vsprintf)
    START_TEST(vsnprintf)
	START_TEST(eof)
	START_TEST(strerror)

    done_testing();
}