Debian Codegraph @

libmowgli-2/src/examples/listsort/listsort.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * listsort.c: Testing of the list sorting routine.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <mowgli.h>
35
36 #ifdef _WIN32
37 # define strcasecmp _stricmp
38 #endif
39
40 int
41 str_comparator(mowgli_node_t *n, mowgli_node_t *n2, void *opaque)
42 {
43 int ret;
44
45 ret = strcasecmp(n->data, n2->data);
46
47 return ret;
48 }
49
50 void
51 test_strings(void)
52 {
53 mowgli_list_t l = { NULL, NULL, 0 };
54 mowgli_node_t *n, *tn;
55
56 mowgli_node_add("foo", mowgli_node_create(), &l);
57 mowgli_node_add("bar", mowgli_node_create(), &l);
58 mowgli_node_add("baz", mowgli_node_create(), &l);
59 mowgli_node_add("splork", mowgli_node_create(), &l);
60 mowgli_node_add("rabbit", mowgli_node_create(), &l);
61 mowgli_node_add("meow", mowgli_node_create(), &l);
62 mowgli_node_add("hi", mowgli_node_create(), &l);
63 mowgli_node_add("konnichiwa", mowgli_node_create(), &l);
64 mowgli_node_add("absolutely", mowgli_node_create(), &l);
65 mowgli_node_add("cat", mowgli_node_create(), &l);
66 mowgli_node_add("dog", mowgli_node_create(), &l);
67 mowgli_node_add("woof", mowgli_node_create(), &l);
68 mowgli_node_add("moon", mowgli_node_create(), &l);
69 mowgli_node_add("new", mowgli_node_create(), &l);
70 mowgli_node_add("delete", mowgli_node_create(), &l);
71 mowgli_node_add("alias", mowgli_node_create(), &l);
72
73 mowgli_list_sort(&l, str_comparator, NULL);
74
75 printf("\nString test results\n");
76
77 MOWGLI_LIST_FOREACH_SAFE(n, tn, l.head)
78 {
79 printf(" %s\n", (char *) n->data);
80 mowgli_node_delete(n, &l);
81 }
82 }
83
84 int
85 int_comparator(mowgli_node_t *n, mowgli_node_t *n2, void *opaque)
86 {
87 long a = (long) n->data;
88 long b = (long) n2->data;
89
90 return a - b;
91 }
92
93 void
94 test_integers(void)
95 {
96 mowgli_list_t l = { NULL, NULL, 0 };
97 mowgli_node_t *n, *tn;
98
99 mowgli_node_add((void *) 3, mowgli_node_create(), &l);
100 mowgli_node_add((void *) 2, mowgli_node_create(), &l);
101 mowgli_node_add((void *) 4, mowgli_node_create(), &l);
102 mowgli_node_add((void *) 1, mowgli_node_create(), &l);
103
104 mowgli_list_sort(&l, int_comparator, NULL);
105
106 printf("\nInteger test results\n");
107
108 MOWGLI_LIST_FOREACH_SAFE(n, tn, l.head)
109 {
110 printf(" %ld\n", (long) n->data);
111 mowgli_node_delete(n, &l);
112 }
113 }
114
115 int
116 main(int argc, char *argv[])
117 {
118 test_strings();
119 test_integers();
120 return EXIT_SUCCESS;
121 }