welcome to memory model land.
[newos.git] / apps / testapp / main.cpp
blobbbca0bbe8caaeeeee56325f9aed58798581f3897
1 /*
2 ** Copyright 2003-2004, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <string.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <ctype.h>
9 #include <sys/syscalls.h>
10 #include <sys/types.h>
11 #include <sys/resource.h>
12 #include <newos/errors.h>
13 #include <newos/drivers.h>
15 #include "tests.h"
17 namespace {
18 struct testcdtors {
19 testcdtors() {
20 printf("(testing constructor)\n");
22 ~testcdtors() {
23 printf("(testing destructor)\n");
26 testcdtors gtest;
29 struct test_option {
30 const char *command;
31 const char *name;
32 int (*func)(int arg);
33 int arg;
36 static test_option opts[] = {
37 { "0", "sleep test", &sleep_test, 0 },
38 { "1", "spawn threads, run forever", &thread_spawn_test, 0 },
39 { "2", "spawn threads, kill them", &thread_spawn_test, 1 },
40 { "3", "spawn threads, self terminating", &thread_spawn_test, 2 },
41 { "4", "syscall benchmark", &syscall_bench, 0 },
42 { "5", "test signals", &sig_test, 0 },
43 { "6", "fpu safety test", &fpu_test, 0 },
44 { 0, 0, 0, 0 }
47 static int get_line(char *buf, int buflen)
49 int i = 0;
50 char c;
52 for(;;) {
53 c = getchar();
54 if(c == '\n' || c == '\r') {
55 buf[i] = 0;
56 break;
59 buf[i++] = tolower(c);
60 if(i == buflen - 1) {
61 buf[i] = 0;
62 break;
65 return i;
68 int main(int argc, char **argv)
70 char command[128];
72 printf("welcome to the newos testapp!\n");
74 for(int cnt = 0; cnt< argc; cnt++) {
75 printf("arg %d = %s \n",cnt,argv[cnt]);
78 retry:
79 printf("Select from tests below, or 'x' to exit:\n");
81 for(test_option *opt = opts; opt->command; opt++) {
82 printf("%s\t%s\n", opt->command, opt->name);
84 printf("test# ");
86 retry_line:
87 get_line(command, sizeof(command));
89 if(strcmp(command, "x") == 0)
90 return 0;
91 if(strlen(command) == 0)
92 goto retry_line;
94 for(test_option *opt = opts; opt->command; opt++) {
95 if(strcmp(command, opt->command) == 0) {
96 printf("\n");
97 int err = opt->func(opt->arg);
98 printf("test returns %d\n", err);
99 break;
103 goto retry;
106 int foo(int argc, char **argv)
108 int rc = 0;
109 #if 0
110 int fd;
111 size_t len;
112 char c;
113 int cnt;
114 #endif
116 #if 0
117 fd = open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE);
118 if(fd >= 0) {
119 for(;;) {
120 size_t len;
121 static char buf[] = {
122 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0x0F, 0x00, 0x0F, 0x00, 0x0F,
123 0x00, 0x08, 0x00, 0x45, 0x00, 0x00, 0x28, 0xF9, 0x55, 0x40, 0x00,
124 0x40, 0x06, 0xC0, 0x02, 0xC0, 0xA8, 0x00, 0x01, 0xC0, 0xA8, 0x00,
125 0x26, 0x00, 0x50, 0x0B, 0x5C, 0x81, 0xD6, 0xFA, 0x48, 0xBB, 0x17,
126 0x03, 0xC9, 0x50, 0x10, 0x7B, 0xB0, 0x6C, 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
129 len = sizeof(buf);
130 pwrite(fd, buf, &len, 0);
133 #endif
134 #if 0
135 fd = open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE);
136 if(fd >= 0) {
137 int foo = 0;
138 for(;;) {
139 size_t len;
140 char buf[1500];
142 len = sizeof(buf);
143 pread(fd, buf, &len, 0);
144 printf("%d read %d bytes\n", foo++, len);
147 #endif
149 // XXX dangerous! This overwrites the beginning of your hard drive
150 #if 0
152 char buf[512];
153 size_t bytes_read;
155 printf("opening /dev/bus/ide/0/0/raw\n");
156 fd = open("/dev/bus/ide/0/0/raw", "", STREAM_TYPE_DEVICE);
157 printf("fd = %d\n", fd);
159 bytes_read = 512;
160 rc = pread(fd, buf, &bytes_read, 0);
161 printf("rc = %d, bytes_read = %d\n", rc, bytes_read);
163 buf[0] = 'f';
164 buf[1] = 'o';
165 buf[2] = 'o';
166 buf[3] = '2';
167 buf[4] = 0;
169 bytes_read = 512;
170 rc = pwrite(fd, buf, &bytes_read, 1024);
171 printf("rc = %d, bytes_read = %d\n", rc, bytes_read);
173 close(fd);
175 #endif
176 #if 0
178 thread_id tids[10];
179 int i;
181 for(i=0; i<10; i++) {
182 tids[i] = _kern_thread_create_thread("foo", &test_thread, (void *)i);
183 _kern_thread_resume_thread(tids[i]);
186 usleep(5000000);
187 _kern_proc_kill_proc(_kern_get_current_proc_id());
189 usleep(3000000);
190 for(i=0; i<10; i++) {
191 _kern_thread_kill_thread(tids[i]);
193 printf("thread_is dead\n");
194 usleep(5000000);
197 #endif
198 #if 0
200 for(;;)
201 _kern_proc_create_proc("/boot/bin/true", "true", 32);
203 #endif
204 #if 0
206 void *buf = (void *)0x60000000;
207 int fd;
208 int rc;
209 int len = 512;
211 fd = open("/boot/testapp", "", STREAM_TYPE_FILE);
213 rc = pread(fd, buf, &len, 0);
214 printf("rc from read = 0x%x\n", rc);
215 close(fd);
217 #endif
218 #if 0
220 char data;
221 int fd;
223 fd = open("/dev/audio/pcbeep/1", STREAM_TYPE_DEVICE, 0);
224 if(fd >= 0) {
225 printf("writing to the speaker\n");
226 data = 3;
227 pwrite(fd, &data, 1, 0);
228 usleep(1000000);
229 data = 0;
230 pwrite(fd, &data, 1, 0);
231 close(fd);
234 #endif
236 #if 0
238 port_test();
240 #endif
241 #if 0
243 int fd, bytes_read;
244 char buf[3];
246 fd = open("/dev/ps2mouse", STREAM_TYPE_DEVICE, 0);
247 if(fd < 0) {
248 printf("failed to open device\n");
249 return -1;
252 bytes_read = read(fd, buf, 3);
253 if(bytes_read < 3) {
254 printf("failed to read device\n");
255 return -1;
258 printf("Status: %X\nDelta X: %d\nDelta Y: %d\n", buf[0], buf[1], buf[2]);
260 #endif
261 #if 0
263 int fd;
264 int buf[512/4];
265 int i, j;
267 fd = open("/dev/disk/netblock/0/raw", STREAM_TYPE_DEVICE, 0);
268 if(fd < 0) {
269 printf("could not open netblock\n");
270 return -1;
273 ioctl(fd, 90001, NULL, 0);
275 for(i=0; i<1*1024*1024; i += 512) {
276 for(j=0; j<512/4; j++)
277 buf[j] = i + j*4;
278 write(fd, buf, sizeof(buf));
281 // pread(fd, buf, sizeof(buf), 0);
282 // pwrite(fd, buf, sizeof(buf), 512);
284 #endif
285 #if 0
287 #define NUM_FDS 150
289 int fds[NUM_FDS], i;
290 struct rlimit rl;
292 rc = getrlimit(RLIMIT_NOFILE, &rl);
293 if (rc < 0) {
294 printf("error in getrlimit\n");
295 return -1;
298 printf("RLIMIT_NOFILE = %lu\n", rl.rlim_cur);
300 for(i = 0; i < NUM_FDS; i++) {
301 fds[i] = open("/boot/bin/testapp", 0);
302 if (fds[i] < 0) {
303 break;
307 printf("opened %d files\n", i);
309 rl.rlim_cur += 15;
311 printf("Setting RLIMIT_NOFILE to %lu\n", rl.rlim_cur);
312 rc = setrlimit(RLIMIT_NOFILE, &rl);
313 if (rc < 0) {
314 printf("error in setrlimit\n");
315 return -1;
318 rl.rlim_cur = 0;
319 rc = getrlimit(RLIMIT_NOFILE, &rl);
320 if (rc < 0) {
321 printf("error in getrlimit\n");
322 return -1;
325 printf("RLIMIT_NOFILE = %lu\n", rl.rlim_cur);
327 for(;i < NUM_FDS; i++) {
328 fds[i] = open("/boot/bin/testapp", 0);
329 if (fds[i] < 0) {
330 break;
334 printf("opened a total of %d files\n", i);
336 #endif
337 #if 0
339 region_id rid;
340 void *ptr;
342 rid = _kern_vm_map_file("netblock", &ptr, REGION_ADDR_ANY_ADDRESS, 16*1024, LOCK_RW,
343 REGION_NO_PRIVATE_MAP, "/dev/disk/netblock/0/raw", 0);
344 if(rid < 0) {
345 printf("error mmaping device\n");
346 return -1;
349 // play around with it
350 printf("mmaped device at %p\n", ptr);
351 printf("%d\n", *(int *)ptr);
352 printf("%d\n", *((int *)ptr + 1));
353 printf("%d\n", *((int *)ptr + 2));
354 printf("%d\n", *((int *)ptr + 3));
356 #endif
357 #if 0
359 int i;
361 // printf("spawning %d copies of true\n", 10000);
363 for(i=0; ; i++) {
364 proc_id id;
365 bigtime_t t;
367 printf("%d...", i);
369 t = _kern_system_time();
371 id = _kern_proc_create_proc("/boot/bin/true", "true", NULL, 0, 20);
372 if(id <= 0x2) {
373 printf("new proc returned 0x%x!\n", id);
374 return -1;
376 _kern_proc_wait_on_proc(id, NULL);
378 printf("done (%Ld usecs)\n", _kern_system_time() - t);
381 #endif
382 #if 0
384 int i;
386 printf("spawning two cpu eaters\n");
388 // _kern_thread_resume_thread(_kern_thread_create_thread("cpu eater 1", &cpu_eater_thread, 0));
389 // _kern_thread_resume_thread(_kern_thread_create_thread("cpu eater 2", &cpu_eater_thread, 0));
391 printf("spawning %d threads\n", 10000);
393 for(i=0; i<10000; i++) {
394 thread_id id;
395 bigtime_t t;
397 printf("%d...", i);
399 t = _kern_system_time();
401 id = _kern_thread_create_thread("testthread", &dummy_thread, 0);
402 _kern_thread_resume_thread(id);
403 _kern_thread_wait_on_thread(id, NULL);
405 printf("done (%Ld usecs)\n", _kern_system_time() - t);
408 #endif
409 #if 0
411 thread_id id;
412 static double f[5] = { 2.43, 5.23, 342.34, 234123.2, 1.4 };
414 printf("spawning a few floating point crunchers\n");
416 id = _kern_thread_create_thread("fpu thread0", &fpu_cruncher_thread, &f[0]);
417 _kern_thread_resume_thread(id);
419 id = _kern_thread_create_thread("fpu thread1", &fpu_cruncher_thread, &f[1]);
420 _kern_thread_resume_thread(id);
422 id = _kern_thread_create_thread("fpu thread2", &fpu_cruncher_thread, &f[2]);
423 _kern_thread_resume_thread(id);
425 id = _kern_thread_create_thread("fpu thread3", &fpu_cruncher_thread, &f[3]);
426 _kern_thread_resume_thread(id);
428 id = _kern_thread_create_thread("fpu thread4", &fpu_cruncher_thread, &f[4]);
429 _kern_thread_resume_thread(id);
431 getchar();
432 printf("passed the test\n");
434 #endif
435 #if 0
436 rc = pipe_test();
437 #endif
438 #if 0
440 int i,j,k;
441 int fd;
442 devfs_framebuffer_info fb;
443 int err;
444 void *framebuffer;
446 fd = open("/dev/graphics/fb/0", 0);
447 if(fd < 0) {
448 printf("error opening framebuffer device\n");
449 return -1;
452 err = ioctl(fd, IOCTL_DEVFS_GET_FRAMEBUFFER_INFO, &fb, sizeof(fb));
453 if(err < 0) {
454 printf("error getting framebuffer info\n");
455 return -1;
458 err = ioctl(fd, IOCTL_DEVFS_MAP_FRAMEBUFFER, &framebuffer, sizeof(framebuffer));
459 if(err < 0) {
460 printf("error mapping framebuffer\n");
461 return -1;
464 for(k=0;; k++) {
465 for(i=0; i<fb.height; i++) {
466 uint16 row[fb.width];
467 for(j=0; j<fb.width; j++) {
468 uint16 color = ((j+i+k) & 0x1f) << 11;
469 row[j] = color;
471 memcpy(framebuffer + i*fb.width*2, row, sizeof(row));
475 #endif
476 #if 0
478 int err;
480 printf("mounting nfs filesystem\n");
482 _kern_mkdir("/nfs");
483 err = _kern_mount("/nfs", "192.168.0.4:/disk", "nfs", NULL);
484 printf("mount returns %d\n", err);
487 int i;
488 int fd;
489 char buf[1024];
491 fd = _kern_opendir("/nfs");
493 for(i=0; i<16; i++) {
494 memset(buf, 0, sizeof(buf));
495 err = _kern_readdir(fd, buf, sizeof(buf));
496 printf("read returns %d '%s'\n", err, buf);
500 #endif
501 printf("exiting w/return code %d\n", rc);
502 return rc;