Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include "lfs.h"
  2. #include "emubd/lfs_emubd.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7.  
  8. // test stuff
  9. static void test_log(const char *s, uintmax_t v) {
  10. printf("%s: %jd\n", s, v);
  11. }
  12.  
  13. static void test_assert(const char *file, unsigned line,
  14. const char *s, uintmax_t v, uintmax_t e) {
  15. test_log(s, v);
  16.  
  17. if (v != e) {
  18. fprintf(stderr, "\033[31m%s:%u: assert %s failed with %jd, "
  19. "expected %jd\033[0m\n", file, line, s, v, e);
  20. exit(-2);
  21. }
  22. }
  23.  
  24. #define test_assert(s, v, e) test_assert(__FILE__, __LINE__, s, v, e)
  25.  
  26.  
  27. // lfs declarations
  28. lfs_t lfs;
  29. lfs_emubd_t bd;
  30.  
  31. uintmax_t test;
  32.  
  33. #ifndef LFS_READ_SIZE
  34. #define LFS_READ_SIZE 1
  35. #endif
  36.  
  37. #ifndef LFS_PROG_SIZE
  38. #define LFS_PROG_SIZE LFS_READ_SIZE
  39. #endif
  40.  
  41. #ifndef LFS_BLOCK_SIZE
  42. #define LFS_BLOCK_SIZE 4096
  43. #endif
  44.  
  45. #ifndef LFS_BLOCK_COUNT
  46. #define LFS_BLOCK_COUNT 128
  47. #endif
  48.  
  49. #ifndef LFS_BLOCK_CYCLES
  50. #define LFS_BLOCK_CYCLES 1024
  51. #endif
  52.  
  53.  
  54. #ifndef LFS_CACHE_SIZE
  55. #define LFS_CACHE_SIZE 64
  56. #endif
  57.  
  58. #ifndef LFS_LOOKAHEAD_SIZE
  59. #define LFS_LOOKAHEAD_SIZE 16
  60. #endif
  61.  
  62. const struct lfs_config cfg = {
  63. .context = &bd,
  64. .read = &lfs_emubd_read,
  65. .prog = &lfs_emubd_prog,
  66. .erase = &lfs_emubd_erase,
  67. .sync = &lfs_emubd_sync,
  68.  
  69. .read_size = LFS_READ_SIZE,
  70. .prog_size = LFS_PROG_SIZE,
  71. .block_size = LFS_BLOCK_SIZE,
  72. .block_count = LFS_BLOCK_COUNT,
  73. .block_cycles = LFS_BLOCK_CYCLES,
  74. .cache_size = LFS_CACHE_SIZE,
  75. .lookahead_size = LFS_LOOKAHEAD_SIZE,
  76. };
  77.  
  78.  
  79. // Entry point
  80. int main(void) {
  81. lfs_emubd_create(&cfg, "blocks");
  82.  
  83. test = lfs_format(&lfs, &cfg);
  84. test_assert("lfs_format", test, 0);
  85. test = lfs_mount(&lfs, &cfg);
  86. test_assert("lfs_mount", test, 0);
  87.  
  88. test = lfs_mkdir(&lfs, "/dir");
  89. test_assert("lfs_mkdir", test, 0);
  90.  
  91. // Test creating an attribute
  92. uint32_t timestamp1 = 12345678;
  93. lfs_file_t file;
  94. struct lfs_file_config config;
  95. struct lfs_attr sAttr;
  96.  
  97. memset(&config, 0, sizeof(config));
  98. memset(&file, 0, sizeof(file));
  99.  
  100. sAttr.type = 1;
  101. sAttr.buffer = &timestamp1;
  102. sAttr.size = sizeof(timestamp1);
  103. config.attr_count = 1;
  104. config.attrs = &sAttr;
  105.  
  106. test = lfs_file_opencfg(&lfs, &file, "/dir/f1",
  107. LFS_O_RDWR | LFS_O_CREAT | LFS_O_EXCL, &config);
  108. test_assert("lfs_file_opencfg", test, 0);
  109.  
  110. uint32_t result;
  111. test = lfs_getattr(&lfs, "/dir/f1", 1, &result, sizeof(result));
  112. test_assert("lfs_getattr", test, LFS_ERR_NOATTR);
  113.  
  114. test = lfs_file_close(&lfs, &file);
  115. test_assert("lfs_file_close", test, 0);
  116.  
  117. test = lfs_getattr(&lfs, "/dir/f1", 1, &result, sizeof(result));
  118. test_assert("lfs_getattr", test, sizeof(result));
  119. test_assert("result", result, timestamp1);
  120.  
  121. // Test creating an unrelated file with a different attribute
  122. uint32_t timestamp2 = 87654321;
  123.  
  124. memset(&config, 0, sizeof(config));
  125. memset(&file, 0, sizeof(file));
  126.  
  127. sAttr.type = 1;
  128. sAttr.buffer = &timestamp2;
  129. sAttr.size = sizeof(timestamp2);
  130. config.attr_count = 1;
  131. config.attrs = &sAttr;
  132.  
  133. test = lfs_file_opencfg(&lfs, &file, "/f1",
  134. LFS_O_RDWR | LFS_O_CREAT | LFS_O_EXCL, &config);
  135. test_assert("lfs_file_opencfg", test, 0);
  136.  
  137. test = lfs_getattr(&lfs, "/f1", 1, &result, sizeof(result));
  138. test_assert("lfs_getattr", test, LFS_ERR_NOATTR);
  139.  
  140. test = lfs_file_close(&lfs, &file);
  141. test_assert("lfs_file_close", test, 0);
  142.  
  143. test = lfs_getattr(&lfs, "/f1", 1, &result, sizeof(result));
  144. test_assert("lfs_getattr", test, sizeof(result));
  145. test_assert("result", result, timestamp2);
  146.  
  147. // And finally make sure original attribute was unmodified
  148. test = lfs_getattr(&lfs, "/dir/f1", 1, &result, sizeof(result));
  149. test_assert("lfs_getattr", test, sizeof(result));
  150. test_assert("result", result, timestamp1);
  151.  
  152. test = lfs_unmount(&lfs);
  153. test_assert("lfs_unmount", test, 0);
  154.  
  155. lfs_emubd_destroy(&cfg);
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement