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[]) {
21 xbt_init_defaultlog(&argc,argv,"dynar.thresh=debug");
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 d=xbt_dynar_new(sizeof(int),NULL);
34 for (cpt=0; cpt< NB_ELEM; cpt++) {
35 xbt_dynar_push_as(d,int,cpt);
36 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
38 for (cursor=0; cursor< NB_ELEM; cursor++) {
39 iptr=xbt_dynar_get_ptr(d,cursor);
40 xbt_assert2(cursor == *iptr,
41 "The retrieved value is not the same than the injected one (%d!=%d)",
44 xbt_dynar_foreach(d,cursor,cpt){
45 xbt_assert2(cursor == cpt,
46 "The retrieved value is not the same than the injected one (%d!=%d)",
49 for (cpt=0; cpt< NB_ELEM; cpt++)
50 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
52 for (cpt=0; cpt< NB_ELEM; cpt++)
53 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
54 /* xbt_dynar_set(d,cpt,&cpt);*/
56 for (cpt=0; cpt< NB_ELEM; cpt++)
57 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
60 xbt_dynar_foreach(d,cursor,i){
62 "The retrieved value is not the same than the injected one (%d!=%d)",
66 xbt_assert2(cpt == NB_ELEM,
67 "Cannot retrieve my %d values. Last got one is %d",
70 for (cpt=0; cpt< NB_ELEM; cpt++) {
71 xbt_dynar_shift(d,&i);
73 "The retrieved value is not the same than the injected one (%d!=%d)",
75 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
81 INFO1("==== Unshift/pop %d int",NB_ELEM);
82 d=xbt_dynar_new(sizeof(int),NULL);
83 for (cpt=0; cpt< NB_ELEM; cpt++) {
84 xbt_dynar_unshift(d,&cpt);
85 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
87 for (cpt=0; cpt< NB_ELEM; cpt++) {
88 i=xbt_dynar_pop_as(d,int);
90 "The retrieved value is not the same than the injected one (%d!=%d)",
92 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
98 INFO1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
99 d=xbt_dynar_new(sizeof(int),NULL);
100 for (cpt=0; cpt< NB_ELEM; cpt++) {
101 xbt_dynar_push_as(d,int,cpt);
102 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
104 for (cpt=0; cpt< 1000; cpt++) {
105 xbt_dynar_insert_at_as(d,2500,int,cpt);
106 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
109 for (cpt=0; cpt< 2500; cpt++) {
110 xbt_dynar_shift(d,&i);
111 xbt_assert2(i == cpt,
112 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
114 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
116 for (cpt=999; cpt>=0; cpt--) {
117 xbt_dynar_shift(d,&i);
118 xbt_assert2(i == cpt,
119 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
122 for (cpt=2500; cpt< NB_ELEM; 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 end (%d!=%d)",
132 INFO1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
133 d=xbt_dynar_new(sizeof(int),NULL);
134 for (cpt=0; cpt< NB_ELEM; cpt++)
135 xbt_dynar_push_as(d,int,cpt);
137 for (cpt=2000; cpt< 4000; cpt++) {
138 xbt_dynar_remove_at(d,2000,&i);
139 xbt_assert2(i == cpt,
140 "Remove a bad value. Got %d, expected %d",
142 DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));