test_index.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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();
  11. int run_minunit_test_infrared_decoder_encoder();
  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. void minunit_print_progress(void) {
  21. static char progress[] = {'\\', '|', '/', '-'};
  22. static uint8_t progress_counter = 0;
  23. static TickType_t last_tick = 0;
  24. TickType_t current_tick = xTaskGetTickCount();
  25. if(current_tick - last_tick > 20) {
  26. last_tick = current_tick;
  27. printf("[%c]\033[3D", progress[++progress_counter % COUNT_OF(progress)]);
  28. }
  29. }
  30. void minunit_print_fail(const char* str) {
  31. printf(FURI_LOG_CLR_E "%s\r\n" FURI_LOG_CLR_RESET, str);
  32. }
  33. void unit_tests_cli(Cli* cli, string_t args, void* context) {
  34. UNUSED(cli);
  35. UNUSED(args);
  36. UNUSED(context);
  37. uint32_t test_result = 0;
  38. minunit_run = 0;
  39. minunit_assert = 0;
  40. minunit_fail = 0;
  41. minunit_status = 0;
  42. Loader* loader = furi_record_open("loader");
  43. NotificationApp* notification = furi_record_open("notification");
  44. // TODO: lock device while test running
  45. if(loader_is_locked(loader)) {
  46. FURI_LOG_E(TAG, "RPC: stop all applications to run tests");
  47. notification_message(notification, &sequence_blink_magenta_100);
  48. } else {
  49. notification_message_block(notification, &sequence_set_only_blue_255);
  50. uint32_t heap_before = memmgr_get_free_heap();
  51. uint32_t cycle_counter = furi_hal_get_tick();
  52. test_result |= run_minunit();
  53. test_result |= run_minunit_test_storage();
  54. test_result |= run_minunit_test_stream();
  55. test_result |= run_minunit_test_dirwalk();
  56. test_result |= run_minunit_test_flipper_format();
  57. test_result |= run_minunit_test_flipper_format_string();
  58. test_result |= run_minunit_test_infrared_decoder_encoder();
  59. test_result |= run_minunit_test_rpc();
  60. test_result |= run_minunit_test_subghz();
  61. test_result |= run_minunit_test_nfc();
  62. cycle_counter = (furi_hal_get_tick() - cycle_counter);
  63. FURI_LOG_I(TAG, "Consumed: %u us", cycle_counter);
  64. if(test_result == 0) {
  65. furi_hal_delay_ms(200); /* wait for tested services and apps to deallocate */
  66. uint32_t heap_after = memmgr_get_free_heap();
  67. notification_message(notification, &sequence_success);
  68. if(heap_after != heap_before) {
  69. FURI_LOG_E(TAG, "Leaked: %d", heap_before - heap_after);
  70. } else {
  71. FURI_LOG_I(TAG, "No leaks");
  72. }
  73. FURI_LOG_I(TAG, "PASSED");
  74. } else {
  75. notification_message(notification, &sequence_error);
  76. FURI_LOG_E(TAG, "FAILED");
  77. }
  78. }
  79. furi_record_close("notification");
  80. furi_record_close("loader");
  81. }
  82. void unit_tests_on_system_start() {
  83. #ifdef SRV_CLI
  84. Cli* cli = furi_record_open("cli");
  85. // We need to launch apps from tests, so we cannot lock loader
  86. cli_add_command(cli, "unit_tests", CliCommandFlagParallelSafe, unit_tests_cli, NULL);
  87. furi_record_close("cli");
  88. #endif
  89. }