3 /* dynar_int: A test case for the dynar using integers as content */
5 /* Copyright (c) 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. */
14 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
16 int main(int argc,char *argv[]) {
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 d=xbt_dynar_new(sizeof(int),NULL);
35 for (cpt=0; cpt< NB_ELEM; cpt++) {
36 xbt_dynar_push_as(d,int,cpt);
37 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
39 for (cursor=0; cursor< NB_ELEM; cursor++) {
40 iptr=xbt_dynar_get_ptr(d,cursor);
41 xbt_assert2(cursor == *iptr,
42 "The retrieved value is not the same than the injected one (%d!=%d)",
45 xbt_dynar_foreach(d,cursor,cpt){
46 xbt_assert2(cursor == cpt,
47 "The retrieved value is not the same than the injected one (%d!=%d)",
50 for (cpt=0; cpt< NB_ELEM; cpt++)
51 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
53 for (cpt=0; cpt< NB_ELEM; cpt++)
54 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
55 /* xbt_dynar_set(d,cpt,&cpt);*/
57 for (cpt=0; cpt< NB_ELEM; cpt++)
58 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
61 xbt_dynar_foreach(d,cursor,i){
63 "The retrieved value is not the same than the injected one (%d!=%d)",
67 xbt_assert2(cpt == NB_ELEM,
68 "Cannot retrieve my %d values. Last got one is %d",
71 for (cpt=0; cpt< NB_ELEM; cpt++) {
72 xbt_dynar_shift(d,&i);
74 "The retrieved value is not the same than the injected one (%d!=%d)",
76 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
82 INFO1("==== Unshift/pop %d int",NB_ELEM);
83 d=xbt_dynar_new(sizeof(int),NULL);
84 for (cpt=0; cpt< NB_ELEM; cpt++) {
85 xbt_dynar_unshift(d,&cpt);
86 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
88 for (cpt=0; cpt< NB_ELEM; cpt++) {
89 i=xbt_dynar_pop_as(d,int);
91 "The retrieved value is not the same than the injected one (%d!=%d)",
93 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
99 INFO1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
100 d=xbt_dynar_new(sizeof(int),NULL);
101 for (cpt=0; cpt< NB_ELEM; cpt++) {
102 xbt_dynar_push_as(d,int,cpt);
103 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
105 for (cpt=0; cpt< 1000; cpt++) {
106 xbt_dynar_insert_at_as(d,2500,int,cpt);
107 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
110 for (cpt=0; cpt< 2500; cpt++) {
111 xbt_dynar_shift(d,&i);
112 xbt_assert2(i == cpt,
113 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
115 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
117 for (cpt=999; cpt>=0; cpt--) {
118 xbt_dynar_shift(d,&i);
119 xbt_assert2(i == cpt,
120 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
123 for (cpt=2500; cpt< NB_ELEM; 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 end (%d!=%d)",
133 INFO1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
134 d=xbt_dynar_new(sizeof(int),NULL);
135 for (cpt=0; cpt< NB_ELEM; cpt++)
136 xbt_dynar_push_as(d,int,cpt);
138 for (cpt=2000; cpt< 4000; cpt++) {
139 xbt_dynar_remove_at(d,2000,&i);
140 xbt_assert2(i == cpt,
141 "Remove a bad value. Got %d, expected %d",
143 DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));