welcome to memory model land.
[newos.git] / apps / testapp / porttests.cpp
blob8070b14e1981bc9ec5dc3a2c96329767aa66a721
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 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 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 = _kern_port_create(1, "test port #1");
33 test_p2 = _kern_port_create(10, "test port #2");
34 test_p3 = _kern_port_create(1024, "test port #3");
35 test_p4 = _kern_port_create(1024, "test port #4");
37 printf("porttest: port_find()\n");
38 printf("'test port #1' has id %d (should be %d)\n", _kern_port_find("test port #1"), test_p1);
40 printf("porttest: port_write() on 1, 2 and 3\n");
41 _kern_port_write(test_p1, 1, &testdata, sizeof(testdata));
42 _kern_port_write(test_p2, 666, &testdata, sizeof(testdata));
43 _kern_port_write(test_p3, 999, &testdata, sizeof(testdata));
44 printf("porttest: port_count(test_p1) = %d\n", _kern_port_count(test_p1));
46 printf("porttest: port_write() on 1 with timeout of 1 sec (blocks 1 sec)\n");
47 _kern_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 = _kern_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 = _kern_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 = _kern_thread_create_thread("port_test", port_test_thread_func, NULL);
58 // resume thread
59 _kern_thread_resume_thread(t);
61 printf("porttest: write\n");
62 _kern_port_write(test_p1, 1, &testdata, sizeof(testdata));
64 // now we can write more (no blocking)
65 printf("porttest: write #2\n");
66 _kern_port_write(test_p1, 2, &testdata, sizeof(testdata));
67 printf("porttest: write #3\n");
68 _kern_port_write(test_p1, 3, &testdata, sizeof(testdata));
70 printf("porttest: waiting on spawned thread\n");
71 _kern_thread_wait_on_thread(t, NULL);
73 printf("porttest: close p1\n");
74 _kern_port_close(test_p2);
75 printf("porttest: attempt write p1 after close\n");
76 res = _kern_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 _kern_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 = _kern_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 = _kern_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 = _kern_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 _kern_port_delete(test_p1);
103 printf("porttest: end port_test_thread_func()\n");
105 return 0;