test_index.c 3.1 KB

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