overhaul of the testapp, started making menu of tests, broke existing tests out into
[newos.git] / apps / testapp / porttests.cpp
blobd10ae71356442ef113e49b7898e8b71b9550e000
1 /*
2 ** Copyright 2003, 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 <sys/syscalls.h>
9 #include <newos/errors.h>
12 static void port_test(void);
13 static int port_test_thread_func(void* arg);
16 * testcode ports
19 port_id test_p1, test_p2, test_p3, test_p4;
21 static void port_test(void)
23 char testdata[5];
24 thread_id t;
25 int res;
26 int32 dummy;
27 int32 dummy2;
29 strcpy(testdata, "abcd");
31 printf("porttest: port_create()\n");
32 test_p1 = sys_port_create(1, "test port #1");
33 test_p2 = sys_port_create(10, "test port #2");
34 test_p3 = sys_port_create(1024, "test port #3");
35 test_p4 = sys_port_create(1024, "test port #4");
37 printf("porttest: port_find()\n");
38 printf("'test port #1' has id %d (should be %d)\n", sys_port_find("test port #1"), test_p1);
40 printf("porttest: port_write() on 1, 2 and 3\n");
41 sys_port_write(test_p1, 1, &testdata, sizeof(testdata));
42 sys_port_write(test_p2, 666, &testdata, sizeof(testdata));
43 sys_port_write(test_p3, 999, &testdata, sizeof(testdata));
44 printf("porttest: port_count(test_p1) = %d\n", sys_port_count(test_p1));
46 printf("porttest: port_write() on 1 with timeout of 1 sec (blocks 1 sec)\n");
47 sys_port_write_etc(test_p1, 1, &testdata, sizeof(testdata), PORT_FLAG_TIMEOUT, 1000000);
48 printf("porttest: port_write() on 2 with timeout of 1 sec (wont block)\n");
49 res = sys_port_write_etc(test_p2, 777, &testdata, sizeof(testdata), PORT_FLAG_TIMEOUT, 1000000);
50 printf("porttest: res=%d, %s\n", res, res == 0 ? "ok" : "BAD");
52 printf("porttest: port_read() on empty port 4 with timeout of 1 sec (blocks 1 sec)\n");
53 res = sys_port_read_etc(test_p4, &dummy, &dummy2, sizeof(dummy2), PORT_FLAG_TIMEOUT, 1000000);
54 printf("porttest: res=%d, %s\n", res, res == ERR_PORT_TIMED_OUT ? "ok" : "BAD");
56 printf("porttest: spawning thread for port 1\n");
57 t = sys_thread_create_thread("port_test", port_test_thread_func, NULL);
58 // resume thread
59 sys_thread_resume_thread(t);
61 printf("porttest: write\n");
62 sys_port_write(test_p1, 1, &testdata, sizeof(testdata));
64 // now we can write more (no blocking)
65 printf("porttest: write #2\n");
66 sys_port_write(test_p1, 2, &testdata, sizeof(testdata));
67 printf("porttest: write #3\n");
68 sys_port_write(test_p1, 3, &testdata, sizeof(testdata));
70 printf("porttest: waiting on spawned thread\n");
71 sys_thread_wait_on_thread(t, NULL);
73 printf("porttest: close p1\n");
74 sys_port_close(test_p2);
75 printf("porttest: attempt write p1 after close\n");
76 res = sys_port_write(test_p2, 4, &testdata, sizeof(testdata));
77 printf("porttest: port_write ret %d\n", res);
79 printf("porttest: testing delete p2\n");
80 sys_port_delete(test_p2);
82 printf("porttest: end test main thread\n");
85 static int port_test_thread_func(void* arg)
87 int msg_code;
88 int n;
89 char buf[5];
91 printf("porttest: port_test_thread_func()\n");
93 n = sys_port_read(test_p1, &msg_code, &buf, 3);
94 printf("port_read #1 code %d len %d buf %3s\n", msg_code, n, buf);
95 n = sys_port_read(test_p1, &msg_code, &buf, 4);
96 printf("port_read #1 code %d len %d buf %4s\n", msg_code, n, buf);
97 buf[4] = 'X';
98 n = sys_port_read(test_p1, &msg_code, &buf, 5);
99 printf("port_read #1 code %d len %d buf %5s\n", msg_code, n, buf);
101 printf("porttest: testing delete p1 from other thread\n");
102 sys_port_delete(test_p1);
103 printf("porttest: end port_test_thread_func()\n");
105 return 0;