Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reuse the dynar of keys to speed the test up;verbose<0 => absolutelely no output...
[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 /* 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 "gras.h"
11
12 #define NB_ELEM 5000
13 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
14
15 int main(int argc,char *argv[]) {
16    /* Vars_decl [doxygen cruft] */
17    xbt_dynar_t d;
18    xbt_error_t errcode;
19    int i,cpt,cursor;
20    int *iptr;
21    
22    xbt_init(&argc,argv);
23
24    INFO0("==== Traverse the empty dynar");
25    d=xbt_dynar_new(sizeof(int),NULL);
26    xbt_dynar_foreach(d,cursor,i){
27      xbt_assert0(0,"Damnit, there is something in the empty dynar");
28    }
29    xbt_dynar_free(&d);
30    xbt_dynar_free(&d);
31
32    INFO1("==== Push %d int, set them again 3 times, traverse them, shift them",
33         NB_ELEM);
34    /* Populate_ints [doxygen cruft] */
35    /* 1. Populate the dynar */
36    d=xbt_dynar_new(sizeof(int),NULL);
37    for (cpt=0; cpt< NB_ELEM; cpt++) {
38      xbt_dynar_push_as(d,int,cpt); /* This is faster (and possible only with scalars) */
39      /* xbt_dynar_push(d,&cpt);       This would also work */
40      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
41    }
42    
43    /* 2. Traverse manually the dynar */
44    for (cursor=0; cursor< NB_ELEM; cursor++) {
45      iptr=xbt_dynar_get_ptr(d,cursor);
46      xbt_assert2(cursor == *iptr,
47                  "The retrieved value is not the same than the injected one (%d!=%d)",
48                  cursor,cpt);
49    }
50    
51    /* 3. Traverse the dynar using the neat macro to that extend */
52    xbt_dynar_foreach(d,cursor,cpt){
53      xbt_assert2(cursor == cpt,
54                  "The retrieved value is not the same than the injected one (%d!=%d)",
55                  cursor,cpt);
56    }
57    /* end_of_traversal */
58    
59    for (cpt=0; cpt< NB_ELEM; cpt++)
60      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
61
62    for (cpt=0; cpt< NB_ELEM; cpt++) 
63      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
64 /*     xbt_dynar_set(d,cpt,&cpt);*/
65    
66    for (cpt=0; cpt< NB_ELEM; cpt++) 
67      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
68    
69    cpt=0;
70    xbt_dynar_foreach(d,cursor,i){
71      xbt_assert2(i == cpt,
72           "The retrieved value is not the same than the injected one (%d!=%d)",
73                   i,cpt);
74      cpt++;
75    }
76    xbt_assert2(cpt == NB_ELEM,
77                 "Cannot retrieve my %d values. Last got one is %d",
78                 NB_ELEM, cpt);
79
80    /* shifting [doxygen cruft] */
81    /* 4. Shift all the values */
82    for (cpt=0; cpt< NB_ELEM; cpt++) {
83      xbt_dynar_shift(d,&i);
84      xbt_assert2(i == cpt,
85            "The retrieved value is not the same than the injected one (%d!=%d)",
86                i,cpt);
87      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
88    }
89    
90    /* 5. Free the resources */
91    xbt_dynar_free(&d);
92    xbt_dynar_free(&d);
93
94    
95    INFO1("==== Unshift/pop %d int",NB_ELEM);
96    d=xbt_dynar_new(sizeof(int),NULL);
97    for (cpt=0; cpt< NB_ELEM; cpt++) {
98      xbt_dynar_unshift(d,&cpt);
99      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
100    }
101    for (cpt=0; cpt< NB_ELEM; cpt++) {
102      i=xbt_dynar_pop_as(d,int);
103      xbt_assert2(i == cpt,
104            "The retrieved value is not the same than the injected one (%d!=%d)",
105                  i,cpt);
106      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
107    }
108    xbt_dynar_free(&d);
109    xbt_dynar_free(&d);
110
111    
112    INFO1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
113    d=xbt_dynar_new(sizeof(int),NULL);
114    for (cpt=0; cpt< NB_ELEM; cpt++) {
115      xbt_dynar_push_as(d,int,cpt);
116      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
117    }
118    for (cpt=0; cpt< 1000; cpt++) {
119      xbt_dynar_insert_at_as(d,2500,int,cpt);
120      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
121    }
122
123    for (cpt=0; cpt< 2500; cpt++) {
124      xbt_dynar_shift(d,&i);
125      xbt_assert2(i == cpt,
126            "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
127                i,cpt);
128      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
129    }
130    for (cpt=999; cpt>=0; cpt--) {
131      xbt_dynar_shift(d,&i);
132      xbt_assert2(i == cpt,
133            "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
134                i,cpt);
135    }
136    for (cpt=2500; cpt< NB_ELEM; cpt++) {
137      xbt_dynar_shift(d,&i);
138       xbt_assert2(i == cpt,
139            "The retrieved value is not the same than the injected one at the end (%d!=%d)",
140                i,cpt);
141    }
142    xbt_dynar_free(&d);
143    xbt_dynar_free(&d);
144
145
146    INFO1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
147    d=xbt_dynar_new(sizeof(int),NULL);
148    for (cpt=0; cpt< NB_ELEM; cpt++) 
149      xbt_dynar_push_as(d,int,cpt);
150    
151    for (cpt=2000; cpt< 4000; cpt++) {
152      xbt_dynar_remove_at(d,2000,&i);
153      xbt_assert2(i == cpt,
154                   "Remove a bad value. Got %d, expected %d",
155                   i,cpt);
156      DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));
157    }
158    xbt_dynar_free(&d);
159    xbt_dynar_free(&d);
160
161    xbt_exit();
162    return 0;
163 }