Tizen 2.1 base
[platform/upstream/glib2.0.git] / glib / tests / sort.c
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 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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21
22 static int
23 int_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
24 {
25   const gint *i1 = p1;
26   const gint *i2 = p2;
27
28   return *i1 - *i2;
29 }
30
31 static void
32 test_sort_basic (void)
33 {
34   gint *data;
35   gint i;
36
37   data = g_malloc (10000 * sizeof (int));
38   for (i = 0; i < 10000; i++)
39     {
40       data[i] = g_random_int_range (0, 10000);
41     }
42
43   g_qsort_with_data (data, 10000, sizeof (int), int_compare_data, NULL);
44
45   for (i = 1; i < 10000; i++)
46     g_assert_cmpint (data[i -1], <=, data[i]);
47
48   g_free (data);
49 }
50
51 typedef struct {
52   int val;
53   int i;
54 } SortItem;
55
56 typedef struct {
57   int val;
58   int i;
59   int data[16];
60 } BigItem;
61
62 static int
63 item_compare_data (gconstpointer p1, gconstpointer p2, gpointer data)
64 {
65   const SortItem *i1 = p1;
66   const SortItem *i2 = p2;
67
68   return i1->val - i2->val;
69 }
70
71 static void
72 test_sort_stable (void)
73 {
74   SortItem *data;
75   gint i;
76
77   data = g_malloc (10000 * sizeof (SortItem));
78   for (i = 0; i < 10000; i++)
79     {
80       data[i].val = g_random_int_range (0, 10000);
81       data[i].i = i;
82     }
83
84   g_qsort_with_data (data, 10000, sizeof (SortItem), item_compare_data, NULL);
85
86   for (i = 1; i < 10000; i++)
87     {
88       g_assert_cmpint (data[i -1].val, <=, data[i].val);
89       if (data[i -1].val == data[i].val)
90         g_assert_cmpint (data[i -1].i, <, data[i].i);
91     }
92   g_free (data);
93 }
94
95 static void
96 test_sort_big (void)
97 {
98   BigItem *data;
99   gint i;
100
101   data = g_malloc (10000 * sizeof (BigItem));
102   for (i = 0; i < 10000; i++)
103     {
104       data[i].val = g_random_int_range (0, 10000);
105       data[i].i = i;
106     }
107
108   g_qsort_with_data (data, 10000, sizeof (BigItem), item_compare_data, NULL);
109
110   for (i = 1; i < 10000; i++)
111     {
112       g_assert_cmpint (data[i -1].val, <=, data[i].val);
113       if (data[i -1].val == data[i].val)
114         g_assert_cmpint (data[i -1].i, <, data[i].i);
115     }
116   g_free (data);
117 }
118
119 int
120 main (int argc, char *argv[])
121 {
122   g_test_init (&argc, &argv, NULL);
123
124   g_test_add_func ("/sort/basic", test_sort_basic);
125   g_test_add_func ("/sort/stable", test_sort_stable);
126   g_test_add_func ("/sort/big", test_sort_big);
127
128   return g_test_run ();
129 }
130