Logo AND Algorithmique Numérique Distribuée

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