2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
10 #define SHOW_PROGRESS 0
13 int global_test_counter
= 0;
14 int global_test_step_counter
= 0;
15 int global_failure_indicator
= 0;
17 FILE * testfilepointer
= NULL
;
18 size_t buffersize
= 1024;
19 char writedata
[1024] = {0};
20 char * testfilename
= "fseek_test_file";
22 void reset_global_test_counter()
24 global_test_counter
= 0;
25 global_test_step_counter
= 0;
30 global_test_counter
++;
31 global_test_step_counter
= 0;
36 global_test_step_counter
++;
39 // Reporting functions
40 void report(const char * status
, const char * message
)
42 printf("REPORT : %s : %s \n", status
, message
);
45 void report_progress(const char * message
)
47 report("PROGRESS", message
);
50 void report_failure(const char * message
)
52 report("FAILED", message
);
55 void report_failure_strerror()
57 report_failure((const char*)strerror(errno
));
60 void test_report(const char * status
, const char * message
)
62 printf("TEST %d-%d : %s : %s \n", global_test_counter
, global_test_step_counter
, status
, message
);
65 void test_report_progress(const char * message
)
67 #if SHOW_PROGRESS == 1
68 test_report("PROGRESS", message
);
72 void test_report_failure(const char * message
)
74 test_report("FAILED", message
);
77 void test_report_success(const char * message
)
79 test_report("OK", message
);
82 void test_report_description(const char * message
)
84 test_report("TEST DESCRIPTION", message
);
87 void test_report_failure_strerror()
89 test_report_failure((const char*)strerror(errno
));
92 void erase_buffer(char * buffer
, size_t size
)
94 memset(buffer
, 0, size
);
97 int compare_buffers(const char * buffer1
, const char * buffer2
, size_t bufferssize
)
99 return memcmp(buffer1
, buffer2
, bufferssize
);
102 void close_test_file()
104 if (testfilepointer
!= NULL
)
106 if (fclose(testfilepointer
) != 0)
108 report_failure_strerror();
109 report_failure("Failed to close the file.");
112 testfilepointer
= NULL
;
117 int test_fseek_ftell_preparation(const char * fopenflags
)
119 size_t operationsize
= 0;
121 report_progress("Starting test_fseek_ftell_preparation");
122 report_progress(fopenflags
);
124 if (testfilepointer
!= NULL
)
126 report_failure("Pointer for test file not closed.");
131 erase_buffer(writedata
, buffersize
);
133 // open file, write data
134 testfilepointer
= fopen(testfilename
, fopenflags
);
135 if (testfilepointer
== NULL
)
137 report_failure_strerror();
138 report_failure("Failed to open file for writting.");
141 operationsize
= fwrite(writedata
, 1, buffersize
, testfilepointer
);
142 if (operationsize
!= buffersize
)
144 report_failure_strerror();
145 report_failure("Failed to write data.");
150 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
152 report_failure_strerror();
153 report_failure("Failed to seek to file beginning.");
161 int test_fseek_ftell()
164 /* Expects that testfilepointer is opened in correct mode */
165 int ftellposition
= 0;
168 report_progress("Start test_fseek_ftell");
176 test_report_description("seek to begin, tell location");
178 // seek to begin, tell location
179 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
181 test_report_failure_strerror();
182 test_report_failure("Failed to seek to beginning of file");
185 test_report_progress("Seek to beginning of file.");
186 ftellposition
= ftell(testfilepointer
);
187 if (ftellposition
== -1)
189 test_report_failure_strerror();
190 test_report_failure("Failed to tell position");
194 if (ftellposition
!= 0)
196 test_report_failure("Wrong position in stream reported");
197 sprintf(conv
, "%d", ftellposition
);
198 test_report_failure(conv
);
201 test_report_success("Correct position in stream reported");
202 test_report_success("SUCCESS");
214 test_report_description("seek foreward, tell location");
216 // seek to begin, tell location
217 if (fseek(testfilepointer
, 153, SEEK_CUR
) != 0)
219 test_report_failure_strerror();
220 test_report_failure("Failed to seek foreward");
223 test_report_progress("Seek foreward made.");
224 ftellposition
= ftell(testfilepointer
);
225 if (ftellposition
== -1)
227 test_report_failure_strerror();
228 test_report_failure("Failed to tell position");
232 if (ftellposition
!= 153)
234 test_report_failure("Wrong position in stream reported");
235 sprintf(conv
, "%d", ftellposition
);
236 test_report_failure(conv
);
239 test_report_success("Correct position in stream reported");
240 test_report_success("SUCCESS");
252 test_report_description("seek backwards, tell location");
254 // seek backwards, tell location
255 if (fseek(testfilepointer
, -80, SEEK_CUR
) != 0)
257 test_report_failure_strerror();
258 test_report_failure("Failed to seek backwards");
261 test_report_progress("Seek backwards made.");
262 ftellposition
= ftell(testfilepointer
);
263 if (ftellposition
== -1)
265 test_report_failure_strerror();
266 test_report_failure("Failed to tell position");
270 if (ftellposition
!= (153-80))
272 test_report_failure("Wrong position in stream reported");
273 sprintf(conv
, "%d", ftellposition
);
274 test_report_failure(conv
);
277 test_report_success("Correct position in stream reported");
278 test_report_success("SUCCESS");
290 test_report_description("seek to the end, tell location");
292 // seek to begin, tell location
293 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
295 test_report_failure_strerror();
296 test_report_failure("Failed to seek to end of file");
299 test_report_progress("Seek to end of file made.");
300 ftellposition
= ftell(testfilepointer
);
301 if (ftellposition
== -1)
303 test_report_failure_strerror();
304 test_report_failure("Failed to tell position");
308 if (ftellposition
!= buffersize
)
310 test_report_failure("Wrong position in stream reported");
311 sprintf(conv
, "%d", ftellposition
);
312 test_report_failure(conv
);
315 test_report_success("Correct position in stream reported");
316 test_report_success("SUCCESS");
328 test_report_description("seek foreward(beyond file), tell location");
330 // seek foreward(beyond file), tell location
331 if (fseek(testfilepointer
, 120, SEEK_CUR
) != 0)
333 test_report_failure_strerror();
334 test_report_failure("Failed to seek beyond file end");
337 test_report_progress("Seek beyond file end made.");
338 ftellposition
= ftell(testfilepointer
);
339 if (ftellposition
== -1)
341 test_report_failure_strerror();
342 test_report_failure("Failed to tell position");
346 if (ftellposition
!= buffersize
+ 120)
348 test_report_failure("Wrong position in stream reported");
349 sprintf(conv
, "%d", ftellposition
);
350 test_report_failure(conv
);
353 test_report_success("Correct position in stream reported");
354 test_report_success("SUCCESS");
366 test_report_description("seek backwards, tell location");
368 // seek backwards, tell location
369 if (fseek(testfilepointer
, -240, SEEK_CUR
) != 0)
371 test_report_failure_strerror();
372 test_report_failure("Failed to seek backwards");
375 test_report_progress("Seek backwards made.");
376 ftellposition
= ftell(testfilepointer
);
377 if (ftellposition
== -1)
379 test_report_failure_strerror();
380 test_report_failure("Failed to tell position");
384 if (ftellposition
!= buffersize
- 120)
386 test_report_failure("Wrong position in stream reported");
387 sprintf(conv
, "%d", ftellposition
);
388 test_report_failure(conv
);
391 test_report_success("Correct position in stream reported");
392 test_report_success("SUCCESS");
404 test_report_description("seek to end with positive offset, tell location");
406 // seek to end with positive offset, tell location
407 if (fseek(testfilepointer
, 120, SEEK_END
) != 0)
409 test_report_failure_strerror();
410 test_report_failure("Failed to seek to end with positive offset");
413 test_report_progress("Seek to end with positive offset made.");
414 ftellposition
= ftell(testfilepointer
);
415 if (ftellposition
== -1)
417 test_report_failure_strerror();
418 test_report_failure("Failed to tell position");
422 if (ftellposition
!= buffersize
+ 120)
424 test_report_failure("Wrong position in stream reported");
425 sprintf(conv
, "%d", ftellposition
);
426 test_report_failure(conv
);
429 test_report_success("Correct position in stream reported");
430 test_report_success("SUCCESS");
439 int test_fseek_ftell_wrapper(const char * fopenflags
)
443 int result
= test_fseek_ftell_preparation(fopenflags
);
446 report_failure("Preparation failed");
449 result
= test_fseek_ftell();
474 int test_fseek_and_fread_preparation(const char * fopenflags
)
476 char readdatabuffer1
[1024] = {0};
478 size_t operationsize
= 0;
480 report_progress("Starting test_fseek_and_fread_preparation");
481 report_progress(fopenflags
);
483 if (testfilepointer
!= NULL
)
485 report_failure("Pointer for test file not closed.");
489 // initialize with random data (always the same because of const seed)
491 for(i
= 0; i
< buffersize
; i
++)
492 writedata
[i
] = (char)rand();
494 // open file, write data, close file
495 testfilepointer
= fopen(testfilename
, "wb+");
496 if (testfilepointer
== NULL
)
498 report_failure_strerror();
499 report_failure("Failed to open file for writting.");
502 operationsize
= fwrite(writedata
, 1, buffersize
, testfilepointer
);
503 if (operationsize
!= buffersize
)
505 report_failure_strerror();
506 report_failure("Failed to write data.");
510 if ((strcmp(fopenflags
, "w+b") == 0) ||
511 (strcmp(fopenflags
, "wb+") == 0))
514 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
516 report_failure_strerror();
517 report_failure("Failed to seek to file beginning (w+b).");
524 if (fclose(testfilepointer
) != 0)
526 report_failure_strerror();
527 report_failure("Failed to close file.");
532 testfilepointer
= fopen(testfilename
, "rb");
533 if (testfilepointer
== NULL
)
535 report_failure_strerror();
536 report_failure("Failed to open file for reading.");
541 //read data, compare data, close file
542 erase_buffer(readdatabuffer1
, buffersize
);
543 operationsize
= fread(readdatabuffer1
, 1, buffersize
, testfilepointer
);
544 if (operationsize
!= buffersize
)
546 report_failure_strerror();
547 report_failure("Failed to read data.");
550 if (compare_buffers(readdatabuffer1
, writedata
, buffersize
) != 0)
552 report_failure("Read data differs from written data");
556 if ((strcmp(fopenflags
, "w+b") == 0) ||
557 (strcmp(fopenflags
, "wb+") == 0))
560 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
562 report_failure_strerror();
563 report_failure("Failed to seek to file beginning (w+b).");
569 // Close file and open it with new flags
570 if (fclose(testfilepointer
) != 0)
572 report_failure_strerror();
573 report_failure("Failed to close file.");
577 testfilepointer
= fopen(testfilename
, fopenflags
);
578 if (testfilepointer
== NULL
)
580 report_failure_strerror();
581 report_failure("Failed to open file for seek testing.");
590 int test_fseek_and_fread()
593 /* Expects that testfilepointer is opened in correct mode */
594 size_t chunksize
= 128;
595 char readdatabuffer1
[1024] = {0};
597 size_t operationsize
= 0;
603 report_progress("Start test_fseek_and_fread");
607 test_report_description("seek to begin, read some data , check data");
609 // seek to begin, read some data, check data
610 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
612 test_report_failure_strerror();
613 test_report_failure("Failed to seek to beginning of file");
616 test_report_progress("Seek to beginning of file.");
617 erase_buffer(readdatabuffer1
, buffersize
);
618 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
619 if (operationsize
!= chunksize
)
621 test_report_failure_strerror();
622 test_report_failure("Failed to read chunk of data.");
625 test_report_progress("Chunk read.");
626 if (compare_buffers(readdatabuffer1
, writedata
, chunksize
) != 0)
628 test_report_failure("Read data does not match written data");
631 test_report_success("Read data match written data.");
632 test_report_success("SUCCESS");
645 test_report_description("make absolute seek, read some data , check data");
647 // make absolute seek, read some data, check data
648 if (fseek(testfilepointer
, 350, SEEK_SET
) != 0)
650 test_report_failure_strerror();
651 test_report_failure("Failed to make absolute seek");
654 test_report_progress("Absolute seek made.");
655 erase_buffer(readdatabuffer1
, buffersize
);
656 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
657 if (operationsize
!= chunksize
)
659 test_report_failure_strerror();
660 test_report_failure("Failed to read chunk of data.");
663 test_report_progress("Chunk read.");
664 if (compare_buffers(readdatabuffer1
, writedata
+ 350, chunksize
) != 0)
666 test_report_failure("Read data does not match written data");
669 test_report_success("Read data match written data.");
670 test_report_success("SUCCESS");
683 test_report_description("make relative seek backwards, read some data , check data");
684 // make relative seek backwards, read some data, check data
685 if (fseek(testfilepointer
, - (120 + chunksize
), SEEK_CUR
) != 0)
687 test_report_failure_strerror();
688 test_report_failure("Failed to make relative seek");
691 test_report_progress("Relative seek made.");
692 erase_buffer(readdatabuffer1
, buffersize
);
693 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
694 if (operationsize
!= chunksize
)
696 test_report_failure_strerror();
697 test_report_failure("Failed to read chunk of data.");
700 test_report_progress("Chunk read.");
701 if (compare_buffers(readdatabuffer1
, writedata
+ (350 - 120), chunksize
) != 0)
703 test_report_failure("Read data does not match written data");
706 test_report_success("Read data match written data.");
707 test_report_success("SUCCESS");
720 test_report_description("make relative seek forewards, read some data , check data");
721 // make relative seek forewards, read some data, check data
722 if (fseek(testfilepointer
, 138, SEEK_CUR
) != 0)
724 test_report_failure_strerror();
725 test_report_failure("Failed to make relative seek");
728 test_report_progress("Relative seek made.");
729 erase_buffer(readdatabuffer1
, buffersize
);
730 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
731 if (operationsize
!= chunksize
)
733 test_report_failure_strerror();
734 test_report_failure("Failed to read chunk of data.");
737 test_report_progress("Chunk read.");
738 if (compare_buffers(readdatabuffer1
, writedata
+ (350 - 120 + chunksize
+ 138), chunksize
) != 0)
740 test_report_failure("Read data does not match written data");
743 test_report_success("Read data match written data.");
744 test_report_success("SUCCESS");
758 test_report_description("seek to end, make relative seek backwards, read some data , check data");
759 // seek to end, make relative seek backwards, read some data, check data
760 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
762 test_report_failure_strerror();
763 test_report_failure("Failed to seek to end");
766 test_report_progress("Seek to end made");
767 if (fseek(testfilepointer
, -210, SEEK_CUR
) != 0)
769 test_report_failure_strerror();
770 test_report_failure("Failed to make relative seek");
773 test_report_progress("Relative seek made.");
774 erase_buffer(readdatabuffer1
, buffersize
);
775 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
776 if (operationsize
!= chunksize
)
778 test_report_failure_strerror();
779 test_report_failure("Failed to read chunk of data.");
782 test_report_progress("Chunk read.");
783 if (compare_buffers(readdatabuffer1
, writedata
+ buffersize
- 210, chunksize
) != 0)
785 test_report_failure("Read data does not match written data");
788 test_report_success("Read data match written data.");
789 test_report_success("SUCCESS");
802 test_report_description("read some data (more than is in file) , check error code data");
803 // read some data (more than is in file) , check error code data
804 erase_buffer(readdatabuffer1
, buffersize
);
805 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
806 if (operationsize
!= chunksize
)
808 if (errno
!= 0/*rb*/ && errno
!= EINVAL
/*a+b, w+b, r+b*/)
810 test_report_failure_strerror();
811 test_report_failure("Error different than expected reported");
817 test_report_failure("Number of read data should be different than requested data");
820 test_report_success("Error reported");
821 test_report_success("SUCCESS");
834 test_report_description("seek to beginning, seek forewards, seek backwards beyond file start, check error code");
835 // seek to beginning, seek forewards, seek backwards beyond file start, check error code
836 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
838 test_report_failure_strerror();
839 test_report_failure("Failed to seek to beginning");
842 test_report_progress("Seek to beginning made.");
843 if (fseek(testfilepointer
, 99, SEEK_CUR
) != 0)
845 test_report_failure_strerror();
846 test_report_failure("Failed to make relative seek forewards");
849 test_report_progress("Relative seek forewards made.");
850 if (fseek(testfilepointer
, -150, SEEK_CUR
) != 0)
854 test_report_failure_strerror();
855 test_report_failure("Error different than expected reported");
861 test_report_progress("Should not made relative seek backwards beyond file start.");
864 test_report_success("Error reported");
865 test_report_success("SUCCESS");
878 test_report_description("seek to end, seek forewards, read some data, check error code");
879 // seek to end, seek forewards, read some data, check error code
880 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
882 test_report_failure_strerror();
883 test_report_failure("Failed to seek to end");
886 test_report_progress("Seek to end made.");
887 if (fseek(testfilepointer
, 99, SEEK_CUR
) != 0)
889 test_report_failure_strerror();
890 test_report_failure("Failed to make relative seek forewards");
893 test_report_progress("Relative seek forewards made.");
894 erase_buffer(readdatabuffer1
, buffersize
);
895 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
896 if (operationsize
!= chunksize
)
900 test_report_failure_strerror();
901 test_report_failure("Error different than expected reported");
907 test_report_failure("Number of read data should be different than requested data");
910 test_report_success("Error reported");
911 test_report_success("SUCCESS");
924 test_report_description("seek to end with offset, read some data, check error code");
925 // seek to end with offset, read some data, check error code
926 if (fseek(testfilepointer
, 100, SEEK_END
) != 0)
928 test_report_failure_strerror();
929 test_report_failure("Failed to seek to end with offset");
932 test_report_progress("Seek to end with offset made.");
933 erase_buffer(readdatabuffer1
, buffersize
);
934 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
935 if (operationsize
!= chunksize
)
939 test_report_failure_strerror();
940 test_report_failure("Error different than expected reported");
946 test_report_failure("Number of read data should be different than requested data");
949 test_report_success("Error reported");
950 test_report_success("SUCCESS");
954 int test_fseek_and_fread_wrapper(const char * fopenflags
)
958 int result
= test_fseek_and_fread_preparation(fopenflags
);
961 report_failure("Preparation failed");
964 result
= test_fseek_and_fread();
971 int test_fseek_fwrite_fread_preparation()
973 char readdatabuffer1
[1024] = {0};
975 size_t operationsize
= 0;
977 report_progress("Starting test_fseek_fwrite_fread_preparation");
979 if (testfilepointer
!= NULL
)
981 report_failure("Pointer for test file not closed.");
985 // Initilize random write buffer
987 for (i
= 0; i
< buffersize
; i
++)
988 writedata
[i
] = (char)rand();
990 // open file, write data
991 testfilepointer
= fopen(testfilename
, "wb+");
992 if (testfilepointer
== NULL
)
994 report_failure_strerror();
995 report_failure("Failed to open file for writting.");
998 operationsize
= fwrite(writedata
, 1, buffersize
, testfilepointer
);
999 if (operationsize
!= buffersize
)
1001 report_failure_strerror();
1002 report_failure("Failed to write data.");
1006 // Seek to beginning
1007 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1009 report_failure_strerror();
1010 report_failure("Failed to seek to file beginning (w+b).");
1014 //read data, compare data
1015 erase_buffer(readdatabuffer1
, buffersize
);
1016 operationsize
= fread(readdatabuffer1
, 1, buffersize
, testfilepointer
);
1017 if (operationsize
!= buffersize
)
1019 report_failure_strerror();
1020 report_failure("Failed to read data.");
1023 if (compare_buffers(readdatabuffer1
, writedata
, buffersize
) != 0)
1025 report_failure("Read data differs from written data");
1029 // Seek to beginning
1030 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1032 report_failure_strerror();
1033 report_failure("Failed to seek to file beginning (w+b).");
1041 int test_fseek_fwrite_fread()
1043 /* Expects that testfilepointer is opened in correct mode */
1044 char randomwritebuffer
[1024] = {0};
1045 size_t chunksize
= 128;
1046 char readdatabuffer1
[1024] = {0};
1047 char zerobuffer
[1024] = {0};
1049 size_t operationsize
= 0;
1051 // Initilize random write buffer
1052 // Don't initialize srand or randomwritebuffer == writedata
1053 for (i
= 0; i
< buffersize
; i
++)
1054 randomwritebuffer
[i
] = (char)rand();
1060 report_progress("Start test_fseek_fwrite_fread");
1064 test_report_description("seek to begin, write some data , seek to begin, read 2x some data, check data");
1066 // seek to begin, write some data , seek to begin, read 2x some data, check data
1067 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1069 test_report_failure_strerror();
1070 test_report_failure("Failed to seek to beginning of file");
1073 test_report_progress("Seek to beginning of file.");
1074 operationsize
= fwrite(randomwritebuffer
, 1, chunksize
, testfilepointer
);
1075 if (operationsize
!= chunksize
)
1077 test_report_failure_strerror();
1078 test_report_failure("Failed to write chunk of data.");
1081 test_report_progress("Chunk written.");
1082 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1084 test_report_failure_strerror();
1085 test_report_failure("Failed to seek to beginning of file");
1088 test_report_progress("Seek to beginning of file.");
1089 erase_buffer(readdatabuffer1
, buffersize
);
1090 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1091 if (operationsize
!= 2 * chunksize
)
1093 test_report_failure_strerror();
1094 test_report_failure("Failed to read chunk of data.");
1097 test_report_progress("Chunk read.");
1098 if (compare_buffers(readdatabuffer1
, randomwritebuffer
, chunksize
) != 0)
1100 test_report_failure("Read data does not match written data for first chunk");
1103 test_report_success("Read data match written data for first chunk.");
1104 if (compare_buffers(readdatabuffer1
+ chunksize
, writedata
+ chunksize
, chunksize
) != 0)
1106 test_report_failure("Read data does not match written data for second chunk");
1109 test_report_success("Read data match written data for second chunk.");
1110 test_report_success("SUCCESS");
1122 test_report_description("seek foreward, write some data , seek backward, read data, check data");
1124 // seek foreward, write some data , seek backward, read data, check data
1125 if (fseek(testfilepointer
, 3 * chunksize
, SEEK_CUR
) != 0)
1127 test_report_failure_strerror();
1128 test_report_failure("Failed to seek foreward");
1131 test_report_progress("Seek foreward made.");
1132 operationsize
= fwrite(randomwritebuffer
, 1, chunksize
, testfilepointer
);
1133 if (operationsize
!= chunksize
)
1135 test_report_failure_strerror();
1136 test_report_failure("Failed to write chunk of data.");
1139 test_report_progress("Chunk written.");
1140 if (fseek(testfilepointer
, - chunksize
, SEEK_CUR
) != 0)
1142 test_report_failure_strerror();
1143 test_report_failure("Failed to seek backwards");
1146 test_report_progress("Seek backwards made.");
1147 erase_buffer(readdatabuffer1
, buffersize
);
1148 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
1149 if (operationsize
!= chunksize
)
1151 test_report_failure_strerror();
1152 test_report_failure("Failed to read chunk of data.");
1155 test_report_progress("Chunk read.");
1156 if (compare_buffers(readdatabuffer1
, randomwritebuffer
, chunksize
) != 0)
1158 test_report_failure("Read data does not match written");
1161 test_report_success("Read data match written data.");
1162 test_report_success("SUCCESS");
1174 test_report_description("seek backward, write some data , seek backward, read 2 x data, check data");
1176 // seek backward, write some data , seek backward, read 2 x data, check data
1177 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1179 test_report_failure_strerror();
1180 test_report_failure("Failed to seek backwards");
1183 test_report_progress("Seek backwards made.");
1184 operationsize
= fwrite(randomwritebuffer
+ chunksize
, 1, chunksize
, testfilepointer
);
1185 if (operationsize
!= chunksize
)
1187 test_report_failure_strerror();
1188 test_report_failure("Failed to write chunk of data.");
1191 test_report_progress("Chunk written.");
1192 if (fseek(testfilepointer
, -chunksize
, SEEK_CUR
) != 0)
1194 test_report_failure_strerror();
1195 test_report_failure("Failed to seek backwards");
1198 test_report_progress("Seek backwards made.");
1199 erase_buffer(readdatabuffer1
, buffersize
);
1200 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1201 if (operationsize
!= 2 * chunksize
)
1203 test_report_failure_strerror();
1204 test_report_failure("Failed to read chunk of data.");
1207 test_report_progress("Chunk read.");
1208 if (compare_buffers(readdatabuffer1
, randomwritebuffer
+ chunksize
, chunksize
) != 0)
1210 test_report_failure("Read data does not match written for first chunk");
1213 test_report_success("Read data match written data for first chunk.");
1214 if (compare_buffers(readdatabuffer1
+ chunksize
, randomwritebuffer
, chunksize
) != 0)
1216 test_report_failure("Read data does not match written for second chunk");
1219 test_report_success("Read data match written data for second chunk.");
1220 test_report_success("SUCCESS");
1232 test_report_description("seek to end, write some data , seek backward, read data, check data");
1234 // seek to end, write some data , seek backward, read data, check data
1235 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
1237 test_report_failure_strerror();
1238 test_report_failure("Failed to seek to end");
1241 test_report_progress("Seek to end of file made.");
1242 operationsize
= fwrite(randomwritebuffer
+ 2 * chunksize
, 1, chunksize
, testfilepointer
);
1243 if (operationsize
!= chunksize
)
1245 test_report_failure_strerror();
1246 test_report_failure("Failed to write chunk of data.");
1249 test_report_progress("Chunk written.");
1250 if (fseek(testfilepointer
, -chunksize
, SEEK_CUR
) != 0)
1252 test_report_failure_strerror();
1253 test_report_failure("Failed to seek backwards");
1256 test_report_progress("Seek backwards made.");
1257 erase_buffer(readdatabuffer1
, buffersize
);
1258 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
1259 if (operationsize
!= chunksize
)
1261 test_report_failure_strerror();
1262 test_report_failure("Failed to read chunk of data.");
1265 test_report_progress("Chunk read.");
1266 if (compare_buffers(readdatabuffer1
, randomwritebuffer
+ 2 * chunksize
, chunksize
) != 0)
1268 test_report_failure("Read data does not match written data");
1271 test_report_success("Read data match written data.");
1272 test_report_success("SUCCESS");
1284 test_report_description("seek to end, seek foreward, write some data , seek backward, read 2 x data, check data");
1286 // seek to end, seek foreward, write some data , seek backward, read 2 x data, check data
1287 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
1289 test_report_failure_strerror();
1290 test_report_failure("Failed to seek to end");
1293 test_report_progress("Seek to end of file made.");
1294 if (fseek(testfilepointer
, chunksize
, SEEK_CUR
) != 0)
1296 test_report_failure_strerror();
1297 test_report_failure("Failed to seek foreward");
1300 test_report_progress("Seek foreward made.");
1301 operationsize
= fwrite(randomwritebuffer
, 1, chunksize
, testfilepointer
);
1302 if (operationsize
!= chunksize
)
1304 test_report_failure_strerror();
1305 test_report_failure("Failed to write chunk of data.");
1308 test_report_progress("Chunk written.");
1309 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1311 test_report_failure_strerror();
1312 test_report_failure("Failed to seek backwards");
1315 test_report_progress("Seek backwards made.");
1316 erase_buffer(readdatabuffer1
, buffersize
);
1317 erase_buffer(zerobuffer
, buffersize
);
1318 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1319 if (operationsize
!= 2 * chunksize
)
1321 test_report_failure_strerror();
1322 test_report_failure("Failed to read chunk of data.");
1325 test_report_progress("Chunk read.");
1326 if (compare_buffers(readdatabuffer1
, zerobuffer
, chunksize
) != 0)
1328 test_report_failure("Read data does not match written data for first chunk");
1331 test_report_success("Read data match written data for first chunk.");
1332 if (compare_buffers(readdatabuffer1
+ chunksize
, randomwritebuffer
, chunksize
) != 0)
1334 test_report_failure("Read data does not match written data for second chunk");
1337 test_report_success("Read data match written data for second chunk.");
1338 test_report_success("SUCCESS");
1350 test_report_description("seek beoynd end, write some data , seek backward, read 2 x data, check data");
1352 // seek beoynd end, write some data , seek backward, read 2 x data, check data
1353 if (fseek(testfilepointer
, chunksize
, SEEK_END
) != 0)
1355 test_report_failure_strerror();
1356 test_report_failure("Failed to seek beyond end of file");
1359 test_report_progress("Seek beyond end of file made.");
1360 operationsize
= fwrite(randomwritebuffer
+ chunksize
, 1, chunksize
, testfilepointer
);
1361 if (operationsize
!= chunksize
)
1363 test_report_failure_strerror();
1364 test_report_failure("Failed to write chunk of data.");
1367 test_report_progress("Chunk written.");
1368 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1370 test_report_failure_strerror();
1371 test_report_failure("Failed to seek backwards");
1374 test_report_progress("Seek backwards made.");
1375 erase_buffer(readdatabuffer1
, buffersize
);
1376 erase_buffer(zerobuffer
, buffersize
);
1377 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1378 if (operationsize
!= 2 * chunksize
)
1380 test_report_failure_strerror();
1381 test_report_failure("Failed to read chunk of data.");
1384 test_report_progress("Chunk read.");
1385 if (compare_buffers(readdatabuffer1
, zerobuffer
, chunksize
) != 0)
1387 test_report_failure("Read data does not match written data for first chunk");
1390 test_report_success("Read data match written data for first chunk.");
1391 if (compare_buffers(readdatabuffer1
+ chunksize
, randomwritebuffer
+ chunksize
, chunksize
) != 0)
1393 test_report_failure("Read data does not match written data for second chunk");
1396 test_report_success("Read data match written data for second chunk.");
1397 test_report_success("SUCCESS");
1406 int test_fseek_fwrite_fread_wrapper()
1410 int result
= test_fseek_fwrite_fread_preparation();
1413 report_failure("Preparation failed");
1416 result
= test_fseek_fwrite_fread();
1423 int test_fseek_fwrite_fread_append_preparation()
1425 char readdatabuffer1
[1024] = {0};
1427 size_t operationsize
= 0;
1429 report_progress("Starting test_fseek_fwrite_fread_append_preparation");
1431 if (testfilepointer
!= NULL
)
1433 report_failure("Pointer for test file not closed.");
1437 // Initilize random write buffer
1439 for (i
= 0; i
< buffersize
; i
++)
1440 writedata
[i
] = (char)rand();
1442 // open file, write data
1443 testfilepointer
= fopen(testfilename
, "wb+");
1444 if (testfilepointer
== NULL
)
1446 report_failure_strerror();
1447 report_failure("Failed to open file for writting.");
1450 operationsize
= fwrite(writedata
, 1, buffersize
, testfilepointer
);
1451 if (operationsize
!= buffersize
)
1453 report_failure_strerror();
1454 report_failure("Failed to write data.");
1458 // Seek to beginning
1459 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1461 report_failure_strerror();
1462 report_failure("Failed to seek to file beginning (w+b).");
1466 //read data, compare data
1467 erase_buffer(readdatabuffer1
, buffersize
);
1468 operationsize
= fread(readdatabuffer1
, 1, buffersize
, testfilepointer
);
1469 if (operationsize
!= buffersize
)
1471 report_failure_strerror();
1472 report_failure("Failed to read data.");
1475 if (compare_buffers(readdatabuffer1
, writedata
, buffersize
) != 0)
1477 report_failure("Read data differs from written data");
1482 if (fclose(testfilepointer
) != 0)
1484 report_failure_strerror();
1485 report_failure("Failed to close the file.");
1490 testfilepointer
= fopen(testfilename
, "ab+");
1491 if (testfilepointer
== NULL
)
1493 report_failure_strerror();
1494 report_failure("Failed to open file for appending.");
1502 int test_fseek_fwrite_fread_append()
1504 /* Expects that testfilepointer is opened in correct mode */
1505 char randomwritebuffer
[1024] = {0};
1506 size_t chunksize
= 128;
1507 char readdatabuffer1
[1024] = {0};
1508 char zerobuffer
[1024] = {0};
1510 size_t operationsize
= 0;
1512 // Initilize random write buffer
1513 // Don't initialize srand or randomwritebuffer == writedata
1514 for (i
= 0; i
< buffersize
; i
++)
1515 randomwritebuffer
[i
] = (char)rand();
1521 report_progress("Start test_fseek_fwrite_fread_append");
1525 test_report_description("seek to begin, write some data , seek to begin, read 2x some data, check data");
1527 // seek to begin, write some data , seek to begin, read 2x some data, check data
1528 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1530 test_report_failure_strerror();
1531 test_report_failure("Failed to seek to beginning of file");
1534 test_report_progress("Seek to beginning of file.");
1535 operationsize
= fwrite(randomwritebuffer
, 1, chunksize
, testfilepointer
);
1536 if (operationsize
!= chunksize
)
1538 test_report_failure_strerror();
1539 test_report_failure("Failed to write chunk of data.");
1542 test_report_progress("Chunk written.");
1543 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1545 test_report_failure_strerror();
1546 test_report_failure("Failed to seek to beginning of file");
1549 test_report_progress("Seek to beginning of file.");
1550 erase_buffer(readdatabuffer1
, buffersize
);
1551 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1552 if (operationsize
!= 2 * chunksize
)
1554 test_report_failure_strerror();
1555 test_report_failure("Failed to read chunk of data.");
1558 test_report_progress("Chunk read.");
1559 if (compare_buffers(readdatabuffer1
, writedata
, chunksize
) != 0)
1561 test_report_failure("Read data does not match written data for first chunk");
1564 test_report_success("Read data match written data for first chunk.");
1565 if (compare_buffers(readdatabuffer1
+ chunksize
, writedata
+ chunksize
, chunksize
) != 0)
1567 test_report_failure("Read data does not match written data for second chunk");
1570 test_report_success("Read data match written data for second chunk.");
1571 test_report_success("SUCCESS");
1583 test_report_description("seek foreward, write some data , seek backward, read data, check data");
1585 // seek foreward, write some data , seek backward, read data, check data
1586 if (fseek(testfilepointer
, 3 * chunksize
, SEEK_CUR
) != 0)
1588 test_report_failure_strerror();
1589 test_report_failure("Failed to seek foreward");
1592 test_report_progress("Seek foreward made.");
1593 operationsize
= fwrite(randomwritebuffer
, 1, chunksize
, testfilepointer
);
1594 if (operationsize
!= chunksize
)
1596 test_report_failure_strerror();
1597 test_report_failure("Failed to write chunk of data.");
1600 test_report_progress("Chunk written.");
1601 if (fseek(testfilepointer
, - chunksize
, SEEK_CUR
) != 0)
1603 test_report_failure_strerror();
1604 test_report_failure("Failed to seek backwards");
1607 test_report_progress("Seek backwards made.");
1608 erase_buffer(readdatabuffer1
, buffersize
);
1609 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
1610 if (operationsize
!= chunksize
)
1612 test_report_failure_strerror();
1613 test_report_failure("Failed to read chunk of data.");
1616 test_report_progress("Chunk read.");
1617 if (compare_buffers(readdatabuffer1
, randomwritebuffer
, chunksize
) != 0)
1619 test_report_failure("Read data does not match written");
1622 test_report_success("Read data match written data.");
1623 test_report_success("SUCCESS");
1635 test_report_description("seek backward, write some data , seek backward, read 2 x data, check error");
1637 // seek backward, write some data , seek backward, read 2 x data, check error
1638 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1640 test_report_failure_strerror();
1641 test_report_failure("Failed to seek backwards");
1644 test_report_progress("Seek backwards made.");
1645 operationsize
= fwrite(randomwritebuffer
+ chunksize
, 1, chunksize
, testfilepointer
);
1646 if (operationsize
!= chunksize
)
1648 test_report_failure_strerror();
1649 test_report_failure("Failed to write chunk of data.");
1652 test_report_progress("Chunk written.");
1653 if (fseek(testfilepointer
, -chunksize
, SEEK_CUR
) != 0)
1655 test_report_failure_strerror();
1656 test_report_failure("Failed to seek backwards");
1659 test_report_progress("Seek backwards made.");
1660 erase_buffer(readdatabuffer1
, buffersize
);
1661 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1662 if (operationsize
!= 2 * chunksize
)
1664 if (errno
!= EINVAL
)
1666 test_report_failure_strerror();
1667 test_report_failure("Different than expected error reported.");
1673 test_report_failure("Expected error");
1676 test_report_success("Error reported.");
1677 test_report_success("SUCCESS");
1689 test_report_description("seek to end, write some data , seek backward, read data, check data");
1691 // seek to end, write some data , seek backward, read data, check data
1692 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
1694 test_report_failure_strerror();
1695 test_report_failure("Failed to seek to end");
1698 test_report_progress("Seek to end of file made.");
1699 operationsize
= fwrite(randomwritebuffer
+ 2 * chunksize
, 1, chunksize
, testfilepointer
);
1700 if (operationsize
!= chunksize
)
1702 test_report_failure_strerror();
1703 test_report_failure("Failed to write chunk of data.");
1706 test_report_progress("Chunk written.");
1707 if (fseek(testfilepointer
, -chunksize
, SEEK_CUR
) != 0)
1709 test_report_failure_strerror();
1710 test_report_failure("Failed to seek backwards");
1713 test_report_progress("Seek backwards made.");
1714 erase_buffer(readdatabuffer1
, buffersize
);
1715 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
1716 if (operationsize
!= chunksize
)
1718 test_report_failure_strerror();
1719 test_report_failure("Failed to read chunk of data.");
1722 test_report_progress("Chunk read.");
1723 if (compare_buffers(readdatabuffer1
, randomwritebuffer
+ 2 * chunksize
, chunksize
) != 0)
1725 test_report_failure("Read data does not match written data");
1728 test_report_success("Read data match written data.");
1729 test_report_success("SUCCESS");
1741 test_report_description("seek to end, seek foreward, write some data , seek backward, read 2 x data, check data");
1743 // seek to end, seek foreward, write some data , seek backward, read 2 x data, check data
1744 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
1746 test_report_failure_strerror();
1747 test_report_failure("Failed to seek to end");
1750 test_report_progress("Seek to end of file made.");
1751 if (fseek(testfilepointer
, chunksize
, SEEK_CUR
) != 0)
1753 test_report_failure_strerror();
1754 test_report_failure("Failed to seek foreward");
1757 test_report_progress("Seek foreward made.");
1758 operationsize
= fwrite(randomwritebuffer
+ 3 * chunksize
, 1, chunksize
, testfilepointer
);
1759 if (operationsize
!= chunksize
)
1761 test_report_failure_strerror();
1762 test_report_failure("Failed to write chunk of data.");
1765 test_report_progress("Chunk written.");
1766 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1768 test_report_failure_strerror();
1769 test_report_failure("Failed to seek backwards");
1772 test_report_progress("Seek backwards made.");
1773 erase_buffer(readdatabuffer1
, buffersize
);
1774 erase_buffer(zerobuffer
, buffersize
);
1775 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1776 if (operationsize
!= 2 * chunksize
)
1778 test_report_failure_strerror();
1779 test_report_failure("Failed to read chunk of data.");
1782 test_report_progress("Chunk read.");
1783 if (compare_buffers(readdatabuffer1
, randomwritebuffer
+ 2 * chunksize
, chunksize
) != 0)
1785 test_report_failure("Read data does not match written data for first chunk");
1788 test_report_success("Read data match written data for first chunk.");
1789 if (compare_buffers(readdatabuffer1
+ chunksize
, randomwritebuffer
+ 3 * chunksize
, chunksize
) != 0)
1791 test_report_failure("Read data does not match written data for second chunk");
1794 test_report_success("Read data match written data for second chunk.");
1795 test_report_success("SUCCESS");
1807 test_report_description("seek beyond end, write some data , seek backward, read 2 x data, check data");
1809 // seek beoynd end, write some data , seek backward, read 2 x data, check data
1810 if (fseek(testfilepointer
, chunksize
, SEEK_END
) != 0)
1812 test_report_failure_strerror();
1813 test_report_failure("Failed to seek beyond end of file");
1816 test_report_progress("Seek beyond end of file made.");
1817 operationsize
= fwrite(randomwritebuffer
+ chunksize
, 1, chunksize
, testfilepointer
);
1818 if (operationsize
!= chunksize
)
1820 test_report_failure_strerror();
1821 test_report_failure("Failed to write chunk of data.");
1824 test_report_progress("Chunk written.");
1825 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1827 test_report_failure_strerror();
1828 test_report_failure("Failed to seek backwards");
1831 test_report_progress("Seek backwards made.");
1832 erase_buffer(readdatabuffer1
, buffersize
);
1833 erase_buffer(zerobuffer
, buffersize
);
1834 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1835 if (operationsize
!= 2 * chunksize
)
1837 test_report_failure_strerror();
1838 test_report_failure("Failed to read chunk of data.");
1841 test_report_progress("Chunk read.");
1842 if (compare_buffers(readdatabuffer1
, randomwritebuffer
+ 3 * chunksize
, chunksize
) != 0)
1844 test_report_failure("Read data does not match written data for first chunk");
1847 test_report_success("Read data match written data for first chunk.");
1848 if (compare_buffers(readdatabuffer1
+ chunksize
, randomwritebuffer
+ chunksize
, chunksize
) != 0)
1850 test_report_failure("Read data does not match written data for second chunk");
1853 test_report_success("Read data match written data for second chunk.");
1854 test_report_success("SUCCESS");
1863 int test_fseek_fwrite_fread_append_wrapper()
1867 int result
= test_fseek_fwrite_fread_append_preparation();
1870 report_failure("Preparation failed");
1873 result
= test_fseek_fwrite_fread_append();
1882 report_progress("Starting tests");
1884 reset_global_test_counter();
1887 if (test_fseek_ftell_wrapper("wb") != 0)
1888 global_failure_indicator
= 1;
1889 if (test_fseek_ftell_wrapper("w+b") != 0)
1890 global_failure_indicator
= 1;
1894 // wb - can only write
1895 // ab - can only append
1896 if (test_fseek_and_fread_wrapper("rb") != 0)
1897 global_failure_indicator
= 1;
1899 if (test_fseek_and_fread_wrapper("r+b") != 0)
1900 global_failure_indicator
= 1;
1902 if (test_fseek_and_fread_wrapper("a+b") != 0)
1903 global_failure_indicator
= 1;
1905 if (test_fseek_and_fread_wrapper("w+b") != 0)
1906 global_failure_indicator
= 1;
1909 if (test_fseek_fwrite_fread_wrapper() != 0)
1910 global_failure_indicator
= 1;
1912 // seek/write/read (append)
1913 if (test_fseek_fwrite_fread_append_wrapper() != 0)
1914 global_failure_indicator
= 1;
1916 if (global_failure_indicator
== 1)
1917 report_failure("One of the tests FAILED");
1919 report_progress("All tests SUCCEEDED");
1921 report_progress("Tests finished");