test_index.c 4.1 KB

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