2 * Copyright (C) 2011 Red Hat, Inc.
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 of the License, or (at your option) any later version.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 int_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
32 test_sort_basic (void)
37 data = g_malloc (10000 * sizeof (int));
38 for (i = 0; i < 10000; i++)
40 data[i] = g_random_int_range (0, 10000);
43 g_qsort_with_data (data, 10000, sizeof (int), int_compare_data, NULL);
45 for (i = 1; i < 10000; i++)
46 g_assert_cmpint (data[i -1], <=, data[i]);
57 item_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
59 const SortItem *i1 = p1;
60 const SortItem *i2 = p2;
62 return i1->val - i2->val;
66 test_sort_stable (void)
71 data = g_malloc (10000 * sizeof (SortItem));
72 for (i = 0; i < 10000; i++)
74 data[i].val = g_random_int_range (0, 10000);
78 g_qsort_with_data (data, 10000, sizeof (SortItem), item_compare_data, NULL);
80 for (i = 1; i < 10000; i++)
82 g_assert_cmpint (data[i -1].val, <=, data[i].val);
83 if (data[i -1].val == data[i].val)
84 g_assert_cmpint (data[i -1].i, <, data[i].i);
90 main (int argc, char *argv[])
92 g_test_init (&argc, &argv, NULL);
94 g_test_add_func ("/sort/basic", test_sort_basic);
95 g_test_add_func ("/sort/stable", test_sort_stable);