3 /* dynar_string: A test case for the dynar using strings as content */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
13 /* NB_ELEM HAS to be a multiple of 5 */
15 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
17 void free_string(void *d);
19 void free_string(void *d){
23 int main(int argc,char *argv[]) {
30 xbt_init_defaultlog(&argc,argv,"dynar.thresh=debug");
32 INFO0("==== Traverse the empty dynar");
33 d=xbt_dynar_new(sizeof(char *),&free_string);
34 xbt_dynar_foreach(d,cpt,s1){
36 "Damnit, there is something in the empty dynar");
41 INFO1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
42 d=xbt_dynar_new(sizeof(char*),&free_string);
43 for (cpt=0; cpt< NB_ELEM; cpt++) {
44 sprintf(buf,"%d",cpt);
46 xbt_dynar_push(d,&s1);
48 for (cpt=0; cpt< NB_ELEM; cpt++) {
49 sprintf(buf,"%d",cpt);
51 xbt_dynar_replace(d,cpt,&s1);
53 for (cpt=0; cpt< NB_ELEM; cpt++) {
54 sprintf(buf,"%d",cpt);
56 xbt_dynar_replace(d,cpt,&s1);
58 for (cpt=0; cpt< NB_ELEM; cpt++) {
59 sprintf(buf,"%d",cpt);
61 xbt_dynar_replace(d,cpt,&s1);
63 for (cpt=0; cpt< NB_ELEM; cpt++) {
64 sprintf(buf,"%d",cpt);
65 xbt_dynar_shift(d,&s2);
66 xbt_assert2 (!strcmp(buf,s2),
67 "The retrieved value is not the same than the injected one (%s!=%s)",
75 INFO1("==== Unshift, traverse and pop %d strings",NB_ELEM);
76 d=xbt_dynar_new(sizeof(char**),&free_string);
77 for (cpt=0; cpt< NB_ELEM; cpt++) {
78 sprintf(buf,"%d",cpt);
80 xbt_dynar_unshift(d,&s1);
82 xbt_dynar_foreach(d,cpt,s1) {
83 sprintf(buf,"%d",NB_ELEM - cpt -1);
84 xbt_assert2 (!strcmp(buf,s1),
85 "The retrieved value is not the same than the injected one (%s!=%s)",
88 for (cpt=0; cpt< NB_ELEM; cpt++) {
89 sprintf(buf,"%d",cpt);
91 xbt_assert2 (!strcmp(buf,s2),
92 "The retrieved value is not the same than the injected one (%s!=%s)",
100 INFO2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
101 d=xbt_dynar_new(sizeof(char*),&free_string);
102 for (cpt=0; cpt< NB_ELEM; cpt++) {
103 sprintf(buf,"%d",cpt);
105 xbt_dynar_push(d,&s1);
107 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
108 sprintf(buf,"%d",cpt);
110 xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
113 for (cpt=0; cpt< NB_ELEM/2; cpt++) {
114 sprintf(buf,"%d",cpt);
115 xbt_dynar_shift(d,&s2);
116 xbt_assert2(!strcmp(buf,s2),
117 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
121 for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
122 sprintf(buf,"%d",cpt);
123 xbt_dynar_shift(d,&s2);
124 xbt_assert2 (!strcmp(buf,s2),
125 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
129 for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
130 sprintf(buf,"%d",cpt);
131 xbt_dynar_shift(d,&s2);
132 xbt_assert2 (!strcmp(buf,s2),
133 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
141 INFO3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
142 d=xbt_dynar_new(sizeof(char*),&free_string);
143 for (cpt=0; cpt< NB_ELEM; cpt++) {
144 sprintf(buf,"%d",cpt);
146 xbt_dynar_push(d,&s1);
148 for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
149 sprintf(buf,"%d",cpt);
150 xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
151 xbt_assert2(!strcmp(buf,s2),
152 "Remove a bad value. Got %s, expected %s",