Remove stray exit() in the regress test.
[oweals/opkg-lede.git] / tests / opkg_active_list_test.c
index ec6bec1d7ebace960e9a44f72fc43d2e309d32f0..b6af3b30e78fc86141fafc577febcec46a3fff0c 100644 (file)
@@ -1,28 +1,35 @@
+/* opkg_active_list.c - the opkg package management system
 
 
-/*
-.--A---B----C----D-----E----F
-    |             |__k---L
-    |                    |_ N
-    |__ G ---H ---I---J
-             |_M      |_O
+   Tick Chen <tick@openmoko.com>
+
+   Copyright (C) 2008 Openmoko
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2, or (at
+   your option) any later version.
 
 
-Then the sequence will be 
-G M H I O J A B K N L C D E F
+   This program 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
+   General Public License for more details.
 */
 
 */
 
+
 #include <stdlib.h>
 #include <libopkg/active_list.h>
 #include <stdlib.h>
 #include <libopkg/active_list.h>
+#include <active_list.h>
 #include <stdio.h>
 
 struct active_test {
     char *str;
     struct active_list list;
 #include <stdio.h>
 
 struct active_test {
     char *str;
     struct active_list list;
-} __attribute__((packed));
+};
 
 struct active_test *active_test_new(char *str) {
     struct active_test *ans = (struct active_test *)calloc(1, sizeof(struct active_test));
     ans->str = str;
 
 struct active_test *active_test_new(char *str) {
     struct active_test *ans = (struct active_test *)calloc(1, sizeof(struct active_test));
     ans->str = str;
-    active_list_init(&ans->list); 
+    active_list_init(&ans->list);
     return ans;
 }
 void active_test_add(struct active_list *head, struct active_test *node) {
     return ans;
 }
 void active_test_add(struct active_list *head, struct active_test *node) {
@@ -33,6 +40,17 @@ void active_test_add_depend(struct active_test *A, struct active_test *B) {
     active_list_add_depend(&A->list, &B->list);
 }
 
     active_list_add_depend(&A->list, &B->list);
 }
 
+/*
+.--A---B----C----D-----E----F
+    |             |__k---L
+    |                    |_ N
+    |__ G ---H ---I---J
+             |_M      |_O
+
+Then the sequence will be
++: G M H I O J A B K N L C D E F
+-: F E D C L N K B A J O I H M G
+*/
 void make_list(struct active_list *head) {
     struct active_test *A = active_test_new("A");
     struct active_test *B = active_test_new("B");
 void make_list(struct active_list *head) {
     struct active_test *A = active_test_new("A");
     struct active_test *B = active_test_new("B");
@@ -72,10 +90,27 @@ void make_list(struct active_list *head) {
     active_test_add_depend(A, J);
     active_test_add_depend(J, O);
     active_test_add_depend(C, K);
     active_test_add_depend(A, J);
     active_test_add_depend(J, O);
     active_test_add_depend(C, K);
-    active_test_add_depend(C, L); 
+    active_test_add_depend(C, L);
     active_test_add_depend(L, N);
 }
 
     active_test_add_depend(L, N);
 }
 
+int active_test_compare(const void *a, const void *b) {
+    struct active_list *first = (struct active_list *)a;
+    struct active_list *second = (struct active_list *)b;
+    return strcmp(list_entry(first, struct active_test, list),
+            list_entry(second, struct active_test, list));
+}
+
+void show_list(struct active_list *head) {
+    struct active_list *ptr;
+    struct active_test *test;
+    for(ptr = active_list_next(head, NULL); ptr ;ptr = active_list_next(head, ptr)) {
+        test = list_entry(ptr, struct active_test, list);
+        printf ("%s ",test->str);
+    }
+    printf("\n");
+}
+
 int main (void) {
     struct active_list head;
     struct active_list *ptr;
 int main (void) {
     struct active_list head;
     struct active_list *ptr;
@@ -83,17 +118,24 @@ int main (void) {
     active_list_init(&head);
     make_list(&head);
 
     active_list_init(&head);
     make_list(&head);
 
-    for(ptr = active_list_next(&head, &head); ptr ;ptr = active_list_next(&head, ptr)) {
+    printf("pos order: ");
+    show_list(&head);
+/*    for(ptr = active_list_next(&head, &head); ptr ;ptr = active_list_next(&head, ptr)) {
         test = list_entry(ptr, struct active_test, list);
         printf ("%s ",test->str);
         test = list_entry(ptr, struct active_test, list);
         printf ("%s ",test->str);
-    }
-    printf("\n");
-    for(ptr = active_list_next(&head, &head); ptr ;ptr = active_list_next(&head, ptr)) {
+    }*/
+    printf("neg order: ");
+    for(ptr = active_list_prev(&head, &head); ptr ;ptr = active_list_prev(&head, ptr)) {
         test = list_entry(ptr, struct active_test, list);
         printf ("%s ",test->str);
     }
         test = list_entry(ptr, struct active_test, list);
         printf ("%s ",test->str);
     }
-    printf("\n");
-    for(ptr = active_list_next(&head, &head); ptr ;ptr = active_list_next(&head, ptr)) {
+    printf("\npos order after sort: ");
+    active_list_sort(&head, &active_test_compare);
+    show_list(&head);
+
+    printf("after clear: ");
+    active_list_clear(&head);
+    for(ptr = active_list_next(&head, NULL); ptr ;ptr = active_list_next(&head, ptr)) {
         test = list_entry(ptr, struct active_test, list);
         printf ("%s ",test->str);
     }
         test = list_entry(ptr, struct active_test, list);
         printf ("%s ",test->str);
     }