forked from OpenKore/openkore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsoleui-test.cpp
More file actions
114 lines (98 loc) · 1.98 KB
/
consoleui-test.cpp
File metadata and controls
114 lines (98 loc) · 1.98 KB
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
#include "consoleui.h"
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
static ConsoleUI *ui = NULL;
static void
usage() {
fprintf(stderr, "Usage: consoleui-test <-c|-p>\n"
" -c Test for correctness.\n"
" -p Test for performance.\n");
exit(1);
}
static void
show(const char *format, ...) {
va_list ap;
char buf[1024 * 4];
va_start(ap, format);
vsnprintf(buf, sizeof(buf) - 1, format, ap);
va_end(ap);
ui->print(buf);
}
/*
static void
doNothing(unsigned int maxIterations) {
FILE *f = fopen("/dev/urandom", "r");
if (f == NULL) {
show("Cannot open /dev/urandom for reading.\n");
exit(1);
}
unsigned int i = 0;
while (i < maxIterations && !feof(f)) {
fgetc(f);
i++;
}
fclose(f);
}
*/
static int
testCorrectness() {
ui = ConsoleUI::getInstance();
ui->start();
srand(time(NULL));
for (int i = 1; i <= 5; i++) {
show("Loading %d...\n", i);
usleep(rand() % 200000);
}
show("Checking for new portals...");
sleep(1);
show(" none found.\n");
show("\e[1;31mThis is a test error.");
sleep(2);
show("\e[1;31m This is another test error.\n");
sleep(2);
show("\e[1;32mThis is a green message.\n");
show("\e[0mThis is a message with normal color.\n");
show("\e[1;32mThis is a green message.\n");
bool quit = false;
while (!quit) {
char *input = ui->getInput();
if (input != NULL) {
if (strcmp(input, "quit") == 0) {
quit = true;
} else if (strlen(input) > 0) {
show("Unknown command '%s'\n", input);
}
free(input);
} else {
usleep(50000);
}
}
return 0;
}
static int
testPerformance() {
ui = ConsoleUI::getInstance();
ui->start();
for (int i = 1; i <= 5000000; i++) {
show("Loading %d...\n", i);
}
return 0;
}
int
main(int argc, char *argv[]) {
if (argc != 2) {
usage();
}
if (strcmp(argv[1], "-c") == 0) {
return testCorrectness();
} else if (strcmp(argv[1], "-p") == 0) {
return testPerformance();
} else {
usage();
return 1; // Never reached.
}
}