Logo AND Algorithmique Numérique Distribuée

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