|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. |
|
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 * Description: ?Description |
|
20 * |
|
21 */ |
|
22 |
|
23 |
|
24 #undef G_DISABLE_ASSERT |
|
25 #undef G_LOG_DOMAIN |
|
26 |
|
27 |
|
28 #include <stdio.h> |
|
29 #include <string.h> |
|
30 #include <glib.h> |
|
31 #include <fcntl.h> |
|
32 #include <goption.h> |
|
33 |
|
34 #ifdef SYMBIAN |
|
35 #include "mrt2_glib2_test.h" |
|
36 #endif /*SYMBIAN*/ |
|
37 |
|
38 #define C2P(c) ((gpointer) ((long) (c))) |
|
39 #define GINT_TO_POINTER(i) ((gpointer) (i)) |
|
40 #define GPOINTER_TO_INT(p) ((gint) (p)) |
|
41 #define TESTPASS 1 |
|
42 #define TESTFAIL 0 |
|
43 |
|
44 |
|
45 //Ascending |
|
46 gint compare_fun_gr(gconstpointer a,gconstpointer b) |
|
47 { |
|
48 return ((*(int *)a==*(int *)b)?0:((*(int *)a>*(int *)b)?1:-1)); |
|
49 } |
|
50 |
|
51 //Data |
|
52 gint compare_fun_gr_data(gconstpointer a,gconstpointer b,gpointer data) |
|
53 { |
|
54 return ((*(int *)a==*(int *)b)?0:((*(int *)a>*(int *)b)?1:-1)); |
|
55 } |
|
56 |
|
57 // Tests for slist |
|
58 void tg_slist_tests() |
|
59 { |
|
60 GSList *slist,*st,*rem; |
|
61 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
|
62 gint chk_buf[20]; |
|
63 gint i; |
|
64 gint g_slist_insert_data; |
|
65 gint g_slist_insert_before_data; |
|
66 gint ip1 = 10; |
|
67 gint ip2 = 15; |
|
68 gint ip3 = 5; |
|
69 gint ip4 = 12; |
|
70 gint g_slist_nth_data_op,g_slist_find_custom_op; |
|
71 |
|
72 //Trying to use the allocators so that even they get tested! |
|
73 GAllocator* alloc = g_allocator_new ("alloc_slist",5000); |
|
74 g_slist_push_allocator (alloc); |
|
75 |
|
76 |
|
77 slist = NULL; |
|
78 for (i = 0; i < 10; i++) |
|
79 slist = g_slist_append (slist, &nums[i]); |
|
80 |
|
81 //List looks like: |
|
82 // 0 1 2 3 4 5 6 7 8 9 |
|
83 |
|
84 //Test for g_slist_insert....inserted 10 at pos 4 |
|
85 g_slist_insert(slist,&ip1,4); |
|
86 st = g_slist_nth (slist,0); |
|
87 for(i = 0;i < 4;i++) |
|
88 st = st->next; |
|
89 g_slist_insert_data = *((gint*) st->data); |
|
90 g_assert(g_slist_insert_data == 10); |
|
91 |
|
92 /* for (i = 0; i < 10; i++) |
|
93 { |
|
94 st = g_slist_nth (slist, i); |
|
95 chk_buf[i] = *((gint*) st->data); |
|
96 }*/ |
|
97 |
|
98 //List looks like: |
|
99 // 0 1 2 3 10 4 5 6 7 8 9 |
|
100 |
|
101 //Test for g_slist_insert_before....inserted 15 at pos 7 |
|
102 st = g_slist_nth (slist,7); |
|
103 g_slist_insert_before(slist,st,&ip2); |
|
104 st = g_slist_nth (slist,0); |
|
105 for(i = 0;i < 7;i++) |
|
106 st = st->next; |
|
107 g_slist_insert_before_data = *((gint*) st->data); |
|
108 g_assert(g_slist_insert_before_data == 15); |
|
109 |
|
110 //List looks like: |
|
111 // 0 1 2 3 10 4 5 15 6 7 8 9 |
|
112 |
|
113 //Test for g_slist_index....finding 15 at pos 7 |
|
114 st = g_slist_nth (slist,0); |
|
115 g_assert(g_slist_index(st,&ip2)==7); |
|
116 |
|
117 //Test for g_slist_nth_data....getting 6 at position 8 |
|
118 g_slist_nth_data_op = *((gint*) g_slist_nth_data(slist,8)); |
|
119 g_assert(g_slist_nth_data_op == 6) ; |
|
120 |
|
121 //Test for g_slist_position |
|
122 st = g_slist_nth (slist,7); |
|
123 g_assert(g_slist_position (slist,st) == 7); |
|
124 |
|
125 //Test for g_slist_find_custom |
|
126 st = g_slist_find_custom(slist,&ip3,compare_fun_gr); |
|
127 g_slist_find_custom_op = *((gint*) st->data); |
|
128 g_assert(g_slist_find_custom_op == 5); |
|
129 |
|
130 //Test for g_slist_sort_with_data |
|
131 st = g_slist_sort_with_data(slist,compare_fun_gr_data,&ip3); |
|
132 for (i = 0; i < 10; i++) |
|
133 { |
|
134 st = g_slist_nth (slist, i); |
|
135 g_assert (*((gint*) st->data) == i); |
|
136 } |
|
137 |
|
138 //List looks like: |
|
139 // 0 1 2 3 4 5 6 7 8 9 10 15 |
|
140 |
|
141 //Test for g_slist_remove_link |
|
142 st = g_slist_nth (slist, 5); |
|
143 rem = g_slist_remove_link(slist , st); |
|
144 st = g_slist_nth (slist, 5); |
|
145 g_assert (*((gint*) st->data) == 6); |
|
146 |
|
147 //List looks like: |
|
148 // 0 1 2 3 4 6 7 8 9 10 15 |
|
149 |
|
150 //Test for g_slist_remove_all |
|
151 g_slist_insert(slist,&ip4,4); |
|
152 g_slist_insert(slist,&ip4,6); |
|
153 g_slist_insert(slist,&ip4,8); |
|
154 //List looks like: |
|
155 // 0 1 2 3 4 12 6 7 12 8 12 9 10 15 |
|
156 g_slist_remove_all(slist ,&ip4); |
|
157 |
|
158 g_slist_free (slist); |
|
159 g_slist_pop_allocator (); |
|
160 } |
|
161 |
|
162 |
|
163 int main (int argc,char *argv[]) |
|
164 { |
|
165 |
|
166 #ifdef SYMBIAN |
|
167 |
|
168 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
169 #endif /*SYMBIAN*/ |
|
170 |
|
171 tg_slist_tests(); |
|
172 #ifdef SYMBIAN |
|
173 testResultXml("tslist"); |
|
174 #endif /* EMULATOR */ |
|
175 return 0; |
|
176 } |