Logo AND Algorithmique Numérique Distribuée

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