forked from adtools/clib2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_clib_functions.c
2687 lines (2192 loc) · 47.4 KB
/
test_clib_functions.c
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Checks the clib support functions
#include <stdarg.h>
#include <stdio.h>
#include <stdio_headers.h>
#include <exec/types.h>
#include <proto/exec.h>
#include <exec/tasks.h>
#include <proto/dos.h>
#include <devices/input.h>
#include <clib/alib_protos.h>
#include <locale_headers.h>
#include <stdlib_constructor.h>
#include <stdlib_headers.h>
#include <dirent.h>
#include <macros.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <ftw.h>
#include <inttypes.h>
#include <sys/mount.h>
#include <setjmp.h>
#include <sys/uio.h>
#include <ulimit.h>
#include <errno.h>
#include <sys/utsname.h>
#include <libgen.h>
#define RAM_PATH "RAM:"
#define RAM_DISK "Ram Disk:"
#define RAM_TEST_FILE "RAM:test_file"
#define RAM_DISK_TEST_FILE "Ram Disk:test_file"
#define RAM_TEST_HARD_LINK "RAM:test_file_hard_link"
#define NEW_RAM_TEST_FILE "RAM:new_test_file"
#define RAM_TEMP_FILE "RAM:temp_file_XXXXXX"
#define RAM_TEMP_FILE_BIS "RAM:temp_file_bis_XXXXXX"
#define RAM_TEST_DIR "RAM:test_dir"
#define RAM_TEMP_DIR "RAM:temp_dir_XXXXXX"
#define DH0_TEST_FILE "DH0:test_file"
#define DH0_TEST_SOFT_LINK "DH0:test_file_soft_link"
#define TEST_STRING_1 "In the beginning "
#define TEST_STRING_2 "God created the heaven and the Earth."
#define TEST_STRING_1_2 "In the beginning God created the heaven and the Earth."
#define BUFFER_SIZE 256
#define FTW_NFTW_DEPTH 4
#define DOS_TYPE "DOS"
#define ERROR_STR_TEST "Test of error string"
#define PIPE_CMD "C:echo "
#define PIPE_TEST_STR "pipe test"
#define LOVE_STRING "JOB loves Amiga"
#define AMIGA_STRING "Amiga"
#define NBR1 12
#define NBR2 24
#define SETENV_VAR_NAME "AMIGA"
#define SETENV_VAR_VALUE "for ever"
#define UNKNOWN_ERROR "Unknown error"
#define UID 4
#define GID 8
#define TRUNCATE_LENGTH 3
#define LOCKF_LEN 4
#define OPTION_TRACE_CLIB 0
extern struct ExecBase *SysBase;
extern struct DosLibrary *DOSBase;
extern LONG __divsi3(LONG dividend,LONG divisor);
extern LONG __divsi4(LONG dividend,LONG divisor);
extern LONG __modsi3(LONG dividend,LONG divisor);
extern LONG __mulsi3(LONG Arg1,LONG Arg2);
extern ULONG __udivsi3(ULONG dividend,ULONG divisor);
extern ULONG __udivsi4(ULONG dividend,ULONG divisor);
extern ULONG __umodsi3(ULONG dividend,ULONG divisor);
static char buffer1[BUFFER_SIZE],buffer2[BUFFER_SIZE],buffer3[BUFFER_SIZE],buffer4[BUFFER_SIZE],test_strings[BUFFER_SIZE],file_buffer[BUFSIZ],file_buffer2[BUFSIZ];
static BOOL SIGINT_caught;
static FILE *file_ptr;
static WORD long_jmp_var;
static jmp_buf env;
static WORD errCnt;
// ftw callback function
static int ftw_callback(const char *name,const struct stat *stat,int flags)
{
#if OPTION_TRACE_CLIB
Printf("ftw: name = %s, st_size = %lu, flags = %lu\n",name,stat->st_size,flags);
#endif
return(0);
}
// nftw callback function
static int nftw_callback(const char *name,const struct stat *stat,int flags,struct FTW *ftw)
{
#if OPTION_TRACE_CLIB
Printf("nftw: name = %s, st_size = %lu, flags = %lu, base = %u, level = %u\n",name,stat->st_size,flags,ftw->base,ftw->level);
#endif
return(0);
}
// Start wait command as a CLI process
static LONG fcntStartWait(void)
{
struct TagItem stags[4];
stags[0].ti_Tag = SYS_Input;
stags[0].ti_Data = (ULONG)Open("NIL:",MODE_READWRITE);
stags[1].ti_Tag = SYS_Output;
stags[1].ti_Data = (ULONG)Open("NIL:",MODE_READWRITE);
stags[2].ti_Tag = SYS_Asynch;
stags[2].ti_Data = (ULONG)TRUE;
stags[3].ti_Tag = TAG_END;
SystemTagList("C:wait 10",stags);
Delay(50); // wait for command to start
return(MaxCli() - 1); // CLI number
}
// Signal handler
static void catch_int(int sig_num)
{
if(sig_num == SIGINT)
SIGINT_caught = TRUE;
}
// Process used to lock (and unlock) a file
static VOID fcntProcLockFile(void)
{
flockfile(file_ptr);
Delay(500); // delay before releasing lock
funlockfile(file_ptr);
}
// To test vasprintf
__attribute__((noinline)) int vasprintf_test(char **ret, const char *format, ...)
{
int result = EOF;
va_list arg;
va_start(arg,format);
result = vasprintf(ret,format,arg);
va_end(arg);
return(result);
}
// To test vfprintf
__attribute__((noinline)) int vfprintf_test(FILE *file_ptr,const char *format, ...)
{
int result = EOF;
va_list arg;
va_start(arg,format);
result = vfprintf(file_ptr,format,arg);
va_end(arg);
return(result);
}
// To test vfscanf
__attribute__((noinline)) int vfscanf_test(FILE *file_ptr,const char *format, ...)
{
int result = EOF;
va_list arg;
va_start(arg,format);
result = vfscanf(file_ptr,format,arg);
va_end(arg);
return(result);
}
// To test vprintf
__attribute__((noinline)) int vprintf_test(const char *format, ...)
{
int result = EOF;
va_list arg;
va_start(arg,format);
result = vprintf(format,arg);
va_end(arg);
return(result);
}
// To test vsnprintf
__attribute__((noinline)) int vsnprintf_test(char *s,size_t n,char *format, ...)
{
int result = EOF;
va_list arg;
va_start(arg,format);
result = vsnprintf(s,n,format,arg);
va_end(arg);
return(result);
}
// To test vsprintf
__attribute__((noinline)) int vsprintf_test(char *s,char *format, ...)
{
int result = EOF;
va_list arg;
va_start(arg,format);
result = vsprintf(s,format,arg);
va_end(arg);
return(result);
}
// To test vsscanf
__attribute__((noinline)) int vsscanf_test(char *s,char *format, ...)
{
int result = EOF;
va_list arg;
va_start(arg,format);
result = vsscanf(s,format,arg);
va_end(arg);
return(result);
}
// To test vscanf
__attribute__((noinline)) int vscanf_test(char *format, ...)
{
int result = EOF;
va_list arg;
va_start(arg,format);
result = vscanf(format,arg);
va_end(arg);
return(result);
}
// Signal handler for SIGABRT
static void catch_SIGABRT(int sig_num)
{
if(sig_num == SIGABRT)
Printf("catch_SIGABRT called\n");
}
// Function called at exit
static void exit_function(void)
{
Printf("Exit function called\n");
}
// To test bsearch
#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]))
#define THIRD_MONTH "mar"
struct mi {
int nr;
const char *name;
};
static struct mi months[] = {
{ 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
{ 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
{ 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
};
static int compmi(const void *m1, const void *m2)
{
const struct mi *mi1 = m1;
const struct mi *mi2 = m2;
return strcmp(mi1->name, mi2->name);
}
// To test longjmp
static void fcnt_long_jmp1(void)
{
long_jmp_var = 1;
longjmp(env,10);
long_jmp_var = 2;
}
WORD testClibFunctions(void)
{
errCnt = 0;
// Test stdio functions requiring an input from the user
// Test getchar, putchar, getchar_unlocked, putchar_unlocked, gets, puts
int c_int;
char *str_test;
printf("Please type a letter: \n");
if((c_int = getchar()) == EOF)
{
errCnt++;
}
else
{
printf("You typed ");
if(putchar((char)c_int) == EOF)
errCnt++;
printf("\n");
}
getchar(); // for \n
printf("Please type another letter: \n");
if((c_int = getchar_unlocked()) == EOF)
{
errCnt++;
}
else
{
printf("You typed ");
if(putchar_unlocked((char)c_int) == EOF)
errCnt++;
printf("\n");
}
getchar(); // for \n
memset(buffer1,0,BUFFER_SIZE);
printf("Please type a string: \n");
if((str_test = gets(buffer1)) == NULL)
{
errCnt++;
}
else
{
printf("You typed ");
if(puts(str_test) == EOF)
errCnt++;
}
// Test scanf, vscanf
int nb1,nb2;
printf("Please type two numbers separated by a space:\n");
if(scanf("%d %d",&nb1,&nb2) != 2)
{
errCnt++;
printf("scanf error\n");
}
else
{
printf("%d + %d = %d\n",nb1,nb2,nb1 + nb2);
}
printf("Please type two other numbers separated by a space:\n");
if(vscanf_test("%d %d",&nb1,&nb2) != 2)
{
errCnt++;
printf("vscanf error\n");
}
else
{
printf("%d + %d = %d\n",nb1,nb2,nb1 + nb2);
}
#if OPTION_TRACE_CLIB
printf("TRACE1 errCnt = %u\n",errCnt);
#endif
// Test isalnum
if(!isalnum('J')) errCnt++;
if(!isalnum('5')) errCnt++;
if(isalnum(0xFF)) errCnt++;
// Test isalpha
if(!isalpha('O')) errCnt++;
if(!isalpha('o')) errCnt++;
if(isalpha('5')) errCnt++;
// Test isascii
if(!isascii('B')) errCnt++;
if(!isascii('0')) errCnt++;
if(isascii(128)) errCnt++;
// Test isblank
if(!isblank('\t')) errCnt++;
if(!isblank(' ')) errCnt++;
if(isblank('A')) errCnt++;
// Test iscntrl
if(!iscntrl('\0')) errCnt++;
if(!iscntrl(127)) errCnt++;
if(iscntrl('A')) errCnt++;
// Test isdigit
if(!isdigit('4')) errCnt++;
if(!isdigit('2')) errCnt++;
if(isdigit('A')) errCnt++;
// Test isgraph
if(!isgraph('A')) errCnt++;
if(!isgraph(126)) errCnt++;
if(isgraph(' ')) errCnt++;
// Test islower
if(!islower('j')) errCnt++;
if(!islower('o')) errCnt++;
if(islower('J')) errCnt++;
// Test isprint
if(!isprint('A')) errCnt++;
if(!isprint('o')) errCnt++;
if(isprint(0xFF)) errCnt++;
// Test ispunct
if(!ispunct('!')) errCnt++;
if(!ispunct('?')) errCnt++;
if(ispunct('B')) errCnt++;
// Test isspace
if(!isspace(' ')) errCnt++;
if(!isspace('\r')) errCnt++;
if(isspace('B')) errCnt++;
// Test isupper
if(!isupper('A')) errCnt++;
if(!isupper('B')) errCnt++;
if(isupper('c')) errCnt++;
// Test isxdigit
if(!isxdigit('C')) errCnt++;
if(!isxdigit('d')) errCnt++;
if(isxdigit('G')) errCnt++;
// Test tolower
if(tolower('C') != 'c') errCnt++;
if(tolower('B') != 'b') errCnt++;
if(tolower('a') != 'a') errCnt++;
// Test toupper
if(toupper('c') != 'C') errCnt++;
if(toupper('b') != 'B') errCnt++;
if(toupper('A') != 'A') errCnt++;
#if OPTION_TRACE_CLIB
printf("TRACE2 errCnt = %u\n",errCnt);
#endif
// Test dir functions (opendir, readdir, rewinddir, closedir)
DIR *dir;
struct dirent *dir_entry;
int file_desc;
Printf("Listing twice RAM: content...\n");
if((dir = opendir(RAM_PATH)) == NULL)
{
errCnt++;
}
else
{
while((dir_entry = readdir(dir)) != NULL)
{
Printf("%s\n",dir_entry->d_name);
}
rewinddir(dir);
while((dir_entry = readdir(dir)) != NULL)
{
Printf("%s\n",dir_entry->d_name);
}
closedir(dir);
}
// Test fcntl functions (creat, write, close, open, lseek, read, fcntl)
size_t test_str1_len = strlen(TEST_STRING_1);
size_t test_str2_len = strlen(TEST_STRING_2);
#if OPTION_TRACE_CLIB
printf("TRACE3 errCnt = %u\n",errCnt);
#endif
// Write first string in file
if((file_desc = creat(RAM_TEST_FILE,S_IRWXU)) == ERROR) // mode is not used
{
errCnt++;
}
else
{
test_str1_len = strlen(TEST_STRING_1);
if((size_t)write(file_desc,TEST_STRING_1,test_str1_len) != test_str1_len)
{
errCnt++;
}
if(close(file_desc) == ERROR)
{
errCnt++;
}
}
#if OPTION_TRACE_CLIB
printf("TRACE4 errCnt = %u\n",errCnt);
#endif
// Write second string in file
if((file_desc = open(RAM_TEST_FILE,O_WRONLY | O_APPEND)) == ERROR)
{
errCnt++;
}
else
{
if(lseek(file_desc,0L,SEEK_END) == SEEK_ERROR)
{
errCnt++;
}
else
{
if((size_t)write(file_desc,TEST_STRING_2,test_str2_len) != test_str2_len)
{
errCnt++;
}
if(close(file_desc) == ERROR)
{
errCnt++;
}
}
}
#if OPTION_TRACE_CLIB
printf("TRACE5 errCnt = %u\n",errCnt);
#endif
// Read strings back (and check fcntl)
if((file_desc = open(RAM_TEST_FILE,O_RDONLY)) == ERROR)
{
errCnt++;
}
else
{
memset(buffer1,0,BUFFER_SIZE);
if(read(file_desc,buffer1,test_str1_len + test_str2_len) != (ssize_t)(test_str1_len + test_str2_len))
{
errCnt++;
}
if(fcntl(file_desc,F_GETFL) != 0)
{
errCnt++;
}
if(close(file_desc) == ERROR)
{
errCnt++;
}
}
strcpy(test_strings,TEST_STRING_1);
strcat(test_strings,TEST_STRING_2);
if(strncmp(buffer1,test_strings,test_str1_len + test_str2_len))
errCnt++;
#if OPTION_TRACE_CLIB
printf("TRACE6 errCnt = %u\n",errCnt);
#endif
// Test ftw and nftw functions
if(ftw(RAM_PATH,ftw_callback,FTW_NFTW_DEPTH) != 0)
errCnt++;
if(nftw(RAM_PATH,nftw_callback,FTW_NFTW_DEPTH,FTW_PHYS) != 0)
errCnt++;
#if defined(USE_64_BIT_INTS) // long long is not supported
// Test imaxdiv, imaxabs, strtoimax, strtoumax
intmax_t a = 9223372036854775807,b = 223372036854775807,c;
uintmax_t d = 18446744073709551615,e;
imaxdiv_t result;
result = imaxdiv(a,b);
if(result.quot != (intmax_t)41 || result.rem != (intmax_t)65118525808967720)
errCnt++;
c = imaxabs(-a);
if(a != c)
errCnt++;
c = strtoimax("9223372036854775807",NULL,10);
if(a != c)
errCnt++;
e = strtoumax("18446744073709551615",NULL,10);
if(d != e)
errCnt++;
#endif
#if OPTION_TRACE_CLIB
printf("TRACE7 errCnt = %u\n",errCnt);
#endif
// Test basename and dirname
if(strcmp(basename("RAM:a/b/c/d/e.txt"),"e.txt"))
errCnt++;
if(strcmp(dirname("RAM:a/b/c/d/e.txt"),"RAM:a/b/c/d"))
errCnt++;
#if OPTION_TRACE_CLIB
printf("TRACE8 errCnt = %u\n",errCnt);
#endif
// Test locale functions (setlocale, localeconv)
struct lconv *lconv;
if(setlocale(LC_ALL,NULL) == NULL) // default locale
{
errCnt++;
}
else
{
if(strcmp(localeconv()->decimal_point,"."))
errCnt++;
}
#if OPTION_TRACE_CLIB
printf("TRACE9 errCnt = %u\n",errCnt);
#endif
// Test fstatfs and statfs
struct statfs stat_fs;
if(statfs(RAM_TEST_FILE,&stat_fs) == ERROR)
{
errCnt++;
}
else
{
if(strncmp(stat_fs.f_fstypename,DOS_TYPE,strlen(DOS_TYPE)))
errCnt++;
}
if((file_desc = open(RAM_TEST_FILE,O_RDONLY)) == ERROR)
{
errCnt++;
}
else
{
if(fstatfs(file_desc,&stat_fs) == ERROR)
{
errCnt++;
}
else
{
if(strncmp(stat_fs.f_fstypename,DOS_TYPE,strlen(DOS_TYPE)))
errCnt++;
close(file_desc);
}
}
#if OPTION_TRACE_CLIB
printf("TRACE10 errCnt = %u\n",errCnt);
#endif
// Test signals (kill, raise, sigaddset, sigblock, sigemptyset, sigmask, signal, sigprocmask, sigsetmask)
ULONG maxCLI;
sigset_t sigset;
maxCLI = fcntStartWait(); // start a CLI wait process
kill((pid_t)FindCliProc(maxCLI),SIGTERM);
Delay(250); // wait for command to end
if(MaxCli() != maxCLI) // the CLI should have ended
errCnt++;
SIGINT_caught = FALSE;
signal(SIGINT,catch_int);
raise(SIGINT);
Delay(250); // wait for signal to be transmitted
if(SIGINT_caught == FALSE)
errCnt++;
sigset = 1 << SIGFPE;
sigemptyset(&sigset);
if(sigset != 0L)
errCnt++;
sigset = sigmask(SIGILL);
if(sigset != 1 << SIGILL)
errCnt++;
sigaddset(&sigset,SIGSEGV);
if(sigset != ((1 << SIGILL) | (1 << SIGSEGV)))
errCnt++;
sigsetmask(sigmask(SIGINT));
SIGINT_caught = FALSE;
signal(SIGINT,catch_int);
raise(SIGINT);
Delay(250); // wait for signal to be transmitted
if(SIGINT_caught != FALSE) // signal must not be transmitted as it is blocked
errCnt++;
sigset = sigmask(SIGINT);
sigprocmask(SIG_UNBLOCK,&sigset,NULL); // unblock SIGINT
SIGINT_caught = FALSE;
signal(SIGINT,catch_int);
raise(SIGINT);
Delay(250); // wait for signal to be transmitted
if(SIGINT_caught == FALSE) // signal should be transmitted
errCnt++;
sigblock(sigmask(SIGINT));
SIGINT_caught = FALSE;
signal(SIGINT,catch_int);
raise(SIGINT);
Delay(250); // wait for signal to be transmitted
if(SIGINT_caught != FALSE) // signal must not be transmitted as it is blocked
errCnt++;
#if OPTION_TRACE_CLIB
printf("TRACE11 errCnt = %u\n",errCnt);
#endif
// Test stat functions (chmod, stat, lstat, fchmod, fstat, umask, mkdir, rmdir)
struct stat file_stat;
if(chmod(RAM_TEST_FILE,S_IWUSR | S_IRUSR | S_IXUSR) == ERROR) // set read, write and execute for user
errCnt++;
if(stat(RAM_TEST_FILE,&file_stat) == ERROR)
{
errCnt++;
}
else
{
if(file_stat.st_mode & S_IXUSR == 0L)
errCnt++;
}
if(lstat(RAM_TEST_FILE,&file_stat) == ERROR)
{
errCnt++;
}
else
{
if(file_stat.st_mode & S_IXUSR == 0L)
errCnt++;
}
if((file_desc = open(RAM_TEST_FILE,O_RDONLY)) == ERROR)
{
errCnt++;
}
else
{
if(fchmod(file_desc,S_IWUSR | S_IRUSR) == ERROR) // do not set execute
errCnt++;
if(fstat(file_desc,&file_stat) == ERROR)
{
errCnt++;
}
else
{
if(file_stat.st_mode & S_IXUSR != 0L)
errCnt++;
}
close(file_desc);
}
umask(S_IRWXU);
if(umask(S_IRWXG) != S_IRWXU)
errCnt++;
if(mkdir(RAM_TEST_DIR,S_IRWXU) == ERROR)
{
errCnt++;
}
else
{
if((dir = opendir(RAM_TEST_DIR)) == NULL)
{
errCnt++;
}
else
{
closedir(dir);
if(rmdir(RAM_TEST_DIR) == ERROR)
{
errCnt++;
}
else
{
if((dir = opendir(RAM_TEST_DIR)) != NULL)
errCnt++;
}
}
}
#if OPTION_TRACE_CLIB
printf("TRACE12 errCnt = %u\n",errCnt);
#endif
// Test stdio functions
char *ret;
// Test asprintf
if(asprintf(&ret,"%s%s",TEST_STRING_1,TEST_STRING_2) != (int)(test_str1_len + test_str2_len))
{
errCnt++;
}
else
{
if(strncmp(ret,test_strings,test_str1_len + test_str2_len))
errCnt++;
free(ret);
}
#if OPTION_TRACE_CLIB
printf("TRACE13 errCnt = %u\n",errCnt);
#endif
// Test fopen, fprintf, fflush, fclose, fread
if((file_ptr = fopen(RAM_TEST_FILE,"w")) == NULL)
{
errCnt++;
}
else
{
if(fprintf(file_ptr,"%s%s",TEST_STRING_1,TEST_STRING_2) != (int)(test_str1_len + test_str2_len))
{
errCnt++;
}
else
{
if(fflush(file_ptr) != 0)
errCnt++;
fclose(file_ptr);
file_ptr = NULL;
if((file_ptr = fopen(RAM_TEST_FILE,"r")) == NULL)
{
errCnt++;
}
else
{
memset(buffer1,0,BUFFER_SIZE);
if(fread(buffer1,test_str1_len + test_str2_len,1,file_ptr) != 1)
{
errCnt++;
}
else
{
if(strncmp(buffer1,test_strings,test_str1_len + test_str2_len))
{
errCnt++;
}
}
fclose(file_ptr);
}
}
}
#if OPTION_TRACE_CLIB
printf("TRACE14 errCnt = %u\n",errCnt);
#endif
// Test snprintf
memset(buffer1,0,BUFFER_SIZE);
if(snprintf(buffer1,test_str1_len + 1,"%s%s",TEST_STRING_1,TEST_STRING_2) != (int)(test_str1_len + test_str2_len)) // + 1 to include the ending '\0'
{
errCnt++;
}
else
{
if(strncmp(buffer1,test_strings,strlen(test_str1_len)))
errCnt++;
}
#if OPTION_TRACE_CLIB
printf("TRACE15 errCnt = %u\n",errCnt);
#endif
// Test sprintf
memset(buffer1,0,BUFFER_SIZE);
if(sprintf(buffer1,"%s%s",TEST_STRING_1,TEST_STRING_2) != (int)(test_str1_len + test_str2_len))
{
errCnt++;
}
else
{
if(strncmp(buffer1,test_strings,test_str1_len + test_str2_len))
errCnt++;
}
#if OPTION_TRACE_CLIB
printf("TRACE16 errCnt = %u\n",errCnt);
#endif
// Test vprintf
printf("Testing vprintf:");
if(vprintf_test(buffer1,BUFFER_SIZE,"%s%s",TEST_STRING_1,TEST_STRING_2) != (int)strlen(TEST_STRING_1_2))
errCnt++;
#if OPTION_TRACE_CLIB
printf("TRACE17 errCnt = %u\n",errCnt);
#endif
// Test fgetc, fgetpos, fsetpos, fgets, fread, feof, perror
if((file_ptr = fopen(RAM_TEST_FILE,"r")) == NULL)
{
errCnt++;
}
else
{
fpos_t pos;
if(fgetc(file_ptr) != (int)test_strings[0])
errCnt++;
if(fgetpos(file_ptr,&pos) != OK)
{
errCnt++;
}
else
{
if(pos != (fpos_t)1)
{
errCnt++;
}
}
pos = (fpos_t)0;
if(fsetpos(file_ptr,&pos)!= OK)
{
errCnt++;
}
else
{
memset(buffer1,0,BUFFER_SIZE);
if(fgets(buffer1,test_str1_len + 1,file_ptr) == NULL)
{
errCnt++;
}
else
{
if(strncmp(buffer1,test_strings,test_str1_len))
errCnt++;
}
}
memset(buffer1,0,BUFFER_SIZE);
if(fread(buffer1,BUFFER_SIZE,1,file_ptr) != 0) // read beyond the file
{
errCnt++;
}
else
{
if(feof(file_ptr) == 0)
{
errCnt++;
}
else
{
clearerr(file_ptr); // clear EOF
if(feof(file_ptr) != 0)
errCnt++;
}
}