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 GRAS_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
17 int main(int argc,char *argv[]) {
23 gras_init_defaultlog(&argc,argv,"dynar.thresh=debug");
25 INFO0("==== Traverse the empty dynar");
26 d=gras_dynar_new(sizeof(int),NULL);
27 gras_dynar_foreach(d,cursor,i){
28 gras_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=gras_dynar_new(sizeof(int),NULL);
36 for (cpt=0; cpt< NB_ELEM; cpt++) {
37 gras_dynar_push_as(d,int,cpt);
38 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
40 for (cursor=0; cursor< NB_ELEM; cursor++) {
41 iptr=gras_dynar_get_ptr(d,cursor);
42 gras_assert2(cursor == *iptr,
43 "The retrieved value is not the same than the injected one (%d!=%d)",
46 gras_dynar_foreach(d,cursor,cpt){
47 gras_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*)gras_dynar_get_ptr(d,cpt) = cpt;
54 for (cpt=0; cpt< NB_ELEM; cpt++)
55 *(int*)gras_dynar_get_ptr(d,cpt) = cpt;
56 /* gras_dynar_set(d,cpt,&cpt);*/
58 for (cpt=0; cpt< NB_ELEM; cpt++)
59 *(int*)gras_dynar_get_ptr(d,cpt) = cpt;
62 gras_dynar_foreach(d,cursor,i){
63 gras_assert2(i == cpt,
64 "The retrieved value is not the same than the injected one (%d!=%d)",
68 gras_assert2(cpt == NB_ELEM,
69 "Cannot retrieve my %d values. Last got one is %d",
72 for (cpt=0; cpt< NB_ELEM; cpt++) {
73 gras_dynar_shift(d,&i);
74 gras_assert2(i == cpt,
75 "The retrieved value is not the same than the injected one (%d!=%d)",
77 DEBUG2("Pop %d, length=%lu",cpt, gras_dynar_length(d));
83 INFO1("==== Unshift/pop %d int",NB_ELEM);
84 d=gras_dynar_new(sizeof(int),NULL);
85 for (cpt=0; cpt< NB_ELEM; cpt++) {
86 gras_dynar_unshift(d,&cpt);
87 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
89 for (cpt=0; cpt< NB_ELEM; cpt++) {
90 i=gras_dynar_pop_as(d,int);
91 gras_assert2(i == cpt,
92 "The retrieved value is not the same than the injected one (%d!=%d)",
94 DEBUG2("Pop %d, length=%lu",cpt, gras_dynar_length(d));
100 INFO1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
101 d=gras_dynar_new(sizeof(int),NULL);
102 for (cpt=0; cpt< NB_ELEM; cpt++) {
103 gras_dynar_push_as(d,int,cpt);
104 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
106 for (cpt=0; cpt< 1000; cpt++) {
107 gras_dynar_insert_at_as(d,2500,int,cpt);
108 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
111 for (cpt=0; cpt< 2500; cpt++) {
112 gras_dynar_shift(d,&i);
113 gras_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, gras_dynar_length(d));
118 for (cpt=999; cpt>=0; cpt--) {
119 gras_dynar_shift(d,&i);
120 gras_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 gras_dynar_shift(d,&i);
126 gras_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=gras_dynar_new(sizeof(int),NULL);
136 for (cpt=0; cpt< NB_ELEM; cpt++)
137 gras_dynar_push_as(d,int,cpt);
139 for (cpt=2000; cpt< 4000; cpt++) {
140 gras_dynar_remove_at(d,2000,&i);
141 gras_assert2(i == cpt,
142 "Remove a bad value. Got %d, expected %d",
144 DEBUG2("remove %d, length=%lu",cpt, gras_dynar_length(d));