Logo AND Algorithmique Numérique Distribuée

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