5 #define SHOW_PROGRESS 0
8 int global_test_counter
= 0;
9 int global_test_step_counter
= 0;
10 int global_failure_indicator
= 0;
12 FILE * testfilepointer
= NULL
;
13 size_t buffersize
= 1024;
14 char writedata
[1024] = {0};
15 char * testfilename
= "fseek_test_file";
17 void reset_global_test_counter()
19 global_test_counter
= 0;
20 global_test_step_counter
= 0;
25 global_test_counter
++;
26 global_test_step_counter
= 0;
31 global_test_step_counter
++;
34 // Reporting functions
35 void report(const char * status
, const char * message
)
37 printf("REPORT : %s : %s \n", status
, message
);
40 void report_progress(const char * message
)
42 report("PROGRESS", message
);
45 void report_failure(const char * message
)
47 report("FAILED", message
);
50 void report_failure_strerror()
52 report_failure((const char*)strerror(errno
));
55 void test_report(const char * status
, const char * message
)
57 printf("TEST %d-%d : %s : %s \n", global_test_counter
, global_test_step_counter
, status
, message
);
60 void test_report_progress(const char * message
)
62 #if SHOW_PROGRESS == 1
63 test_report("PROGRESS", message
);
67 void test_report_failure(const char * message
)
69 test_report("FAILED", message
);
72 void test_report_success(const char * message
)
74 test_report("OK", message
);
77 void test_report_description(const char * message
)
79 test_report("TEST DESCRIPTION", message
);
82 void test_report_failure_strerror()
84 test_report_failure((const char*)strerror(errno
));
87 void erase_buffer(char * buffer
, size_t size
)
89 memset(buffer
, 0, size
);
92 int compare_buffers(const char * buffer1
, const char * buffer2
, size_t bufferssize
)
94 return memcmp(buffer1
, buffer2
, bufferssize
);
97 void close_test_file()
99 if (testfilepointer
!= NULL
)
101 if (fclose(testfilepointer
) != 0)
103 report_failure_strerror();
104 report_failure("Failed to close the file.");
107 testfilepointer
= NULL
;
112 int test_fseek_ftell_preparation(const char * fopenflags
)
114 size_t operationsize
= 0;
116 report_progress("Starting test_fseek_ftell_preparation");
117 report_progress(fopenflags
);
119 if (testfilepointer
!= NULL
)
121 report_failure("Pointer for test file not closed.");
126 erase_buffer(writedata
, buffersize
);
128 // open file, write data
129 testfilepointer
= fopen(testfilename
, fopenflags
);
130 if (testfilepointer
== NULL
)
132 report_failure_strerror();
133 report_failure("Failed to open file for writting.");
136 operationsize
= fwrite(writedata
, 1, buffersize
, testfilepointer
);
137 if (operationsize
!= buffersize
)
139 report_failure_strerror();
140 report_failure("Failed to write data.");
145 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
147 report_failure_strerror();
148 report_failure("Failed to seek to file beginning.");
156 int test_fseek_ftell()
159 /* Expects that testfilepointer is opened in correct mode */
160 int ftellposition
= 0;
163 report_progress("Start test_fseek_ftell");
171 test_report_description("seek to begin, tell location");
173 // seek to begin, tell location
174 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
176 test_report_failure_strerror();
177 test_report_failure("Failed to seek to beginning of file");
180 test_report_progress("Seek to beginning of file.");
181 ftellposition
= ftell(testfilepointer
);
182 if (ftellposition
== -1)
184 test_report_failure_strerror();
185 test_report_failure("Failed to tell position");
189 if (ftellposition
!= 0)
191 test_report_failure("Wrong position in stream reported");
192 sprintf(conv
, "%d", ftellposition
);
193 test_report_failure(conv
);
196 test_report_success("Correct position in stream reported");
197 test_report_success("SUCCESS");
209 test_report_description("seek foreward, tell location");
211 // seek to begin, tell location
212 if (fseek(testfilepointer
, 153, SEEK_CUR
) != 0)
214 test_report_failure_strerror();
215 test_report_failure("Failed to seek foreward");
218 test_report_progress("Seek foreward made.");
219 ftellposition
= ftell(testfilepointer
);
220 if (ftellposition
== -1)
222 test_report_failure_strerror();
223 test_report_failure("Failed to tell position");
227 if (ftellposition
!= 153)
229 test_report_failure("Wrong position in stream reported");
230 sprintf(conv
, "%d", ftellposition
);
231 test_report_failure(conv
);
234 test_report_success("Correct position in stream reported");
235 test_report_success("SUCCESS");
247 test_report_description("seek backwards, tell location");
249 // seek backwards, tell location
250 if (fseek(testfilepointer
, -80, SEEK_CUR
) != 0)
252 test_report_failure_strerror();
253 test_report_failure("Failed to seek backwards");
256 test_report_progress("Seek backwards made.");
257 ftellposition
= ftell(testfilepointer
);
258 if (ftellposition
== -1)
260 test_report_failure_strerror();
261 test_report_failure("Failed to tell position");
265 if (ftellposition
!= (153-80))
267 test_report_failure("Wrong position in stream reported");
268 sprintf(conv
, "%d", ftellposition
);
269 test_report_failure(conv
);
272 test_report_success("Correct position in stream reported");
273 test_report_success("SUCCESS");
285 test_report_description("seek to the end, tell location");
287 // seek to begin, tell location
288 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
290 test_report_failure_strerror();
291 test_report_failure("Failed to seek to end of file");
294 test_report_progress("Seek to end of file made.");
295 ftellposition
= ftell(testfilepointer
);
296 if (ftellposition
== -1)
298 test_report_failure_strerror();
299 test_report_failure("Failed to tell position");
303 if (ftellposition
!= buffersize
)
305 test_report_failure("Wrong position in stream reported");
306 sprintf(conv
, "%d", ftellposition
);
307 test_report_failure(conv
);
310 test_report_success("Correct position in stream reported");
311 test_report_success("SUCCESS");
323 test_report_description("seek foreward(beyond file), tell location");
325 // seek foreward(beyond file), tell location
326 if (fseek(testfilepointer
, 120, SEEK_CUR
) != 0)
328 test_report_failure_strerror();
329 test_report_failure("Failed to seek beyond file end");
332 test_report_progress("Seek beyond file end made.");
333 ftellposition
= ftell(testfilepointer
);
334 if (ftellposition
== -1)
336 test_report_failure_strerror();
337 test_report_failure("Failed to tell position");
341 if (ftellposition
!= buffersize
+ 120)
343 test_report_failure("Wrong position in stream reported");
344 sprintf(conv
, "%d", ftellposition
);
345 test_report_failure(conv
);
348 test_report_success("Correct position in stream reported");
349 test_report_success("SUCCESS");
361 test_report_description("seek backwards, tell location");
363 // seek backwards, tell location
364 if (fseek(testfilepointer
, -240, SEEK_CUR
) != 0)
366 test_report_failure_strerror();
367 test_report_failure("Failed to seek backwards");
370 test_report_progress("Seek backwards made.");
371 ftellposition
= ftell(testfilepointer
);
372 if (ftellposition
== -1)
374 test_report_failure_strerror();
375 test_report_failure("Failed to tell position");
379 if (ftellposition
!= buffersize
- 120)
381 test_report_failure("Wrong position in stream reported");
382 sprintf(conv
, "%d", ftellposition
);
383 test_report_failure(conv
);
386 test_report_success("Correct position in stream reported");
387 test_report_success("SUCCESS");
399 test_report_description("seek to end with positive offset, tell location");
401 // seek to end with positive offset, tell location
402 if (fseek(testfilepointer
, 120, SEEK_END
) != 0)
404 test_report_failure_strerror();
405 test_report_failure("Failed to seek to end with positive offset");
408 test_report_progress("Seek to end with positive offset made.");
409 ftellposition
= ftell(testfilepointer
);
410 if (ftellposition
== -1)
412 test_report_failure_strerror();
413 test_report_failure("Failed to tell position");
417 if (ftellposition
!= buffersize
+ 120)
419 test_report_failure("Wrong position in stream reported");
420 sprintf(conv
, "%d", ftellposition
);
421 test_report_failure(conv
);
424 test_report_success("Correct position in stream reported");
425 test_report_success("SUCCESS");
434 int test_fseek_ftell_wrapper(const char * fopenflags
)
438 int result
= test_fseek_ftell_preparation(fopenflags
);
441 report_failure("Preparation failed");
444 result
= test_fseek_ftell();
469 int test_fseek_and_fread_preparation(const char * fopenflags
)
471 char readdatabuffer1
[1024] = {0};
473 size_t operationsize
= 0;
475 report_progress("Starting test_fseek_and_fread_preparation");
476 report_progress(fopenflags
);
478 if (testfilepointer
!= NULL
)
480 report_failure("Pointer for test file not closed.");
484 // initialize with random data (always the same because of const seed)
486 for(i
= 0; i
< buffersize
; i
++)
487 writedata
[i
] = (char)rand();
489 // open file, write data, close file
490 testfilepointer
= fopen(testfilename
, "wb+");
491 if (testfilepointer
== NULL
)
493 report_failure_strerror();
494 report_failure("Failed to open file for writting.");
497 operationsize
= fwrite(writedata
, 1, buffersize
, testfilepointer
);
498 if (operationsize
!= buffersize
)
500 report_failure_strerror();
501 report_failure("Failed to write data.");
505 if ((strcmp(fopenflags
, "w+b") == 0) ||
506 (strcmp(fopenflags
, "wb+") == 0))
509 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
511 report_failure_strerror();
512 report_failure("Failed to seek to file beginning (w+b).");
519 if (fclose(testfilepointer
) != 0)
521 report_failure_strerror();
522 report_failure("Failed to close file.");
527 testfilepointer
= fopen(testfilename
, "rb");
528 if (testfilepointer
== NULL
)
530 report_failure_strerror();
531 report_failure("Failed to open file for reading.");
536 //read data, compare data, close file
537 erase_buffer(readdatabuffer1
, buffersize
);
538 operationsize
= fread(readdatabuffer1
, 1, buffersize
, testfilepointer
);
539 if (operationsize
!= buffersize
)
541 report_failure_strerror();
542 report_failure("Failed to read data.");
545 if (compare_buffers(readdatabuffer1
, writedata
, buffersize
) != 0)
547 report_failure("Read data differs from written data");
551 if ((strcmp(fopenflags
, "w+b") == 0) ||
552 (strcmp(fopenflags
, "wb+") == 0))
555 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
557 report_failure_strerror();
558 report_failure("Failed to seek to file beginning (w+b).");
564 // Close file and open it with new flags
565 if (fclose(testfilepointer
) != 0)
567 report_failure_strerror();
568 report_failure("Failed to close file.");
572 testfilepointer
= fopen(testfilename
, fopenflags
);
573 if (testfilepointer
== NULL
)
575 report_failure_strerror();
576 report_failure("Failed to open file for seek testing.");
585 int test_fseek_and_fread()
588 /* Expects that testfilepointer is opened in correct mode */
589 size_t chunksize
= 128;
590 char readdatabuffer1
[1024] = {0};
592 size_t operationsize
= 0;
598 report_progress("Start test_fseek_and_fread");
602 test_report_description("seek to begin, read some data , check data");
604 // seek to begin, read some data, check data
605 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
607 test_report_failure_strerror();
608 test_report_failure("Failed to seek to beginning of file");
611 test_report_progress("Seek to beginning of file.");
612 erase_buffer(readdatabuffer1
, buffersize
);
613 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
614 if (operationsize
!= chunksize
)
616 test_report_failure_strerror();
617 test_report_failure("Failed to read chunk of data.");
620 test_report_progress("Chunk read.");
621 if (compare_buffers(readdatabuffer1
, writedata
, chunksize
) != 0)
623 test_report_failure("Read data does not match written data");
626 test_report_success("Read data match written data.");
627 test_report_success("SUCCESS");
640 test_report_description("make absolute seek, read some data , check data");
642 // make absolute seek, read some data, check data
643 if (fseek(testfilepointer
, 350, SEEK_SET
) != 0)
645 test_report_failure_strerror();
646 test_report_failure("Failed to make absolute seek");
649 test_report_progress("Absolute seek made.");
650 erase_buffer(readdatabuffer1
, buffersize
);
651 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
652 if (operationsize
!= chunksize
)
654 test_report_failure_strerror();
655 test_report_failure("Failed to read chunk of data.");
658 test_report_progress("Chunk read.");
659 if (compare_buffers(readdatabuffer1
, writedata
+ 350, chunksize
) != 0)
661 test_report_failure("Read data does not match written data");
664 test_report_success("Read data match written data.");
665 test_report_success("SUCCESS");
678 test_report_description("make relative seek backwards, read some data , check data");
679 // make relative seek backwards, read some data, check data
680 if (fseek(testfilepointer
, - (120 + chunksize
), SEEK_CUR
) != 0)
682 test_report_failure_strerror();
683 test_report_failure("Failed to make relative seek");
686 test_report_progress("Relative seek made.");
687 erase_buffer(readdatabuffer1
, buffersize
);
688 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
689 if (operationsize
!= chunksize
)
691 test_report_failure_strerror();
692 test_report_failure("Failed to read chunk of data.");
695 test_report_progress("Chunk read.");
696 if (compare_buffers(readdatabuffer1
, writedata
+ (350 - 120), chunksize
) != 0)
698 test_report_failure("Read data does not match written data");
701 test_report_success("Read data match written data.");
702 test_report_success("SUCCESS");
715 test_report_description("make relative seek forewards, read some data , check data");
716 // make relative seek forewards, read some data, check data
717 if (fseek(testfilepointer
, 138, SEEK_CUR
) != 0)
719 test_report_failure_strerror();
720 test_report_failure("Failed to make relative seek");
723 test_report_progress("Relative seek made.");
724 erase_buffer(readdatabuffer1
, buffersize
);
725 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
726 if (operationsize
!= chunksize
)
728 test_report_failure_strerror();
729 test_report_failure("Failed to read chunk of data.");
732 test_report_progress("Chunk read.");
733 if (compare_buffers(readdatabuffer1
, writedata
+ (350 - 120 + chunksize
+ 138), chunksize
) != 0)
735 test_report_failure("Read data does not match written data");
738 test_report_success("Read data match written data.");
739 test_report_success("SUCCESS");
753 test_report_description("seek to end, make relative seek backwards, read some data , check data");
754 // seek to end, make relative seek backwards, read some data, check data
755 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
757 test_report_failure_strerror();
758 test_report_failure("Failed to seek to end");
761 test_report_progress("Seek to end made");
762 if (fseek(testfilepointer
, -210, SEEK_CUR
) != 0)
764 test_report_failure_strerror();
765 test_report_failure("Failed to make relative seek");
768 test_report_progress("Relative seek made.");
769 erase_buffer(readdatabuffer1
, buffersize
);
770 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
771 if (operationsize
!= chunksize
)
773 test_report_failure_strerror();
774 test_report_failure("Failed to read chunk of data.");
777 test_report_progress("Chunk read.");
778 if (compare_buffers(readdatabuffer1
, writedata
+ buffersize
- 210, chunksize
) != 0)
780 test_report_failure("Read data does not match written data");
783 test_report_success("Read data match written data.");
784 test_report_success("SUCCESS");
797 test_report_description("read some data (more than is in file) , check error code data");
798 // read some data (more than is in file) , check error code data
799 erase_buffer(readdatabuffer1
, buffersize
);
800 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
801 if (operationsize
!= chunksize
)
803 if (errno
!= 0/*rb*/ && errno
!= EINVAL
/*a+b, w+b, r+b*/)
805 test_report_failure_strerror();
806 test_report_failure("Error different than expected reported");
812 test_report_failure("Number of read data should be different than requested data");
815 test_report_success("Error reported");
816 test_report_success("SUCCESS");
829 test_report_description("seek to beginning, seek forewards, seek backwards beyond file start, check error code");
830 // seek to beginning, seek forewards, seek backwards beyond file start, check error code
831 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
833 test_report_failure_strerror();
834 test_report_failure("Failed to seek to beginning");
837 test_report_progress("Seek to beginning made.");
838 if (fseek(testfilepointer
, 99, SEEK_CUR
) != 0)
840 test_report_failure_strerror();
841 test_report_failure("Failed to make relative seek forewards");
844 test_report_progress("Relative seek forewards made.");
845 if (fseek(testfilepointer
, -150, SEEK_CUR
) != 0)
849 test_report_failure_strerror();
850 test_report_failure("Error different than expected reported");
856 test_report_progress("Should not made relative seek backwards beyond file start.");
859 test_report_success("Error reported");
860 test_report_success("SUCCESS");
873 test_report_description("seek to end, seek forewards, read some data, check error code");
874 // seek to end, seek forewards, read some data, check error code
875 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
877 test_report_failure_strerror();
878 test_report_failure("Failed to seek to end");
881 test_report_progress("Seek to end made.");
882 if (fseek(testfilepointer
, 99, SEEK_CUR
) != 0)
884 test_report_failure_strerror();
885 test_report_failure("Failed to make relative seek forewards");
888 test_report_progress("Relative seek forewards made.");
889 erase_buffer(readdatabuffer1
, buffersize
);
890 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
891 if (operationsize
!= chunksize
)
895 test_report_failure_strerror();
896 test_report_failure("Error different than expected reported");
902 test_report_failure("Number of read data should be different than requested data");
905 test_report_success("Error reported");
906 test_report_success("SUCCESS");
919 test_report_description("seek to end with offset, read some data, check error code");
920 // seek to end with offset, read some data, check error code
921 if (fseek(testfilepointer
, 100, SEEK_END
) != 0)
923 test_report_failure_strerror();
924 test_report_failure("Failed to seek to end with offset");
927 test_report_progress("Seek to end with offset made.");
928 erase_buffer(readdatabuffer1
, buffersize
);
929 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
930 if (operationsize
!= chunksize
)
934 test_report_failure_strerror();
935 test_report_failure("Error different than expected reported");
941 test_report_failure("Number of read data should be different than requested data");
944 test_report_success("Error reported");
945 test_report_success("SUCCESS");
949 int test_fseek_and_fread_wrapper(const char * fopenflags
)
953 int result
= test_fseek_and_fread_preparation(fopenflags
);
956 report_failure("Preparation failed");
959 result
= test_fseek_and_fread();
966 int test_fseek_fwrite_fread_preparation()
968 char readdatabuffer1
[1024] = {0};
970 size_t operationsize
= 0;
972 report_progress("Starting test_fseek_fwrite_fread_preparation");
974 if (testfilepointer
!= NULL
)
976 report_failure("Pointer for test file not closed.");
980 // Initilize random write buffer
982 for (i
= 0; i
< buffersize
; i
++)
983 writedata
[i
] = (char)rand();
985 // open file, write data
986 testfilepointer
= fopen(testfilename
, "wb+");
987 if (testfilepointer
== NULL
)
989 report_failure_strerror();
990 report_failure("Failed to open file for writting.");
993 operationsize
= fwrite(writedata
, 1, buffersize
, testfilepointer
);
994 if (operationsize
!= buffersize
)
996 report_failure_strerror();
997 report_failure("Failed to write data.");
1001 // Seek to beginning
1002 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1004 report_failure_strerror();
1005 report_failure("Failed to seek to file beginning (w+b).");
1009 //read data, compare data
1010 erase_buffer(readdatabuffer1
, buffersize
);
1011 operationsize
= fread(readdatabuffer1
, 1, buffersize
, testfilepointer
);
1012 if (operationsize
!= buffersize
)
1014 report_failure_strerror();
1015 report_failure("Failed to read data.");
1018 if (compare_buffers(readdatabuffer1
, writedata
, buffersize
) != 0)
1020 report_failure("Read data differs from written data");
1024 // Seek to beginning
1025 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1027 report_failure_strerror();
1028 report_failure("Failed to seek to file beginning (w+b).");
1036 int test_fseek_fwrite_fread()
1038 /* Expects that testfilepointer is opened in correct mode */
1039 char randomwritebuffer
[1024] = {0};
1040 size_t chunksize
= 128;
1041 char readdatabuffer1
[1024] = {0};
1042 char zerobuffer
[1024] = {0};
1044 size_t operationsize
= 0;
1046 // Initilize random write buffer
1047 // Don't initialize srand or randomwritebuffer == writedata
1048 for (i
= 0; i
< buffersize
; i
++)
1049 randomwritebuffer
[i
] = (char)rand();
1055 report_progress("Start test_fseek_fwrite_fread");
1059 test_report_description("seek to begin, write some data , seek to begin, read 2x some data, check data");
1061 // seek to begin, write some data , seek to begin, read 2x some data, check data
1062 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1064 test_report_failure_strerror();
1065 test_report_failure("Failed to seek to beginning of file");
1068 test_report_progress("Seek to beginning of file.");
1069 operationsize
= fwrite(randomwritebuffer
, 1, chunksize
, testfilepointer
);
1070 if (operationsize
!= chunksize
)
1072 test_report_failure_strerror();
1073 test_report_failure("Failed to write chunk of data.");
1076 test_report_progress("Chunk written.");
1077 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1079 test_report_failure_strerror();
1080 test_report_failure("Failed to seek to beginning of file");
1083 test_report_progress("Seek to beginning of file.");
1084 erase_buffer(readdatabuffer1
, buffersize
);
1085 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1086 if (operationsize
!= 2 * chunksize
)
1088 test_report_failure_strerror();
1089 test_report_failure("Failed to read chunk of data.");
1092 test_report_progress("Chunk read.");
1093 if (compare_buffers(readdatabuffer1
, randomwritebuffer
, chunksize
) != 0)
1095 test_report_failure("Read data does not match written data for first chunk");
1098 test_report_success("Read data match written data for first chunk.");
1099 if (compare_buffers(readdatabuffer1
+ chunksize
, writedata
+ chunksize
, chunksize
) != 0)
1101 test_report_failure("Read data does not match written data for second chunk");
1104 test_report_success("Read data match written data for second chunk.");
1105 test_report_success("SUCCESS");
1117 test_report_description("seek foreward, write some data , seek backward, read data, check data");
1119 // seek foreward, write some data , seek backward, read data, check data
1120 if (fseek(testfilepointer
, 3 * chunksize
, SEEK_CUR
) != 0)
1122 test_report_failure_strerror();
1123 test_report_failure("Failed to seek foreward");
1126 test_report_progress("Seek foreward made.");
1127 operationsize
= fwrite(randomwritebuffer
, 1, chunksize
, testfilepointer
);
1128 if (operationsize
!= chunksize
)
1130 test_report_failure_strerror();
1131 test_report_failure("Failed to write chunk of data.");
1134 test_report_progress("Chunk written.");
1135 if (fseek(testfilepointer
, - chunksize
, SEEK_CUR
) != 0)
1137 test_report_failure_strerror();
1138 test_report_failure("Failed to seek backwards");
1141 test_report_progress("Seek backwards made.");
1142 erase_buffer(readdatabuffer1
, buffersize
);
1143 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
1144 if (operationsize
!= chunksize
)
1146 test_report_failure_strerror();
1147 test_report_failure("Failed to read chunk of data.");
1150 test_report_progress("Chunk read.");
1151 if (compare_buffers(readdatabuffer1
, randomwritebuffer
, chunksize
) != 0)
1153 test_report_failure("Read data does not match written");
1156 test_report_success("Read data match written data.");
1157 test_report_success("SUCCESS");
1169 test_report_description("seek backward, write some data , seek backward, read 2 x data, check data");
1171 // seek backward, write some data , seek backward, read 2 x data, check data
1172 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1174 test_report_failure_strerror();
1175 test_report_failure("Failed to seek backwards");
1178 test_report_progress("Seek backwards made.");
1179 operationsize
= fwrite(randomwritebuffer
+ chunksize
, 1, chunksize
, testfilepointer
);
1180 if (operationsize
!= chunksize
)
1182 test_report_failure_strerror();
1183 test_report_failure("Failed to write chunk of data.");
1186 test_report_progress("Chunk written.");
1187 if (fseek(testfilepointer
, -chunksize
, SEEK_CUR
) != 0)
1189 test_report_failure_strerror();
1190 test_report_failure("Failed to seek backwards");
1193 test_report_progress("Seek backwards made.");
1194 erase_buffer(readdatabuffer1
, buffersize
);
1195 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1196 if (operationsize
!= 2 * chunksize
)
1198 test_report_failure_strerror();
1199 test_report_failure("Failed to read chunk of data.");
1202 test_report_progress("Chunk read.");
1203 if (compare_buffers(readdatabuffer1
, randomwritebuffer
+ chunksize
, chunksize
) != 0)
1205 test_report_failure("Read data does not match written for first chunk");
1208 test_report_success("Read data match written data for first chunk.");
1209 if (compare_buffers(readdatabuffer1
+ chunksize
, randomwritebuffer
, chunksize
) != 0)
1211 test_report_failure("Read data does not match written for second chunk");
1214 test_report_success("Read data match written data for second chunk.");
1215 test_report_success("SUCCESS");
1227 test_report_description("seek to end, write some data , seek backward, read data, check data");
1229 // seek to end, write some data , seek backward, read data, check data
1230 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
1232 test_report_failure_strerror();
1233 test_report_failure("Failed to seek to end");
1236 test_report_progress("Seek to end of file made.");
1237 operationsize
= fwrite(randomwritebuffer
+ 2 * chunksize
, 1, chunksize
, testfilepointer
);
1238 if (operationsize
!= chunksize
)
1240 test_report_failure_strerror();
1241 test_report_failure("Failed to write chunk of data.");
1244 test_report_progress("Chunk written.");
1245 if (fseek(testfilepointer
, -chunksize
, SEEK_CUR
) != 0)
1247 test_report_failure_strerror();
1248 test_report_failure("Failed to seek backwards");
1251 test_report_progress("Seek backwards made.");
1252 erase_buffer(readdatabuffer1
, buffersize
);
1253 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
1254 if (operationsize
!= chunksize
)
1256 test_report_failure_strerror();
1257 test_report_failure("Failed to read chunk of data.");
1260 test_report_progress("Chunk read.");
1261 if (compare_buffers(readdatabuffer1
, randomwritebuffer
+ 2 * chunksize
, chunksize
) != 0)
1263 test_report_failure("Read data does not match written data");
1266 test_report_success("Read data match written data.");
1267 test_report_success("SUCCESS");
1279 test_report_description("seek to end, seek foreward, write some data , seek backward, read 2 x data, check data");
1281 // seek to end, seek foreward, write some data , seek backward, read 2 x data, check data
1282 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
1284 test_report_failure_strerror();
1285 test_report_failure("Failed to seek to end");
1288 test_report_progress("Seek to end of file made.");
1289 if (fseek(testfilepointer
, chunksize
, SEEK_CUR
) != 0)
1291 test_report_failure_strerror();
1292 test_report_failure("Failed to seek foreward");
1295 test_report_progress("Seek foreward made.");
1296 operationsize
= fwrite(randomwritebuffer
, 1, chunksize
, testfilepointer
);
1297 if (operationsize
!= chunksize
)
1299 test_report_failure_strerror();
1300 test_report_failure("Failed to write chunk of data.");
1303 test_report_progress("Chunk written.");
1304 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1306 test_report_failure_strerror();
1307 test_report_failure("Failed to seek backwards");
1310 test_report_progress("Seek backwards made.");
1311 erase_buffer(readdatabuffer1
, buffersize
);
1312 erase_buffer(zerobuffer
, buffersize
);
1313 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1314 if (operationsize
!= 2 * chunksize
)
1316 test_report_failure_strerror();
1317 test_report_failure("Failed to read chunk of data.");
1320 test_report_progress("Chunk read.");
1321 if (compare_buffers(readdatabuffer1
, zerobuffer
, chunksize
) != 0)
1323 test_report_failure("Read data does not match written data for first chunk");
1326 test_report_success("Read data match written data for first chunk.");
1327 if (compare_buffers(readdatabuffer1
+ chunksize
, randomwritebuffer
, chunksize
) != 0)
1329 test_report_failure("Read data does not match written data for second chunk");
1332 test_report_success("Read data match written data for second chunk.");
1333 test_report_success("SUCCESS");
1345 test_report_description("seek beoynd end, write some data , seek backward, read 2 x data, check data");
1347 // seek beoynd end, write some data , seek backward, read 2 x data, check data
1348 if (fseek(testfilepointer
, chunksize
, SEEK_END
) != 0)
1350 test_report_failure_strerror();
1351 test_report_failure("Failed to seek beyond end of file");
1354 test_report_progress("Seek beyond end of file made.");
1355 operationsize
= fwrite(randomwritebuffer
+ chunksize
, 1, chunksize
, testfilepointer
);
1356 if (operationsize
!= chunksize
)
1358 test_report_failure_strerror();
1359 test_report_failure("Failed to write chunk of data.");
1362 test_report_progress("Chunk written.");
1363 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1365 test_report_failure_strerror();
1366 test_report_failure("Failed to seek backwards");
1369 test_report_progress("Seek backwards made.");
1370 erase_buffer(readdatabuffer1
, buffersize
);
1371 erase_buffer(zerobuffer
, buffersize
);
1372 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1373 if (operationsize
!= 2 * chunksize
)
1375 test_report_failure_strerror();
1376 test_report_failure("Failed to read chunk of data.");
1379 test_report_progress("Chunk read.");
1380 if (compare_buffers(readdatabuffer1
, zerobuffer
, chunksize
) != 0)
1382 test_report_failure("Read data does not match written data for first chunk");
1385 test_report_success("Read data match written data for first chunk.");
1386 if (compare_buffers(readdatabuffer1
+ chunksize
, randomwritebuffer
+ chunksize
, chunksize
) != 0)
1388 test_report_failure("Read data does not match written data for second chunk");
1391 test_report_success("Read data match written data for second chunk.");
1392 test_report_success("SUCCESS");
1401 int test_fseek_fwrite_fread_wrapper()
1405 int result
= test_fseek_fwrite_fread_preparation();
1408 report_failure("Preparation failed");
1411 result
= test_fseek_fwrite_fread();
1418 int test_fseek_fwrite_fread_append_preparation()
1420 char readdatabuffer1
[1024] = {0};
1422 size_t operationsize
= 0;
1424 report_progress("Starting test_fseek_fwrite_fread_append_preparation");
1426 if (testfilepointer
!= NULL
)
1428 report_failure("Pointer for test file not closed.");
1432 // Initilize random write buffer
1434 for (i
= 0; i
< buffersize
; i
++)
1435 writedata
[i
] = (char)rand();
1437 // open file, write data
1438 testfilepointer
= fopen(testfilename
, "wb+");
1439 if (testfilepointer
== NULL
)
1441 report_failure_strerror();
1442 report_failure("Failed to open file for writting.");
1445 operationsize
= fwrite(writedata
, 1, buffersize
, testfilepointer
);
1446 if (operationsize
!= buffersize
)
1448 report_failure_strerror();
1449 report_failure("Failed to write data.");
1453 // Seek to beginning
1454 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1456 report_failure_strerror();
1457 report_failure("Failed to seek to file beginning (w+b).");
1461 //read data, compare data
1462 erase_buffer(readdatabuffer1
, buffersize
);
1463 operationsize
= fread(readdatabuffer1
, 1, buffersize
, testfilepointer
);
1464 if (operationsize
!= buffersize
)
1466 report_failure_strerror();
1467 report_failure("Failed to read data.");
1470 if (compare_buffers(readdatabuffer1
, writedata
, buffersize
) != 0)
1472 report_failure("Read data differs from written data");
1477 if (fclose(testfilepointer
) != 0)
1479 report_failure_strerror();
1480 report_failure("Failed to close the file.");
1485 testfilepointer
= fopen(testfilename
, "ab+");
1486 if (testfilepointer
== NULL
)
1488 report_failure_strerror();
1489 report_failure("Failed to open file for appending.");
1497 int test_fseek_fwrite_fread_append()
1499 /* Expects that testfilepointer is opened in correct mode */
1500 char randomwritebuffer
[1024] = {0};
1501 size_t chunksize
= 128;
1502 char readdatabuffer1
[1024] = {0};
1503 char zerobuffer
[1024] = {0};
1505 size_t operationsize
= 0;
1507 // Initilize random write buffer
1508 // Don't initialize srand or randomwritebuffer == writedata
1509 for (i
= 0; i
< buffersize
; i
++)
1510 randomwritebuffer
[i
] = (char)rand();
1516 report_progress("Start test_fseek_fwrite_fread_append");
1520 test_report_description("seek to begin, write some data , seek to begin, read 2x some data, check data");
1522 // seek to begin, write some data , seek to begin, read 2x some data, check data
1523 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1525 test_report_failure_strerror();
1526 test_report_failure("Failed to seek to beginning of file");
1529 test_report_progress("Seek to beginning of file.");
1530 operationsize
= fwrite(randomwritebuffer
, 1, chunksize
, testfilepointer
);
1531 if (operationsize
!= chunksize
)
1533 test_report_failure_strerror();
1534 test_report_failure("Failed to write chunk of data.");
1537 test_report_progress("Chunk written.");
1538 if (fseek(testfilepointer
, 0, SEEK_SET
) != 0)
1540 test_report_failure_strerror();
1541 test_report_failure("Failed to seek to beginning of file");
1544 test_report_progress("Seek to beginning of file.");
1545 erase_buffer(readdatabuffer1
, buffersize
);
1546 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1547 if (operationsize
!= 2 * chunksize
)
1549 test_report_failure_strerror();
1550 test_report_failure("Failed to read chunk of data.");
1553 test_report_progress("Chunk read.");
1554 if (compare_buffers(readdatabuffer1
, writedata
, chunksize
) != 0)
1556 test_report_failure("Read data does not match written data for first chunk");
1559 test_report_success("Read data match written data for first chunk.");
1560 if (compare_buffers(readdatabuffer1
+ chunksize
, writedata
+ chunksize
, chunksize
) != 0)
1562 test_report_failure("Read data does not match written data for second chunk");
1565 test_report_success("Read data match written data for second chunk.");
1566 test_report_success("SUCCESS");
1578 test_report_description("seek foreward, write some data , seek backward, read data, check data");
1580 // seek foreward, write some data , seek backward, read data, check data
1581 if (fseek(testfilepointer
, 3 * chunksize
, SEEK_CUR
) != 0)
1583 test_report_failure_strerror();
1584 test_report_failure("Failed to seek foreward");
1587 test_report_progress("Seek foreward made.");
1588 operationsize
= fwrite(randomwritebuffer
, 1, chunksize
, testfilepointer
);
1589 if (operationsize
!= chunksize
)
1591 test_report_failure_strerror();
1592 test_report_failure("Failed to write chunk of data.");
1595 test_report_progress("Chunk written.");
1596 if (fseek(testfilepointer
, - chunksize
, SEEK_CUR
) != 0)
1598 test_report_failure_strerror();
1599 test_report_failure("Failed to seek backwards");
1602 test_report_progress("Seek backwards made.");
1603 erase_buffer(readdatabuffer1
, buffersize
);
1604 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
1605 if (operationsize
!= chunksize
)
1607 test_report_failure_strerror();
1608 test_report_failure("Failed to read chunk of data.");
1611 test_report_progress("Chunk read.");
1612 if (compare_buffers(readdatabuffer1
, randomwritebuffer
, chunksize
) != 0)
1614 test_report_failure("Read data does not match written");
1617 test_report_success("Read data match written data.");
1618 test_report_success("SUCCESS");
1630 test_report_description("seek backward, write some data , seek backward, read 2 x data, check error");
1632 // seek backward, write some data , seek backward, read 2 x data, check error
1633 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1635 test_report_failure_strerror();
1636 test_report_failure("Failed to seek backwards");
1639 test_report_progress("Seek backwards made.");
1640 operationsize
= fwrite(randomwritebuffer
+ chunksize
, 1, chunksize
, testfilepointer
);
1641 if (operationsize
!= chunksize
)
1643 test_report_failure_strerror();
1644 test_report_failure("Failed to write chunk of data.");
1647 test_report_progress("Chunk written.");
1648 if (fseek(testfilepointer
, -chunksize
, SEEK_CUR
) != 0)
1650 test_report_failure_strerror();
1651 test_report_failure("Failed to seek backwards");
1654 test_report_progress("Seek backwards made.");
1655 erase_buffer(readdatabuffer1
, buffersize
);
1656 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1657 if (operationsize
!= 2 * chunksize
)
1659 if (errno
!= EINVAL
)
1661 test_report_failure_strerror();
1662 test_report_failure("Different than expected error reported.");
1668 test_report_failure("Expected error");
1671 test_report_success("Error reported.");
1672 test_report_success("SUCCESS");
1684 test_report_description("seek to end, write some data , seek backward, read data, check data");
1686 // seek to end, write some data , seek backward, read data, check data
1687 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
1689 test_report_failure_strerror();
1690 test_report_failure("Failed to seek to end");
1693 test_report_progress("Seek to end of file made.");
1694 operationsize
= fwrite(randomwritebuffer
+ 2 * chunksize
, 1, chunksize
, testfilepointer
);
1695 if (operationsize
!= chunksize
)
1697 test_report_failure_strerror();
1698 test_report_failure("Failed to write chunk of data.");
1701 test_report_progress("Chunk written.");
1702 if (fseek(testfilepointer
, -chunksize
, SEEK_CUR
) != 0)
1704 test_report_failure_strerror();
1705 test_report_failure("Failed to seek backwards");
1708 test_report_progress("Seek backwards made.");
1709 erase_buffer(readdatabuffer1
, buffersize
);
1710 operationsize
= fread(readdatabuffer1
, 1, chunksize
, testfilepointer
);
1711 if (operationsize
!= chunksize
)
1713 test_report_failure_strerror();
1714 test_report_failure("Failed to read chunk of data.");
1717 test_report_progress("Chunk read.");
1718 if (compare_buffers(readdatabuffer1
, randomwritebuffer
+ 2 * chunksize
, chunksize
) != 0)
1720 test_report_failure("Read data does not match written data");
1723 test_report_success("Read data match written data.");
1724 test_report_success("SUCCESS");
1736 test_report_description("seek to end, seek foreward, write some data , seek backward, read 2 x data, check data");
1738 // seek to end, seek foreward, write some data , seek backward, read 2 x data, check data
1739 if (fseek(testfilepointer
, 0, SEEK_END
) != 0)
1741 test_report_failure_strerror();
1742 test_report_failure("Failed to seek to end");
1745 test_report_progress("Seek to end of file made.");
1746 if (fseek(testfilepointer
, chunksize
, SEEK_CUR
) != 0)
1748 test_report_failure_strerror();
1749 test_report_failure("Failed to seek foreward");
1752 test_report_progress("Seek foreward made.");
1753 operationsize
= fwrite(randomwritebuffer
+ 3 * chunksize
, 1, chunksize
, testfilepointer
);
1754 if (operationsize
!= chunksize
)
1756 test_report_failure_strerror();
1757 test_report_failure("Failed to write chunk of data.");
1760 test_report_progress("Chunk written.");
1761 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1763 test_report_failure_strerror();
1764 test_report_failure("Failed to seek backwards");
1767 test_report_progress("Seek backwards made.");
1768 erase_buffer(readdatabuffer1
, buffersize
);
1769 erase_buffer(zerobuffer
, buffersize
);
1770 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1771 if (operationsize
!= 2 * chunksize
)
1773 test_report_failure_strerror();
1774 test_report_failure("Failed to read chunk of data.");
1777 test_report_progress("Chunk read.");
1778 if (compare_buffers(readdatabuffer1
, randomwritebuffer
+ 2 * chunksize
, chunksize
) != 0)
1780 test_report_failure("Read data does not match written data for first chunk");
1783 test_report_success("Read data match written data for first chunk.");
1784 if (compare_buffers(readdatabuffer1
+ chunksize
, randomwritebuffer
+ 3 * chunksize
, chunksize
) != 0)
1786 test_report_failure("Read data does not match written data for second chunk");
1789 test_report_success("Read data match written data for second chunk.");
1790 test_report_success("SUCCESS");
1802 test_report_description("seek beyond end, write some data , seek backward, read 2 x data, check data");
1804 // seek beoynd end, write some data , seek backward, read 2 x data, check data
1805 if (fseek(testfilepointer
, chunksize
, SEEK_END
) != 0)
1807 test_report_failure_strerror();
1808 test_report_failure("Failed to seek beyond end of file");
1811 test_report_progress("Seek beyond end of file made.");
1812 operationsize
= fwrite(randomwritebuffer
+ chunksize
, 1, chunksize
, testfilepointer
);
1813 if (operationsize
!= chunksize
)
1815 test_report_failure_strerror();
1816 test_report_failure("Failed to write chunk of data.");
1819 test_report_progress("Chunk written.");
1820 if (fseek(testfilepointer
, -2 * chunksize
, SEEK_CUR
) != 0)
1822 test_report_failure_strerror();
1823 test_report_failure("Failed to seek backwards");
1826 test_report_progress("Seek backwards made.");
1827 erase_buffer(readdatabuffer1
, buffersize
);
1828 erase_buffer(zerobuffer
, buffersize
);
1829 operationsize
= fread(readdatabuffer1
, 1, 2 * chunksize
, testfilepointer
);
1830 if (operationsize
!= 2 * chunksize
)
1832 test_report_failure_strerror();
1833 test_report_failure("Failed to read chunk of data.");
1836 test_report_progress("Chunk read.");
1837 if (compare_buffers(readdatabuffer1
, randomwritebuffer
+ 3 * chunksize
, chunksize
) != 0)
1839 test_report_failure("Read data does not match written data for first chunk");
1842 test_report_success("Read data match written data for first chunk.");
1843 if (compare_buffers(readdatabuffer1
+ chunksize
, randomwritebuffer
+ chunksize
, chunksize
) != 0)
1845 test_report_failure("Read data does not match written data for second chunk");
1848 test_report_success("Read data match written data for second chunk.");
1849 test_report_success("SUCCESS");
1858 int test_fseek_fwrite_fread_append_wrapper()
1862 int result
= test_fseek_fwrite_fread_append_preparation();
1865 report_failure("Preparation failed");
1868 result
= test_fseek_fwrite_fread_append();
1877 report_progress("Starting tests");
1879 reset_global_test_counter();
1882 if (test_fseek_ftell_wrapper("wb") != 0)
1883 global_failure_indicator
= 1;
1884 if (test_fseek_ftell_wrapper("w+b") != 0)
1885 global_failure_indicator
= 1;
1889 // wb - can only write
1890 // ab - can only append
1891 if (test_fseek_and_fread_wrapper("rb") != 0)
1892 global_failure_indicator
= 1;
1894 if (test_fseek_and_fread_wrapper("r+b") != 0)
1895 global_failure_indicator
= 1;
1897 if (test_fseek_and_fread_wrapper("a+b") != 0)
1898 global_failure_indicator
= 1;
1900 if (test_fseek_and_fread_wrapper("w+b") != 0)
1901 global_failure_indicator
= 1;
1904 if (test_fseek_fwrite_fread_wrapper() != 0)
1905 global_failure_indicator
= 1;
1907 // seek/write/read (append)
1908 if (test_fseek_fwrite_fread_append_wrapper() != 0)
1909 global_failure_indicator
= 1;
1911 if (global_failure_indicator
== 1)
1912 report_failure("One of the tests FAILED");
1914 report_progress("All tests SUCCEEDED");
1916 report_progress("Tests finished");