test_runner.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "runners/test_runner.h"
  2. #include <getopt.h>
  3. // disk geometries
  4. struct test_geometry {
  5. const char *name;
  6. lfs_size_t read_size;
  7. lfs_size_t prog_size;
  8. lfs_size_t erase_size;
  9. lfs_size_t erase_count;
  10. };
  11. const struct test_geometry test_geometries[] = {
  12. // Made up geometries that works well for testing
  13. {"small", 16, 16, 512, (1024*1024)/512},
  14. {"medium", 16, 16, 4096, (1024*1024)/4096},
  15. {"big", 16, 16, 32*1024, (1024*1024)/(32*1024)},
  16. // EEPROM/NVRAM
  17. {"eeprom", 1, 1, 512, (1024*1024)/512},
  18. // SD/eMMC
  19. {"emmc", 512, 512, 512, (1024*1024)/512},
  20. // NOR flash
  21. {"nor", 1, 1, 4096, (1024*1024)/4096},
  22. // NAND flash
  23. {"nand", 4096, 4096, 32*1024, (1024*1024)/(32*1024)},
  24. };
  25. const size_t test_geometry_count = (
  26. sizeof(test_geometries) / sizeof(test_geometries[0]));
  27. // option handling
  28. enum opt_flags {
  29. OPT_HELP = 'h',
  30. OPT_LIST = 'l',
  31. OPT_LIST_PATHS = 1,
  32. OPT_LIST_DEFINES = 2,
  33. OPT_LIST_GEOMETRIES = 3,
  34. };
  35. const struct option long_opts[] = {
  36. {"help", no_argument, NULL, OPT_HELP},
  37. {"list", no_argument, NULL, OPT_LIST},
  38. {"list-paths", no_argument, NULL, OPT_LIST_PATHS},
  39. {"list-defines", no_argument, NULL, OPT_LIST_DEFINES},
  40. {"list-geometries", no_argument, NULL, OPT_LIST_GEOMETRIES},
  41. {NULL, 0, NULL, 0},
  42. };
  43. const char *const help_text[] = {
  44. "Show this help message.",
  45. "List test cases.",
  46. "List the path for each test case.",
  47. "List the defines for each test permutation.",
  48. "List the disk geometries used for testing.",
  49. };
  50. int main(int argc, char **argv) {
  51. bool list = false;
  52. bool list_paths = false;
  53. bool list_defines = false;
  54. bool list_geometries = false;
  55. // parse options
  56. while (true) {
  57. int index = 0;
  58. int c = getopt_long(argc, argv, "hl", long_opts, &index);
  59. switch (c) {
  60. // generate help message
  61. case OPT_HELP: {
  62. printf("usage: %s [options] [test_case]\n", argv[0]);
  63. printf("\n");
  64. printf("options:\n");
  65. size_t i = 0;
  66. while (long_opts[i].name) {
  67. size_t indent;
  68. if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
  69. printf(" -%c, --%-16s",
  70. long_opts[i].val,
  71. long_opts[i].name);
  72. indent = 8+strlen(long_opts[i].name);
  73. } else {
  74. printf(" --%-20s", long_opts[i].name);
  75. indent = 4+strlen(long_opts[i].name);
  76. }
  77. // a quick, hacky, byte-level method for text wrapping
  78. size_t len = strlen(help_text[i]);
  79. size_t j = 0;
  80. if (indent < 24) {
  81. printf("%.80s\n", &help_text[i][j]);
  82. j += 80;
  83. }
  84. while (j < len) {
  85. printf("%24s%.80s\n", "", &help_text[i][j]);
  86. j += 80;
  87. }
  88. i += 1;
  89. }
  90. printf("\n");
  91. exit(0);
  92. }
  93. // list flags
  94. case OPT_LIST:
  95. list = true;
  96. break;
  97. case OPT_LIST_PATHS:
  98. list_paths = true;
  99. break;
  100. case OPT_LIST_DEFINES:
  101. list_defines = true;
  102. break;
  103. case OPT_LIST_GEOMETRIES:
  104. list_geometries = true;
  105. break;
  106. // done parsing
  107. case -1:
  108. goto getopt_done;
  109. // unknown arg, getopt prints a message for us
  110. default:
  111. exit(-1);
  112. }
  113. }
  114. getopt_done:
  115. // what do we need to do?
  116. if (list) {
  117. printf("%-36s %-12s %-12s %7s %7s\n",
  118. "id", "suite", "case", "type", "perms");
  119. for (size_t i = 0; i < test_suite_count; i++) {
  120. for (size_t j = 0; j < test_suites[i]->case_count; j++) {
  121. printf("%-36s %-12s %-12s %7s %7d\n",
  122. test_suites[i]->cases[j]->id,
  123. test_suites[i]->name,
  124. test_suites[i]->cases[j]->name,
  125. "n", // TODO
  126. test_suites[i]->cases[j]->permutations);
  127. }
  128. }
  129. } else if (list_paths) {
  130. printf("%-36s %-36s\n", "id", "path");
  131. for (size_t i = 0; i < test_suite_count; i++) {
  132. for (size_t j = 0; j < test_suites[i]->case_count; j++) {
  133. printf("%-36s %-36s\n",
  134. test_suites[i]->cases[j]->id,
  135. test_suites[i]->cases[j]->path);
  136. }
  137. }
  138. } else if (list_defines) {
  139. // TODO
  140. } else if (list_geometries) {
  141. printf("%-12s %7s %7s %7s %7s %7s\n",
  142. "name", "read", "prog", "erase", "count", "size");
  143. for (size_t i = 0; i < test_geometry_count; i++) {
  144. printf("%-12s %7d %7d %7d %7d %7d\n",
  145. test_geometries[i].name,
  146. test_geometries[i].read_size,
  147. test_geometries[i].prog_size,
  148. test_geometries[i].erase_size,
  149. test_geometries[i].erase_count,
  150. test_geometries[i].erase_size
  151. * test_geometries[i].erase_count);
  152. }
  153. } else {
  154. printf("remaining: ");
  155. for (int i = optind; i < argc; i++) {
  156. printf("%s ", argv[i]);
  157. }
  158. printf("\n");
  159. }
  160. }