Logo AND Algorithmique Numérique Distribuée

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