test_index.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include "minunit_vars.h"
  5. #include <notification/notification_messages.h>
  6. #include <cli/cli.h>
  7. #include <loader/loader.h>
  8. #define TAG "UnitTests"
  9. int run_minunit_test_furi();
  10. int run_minunit_test_furi_hal();
  11. int run_minunit_test_furi_string();
  12. int run_minunit_test_infrared();
  13. int run_minunit_test_rpc();
  14. int run_minunit_test_flipper_format();
  15. int run_minunit_test_flipper_format_string();
  16. int run_minunit_test_stream();
  17. int run_minunit_test_storage();
  18. int run_minunit_test_subghz();
  19. int run_minunit_test_dirwalk();
  20. int run_minunit_test_power();
  21. int run_minunit_test_protocol_dict();
  22. int run_minunit_test_lfrfid_protocols();
  23. int run_minunit_test_nfc();
  24. int run_minunit_test_bit_lib();
  25. int run_minunit_test_float_tools();
  26. int run_minunit_test_bt();
  27. typedef int (*UnitTestEntry)();
  28. typedef struct {
  29. const char* name;
  30. const UnitTestEntry entry;
  31. } UnitTest;
  32. const UnitTest unit_tests[] = {
  33. {.name = "furi", .entry = run_minunit_test_furi},
  34. {.name = "furi_hal", .entry = run_minunit_test_furi_hal},
  35. {.name = "furi_string", .entry = run_minunit_test_furi_string},
  36. {.name = "storage", .entry = run_minunit_test_storage},
  37. {.name = "stream", .entry = run_minunit_test_stream},
  38. {.name = "dirwalk", .entry = run_minunit_test_dirwalk},
  39. {.name = "flipper_format", .entry = run_minunit_test_flipper_format},
  40. {.name = "flipper_format_string", .entry = run_minunit_test_flipper_format_string},
  41. {.name = "rpc", .entry = run_minunit_test_rpc},
  42. {.name = "subghz", .entry = run_minunit_test_subghz},
  43. {.name = "infrared", .entry = run_minunit_test_infrared},
  44. {.name = "nfc", .entry = run_minunit_test_nfc},
  45. {.name = "power", .entry = run_minunit_test_power},
  46. {.name = "protocol_dict", .entry = run_minunit_test_protocol_dict},
  47. {.name = "lfrfid", .entry = run_minunit_test_lfrfid_protocols},
  48. {.name = "bit_lib", .entry = run_minunit_test_bit_lib},
  49. {.name = "float_tools", .entry = run_minunit_test_float_tools},
  50. {.name = "bt", .entry = run_minunit_test_bt},
  51. };
  52. void minunit_print_progress() {
  53. static const char progress[] = {'\\', '|', '/', '-'};
  54. static uint8_t progress_counter = 0;
  55. static TickType_t last_tick = 0;
  56. TickType_t current_tick = xTaskGetTickCount();
  57. if(current_tick - last_tick > 20) {
  58. last_tick = current_tick;
  59. printf("[%c]\033[3D", progress[++progress_counter % COUNT_OF(progress)]);
  60. fflush(stdout);
  61. }
  62. }
  63. void minunit_print_fail(const char* str) {
  64. printf(FURI_LOG_CLR_E "%s\r\n" FURI_LOG_CLR_RESET, str);
  65. }
  66. void unit_tests_cli(Cli* cli, FuriString* args, void* context) {
  67. UNUSED(cli);
  68. UNUSED(args);
  69. UNUSED(context);
  70. minunit_run = 0;
  71. minunit_assert = 0;
  72. minunit_fail = 0;
  73. minunit_status = 0;
  74. Loader* loader = furi_record_open(RECORD_LOADER);
  75. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  76. // TODO: lock device while test running
  77. if(loader_is_locked(loader)) {
  78. printf("RPC: stop all applications to run tests\r\n");
  79. notification_message(notification, &sequence_blink_magenta_100);
  80. } else {
  81. notification_message_block(notification, &sequence_set_only_blue_255);
  82. uint32_t heap_before = memmgr_get_free_heap();
  83. uint32_t cycle_counter = furi_get_tick();
  84. for(size_t i = 0; i < COUNT_OF(unit_tests); i++) {
  85. if(cli_cmd_interrupt_received(cli)) {
  86. break;
  87. }
  88. if(furi_string_size(args)) {
  89. if(furi_string_cmp_str(args, unit_tests[i].name) == 0) {
  90. unit_tests[i].entry();
  91. } else {
  92. printf("Skipping %s\r\n", unit_tests[i].name);
  93. }
  94. } else {
  95. unit_tests[i].entry();
  96. }
  97. }
  98. if(minunit_run != 0) {
  99. printf("\r\nFailed tests: %u\r\n", minunit_fail);
  100. // Time report
  101. cycle_counter = (furi_get_tick() - cycle_counter);
  102. printf("Consumed: %lu ms\r\n", cycle_counter);
  103. // Wait for tested services and apps to deallocate memory
  104. furi_delay_ms(200);
  105. uint32_t heap_after = memmgr_get_free_heap();
  106. printf("Leaked: %ld\r\n", heap_before - heap_after);
  107. // Final Report
  108. if(minunit_fail == 0) {
  109. notification_message(notification, &sequence_success);
  110. printf("Status: PASSED\r\n");
  111. } else {
  112. notification_message(notification, &sequence_error);
  113. printf("Status: FAILED\r\n");
  114. }
  115. }
  116. }
  117. furi_record_close(RECORD_NOTIFICATION);
  118. furi_record_close(RECORD_LOADER);
  119. }
  120. void unit_tests_on_system_start() {
  121. #ifdef SRV_CLI
  122. Cli* cli = furi_record_open(RECORD_CLI);
  123. // We need to launch apps from tests, so we cannot lock loader
  124. cli_add_command(cli, "unit_tests", CliCommandFlagParallelSafe, unit_tests_cli, NULL);
  125. furi_record_close(RECORD_CLI);
  126. #endif
  127. }