Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initial revision
[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
16 int main(int argc,char *argv[]) {
17    gras_dynar_t *d;
18    gras_error_t errcode;
19    int i,cpt,cursor;
20    
21    //   TRYFAIL(gras_log_control_set("root.thresh=debug dynar.thresh=info"));
22    TRYFAIL(gras_log_control_set("root.thresh=info"));
23    fprintf(stderr,"==== Push %d int, set them again 3 times, traverse them, shift them\n",NB_ELEM);
24    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
25    for (cpt=0; cpt< NB_ELEM; cpt++) {
26      TRYFAIL(gras_dynar_push(d,&cpt));
27      //     fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
28    }
29    for (cpt=0; cpt< NB_ELEM; cpt++) {
30      TRYFAIL(gras_dynar_set(d,cpt,&cpt));
31    }
32    for (cpt=0; cpt< NB_ELEM; cpt++) {
33      TRYFAIL(gras_dynar_set(d,cpt,&cpt));
34    }
35    for (cpt=0; cpt< NB_ELEM; cpt++) {
36      TRYFAIL(gras_dynar_set(d,cpt,&cpt));
37    }
38    cpt=0;
39    gras_dynar_foreach(d,cursor,i){
40      if (i != cpt) {
41        fprintf(stderr,
42           "The retrieved value is not the same than the injected one (%d!=%d)\n",
43                i,cpt);
44        abort();
45      }
46      cpt++;
47    }
48    if (cpt !=NB_ELEM) {
49      fprintf(stderr,
50              "Cannot retrieve my %d values. Last got one is %d\n",NB_ELEM,
51              cpt);
52        abort();
53    }     
54
55    for (cpt=0; cpt< NB_ELEM; cpt++) {
56      gras_dynar_shift(d,&i);
57      if (i != cpt) {
58        fprintf(stderr,
59            "The retrieved value is not the same than the injected one (%d!=%d)\n",
60                i,cpt);
61        abort();
62      }
63      //     fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
64    }
65    gras_dynar_free(d);
66
67
68    fprintf(stderr,"==== Unshift/pop %d int\n",NB_ELEM);
69    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
70    for (cpt=0; cpt< NB_ELEM; cpt++) {
71      TRYFAIL(gras_dynar_unshift(d,&cpt));
72      //     fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
73    }
74    for (cpt=0; cpt< NB_ELEM; cpt++) {
75      gras_dynar_pop(d,&i);
76      if (i != cpt) {
77        fprintf(stderr,
78            "The retrieved value is not the same than the injected one (%d!=%d)\n",
79                i,cpt);
80        abort();
81      }
82      //     fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
83    }
84    gras_dynar_free(d);
85
86
87
88    fprintf(stderr,"==== Push %d int, insert 1000 int in the middle, shift everything\n",NB_ELEM);
89    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
90    for (cpt=0; cpt< NB_ELEM; cpt++) {
91      TRYFAIL(gras_dynar_push(d,&cpt));
92      //     fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
93    }
94    for (cpt=0; cpt< 1000; cpt++) {
95      TRYFAIL(gras_dynar_insert_at(d,2500,&cpt));
96      //     fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
97    }
98
99    for (cpt=0; cpt< 2500; cpt++) {
100      gras_dynar_shift(d,&i);
101      if (i != cpt) {
102        fprintf(stderr,
103            "The retrieved value is not the same than the injected one at the begining (%d!=%d)\n",
104                i,cpt);
105        abort();
106      }
107      //     fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
108    }
109    for (cpt=999; cpt>=0; cpt--) {
110      gras_dynar_shift(d,&i);
111      if (i != cpt) {
112        fprintf(stderr,
113            "The retrieved value is not the same than the injected one in the middle (%d!=%d)\n",
114                i,cpt);
115        abort();
116      }
117    }
118    for (cpt=2500; cpt< NB_ELEM; cpt++) {
119      gras_dynar_shift(d,&i);
120      if (i != cpt) {
121        fprintf(stderr,
122            "The retrieved value is not the same than the injected one at the end (%d!=%d)\n",
123                i,cpt);
124        abort();
125      }
126    }
127    gras_dynar_free(d);
128
129
130    fprintf(stderr,"==== Push %d int, remove 2000-4000. free the rest\n",NB_ELEM);
131    TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
132    for (cpt=0; cpt< NB_ELEM; cpt++) {
133      TRYFAIL(gras_dynar_push(d,&cpt));
134    }
135    for (cpt=2000; cpt< 4000; cpt++) {
136      gras_dynar_remove_at(d,2000,&i);
137      if (i != cpt) {
138        fprintf(stderr,
139            "Remove a bad value. Got %d, expected %d\n",
140                i,cpt);
141        abort();
142      }
143      //     fprintf (stderr,"remove %d, length=%d \n",cpt, gras_dynar_length(d));
144    }
145    gras_dynar_free(d);
146
147    return 0;
148 }