forked from harryausten/ydotool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
148 lines (126 loc) · 4.91 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/// @copyright
/// This file is part of ydotool.
/// Copyright (C) 2019 Harry Austen
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the MIT License.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
/// @file test.c
/// @author Harry Austen
/// @brief Program for testing the ydotool code
// System includes
#include <string.h>
#include <stdio.h>
// Local includes
#include "uinput.h"
/// Check that the char/string to keycode mapping arrays are in chronological order
/// The strings/characters are compared when using the binary search algorithm and
/// are assumed to be in order
/// @return 0 on success, >0 if errors
int uinput_test_array_order() {
int ret = 0;
for (size_t i = 1; i != NUM_NORMAL_KEYS; ++i) {
if (NORMAL_KEYS[i].character < NORMAL_KEYS[i-1].character) {
printf("%c < %c\n", NORMAL_KEYS[i].character, NORMAL_KEYS[i-1].character);
ret++;
}
}
for (size_t i = 1; i != NUM_SHIFTED_KEYS; ++i) {
if (SHIFTED_KEYS[i].character < SHIFTED_KEYS[i-1].character) {
printf("%c < %c\n", SHIFTED_KEYS[i].character, SHIFTED_KEYS[i-1].character);
ret++;
}
}
for (size_t i = 1; i != NUM_MODIFIER_KEYS; ++i) {
if (strcmp(MODIFIER_KEYS[i].string, MODIFIER_KEYS[i-1].string) < 0) {
printf("%s < %s\n", MODIFIER_KEYS[i].string, MODIFIER_KEYS[i-1].string);
ret++;
}
}
for (size_t i = 1; i != NUM_FUNCTION_KEYS; ++i) {
if (strcmp(FUNCTION_KEYS[i].string, FUNCTION_KEYS[i-1].string) < 0) {
printf("%s < %s\n", FUNCTION_KEYS[i].string, FUNCTION_KEYS[i-1].string);
ret++;
}
}
return ret;
}
/// Check that the string to code function returns the correct values
/// @return 0 on success, >0 if errors
int uinput_test_keystring_to_keycode() {
int ret = 0;
uint16_t code = 0;
uint8_t shifted = 0;
for (size_t i = 0; i != NUM_NORMAL_KEYS; ++i) {
if (uinput_keystring_to_keycode(&NORMAL_KEYS[i].character, &code, &shifted)) {
printf("'%c' NOT FOUND!\n", NORMAL_KEYS[i].character);
} else if (code != NORMAL_KEYS[i].code) {
printf("Code does not match for '%c'. Got %d, expected %d\n", NORMAL_KEYS[i].character, code, NORMAL_KEYS[i].character);
} else if (shifted != 0) {
printf("Returned shifted character, but expected normal character '%c'\n", NORMAL_KEYS[i].character);
} else {
continue;
}
ret++;
}
for (size_t i = 0; i != NUM_SHIFTED_KEYS; ++i) {
if (uinput_keystring_to_keycode(&SHIFTED_KEYS[i].character, &code, &shifted)) {
printf("'%c' NOT FOUND!\n", SHIFTED_KEYS[i].character);
} else if (code != SHIFTED_KEYS[i].code) {
printf("Code does not match for '%c'. Got %d, expected %d\n", SHIFTED_KEYS[i].character, code, SHIFTED_KEYS[i].character);
} else if (shifted != 1) {
printf("Returned non-shifted character, but expected shifted character '%c'\n", SHIFTED_KEYS[i].character);
} else {
continue;
}
ret++;
}
for (size_t i = 0; i != NUM_MODIFIER_KEYS; ++i) {
if (uinput_keystring_to_keycode(MODIFIER_KEYS[i].string, &code, &shifted)) {
printf("%s NOT FOUND!\n", MODIFIER_KEYS[i].string);
} else if ( code != MODIFIER_KEYS[i].code ) {
printf("Code does not match for %s. Got %d, expected %d\n", MODIFIER_KEYS[i].string, code, MODIFIER_KEYS[i].code);
} else if (shifted != 0) {
printf("Returned shifted key, but expected normal key '%s'\n", MODIFIER_KEYS[i].string);
} else {
continue;
}
ret++;
}
for (size_t i = 0; i != NUM_FUNCTION_KEYS; ++i) {
if (uinput_keystring_to_keycode(FUNCTION_KEYS[i].string, &code, &shifted)) {
printf("%s NOT FOUND!\n", FUNCTION_KEYS[i].string);
} else if ( code != FUNCTION_KEYS[i].code ) {
printf("Code does not match for %s. Got %d, expected %d\n", FUNCTION_KEYS[i].string, code, FUNCTION_KEYS[i].code);
} else if (shifted != 0) {
printf("Returned shifted key, but expected normal key '%s'\n", FUNCTION_KEYS[i].string);
} else {
continue;
}
ret++;
}
return ret;
}
/// Tests for the uinput.c/h functions
/// @return 0 on success, >0 if errors
int uinput_test() {
int ret = 0;
ret += uinput_test_array_order();
ret += uinput_test_keystring_to_keycode();
return ret;
}
/// Main entrypoint for the test executable
/// @return 0 on success, >0 if errors
int main() {
int ret = 0;
ret += uinput_test();
if (ret) {
printf("FAILED %d tests\n", ret);
} else {
printf("PASSED!\n");
}
return ret;
}