Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fill the tree before manually removing elements form it + cosmetics
[simgrid.git] / testsuite / xbt / dynar_string.c
1 /* $Id$ */
2
3 /* dynar_string: A test case for the dynar using strings 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 /* NB_ELEM HAS to be a multiple of 5 */
15 #define NB_ELEM 5000
16
17 void free_string(void *d);
18
19 void free_string(void *d){
20   free(*(void**)d);
21 }
22
23 int main(int argc,char *argv[]) {
24    gras_dynar_t *d;
25    gras_error_t errcode;
26    int cpt;
27    char buf[1024];
28    char *s1,*s2;
29    
30    gras_init_defaultlog(&argc,argv,"dynar.thresh=debug");
31    
32    fprintf(stderr,"==== Traverse the empty dynar\n");
33    TRYFAIL(gras_dynar_new(&d,sizeof(char *),&free_string));
34    gras_dynar_foreach(d,cpt,s1){
35      fprintf(stderr,
36              "Damnit, there is something in the empty dynar\n");
37      abort();
38    }
39    gras_dynar_free(d);
40
41    fprintf(stderr,"==== Push %d strings, set them again 3 times, shift them\n",NB_ELEM);
42    TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
43    for (cpt=0; cpt< NB_ELEM; cpt++) {
44      sprintf(buf,"%d",cpt);
45      s1=strdup(buf);
46      TRYFAIL(gras_dynar_push(d,&s1));
47    }
48    for (cpt=0; cpt< NB_ELEM; cpt++) {
49      sprintf(buf,"%d",cpt);
50      s1=strdup(buf);
51      TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
52    }
53    for (cpt=0; cpt< NB_ELEM; cpt++) {
54      sprintf(buf,"%d",cpt);
55      s1=strdup(buf);
56      TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
57    }
58    for (cpt=0; cpt< NB_ELEM; cpt++) {
59      sprintf(buf,"%d",cpt);
60      s1=strdup(buf);
61      TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
62    }
63    for (cpt=0; cpt< NB_ELEM; cpt++) {
64      sprintf(buf,"%d",cpt);
65      gras_dynar_shift(d,&s2);
66      if (strcmp(buf,s2)) {
67        fprintf(stderr,
68             "The retrieved value is not the same than the injected one (%s!=%s)\n",
69                buf,s2);
70        abort();
71      }
72      free(s2);
73    }
74    gras_dynar_free(d);
75
76
77    fprintf(stderr,"==== Unshift, traverse and pop %d strings\n",NB_ELEM);
78    TRYFAIL(gras_dynar_new(&d,sizeof(char**),&free_string));
79    for (cpt=0; cpt< NB_ELEM; cpt++) {
80      sprintf(buf,"%d",cpt);
81      s1=strdup(buf);
82      TRYFAIL(gras_dynar_unshift(d,&s1));
83    }
84    gras_dynar_foreach(d,cpt,s1) {
85      sprintf(buf,"%d",NB_ELEM - cpt -1);
86      if (strcmp(buf,s1)) {
87        fprintf(stderr,
88            "The retrieved value is not the same than the injected one (%s!=%s)\n",
89                buf,s1);
90        abort();
91      }
92    }
93    for (cpt=0; cpt< NB_ELEM; cpt++) {
94      sprintf(buf,"%d",cpt);
95      gras_dynar_pop(d,&s2);
96      if (strcmp(buf,s2)) {
97        fprintf(stderr,
98            "The retrieved value is not the same than the injected one (%s!=%s)\n",
99                buf,s2);
100        abort();
101      }
102      free(s2);
103    }
104    gras_dynar_free(d);
105
106
107
108    fprintf(stderr,"==== Push %d strings, insert %d strings in the middle, shift everything\n",NB_ELEM,NB_ELEM/5);
109    TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
110    for (cpt=0; cpt< NB_ELEM; cpt++) {
111      sprintf(buf,"%d",cpt);
112      s1=strdup(buf);
113      TRYFAIL(gras_dynar_push(d,&s1));
114    }
115    for (cpt=0; cpt< NB_ELEM/5; cpt++) {
116      sprintf(buf,"%d",cpt);
117      s1=strdup(buf);
118      TRYFAIL(gras_dynar_insert_at(d,NB_ELEM/2,&s1));
119    }
120
121    for (cpt=0; cpt< NB_ELEM/2; cpt++) {
122      sprintf(buf,"%d",cpt);
123      gras_dynar_shift(d,&s2);
124      if (strcmp(buf,s2)) {
125        fprintf(stderr,
126            "The retrieved value is not the same than the injected one at the begining (%s!=%s)\n",
127                buf,s2);
128        abort();
129      }
130      free(s2);
131    }
132    for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
133      sprintf(buf,"%d",cpt);
134      gras_dynar_shift(d,&s2);
135      if (strcmp(buf,s2)) {
136        fprintf(stderr,
137            "The retrieved value is not the same than the injected one in the middle (%s!=%s)\n",
138                buf,s2);
139        abort();
140      }
141      free(s2);
142    }
143    for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
144      sprintf(buf,"%d",cpt);
145      gras_dynar_shift(d,&s2);
146      if (strcmp(buf,s2)) {
147        fprintf(stderr,
148            "The retrieved value is not the same than the injected one at the end (%s!=%s)\n",
149                buf,s2);
150        abort();
151      }
152      free(s2);
153    }
154    gras_dynar_free(d);
155
156
157    fprintf(stderr,"==== Push %d strings, remove %d-%d. free the rest\n",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
158    TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
159    for (cpt=0; cpt< NB_ELEM; cpt++) {
160      sprintf(buf,"%d",cpt);
161      s1=strdup(buf);
162      TRYFAIL(gras_dynar_push(d,&s1));
163    }
164    for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
165      sprintf(buf,"%d",cpt);
166      gras_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
167      if (strcmp(buf,s2)) {
168        fprintf(stderr,
169            "Remove a bad value. Got %s, expected %s\n",
170                s2,buf);
171        abort();
172      }
173      free(s2);
174    }
175    gras_dynar_free(d);
176
177    gras_exit();
178    return 0;
179 }