main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #define PATH_TEST_FOLDER "./test"
  14. #define PATH_TEST_FILE (PATH_TEST_FOLDER "/test.txt")
  15. #define FILE_TEXT "Hello, world!"
  16. #define WORLD_OFFSET 7
  17. #define NAME_REPLACMENT "James"
  18. #define NAME_REPLACMENT_LEN (sizeof(NAME_REPLACMENT) - 1)
  19. #define ADDITIONAL_SPACE 16 * 1024
  20. int
  21. main(int argc, char **argv)
  22. {
  23. FILE *file;
  24. const char *text = FILE_TEXT;
  25. char buffer[1000];
  26. int ret;
  27. long long stat_size;
  28. // Test: Create a folder to store the file, if it does not exist yet
  29. ret = mkdir(PATH_TEST_FOLDER, 777);
  30. assert(ret == 0 || (ret == -1 && errno == EEXIST));
  31. // Test: File opening (fopen)
  32. printf("Opening a file..\n");
  33. file = fopen(PATH_TEST_FILE, "w+");
  34. if (file == NULL) {
  35. printf("Error! errno: %d\n", errno);
  36. }
  37. assert(file != NULL);
  38. printf("[Test] File opening passed.\n");
  39. // Test: Writing to a file (fprintf)
  40. printf("Writing to the file..\n");
  41. ret = fprintf(file, "%s", text);
  42. assert(ret == strlen(text));
  43. printf("[Test] File writing passed.\n");
  44. // Test: Reading from a file (fseek)
  45. printf("Moving the cursor to the start of the file..\n");
  46. ret = fseek(file, 0, SEEK_SET);
  47. assert(ret == 0);
  48. printf("Reading from the file, up to 1000 characters..\n");
  49. fread(buffer, 1, sizeof(buffer), file);
  50. printf("Text read: %s\n", buffer);
  51. assert(strncmp(text, buffer, strlen(text)) == 0);
  52. printf("[Test] File reading passed.\n");
  53. // Test: end of file detection (feof)
  54. printf("Determine whether we reach the end of the file..\n");
  55. int is_end_of_file = feof(file);
  56. printf("Is the end of file? %d\n", is_end_of_file);
  57. assert(is_end_of_file == 1);
  58. printf("[Test] End of file detection passed.\n");
  59. // Test: retrieving file offset (ftell)
  60. printf("Getting the plaintext size..\n");
  61. long plaintext_size = ftell(file);
  62. printf("The plaintext size is %ld.\n", plaintext_size);
  63. assert(plaintext_size == 13);
  64. printf("[Test] Retrieving file offset passed.\n");
  65. // Test: persist changes on disk (fflush)
  66. printf("Force actual write of all the cached data to the disk..\n");
  67. ret = fflush(file);
  68. assert(ret == 0);
  69. printf("[Test] Retrieving file offset passed.\n");
  70. // Test: writing at specified offset (pwrite)
  71. printf("Writing 5 characters at offset %d..\n", WORLD_OFFSET);
  72. ret = pwrite(fileno(file), NAME_REPLACMENT, NAME_REPLACMENT_LEN,
  73. WORLD_OFFSET);
  74. printf("File current offset: %ld\n", ftell(file));
  75. assert(ret == NAME_REPLACMENT_LEN);
  76. assert(ftell(file) == strlen(FILE_TEXT));
  77. printf("[Test] Writing at specified offset passed.\n");
  78. // Test: reading at specified offset (pread)
  79. printf("Reading %ld characters at offset %d..\n", NAME_REPLACMENT_LEN,
  80. WORLD_OFFSET);
  81. buffer[NAME_REPLACMENT_LEN] = '\0';
  82. pread(fileno(file), buffer, NAME_REPLACMENT_LEN, WORLD_OFFSET);
  83. printf("Text read: %s\n", buffer);
  84. printf("File current offset: %ld\n", ftell(file));
  85. assert(strcmp(NAME_REPLACMENT, buffer) == 0);
  86. assert(ftell(file) == strlen(FILE_TEXT));
  87. printf("[Test] Reading at specified offset passed.\n");
  88. // Test: moving at the start of the file (fseek)
  89. printf("Move at the start of the file (fseek)..\n");
  90. printf("File current offset: %ld\n", ftell(file));
  91. fseek(file, 0, SEEK_SET);
  92. printf("File current offset: %ld\n", ftell(file));
  93. assert(ftell(file) == 0);
  94. // Test: moving at the end of the file (fseek)
  95. printf("Move at the end of the file (fseek)..\n");
  96. printf("File current offset: %ld\n", ftell(file));
  97. fseek(file, 0, SEEK_END);
  98. printf("File current offset: %ld\n", ftell(file));
  99. assert(ftell(file) == strlen(FILE_TEXT));
  100. int end_position = ftell(file) / 2;
  101. // Test: moving at the middle of the file (fseek)
  102. printf("Move at the middle of the file (fseek)..\n");
  103. printf("File current offset: %ld\n", ftell(file));
  104. fseek(file, 0, SEEK_SET);
  105. fseek(file, end_position, SEEK_CUR);
  106. printf("File current offset: %ld\n", ftell(file));
  107. assert(ftell(file) == end_position);
  108. // Test: allocate more space to the file (posix_fallocate)
  109. printf("Allocate more space to the file (posix_fallocate)..\n");
  110. fseek(file, 0, SEEK_END);
  111. posix_fallocate(fileno(file), ftell(file), ADDITIONAL_SPACE);
  112. printf("File current offset: %ld\n", ftell(file));
  113. printf("Moving to the end..\n");
  114. fseek(file, 0, SEEK_END);
  115. printf("File current offset: %ld\n", ftell(file));
  116. assert(ftell(file) == strlen(text) + ADDITIONAL_SPACE);
  117. printf("[Test] Allocation or more space passed.\n");
  118. // Test: allocate more space to the file (ftruncate)
  119. printf("Allocate more space to the file (ftruncate)..\n");
  120. ftruncate(fileno(file), ftell(file) + ADDITIONAL_SPACE);
  121. assert(ftell(file) == strlen(text) + ADDITIONAL_SPACE);
  122. printf("File current offset: %ld\n", ftell(file));
  123. printf("Moving to the end..\n");
  124. fseek(file, 0, SEEK_END);
  125. printf("File current offset: %ld\n", ftell(file));
  126. assert(ftell(file) == strlen(text) + 2 * ADDITIONAL_SPACE);
  127. printf("[Test] Extension of the file size passed.\n");
  128. // Test: allocate more space to the file (fseek, from the start)
  129. printf("Allocate more space to the file (fseek) from the start..\n");
  130. printf("File current offset: %ld\n", ftell(file));
  131. fseek(file, 3 * ADDITIONAL_SPACE, SEEK_SET);
  132. printf("File current offset: %ld\n", ftell(file));
  133. assert(ftell(file) == 3 * ADDITIONAL_SPACE);
  134. printf("[Test] Extension of the file size passed.\n");
  135. // Test: allocate more space to the file (fseek, from the middle)
  136. printf("Allocate more space to the file (fseek) from the middle..\n");
  137. fseek(file, 3 * ADDITIONAL_SPACE, SEEK_SET);
  138. printf("File current offset: %ld\n", ftell(file));
  139. fseek(file, 2 * ADDITIONAL_SPACE, SEEK_CUR);
  140. printf("File current offset: %ld\n", ftell(file));
  141. assert(ftell(file) == 5 * ADDITIONAL_SPACE);
  142. printf("[Test] Extension of the file size passed.\n");
  143. // Display some debug information
  144. printf("Getting the size of the file on disk..\n");
  145. struct stat st;
  146. stat(PATH_TEST_FILE, &st);
  147. stat_size = st.st_size;
  148. assert(stat_size != 0);
  149. // Compare with the size from fstat
  150. fstat(fileno(file), &st);
  151. printf("The file size is: %lld (stat), %lld (fstat).\n", stat_size,
  152. st.st_size);
  153. assert(stat_size != 0);
  154. assert(stat_size == st.st_size);
  155. // Test: closing the file (fclose)
  156. printf("Closing from the file..\n");
  157. ret = fclose(file);
  158. assert(ret == 0);
  159. printf("[Test] Closing file passed.\n");
  160. printf("All the tests passed!\n");
  161. return 0;
  162. }