3 /* dynar_string: A test case for the dynar using strings 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. */
14 /* NB_ELEM HAS to be a multiple of 5 */
17 void free_string(void *d);
19 void free_string(void *d){
23 int main(int argc,char *argv[]) {
30 gras_init_defaultlog(argc,argv,"dynar.thresh=debug");
32 fprintf(stderr,"==== Traverse the empty dynar\n");
33 TRYFAIL(gras_dynar_new(&d,sizeof(char *),&free_string));
34 gras_dynar_foreach(d,cpt,i){
36 "Damnit, there is something in the empty dynar\n");
41 fprintf(stderr,"==== Push %d strings, set them again 3 times, shift them\n",NB_ELEM);
42 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
43 for (cpt=0; cpt< NB_ELEM; cpt++) {
44 sprintf(buf,"%d",cpt);
46 TRYFAIL(gras_dynar_push(d,&s1));
48 for (cpt=0; cpt< NB_ELEM; cpt++) {
49 sprintf(buf,"%d",cpt);
51 TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
53 for (cpt=0; cpt< NB_ELEM; cpt++) {
54 sprintf(buf,"%d",cpt);
56 TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
58 for (cpt=0; cpt< NB_ELEM; cpt++) {
59 sprintf(buf,"%d",cpt);
61 TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
63 for (cpt=0; cpt< NB_ELEM; cpt++) {
64 sprintf(buf,"%d",cpt);
65 gras_dynar_shift(d,&s2);
68 "The retrieved value is not the same than the injected one (%s!=%s)\n",
77 fprintf(stderr,"==== Unshift/pop %d strings\n",NB_ELEM);
78 TRYFAIL(gras_dynar_new(&d,sizeof(char**),&free_string));
79 for (cpt=0; cpt< NB_ELEM; cpt++) {
80 sprintf(buf,"%d",cpt);
82 TRYFAIL(gras_dynar_unshift(d,&s1));
84 for (cpt=0; cpt< NB_ELEM; cpt++) {
85 sprintf(buf,"%d",cpt);
86 gras_dynar_pop(d,&s2);
89 "The retrieved value is not the same than the injected one (%s!=%s)\n",
99 fprintf(stderr,"==== Push %d strings, insert %d strings in the middle, shift everything\n",NB_ELEM,NB_ELEM/5);
100 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
101 for (cpt=0; cpt< NB_ELEM; cpt++) {
102 sprintf(buf,"%d",cpt);
104 TRYFAIL(gras_dynar_push(d,&s1));
106 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
107 sprintf(buf,"%d",cpt);
109 TRYFAIL(gras_dynar_insert_at(d,NB_ELEM/2,&s1));
112 for (cpt=0; cpt< NB_ELEM/2; cpt++) {
113 sprintf(buf,"%d",cpt);
114 gras_dynar_shift(d,&s2);
115 if (strcmp(buf,s2)) {
117 "The retrieved value is not the same than the injected one at the begining (%s!=%s)\n",
123 for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
124 sprintf(buf,"%d",cpt);
125 gras_dynar_shift(d,&s2);
126 if (strcmp(buf,s2)) {
128 "The retrieved value is not the same than the injected one in the middle (%s!=%s)\n",
134 for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
135 sprintf(buf,"%d",cpt);
136 gras_dynar_shift(d,&s2);
137 if (strcmp(buf,s2)) {
139 "The retrieved value is not the same than the injected one at the end (%s!=%s)\n",
148 fprintf(stderr,"==== Push %d strings, remove %d-%d. free the rest\n",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
149 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
150 for (cpt=0; cpt< NB_ELEM; cpt++) {
151 sprintf(buf,"%d",cpt);
153 TRYFAIL(gras_dynar_push(d,&s1));
155 for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
156 sprintf(buf,"%d",cpt);
157 gras_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
158 if (strcmp(buf,s2)) {
160 "Remove a bad value. Got %s, expected %s\n",