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");
32 INFO1("==== Push %d int, set them again 3 times, traverse them, shift them",
34 d=gras_dynar_new(sizeof(int),NULL);
35 for (cpt=0; cpt< NB_ELEM; cpt++) {
36 gras_dynar_push(d,&cpt);
37 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
39 for (cursor=0; cursor< NB_ELEM; cursor++) {
40 iptr=gras_dynar_get_ptr(d,cursor);
41 gras_assert2(cursor == *iptr,
42 "The retrieved value is not the same than the injected one (%d!=%d)",
45 gras_dynar_foreach(d,cursor,cpt){
46 gras_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 gras_dynar_set(d,cpt,&cpt);
53 for (cpt=0; cpt< NB_ELEM; cpt++)
54 gras_dynar_set(d,cpt,&cpt);
56 for (cpt=0; cpt< NB_ELEM; cpt++)
57 gras_dynar_set(d,cpt,&cpt);
60 gras_dynar_foreach(d,cursor,i){
61 gras_assert2(i == cpt,
62 "The retrieved value is not the same than the injected one (%d!=%d)",
66 gras_assert2(cpt == NB_ELEM,
67 "Cannot retrieve my %d values. Last got one is %d",
70 for (cpt=0; cpt< NB_ELEM; cpt++) {
71 gras_dynar_shift(d,&i);
72 gras_assert2(i == cpt,
73 "The retrieved value is not the same than the injected one (%d!=%d)",
75 DEBUG2("Pop %d, length=%lu",cpt, gras_dynar_length(d));
80 INFO1("==== Unshift/pop %d int",NB_ELEM);
81 d=gras_dynar_new(sizeof(int),NULL);
82 for (cpt=0; cpt< NB_ELEM; cpt++) {
83 gras_dynar_unshift(d,&cpt);
84 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
86 for (cpt=0; cpt< NB_ELEM; cpt++) {
88 gras_assert2(i == cpt,
89 "The retrieved value is not the same than the injected one (%d!=%d)",
91 DEBUG2("Pop %d, length=%lu",cpt, gras_dynar_length(d));
97 INFO1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
98 d=gras_dynar_new(sizeof(int),NULL);
99 for (cpt=0; cpt< NB_ELEM; cpt++) {
100 gras_dynar_push(d,&cpt);
101 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
103 for (cpt=0; cpt< 1000; cpt++) {
104 gras_dynar_insert_at(d,2500,&cpt);
105 DEBUG2("Push %d, length=%lu",cpt, gras_dynar_length(d));
108 for (cpt=0; cpt< 2500; cpt++) {
109 gras_dynar_shift(d,&i);
110 gras_assert2(i == cpt,
111 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
113 DEBUG2("Pop %d, length=%lu",cpt, gras_dynar_length(d));
115 for (cpt=999; cpt>=0; cpt--) {
116 gras_dynar_shift(d,&i);
117 gras_assert2(i == cpt,
118 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
121 for (cpt=2500; cpt< NB_ELEM; cpt++) {
122 gras_dynar_shift(d,&i);
123 gras_assert2(i == cpt,
124 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
130 INFO1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
131 d=gras_dynar_new(sizeof(int),NULL);
132 for (cpt=0; cpt< NB_ELEM; cpt++)
133 gras_dynar_push(d,&cpt);
135 for (cpt=2000; cpt< 4000; cpt++) {
136 gras_dynar_remove_at(d,2000,&i);
137 gras_assert2(i == cpt,
138 "Remove a bad value. Got %d, expected %d",
140 DEBUG2("remove %d, length=%lu",cpt, gras_dynar_length(d));