Logo AND Algorithmique Numérique Distribuée

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