Added the repository from the odin svn
[educational.data.git] / fag / speciale / nodes / src / libs / utils / test.cpp
blob437b75370da36edb253af8407e50985fb9cb063d
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #else
4 # error I depend on config.h
5 #endif
7 #include "common.h"
9 #include "crc32.h"
10 #include "array.h"
11 #include "queue.h"
12 #include "lrandom.h"
13 #include "stdio.h"
14 #include <assert.h>
15 #include "lobject.h"
16 #include "lmemory.h"
17 #include "lsocket.h"
19 void test_array() {
20 larray_t array;
22 char message1[] = "Buuh";
23 char message2[] = "Baa";
25 larray_create (&array);
26 assert(larray_buffersize(&array) == 1);
27 larray_insert (&array, message1);
28 larray_insert (&array, message2);
29 assert (larray_get (&array, 0) == message1);
30 assert (larray_get (&array, 1) == message2);
32 assert (larray_contains (&array, message1) == TRUE);
33 assert (larray_contains (&array, message2) == TRUE);
34 assert (larray_size (&array) == 2);
35 assert(larray_buffersize(&array) == 11);
37 assert (larray_contains(&array, message1) == TRUE);
38 larray_remove (&array, message1);
39 assert (larray_contains(&array, message1) == FALSE);
40 assert (larray_get (&array, 0) == message2);
41 assert (larray_size(&array) == 1);
42 larray_clear(&array);
43 assert (larray_size(&array) == 0);
44 larray_destroy (&array);
47 void test_queue() {
48 char message1[] = "Buuh";
49 char message2[] = "Baa";
51 lqueue q;
53 lqueue_create (&q);
54 lqueue_push (&q, message1);
55 lqueue_push (&q, message2);
57 assert (lqueue_empty(&q) == FALSE);
58 assert (lqueue_pop (&q) == message1);
59 assert (lqueue_pop (&q) == message2);
60 assert (lqueue_empty(&q) == TRUE);
61 assert (lqueue_pop (&q) == NULL);
62 lqueue_destroy (&q);
65 void test_rand()
67 int i;
68 for(i = 0;i< 20;i++) {
69 printf("%d ",(int)(lrandom() % 100));
71 printf("\n");
75 struct test_object_t {
76 LOBJECT;
77 int i;
80 int test_val = 0;
81 void destroy_test_object(struct test_object_t *obj) {
82 test_val = 1;
83 lfree(obj);
86 void test_lobject()
88 struct test_object_t *obj = lobject_create(struct test_object_t);
90 test_val=0;
91 lobject_set_destructor(obj, (lobject_destructor)&destroy_test_object);
94 assert(test_val == 0);
95 up(obj);
96 assert(test_val == 1);
99 BOOL retval = FALSE;
100 int wc = 0;
101 int rc = 0;
102 int pc = 0;
104 int
105 socket_writer(lsocket_provider_t *p,
106 uint8_t protocol,
107 object_t ptr,
108 object_t buf,
109 uint8_t size)
111 wc++;
112 return 0;
115 int
116 socket_reader(lsocket_provider_t *p,
117 uint8_t protocol,
118 object_t ptr,
119 object_t buf,
120 uint8_t size)
122 rc++;
123 return 0;
126 BOOL poll(lsocket_provider_t *p,
127 uint8_t protocol,
128 object_t ptr)
130 pc++;
131 return FALSE;
134 struct lsocket_provider_funcs funcs;
136 void test_socket()
138 assert(wc == 0);
139 assert(rc == 0);
140 assert(pc == 0);
141 struct lsocket_provider *provider =
142 lsocket_create_provider(NULL);
143 assert(provider == NULL);
145 funcs.write_to_socket = socket_writer;
146 funcs.read_from_socket = socket_reader;
147 funcs.poll = poll;
149 provider = lsocket_create_provider(&funcs);
150 provider->ptr = NULL;
152 retval = FALSE;
153 struct lsocket *sock = lsocket_create(0,0);
154 assert(sock != NULL);
156 assert(socket_write(sock,NULL,0) < 0);
157 assert(rc == 0);
158 assert(socket_read(sock,NULL,0) < 0);
159 assert(wc == 0);
161 lsocket_provider_connect_socket(provider,sock);
163 assert(socket_write(sock,NULL,0) == 0);
164 assert(rc == 1);
165 assert(socket_read(sock,NULL,0) == 0);
166 assert(wc == 1);
168 up(sock);
170 up(provider);
174 main (int argc, char *argv)
176 test_array();
177 test_queue();
178 //test_rand();
181 test_lobject();
183 test_socket();
185 return 0;