test_index.c 3.3 KB

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