filex_fault_tolerant_loop_write_data_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /* This FileX test concentrates on the Fault-Tolerant data loop write operation. */
  2. /*
  3. For FAT 12, 16, 32, one cluster size is 1024 bytes;
  4. 1024 1024 1024 1024 1024 1024
  5. |------------|------------|-----------|------------|------------|------------|
  6. | | TEST.TXT | TEST.TXT | | | |
  7. |------------|------------|-----------|------------|------------|------------|
  8. Check loop write data operation:
  9. Step1: Format and open the media;
  10. Step2: Enable fault tolerant feature;
  11. Step3: Create new file called "TEST.TXT";
  12. Step4: Check the media available bytes;
  13. Step5: Write 1500 bytes to "TEST.TXT"(two clusters);
  14. Step6: Check the media available bytes;
  15. Step7: Loop to write random size bytes to "TEST.TXT"(less than two clusters);
  16. Step8: Check the media available bytes every write operation.
  17. */
  18. #ifndef FX_STANDALONE_ENABLE
  19. #include "tx_api.h"
  20. #endif
  21. #include "fx_api.h"
  22. #include "fx_system.h"
  23. #include "fx_fault_tolerant.h"
  24. #include <stdio.h>
  25. #include "fx_ram_driver_test.h"
  26. extern void test_control_return(UINT status);
  27. void filex_fault_tolerant_loop_write_data_test_application_define(void *first_unused_memory);
  28. #if defined (FX_ENABLE_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT_DATA)
  29. #define DEMO_STACK_SIZE 4096
  30. #define CACHE_SIZE 2048
  31. #define FAULT_TOLERANT_SIZE FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE
  32. /* Define the ThreadX and FileX object control blocks... */
  33. #ifndef FX_STANDALONE_ENABLE
  34. static TX_THREAD ftest_0;
  35. #endif
  36. static FX_MEDIA ram_disk;
  37. static FX_FILE my_file;
  38. static UCHAR *pointer;
  39. /* Define the counters used in the test application... */
  40. #ifndef FX_STANDALONE_ENABLE
  41. static UCHAR *cache_buffer;
  42. static UCHAR *fault_tolerant_buffer;
  43. #else
  44. static UCHAR cache_buffer[CACHE_SIZE];
  45. static UCHAR fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
  46. #endif
  47. static CHAR write_buffer[2048];
  48. static UINT write_buffer_size = 2048;
  49. static UINT write_index;
  50. static CHAR read_buffer[4096];
  51. static UINT read_buffer_size = 4096;
  52. static UINT i;
  53. #define TEST_COUNT 3 /* FAT12, 16, 32. */
  54. /* Define thread prototypes. */
  55. static void ftest_0_entry(ULONG thread_input);
  56. extern void _fx_ram_driver(FX_MEDIA *media_ptr);
  57. extern void test_control_return(UINT status);
  58. extern UINT (*driver_write_callback)(FX_MEDIA *media_ptr, UINT sector_type, UCHAR *block_ptr, UINT *operation_ptr);
  59. /* Define what the initial system looks like. */
  60. #ifdef CTEST
  61. void test_application_define(void *first_unused_memory)
  62. #else
  63. void filex_fault_tolerant_loop_write_data_test_application_define(void *first_unused_memory)
  64. #endif
  65. {
  66. #ifndef FX_STANDALONE_ENABLE
  67. /* Setup the working pointer. */
  68. pointer = (UCHAR *) first_unused_memory;
  69. /* Create the main thread. */
  70. tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
  71. pointer, DEMO_STACK_SIZE,
  72. 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
  73. pointer = pointer + DEMO_STACK_SIZE;
  74. /* Setup memory for the RAM disk and the sector cache. */
  75. cache_buffer = pointer;
  76. pointer += CACHE_SIZE;
  77. fault_tolerant_buffer = pointer;
  78. pointer += FAULT_TOLERANT_SIZE;
  79. #endif
  80. /* Initialize the FileX system. */
  81. fx_system_initialize();
  82. #ifdef FX_STANDALONE_ENABLE
  83. ftest_0_entry(0);
  84. #endif
  85. }
  86. /* Define the test threads. */
  87. static void ftest_0_entry(ULONG thread_input)
  88. {
  89. UINT status;
  90. ULONG actual;
  91. UINT loop_count;
  92. UINT seek_index;
  93. UINT write_size;
  94. UINT two_cluster_size;
  95. ULONG file_size;
  96. ULONG current_available_bytes;
  97. ULONG available_bytes;
  98. FX_PARAMETER_NOT_USED(thread_input);
  99. /* Print out some test information banners. */
  100. printf("FileX Test: Fault Tolerant Loop Write DATA Test....................");
  101. /* Random genearte the write data. */
  102. for (write_index = 0; write_index < write_buffer_size; write_index ++)
  103. write_buffer[write_index] = (CHAR)rand();
  104. /* Loop to test FAT 12, 16, 32. */
  105. for (i = 0; i < TEST_COUNT; i ++)
  106. {
  107. if (i == 0)
  108. {
  109. /* Format the media with FAT12. This needs to be done before opening it! */
  110. status = fx_media_format(&ram_disk,
  111. _fx_ram_driver, // Driver entry
  112. ram_disk_memory_large, // RAM disk memory pointer
  113. cache_buffer, // Media buffer pointer
  114. CACHE_SIZE, // Media buffer size
  115. "MY_RAM_DISK", // Volume Name
  116. 1, // Number of FATs
  117. 32, // Directory Entries
  118. 0, // Hidden sectors
  119. 256, // Total sectors
  120. 256, // Sector size
  121. 8, // Sectors per cluster
  122. 1, // Heads
  123. 1); // Sectors per track
  124. }
  125. else if (i == 1)
  126. {
  127. /* Format the media with FAT16. This needs to be done before opening it! */
  128. status = fx_media_format(&ram_disk,
  129. _fx_ram_driver, // Driver entry
  130. ram_disk_memory_large, // RAM disk memory pointer
  131. cache_buffer, // Media buffer pointer
  132. CACHE_SIZE, // Media buffer size
  133. "MY_RAM_DISK", // Volume Name
  134. 1, // Number of FATs
  135. 32, // Directory Entries
  136. 0, // Hidden sectors
  137. 4200 * 8, // Total sectors
  138. 256, // Sector size
  139. 8, // Sectors per cluster
  140. 1, // Heads
  141. 1); // Sectors per track
  142. }
  143. else if (i == 2)
  144. {
  145. /* Format the media with FAT32. This needs to be done before opening it! */
  146. status = fx_media_format(&ram_disk,
  147. _fx_ram_driver, // Driver entry
  148. ram_disk_memory_large, // RAM disk memory pointer
  149. cache_buffer, // Media buffer pointer
  150. CACHE_SIZE, // Media buffer size
  151. "MY_RAM_DISK", // Volume Name
  152. 1, // Number of FATs
  153. 32, // Directory Entries
  154. 0, // Hidden sectors
  155. 70000 * 8, // Total sectors
  156. 256, // Sector size
  157. 8, // Sectors per cluster
  158. 1, // Heads
  159. 1); // Sectors per track
  160. }
  161. /* Determine if the format had an error. */
  162. if (status)
  163. {
  164. printf("ERROR!\n");
  165. test_control_return(1);
  166. }
  167. /* Initialize the parmeters. */
  168. file_size = 1500;
  169. two_cluster_size = 2048;
  170. /* Open the ram_disk. */
  171. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
  172. /* Check the status. */
  173. if (status != FX_SUCCESS)
  174. {
  175. /* Error, return error code. */
  176. printf("ERROR!\n");
  177. test_control_return(2);
  178. }
  179. /* Enable the Fault-tolerant feature. */
  180. status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
  181. /* Check status. */
  182. if (status)
  183. {
  184. printf("ERROR!\n");
  185. test_control_return(3);
  186. }
  187. /* Pickup the available bytes in the media. */
  188. status = fx_media_space_available(&ram_disk, &available_bytes);
  189. /* Check for available bytes error. */
  190. if (status != FX_SUCCESS)
  191. {
  192. printf("ERROR!\n");
  193. test_control_return(4);
  194. }
  195. /* Create a file called TEST.TXT in the root directory. */
  196. status = fx_file_create(&ram_disk, "TEST.TXT");
  197. /* Check the create status. */
  198. if (status != FX_SUCCESS)
  199. {
  200. printf("ERROR!\n");
  201. test_control_return(5);
  202. }
  203. /* Open the test file. */
  204. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  205. /* Check the file open status. */
  206. if (status != FX_SUCCESS)
  207. {
  208. printf("ERROR!\n");
  209. test_control_return(6);
  210. }
  211. /* Write a string to the test file, (two cluster). */
  212. status = fx_file_write(&my_file, (void *) write_buffer, file_size);
  213. /* Check the file write status. */
  214. if (status != FX_SUCCESS)
  215. {
  216. printf("ERROR!\n");
  217. test_control_return(7);
  218. }
  219. /* Pickup the available bytes in the media. */
  220. status = fx_media_space_available(&ram_disk, &current_available_bytes);
  221. /* Check for available bytes error. */
  222. if ((status != FX_SUCCESS) && (available_bytes != (current_available_bytes + two_cluster_size * 2)))
  223. {
  224. printf("ERROR!\n");
  225. test_control_return(8);
  226. }
  227. /* Update the avalibale_bytes. */
  228. available_bytes = current_available_bytes;
  229. /* Loop to write the data to test file. */
  230. for (loop_count = 0; loop_count < 1000; loop_count ++)
  231. {
  232. /* Random generate the seek index (0~file_size). */
  233. seek_index = (ULONG)rand() % (file_size + 1);
  234. /* Seek to the beginning of the test file. */
  235. status = fx_file_seek(&my_file, seek_index);
  236. /* Check the file seek status. */
  237. if (status != FX_SUCCESS)
  238. {
  239. printf("ERROR!\n");
  240. test_control_return(9);
  241. }
  242. /* Random generate the write size, (0 ~ two cluster size - seek_index); */
  243. write_size = (UINT)rand() % (two_cluster_size - seek_index + 1);
  244. /* Write a string to the test file. */
  245. status = fx_file_write(&my_file, (void *) write_buffer, write_size);
  246. /* Check the file write status. */
  247. if (status != FX_SUCCESS)
  248. {
  249. printf("ERROR!\n");
  250. test_control_return(10);
  251. }
  252. /* Update the file_size. */
  253. if (seek_index + write_size > file_size)
  254. file_size = seek_index + write_size;
  255. /* Seek to the beginning of the test file. */
  256. status = fx_file_seek(&my_file, 0);
  257. /* Check the file seek status. */
  258. if (status != FX_SUCCESS)
  259. {
  260. printf("ERROR!\n");
  261. test_control_return(11);
  262. }
  263. /* Read the bytes of the test file. */
  264. status = fx_file_read(&my_file, read_buffer, read_buffer_size, &actual);
  265. /* Check the file read status. */
  266. if ((status != FX_SUCCESS) || (actual != file_size))
  267. {
  268. printf("ERROR!\n");
  269. test_control_return(12);
  270. }
  271. /* Pickup the available bytes in the media. */
  272. status = fx_media_space_available(&ram_disk, &available_bytes);
  273. /* Check for available bytes error. */
  274. if ((status != FX_SUCCESS) || (available_bytes != current_available_bytes))
  275. {
  276. printf("ERROR!\n");
  277. test_control_return(13);
  278. }
  279. }
  280. /* Close the test file. */
  281. status = fx_file_close(&my_file);
  282. /* Check the file close status. */
  283. if (status != FX_SUCCESS)
  284. {
  285. printf("ERROR!\n");
  286. test_control_return(14);
  287. }
  288. /* Close the media. */
  289. status = fx_media_close(&ram_disk);
  290. /* Determine if the test was successful. */
  291. if (status != FX_SUCCESS)
  292. {
  293. printf("ERROR!\n");
  294. test_control_return(15);
  295. }
  296. }
  297. /* Output successful. */
  298. printf("SUCCESS!\n");
  299. test_control_return(0);
  300. }
  301. #else
  302. #ifdef CTEST
  303. void test_application_define(void *first_unused_memory)
  304. #else
  305. void filex_fault_tolerant_loop_write_data_test_application_define(void *first_unused_memory)
  306. #endif
  307. {
  308. FX_PARAMETER_NOT_USED(first_unused_memory);
  309. /* Print out some test information banners. */
  310. printf("FileX Test: Fault Tolerant Loop Write DATA Test....................N/A\n");
  311. test_control_return(255);
  312. }
  313. #endif