opkg_active_list_test: mark functions static, swap strcmp() with memcmp()
[oweals/opkg-lede.git] / tests / opkg_active_list_test.c
1 /* opkg_active_list.c - the opkg package management system
2
3    Tick Chen <tick@openmoko.com>
4
5    Copyright (C) 2008 Openmoko
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include <stdlib.h>
19 #include <libopkg/active_list.h>
20 #include <active_list.h>
21 #include <stdio.h>
22
23 struct active_test {
24         char *str;
25         struct active_list list;
26 };
27
28 static struct active_test *active_test_new(char *str)
29 {
30         struct active_test *ans =
31             (struct active_test *)calloc(1, sizeof(struct active_test));
32         ans->str = str;
33         active_list_init(&ans->list);
34         return ans;
35 }
36
37 static void active_test_add(struct active_list *head, struct active_test *node)
38 {
39         active_list_add(head, &node->list);
40 }
41
42 static void active_test_add_depend(struct active_test *A, struct active_test *B)
43 {
44         active_list_add_depend(&A->list, &B->list);
45 }
46
47 /*
48 .--A---B----C----D-----E----F
49     |             |__k---L
50     |                    |_ N
51     |__ G ---H ---I---J
52              |_M      |_O
53
54 Then the sequence will be
55 +: G M H I O J A B K N L C D E F
56 -: F E D C L N K B A J O I H M G
57 */
58 static void make_list(struct active_list *head)
59 {
60         struct active_test *A = active_test_new("A");
61         struct active_test *B = active_test_new("B");
62         struct active_test *C = active_test_new("C");
63         struct active_test *D = active_test_new("D");
64         struct active_test *E = active_test_new("E");
65         struct active_test *F = active_test_new("F");
66         struct active_test *G = active_test_new("G");
67         struct active_test *H = active_test_new("H");
68         struct active_test *I = active_test_new("I");
69         struct active_test *J = active_test_new("J");
70         struct active_test *K = active_test_new("K");
71         struct active_test *L = active_test_new("L");
72         struct active_test *M = active_test_new("M");
73         struct active_test *N = active_test_new("N");
74         struct active_test *O = active_test_new("O");
75
76         active_test_add(head, A);
77         active_test_add(head, B);
78         active_test_add(head, C);
79         active_test_add(head, D);
80         active_test_add(head, E);
81         active_test_add(head, F);
82         active_test_add(head, G);
83         active_test_add(head, H);
84         active_test_add(head, I);
85         active_test_add(head, J);
86         active_test_add(head, K);
87         active_test_add(head, L);
88         active_test_add(head, M);
89         active_test_add(head, N);
90         active_test_add(head, O);
91         active_test_add_depend(H, M);
92         active_test_add_depend(A, G);
93         active_test_add_depend(A, H);
94         active_test_add_depend(A, I);
95         active_test_add_depend(A, J);
96         active_test_add_depend(J, O);
97         active_test_add_depend(C, K);
98         active_test_add_depend(C, L);
99         active_test_add_depend(L, N);
100 }
101
102 static int active_test_compare(const void *a, const void *b)
103 {
104         struct active_list *first = (struct active_list *)a;
105         struct active_list *second = (struct active_list *)b;
106         return memcmp(list_entry(first, struct active_test, list),
107                       list_entry(second, struct active_test, list),
108                       sizeof(struct active_test));
109 }
110
111 static void show_list(struct active_list *head)
112 {
113         struct active_list *ptr;
114         struct active_test *test;
115         for (ptr = active_list_next(head, NULL); ptr;
116              ptr = active_list_next(head, ptr)) {
117                 test = list_entry(ptr, struct active_test, list);
118                 printf("%s ", test->str);
119         }
120         printf("\n");
121 }
122
123 int main(void)
124 {
125         struct active_list head;
126         struct active_list *ptr;
127         struct active_test *test;
128         active_list_init(&head);
129         make_list(&head);
130
131         printf("pos order: ");
132         show_list(&head);
133 /*    for(ptr = active_list_next(&head, &head); ptr ;ptr = active_list_next(&head, ptr)) {
134         test = list_entry(ptr, struct active_test, list);
135         printf ("%s ",test->str);
136     }*/
137         printf("neg order: ");
138         for (ptr = active_list_prev(&head, &head); ptr;
139              ptr = active_list_prev(&head, ptr)) {
140                 test = list_entry(ptr, struct active_test, list);
141                 printf("%s ", test->str);
142         }
143         printf("\npos order after sort: ");
144         active_list_sort(&head, &active_test_compare);
145         show_list(&head);
146
147         printf("after clear: ");
148         active_list_clear(&head);
149         for (ptr = active_list_next(&head, NULL); ptr;
150              ptr = active_list_next(&head, ptr)) {
151                 test = list_entry(ptr, struct active_test, list);
152                 printf("%s ", test->str);
153         }
154         printf("\n");
155
156 }