test_index.c 3.2 KB

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