From cd9ae8d4f5685c4baae153dda9a60605abd9a140 Mon Sep 17 00:00:00 2001 From: Travis Geiselbrecht Date: Fri, 24 Jan 2003 22:19:31 +0000 Subject: [PATCH] overhaul of the testapp, started making menu of tests, broke existing tests out into seperate files, though they are not wired into the main menu. git-svn-id: svn+ssh://newos.org/var/svn/newos/newos@853 c25cc9d1-44fa-0310-b259-ad778cb1d433 --- apps/testapp/main.cpp | 269 ++++++++++--------------------------------- apps/testapp/makefile | 6 +- apps/testapp/pipetests.cpp | 70 +++++++++++ apps/testapp/porttests.cpp | 107 +++++++++++++++++ apps/testapp/tests.h | 13 +++ apps/testapp/threadtests.cpp | 111 ++++++++++++++++++ apps/testapp/vmtests.cpp | 9 ++ 7 files changed, 378 insertions(+), 207 deletions(-) create mode 100644 apps/testapp/pipetests.cpp create mode 100644 apps/testapp/porttests.cpp create mode 100644 apps/testapp/tests.h create mode 100644 apps/testapp/threadtests.cpp create mode 100644 apps/testapp/vmtests.cpp diff --git a/apps/testapp/main.cpp b/apps/testapp/main.cpp index e17168d..e530567 100644 --- a/apps/testapp/main.cpp +++ b/apps/testapp/main.cpp @@ -1,92 +1,101 @@ /* -** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. +** Copyright 2003, Travis Geiselbrecht. All rights reserved. ** Distributed under the terms of the NewOS License. */ #include #include #include +#include #include #include #include #include #include -static int pipe_test(void); +#include "tests.h" -static int pipe_test(void); +struct test_option { + const char *command; + const char *name; + int (*func)(int arg); + int arg; +}; -static void port_test(void); -static int port_test_thread_func(void* arg); +static test_option opts[] = { + { "0", "sleep test", &sleep_test, 0 }, + { "1", "spawn threads, run forever", &thread_spawn_test, 0 }, + { "2", "spawn threads, kill them", &thread_spawn_test, 1 }, + { "3", "spawn threads, self terminating", &thread_spawn_test, 2 }, + { 0, 0, 0, 0 } +}; -static int test_thread(void *args) +static int get_line(char *buf, int buflen) { - int i = (int)args; + int i = 0; + char c; - sys_snooze(1000000); for(;;) { - printf("%c", 'a' + i); + c = getchar(); + if(c == '\n' || c == '\r') { + buf[i] = 0; + break; + } + + buf[i++] = tolower(c); + if(i == buflen - 1) { + buf[i] = 0; + break; + } } - return 0; + return i; } -static int dummy_thread(void *args) +int main(int argc, char **argv) { - return 1; -} + char command[128]; -static int cpu_eater_thread(void *args) -{ - for(;;) - ; -} + printf("welcome to the newos testapp!\n"); -static int fpu_cruncher_thread(void *args) -{ - double y = *(double *)args; - double z; + for(int cnt = 0; cnt< argc; cnt++) { + printf("arg %d = %s \n",cnt,argv[cnt]); + } - for(;;) { - z = y * 1.47; - y = z / 1.47; - if(y != *(double *)args) - printf("error: y %f\n", y); +retry: + printf("Select from tests below, or 'x' to exit:\n"); + + for(test_option *opt = opts; opt->command; opt++) { + printf("%s\t%s\n", opt->command, opt->name); + } + printf("> "); + +retry_line: + get_line(command, sizeof(command)); + + if(strcmp(command, "x") == 0) + return 0; + if(strlen(command) == 0) + goto retry_line; + + for(test_option *opt = opts; opt->command; opt++) { + if(strcmp(command, opt->command) == 0) { + printf("\n"); + int err = opt->func(opt->arg); + printf("test returns %d\n", err); + break; + } } + + goto retry; } -int main(int argc, char **argv) +int foo(int argc, char **argv) { int fd; size_t len; char c; int rc = 0; int cnt; - printf("testapp\n"); - - for(cnt = 0; cnt< argc; cnt++) { - printf("arg %d = %s \n",cnt,argv[cnt]); - } - - printf("my thread id is %d\n", sys_get_current_thread_id()); -#if 0 - printf("enter something: "); - for(;;) { - c = getchar(); - printf("%c", c); - } -#endif -#if 0 - for(;;) { - sys_snooze(100000); - printf("booyah!"); - } - - for(;;); -#endif -#if 0 - printf("waiting 5 seconds\n"); - sys_snooze(5000000); -#endif #if 0 fd = sys_open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE); if(fd >= 0) { @@ -476,156 +485,4 @@ int main(int argc, char **argv) return rc; } -static int pipe_read_thread(void *args) -{ - int fd = *(int *)args; - int err; - char buf[1024]; - int i; - - for(;;) { - err = read(fd, buf, sizeof(buf)); - printf("pipe_read_thread: read returns %d\n", err); - if(err < 0) - break; - - printf("'"); - for(i=0; i +#include +#include +#include + +static int pipe_read_thread(void *args) +{ + int fd = *(int *)args; + int err; + char buf[1024]; + int i; + + for(;;) { + err = read(fd, buf, sizeof(buf)); + printf("pipe_read_thread: read returns %d\n", err); + if(err < 0) + break; + + printf("'"); + for(i=0; i +#include +#include +#include +#include + + +static void port_test(void); +static int port_test_thread_func(void* arg); + +/* + * testcode ports + */ + +port_id test_p1, test_p2, test_p3, test_p4; + +static void port_test(void) +{ + char testdata[5]; + thread_id t; + int res; + int32 dummy; + int32 dummy2; + + strcpy(testdata, "abcd"); + + printf("porttest: port_create()\n"); + test_p1 = sys_port_create(1, "test port #1"); + test_p2 = sys_port_create(10, "test port #2"); + test_p3 = sys_port_create(1024, "test port #3"); + test_p4 = sys_port_create(1024, "test port #4"); + + printf("porttest: port_find()\n"); + printf("'test port #1' has id %d (should be %d)\n", sys_port_find("test port #1"), test_p1); + + printf("porttest: port_write() on 1, 2 and 3\n"); + sys_port_write(test_p1, 1, &testdata, sizeof(testdata)); + sys_port_write(test_p2, 666, &testdata, sizeof(testdata)); + sys_port_write(test_p3, 999, &testdata, sizeof(testdata)); + printf("porttest: port_count(test_p1) = %d\n", sys_port_count(test_p1)); + + printf("porttest: port_write() on 1 with timeout of 1 sec (blocks 1 sec)\n"); + sys_port_write_etc(test_p1, 1, &testdata, sizeof(testdata), PORT_FLAG_TIMEOUT, 1000000); + printf("porttest: port_write() on 2 with timeout of 1 sec (wont block)\n"); + res = sys_port_write_etc(test_p2, 777, &testdata, sizeof(testdata), PORT_FLAG_TIMEOUT, 1000000); + printf("porttest: res=%d, %s\n", res, res == 0 ? "ok" : "BAD"); + + printf("porttest: port_read() on empty port 4 with timeout of 1 sec (blocks 1 sec)\n"); + res = sys_port_read_etc(test_p4, &dummy, &dummy2, sizeof(dummy2), PORT_FLAG_TIMEOUT, 1000000); + printf("porttest: res=%d, %s\n", res, res == ERR_PORT_TIMED_OUT ? "ok" : "BAD"); + + printf("porttest: spawning thread for port 1\n"); + t = sys_thread_create_thread("port_test", port_test_thread_func, NULL); + // resume thread + sys_thread_resume_thread(t); + + printf("porttest: write\n"); + sys_port_write(test_p1, 1, &testdata, sizeof(testdata)); + + // now we can write more (no blocking) + printf("porttest: write #2\n"); + sys_port_write(test_p1, 2, &testdata, sizeof(testdata)); + printf("porttest: write #3\n"); + sys_port_write(test_p1, 3, &testdata, sizeof(testdata)); + + printf("porttest: waiting on spawned thread\n"); + sys_thread_wait_on_thread(t, NULL); + + printf("porttest: close p1\n"); + sys_port_close(test_p2); + printf("porttest: attempt write p1 after close\n"); + res = sys_port_write(test_p2, 4, &testdata, sizeof(testdata)); + printf("porttest: port_write ret %d\n", res); + + printf("porttest: testing delete p2\n"); + sys_port_delete(test_p2); + + printf("porttest: end test main thread\n"); +} + +static int port_test_thread_func(void* arg) +{ + int msg_code; + int n; + char buf[5]; + + printf("porttest: port_test_thread_func()\n"); + + n = sys_port_read(test_p1, &msg_code, &buf, 3); + printf("port_read #1 code %d len %d buf %3s\n", msg_code, n, buf); + n = sys_port_read(test_p1, &msg_code, &buf, 4); + printf("port_read #1 code %d len %d buf %4s\n", msg_code, n, buf); + buf[4] = 'X'; + n = sys_port_read(test_p1, &msg_code, &buf, 5); + printf("port_read #1 code %d len %d buf %5s\n", msg_code, n, buf); + + printf("porttest: testing delete p1 from other thread\n"); + sys_port_delete(test_p1); + printf("porttest: end port_test_thread_func()\n"); + + return 0; +} + diff --git a/apps/testapp/tests.h b/apps/testapp/tests.h new file mode 100644 index 0000000..9c48f4c --- /dev/null +++ b/apps/testapp/tests.h @@ -0,0 +1,13 @@ +/* +** Copyright 2003, Travis Geiselbrecht. All rights reserved. +** Distributed under the terms of the NewOS License. +*/ +#ifndef _TESTAPP_TESTS_H +#define _TESTAPP_TESTS_H + +// thread tests +int sleep_test(int arg); +int thread_spawn_test(int arg); + +#endif + diff --git a/apps/testapp/threadtests.cpp b/apps/testapp/threadtests.cpp new file mode 100644 index 0000000..eb4f6a3 --- /dev/null +++ b/apps/testapp/threadtests.cpp @@ -0,0 +1,111 @@ +/* +** Copyright 2003, Travis Geiselbrecht. All rights reserved. +** Distributed under the terms of the NewOS License. +*/ +#include +#include +#include +#include + +int sleep_test(int arg) +{ + printf("should display 'booyah!' 10 times, one second apart\n"); + for(int i = 0; i < 10; i++) { + sys_snooze(1000000); + printf("booyah!"); + } + + return 0; +} + +static int test_thread(void *args) +{ + int i = (int)args; + + for(;;) { + printf("%c", 'a' + i); + } + return 0; +} + +static int test_thread_self_terminate(void *args) +{ + int i = (int)args; + bigtime_t start_time = sys_system_time(); + + for(;;) { + if(sys_system_time() - start_time >= 10000000) { + printf("thread %c terminating...\n", 'a' + i); + break; + } + printf("%c", 'a' + i); + } + return 0; +} + +static int dummy_thread(void *args) +{ + return 1; +} + +static int cpu_eater_thread(void *args) +{ + for(;;) + ; +} + +static int fpu_cruncher_thread(void *args) +{ + double y = *(double *)args; + double z; + + for(;;) { + z = y * 1.47; + y = z / 1.47; + if(y != *(double *)args) + printf("error: y %f\n", y); + } +} + +int thread_spawn_test(int arg) +{ + int i; + int (*thread_func)(void *) = NULL; + int num_threads = 10; + + if(arg == 0) { + printf("creating 10 threads, runs forever...\n"); + thread_func = &test_thread; + } else if(arg == 1) { + printf("creating 10 threads, then killing them after 10 seconds\n"); + thread_func = &test_thread; + } else if(arg == 2) { + printf("creating 10 threads, self terminating after 10 seconds\n"); + thread_func = &test_thread_self_terminate; + } else + return -1; + + thread_id tids[num_threads]; + for(i=0; i +#include +#include +#include + -- 2.11.4.GIT