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);
17 void parse_log_opt(int argc, char **argv, const char *def);
19 int main(int argc,char *argv[]) {
24 parse_log_opt(argc,argv,"dynar.thresh=debug");
26 fprintf(stderr,"==== Traverse the empty dynar\n");
27 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
28 gras_dynar_foreach(d,cursor,i){
30 "Damnit, there is something in the empty dynar\n");
36 "==== Push %d int, set them again 3 times, traverse them, shift them\n",
38 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
39 for (cpt=0; cpt< NB_ELEM; cpt++) {
40 TRYFAIL(gras_dynar_push(d,&cpt));
41 //fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
43 for (cursor=0; cursor< NB_ELEM; cursor++) {
44 gras_dynar_get(d,cursor,&cpt);
45 gras_assert2(cursor == cpt,
46 "The retrieved value is not the same than the injected one (%d!=%d)",
49 gras_dynar_foreach(d,cursor,cpt){
50 gras_assert2(cursor == cpt,
51 "The retrieved value is not the same than the injected one (%d!=%d)",
54 for (cpt=0; cpt< NB_ELEM; cpt++) {
55 TRYFAIL(gras_dynar_set(d,cpt,&cpt));
57 for (cpt=0; cpt< NB_ELEM; cpt++) {
58 TRYFAIL(gras_dynar_set(d,cpt,&cpt));
60 for (cpt=0; cpt< NB_ELEM; cpt++) {
61 TRYFAIL(gras_dynar_set(d,cpt,&cpt));
64 gras_dynar_foreach(d,cursor,i){
67 "The retrieved value is not the same than the injected one (%d!=%d)\n",
75 "Cannot retrieve my %d values. Last got one is %d\n",NB_ELEM,
80 for (cpt=0; cpt< NB_ELEM; cpt++) {
81 gras_dynar_shift(d,&i);
84 "The retrieved value is not the same than the injected one (%d!=%d)\n",
88 // fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
93 fprintf(stderr,"==== Unshift/pop %d int\n",NB_ELEM);
94 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
95 for (cpt=0; cpt< NB_ELEM; cpt++) {
96 TRYFAIL(gras_dynar_unshift(d,&cpt));
97 // fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
99 for (cpt=0; cpt< NB_ELEM; cpt++) {
100 gras_dynar_pop(d,&i);
103 "The retrieved value is not the same than the injected one (%d!=%d)\n",
107 // fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
113 fprintf(stderr,"==== Push %d int, insert 1000 int in the middle, shift everything\n",NB_ELEM);
114 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
115 for (cpt=0; cpt< NB_ELEM; cpt++) {
116 TRYFAIL(gras_dynar_push(d,&cpt));
117 // fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
119 for (cpt=0; cpt< 1000; cpt++) {
120 TRYFAIL(gras_dynar_insert_at(d,2500,&cpt));
121 // fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
124 for (cpt=0; cpt< 2500; cpt++) {
125 gras_dynar_shift(d,&i);
128 "The retrieved value is not the same than the injected one at the begining (%d!=%d)\n",
132 // fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
134 for (cpt=999; cpt>=0; cpt--) {
135 gras_dynar_shift(d,&i);
138 "The retrieved value is not the same than the injected one in the middle (%d!=%d)\n",
143 for (cpt=2500; cpt< NB_ELEM; cpt++) {
144 gras_dynar_shift(d,&i);
147 "The retrieved value is not the same than the injected one at the end (%d!=%d)\n",
155 fprintf(stderr,"==== Push %d int, remove 2000-4000. free the rest\n",NB_ELEM);
156 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
157 for (cpt=0; cpt< NB_ELEM; cpt++) {
158 TRYFAIL(gras_dynar_push(d,&cpt));
160 for (cpt=2000; cpt< 4000; cpt++) {
161 gras_dynar_remove_at(d,2000,&i);
164 "Remove a bad value. Got %d, expected %d\n",
168 // fprintf (stderr,"remove %d, length=%d \n",cpt, gras_dynar_length(d));