1/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <glib.h>
19
20static int
21int_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
22{
23 const gint *i1 = p1;
24 const gint *i2 = p2;
25
26 return *i1 - *i2;
27}
28
29static void
30test_sort_basic (void)
31{
32 gint *data;
33 gint i;
34
35 data = g_malloc (n_bytes: 10000 * sizeof (int));
36 for (i = 0; i < 10000; i++)
37 {
38 data[i] = g_random_int_range (begin: 0, end: 10000);
39 }
40
41 g_qsort_with_data (pbase: data, total_elems: 10000, size: sizeof (int), compare_func: int_compare_data, NULL);
42
43 for (i = 1; i < 10000; i++)
44 g_assert_cmpint (data[i -1], <=, data[i]);
45
46 g_free (mem: data);
47}
48
49typedef struct {
50 int val;
51 int i;
52} SortItem;
53
54typedef struct {
55 int val;
56 int i;
57 int data[16];
58} BigItem;
59
60static int
61item_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
62{
63 const SortItem *i1 = p1;
64 const SortItem *i2 = p2;
65
66 return i1->val - i2->val;
67}
68
69static void
70test_sort_stable (void)
71{
72 SortItem *data;
73 gint i;
74
75 data = g_malloc (n_bytes: 10000 * sizeof (SortItem));
76 for (i = 0; i < 10000; i++)
77 {
78 data[i].val = g_random_int_range (begin: 0, end: 10000);
79 data[i].i = i;
80 }
81
82 g_qsort_with_data (pbase: data, total_elems: 10000, size: sizeof (SortItem), compare_func: item_compare_data, NULL);
83
84 for (i = 1; i < 10000; i++)
85 {
86 g_assert_cmpint (data[i -1].val, <=, data[i].val);
87 if (data[i -1].val == data[i].val)
88 g_assert_cmpint (data[i -1].i, <, data[i].i);
89 }
90 g_free (mem: data);
91}
92
93static void
94test_sort_big (void)
95{
96 BigItem *data;
97 gint i;
98
99 data = g_malloc (n_bytes: 10000 * sizeof (BigItem));
100 for (i = 0; i < 10000; i++)
101 {
102 data[i].val = g_random_int_range (begin: 0, end: 10000);
103 data[i].i = i;
104 }
105
106 g_qsort_with_data (pbase: data, total_elems: 10000, size: sizeof (BigItem), compare_func: item_compare_data, NULL);
107
108 for (i = 1; i < 10000; i++)
109 {
110 g_assert_cmpint (data[i -1].val, <=, data[i].val);
111 if (data[i -1].val == data[i].val)
112 g_assert_cmpint (data[i -1].i, <, data[i].i);
113 }
114 g_free (mem: data);
115}
116
117int
118main (int argc, char *argv[])
119{
120 g_test_init (argc: &argc, argv: &argv, NULL);
121
122 g_test_add_func (testpath: "/sort/basic", test_func: test_sort_basic);
123 g_test_add_func (testpath: "/sort/stable", test_func: test_sort_stable);
124 g_test_add_func (testpath: "/sort/big", test_func: test_sort_big);
125
126 return g_test_run ();
127}
128

source code of gtk/subprojects/glib/glib/tests/sort.c