add .gitignore
[tedtools.git] / glisttest.c
1 /*
2  * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *      may be used to endorse or promote products derived from this software
15  *      without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34
35
36 #include "tmalloc.h"
37
38 #include "glist.h"
39
40 static void
41 dumpGList(GList *l, char *str) {
42         int i=0;
43         GListCell *c;
44
45         printf("%s: GLIST_LENGTH(%d)\n", str, GLIST_LENGTH(l));
46         GListForeach(c,l)
47                 printf("\t%d\t%s\n", i++, (char*)GLCELL_DATA(c));
48 }
49
50 static GList*
51 fillF() {
52         int i;
53         char    buf[1024];
54         GList   *l = NULL;
55
56         for(i=0;i<16;i++) {
57                 sprintf(buf,":%d:",i);
58                 l = GListPush(l, tstrdup(buf));
59         }
60
61         l->cmpFunc = (GListCellComparator)strcmp;
62         l->freeFunc = (GListDataFreeFunc)tfree;
63         return l;
64 }
65
66 static GList*
67 fillB() {
68         int i;
69         char    buf[1024];
70         GList   *l = NULL;
71
72         for(i=0;i<20;i+=2) {
73                 sprintf(buf,":%d:",i);
74                 l = GListUnshift(l, tstrdup(buf));
75         }
76
77         l->cmpFunc = (GListCellComparator)strcmp;
78         l->freeFunc = (GListDataFreeFunc)tfree;
79         return l;
80 }
81
82
83 int
84 main(int argn, char *argv[]) {
85         GList   *l;
86         GListCell       *c;
87
88         dumpGList(fillF(), "Forward");
89         dumpGList(fillB(), "Backward");
90                 
91         dumpGList(GListTruncate(fillF(), 3), "Truncate");
92         dumpGList(GListConcat(fillF(), fillB()), "Concat");
93         dumpGList(GListDifference(fillF(), fillB()), "Difference");
94         dumpGList(GListUnion(fillF(), fillB()), "Union");
95         dumpGList(GListIntersect(fillF(), fillB()), "Intersect");
96
97         dumpGList(GListSort(fillB()), "Sort");
98         dumpGList(GListUniq(GListSort(GListConcat(fillF(), fillB()))), "Sort + Uniq + Concat");
99
100         l = fillB();
101         c = GListFind(l,":10:");
102         printf("Find: %s\n", (char*)GLCELL_DATA(c));
103         dumpGList(GListInsert(l,c,"|11|"),"Insert");
104         dumpGList(GListDelete(l,c),"Delete :10:");
105         printf("Get #5: %s\n", (char*)GLCELL_DATA(GListGet(l,5)));
106         printf("Backward scan:\n");
107         GListForeachBack(c,l)
108                 printf("\t%s\n", (char*)GLCELL_DATA(c));
109
110
111         printf("Shift:\n");
112         while( (c=GListShift(l))!=NULL )
113                 printf("\t%s\n", (char*)GLCELL_DATA(c));
114
115         l = fillB();
116         printf("Pop:\n");
117         while( (c=GListPop(l))!=NULL )
118                 printf("\t%s\n", (char*)GLCELL_DATA(c));
119
120         GListFree(fillF());
121
122         return 0;
123 }
124
125