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 void free_string(void *d);
16 void free_string(void *d){
20 int main(int argc,char *argv[]) {
27 fprintf(stderr,"==== Traverse the empty dynar\n");
28 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
29 gras_dynar_foreach(d,cpt,i){
31 "Damnit, there is something in the empty dynar\n");
36 fprintf(stderr,"==== Push 5000 strings, set them again 3 times, shift them\n");
37 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
38 for (cpt=0; cpt< 5000; cpt++) {
39 sprintf(buf,"%d",cpt);
41 TRYFAIL(gras_dynar_push(d,&s1));
43 for (cpt=0; cpt< 5000; cpt++) {
44 sprintf(buf,"%d",cpt);
46 TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
48 for (cpt=0; cpt< 5000; cpt++) {
49 sprintf(buf,"%d",cpt);
51 TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
53 for (cpt=0; cpt< 5000; cpt++) {
54 sprintf(buf,"%d",cpt);
56 TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
58 for (cpt=0; cpt< 5000; cpt++) {
59 sprintf(buf,"%d",cpt);
60 gras_dynar_shift(d,&s2);
63 "The retrieved value is not the same than the injected one (%s!=%s)\n",
72 fprintf(stderr,"==== Unshift/pop 5000 strings\n");
73 TRYFAIL(gras_dynar_new(&d,sizeof(char**),&free_string));
74 for (cpt=0; cpt< 5000; cpt++) {
75 sprintf(buf,"%d",cpt);
77 TRYFAIL(gras_dynar_unshift(d,&s1));
79 for (cpt=0; cpt< 5000; cpt++) {
80 sprintf(buf,"%d",cpt);
81 gras_dynar_pop(d,&s2);
84 "The retrieved value is not the same than the injected one (%s!=%s)\n",
94 fprintf(stderr,"==== Push 5000 strings, insert 1000 strings in the middle, shift everything\n");
95 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
96 for (cpt=0; cpt< 5000; cpt++) {
97 sprintf(buf,"%d",cpt);
99 TRYFAIL(gras_dynar_push(d,&s1));
101 for (cpt=0; cpt< 1000; cpt++) {
102 sprintf(buf,"%d",cpt);
104 TRYFAIL(gras_dynar_insert_at(d,2500,&s1));
107 for (cpt=0; cpt< 2500; cpt++) {
108 sprintf(buf,"%d",cpt);
109 gras_dynar_shift(d,&s2);
110 if (strcmp(buf,s2)) {
112 "The retrieved value is not the same than the injected one at the begining (%s!=%s)\n",
118 for (cpt=999; cpt>=0; cpt--) {
119 sprintf(buf,"%d",cpt);
120 gras_dynar_shift(d,&s2);
121 if (strcmp(buf,s2)) {
123 "The retrieved value is not the same than the injected one in the middle (%s!=%s)\n",
129 for (cpt=2500; cpt< 5000; cpt++) {
130 sprintf(buf,"%d",cpt);
131 gras_dynar_shift(d,&s2);
132 if (strcmp(buf,s2)) {
134 "The retrieved value is not the same than the injected one at the end (%s!=%s)\n",
143 fprintf(stderr,"==== Push 5000 strings, remove 2000-4000. free the rest\n");
144 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
145 for (cpt=0; cpt< 5000; cpt++) {
146 sprintf(buf,"%d",cpt);
148 TRYFAIL(gras_dynar_push(d,&s1));
150 for (cpt=2000; cpt< 4000; cpt++) {
151 sprintf(buf,"%d",cpt);
152 gras_dynar_remove_at(d,2000,&s2);
153 if (strcmp(buf,s2)) {
155 "Remove a bad value. Got %s, expected %s\n",