Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7e8ef1aab3c776b079d9f7655e31163c4688403e
[simgrid.git] / testsuite / xbt / dynar_double.c
1 /* $Id$ */
2
3 /* dynar_double: A test case for the dynar using doubles 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 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
13
14 int main(int argc,char *argv[]) {
15    xbt_dynar_t d;
16    int cpt,cursor;
17    double d1,d2;
18    
19    xbt_init(&argc,argv);
20
21    INFO0("==== Traverse the empty dynar");
22    d=xbt_dynar_new(sizeof(int),NULL);
23    xbt_dynar_foreach(d,cursor,cpt){
24      xbt_assert0(FALSE,
25              "Damnit, there is something in the empty dynar");
26    }
27    xbt_dynar_free(&d);
28    xbt_dynar_free(&d);
29
30    INFO0("==== Push/shift 5000 doubles");
31    d=xbt_dynar_new(sizeof(double),NULL);
32    for (cpt=0; cpt< 5000; cpt++) {
33      d1=(double)cpt;
34      xbt_dynar_push(d,&d1);
35    }
36    xbt_dynar_foreach(d,cursor,d2){
37      d1=(double)cursor;
38      xbt_assert2(d1 == d2,
39            "The retrieved value is not the same than the injected one (%f!=%f)",
40                   d1,d2);
41    }
42    for (cpt=0; cpt< 5000; cpt++) {
43      d1=(double)cpt;
44      xbt_dynar_shift(d,&d2);
45      xbt_assert2(d1 == d2,
46            "The retrieved value is not the same than the injected one (%f!=%f)",
47                   d1,d2);
48    }
49    xbt_dynar_free(&d);
50    xbt_dynar_free(&d);
51
52
53    INFO0("==== Unshift/pop 5000 doubles");
54    d=xbt_dynar_new(sizeof(double),NULL);
55    for (cpt=0; cpt< 5000; cpt++) {
56      d1=(double)cpt;
57      xbt_dynar_unshift(d,&d1);
58    }
59    for (cpt=0; cpt< 5000; cpt++) {
60      d1=(double)cpt;
61      xbt_dynar_pop(d,&d2);
62      xbt_assert2 (d1 == d2,
63            "The retrieved value is not the same than the injected one (%f!=%f)",
64                    d1,d2);
65    }
66    xbt_dynar_free(&d);
67    xbt_dynar_free(&d);
68
69
70
71    INFO0("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
72    d=xbt_dynar_new(sizeof(double),NULL);
73    for (cpt=0; cpt< 5000; cpt++) {
74      d1=(double)cpt;
75      xbt_dynar_push(d,&d1);
76    }
77    for (cpt=0; cpt< 1000; cpt++) {
78      d1=(double)cpt;
79      xbt_dynar_insert_at(d,2500,&d1);
80    }
81
82    for (cpt=0; cpt< 2500; cpt++) {
83      d1=(double)cpt;
84      xbt_dynar_shift(d,&d2);
85      xbt_assert2(d1 == d2,
86            "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
87                   d1,d2);
88      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
89    }
90    for (cpt=999; cpt>=0; cpt--) {
91      d1=(double)cpt;
92      xbt_dynar_shift(d,&d2);
93      xbt_assert2 (d1 == d2,
94            "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
95                    d1,d2);
96    }
97    for (cpt=2500; cpt< 5000; cpt++) {
98      d1=(double)cpt;
99      xbt_dynar_shift(d,&d2);
100      xbt_assert2 (d1 == d2,
101            "The retrieved value is not the same than the injected one at the end (%f!=%f)",
102                    d1,d2);
103    }
104    xbt_dynar_free(&d);
105    xbt_dynar_free(&d);
106
107
108    INFO0("==== Push 5000 double, remove 2000-4000. free the rest");
109    d=xbt_dynar_new(sizeof(double),NULL);
110    for (cpt=0; cpt< 5000; cpt++) {
111      d1=(double)cpt;
112      xbt_dynar_push(d,&d1);
113    }
114    for (cpt=2000; cpt< 4000; cpt++) {
115      d1=(double)cpt;
116      xbt_dynar_remove_at(d,2000,&d2);
117      xbt_assert2 (d1 == d2,
118            "Remove a bad value. Got %f, expected %f",
119                d2,d1);
120    }
121    xbt_dynar_free(&d);
122    xbt_dynar_free(&d);
123
124    xbt_exit();
125    return 0;
126 }