Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2004-04-09 Martin Quinson <martin.quinson@tuxfamily.org>
[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 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <stdio.h>
12 #include <gras.h>
13
14 #define NB_ELEM 5000
15 GRAS_LOG_NEW_DEFAULT_CATEGORY(test);
16
17 int main(int argc,char *argv[]) {
18    gras_dynar_t *d;
19    gras_error_t errcode;
20    int i,cpt,cursor;
21    
22    gras_init_defaultlog(argc,argv,"dynar.thresh=debug");
23
24    fprintf(stderr,"==== Traverse the empty dynar\n");
25    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
26    gras_dynar_foreach(d,cursor,i){
27      fprintf(stderr,
28              "Damnit, there is something in the empty dynar\n");
29      abort();
30    }
31    gras_dynar_free(d);
32
33    fprintf(stderr,
34            "==== Push %d int, set them again 3 times, traverse them, shift them\n",
35            NB_ELEM);
36    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
37    for (cpt=0; cpt< NB_ELEM; cpt++) {
38      TRYFAIL(gras_dynar_push(d,&cpt));
39      //fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
40    }
41    for (cursor=0; cursor< NB_ELEM; cursor++) {
42      gras_dynar_get(d,cursor,&cpt);
43      gras_assert2(cursor == cpt,
44                   "The retrieved value is not the same than the injected one (%d!=%d)",
45                   cursor,cpt);
46    }
47    gras_dynar_foreach(d,cursor,cpt){
48      gras_assert2(cursor == cpt,
49                   "The retrieved value is not the same than the injected one (%d!=%d)",
50                   cursor,cpt);
51    }
52    for (cpt=0; cpt< NB_ELEM; cpt++) {
53      TRYFAIL(gras_dynar_set(d,cpt,&cpt));
54    }
55    for (cpt=0; cpt< NB_ELEM; cpt++) {
56      TRYFAIL(gras_dynar_set(d,cpt,&cpt));
57    }
58    for (cpt=0; cpt< NB_ELEM; cpt++) {
59      TRYFAIL(gras_dynar_set(d,cpt,&cpt));
60    }
61    cpt=0;
62    gras_dynar_foreach(d,cursor,i){
63      if (i != cpt) {
64        fprintf(stderr,
65           "The retrieved value is not the same than the injected one (%d!=%d)\n",
66                i,cpt);
67        abort();
68      }
69      cpt++;
70    }
71    if (cpt !=NB_ELEM) {
72      fprintf(stderr,
73              "Cannot retrieve my %d values. Last got one is %d\n",NB_ELEM,
74              cpt);
75        abort();
76    }     
77
78    for (cpt=0; cpt< NB_ELEM; cpt++) {
79      gras_dynar_shift(d,&i);
80      if (i != cpt) {
81        fprintf(stderr,
82            "The retrieved value is not the same than the injected one (%d!=%d)\n",
83                i,cpt);
84        abort();
85      }
86      //     fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
87    }
88    gras_dynar_free(d);
89
90
91    fprintf(stderr,"==== Unshift/pop %d int\n",NB_ELEM);
92    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
93    for (cpt=0; cpt< NB_ELEM; cpt++) {
94      TRYFAIL(gras_dynar_unshift(d,&cpt));
95      //     fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
96    }
97    for (cpt=0; cpt< NB_ELEM; cpt++) {
98      gras_dynar_pop(d,&i);
99      if (i != cpt) {
100        fprintf(stderr,
101            "The retrieved value is not the same than the injected one (%d!=%d)\n",
102                i,cpt);
103        abort();
104      }
105      //     fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
106    }
107    gras_dynar_free(d);
108
109
110
111    fprintf(stderr,"==== Push %d int, insert 1000 int in the middle, shift everything\n",NB_ELEM);
112    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
113    for (cpt=0; cpt< NB_ELEM; cpt++) {
114      TRYFAIL(gras_dynar_push(d,&cpt));
115      //     fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
116    }
117    for (cpt=0; cpt< 1000; cpt++) {
118      TRYFAIL(gras_dynar_insert_at(d,2500,&cpt));
119      //     fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
120    }
121
122    for (cpt=0; cpt< 2500; cpt++) {
123      gras_dynar_shift(d,&i);
124      if (i != cpt) {
125        fprintf(stderr,
126            "The retrieved value is not the same than the injected one at the begining (%d!=%d)\n",
127                i,cpt);
128        abort();
129      }
130      //     fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
131    }
132    for (cpt=999; cpt>=0; cpt--) {
133      gras_dynar_shift(d,&i);
134      if (i != cpt) {
135        fprintf(stderr,
136            "The retrieved value is not the same than the injected one in the middle (%d!=%d)\n",
137                i,cpt);
138        abort();
139      }
140    }
141    for (cpt=2500; cpt< NB_ELEM; cpt++) {
142      gras_dynar_shift(d,&i);
143      if (i != cpt) {
144        fprintf(stderr,
145            "The retrieved value is not the same than the injected one at the end (%d!=%d)\n",
146                i,cpt);
147        abort();
148      }
149    }
150    gras_dynar_free(d);
151
152
153    fprintf(stderr,"==== Push %d int, remove 2000-4000. free the rest\n",NB_ELEM);
154    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
155    for (cpt=0; cpt< NB_ELEM; cpt++) {
156      TRYFAIL(gras_dynar_push(d,&cpt));
157    }
158    for (cpt=2000; cpt< 4000; cpt++) {
159      gras_dynar_remove_at(d,2000,&i);
160      if (i != cpt) {
161        fprintf(stderr,
162            "Remove a bad value. Got %d, expected %d\n",
163                i,cpt);
164        abort();
165      }
166      //     fprintf (stderr,"remove %d, length=%d \n",cpt, gras_dynar_length(d));
167    }
168    gras_dynar_free(d);
169
170    return 0;
171 }