3 /* dynar_int: A test case for the dynar using integers as content */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
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. */
13 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
15 int main(int argc,char *argv[]) {
16 /* Vars_decl [doxygen cruft] */
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");
31 INFO1("==== Push %d int, set them again 3 times, traverse them, shift them",
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));
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)",
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)",
56 /* end_of_traversal */
58 for (cpt=0; cpt< NB_ELEM; cpt++)
59 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
61 for (cpt=0; cpt< NB_ELEM; cpt++)
62 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
63 /* xbt_dynar_set(d,cpt,&cpt);*/
65 for (cpt=0; cpt< NB_ELEM; cpt++)
66 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
69 xbt_dynar_foreach(d,cursor,i){
71 "The retrieved value is not the same than the injected one (%d!=%d)",
75 xbt_assert2(cpt == NB_ELEM,
76 "Cannot retrieve my %d values. Last got one is %d",
79 /* shifting [doxygen cruft] */
80 /* 4. Shift all the values */
81 for (cpt=0; cpt< NB_ELEM; cpt++) {
82 xbt_dynar_shift(d,&i);
84 "The retrieved value is not the same than the injected one (%d!=%d)",
86 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
89 /* 5. Free the resources */
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));
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)",
105 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
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));
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));
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)",
127 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
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)",
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)",
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);
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",
155 DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));