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