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 void parse_log_opt(int argc, char **argv,const char *deft);
22 int main(int argc,char *argv[]) {
29 parse_log_opt(argc,argv,"dynar.thresh=debug");
31 fprintf(stderr,"==== Traverse the empty dynar\n");
32 TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
33 gras_dynar_foreach(d,cpt,i){
35 "Damnit, there is something in the empty dynar\n");
40 fprintf(stderr,"==== Push 5000 strings, set them again 3 times, shift them\n");
41 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
42 for (cpt=0; cpt< 5000; cpt++) {
43 sprintf(buf,"%d",cpt);
45 TRYFAIL(gras_dynar_push(d,&s1));
47 for (cpt=0; cpt< 5000; cpt++) {
48 sprintf(buf,"%d",cpt);
50 TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
52 for (cpt=0; cpt< 5000; cpt++) {
53 sprintf(buf,"%d",cpt);
55 TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
57 for (cpt=0; cpt< 5000; cpt++) {
58 sprintf(buf,"%d",cpt);
60 TRYFAIL(gras_dynar_remplace(d,cpt,&s1));
62 for (cpt=0; cpt< 5000; cpt++) {
63 sprintf(buf,"%d",cpt);
64 gras_dynar_shift(d,&s2);
67 "The retrieved value is not the same than the injected one (%s!=%s)\n",
76 fprintf(stderr,"==== Unshift/pop 5000 strings\n");
77 TRYFAIL(gras_dynar_new(&d,sizeof(char**),&free_string));
78 for (cpt=0; cpt< 5000; cpt++) {
79 sprintf(buf,"%d",cpt);
81 TRYFAIL(gras_dynar_unshift(d,&s1));
83 for (cpt=0; cpt< 5000; cpt++) {
84 sprintf(buf,"%d",cpt);
85 gras_dynar_pop(d,&s2);
88 "The retrieved value is not the same than the injected one (%s!=%s)\n",
98 fprintf(stderr,"==== Push 5000 strings, insert 1000 strings in the middle, shift everything\n");
99 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
100 for (cpt=0; cpt< 5000; cpt++) {
101 sprintf(buf,"%d",cpt);
103 TRYFAIL(gras_dynar_push(d,&s1));
105 for (cpt=0; cpt< 1000; cpt++) {
106 sprintf(buf,"%d",cpt);
108 TRYFAIL(gras_dynar_insert_at(d,2500,&s1));
111 for (cpt=0; cpt< 2500; cpt++) {
112 sprintf(buf,"%d",cpt);
113 gras_dynar_shift(d,&s2);
114 if (strcmp(buf,s2)) {
116 "The retrieved value is not the same than the injected one at the begining (%s!=%s)\n",
122 for (cpt=999; cpt>=0; cpt--) {
123 sprintf(buf,"%d",cpt);
124 gras_dynar_shift(d,&s2);
125 if (strcmp(buf,s2)) {
127 "The retrieved value is not the same than the injected one in the middle (%s!=%s)\n",
133 for (cpt=2500; cpt< 5000; cpt++) {
134 sprintf(buf,"%d",cpt);
135 gras_dynar_shift(d,&s2);
136 if (strcmp(buf,s2)) {
138 "The retrieved value is not the same than the injected one at the end (%s!=%s)\n",
147 fprintf(stderr,"==== Push 5000 strings, remove 2000-4000. free the rest\n");
148 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
149 for (cpt=0; cpt< 5000; cpt++) {
150 sprintf(buf,"%d",cpt);
152 TRYFAIL(gras_dynar_push(d,&s1));
154 for (cpt=2000; cpt< 4000; cpt++) {
155 sprintf(buf,"%d",cpt);
156 gras_dynar_remove_at(d,2000,&s2);
157 if (strcmp(buf,s2)) {
159 "Remove a bad value. Got %s, expected %s\n",