summaryrefslogtreecommitdiff
path: root/test/dlist_test.c
blob: ddd0ae8d67867bc7c2739e646bf71ec73beda53f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * dlist_test.c
 *
 * Copyright (C) 2003 Eric J Bohm
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */


/* Double linked list implementation tester.
 */
#include "dlist.h"
#include <stdio.h>
#include <string.h>
// create some dlists put nodes in and out
// use dump
// try list with del_func and without
// use insert, unshift, push
// use pop push mark
// use prev next

typedef struct simple {
  char label[80];
  int number;
} Simple;


typedef struct complex {
  int cnumber;
  Simple *sthing;
} Complex;

void complex_silly_multiply_by_two( void *a);
int complex_equal(void *a, void *b);
int complex_less(void *a, void *b);
int complex_greater(void *a, void *b);
int complex_comp(void *a, void *b);
void simple_dump(Dlist *);
void simple_dump_rev(Dlist *);
void complex_dump(Dlist *);
void complex_dump_rev(Dlist *);
void complex_out(void *);
void complex_del(void *);
Complex *complex_maker(int,int,char *);

Simple *simple_maker(int ,char *);

int main (int argc,char *argv[])
{
  Dlist *list;
  Simple *s1,*s2,*s3,*stemp;
  Complex *c1,*c2,*c3, *c4, *ctemp, *cfound;
  while(1)
    {
	s1=simple_maker(1,"one");
	s2=simple_maker(2,"two");
	s3=simple_maker(3,"three");
      if((list=dlist_new(sizeof(Simple)))==NULL)	
	{
	   fprintf(stderr,"ERR dlist_new fail\n");
	  return(2);
	 }
      dlist_push(list,s1);
      dlist_push(list,s2);
      dlist_push(list,s3);
      printf("count is %ld\n",list->count);
      simple_dump(list);
      simple_dump_rev(list);
      stemp=dlist_pop(list);
      printf("popped %s\n",stemp->label);
      simple_dump(list);
      printf("pushed\n");
      dlist_push(list,s3);
      simple_dump(list);
      stemp=dlist_shift(list);
      printf("shifted %s\n",stemp->label);
      simple_dump(list);
      printf("unshifted\n");
      dlist_unshift(list,stemp);
      simple_dump(list);
      dlist_destroy(list);
      c1=complex_maker(1,1,"one");
      c2=complex_maker(2,2,"two");
      c3=complex_maker(3,3,"three");
      if((list=dlist_new_with_delete(sizeof(Complex),complex_del))==NULL)
	{
		fprintf(stderr,"ERR dlist_new fail\n");
		return(2);
	}
	if(dlist_insert_sorted(list,c1,complex_less)==NULL)
	{
		fprintf(stderr,"ERR dlist_insert fail\n");
		return(2);
	}
	printf("sorted insert 1\n");
	if(dlist_insert_sorted(list,c3,complex_less)==NULL)
	{
		fprintf(stderr,"ERR dlist_insert fail\n");
		return(2);
	}
	if(dlist_insert_sorted(list,c2,complex_less)==NULL)
	{
		fprintf(stderr,"ERR dlist_insert fail\n");
		return(2);
	}
	printf("sorted insert 2\n");
	printf("ascending sorted output\n");
	complex_dump(list);
	dlist_transform(list,complex_silly_multiply_by_two);
	printf("transform multi by 2 output\n");
	complex_dump(list);
	ctemp=complex_maker(6,6,"three");
	if((cfound=(Complex *) dlist_find_custom(list,ctemp,complex_equal))!=NULL)

	{
		printf("found %d as %d in list\n",ctemp->cnumber,cfound->cnumber);
	} else {
		printf("ERROR find failed on %d \n",ctemp->cnumber);
		return(3);
	}
	complex_del(ctemp);
	dlist_destroy(list);
	c1=complex_maker(1,1,"one");
	c2=complex_maker(2,2,"two");
	c3=complex_maker(3,3,"three");
      if((list=dlist_new_with_delete(sizeof(Complex),complex_del))==NULL)
	{
	   fprintf(stderr,"ERR dlist_new fail\n");
	  return(2);
	 }
	if(dlist_insert_sorted(list,c1,complex_greater)==NULL)
	{
	  fprintf(stderr,"ERR dlist_insert fail\n");
	  return(2);
	}
	printf("greater sorted insert 1\n");
	if(dlist_insert_sorted(list,c3,complex_greater)==NULL)
	{
	   fprintf(stderr,"ERR dlist_insert fail\n");
	   return(2);
	 }
	printf("greater sorted insert 3\n");
	if(dlist_insert_sorted(list,c2,complex_greater)==NULL)
	{
	   fprintf(stderr,"ERR dlist_insert fail\n");
	   return(2);
	 }
	printf("greater sorted insert 2\n");
	printf("descending sorted output using transform\n");
	dlist_transform(list,complex_out);
      dlist_destroy(list);
      c1=complex_maker(1,1,"one");
      c2=complex_maker(2,2,"two");
      c3=complex_maker(3,3,"three");
      c4=complex_maker(4,4,"four");
      if((list=dlist_new_with_delete(sizeof(Complex),complex_del))==NULL)
	{
	  fprintf(stderr,"ERR dlist_new fail\n");
	  return(2);
	}
      dlist_push(list,c2);
      dlist_push(list,c1);
      dlist_push(list,c4);
      dlist_push(list,c3);
      printf("unsorted custom\n");
      complex_dump(list);
      printf("unsorted custom reversed\n");
      complex_dump_rev(list);
      dlist_sort_custom(list,complex_comp);
      printf("custom sorted output\n");
      complex_dump(list);
      dlist_destroy(list);
    }
  return(0);
}

void simple_dump (Dlist *list)
{
  Simple *thisone;
  printf("count  %ld \n",list->count);
  dlist_for_each_data(list,thisone,Simple)
    {
      printf("label %s number %d \n",thisone->label,thisone->number);
    }

}
void simple_dump_rev (Dlist *list)
{
  Simple *thisone;
  printf("rev count  %ld \n",list->count);
  dlist_for_each_data_rev(list,thisone,Simple)
    {
      printf("label %s number %d \n",thisone->label,thisone->number);
    }
}

Simple * simple_maker(int snumber,char *label)
{
	Simple *stemp;
	if((stemp=malloc(sizeof(Simple)))==NULL)
	{
		fprintf(stderr,"ERR malloc fail\n");
		return(NULL);
	}
	stemp->number=snumber;
	strcpy(stemp->label,label);
	return(stemp);
}

Complex * complex_maker(int cnumber, int snumber, char* label)
{
	Complex *ctemp;
	if((ctemp=malloc(sizeof(Complex)))==NULL)
	{
		fprintf(stderr,"ERR malloc fail\n");
		return(NULL);
	}
	ctemp->cnumber=cnumber;
	ctemp->sthing=simple_maker(snumber,label);
	return(ctemp);
}

void complex_out(void *data)
{
	Complex *thisone=(Complex *)data;
	printf("cnumber %d label %s number %d \n",thisone->cnumber,thisone->sthing->label,thisone->sthing->number);
}

/**
 * return 1 if a==b, else 0
 */
int complex_equal(void *a, void *b)
{
	if((((Complex *)a)->cnumber==((Complex *)b)->cnumber)
	    && (((Complex *)a)->sthing->number==
		((Complex *)b)->sthing->number)
	    &&  strcmp(((Complex *)a)->sthing->label,
		((Complex *)b)->sthing->label)==0)
		return(1);
	return(0);
}

/** for sorting
 * return 1 if a<b, else 0
 */
int complex_less(void *a, void *b)
{
	return( ((Complex *)a)->cnumber <  ((Complex *)b)->cnumber );
}

/** for sorting
 * return 1 if a>b, else 0
 */
int complex_greater(void *a, void *b)
{
	return( ((Complex *)a)->cnumber >  ((Complex *)b)->cnumber );
}

int complex_comp(void *a, void *b)
{
	return( ((Complex *)a)->cnumber -  ((Complex *)b)->cnumber );
}

void complex_silly_multiply_by_two( void *a)
{
	((Complex *)a)->cnumber=((Complex *)a)->cnumber*2;
	((Complex *)a)->sthing->number=((Complex *)a)->sthing->number*2;
}

void complex_dump (Dlist *list)
{
  Complex *thisone;
  dlist_start(list);
  printf("count  %ld \n",list->count);
  dlist_for_each_data(list,thisone,Complex)
    {
      printf("cnumber %d label %s number %d \n",thisone->cnumber,thisone->sthing->label,thisone->sthing->number);
    }

}

void complex_dump_rev (Dlist *list)
{
  Complex *thisone;
  dlist_start(list);
  printf("count  %ld \n",list->count);
  dlist_for_each_data_rev(list,thisone,Complex)
    {
      printf("cnumber %d label %s number %d \n",thisone->cnumber,thisone->sthing->label,thisone->sthing->number);
    }

}

void complex_del (void *item)
{
  Complex *corpse=item;
  printf("freeing complex\n");
  free(corpse->sthing);
  free(corpse);
}