3 /* dynar_int: A test case for the dynar using integers as content */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003 the OURAGAN project. */
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. */
15 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
17 int main(int argc,char *argv[]) {
23 xbt_init_defaultlog(&argc,argv,"dynar.thresh=debug");
25 INFO0("==== Traverse the empty dynar");
26 d=xbt_dynar_new(sizeof(int),NULL);
27 xbt_dynar_foreach(d,cursor,i){
28 xbt_assert0(0,"Damnit, there is something in the empty dynar");
33 INFO1("==== Push %d int, set them again 3 times, traverse them, shift them",
35 d=xbt_dynar_new(sizeof(int),NULL);
36 for (cpt=0; cpt< NB_ELEM; cpt++) {
37 xbt_dynar_push_as(d,int,cpt);
38 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
40 for (cursor=0; cursor< NB_ELEM; cursor++) {
41 iptr=xbt_dynar_get_ptr(d,cursor);
42 xbt_assert2(cursor == *iptr,
43 "The retrieved value is not the same than the injected one (%d!=%d)",
46 xbt_dynar_foreach(d,cursor,cpt){
47 xbt_assert2(cursor == cpt,
48 "The retrieved value is not the same than the injected one (%d!=%d)",
51 for (cpt=0; cpt< NB_ELEM; cpt++)
52 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
54 for (cpt=0; cpt< NB_ELEM; cpt++)
55 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
56 /* xbt_dynar_set(d,cpt,&cpt);*/
58 for (cpt=0; cpt< NB_ELEM; cpt++)
59 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
62 xbt_dynar_foreach(d,cursor,i){
64 "The retrieved value is not the same than the injected one (%d!=%d)",
68 xbt_assert2(cpt == NB_ELEM,
69 "Cannot retrieve my %d values. Last got one is %d",
72 for (cpt=0; cpt< NB_ELEM; cpt++) {
73 xbt_dynar_shift(d,&i);
75 "The retrieved value is not the same than the injected one (%d!=%d)",
77 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
83 INFO1("==== Unshift/pop %d int",NB_ELEM);
84 d=xbt_dynar_new(sizeof(int),NULL);
85 for (cpt=0; cpt< NB_ELEM; cpt++) {
86 xbt_dynar_unshift(d,&cpt);
87 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
89 for (cpt=0; cpt< NB_ELEM; cpt++) {
90 i=xbt_dynar_pop_as(d,int);
92 "The retrieved value is not the same than the injected one (%d!=%d)",
94 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
100 INFO1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
101 d=xbt_dynar_new(sizeof(int),NULL);
102 for (cpt=0; cpt< NB_ELEM; cpt++) {
103 xbt_dynar_push_as(d,int,cpt);
104 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
106 for (cpt=0; cpt< 1000; cpt++) {
107 xbt_dynar_insert_at_as(d,2500,int,cpt);
108 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
111 for (cpt=0; cpt< 2500; cpt++) {
112 xbt_dynar_shift(d,&i);
113 xbt_assert2(i == cpt,
114 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
116 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
118 for (cpt=999; cpt>=0; cpt--) {
119 xbt_dynar_shift(d,&i);
120 xbt_assert2(i == cpt,
121 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
124 for (cpt=2500; cpt< NB_ELEM; cpt++) {
125 xbt_dynar_shift(d,&i);
126 xbt_assert2(i == cpt,
127 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
134 INFO1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
135 d=xbt_dynar_new(sizeof(int),NULL);
136 for (cpt=0; cpt< NB_ELEM; cpt++)
137 xbt_dynar_push_as(d,int,cpt);
139 for (cpt=2000; cpt< 4000; cpt++) {
140 xbt_dynar_remove_at(d,2000,&i);
141 xbt_assert2(i == cpt,
142 "Remove a bad value. Got %d, expected %d",
144 DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));