test_index.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. uint32_t test_result = 0;
  33. minunit_run = 0;
  34. minunit_assert = 0;
  35. minunit_fail = 0;
  36. minunit_status = 0;
  37. Loader* loader = furi_record_open("loader");
  38. NotificationApp* notification = furi_record_open("notification");
  39. // TODO: lock device while test running
  40. if(loader_is_locked(loader)) {
  41. FURI_LOG_E(TAG, "RPC: stop all applications to run tests");
  42. notification_message(notification, &sequence_blink_magenta_100);
  43. } else {
  44. notification_message_block(notification, &sequence_set_only_blue_255);
  45. uint32_t heap_before = memmgr_get_free_heap();
  46. uint32_t cycle_counter = furi_hal_get_tick();
  47. test_result |= run_minunit();
  48. test_result |= run_minunit_test_storage();
  49. test_result |= run_minunit_test_stream();
  50. test_result |= run_minunit_test_flipper_format();
  51. test_result |= run_minunit_test_flipper_format_string();
  52. test_result |= run_minunit_test_infrared_decoder_encoder();
  53. test_result |= run_minunit_test_rpc();
  54. test_result |= run_minunit_test_subghz();
  55. cycle_counter = (furi_hal_get_tick() - cycle_counter);
  56. FURI_LOG_I(TAG, "Consumed: %0.2fs", (float)cycle_counter / 1000);
  57. if(test_result == 0) {
  58. furi_hal_delay_ms(200); /* wait for tested services and apps to deallocate */
  59. uint32_t heap_after = memmgr_get_free_heap();
  60. notification_message(notification, &sequence_success);
  61. if(heap_after != heap_before) {
  62. FURI_LOG_E(TAG, "Leaked: %d", heap_before - heap_after);
  63. } else {
  64. FURI_LOG_I(TAG, "No leaks");
  65. }
  66. FURI_LOG_I(TAG, "PASSED");
  67. } else {
  68. notification_message(notification, &sequence_error);
  69. FURI_LOG_E(TAG, "FAILED");
  70. }
  71. }
  72. furi_record_close("notification");
  73. furi_record_close("loader");
  74. }
  75. void unit_tests_on_system_start() {
  76. #ifdef SRV_CLI
  77. Cli* cli = furi_record_open("cli");
  78. // We need to launch apps from tests, so we cannot lock loader
  79. cli_add_command(cli, "unit_tests", CliCommandFlagParallelSafe, unit_tests_cli, NULL);
  80. furi_record_close("cli");
  81. #endif
  82. }