unity_port_linux.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #include <unistd.h>
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <sys/time.h>
  12. #include "unity.h"
  13. #include "sdkconfig.h"
  14. static struct timeval s_test_start, s_test_stop;
  15. void unity_putc(int c)
  16. {
  17. putc(c, stdout);
  18. }
  19. void unity_flush(void)
  20. {
  21. fflush(stdout);
  22. fsync(fileno(stdout));
  23. }
  24. static void esp_unity_readline(char* dst, size_t len)
  25. {
  26. /* Read line from console with support for echoing and backspaces */
  27. size_t write_index = 0;
  28. for (;;) {
  29. char c = 0;
  30. int result = fgetc(stdin);
  31. if (result == EOF) {
  32. continue;
  33. }
  34. c = (char) result;
  35. if (c == '\r' || c == '\n') {
  36. /* Add null terminator and return on newline */
  37. unity_putc('\n');
  38. dst[write_index] = '\0';
  39. return;
  40. } else if (c == '\b') {
  41. if (write_index > 0) {
  42. /* Delete previously entered character */
  43. write_index--;
  44. unity_putc('\b');
  45. unity_putc(' ');
  46. unity_putc('\b');
  47. }
  48. } else if (len > 0 && write_index < len - 1 && !iscntrl(c)) {
  49. /* Write a max of len - 1 characters to allow for null terminator */
  50. unity_putc(c);
  51. dst[write_index++] = c;
  52. }
  53. }
  54. }
  55. void unity_gets(char *dst, size_t len)
  56. {
  57. esp_unity_readline(dst, len);
  58. }
  59. void unity_exec_time_start(void)
  60. {
  61. gettimeofday(&s_test_start, NULL);
  62. }
  63. void unity_exec_time_stop(void)
  64. {
  65. gettimeofday(&s_test_stop, NULL);
  66. }
  67. uint32_t unity_exec_time_get_ms(void)
  68. {
  69. return (uint32_t) (((s_test_stop.tv_sec * 1000000ULL + s_test_stop.tv_usec) -
  70. (s_test_start.tv_sec * 1000000ULL + s_test_start.tv_usec)) / 1000);
  71. }