Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixed licence and copyright. No more reference to da GRAS possee or the
[simgrid.git] / testsuite / xbt / dynar_int.c
1 /* $Id$ */
2
3 /* dynar_int: A test case for the dynar using integers as content           */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include <stdio.h>
11 #include <gras.h>
12
13 #define NB_ELEM 5000
14 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
15
16 int main(int argc,char *argv[]) {
17    xbt_dynar_t d;
18    xbt_error_t errcode;
19    int i,cpt,cursor;
20    int *iptr;
21    
22    xbt_init_defaultlog(&argc,argv,"dynar.thresh=debug");
23
24    INFO0("==== Traverse the empty dynar");
25    d=xbt_dynar_new(sizeof(int),NULL);
26    xbt_dynar_foreach(d,cursor,i){
27      xbt_assert0(0,"Damnit, there is something in the empty dynar");
28    }
29    xbt_dynar_free(&d);
30    xbt_dynar_free(&d);
31
32    INFO1("==== Push %d int, set them again 3 times, traverse them, shift them",
33         NB_ELEM);
34    d=xbt_dynar_new(sizeof(int),NULL);
35    for (cpt=0; cpt< NB_ELEM; cpt++) {
36      xbt_dynar_push_as(d,int,cpt);
37      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
38    }
39    for (cursor=0; cursor< NB_ELEM; cursor++) {
40      iptr=xbt_dynar_get_ptr(d,cursor);
41      xbt_assert2(cursor == *iptr,
42                   "The retrieved value is not the same than the injected one (%d!=%d)",
43                   cursor,cpt);
44    }
45    xbt_dynar_foreach(d,cursor,cpt){
46      xbt_assert2(cursor == cpt,
47                   "The retrieved value is not the same than the injected one (%d!=%d)",
48                   cursor,cpt);
49    }
50    for (cpt=0; cpt< NB_ELEM; cpt++)
51      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
52
53    for (cpt=0; cpt< NB_ELEM; cpt++) 
54      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
55 /*     xbt_dynar_set(d,cpt,&cpt);*/
56    
57    for (cpt=0; cpt< NB_ELEM; cpt++) 
58      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
59    
60    cpt=0;
61    xbt_dynar_foreach(d,cursor,i){
62      xbt_assert2(i == cpt,
63           "The retrieved value is not the same than the injected one (%d!=%d)",
64                   i,cpt);
65      cpt++;
66    }
67    xbt_assert2(cpt == NB_ELEM,
68                 "Cannot retrieve my %d values. Last got one is %d",
69                 NB_ELEM, cpt);
70
71    for (cpt=0; cpt< NB_ELEM; cpt++) {
72      xbt_dynar_shift(d,&i);
73      xbt_assert2(i == cpt,
74            "The retrieved value is not the same than the injected one (%d!=%d)",
75                i,cpt);
76      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
77    }
78    xbt_dynar_free(&d);
79    xbt_dynar_free(&d);
80
81    
82    INFO1("==== Unshift/pop %d int",NB_ELEM);
83    d=xbt_dynar_new(sizeof(int),NULL);
84    for (cpt=0; cpt< NB_ELEM; cpt++) {
85      xbt_dynar_unshift(d,&cpt);
86      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
87    }
88    for (cpt=0; cpt< NB_ELEM; cpt++) {
89      i=xbt_dynar_pop_as(d,int);
90      xbt_assert2(i == cpt,
91            "The retrieved value is not the same than the injected one (%d!=%d)",
92                  i,cpt);
93      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
94    }
95    xbt_dynar_free(&d);
96    xbt_dynar_free(&d);
97
98    
99    INFO1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
100    d=xbt_dynar_new(sizeof(int),NULL);
101    for (cpt=0; cpt< NB_ELEM; cpt++) {
102      xbt_dynar_push_as(d,int,cpt);
103      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
104    }
105    for (cpt=0; cpt< 1000; cpt++) {
106      xbt_dynar_insert_at_as(d,2500,int,cpt);
107      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
108    }
109
110    for (cpt=0; cpt< 2500; cpt++) {
111      xbt_dynar_shift(d,&i);
112      xbt_assert2(i == cpt,
113            "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
114                i,cpt);
115      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
116    }
117    for (cpt=999; cpt>=0; cpt--) {
118      xbt_dynar_shift(d,&i);
119      xbt_assert2(i == cpt,
120            "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
121                i,cpt);
122    }
123    for (cpt=2500; cpt< NB_ELEM; cpt++) {
124      xbt_dynar_shift(d,&i);
125       xbt_assert2(i == cpt,
126            "The retrieved value is not the same than the injected one at the end (%d!=%d)",
127                i,cpt);
128    }
129    xbt_dynar_free(&d);
130    xbt_dynar_free(&d);
131
132
133    INFO1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
134    d=xbt_dynar_new(sizeof(int),NULL);
135    for (cpt=0; cpt< NB_ELEM; cpt++) 
136      xbt_dynar_push_as(d,int,cpt);
137    
138    for (cpt=2000; cpt< 4000; cpt++) {
139      xbt_dynar_remove_at(d,2000,&i);
140      xbt_assert2(i == cpt,
141                   "Remove a bad value. Got %d, expected %d",
142                   i,cpt);
143      DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));
144    }
145    xbt_dynar_free(&d);
146    xbt_dynar_free(&d);
147
148    xbt_exit();
149    return 0;
150 }