Logo AND Algorithmique Numérique Distribuée

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