Bump version to 0.3.5next
[archipelago] / xseg / xtypes / xpool_test.c
1 /*
2  * Copyright 2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *   2. Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and
30  * documentation are those of the authors and should not be
31  * interpreted as representing official policies, either expressed
32  * or implied, of GRNET S.A.
33  */
34
35 #include <xpool.h>
36 #include <stdlib.h>
37 #include <pthread.h>
38 #include <sys/time.h>
39 #include <xseg/xseg.h>
40
41 uint64_t size = 16;
42 uint64_t loops = 100000;
43
44 struct xpool xp;
45 struct xq xq;
46
47 void test_all_funcs(){
48         xpool_index idx1, idx2, ret;
49         xpool_data data;
50         
51         idx1 = xpool_add(&xp, (xpool_data) 5);
52         if (idx1 == NoSerial) {
53                 printf("couldn't add idx1\n");
54                 return 1;
55         }
56         printf("added 5 in idx %llu\n", idx1);
57         idx2 = xpool_add(&xp, (xpool_data) 6);
58         if (idx2 == NoSerial) {
59                 printf("couldn't add idx2\n");
60                 return 1;
61         }
62         printf("added 6 in idx %llu\n", idx2);
63
64         ret = xpool_peek(&xp, &data);
65         if (ret == NoSerial) {
66                 printf("couldn't peek\n");
67                 return 1;
68         }
69         printf("peeked xpool (idx %llu) and took data %u\n", ret, data);
70         ret = xpool_peek_idx(&xp, idx2, &data);
71         if (ret != idx2) {
72                 printf("couldn't peek\n");
73                 return 1;
74         }
75         printf("peeked xpool in idx %llu and took data %u\n", idx2, data);
76         ret = xpool_peek_and_fwd(&xp, &data);
77         if (ret == NoSerial) {
78                 printf("couldn't peek\n");
79                 return 1;
80         }
81         printf("peeked and fwd xpool (idx %llu) and took data %u\n", ret, data);
82         ret = xpool_peek_and_fwd(&xp, &data);
83         if (ret == NoSerial) {
84                 printf("couldn't peek\n");
85                 return 1;
86         }
87         printf("peeked and fwd xpool (idx %llu) and took data %u\n", ret, data);
88
89         ret = xpool_remove(&xp, idx1, &data);
90         if (ret != idx1) {
91                 printf("couldn't remove idx1\n");
92                 return 1;
93         }
94         if (data != (xpool_data) 5) {
95                 printf("idx1 returned wrong value (%u)\n", data);
96                 return -1;
97         }
98         printf("removed idx1 with data %u\n", data);
99         ret = xpool_remove(&xp, idx2, &data);
100         if (ret != idx2) {
101                 printf("couldn't remove idx2\n");
102                 return 1;
103         }
104         if (data != (xpool_data) 6) {
105                 printf("idx2 returned wrong value (%u)\n", data);
106                 return -1;
107         }
108         printf("removed idx2 with data %u\n", data);
109
110         printf("test succesfull\n");
111 }
112
113 void *xpool_func(void *arg)
114 {
115         int id = (int) arg;
116         xpool_index idx, ret;
117         xpool_data data;
118         uint64_t i ;
119         struct timeval start, end;
120
121         gettimeofday(&start, NULL);
122         for (i = 0; i < loops; i++) {
123                 idx = xpool_add(&xp, (xpool_data) id);
124                 if (idx == NoSerial) {
125                         printf("couldn't add idx\n");
126                         return NULL;
127                 }
128                 /*
129                 ret = xpool_peek_idx(&xp, idx, &data);
130                 if (ret != idx) {
131                         printf("couldn't peek\n");
132                         return NULL;
133                 }
134                 if (data != (xpool_data) id){
135                         printf("peekidx returned wrong value %u instead of %u\n", 
136                                         data, (xpool_data) id);
137                         return NULL;
138                 }
139                 ret = xpool_peek_and_fwd(&xp, &data);
140                 if (ret == NoSerial) {
141                         printf("couldn't peek and fwd\n");
142                         return NULL;
143                 }
144                 */
145                 ret = xpool_remove(&xp, idx, &data);
146 //              if (ret != idx) {
147 //                      printf("couldn't remove idx: %llu\n", idx);
148 //                      return NULL;
149 //              }
150 //              if (data != (xpool_data) id){
151 //                      printf("remove returned wrong value %u instead of %u\n", 
152 //                                      data, (xpool_data) id);
153 //                      return NULL;
154 //              }
155         }
156         gettimeofday(&end, NULL);
157         timersub(&end, &start, &end);
158         printf("%d: test succesfull (%llu loops in %u.%us)\n", id, loops, end.tv_sec, end.tv_usec);
159
160         return NULL;
161 }
162
163 void *xq_func(void *arg)
164 {
165         int id = (int) arg;
166         xqindex idx, ret;
167         uint64_t i ;
168         struct timeval start, end;
169
170         gettimeofday(&start, NULL);
171         for (i = 0; i < loops; i++) {
172                 
173                 idx = xq_append_tail(&xq, (xqindex) id);
174                 if (idx == NoSerial) {
175                         printf("couldn't add idx\n");
176                         return NULL;
177                 }
178                 /*
179                 ret = xpool_peek_idx(&xp, idx, &data);
180                 if (ret != idx) {
181                         printf("couldn't peek\n");
182                         return NULL;
183                 }
184                 if (data != (xpool_data) id){
185                         printf("peekidx returned wrong value %u instead of %u\n", 
186                                         data, (xpool_data) id);
187                         return NULL;
188                 }
189                 ret = xpool_peek_and_fwd(&xp, &data);
190                 if (ret == NoSerial) {
191                         printf("couldn't peek and fwd\n");
192                         return NULL;
193                 }
194                 */
195                 ret = xq_pop_head(&xq);
196                 //if (ret != idx) {
197                 //      printf("couldn't remove idx: %llu\n", idx);
198                 //      return NULL;
199                 //}
200                 //if (ret != (xqindex) id){
201                 //      printf("remove returned wrong value %u instead of %u\n", 
202                 //                      ret, (xqindex) id);
203                 //      return NULL;
204                 //}
205         }
206         gettimeofday(&end, NULL);
207         timersub(&end, &start, &end);
208         printf("%d: test succesfull (%llu loops in %u.%us)\n", id, loops, end.tv_sec, end.tv_usec);
209
210         return NULL;
211 }
212
213
214 int main(int argc, const char *argv[])
215 {
216
217         size = atoi(argv[1]);
218         loops = atoi(argv[2]);
219
220         struct xpool_node* mem = malloc(sizeof(struct xpool_node) * size);
221         xqindex* xqmem = malloc(sizeof(xqindex) * size);
222         pthread_t *thread, tid;
223         void *ret;
224         int i;
225
226         thread = malloc(sizeof(pthread_t)*size);
227
228         printf("Testing xpool\n");
229         xpool_init(&xp, size, mem);
230         for (i = 0; i < size; i++) {
231                 pthread_create(thread+i, NULL, xpool_func, (void *) i);
232         }
233
234         for (i = 0; i < size; i++) {
235                 pthread_join(thread[i], &ret);
236         }
237         
238         printf("Testing xq\n");
239         xq_init_empty(&xq, size, xqmem);
240         for (i = 0; i < size; i++) {
241                 pthread_create(thread+i, NULL, xq_func, (void *) i);
242         }
243
244         for (i = 0; i < size; i++) {
245                 pthread_join(thread[i], &ret);
246         }
247
248
249         // and again
250
251         return 0;
252 }