Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A new data container coupling the facilities of a dynar with the ones of a dict
[simgrid.git] / testsuite / xbt / dynar_int.c
1 /* $Id$ */
2
3 /* dynar_int: A test case for the dynar using integers 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 #define NB_ELEM 5000
15 GRAS_LOG_NEW_DEFAULT_CATEGORY(test);
16
17 void parse_log_opt(int argc, char **argv, const char *def);
18
19 int main(int argc,char *argv[]) {
20    gras_dynar_t *d;
21    gras_error_t errcode;
22    int i,cpt,cursor;
23    
24    parse_log_opt(argc,argv,"dynar.thresh=debug");
25
26    fprintf(stderr,"==== Traverse the empty dynar\n");
27    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
28    gras_dynar_foreach(d,cursor,i){
29      fprintf(stderr,
30              "Damnit, there is something in the empty dynar\n");
31      abort();
32    }
33    gras_dynar_free(d);
34
35    fprintf(stderr,
36            "==== Push %d int, set them again 3 times, traverse them, shift them\n",
37            NB_ELEM);
38    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
39    for (cpt=0; cpt< NB_ELEM; cpt++) {
40      TRYFAIL(gras_dynar_push(d,&cpt));
41      //fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
42    }
43    for (cursor=0; cursor< NB_ELEM; cursor++) {
44      gras_dynar_get(d,cursor,&cpt);
45      gras_assert2(cursor == cpt,
46                   "The retrieved value is not the same than the injected one (%d!=%d)",
47                   cursor,cpt);
48    }
49    gras_dynar_foreach(d,cursor,cpt){
50      gras_assert2(cursor == cpt,
51                   "The retrieved value is not the same than the injected one (%d!=%d)",
52                   cursor,cpt);
53    }
54    for (cpt=0; cpt< NB_ELEM; cpt++) {
55      TRYFAIL(gras_dynar_set(d,cpt,&cpt));
56    }
57    for (cpt=0; cpt< NB_ELEM; cpt++) {
58      TRYFAIL(gras_dynar_set(d,cpt,&cpt));
59    }
60    for (cpt=0; cpt< NB_ELEM; cpt++) {
61      TRYFAIL(gras_dynar_set(d,cpt,&cpt));
62    }
63    cpt=0;
64    gras_dynar_foreach(d,cursor,i){
65      if (i != cpt) {
66        fprintf(stderr,
67           "The retrieved value is not the same than the injected one (%d!=%d)\n",
68                i,cpt);
69        abort();
70      }
71      cpt++;
72    }
73    if (cpt !=NB_ELEM) {
74      fprintf(stderr,
75              "Cannot retrieve my %d values. Last got one is %d\n",NB_ELEM,
76              cpt);
77        abort();
78    }     
79
80    for (cpt=0; cpt< NB_ELEM; cpt++) {
81      gras_dynar_shift(d,&i);
82      if (i != cpt) {
83        fprintf(stderr,
84            "The retrieved value is not the same than the injected one (%d!=%d)\n",
85                i,cpt);
86        abort();
87      }
88      //     fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
89    }
90    gras_dynar_free(d);
91
92
93    fprintf(stderr,"==== Unshift/pop %d int\n",NB_ELEM);
94    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
95    for (cpt=0; cpt< NB_ELEM; cpt++) {
96      TRYFAIL(gras_dynar_unshift(d,&cpt));
97      //     fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
98    }
99    for (cpt=0; cpt< NB_ELEM; cpt++) {
100      gras_dynar_pop(d,&i);
101      if (i != cpt) {
102        fprintf(stderr,
103            "The retrieved value is not the same than the injected one (%d!=%d)\n",
104                i,cpt);
105        abort();
106      }
107      //     fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
108    }
109    gras_dynar_free(d);
110
111
112
113    fprintf(stderr,"==== Push %d int, insert 1000 int in the middle, shift everything\n",NB_ELEM);
114    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
115    for (cpt=0; cpt< NB_ELEM; cpt++) {
116      TRYFAIL(gras_dynar_push(d,&cpt));
117      //     fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
118    }
119    for (cpt=0; cpt< 1000; cpt++) {
120      TRYFAIL(gras_dynar_insert_at(d,2500,&cpt));
121      //     fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
122    }
123
124    for (cpt=0; cpt< 2500; cpt++) {
125      gras_dynar_shift(d,&i);
126      if (i != cpt) {
127        fprintf(stderr,
128            "The retrieved value is not the same than the injected one at the begining (%d!=%d)\n",
129                i,cpt);
130        abort();
131      }
132      //     fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
133    }
134    for (cpt=999; cpt>=0; cpt--) {
135      gras_dynar_shift(d,&i);
136      if (i != cpt) {
137        fprintf(stderr,
138            "The retrieved value is not the same than the injected one in the middle (%d!=%d)\n",
139                i,cpt);
140        abort();
141      }
142    }
143    for (cpt=2500; cpt< NB_ELEM; cpt++) {
144      gras_dynar_shift(d,&i);
145      if (i != cpt) {
146        fprintf(stderr,
147            "The retrieved value is not the same than the injected one at the end (%d!=%d)\n",
148                i,cpt);
149        abort();
150      }
151    }
152    gras_dynar_free(d);
153
154
155    fprintf(stderr,"==== Push %d int, remove 2000-4000. free the rest\n",NB_ELEM);
156    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
157    for (cpt=0; cpt< NB_ELEM; cpt++) {
158      TRYFAIL(gras_dynar_push(d,&cpt));
159    }
160    for (cpt=2000; cpt< 4000; cpt++) {
161      gras_dynar_remove_at(d,2000,&i);
162      if (i != cpt) {
163        fprintf(stderr,
164            "Remove a bad value. Got %d, expected %d\n",
165                i,cpt);
166        abort();
167      }
168      //     fprintf (stderr,"remove %d, length=%d \n",cpt, gras_dynar_length(d));
169    }
170    gras_dynar_free(d);
171
172    return 0;
173 }