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[]) {
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 /* Populate_str [doxygen cruft] */
45 /* 1. Populate the dynar */
46 for (cpt=0; cpt< NB_ELEM; cpt++) {
47 sprintf(buf,"%d",cpt);
49 xbt_dynar_push(d,&s1);
51 for (cpt=0; cpt< NB_ELEM; cpt++) {
52 sprintf(buf,"%d",cpt);
54 xbt_dynar_replace(d,cpt,&s1);
56 for (cpt=0; cpt< NB_ELEM; cpt++) {
57 sprintf(buf,"%d",cpt);
59 xbt_dynar_replace(d,cpt,&s1);
61 for (cpt=0; cpt< NB_ELEM; cpt++) {
62 sprintf(buf,"%d",cpt);
64 xbt_dynar_replace(d,cpt,&s1);
66 for (cpt=0; cpt< NB_ELEM; cpt++) {
67 sprintf(buf,"%d",cpt);
68 xbt_dynar_shift(d,&s2);
69 xbt_assert2 (!strcmp(buf,s2),
70 "The retrieved value is not the same than the injected one (%s!=%s)",
78 INFO1("==== Unshift, traverse and pop %d strings",NB_ELEM);
79 d=xbt_dynar_new(sizeof(char**),&free_string);
80 for (cpt=0; cpt< NB_ELEM; cpt++) {
81 sprintf(buf,"%d",cpt);
83 xbt_dynar_unshift(d,&s1);
85 /* 2. Traverse the dynar with the macro */
86 xbt_dynar_foreach(d,cpt,s1) {
87 sprintf(buf,"%d",NB_ELEM - cpt -1);
88 xbt_assert2 (!strcmp(buf,s1),
89 "The retrieved value is not the same than the injected one (%s!=%s)",
92 /* 3. Traverse the dynar with the macro */
93 for (cpt=0; cpt< NB_ELEM; cpt++) {
94 sprintf(buf,"%d",cpt);
96 xbt_assert2 (!strcmp(buf,s2),
97 "The retrieved value is not the same than the injected one (%s!=%s)",
101 /* 4. Free the resources */
106 INFO2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
107 d=xbt_dynar_new(sizeof(char*),&free_string);
108 for (cpt=0; cpt< NB_ELEM; cpt++) {
109 sprintf(buf,"%d",cpt);
111 xbt_dynar_push(d,&s1);
113 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
114 sprintf(buf,"%d",cpt);
116 xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
119 for (cpt=0; cpt< NB_ELEM/2; cpt++) {
120 sprintf(buf,"%d",cpt);
121 xbt_dynar_shift(d,&s2);
122 xbt_assert2(!strcmp(buf,s2),
123 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
127 for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
128 sprintf(buf,"%d",cpt);
129 xbt_dynar_shift(d,&s2);
130 xbt_assert2 (!strcmp(buf,s2),
131 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
135 for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
136 sprintf(buf,"%d",cpt);
137 xbt_dynar_shift(d,&s2);
138 xbt_assert2 (!strcmp(buf,s2),
139 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
147 INFO3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
148 d=xbt_dynar_new(sizeof(char*),&free_string);
149 for (cpt=0; cpt< NB_ELEM; cpt++) {
150 sprintf(buf,"%d",cpt);
152 xbt_dynar_push(d,&s1);
154 for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
155 sprintf(buf,"%d",cpt);
156 xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
157 xbt_assert2(!strcmp(buf,s2),
158 "Remove a bad value. Got %s, expected %s",