50
|
1 |
// Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
//
|
|
15 |
|
|
16 |
|
72
|
17 |
#include <sys/stat.h>
|
|
18 |
|
50
|
19 |
#undef G_DISABLE_ASSERT
|
|
20 |
#undef G_LOG_DOMAIN
|
|
21 |
|
|
22 |
#include <stdio.h>
|
|
23 |
#include <glib.h>
|
72
|
24 |
#include <glib/gprintf.h>
|
|
25 |
#ifdef __SYMBIAN32__
|
|
26 |
#include "mrt2_glib2_test.h"
|
|
27 |
#endif /*__SYMBIAN32__*/
|
50
|
28 |
|
|
29 |
static gint psort (gconstpointer a, gconstpointer b)
|
|
30 |
{
|
|
31 |
if(**(guint32**)a == **(guint32**)b)
|
|
32 |
return 0;
|
|
33 |
else
|
|
34 |
return **(guint32**)a < **(guint32**)b ? -1 : 1;
|
|
35 |
}
|
|
36 |
|
|
37 |
|
|
38 |
static gint psort_userdata (gconstpointer a, gconstpointer b, gpointer user_data)
|
|
39 |
{
|
|
40 |
if(**(guint32**)a == **(guint32**)b)
|
|
41 |
return 0;
|
|
42 |
else
|
|
43 |
return **(guint32**)a < **(guint32**)b ? -1 : 1;
|
|
44 |
}
|
|
45 |
|
|
46 |
gboolean compare_pointer_array(GPtrArray *parray1, gint* array2, gint size )
|
|
47 |
{
|
|
48 |
|
|
49 |
int i;
|
|
50 |
int* val;
|
|
51 |
if ( size != parray1->len)
|
|
52 |
return FALSE;
|
|
53 |
for ( i = 0; i < size ; i++)
|
|
54 |
{
|
|
55 |
val = (int*)g_ptr_array_index(parray1, i) ;
|
|
56 |
if(val == NULL)
|
|
57 |
return FALSE;
|
|
58 |
if ( *val != array2[i])
|
|
59 |
return FALSE;
|
|
60 |
}
|
|
61 |
|
|
62 |
return TRUE;
|
|
63 |
|
|
64 |
|
|
65 |
}
|
|
66 |
void test_pointer_array_remove_range()
|
|
67 |
{
|
|
68 |
GPtrArray *gparray;
|
|
69 |
const gint ARRAY_SIZE = 15;
|
|
70 |
const gint ARRAY_SIZE_AFTER_REMOVE_RANGE = 12; /*removing 3 elements starting from index 3*/
|
|
71 |
|
|
72 |
gint array[ARRAY_SIZE]= {99,88,77,33,44,11,66,22,0,39,1,9,100,2,73};
|
|
73 |
gint array_after_remove_range[ARRAY_SIZE_AFTER_REMOVE_RANGE ]= {99,88,77,66,22,0,39,1,9,100,2,73};
|
|
74 |
gboolean ret;
|
|
75 |
int i;
|
|
76 |
|
|
77 |
|
|
78 |
gparray = g_ptr_array_new ();
|
|
79 |
if(gparray == NULL)
|
|
80 |
{
|
72
|
81 |
g_print( "Pointer Array Not created");
|
50
|
82 |
assert_failed = 1;
|
|
83 |
return;
|
|
84 |
}
|
|
85 |
|
|
86 |
/*Add elements to array*/
|
|
87 |
for (i = 0; i < ARRAY_SIZE; i++)
|
|
88 |
{
|
|
89 |
g_ptr_array_add (gparray, &(array[i]));
|
72
|
90 |
g_print( "Ptr Array element at index %d is %d",i, array[i]);
|
50
|
91 |
}
|
|
92 |
|
|
93 |
g_ptr_array_remove_range(gparray,3,3);
|
|
94 |
|
72
|
95 |
g_print( "AFTER DELETING THE RANGE");
|
50
|
96 |
|
|
97 |
/*Print the garray pointer->values*/
|
|
98 |
for(i=0;i<gparray->len;i++)
|
|
99 |
{
|
|
100 |
gpointer val = g_ptr_array_index (gparray,i);
|
72
|
101 |
g_print( "Ptr Array element at index %d is %d",i, *((int*)val));
|
50
|
102 |
}
|
|
103 |
|
|
104 |
ret = compare_pointer_array(gparray, array_after_remove_range, ARRAY_SIZE_AFTER_REMOVE_RANGE );
|
|
105 |
|
|
106 |
if(ret != TRUE)
|
|
107 |
{
|
72
|
108 |
g_print( "Pointer Array Elements not properly deleted by g_ptr_array_remove_range");
|
50
|
109 |
assert_failed = 1;
|
|
110 |
g_ptr_array_free(gparray,TRUE);
|
|
111 |
return ;
|
|
112 |
}
|
|
113 |
g_ptr_array_free (gparray, TRUE);
|
|
114 |
}
|
|
115 |
|
|
116 |
|
|
117 |
void sort_pointer_array()
|
|
118 |
{
|
|
119 |
|
|
120 |
GPtrArray *gparray;
|
|
121 |
const gint ARRAY_SIZE = 15;
|
|
122 |
|
|
123 |
gint array[ARRAY_SIZE]= {99,88,77,33,44,11,66,22,0,39,1,9,100,2,73};
|
|
124 |
gint sorted_array[ARRAY_SIZE]= { 0,1,2,9,11,22,33,39,44,66,73,77,88,99,100};
|
|
125 |
int i;
|
|
126 |
gboolean ret;
|
|
127 |
|
|
128 |
/* Test to sort the pointer array*/
|
|
129 |
gparray = g_ptr_array_new ();
|
|
130 |
if(gparray == NULL)
|
|
131 |
{
|
72
|
132 |
g_print( "Pointer Array not created");
|
50
|
133 |
assert_failed = 1;
|
|
134 |
g_ptr_array_free(gparray,TRUE);
|
|
135 |
return ;
|
|
136 |
}
|
|
137 |
|
|
138 |
for (i = 0; i < ARRAY_SIZE; i++)
|
|
139 |
{
|
|
140 |
g_ptr_array_add (gparray, &array[i]);
|
72
|
141 |
g_print( "Ptr Array element at index %d is %d",i, array[i]);
|
50
|
142 |
}
|
|
143 |
|
|
144 |
g_ptr_array_sort(gparray,psort);
|
|
145 |
|
|
146 |
/*Print the sorted Array*/
|
72
|
147 |
g_print( "SORTED ARRAY");
|
50
|
148 |
|
|
149 |
for(i=0;i<gparray->len;i++)
|
|
150 |
{
|
|
151 |
gpointer val = g_ptr_array_index (gparray,i);
|
72
|
152 |
g_print( "Ptr Array element at index %d is %d",i, *((int*)val));
|
50
|
153 |
}
|
|
154 |
|
|
155 |
|
|
156 |
ret = compare_pointer_array(gparray, sorted_array, ARRAY_SIZE);
|
|
157 |
if(ret != TRUE)
|
|
158 |
{
|
72
|
159 |
g_print( "Pointer Array Elements not sorted by g_ptr_array_sort");
|
50
|
160 |
assert_failed = 1;
|
|
161 |
g_ptr_array_free(gparray,TRUE);
|
|
162 |
return ;
|
|
163 |
}
|
|
164 |
g_ptr_array_free (gparray, TRUE);
|
|
165 |
}
|
|
166 |
|
|
167 |
|
|
168 |
void sort_pointer_array_with_data()
|
|
169 |
{
|
|
170 |
|
|
171 |
GPtrArray *gparray;
|
|
172 |
const gint ARRAY_SIZE = 15;
|
|
173 |
|
|
174 |
gint array[ARRAY_SIZE]= {99,88,77,33,44,11,66,22,0,39,1,9,100,2,73};
|
|
175 |
gint sorted_array[ARRAY_SIZE]= { 0,1,2,9,11,22,33,39,44,66,73,77,88,99,100};
|
|
176 |
int i;
|
|
177 |
gboolean ret;
|
|
178 |
|
|
179 |
/* Test to sort the pointer array*/
|
|
180 |
gparray = g_ptr_array_new ();
|
|
181 |
if(gparray == NULL)
|
|
182 |
{
|
72
|
183 |
g_print( "Pointer Array not created");
|
50
|
184 |
assert_failed = 1;
|
|
185 |
g_ptr_array_free(gparray,TRUE);
|
|
186 |
return ;
|
|
187 |
}
|
|
188 |
|
|
189 |
for (i = 0; i < ARRAY_SIZE; i++)
|
|
190 |
{
|
|
191 |
g_ptr_array_add (gparray, &array[i]);
|
|
192 |
|
72
|
193 |
g_print( "Ptr Array element at index %d is %d",i, array[i]);
|
50
|
194 |
}
|
|
195 |
|
|
196 |
g_ptr_array_sort_with_data(gparray,psort_userdata, NULL);
|
|
197 |
|
|
198 |
|
|
199 |
/*Print the sorted Array*/
|
72
|
200 |
g_print( "SORTED ARRAY");
|
50
|
201 |
|
|
202 |
for(i=0;i<gparray->len;i++)
|
|
203 |
{
|
|
204 |
gpointer val = g_ptr_array_index (gparray,i);
|
72
|
205 |
g_print( "Ptr Array element at index %d is %d",i, *((int*)val) );
|
50
|
206 |
}
|
|
207 |
|
|
208 |
|
|
209 |
ret = compare_pointer_array(gparray, sorted_array, ARRAY_SIZE);
|
|
210 |
|
|
211 |
if(ret != TRUE)
|
|
212 |
{
|
72
|
213 |
g_print( "Pointer Array Elements not sorted by g_ptr_array_sort");
|
50
|
214 |
assert_failed = 1;
|
|
215 |
g_ptr_array_free(gparray,TRUE);
|
|
216 |
return ;
|
|
217 |
}
|
|
218 |
g_ptr_array_free (gparray, TRUE);
|
|
219 |
}
|
|
220 |
|
|
221 |
|
|
222 |
int main (void)
|
|
223 |
{
|
72
|
224 |
#ifdef __SYMBIAN32__
|
|
225 |
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);
|
|
226 |
g_set_print_handler(mrtPrintHandler);
|
|
227 |
#endif /*__SYMBIAN32__*/
|
|
228 |
|
|
229 |
g_print("Test array2-test Start");
|
|
230 |
test_pointer_array_remove_range();
|
50
|
231 |
sort_pointer_array();
|
|
232 |
sort_pointer_array_with_data();
|
|
233 |
|
|
234 |
if(assert_failed)
|
72
|
235 |
g_print("Test array2-test Failed");
|
50
|
236 |
else
|
72
|
237 |
g_print("Test array2-test Successful");
|
50
|
238 |
|
72
|
239 |
#if __SYMBIAN32__
|
|
240 |
testResultXml("array2-test");
|
|
241 |
#endif /* EMULATOR */
|
50
|
242 |
return 0;
|
|
243 |
}
|