Logo AND Algorithmique Numérique Distribuée

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