Hosted by the courtesy of  
 GitHub 
The stars ASAP english francais spanish arab
Durée du voyage intersidéral francais
Résolutions de l'ONU en HTML francais
Bussard Ramjet english francais
DWARF : dwarf2xml english
ELF : libelf examples english
Code presentation : ctoohtml english

To rings Doc++

File Index

All Tags

Tags by File

Tags referrers

file: ring_test.c


  1 /*
  2 * Copyright (C) 2008-2009 by Emmanuel Azencot under the GNU GPL
  3 * license version 2.0 or 2.1.  You should have received a copy of the
  4 * GPL license along with this library if you did not you can find it
  5 * at http://www.gnu.org/.
  6 */

  7 /*
  8 * Azencot : Sat Dec 13 00:20:02 CET 2008
  9 *  Creation
 10 */

 11
 12 /*
 13 gcc -g -I. -I.. -I../excp -o ring_test ../ring.c ring_test.c
 14 */

 15 #include <stdio.h>
 16 #include <errno.h>
 17 #include <assert.h>
 18
 19 #include "ring.h"
 20
 21 int /*X*/ f_ring_test(void) {
 22   int i, j;
 23   struct s_ring_test {
 24      unsigned long stuff;
 25      struct s_ring link;
 26      int no;
 27      unsigned long end;
 28   } elem[4], *node = 0, *list = 0, out;
 29
 30   assert ( (list = m_ring_link(list, link, &elem[0])) );
 31   assert ( !(list = m_ring_link(list, link, &elem[0])) && errno == EEXIST); errno = 0;
 32
 33   list = 0;
 34   for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
 35      assert(list = m_ring_link(list, link, &elem[i]) );
 36      assert( m_ring_selftest(list, link) );
 37   }
 38   
 39   for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
 40      assert( m_ring_is_in(&elem[i], list, link) );
 41      assert( m_ring_is_in(m_ring_next(&elem[i], link), list, link) );
 42   }
 43   j = 0;
 44   m_ring_do(list, node) {
 45      for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
 46         if ( &elem[i] == node ) break;
 47      }
 48      assert( i < sizeof(elem)/sizeof(elem[0]) );
 49      ++j;
 50   } m_ring_done(list, node, link);
 51   assert( j == sizeof(elem)/sizeof(elem[0]) );
 52
 53   j = 0;
 54   for ( node = list; node; node = m_ring_list(list, node, link) ) {
 55      for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
 56         if ( &elem[i] == node ) break;
 57      }
 58      assert( i < sizeof(elem)/sizeof(elem[0]) );
 59      ++j;
 60   }
 61   assert( j == sizeof(elem)/sizeof(elem[0]) );
 62
 63   assert( !m_ring_is_in(&out, list, link) );
 64   assert( !m_ring_is_in(&out, (struct s_ring_test *)0, link) && errno == 0); errno = 0;
 65   assert( !m_ring_is_in((struct s_ring_test *)0, list, link) && errno == EFAULT); errno = 0;
 66
 67   assert( (list = m_ring_unlink(&elem[3], link)) && errno == 0);
 68   assert ( m_ring_is_in(&elem[0], list, link) );
 69   assert ( m_ring_is_in(&elem[1], list, link) );
 70   assert ( m_ring_is_in(&elem[2], list, link) );
 71   assert (!m_ring_is_in(&elem[3], list, link) );
 72   assert( (list = m_ring_unlink(&elem[1], link)) && errno == 0);
 73   assert ( m_ring_is_in(&elem[0], list, link) );
 74   assert (!m_ring_is_in(&elem[1], list, link) );
 75   assert ( m_ring_is_in(&elem[2], list, link) );
 76   assert (!m_ring_is_in(&elem[3], list, link) );
 77   assert (list = m_ring_link(list, link, &elem[3]) );
 78   assert ( m_ring_is_in(&elem[0], list, link) );
 79   assert (!m_ring_is_in(&elem[1], list, link) );
 80   assert ( m_ring_is_in(&elem[2], list, link) );
 81   assert ( m_ring_is_in(&elem[3], list, link) );
 82   assert( (list = m_ring_unlink(&elem[0], link)) && errno == 0);
 83   assert (!m_ring_is_in(&elem[0], list, link) );
 84   assert (!m_ring_is_in(&elem[1], list, link) );
 85   assert ( m_ring_is_in(&elem[3], list, link) );
 86   assert ( m_ring_is_in(&elem[2], list, link) );
 87   assert( (list = m_ring_unlink(&elem[2], link)) && errno == 0);
 88   assert (!m_ring_is_in(&elem[0], list, link) );
 89   assert (!m_ring_is_in(&elem[1], list, link) );
 90   assert (!m_ring_is_in(&elem[2], list, link) );
 91   assert ( m_ring_is_in(&elem[3], list, link) );
 92   assert( !(list = m_ring_unlink(&elem[3], link)) && errno == 0);
 93   assert (!m_ring_is_in(&elem[0], list, link) );
 94   assert (!m_ring_is_in(&elem[1], list, link) );
 95   assert (!m_ring_is_in(&elem[2], list, link) );
 96   assert (!m_ring_is_in(&elem[3], list, link) );
 97 }
 98 int /*X*/ f_nring_test(void) {
 99   int i, j;
100   struct s_nring_test {
101      unsigned long stuff;
102      struct s_nring link;
103      int no;
104      unsigned long end;
105   } elem[4], *node = 0, *list = 0, out;
106   char *names[4] = { "node 0",  "node 1", "node 2", "node 3", };
107
108   elem[0].link.name = names[0];
109   assert ( (list = m_nring_link(list, link, &elem[0])) );
110   elem[1].link.name = names[0];
111   assert ( !(list = m_nring_link(list, link, &elem[1])) && errno == ENOTUNIQ); errno = 0;
112
113   list = 0;
114   assert ( (list = m_nring_link(list, link, &elem[0])) );
115   assert ( !(list = m_nring_link(list, link, &elem[0])) && errno == EEXIST); errno = 0;
116
117   list = 0;
118   for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
119      elem[i].link.name = names[i];
120   }
121      
122   for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
123      assert(list = m_nring_link(list, link, &elem[i]) );
124      assert( m_nring_selftest(list, link) );
125   }
126   for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
127      assert( m_nring_is_in(&elem[i], list, link) );
128      assert( m_nring_is_in(m_nring_next(&elem[i], link), list, link) );
129      assert( m_nring_find(list, link, names[i]) == &elem[i] );
130   }
131   j = 0;
132   m_nring_do(list, node) {
133      for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
134         if ( &elem[i] == node ) break;
135      }
136      assert( i < sizeof(elem)/sizeof(elem[0]) );
137      ++j;
138   } m_nring_done(list, node, link);
139   assert( j == sizeof(elem)/sizeof(elem[0]) );
140
141   j = 0;
142   for ( node = list; node; node = m_nring_list(list, node, link) ) {
143      for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
144         if ( &elem[i] == node ) break;
145      }
146      assert( i < sizeof(elem)/sizeof(elem[0]) );
147      ++j;
148   }
149   assert( j == sizeof(elem)/sizeof(elem[0]) );
150
151   assert( !m_nring_is_in(&out, list, link) );
152   assert( !m_nring_is_in(&out, (struct s_nring_test *)0, link) && errno == 0); errno = 0;
153   assert( !m_nring_is_in((struct s_nring_test *)0, list, link) && errno == EFAULT); errno = 0;
154
155   assert( (list = m_nring_unlink(&elem[3], link)) && errno == 0);
156   assert ( m_nring_is_in(&elem[0], list, link) );
157   assert ( m_nring_is_in(&elem[1], list, link) );
158   assert ( m_nring_is_in(&elem[2], list, link) );
159   assert (!m_nring_is_in(&elem[3], list, link) );
160   assert( (list = m_nring_unlink(&elem[1], link)) && errno == 0);
161   assert ( m_nring_is_in(&elem[0], list, link) );
162   assert (!m_nring_is_in(&elem[1], list, link) );
163   assert ( m_nring_is_in(&elem[2], list, link) );
164   assert (!m_nring_is_in(&elem[3], list, link) );
165   assert (list = m_nring_link(list, link, &elem[3]) );
166   assert ( m_nring_is_in(&elem[0], list, link) );
167   assert (!m_nring_is_in(&elem[1], list, link) );
168   assert ( m_nring_is_in(&elem[2], list, link) );
169   assert ( m_nring_is_in(&elem[3], list, link) );
170   assert( (list = m_nring_unlink(&elem[0], link)) && errno == 0);
171   assert (!m_nring_is_in(&elem[0], list, link) );
172   assert (!m_nring_is_in(&elem[1], list, link) );
173   assert ( m_nring_is_in(&elem[3], list, link) );
174   assert ( m_nring_is_in(&elem[2], list, link) );
175   assert( (list = m_nring_unlink(&elem[2], link)) && errno == 0);
176   assert (!m_nring_is_in(&elem[0], list, link) );
177   assert (!m_nring_is_in(&elem[1], list, link) );
178   assert (!m_nring_is_in(&elem[2], list, link) );
179   assert ( m_nring_is_in(&elem[3], list, link) );
180   assert( !(list = m_nring_unlink(&elem[3], link)) && errno == 0);
181   assert (!m_nring_is_in(&elem[0], list, link) );
182   assert (!m_nring_is_in(&elem[1], list, link) );
183   assert (!m_nring_is_in(&elem[2], list, link) );
184   assert (!m_nring_is_in(&elem[3], list, link) );
185
186 }
187 int /*X*/ f_dring_test(void) {
188   int i, j;
189   struct s_dring_test {
190      unsigned long stuff;
191      struct s_dring link;
192      int no;
193      unsigned long end;
194   } elem[4], *node = 0, *list = 0, out;
195
196   assert ( (list = m_dring_link(list, link, &elem[0])) );
197   assert ( !(list = m_dring_link(list, link, &elem[0])) && errno == EEXIST); errno = 0;
198
199   list = 0;
200   for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
201      assert(list = m_dring_link(list, link, &elem[i]) );
202      assert( m_dring_selftest(list, link) );
203   }
204   
205   for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
206      assert( m_dring_is_in(&elem[i], list, link) );
207      assert( m_dring_is_in(m_dring_next(&elem[i], link), list, link) );
208      assert( m_dring_is_in(m_dring_prev(&elem[i], link), list, link) );
209      assert( m_dring_is_in(m_dring_move(&elem[i], 3, 1, link), list, link) );
210      assert( m_dring_is_in(m_dring_move(&elem[i],-3, 1, link), list, link) );
211      assert( m_dring_is_in(m_dring_move(&elem[i],-3, 1, link), list, link) );
212      assert(!m_dring_is_in(m_dring_move(&elem[i],-4, 1, link), list, link) && errno == ELOOP); errno = 0;
213      assert(!m_dring_is_in(m_dring_move(&elem[i], 4, 1, link), list, link) && errno == ELOOP); errno = 0;
214
215 //printf("node : %08x, move -1 : %08x, prev : %08x, next : %08x\n", &elem[i], m_dring_move(&elem[i],-1, 1, link), m_dring_prev(&elem[i], link), m_dring_next(&elem[i], link));
216 //fflush(stdout);
217      assert( m_dring_move(&elem[i], 4, 0, link) == &elem[i]);
218      assert( m_dring_move(&elem[i],-4, 0, link) == &elem[i]);
219      assert( m_dring_move(&elem[i], 0, 1, link) == &elem[i] );
220      assert( m_dring_move(&elem[i],-1, 1, link) == m_dring_prev(&elem[i], link) );
221      assert( m_dring_move(&elem[i], 1, 1, link) == m_dring_next(&elem[i], link) );
222      assert( m_dring_prev(m_dring_next(&elem[i], link), link) == &elem[i] );
223      assert( m_dring_next(m_dring_prev(&elem[i], link), link) == &elem[i] );
224   }
225   j = 0;
226   m_dring_do(list, node) {
227      for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
228         if ( &elem[i] == node ) break;
229      }
230      assert( i < sizeof(elem)/sizeof(elem[0]) );
231      ++j;
232   } m_dring_done(list, node, link);
233   assert( j == sizeof(elem)/sizeof(elem[0]) );
234
235   j = 0;
236   for ( node = list; node; node = m_dring_list(list, node, link) ) {
237      for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
238         if ( &elem[i] == node ) break;
239      }
240      assert( i < sizeof(elem)/sizeof(elem[0]) );
241      ++j;
242   }
243   assert( j == sizeof(elem)/sizeof(elem[0]) );
244
245   assert( !m_dring_is_in(&out, list, link) );
246   assert( !m_dring_is_in(&out, (struct s_dring_test *)0, link) && errno == 0); errno = 0;
247   assert( !m_dring_is_in((struct s_dring_test *)0, list, link) && errno == EFAULT); errno = 0;
248
249   assert( (list = m_dring_unlink(&elem[3], link)) && errno == 0);
250   assert ( m_dring_is_in(&elem[0], list, link) );
251   assert ( m_dring_is_in(&elem[1], list, link) );
252   assert ( m_dring_is_in(&elem[2], list, link) );
253   assert (!m_dring_is_in(&elem[3], list, link) );
254   assert( (list = m_dring_unlink(&elem[1], link)) && errno == 0);
255   assert ( m_dring_is_in(&elem[0], list, link) );
256   assert (!m_dring_is_in(&elem[1], list, link) );
257   assert ( m_dring_is_in(&elem[2], list, link) );
258   assert (!m_dring_is_in(&elem[3], list, link) );
259   assert (list = m_dring_link(list, link, &elem[3]) );
260   assert ( m_dring_is_in(&elem[0], list, link) );
261   assert (!m_dring_is_in(&elem[1], list, link) );
262   assert ( m_dring_is_in(&elem[2], list, link) );
263   assert ( m_dring_is_in(&elem[3], list, link) );
264   assert( (list = m_dring_unlink(&elem[0], link)) && errno == 0);
265   assert (!m_dring_is_in(&elem[0], list, link) );
266   assert (!m_dring_is_in(&elem[1], list, link) );
267   assert ( m_dring_is_in(&elem[3], list, link) );
268   assert ( m_dring_is_in(&elem[2], list, link) );
269   assert( (list = m_dring_unlink(&elem[2], link)) && errno == 0);
270   assert (!m_dring_is_in(&elem[0], list, link) );
271   assert (!m_dring_is_in(&elem[1], list, link) );
272   assert (!m_dring_is_in(&elem[2], list, link) );
273   assert ( m_dring_is_in(&elem[3], list, link) );
274   assert( !(list = m_dring_unlink(&elem[3], link)) && errno == 0);
275   assert (!m_dring_is_in(&elem[0], list, link) );
276   assert (!m_dring_is_in(&elem[1], list, link) );
277   assert (!m_dring_is_in(&elem[2], list, link) );
278   assert (!m_dring_is_in(&elem[3], list, link) );
279 }
280 int /*X*/ f_ndring_test(void) {
281   int i, j;
282   struct s_ndring_test {
283      unsigned long stuff;
284      struct s_ndring link;
285      int no;
286      unsigned long end;
287   } elem[4], *node = 0, *list = 0, out;
288   char *names[4] = { "node 0",  "node 1", "node 2", "node 3", };
289
290   elem[0].link.name = names[0];
291   assert ( (list = m_ndring_link(list, link, &elem[0])) );
292   elem[1].link.name = names[0];
293   assert ( !(list = m_ndring_link(list, link, &elem[1])) && errno == ENOTUNIQ); errno = 0;
294
295   list = 0;
296   assert ( (list = m_ndring_link(list, link, &elem[0])) );
297   assert ( !(list = m_ndring_link(list, link, &elem[0])) && errno == EEXIST); errno = 0;
298
299   list = 0;
300   for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
301      elem[i].link.name = names[i];
302   }
303      
304   assert ( (list = m_ndring_link(list, link, &elem[0])) );
305   assert ( !(list = m_ndring_link(list, link, &elem[0])) && errno == EEXIST); errno = 0;
306
307   list = 0;
308   for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
309      assert(list = m_ndring_link(list, link, &elem[i]) );
310      assert( m_ndring_selftest(list, link) );
311   }
312   
313   for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
314      assert( m_ndring_is_in(&elem[i], list, link) );
315      assert( m_ndring_find(list, link, names[i]) == &elem[i] );
316      assert( m_ndring_is_in(m_ndring_next(&elem[i], link), list, link) );
317      assert( m_ndring_is_in(m_ndring_prev(&elem[i], link), list, link) );
318      assert( m_ndring_is_in(m_ndring_move(&elem[i], 3, 1, link), list, link) );
319      assert( m_ndring_is_in(m_ndring_move(&elem[i],-3, 1, link), list, link) );
320      assert( m_ndring_is_in(m_ndring_move(&elem[i],-3, 1, link), list, link) );
321      assert(!m_ndring_is_in(m_ndring_move(&elem[i],-4, 1, link), list, link) && errno == ELOOP); errno = 0;
322      assert(!m_ndring_is_in(m_ndring_move(&elem[i], 4, 1, link), list, link) && errno == ELOOP); errno = 0;
323
324 //printf("node : %08x, move -1 : %08x, prev : %08x, next : %08x\n", &elem[i], m_ndring_move(&elem[i],-1, 1, link), m_ndring_prev(&elem[i], link), m_ndring_next(&elem[i], link));
325 //fflush(stdout);
326      assert( m_ndring_move(&elem[i], 4, 0, link) == &elem[i]);
327      assert( m_ndring_move(&elem[i],-4, 0, link) == &elem[i]);
328      assert( m_ndring_move(&elem[i], 0, 1, link) == &elem[i] );
329      assert( m_ndring_move(&elem[i],-1, 1, link) == m_ndring_prev(&elem[i], link) );
330      assert( m_ndring_move(&elem[i], 1, 1, link) == m_ndring_next(&elem[i], link) );
331      assert( m_ndring_prev(m_ndring_next(&elem[i], link), link) == &elem[i] );
332      assert( m_ndring_next(m_ndring_prev(&elem[i], link), link) == &elem[i] );
333   }
334   j = 0;
335   m_ndring_do(list, node) {
336      for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
337         if ( &elem[i] == node ) break;
338      }
339      assert( i < sizeof(elem)/sizeof(elem[0]) );
340      ++j;
341   } m_ndring_done(list, node, link);
342   assert( j == sizeof(elem)/sizeof(elem[0]) );
343
344   j = 0;
345   for ( node = list; node; node = m_ndring_list(list, node, link) ) {
346      for ( i = 0; i < sizeof(elem)/sizeof(elem[0]); i++ ) {
347         if ( &elem[i] == node ) break;
348      }
349      assert( i < sizeof(elem)/sizeof(elem[0]) );
350      ++j;
351   }
352   assert( j == sizeof(elem)/sizeof(elem[0]) );
353
354   assert( !m_ndring_is_in(&out, list, link) );
355   assert( !m_ndring_is_in(&out, (struct s_ndring_test *)0, link) && errno == 0); errno = 0;
356   assert( !m_ndring_is_in((struct s_ndring_test *)0, list, link) && errno == EFAULT); errno = 0;
357
358   assert( (list = m_ndring_unlink(&elem[3], link)) && errno == 0);
359   assert ( m_ndring_is_in(&elem[0], list, link) );
360   assert ( m_ndring_is_in(&elem[1], list, link) );
361   assert ( m_ndring_is_in(&elem[2], list, link) );
362   assert (!m_ndring_is_in(&elem[3], list, link) );
363   assert( (list = m_ndring_unlink(&elem[1], link)) && errno == 0);
364   assert ( m_ndring_is_in(&elem[0], list, link) );
365   assert (!m_ndring_is_in(&elem[1], list, link) );
366   assert ( m_ndring_is_in(&elem[2], list, link) );
367   assert (!m_ndring_is_in(&elem[3], list, link) );
368   assert (list = m_ndring_link(list, link, &elem[3]) );
369   assert ( m_ndring_is_in(&elem[0], list, link) );
370   assert (!m_ndring_is_in(&elem[1], list, link) );
371   assert ( m_ndring_is_in(&elem[2], list, link) );
372   assert ( m_ndring_is_in(&elem[3], list, link) );
373   assert( (list = m_ndring_unlink(&elem[0], link)) && errno == 0);
374   assert (!m_ndring_is_in(&elem[0], list, link) );
375   assert (!m_ndring_is_in(&elem[1], list, link) );
376   assert ( m_ndring_is_in(&elem[3], list, link) );
377   assert ( m_ndring_is_in(&elem[2], list, link) );
378   assert( (list = m_ndring_unlink(&elem[2], link)) && errno == 0);
379   assert (!m_ndring_is_in(&elem[0], list, link) );
380   assert (!m_ndring_is_in(&elem[1], list, link) );
381   assert (!m_ndring_is_in(&elem[2], list, link) );
382   assert ( m_ndring_is_in(&elem[3], list, link) );
383   assert( !(list = m_ndring_unlink(&elem[3], link)) && errno == 0);
384   assert (!m_ndring_is_in(&elem[0], list, link) );
385   assert (!m_ndring_is_in(&elem[1], list, link) );
386   assert (!m_ndring_is_in(&elem[2], list, link) );
387   assert (!m_ndring_is_in(&elem[3], list, link) );
388 }
389
390 int /*X*/ main(int argc, char *argv[]) {
391   f_ring_check_opt(1);
392   f_ring_test();
393   f_nring_test();
394   f_dring_test();
395   f_ndring_test();
396   return 0;
397 }
398


To rings Doc++

File Index

All Tags

Tags by File

Tags referrers

C to HTML Conversion by ctoohtml

Hosted by the courtesy of  
 GitHub 
The stars ASAP english francais spanish
Durée du voyage intersidéral francais
Résolutions de l'ONU en HTML francais
Bussard Ramjet english francais
DWARF : dwarf2xml english
ELF : libelf examples english
Code presentation : ctoohtml english