Skip to content

Commit 6e570c5

Browse files
committed
improve test command
1 parent c890ca5 commit 6e570c5

1 file changed

Lines changed: 162 additions & 44 deletions

File tree

user/test/main.c

Lines changed: 162 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,179 @@
1-
// #include <stdio.h>
2-
// #include <buildin/time.h>
3-
// #include <sys/spawn.h>
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
44

5-
// void test_thread() {
6-
// FILE* file = fopen("README.md", "r");
7-
// FILE* file2 = fopen("README.md", "r");
8-
// fsize(file, test);
5+
#include <non-standard/sys/env.h>
6+
#include <non-standard/sys/spawn.h>
7+
#include <non-standard/buildin/path.h>
98

10-
// fclose(file2);
9+
typedef struct {
10+
char** argv;
11+
bool (*test)(pipe_t* stdout_pipe, int exit_code);
12+
} test_t;
1113

12-
// while (true) {
13-
// printf("Hello thread %d\n", test);
14-
// sleep_s(1);
15-
// }
16-
// }
14+
#define PIPE_BUFFER_SIZE 65536
15+
pipe_t global_stdout_pipe;
1716

18-
// int main(int argc, char* argv[], char* envp[]) {
19-
// int child = thread(test_thread);
17+
void set_wait_and_yield_term() {
18+
set_env(SYS_ENV_TASK_SET_WAIT_TIME, (void*)100);
19+
yield();
20+
}
2021

21-
// sleep_ms(500);
22+
int spawn_and_capture(char** argv, pipe_t* stdout_pipe) {
23+
char* executable = search_executable(argv[0]);
24+
if (!executable) {
25+
return -1;
26+
}
2227

23-
// for (int i = 0; i < 60; i++) {
24-
// printf("Hello %d\n", i);
25-
// sleep_s(1);
28+
stdout_pipe->pos = 0;
29+
memset(stdout_pipe->buffer, 0, PIPE_BUFFER_SIZE);
2630

27-
// if (i == 30) {
28-
// kill(child);
29-
// }
30-
// }
31+
const char** envp = (const char**)env(SYS_GET_ENVP_ID);
3132

32-
// return 0;
33-
// }
33+
set_env(SYS_ENV_PIN, (void*)1);
34+
int pid = spawn(executable, (const char**)argv, envp);
3435

35-
// #include <buildin/graphics.h>
36-
// #include <buildin/mouse.h>
37-
// #include <sys/getc.h>
36+
if (pid == -1) {
37+
set_env(SYS_ENV_PIN, (void*)0);
38+
free(executable);
39+
return -1;
40+
}
41+
42+
set_pipe(pid, stdout_pipe, PIPE_STDOUT);
43+
set_env(SYS_ENV_PIN, (void*)0);
44+
45+
while (get_proc_info(pid)) {
46+
set_wait_and_yield_term();
47+
}
48+
49+
free(executable);
50+
return get_exit_code(pid);
51+
}
3852

39-
// int main(int argc, char* argv[], char* envp[]) {
40-
// while (async_getc() != 27) {
41-
// start_frame();
42-
// draw_string(0, 0, "Hello world", 1);
43-
// update_mouse();
44-
// end_frame();
45-
// }
46-
// return 0;
47-
// }
4853

4954

50-
#include <stdlib.h>
51-
#include <stdio.h>
52-
#include <non-standard/sys/getc.h>
55+
56+
bool test_exit_code(pipe_t* stdout_pipe, int exit_code) {
57+
return exit_code == 0;
58+
}
59+
60+
bool test_calc(pipe_t* stdout_pipe, int exit_code) {
61+
if (exit_code != 0) {
62+
return false;
63+
}
64+
return strcmp(stdout_pipe->buffer, "7\n") == 0;
65+
}
66+
67+
bool test_hello(pipe_t* stdout_pipe, int exit_code) {
68+
if (exit_code != 0) {
69+
return false;
70+
}
71+
return strcmp(stdout_pipe->buffer, "Hello world!\n") == 0;
72+
}
73+
74+
bool test_hello_libc(pipe_t* stdout_pipe, int exit_code) {
75+
if (exit_code != 0) {
76+
return false;
77+
}
78+
return strcmp(stdout_pipe->buffer, "Hello, world 69!\n") == 0;
79+
}
80+
81+
bool test_wc(pipe_t* stdout_pipe, int exit_code) {
82+
if (exit_code != 0) {
83+
return false;
84+
}
85+
return strcmp(stdout_pipe->buffer, "34 76 420 initrd:/examples/hello.asm\n") == 0;
86+
}
87+
88+
89+
test_t tests[] = {
90+
{
91+
.argv = (char*[]){ "calc", "1", "+", "2", "*", "3", NULL },
92+
.test = test_calc
93+
},
94+
{
95+
.argv = (char*[]){ "fasm", "initrd:/examples/hello.asm", "tmp:/hello.elf", NULL },
96+
.test = test_exit_code
97+
},
98+
{
99+
.argv = (char*[]){ "tmp:/hello.elf", NULL },
100+
.test = test_hello
101+
},
102+
{
103+
.argv = (char*[]){ "fasm", "initrd:/examples/hello-libc.asm", "tmp:/hello-libc.o", NULL },
104+
.test = test_exit_code
105+
},
106+
{
107+
.argv = (char*[]){ "load", "tmp:/hello-libc.o", NULL },
108+
.test = test_hello_libc
109+
},
110+
{
111+
.argv = (char*[]){ "fasm", "initrd:/examples/wc.asm", "tmp:/wc.o", NULL },
112+
.test = test_exit_code
113+
},
114+
{
115+
.argv = (char*[]){ "load", "tmp:/wc.o", "initrd:/examples/hello.asm", NULL },
116+
.test = test_wc
117+
},
118+
{
119+
.argv = (char*[]){ "wc", "initrd:/examples/hello.asm", NULL },
120+
.test = test_wc
121+
},
122+
{
123+
.argv = (char*[]){ "mcc", "initrd:/examples/hello.c", "-o", "tmp:/hello-c.elf", NULL },
124+
.test = test_exit_code
125+
},
126+
{
127+
.argv = (char*[]){ "tmp:/hello-c.elf", NULL },
128+
.test = test_hello
129+
},
130+
{
131+
.argv = (char*[]){ "mcc", "initrd:/examples/hello-libc.c", "-r", "-o", "tmp:/hello-libc-c.o", NULL },
132+
.test = test_exit_code
133+
},
134+
{
135+
.argv = (char*[]){ "load", "tmp:/hello-libc-c.o", NULL },
136+
.test = test_hello
137+
},
138+
};
53139

54140
int main(int argc, char* argv[], char* envp[]) {
55-
char c;
56-
while ((c = async_getc()) != EOF_CHAR) {
57-
void* ptr = malloc(100);
58-
printf("malloced 100 bytes at 0x%x\n", ptr);
141+
global_stdout_pipe.buffer = malloc(PIPE_BUFFER_SIZE);
142+
global_stdout_pipe.size = PIPE_BUFFER_SIZE;
143+
global_stdout_pipe.pos = 0;
144+
memset(global_stdout_pipe.buffer, 0, PIPE_BUFFER_SIZE);
145+
146+
int success = 0;
147+
int failed = 0;
148+
149+
for (int i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
150+
char** argv = tests[i].argv;
151+
printf("test[%d]: ", i);
152+
for (int j = 0; argv[j]; j++) {
153+
printf("%s ", argv[j]);
154+
}
155+
printf("\n");
156+
157+
int exit_code = spawn_and_capture(argv, &global_stdout_pipe);
158+
159+
if (tests[i].test(&global_stdout_pipe, exit_code)) {
160+
success++;
161+
} else {
162+
failed++;
163+
printf("test[%d]: Failed %s\n", i, argv[0]);
164+
165+
printf("test[%d]: ", i);
166+
for (int j = 0; j < global_stdout_pipe.size; j++) {
167+
if (global_stdout_pipe.buffer[j] == '\n') {
168+
printf("\ntest[%d]: ", i);
169+
} else {
170+
putchar(global_stdout_pipe.buffer[j]);
171+
}
172+
}
173+
}
59174
}
175+
176+
printf("test: success: %d, failed: %d\n", success, failed);
177+
60178
return 0;
61179
}

0 commit comments

Comments
 (0)