test_index.c 3.0 KB

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