getifaddrs: add un-namespaced alias
[minix.git] / test / ds / dstest.c
blobf903b34a12ac55ddb14612baa5b36cf189037b98
1 #include "inc.h"
3 char *key_u32 = "test_u32";
4 char *key_str = "test_str";
5 char *key_mem = "test_mem";
6 char *key_map = "test_map";
7 char *key_label = "test_label";
9 /*===========================================================================*
10 * test_u32 *
11 *===========================================================================*/
12 void test_u32(void)
14 int r;
15 u32_t value;
17 /* Publish and retrieve. */
18 r = ds_publish_u32(key_u32, 1234, 0);
19 assert(r == OK);
20 r = ds_retrieve_u32(key_u32, &value);
21 assert(r == OK && value == 1234);
23 /* If dstest deletes 'test_u32' immediately after publishing it,
24 * subs will catch the event, but it can't check it immediately.
25 * So dstest will sleep 2 seconds to wait for subs to complete. */
26 sleep(2);
28 /* Publish again without DSF_OVERWRITE. */
29 r = ds_publish_u32(key_u32, 4321, 0);
30 assert(r == EEXIST);
32 /* Publish again with DSF_OVERWRITE to overwrite it. */
33 r = ds_publish_u32(key_u32, 4321, DSF_OVERWRITE);
34 assert(r == OK);
35 r = ds_retrieve_u32(key_u32, &value);
36 assert(r == OK && value == 4321);
38 /* Delete. */
39 r = ds_delete_u32(key_u32);
40 assert(r == OK);
41 r = ds_retrieve_u32(key_u32, &value);
42 assert(r == ESRCH);
44 printf("DSTEST: U32 test successful!\n");
47 /*===========================================================================*
48 * test_str *
49 *===========================================================================*/
50 void test_str(void)
52 int r;
53 char *string = "little";
54 char *longstring = "verylooooooongstring";
55 char buf[17];
57 r = ds_publish_str(key_str, string, 0);
58 assert(r == OK);
60 r = ds_retrieve_str(key_str, buf, sizeof(buf)-1);
61 assert(r == OK && strcmp(string, buf) == 0);
63 r = ds_delete_str(key_str);
64 assert(r == OK);
66 /* Publish a long string. */
67 r = ds_publish_str(key_str, longstring, 0);
68 assert(r == OK);
70 r = ds_retrieve_str(key_str, buf, sizeof(buf)-1);
71 assert(r == OK && strcmp(string, buf) != 0
72 && strncmp(longstring, buf, sizeof(buf)-1) == 0);
74 r = ds_delete_str(key_str);
75 assert(r == OK);
77 printf("DSTEST: STRING test successful!\n");
80 /*===========================================================================*
81 * test_mem *
82 *===========================================================================*/
83 void test_mem(void)
85 char *string1 = "ok, this is a string";
86 char *string2 = "ok, this is a very looooong string";
87 size_t len1 = strlen(string1) + 1;
88 size_t len2 = strlen(string2) + 1;
89 char buf[100];
90 size_t get_len;
91 int r;
93 /* Publish and retrieve. */
94 r = ds_publish_mem(key_mem, string1, len1, 0);
95 assert(r == OK);
96 get_len = 100;
97 r = ds_retrieve_mem(key_mem, buf, &get_len);
98 assert(r == OK && strcmp(string1, buf) == 0);
99 assert(get_len == len1);
101 /* Let get_len=8, which is less than strlen(string1). */
102 get_len = 8;
103 r = ds_retrieve_mem(key_mem, buf, &get_len);
104 assert(r == OK && get_len == 8);
106 /* Publish again to overwrite with a bigger memory range. */
107 r = ds_publish_mem(key_mem, string2, len2, DSF_OVERWRITE);
108 assert(r == OK);
110 get_len = 100;
111 r = ds_retrieve_mem(key_mem, buf, &get_len);
112 assert(r == OK && strcmp(string2, buf) == 0);
113 assert(get_len == len2);
115 r = ds_delete_mem(key_mem);
116 assert(r == OK);
118 printf("DSTEST: MEM test successful!\n");
121 /*===========================================================================*
122 * test_label *
123 *===========================================================================*/
124 void test_label(void)
126 int r;
127 char label[DS_MAX_KEYLEN];
128 endpoint_t endpoint;
130 /* Retrieve own label and endpoint. */
131 r = ds_retrieve_label_name(label, getprocnr());
132 assert(r == OK);
133 r = ds_retrieve_label_endpt(label, &endpoint);
134 assert(r == OK && endpoint == getprocnr());
136 /* Publish and delete. */
137 r = ds_publish_label(label, endpoint, 0);
138 assert(r == EPERM);
139 r = ds_delete_label(label);
140 assert(r == EPERM);
142 printf("DSTEST: LABEL test successful!\n");
145 /*===========================================================================*
146 * test_map *
147 *===========================================================================*/
148 void test_map(void)
150 char buf_buf[CLICK_SIZE * 2];
151 char buf_buf2[CLICK_SIZE * 2];
152 char *buf, *buf2;
153 char get_buf[CLICK_SIZE];
154 int *p;
155 volatile int *p2;
156 int *get_p;
157 size_t get_len;
158 int is;
159 int r;
161 buf = (char*) CLICK_CEIL(buf_buf);
162 buf2 = (char*) CLICK_CEIL(buf_buf2);
164 p = (int *)buf;
165 p2 = (int *)buf2;
166 get_p = (int *)get_buf;
168 *p = 1;
169 r = ds_publish_map(key_map, buf, CLICK_SIZE, 0);
170 assert(r == OK);
172 r = ds_snapshot_map(key_map, &is);
173 assert(r == OK);
175 /* Copy the mapped memory range.
176 * Set *p=2, then the mapped memory range should change too
177 * and *get_p should be 2.
179 *p = 2;
180 get_len = CLICK_SIZE;
181 r = ds_retrieve_map(key_map, get_buf, &get_len, 0, DSMF_COPY_MAPPED);
182 assert(r == OK && get_len == CLICK_SIZE && *get_p == 2);
184 /* Copy snapshot, where *get_p should still be 1. */
185 get_len = CLICK_SIZE;
186 r = ds_retrieve_map(key_map, get_buf, &get_len, is, DSMF_COPY_SNAPSHOT);
187 assert(r == OK && get_len == CLICK_SIZE && *get_p == 1);
189 /* Map the mapped memory range to @buf2, then set *p=3, which
190 * in turn should let *p2=3.
192 get_len = CLICK_SIZE;
193 r = ds_retrieve_map(key_map, buf2, &get_len, 0, DSMF_MAP_MAPPED);
194 assert(r == OK && get_len == CLICK_SIZE);
195 *p = 3;
196 assert(*p2 == 3);
198 r = ds_delete_map(key_map);
199 assert(r == OK);
201 printf("DSTEST: MAP test successful!\n");
204 /*===========================================================================*
205 * sef_cb_init_fresh *
206 *===========================================================================*/
207 static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
209 /* Run all the tests. */
210 test_u32();
211 test_str();
212 test_mem();
213 test_map();
214 test_label();
216 return OK;
219 /*===========================================================================*
220 * sef_local_startup *
221 *===========================================================================*/
222 static void sef_local_startup(void)
224 /* Let SEF perform startup. */
225 sef_setcb_init_fresh(sef_cb_init_fresh);
227 sef_startup();
230 /*===========================================================================*
231 * main *
232 *===========================================================================*/
233 int main(void)
235 /* SEF local startup. */
236 sef_local_startup();
238 return 0;