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[]) {
22 gras_init_defaultlog(&argc,argv,"dynar.thresh=debug");
24 INFO0("==== Traverse the empty dynar");
25 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
26 gras_dynar_foreach(d,cursor,i){
27 gras_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 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
34 for (cpt=0; cpt< NB_ELEM; cpt++) {
35 TRYFAIL(gras_dynar_push(d,&cpt));
36 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
38 for (cursor=0; cursor< NB_ELEM; cursor++) {
39 gras_dynar_get(d,cursor,&cpt);
40 gras_assert2(cursor == cpt,
41 "The retrieved value is not the same than the injected one (%d!=%d)",
44 gras_dynar_foreach(d,cursor,cpt){
45 gras_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 TRYFAIL(gras_dynar_set(d,cpt,&cpt));
52 for (cpt=0; cpt< NB_ELEM; cpt++)
53 TRYFAIL(gras_dynar_set(d,cpt,&cpt));
55 for (cpt=0; cpt< NB_ELEM; cpt++)
56 TRYFAIL(gras_dynar_set(d,cpt,&cpt));
59 gras_dynar_foreach(d,cursor,i){
60 gras_assert2(i == cpt,
61 "The retrieved value is not the same than the injected one (%d!=%d)",
65 gras_assert2(cpt == NB_ELEM,
66 "Cannot retrieve my %d values. Last got one is %d",
69 for (cpt=0; cpt< NB_ELEM; cpt++) {
70 gras_dynar_shift(d,&i);
71 gras_assert2(i == cpt,
72 "The retrieved value is not the same than the injected one (%d!=%d)",
74 DEBUG2("Pop %d, length=%lu",cpt, gras_dynar_length(d));
79 INFO1("==== Unshift/pop %d int",NB_ELEM);
80 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
81 for (cpt=0; cpt< NB_ELEM; cpt++) {
82 TRYFAIL(gras_dynar_unshift(d,&cpt));
83 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
85 for (cpt=0; cpt< NB_ELEM; cpt++) {
87 gras_assert2(i == cpt,
88 "The retrieved value is not the same than the injected one (%d!=%d)",
90 DEBUG2("Pop %d, length=%lu",cpt, gras_dynar_length(d));
96 INFO1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
97 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
98 for (cpt=0; cpt< NB_ELEM; cpt++) {
99 TRYFAIL(gras_dynar_push(d,&cpt));
100 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
102 for (cpt=0; cpt< 1000; cpt++) {
103 TRYFAIL(gras_dynar_insert_at(d,2500,&cpt));
104 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
107 for (cpt=0; cpt< 2500; cpt++) {
108 gras_dynar_shift(d,&i);
109 gras_assert2(i == cpt,
110 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
112 DEBUG2("Pop %d, length=%lu",cpt, gras_dynar_length(d));
114 for (cpt=999; cpt>=0; cpt--) {
115 gras_dynar_shift(d,&i);
116 gras_assert2(i == cpt,
117 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
120 for (cpt=2500; cpt< NB_ELEM; cpt++) {
121 gras_dynar_shift(d,&i);
122 gras_assert2(i == cpt,
123 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
129 INFO1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
130 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
131 for (cpt=0; cpt< NB_ELEM; cpt++)
132 TRYFAIL(gras_dynar_push(d,&cpt));
134 for (cpt=2000; cpt< 4000; cpt++) {
135 gras_dynar_remove_at(d,2000,&i);
136 gras_assert2(i == cpt,
137 "Remove a bad value. Got %d, expected %d",
139 DEBUG2("remove %d, length=%lu",cpt, gras_dynar_length(d));