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] */
22 xbt_init_defaultlog(&argc,argv,"dynar.thresh=debug");
24 INFO0("==== Traverse the empty dynar");
25 d=xbt_dynar_new(sizeof(int),NULL);
26 xbt_dynar_foreach(d,cursor,i){
27 xbt_assert0(0,"Damnit, there is something in the empty dynar");
32 INFO1("==== Push %d int, set them again 3 times, traverse them, shift them",
34 /* Populate_ints [doxygen cruft] */
35 /* 1. Populate the dynar */
36 d=xbt_dynar_new(sizeof(int),NULL);
37 for (cpt=0; cpt< NB_ELEM; cpt++) {
38 xbt_dynar_push_as(d,int,cpt); /* This is faster (and possible only with scalars) */
39 /* xbt_dynar_push(d,&cpt); This would also work */
40 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
43 /* 2. Traverse manually the dynar */
44 for (cursor=0; cursor< NB_ELEM; cursor++) {
45 iptr=xbt_dynar_get_ptr(d,cursor);
46 xbt_assert2(cursor == *iptr,
47 "The retrieved value is not the same than the injected one (%d!=%d)",
51 /* 3. Traverse the dynar using the neat macro to that extend */
52 xbt_dynar_foreach(d,cursor,cpt){
53 xbt_assert2(cursor == cpt,
54 "The retrieved value is not the same than the injected one (%d!=%d)",
57 /* end_of_traversal */
59 for (cpt=0; cpt< NB_ELEM; cpt++)
60 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
62 for (cpt=0; cpt< NB_ELEM; cpt++)
63 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
64 /* xbt_dynar_set(d,cpt,&cpt);*/
66 for (cpt=0; cpt< NB_ELEM; cpt++)
67 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
70 xbt_dynar_foreach(d,cursor,i){
72 "The retrieved value is not the same than the injected one (%d!=%d)",
76 xbt_assert2(cpt == NB_ELEM,
77 "Cannot retrieve my %d values. Last got one is %d",
80 /* shifting [doxygen cruft] */
81 /* 4. Shift all the values */
82 for (cpt=0; cpt< NB_ELEM; cpt++) {
83 xbt_dynar_shift(d,&i);
85 "The retrieved value is not the same than the injected one (%d!=%d)",
87 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
90 /* 5. Free the resources */
95 INFO1("==== Unshift/pop %d int",NB_ELEM);
96 d=xbt_dynar_new(sizeof(int),NULL);
97 for (cpt=0; cpt< NB_ELEM; cpt++) {
98 xbt_dynar_unshift(d,&cpt);
99 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
101 for (cpt=0; cpt< NB_ELEM; cpt++) {
102 i=xbt_dynar_pop_as(d,int);
103 xbt_assert2(i == cpt,
104 "The retrieved value is not the same than the injected one (%d!=%d)",
106 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
112 INFO1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
113 d=xbt_dynar_new(sizeof(int),NULL);
114 for (cpt=0; cpt< NB_ELEM; cpt++) {
115 xbt_dynar_push_as(d,int,cpt);
116 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
118 for (cpt=0; cpt< 1000; cpt++) {
119 xbt_dynar_insert_at_as(d,2500,int,cpt);
120 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
123 for (cpt=0; cpt< 2500; cpt++) {
124 xbt_dynar_shift(d,&i);
125 xbt_assert2(i == cpt,
126 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
128 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
130 for (cpt=999; cpt>=0; cpt--) {
131 xbt_dynar_shift(d,&i);
132 xbt_assert2(i == cpt,
133 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
136 for (cpt=2500; cpt< NB_ELEM; cpt++) {
137 xbt_dynar_shift(d,&i);
138 xbt_assert2(i == cpt,
139 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
146 INFO1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
147 d=xbt_dynar_new(sizeof(int),NULL);
148 for (cpt=0; cpt< NB_ELEM; cpt++)
149 xbt_dynar_push_as(d,int,cpt);
151 for (cpt=2000; cpt< 4000; cpt++) {
152 xbt_dynar_remove_at(d,2000,&i);
153 xbt_assert2(i == cpt,
154 "Remove a bad value. Got %d, expected %d",
156 DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));