Logo AND Algorithmique Numérique Distribuée

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