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 */
16 GRAS_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
18 void free_string(void *d);
20 void free_string(void *d){
21 gras_free(*(void**)d);
24 int main(int argc,char *argv[]) {
31 gras_init_defaultlog(&argc,argv,"dynar.thresh=debug");
33 INFO0("==== Traverse the empty dynar");
34 TRYFAIL(gras_dynar_new(&d,sizeof(char *),&free_string));
35 gras_dynar_foreach(d,cpt,s1){
37 "Damnit, there is something in the empty dynar");
41 INFO1("==== Push %d strings, set them again 3 times, shift them",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);
66 gras_assert2 (!strcmp(buf,s2),
67 "The retrieved value is not the same than the injected one (%s!=%s)",
74 INFO1("==== Unshift, traverse and pop %d strings",NB_ELEM);
75 TRYFAIL(gras_dynar_new(&d,sizeof(char**),&free_string));
76 for (cpt=0; cpt< NB_ELEM; cpt++) {
77 sprintf(buf,"%d",cpt);
79 TRYFAIL(gras_dynar_unshift(d,&s1));
81 gras_dynar_foreach(d,cpt,s1) {
82 sprintf(buf,"%d",NB_ELEM - cpt -1);
83 gras_assert2 (!strcmp(buf,s1),
84 "The retrieved value is not the same than the injected one (%s!=%s)",
87 for (cpt=0; cpt< NB_ELEM; cpt++) {
88 sprintf(buf,"%d",cpt);
89 gras_dynar_pop(d,&s2);
90 gras_assert2 (!strcmp(buf,s2),
91 "The retrieved value is not the same than the injected one (%s!=%s)",
98 INFO2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
99 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
100 for (cpt=0; cpt< NB_ELEM; cpt++) {
101 sprintf(buf,"%d",cpt);
103 TRYFAIL(gras_dynar_push(d,&s1));
105 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
106 sprintf(buf,"%d",cpt);
108 TRYFAIL(gras_dynar_insert_at(d,NB_ELEM/2,&s1));
111 for (cpt=0; cpt< NB_ELEM/2; cpt++) {
112 sprintf(buf,"%d",cpt);
113 gras_dynar_shift(d,&s2);
114 gras_assert2(!strcmp(buf,s2),
115 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
119 for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
120 sprintf(buf,"%d",cpt);
121 gras_dynar_shift(d,&s2);
122 gras_assert2 (!strcmp(buf,s2),
123 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
127 for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
128 sprintf(buf,"%d",cpt);
129 gras_dynar_shift(d,&s2);
130 gras_assert2 (!strcmp(buf,s2),
131 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
138 INFO3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
139 TRYFAIL(gras_dynar_new(&d,sizeof(char*),&free_string));
140 for (cpt=0; cpt< NB_ELEM; cpt++) {
141 sprintf(buf,"%d",cpt);
143 TRYFAIL(gras_dynar_push(d,&s1));
145 for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
146 sprintf(buf,"%d",cpt);
147 gras_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
148 gras_assert2(!strcmp(buf,s2),
149 "Remove a bad value. Got %s, expected %s",