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 int main(int argc,char *argv[]) {
22 gras_init_defaultlog(argc,argv,"dynar.thresh=debug");
24 fprintf(stderr,"==== Traverse the empty dynar\n");
25 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
26 gras_dynar_foreach(d,cursor,i){
28 "Damnit, there is something in the empty dynar\n");
34 "==== Push %d int, set them again 3 times, traverse them, shift them\n",
36 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
37 for (cpt=0; cpt< NB_ELEM; cpt++) {
38 TRYFAIL(gras_dynar_push(d,&cpt));
39 //fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
41 for (cursor=0; cursor< NB_ELEM; cursor++) {
42 gras_dynar_get(d,cursor,&cpt);
43 gras_assert2(cursor == cpt,
44 "The retrieved value is not the same than the injected one (%d!=%d)",
47 gras_dynar_foreach(d,cursor,cpt){
48 gras_assert2(cursor == cpt,
49 "The retrieved value is not the same than the injected one (%d!=%d)",
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));
58 for (cpt=0; cpt< NB_ELEM; cpt++) {
59 TRYFAIL(gras_dynar_set(d,cpt,&cpt));
62 gras_dynar_foreach(d,cursor,i){
65 "The retrieved value is not the same than the injected one (%d!=%d)\n",
73 "Cannot retrieve my %d values. Last got one is %d\n",NB_ELEM,
78 for (cpt=0; cpt< NB_ELEM; cpt++) {
79 gras_dynar_shift(d,&i);
82 "The retrieved value is not the same than the injected one (%d!=%d)\n",
86 // fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
91 fprintf(stderr,"==== Unshift/pop %d int\n",NB_ELEM);
92 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
93 for (cpt=0; cpt< NB_ELEM; cpt++) {
94 TRYFAIL(gras_dynar_unshift(d,&cpt));
95 // fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
97 for (cpt=0; cpt< NB_ELEM; cpt++) {
101 "The retrieved value is not the same than the injected one (%d!=%d)\n",
105 // fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
111 fprintf(stderr,"==== Push %d int, insert 1000 int in the middle, shift everything\n",NB_ELEM);
112 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
113 for (cpt=0; cpt< NB_ELEM; cpt++) {
114 TRYFAIL(gras_dynar_push(d,&cpt));
115 // fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
117 for (cpt=0; cpt< 1000; cpt++) {
118 TRYFAIL(gras_dynar_insert_at(d,2500,&cpt));
119 // fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
122 for (cpt=0; cpt< 2500; cpt++) {
123 gras_dynar_shift(d,&i);
126 "The retrieved value is not the same than the injected one at the begining (%d!=%d)\n",
130 // fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
132 for (cpt=999; cpt>=0; cpt--) {
133 gras_dynar_shift(d,&i);
136 "The retrieved value is not the same than the injected one in the middle (%d!=%d)\n",
141 for (cpt=2500; cpt< NB_ELEM; cpt++) {
142 gras_dynar_shift(d,&i);
145 "The retrieved value is not the same than the injected one at the end (%d!=%d)\n",
153 fprintf(stderr,"==== Push %d int, remove 2000-4000. free the rest\n",NB_ELEM);
154 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
155 for (cpt=0; cpt< NB_ELEM; cpt++) {
156 TRYFAIL(gras_dynar_push(d,&cpt));
158 for (cpt=2000; cpt< 4000; cpt++) {
159 gras_dynar_remove_at(d,2000,&i);
162 "Remove a bad value. Got %d, expected %d\n",
166 // fprintf (stderr,"remove %d, length=%d \n",cpt, gras_dynar_length(d));