Logo AND Algorithmique Numérique Distribuée

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