-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
125 lines (100 loc) · 2.15 KB
/
test.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <stdarg.h>
#include <uk/plat/time.h>
#include "test.h"
/* circular log buffer */
static char log_buf[0x10000];
static char *head = log_buf, *tail = log_buf;
static inline void advance(char **ptr)
{
if ((unsigned int)(*ptr - log_buf) >= sizeof(log_buf))
*ptr = log_buf;
else
*ptr = *ptr + 1;
}
static void log_char(char c)
{
*tail = c;
advance(&tail);
if (tail == head)
advance(&head);
}
static void print_log(void)
{
char last;
printf(" log: |\n");
last = '\n';
while (head != tail) {
if (last == '\n')
printf(" ");
last = *head;
putchar(last);
advance(&head);
}
if (last != '\n')
putchar('\n');
}
int lkl_test_run(const struct lkl_test *tests, int nr, const char *fmt, ...)
{
int i, ret, status = TEST_SUCCESS;
__nsec start, stop;
char name[1024];
va_list args;
va_start(args, fmt);
vsnprintf(name, sizeof(name), fmt, args);
va_end(args);
printf("1..%d # %s\n", nr, name);
for (i = 1; i <= nr; i++) {
const struct lkl_test *t = &tests[i-1];
unsigned long delta_us;
printf("* %d %s\n", i, t->name);
fflush(stdout);
start = ukplat_monotonic_clock();
ret = t->fn(t->arg1, t->arg2, t->arg3);
stop = ukplat_monotonic_clock();
switch (ret) {
case TEST_SUCCESS:
printf("ok %d %s\n", i, t->name);
break;
case TEST_SKIP:
printf("ok %d %s # SKIP\n", i, t->name);
break;
case TEST_BAILOUT:
status = TEST_BAILOUT;
/* fall through */
case TEST_FAILURE:
default:
if (status != TEST_BAILOUT)
status = TEST_FAILURE;
printf("not ok %d %s\n", i, t->name);
}
printf(" ---\n");
delta_us = (stop - start) / 1000;
printf(" time_us: %ld\n", delta_us);
print_log();
printf(" ...\n");
if (status == TEST_BAILOUT) {
printf("Bail out!\n");
return TEST_FAILURE;
}
fflush(stdout);
}
return status;
}
void lkl_test_log(const char *str, int len)
{
while (len--)
log_char(*(str++));
}
int lkl_test_logf(const char *fmt, ...)
{
char tmp[1024], *c;
va_list args;
unsigned int n;
va_start(args, fmt);
n = vsnprintf(tmp, sizeof(tmp), fmt, args);
va_end(args);
for (c = tmp; *c != 0; c++)
log_char(*c);
return n > sizeof(tmp) ? sizeof(tmp) : n;
}