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 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
18 void free_string(void *d);
20 void free_string(void *d){
24 int main(int argc,char *argv[]) {
31 xbt_init_defaultlog(&argc,argv,"dynar.thresh=debug");
33 INFO0("==== Traverse the empty dynar");
34 d=xbt_dynar_new(sizeof(char *),&free_string);
35 xbt_dynar_foreach(d,cpt,s1){
37 "Damnit, there is something in the empty dynar");
42 INFO1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
43 d=xbt_dynar_new(sizeof(char*),&free_string);
44 for (cpt=0; cpt< NB_ELEM; cpt++) {
45 sprintf(buf,"%d",cpt);
47 xbt_dynar_push(d,&s1);
49 for (cpt=0; cpt< NB_ELEM; cpt++) {
50 sprintf(buf,"%d",cpt);
52 xbt_dynar_replace(d,cpt,&s1);
54 for (cpt=0; cpt< NB_ELEM; cpt++) {
55 sprintf(buf,"%d",cpt);
57 xbt_dynar_replace(d,cpt,&s1);
59 for (cpt=0; cpt< NB_ELEM; cpt++) {
60 sprintf(buf,"%d",cpt);
62 xbt_dynar_replace(d,cpt,&s1);
64 for (cpt=0; cpt< NB_ELEM; cpt++) {
65 sprintf(buf,"%d",cpt);
66 xbt_dynar_shift(d,&s2);
67 xbt_assert2 (!strcmp(buf,s2),
68 "The retrieved value is not the same than the injected one (%s!=%s)",
76 INFO1("==== Unshift, traverse and pop %d strings",NB_ELEM);
77 d=xbt_dynar_new(sizeof(char**),&free_string);
78 for (cpt=0; cpt< NB_ELEM; cpt++) {
79 sprintf(buf,"%d",cpt);
81 xbt_dynar_unshift(d,&s1);
83 xbt_dynar_foreach(d,cpt,s1) {
84 sprintf(buf,"%d",NB_ELEM - cpt -1);
85 xbt_assert2 (!strcmp(buf,s1),
86 "The retrieved value is not the same than the injected one (%s!=%s)",
89 for (cpt=0; cpt< NB_ELEM; cpt++) {
90 sprintf(buf,"%d",cpt);
92 xbt_assert2 (!strcmp(buf,s2),
93 "The retrieved value is not the same than the injected one (%s!=%s)",
101 INFO2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
102 d=xbt_dynar_new(sizeof(char*),&free_string);
103 for (cpt=0; cpt< NB_ELEM; cpt++) {
104 sprintf(buf,"%d",cpt);
106 xbt_dynar_push(d,&s1);
108 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
109 sprintf(buf,"%d",cpt);
111 xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
114 for (cpt=0; cpt< NB_ELEM/2; cpt++) {
115 sprintf(buf,"%d",cpt);
116 xbt_dynar_shift(d,&s2);
117 xbt_assert2(!strcmp(buf,s2),
118 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
122 for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
123 sprintf(buf,"%d",cpt);
124 xbt_dynar_shift(d,&s2);
125 xbt_assert2 (!strcmp(buf,s2),
126 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
130 for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
131 sprintf(buf,"%d",cpt);
132 xbt_dynar_shift(d,&s2);
133 xbt_assert2 (!strcmp(buf,s2),
134 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
142 INFO3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
143 d=xbt_dynar_new(sizeof(char*),&free_string);
144 for (cpt=0; cpt< NB_ELEM; cpt++) {
145 sprintf(buf,"%d",cpt);
147 xbt_dynar_push(d,&s1);
149 for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
150 sprintf(buf,"%d",cpt);
151 xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
152 xbt_assert2(!strcmp(buf,s2),
153 "Remove a bad value. Got %s, expected %s",