3 /* dynar_string: A test case for the dynar using strings as content */
5 /* Copyright (c) 2003, 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. */
10 #include <stdio.h> /* sprintf */
13 /* NB_ELEM HAS to be a multiple of 5 */
15 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
17 /* doxygen_first_cruft*/
19 /* The function we will use to free the data */
20 static void free_string(void *d){
24 int main(int argc,char *argv[]) {
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 /* Populate_str [doxygen cruft] */
44 /* 1. Populate the dynar */
45 for (cpt=0; cpt< NB_ELEM; cpt++) {
46 sprintf(buf,"%d",cpt);
48 xbt_dynar_push(d,&s1);
50 for (cpt=0; cpt< NB_ELEM; cpt++) {
51 sprintf(buf,"%d",cpt);
53 xbt_dynar_replace(d,cpt,&s1);
55 for (cpt=0; cpt< NB_ELEM; cpt++) {
56 sprintf(buf,"%d",cpt);
58 xbt_dynar_replace(d,cpt,&s1);
60 for (cpt=0; cpt< NB_ELEM; cpt++) {
61 sprintf(buf,"%d",cpt);
63 xbt_dynar_replace(d,cpt,&s1);
65 for (cpt=0; cpt< NB_ELEM; cpt++) {
66 sprintf(buf,"%d",cpt);
67 xbt_dynar_shift(d,&s2);
68 xbt_assert2 (!strcmp(buf,s2),
69 "The retrieved value is not the same than the injected one (%s!=%s)",
77 INFO1("==== Unshift, traverse and pop %d strings",NB_ELEM);
78 d=xbt_dynar_new(sizeof(char**),&free_string);
79 for (cpt=0; cpt< NB_ELEM; cpt++) {
80 sprintf(buf,"%d",cpt);
82 xbt_dynar_unshift(d,&s1);
84 /* 2. Traverse the dynar with the macro */
85 xbt_dynar_foreach(d,cpt,s1) {
86 sprintf(buf,"%d",NB_ELEM - cpt -1);
87 xbt_assert2 (!strcmp(buf,s1),
88 "The retrieved value is not the same than the injected one (%s!=%s)",
91 /* 3. Traverse the dynar with the macro */
92 for (cpt=0; cpt< NB_ELEM; cpt++) {
93 sprintf(buf,"%d",cpt);
95 xbt_assert2 (!strcmp(buf,s2),
96 "The retrieved value is not the same than the injected one (%s!=%s)",
100 /* 4. Free the resources */
105 INFO2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
106 d=xbt_dynar_new(sizeof(char*),&free_string);
107 for (cpt=0; cpt< NB_ELEM; cpt++) {
108 sprintf(buf,"%d",cpt);
110 xbt_dynar_push(d,&s1);
112 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
113 sprintf(buf,"%d",cpt);
115 xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
118 for (cpt=0; cpt< NB_ELEM/2; cpt++) {
119 sprintf(buf,"%d",cpt);
120 xbt_dynar_shift(d,&s2);
121 xbt_assert2(!strcmp(buf,s2),
122 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
126 for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
127 sprintf(buf,"%d",cpt);
128 xbt_dynar_shift(d,&s2);
129 xbt_assert2 (!strcmp(buf,s2),
130 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
134 for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
135 sprintf(buf,"%d",cpt);
136 xbt_dynar_shift(d,&s2);
137 xbt_assert2 (!strcmp(buf,s2),
138 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
146 INFO3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
147 d=xbt_dynar_new(sizeof(char*),&free_string);
148 for (cpt=0; cpt< NB_ELEM; cpt++) {
149 sprintf(buf,"%d",cpt);
151 xbt_dynar_push(d,&s1);
153 for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
154 sprintf(buf,"%d",cpt);
155 xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
156 xbt_assert2(!strcmp(buf,s2),
157 "Remove a bad value. Got %s, expected %s",