File: test-io.c

package info (click to toggle)
keyd 2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,524 kB
  • sloc: ansic: 5,206; python: 1,121; makefile: 114; javascript: 105; perl: 95; sh: 80
file content (272 lines) | stat: -rw-r--r-- 4,983 bytes parent folder | download | duplicates (2)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "../src/keyd.h"

#define MAX_EVENTS 1024

struct key_event output[MAX_EVENTS];
size_t noutput = 0;

static uint64_t get_time_ns()
{
	struct timespec ts;
	clock_gettime(CLOCK_MONOTONIC, &ts);

	return (uint64_t)(ts.tv_sec*1E9)+(uint64_t)ts.tv_nsec;
}

static uint8_t lookup_code(const char *name)
{
	size_t i;

	if (!strcmp(name, "control"))
		return KEYD_LEFTCTRL;
	if (!strcmp(name, "shift"))
		return KEYD_LEFTSHIFT;
	if (!strcmp(name, "meta"))
		return KEYD_LEFTMETA;
	if (!strcmp(name, "alt"))
		return KEYD_LEFTALT;

	for (i = 0; i < ARRAY_SIZE(keycode_table); i++)
		if (keycode_table[i].name && !strcmp(keycode_table[i].name, name))
			return i;
	return 0;
}

static void send_key(uint8_t code, uint8_t pressed)
{
	output[noutput].code = code;
	output[noutput].pressed = pressed;
	noutput++;
}

static char *read_file(const char *path)
{
	int fd = open(path, O_RDONLY);
	static char buf[4096];
	size_t sz = 0;
	ssize_t n;

	if (fd < 0) {
		perror("open");
		exit(-1);
	}

	while ((n = read(fd, buf, sizeof(buf) - sz)) > 0) {
		sz += n;
		assert(sz < sizeof buf);
	}

	buf[sz] = 0;
	return buf;
}

static int cmp_events(struct key_event *input, size_t nin,
		      struct key_event *output, size_t nout)
{
	size_t i;

	if (nin != nout)
		return -1;

	for (i = 0; i < nin; i++) {
		if (input[i].code != output[i].code
		    || input[i].pressed != output[i].pressed)
			return -1;
	}

	return 0;
}

static int print_diff(struct key_event *expected, size_t nexp,
		      struct key_event *output, size_t nout)
{
	int i;
	int n = nout > nexp ? nout : nexp;
	int ret = 0;

	printf("\n%-30s%s\n\n", "Expected", "Output");

	for (i = 0; i < n; i++) {
		int np = 0;
		int match = i < nexp &&
		    i < nout &&
		    output[i].code == expected[i].code &&
		    output[i].pressed == expected[i].pressed;

		if (!match)
			ret = -1;

		if (!match)
			printf("\033[32;1m");

		np = 0;
		if (i < nexp)
			np = printf("%s %s",
				    keycode_table[expected[i].code].name,
				    expected[i].pressed ? "down" : "up");

		while (np++ < 30)
			printf(" ");

		if (!match)
			printf("\033[0m\033[31;1m");

		if (i < nout)
			printf("%s %s",
			       keycode_table[output[i].code].name,
			       output[i].pressed ? "down" : "up");

		if (!match)
			printf("\033[0m");

		printf("\n");
	}

	return ret;
}

static int parse_events(char *s, struct key_event in[MAX_EVENTS], size_t *nin,
			struct key_event out[MAX_EVENTS], size_t *nout)
{
	int ret;
	int time = 0;
	int ln = 0;
	int n = 0;
	struct key_event *events = in;

	char *line = s;
	*nin = 0;
	*nout = 0;

	while (1) {
		int len;
		char *end = strchr(line, '\n');

		if (!end)
			break;
		*end = 0;

		len = strlen(line);

		ln++;

		if (line[0] == '#')
			goto next;

		while (line[0] == ' ')
			line++;

		if (!line[0]) {
			*nin = n;
			events = out;
			n = 0;

			goto next;
		}

		if (len >= 2 && line[len - 1] == 's' && line[len - 2] == 'm') {
			time += atoi(line);
		} else {
			uint8_t code;
			char *k = strtok(line, " ");
			char *v = strtok(NULL, " \n");

			if (!v || (strcmp(v, "up") && strcmp(v, "down"))) {
				printf("%d: Invalid line\n", ln);
				goto next;
			}

			if (!(code = lookup_code(k))) {
				printf("%d: %s is not a valid key\n", ln,
				       k);
				goto next;
			}

			assert(n < MAX_EVENTS);
			events[n].code = code;
			events[n].pressed = !strcmp(v, "down");
			events[n].timestamp = time;
			n++;
		}

	      next:
		line = end + 1;
	}

	*nout = n;
	return 0;
}

uint64_t run_test(struct keyboard *kbd, const char *path)
{
	uint64_t time;
	char *data = read_file(path);

	struct key_event input[MAX_EVENTS];
	size_t ninput;

	struct key_event expected[MAX_EVENTS];
	size_t nexpected;

	if (parse_events(data, input, &ninput, expected, &nexpected) < 0) {
		fprintf(stderr, "Failed to parse input\n");
		exit(-1);
	}

	noutput = 0;

	time = get_time_ns();
	kbd_process_events(kbd, input, ninput);
	time = get_time_ns()-time;

	if (cmp_events(output, noutput, expected, nexpected)) {
		printf("%s \033[31;1mFAILED\033[0m\n", path);
		print_diff(expected, nexpected, output, noutput);
		exit(-1);
	} else {
		printf("%s \033[32;1mPASSED\033[0m (%zu us)\n", path, time/1000);
	}

	return time;
}

static void on_layer_change(const struct keyboard *kbd, const struct layer *layer, uint8_t active)
{
}

int main(int argc, char *argv[])
{
	size_t i;
	struct config config;
	uint64_t total_time;

	struct keyboard *kbd;

	struct output output = {
		.send_key = send_key,
		.on_layer_change = on_layer_change,
	};

	if (argc < 2) {
		printf
		    ("usage: %s <test config> <test file> [<test file>...]\n",
		     argv[0]);
		return -1;
	}

	if (config_parse(&config, argv[1])) {
		printf("Failed to parse config %s\n", argv[1]);
		return -1;
	}

	kbd = new_keyboard(&config, &output);

	for (i = 2; i < argc; i++)
		total_time += run_test(kbd, argv[i]);

	printf("\nTotal time spent in the main loop: %zu us\n", total_time/1000);
	return 0;
}