Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modifying the API so as to prevent a use of the context that would make valgrind...
[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 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include <stdio.h> /* sprintf */
11 #include "gras.h"
12
13 /* NB_ELEM HAS to be a multiple of 5 */
14 #define NB_ELEM 5000
15 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
16
17 void free_string(void *d);
18
19 void free_string(void *d){
20   free(*(void**)d);
21 }
22
23 int main(int argc,char *argv[]) {
24    xbt_dynar_t d;
25    xbt_error_t errcode;
26    int cpt;
27    char buf[1024];
28    char *s1,*s2;
29    
30    xbt_init_defaultlog(&argc,argv,"dynar.thresh=debug");
31    
32    INFO0("==== Traverse the empty dynar");
33    d=xbt_dynar_new(sizeof(char *),&free_string);
34    xbt_dynar_foreach(d,cpt,s1){
35      xbt_assert0(FALSE,
36                   "Damnit, there is something in the empty dynar");
37    }
38    xbt_dynar_free(&d);
39    xbt_dynar_free(&d);
40
41    INFO1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
42    d=xbt_dynar_new(sizeof(char*),&free_string);
43    for (cpt=0; cpt< NB_ELEM; cpt++) {
44      sprintf(buf,"%d",cpt);
45      s1=strdup(buf);
46      xbt_dynar_push(d,&s1);
47    }
48    for (cpt=0; cpt< NB_ELEM; cpt++) {
49      sprintf(buf,"%d",cpt);
50      s1=strdup(buf);
51      xbt_dynar_replace(d,cpt,&s1);
52    }
53    for (cpt=0; cpt< NB_ELEM; cpt++) {
54      sprintf(buf,"%d",cpt);
55      s1=strdup(buf);
56      xbt_dynar_replace(d,cpt,&s1);
57    }
58    for (cpt=0; cpt< NB_ELEM; cpt++) {
59      sprintf(buf,"%d",cpt);
60      s1=strdup(buf);
61      xbt_dynar_replace(d,cpt,&s1);
62    }
63    for (cpt=0; cpt< NB_ELEM; cpt++) {
64      sprintf(buf,"%d",cpt);
65      xbt_dynar_shift(d,&s2);
66      xbt_assert2 (!strcmp(buf,s2),
67             "The retrieved value is not the same than the injected one (%s!=%s)",
68                    buf,s2);
69      free(s2);
70    }
71    xbt_dynar_free(&d);
72    xbt_dynar_free(&d);
73
74
75    INFO1("==== Unshift, traverse and pop %d strings",NB_ELEM);
76    d=xbt_dynar_new(sizeof(char**),&free_string);
77    for (cpt=0; cpt< NB_ELEM; cpt++) {
78      sprintf(buf,"%d",cpt);
79      s1=strdup(buf);
80      xbt_dynar_unshift(d,&s1);
81    }
82    xbt_dynar_foreach(d,cpt,s1) {
83      sprintf(buf,"%d",NB_ELEM - cpt -1);
84      xbt_assert2 (!strcmp(buf,s1),
85            "The retrieved value is not the same than the injected one (%s!=%s)",
86                buf,s1);
87    }
88    for (cpt=0; cpt< NB_ELEM; cpt++) {
89      sprintf(buf,"%d",cpt);
90      xbt_dynar_pop(d,&s2);
91      xbt_assert2 (!strcmp(buf,s2),
92            "The retrieved value is not the same than the injected one (%s!=%s)",
93                buf,s2);
94      free(s2);
95    }
96    xbt_dynar_free(&d);
97    xbt_dynar_free(&d);
98
99
100    INFO2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
101    d=xbt_dynar_new(sizeof(char*),&free_string);
102    for (cpt=0; cpt< NB_ELEM; cpt++) {
103      sprintf(buf,"%d",cpt);
104      s1=strdup(buf);
105      xbt_dynar_push(d,&s1);
106    }
107    for (cpt=0; cpt< NB_ELEM/5; cpt++) {
108      sprintf(buf,"%d",cpt);
109      s1=strdup(buf);
110      xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
111    }
112
113    for (cpt=0; cpt< NB_ELEM/2; cpt++) {
114      sprintf(buf,"%d",cpt);
115      xbt_dynar_shift(d,&s2);
116      xbt_assert2(!strcmp(buf,s2),
117            "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
118                buf,s2);
119       free(s2);
120    }
121    for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
122      sprintf(buf,"%d",cpt);
123      xbt_dynar_shift(d,&s2);
124      xbt_assert2 (!strcmp(buf,s2),
125            "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
126                buf,s2);
127      free(s2);
128    }
129    for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
130      sprintf(buf,"%d",cpt);
131      xbt_dynar_shift(d,&s2);
132      xbt_assert2 (!strcmp(buf,s2),
133            "The retrieved value is not the same than the injected one at the end (%s!=%s)",
134                buf,s2);
135      free(s2);
136    }
137    xbt_dynar_free(&d);
138    xbt_dynar_free(&d);
139
140
141    INFO3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
142    d=xbt_dynar_new(sizeof(char*),&free_string);
143    for (cpt=0; cpt< NB_ELEM; cpt++) {
144      sprintf(buf,"%d",cpt);
145      s1=strdup(buf);
146      xbt_dynar_push(d,&s1);
147    }
148    for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
149      sprintf(buf,"%d",cpt);
150      xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
151      xbt_assert2(!strcmp(buf,s2),
152                   "Remove a bad value. Got %s, expected %s",
153                   s2,buf);
154       free(s2);
155    }
156    xbt_dynar_free(&d);
157    xbt_dynar_free(&d);
158
159    xbt_exit();
160    return 0;
161 }