Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleanup
[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    xbt_dynar_t d;
17    xbt_error_t errcode;
18    int i,cpt,cursor;
19    int *iptr;
20    
21    xbt_init_defaultlog(&argc,argv,"dynar.thresh=debug");
22
23    INFO0("==== Traverse the empty dynar");
24    d=xbt_dynar_new(sizeof(int),NULL);
25    xbt_dynar_foreach(d,cursor,i){
26      xbt_assert0(0,"Damnit, there is something in the empty dynar");
27    }
28    xbt_dynar_free(&d);
29    xbt_dynar_free(&d);
30
31    INFO1("==== Push %d int, set them again 3 times, traverse them, shift them",
32         NB_ELEM);
33    d=xbt_dynar_new(sizeof(int),NULL);
34    for (cpt=0; cpt< NB_ELEM; cpt++) {
35      xbt_dynar_push_as(d,int,cpt);
36      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
37    }
38    for (cursor=0; cursor< NB_ELEM; cursor++) {
39      iptr=xbt_dynar_get_ptr(d,cursor);
40      xbt_assert2(cursor == *iptr,
41                   "The retrieved value is not the same than the injected one (%d!=%d)",
42                   cursor,cpt);
43    }
44    xbt_dynar_foreach(d,cursor,cpt){
45      xbt_assert2(cursor == cpt,
46                   "The retrieved value is not the same than the injected one (%d!=%d)",
47                   cursor,cpt);
48    }
49    for (cpt=0; cpt< NB_ELEM; cpt++)
50      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
51
52    for (cpt=0; cpt< NB_ELEM; cpt++) 
53      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
54 /*     xbt_dynar_set(d,cpt,&cpt);*/
55    
56    for (cpt=0; cpt< NB_ELEM; cpt++) 
57      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
58    
59    cpt=0;
60    xbt_dynar_foreach(d,cursor,i){
61      xbt_assert2(i == cpt,
62           "The retrieved value is not the same than the injected one (%d!=%d)",
63                   i,cpt);
64      cpt++;
65    }
66    xbt_assert2(cpt == NB_ELEM,
67                 "Cannot retrieve my %d values. Last got one is %d",
68                 NB_ELEM, cpt);
69
70    for (cpt=0; cpt< NB_ELEM; cpt++) {
71      xbt_dynar_shift(d,&i);
72      xbt_assert2(i == cpt,
73            "The retrieved value is not the same than the injected one (%d!=%d)",
74                i,cpt);
75      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
76    }
77    xbt_dynar_free(&d);
78    xbt_dynar_free(&d);
79
80    
81    INFO1("==== Unshift/pop %d int",NB_ELEM);
82    d=xbt_dynar_new(sizeof(int),NULL);
83    for (cpt=0; cpt< NB_ELEM; cpt++) {
84      xbt_dynar_unshift(d,&cpt);
85      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
86    }
87    for (cpt=0; cpt< NB_ELEM; cpt++) {
88      i=xbt_dynar_pop_as(d,int);
89      xbt_assert2(i == cpt,
90            "The retrieved value is not the same than the injected one (%d!=%d)",
91                  i,cpt);
92      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
93    }
94    xbt_dynar_free(&d);
95    xbt_dynar_free(&d);
96
97    
98    INFO1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
99    d=xbt_dynar_new(sizeof(int),NULL);
100    for (cpt=0; cpt< NB_ELEM; cpt++) {
101      xbt_dynar_push_as(d,int,cpt);
102      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
103    }
104    for (cpt=0; cpt< 1000; cpt++) {
105      xbt_dynar_insert_at_as(d,2500,int,cpt);
106      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
107    }
108
109    for (cpt=0; cpt< 2500; cpt++) {
110      xbt_dynar_shift(d,&i);
111      xbt_assert2(i == cpt,
112            "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
113                i,cpt);
114      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
115    }
116    for (cpt=999; cpt>=0; cpt--) {
117      xbt_dynar_shift(d,&i);
118      xbt_assert2(i == cpt,
119            "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
120                i,cpt);
121    }
122    for (cpt=2500; cpt< NB_ELEM; cpt++) {
123      xbt_dynar_shift(d,&i);
124       xbt_assert2(i == cpt,
125            "The retrieved value is not the same than the injected one at the end (%d!=%d)",
126                i,cpt);
127    }
128    xbt_dynar_free(&d);
129    xbt_dynar_free(&d);
130
131
132    INFO1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
133    d=xbt_dynar_new(sizeof(int),NULL);
134    for (cpt=0; cpt< NB_ELEM; cpt++) 
135      xbt_dynar_push_as(d,int,cpt);
136    
137    for (cpt=2000; cpt< 4000; cpt++) {
138      xbt_dynar_remove_at(d,2000,&i);
139      xbt_assert2(i == cpt,
140                   "Remove a bad value. Got %d, expected %d",
141                   i,cpt);
142      DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));
143    }
144    xbt_dynar_free(&d);
145    xbt_dynar_free(&d);
146
147    xbt_exit();
148    return 0;
149 }