Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Renamed host to cpu. That was really confusing.
[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 XBT_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   free(*(void**)d);
22 }
23
24 int main(int argc,char *argv[]) {
25    xbt_dynar_t d;
26    xbt_error_t errcode;
27    int cpt;
28    char buf[1024];
29    char *s1,*s2;
30    
31    xbt_init_defaultlog(&argc,argv,"dynar.thresh=debug");
32    
33    INFO0("==== Traverse the empty dynar");
34    d=xbt_dynar_new(sizeof(char *),&free_string);
35    xbt_dynar_foreach(d,cpt,s1){
36      xbt_assert0(FALSE,
37                   "Damnit, there is something in the empty dynar");
38    }
39    xbt_dynar_free(&d);
40    xbt_dynar_free(&d);
41
42    INFO1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
43    d=xbt_dynar_new(sizeof(char*),&free_string);
44    for (cpt=0; cpt< NB_ELEM; cpt++) {
45      sprintf(buf,"%d",cpt);
46      s1=strdup(buf);
47      xbt_dynar_push(d,&s1);
48    }
49    for (cpt=0; cpt< NB_ELEM; cpt++) {
50      sprintf(buf,"%d",cpt);
51      s1=strdup(buf);
52      xbt_dynar_replace(d,cpt,&s1);
53    }
54    for (cpt=0; cpt< NB_ELEM; cpt++) {
55      sprintf(buf,"%d",cpt);
56      s1=strdup(buf);
57      xbt_dynar_replace(d,cpt,&s1);
58    }
59    for (cpt=0; cpt< NB_ELEM; cpt++) {
60      sprintf(buf,"%d",cpt);
61      s1=strdup(buf);
62      xbt_dynar_replace(d,cpt,&s1);
63    }
64    for (cpt=0; cpt< NB_ELEM; cpt++) {
65      sprintf(buf,"%d",cpt);
66      xbt_dynar_shift(d,&s2);
67      xbt_assert2 (!strcmp(buf,s2),
68             "The retrieved value is not the same than the injected one (%s!=%s)",
69                    buf,s2);
70      free(s2);
71    }
72    xbt_dynar_free(&d);
73    xbt_dynar_free(&d);
74
75
76    INFO1("==== Unshift, traverse and pop %d strings",NB_ELEM);
77    d=xbt_dynar_new(sizeof(char**),&free_string);
78    for (cpt=0; cpt< NB_ELEM; cpt++) {
79      sprintf(buf,"%d",cpt);
80      s1=strdup(buf);
81      xbt_dynar_unshift(d,&s1);
82    }
83    xbt_dynar_foreach(d,cpt,s1) {
84      sprintf(buf,"%d",NB_ELEM - cpt -1);
85      xbt_assert2 (!strcmp(buf,s1),
86            "The retrieved value is not the same than the injected one (%s!=%s)",
87                buf,s1);
88    }
89    for (cpt=0; cpt< NB_ELEM; cpt++) {
90      sprintf(buf,"%d",cpt);
91      xbt_dynar_pop(d,&s2);
92      xbt_assert2 (!strcmp(buf,s2),
93            "The retrieved value is not the same than the injected one (%s!=%s)",
94                buf,s2);
95      free(s2);
96    }
97    xbt_dynar_free(&d);
98    xbt_dynar_free(&d);
99
100
101    INFO2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
102    d=xbt_dynar_new(sizeof(char*),&free_string);
103    for (cpt=0; cpt< NB_ELEM; cpt++) {
104      sprintf(buf,"%d",cpt);
105      s1=strdup(buf);
106      xbt_dynar_push(d,&s1);
107    }
108    for (cpt=0; cpt< NB_ELEM/5; cpt++) {
109      sprintf(buf,"%d",cpt);
110      s1=strdup(buf);
111      xbt_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      xbt_dynar_shift(d,&s2);
117      xbt_assert2(!strcmp(buf,s2),
118            "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
119                buf,s2);
120       free(s2);
121    }
122    for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
123      sprintf(buf,"%d",cpt);
124      xbt_dynar_shift(d,&s2);
125      xbt_assert2 (!strcmp(buf,s2),
126            "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
127                buf,s2);
128      free(s2);
129    }
130    for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
131      sprintf(buf,"%d",cpt);
132      xbt_dynar_shift(d,&s2);
133      xbt_assert2 (!strcmp(buf,s2),
134            "The retrieved value is not the same than the injected one at the end (%s!=%s)",
135                buf,s2);
136      free(s2);
137    }
138    xbt_dynar_free(&d);
139    xbt_dynar_free(&d);
140
141
142    INFO3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
143    d=xbt_dynar_new(sizeof(char*),&free_string);
144    for (cpt=0; cpt< NB_ELEM; cpt++) {
145      sprintf(buf,"%d",cpt);
146      s1=strdup(buf);
147      xbt_dynar_push(d,&s1);
148    }
149    for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
150      sprintf(buf,"%d",cpt);
151      xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
152      xbt_assert2(!strcmp(buf,s2),
153                   "Remove a bad value. Got %s, expected %s",
154                   s2,buf);
155       free(s2);
156    }
157    xbt_dynar_free(&d);
158    xbt_dynar_free(&d);
159
160    xbt_exit();
161    return 0;
162 }