main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 1 * 1024 * 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: allocate more space to the file (posix_fallocate)
  89. printf("Allocate more space to the file (posix_fallocate)..\n");
  90. posix_fallocate(fileno(file), ftell(file), ADDITIONAL_SPACE);
  91. printf("File current offset: %ld\n", ftell(file));
  92. printf("Moving to the end..\n");
  93. fseek(file, 0, SEEK_END);
  94. printf("File current offset: %ld\n", ftell(file));
  95. assert(ftell(file) == strlen(text) + ADDITIONAL_SPACE);
  96. printf("[Test] Allocation or more space passed.\n");
  97. // Test: allocate more space to the file (ftruncate)
  98. printf("Allocate more space to the file (ftruncate)..\n");
  99. ftruncate(fileno(file), ftell(file) + ADDITIONAL_SPACE);
  100. assert(ftell(file) == strlen(text) + ADDITIONAL_SPACE);
  101. printf("File current offset: %ld\n", ftell(file));
  102. printf("Moving to the end..\n");
  103. fseek(file, 0, SEEK_END);
  104. printf("File current offset: %ld\n", ftell(file));
  105. assert(ftell(file) == strlen(text) + 2 * ADDITIONAL_SPACE);
  106. printf("[Test] Extension of the file size passed.\n");
  107. // Test: allocate more space to the file (fseek)
  108. printf("Allocate more space to the file (fseek) from the start..\n");
  109. printf("File current offset: %ld\n", ftell(file));
  110. fseek(file, 3 * ADDITIONAL_SPACE, SEEK_SET);
  111. printf("File current offset: %ld\n", ftell(file));
  112. assert(ftell(file) == 3 * ADDITIONAL_SPACE);
  113. printf("[Test] Extension of the file size passed.\n");
  114. // Test: allocate more space to the file (fseek)
  115. printf("Allocate more space to the file (fseek) from the end..\n");
  116. printf("File current offset: %ld\n", ftell(file));
  117. fseek(file, ADDITIONAL_SPACE, SEEK_END);
  118. printf("File current offset: %ld\n", ftell(file));
  119. assert(ftell(file) == 4 * ADDITIONAL_SPACE);
  120. printf("[Test] Extension of the file size passed.\n");
  121. // Test: allocate more space to the file (fseek)
  122. printf("Allocate more space to the file (fseek) from the middle..\n");
  123. fseek(file, 3 * ADDITIONAL_SPACE, SEEK_SET);
  124. printf("File current offset: %ld\n", ftell(file));
  125. fseek(file, 2 * ADDITIONAL_SPACE, SEEK_CUR);
  126. printf("File current offset: %ld\n", ftell(file));
  127. assert(ftell(file) == 5 * ADDITIONAL_SPACE);
  128. printf("[Test] Extension of the file size passed.\n");
  129. // Display some debug information
  130. printf("Getting the size of the file on disk..\n");
  131. struct stat st;
  132. stat(PATH_TEST_FILE, &st);
  133. stat_size = st.st_size;
  134. assert(stat_size != 0);
  135. // Compare with the size from fstat
  136. fstat(fileno(file), &st);
  137. printf("The file size is: %lld (stat), %lld (fstat).\n", stat_size,
  138. st.st_size);
  139. assert(stat_size != 0);
  140. assert(stat_size == st.st_size);
  141. // Test: closing the file (fclose)
  142. printf("Closing from the file..\n");
  143. ret = fclose(file);
  144. assert(ret == 0);
  145. printf("[Test] Closing file passed.\n");
  146. printf("All the tests passed!\n");
  147. return 0;
  148. }