Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill a calloc
[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 /* doxygen_first_cruft*/
18
19 /* The function we will use to free the data */
20 static 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    /* Populate_str [doxygen cruft] */
45    /* 1. Populate the dynar */
46    for (cpt=0; cpt< NB_ELEM; cpt++) {
47      sprintf(buf,"%d",cpt);
48      s1=strdup(buf);
49      xbt_dynar_push(d,&s1);
50    }
51    for (cpt=0; cpt< NB_ELEM; cpt++) {
52      sprintf(buf,"%d",cpt);
53      s1=strdup(buf);
54      xbt_dynar_replace(d,cpt,&s1);
55    }
56    for (cpt=0; cpt< NB_ELEM; cpt++) {
57      sprintf(buf,"%d",cpt);
58      s1=strdup(buf);
59      xbt_dynar_replace(d,cpt,&s1);
60    }
61    for (cpt=0; cpt< NB_ELEM; cpt++) {
62      sprintf(buf,"%d",cpt);
63      s1=strdup(buf);
64      xbt_dynar_replace(d,cpt,&s1);
65    }
66    for (cpt=0; cpt< NB_ELEM; cpt++) {
67      sprintf(buf,"%d",cpt);
68      xbt_dynar_shift(d,&s2);
69      xbt_assert2 (!strcmp(buf,s2),
70             "The retrieved value is not the same than the injected one (%s!=%s)",
71                    buf,s2);
72      free(s2);
73    }
74    xbt_dynar_free(&d);
75    xbt_dynar_free(&d);
76
77
78    INFO1("==== Unshift, traverse and pop %d strings",NB_ELEM);
79    d=xbt_dynar_new(sizeof(char**),&free_string);
80    for (cpt=0; cpt< NB_ELEM; cpt++) {
81      sprintf(buf,"%d",cpt);
82      s1=strdup(buf);
83      xbt_dynar_unshift(d,&s1);
84    }
85    /* 2. Traverse the dynar with the macro */
86    xbt_dynar_foreach(d,cpt,s1) {
87      sprintf(buf,"%d",NB_ELEM - cpt -1);
88      xbt_assert2 (!strcmp(buf,s1),
89            "The retrieved value is not the same than the injected one (%s!=%s)",
90                buf,s1);
91    }
92    /* 3. Traverse the dynar with the macro */
93    for (cpt=0; cpt< NB_ELEM; cpt++) {
94      sprintf(buf,"%d",cpt);
95      xbt_dynar_pop(d,&s2);
96      xbt_assert2 (!strcmp(buf,s2),
97            "The retrieved value is not the same than the injected one (%s!=%s)",
98                buf,s2);
99      free(s2);
100    }
101    /* 4. Free the resources */
102    xbt_dynar_free(&d);
103    xbt_dynar_free(&d);
104
105
106    INFO2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
107    d=xbt_dynar_new(sizeof(char*),&free_string);
108    for (cpt=0; cpt< NB_ELEM; cpt++) {
109      sprintf(buf,"%d",cpt);
110      s1=strdup(buf);
111      xbt_dynar_push(d,&s1);
112    }
113    for (cpt=0; cpt< NB_ELEM/5; cpt++) {
114      sprintf(buf,"%d",cpt);
115      s1=strdup(buf);
116      xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
117    }
118
119    for (cpt=0; cpt< NB_ELEM/2; cpt++) {
120      sprintf(buf,"%d",cpt);
121      xbt_dynar_shift(d,&s2);
122      xbt_assert2(!strcmp(buf,s2),
123            "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
124                buf,s2);
125       free(s2);
126    }
127    for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
128      sprintf(buf,"%d",cpt);
129      xbt_dynar_shift(d,&s2);
130      xbt_assert2 (!strcmp(buf,s2),
131            "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
132                buf,s2);
133      free(s2);
134    }
135    for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
136      sprintf(buf,"%d",cpt);
137      xbt_dynar_shift(d,&s2);
138      xbt_assert2 (!strcmp(buf,s2),
139            "The retrieved value is not the same than the injected one at the end (%s!=%s)",
140                buf,s2);
141      free(s2);
142    }
143    xbt_dynar_free(&d);
144    xbt_dynar_free(&d);
145
146
147    INFO3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
148    d=xbt_dynar_new(sizeof(char*),&free_string);
149    for (cpt=0; cpt< NB_ELEM; cpt++) {
150      sprintf(buf,"%d",cpt);
151      s1=strdup(buf);
152      xbt_dynar_push(d,&s1);
153    }
154    for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
155      sprintf(buf,"%d",cpt);
156      xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
157      xbt_assert2(!strcmp(buf,s2),
158                   "Remove a bad value. Got %s, expected %s",
159                   s2,buf);
160       free(s2);
161    }
162    xbt_dynar_free(&d);
163    xbt_dynar_free(&d);
164
165    xbt_exit();
166    return 0;
167 }