glib/tests/array-test2.c
changeset 50 79045913e4e9
child 72 403e7f6ed6c5
equal deleted inserted replaced
49:e5d77a29bdca 50:79045913e4e9
       
     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 
       
    17 #undef G_DISABLE_ASSERT
       
    18 #undef G_LOG_DOMAIN
       
    19 
       
    20 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    21 #include <stdio.h>
       
    22 #include <glib.h>
       
    23 
       
    24 #define LOG_FILE "c:\\logs\\array2_test_log.txt"
       
    25 #include "std_log_result.h"
       
    26 
       
    27 
       
    28 void create_xml(int result)
       
    29 {
       
    30     if(result)
       
    31         assert_failed = 1;
       
    32 
       
    33     testResultXml("ptrarray-test");
       
    34     close_log_file();
       
    35 }
       
    36 
       
    37 static gint psort (gconstpointer a, gconstpointer b)
       
    38 {
       
    39   if(**(guint32**)a == **(guint32**)b)
       
    40 	  return 0;
       
    41   else
       
    42 	return **(guint32**)a < **(guint32**)b ? -1 : 1;
       
    43 }
       
    44 
       
    45 
       
    46 static gint psort_userdata (gconstpointer a, gconstpointer b, gpointer user_data)
       
    47 {
       
    48     if(**(guint32**)a == **(guint32**)b)
       
    49 	   return 0;
       
    50 	else
       
    51 	   return **(guint32**)a < **(guint32**)b ? -1 : 1;
       
    52 }
       
    53 
       
    54 gboolean compare_pointer_array(GPtrArray *parray1, gint* array2, gint size )
       
    55 {
       
    56 
       
    57 	int i;
       
    58 	int* val;
       
    59 	if ( size != parray1->len)
       
    60 		return FALSE;
       
    61 	for ( i = 0; i < size ; i++)
       
    62 	{
       
    63 		val = (int*)g_ptr_array_index(parray1, i) ;
       
    64 		if(val == NULL)
       
    65 			return FALSE;
       
    66 		if ( *val != array2[i])
       
    67 			return FALSE;
       
    68 	}
       
    69 
       
    70 	return TRUE;
       
    71 
       
    72 
       
    73 }
       
    74 void test_pointer_array_remove_range()
       
    75 {
       
    76 	GPtrArray *gparray;
       
    77     const gint ARRAY_SIZE = 15;
       
    78     const gint ARRAY_SIZE_AFTER_REMOVE_RANGE = 12; /*removing 3 elements starting from index 3*/
       
    79 
       
    80     gint array[ARRAY_SIZE]= {99,88,77,33,44,11,66,22,0,39,1,9,100,2,73};
       
    81     gint array_after_remove_range[ARRAY_SIZE_AFTER_REMOVE_RANGE ]= {99,88,77,66,22,0,39,1,9,100,2,73};
       
    82     gboolean ret;
       
    83     int i;
       
    84 
       
    85 
       
    86 	gparray = g_ptr_array_new ();
       
    87  	if(gparray == NULL)
       
    88  	{
       
    89  		std_log(LOG_FILENAME_LINE, "Pointer Array Not created");
       
    90  		assert_failed = 1;
       
    91  		return;
       
    92  	}
       
    93 
       
    94  	/*Add elements to array*/
       
    95 	for (i = 0; i < ARRAY_SIZE; i++)
       
    96 	{
       
    97 		g_ptr_array_add (gparray, &(array[i]));
       
    98 		std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, array[i]);
       
    99 	}
       
   100 
       
   101 	g_ptr_array_remove_range(gparray,3,3);
       
   102 
       
   103 	std_log(LOG_FILENAME_LINE, "AFTER DELETING THE RANGE");
       
   104 
       
   105 	/*Print the garray pointer->values*/
       
   106 	for(i=0;i<gparray->len;i++)
       
   107 	{
       
   108         gpointer val = g_ptr_array_index (gparray,i);	
       
   109         std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i,  *((int*)val));	
       
   110 	}
       
   111 
       
   112 	ret = compare_pointer_array(gparray, array_after_remove_range, ARRAY_SIZE_AFTER_REMOVE_RANGE );
       
   113 
       
   114     if(ret != TRUE)
       
   115     {
       
   116 		std_log(LOG_FILENAME_LINE, "Pointer Array Elements not properly deleted by g_ptr_array_remove_range");
       
   117 		assert_failed = 1;
       
   118 		g_ptr_array_free(gparray,TRUE);
       
   119 		return ;
       
   120 	}
       
   121  	g_ptr_array_free (gparray, TRUE);
       
   122 }
       
   123 
       
   124 
       
   125 void sort_pointer_array()
       
   126 {
       
   127 
       
   128 	GPtrArray *gparray;
       
   129     const gint ARRAY_SIZE = 15;
       
   130 
       
   131     gint array[ARRAY_SIZE]= {99,88,77,33,44,11,66,22,0,39,1,9,100,2,73};
       
   132     gint sorted_array[ARRAY_SIZE]= { 0,1,2,9,11,22,33,39,44,66,73,77,88,99,100};
       
   133     int i;
       
   134     gboolean ret;
       
   135 
       
   136 	/* Test to sort the pointer array*/
       
   137 	gparray = g_ptr_array_new ();
       
   138 	if(gparray == NULL)
       
   139 	{
       
   140 		std_log(LOG_FILENAME_LINE, "Pointer Array not created");
       
   141 		assert_failed = 1;
       
   142 		g_ptr_array_free(gparray,TRUE);
       
   143 		return ;
       
   144 	}
       
   145 
       
   146 	for (i = 0; i < ARRAY_SIZE; i++)
       
   147 	{
       
   148 		g_ptr_array_add (gparray, &array[i]);
       
   149 		std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, array[i]);
       
   150 	}
       
   151 
       
   152 	g_ptr_array_sort(gparray,psort);
       
   153 
       
   154 	/*Print the sorted Array*/
       
   155 	std_log(LOG_FILENAME_LINE, "SORTED ARRAY");
       
   156 
       
   157 	for(i=0;i<gparray->len;i++)
       
   158 	{
       
   159         gpointer val = g_ptr_array_index (gparray,i);
       
   160         std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, *((int*)val));
       
   161 	}
       
   162 
       
   163 
       
   164 	ret = compare_pointer_array(gparray, sorted_array, ARRAY_SIZE);
       
   165     if(ret != TRUE)
       
   166     {
       
   167 		std_log(LOG_FILENAME_LINE, "Pointer Array Elements not sorted by g_ptr_array_sort");
       
   168 		assert_failed = 1;
       
   169 		g_ptr_array_free(gparray,TRUE);
       
   170 		return ;
       
   171 	}
       
   172 	g_ptr_array_free (gparray, TRUE);
       
   173 }
       
   174 
       
   175 
       
   176 void sort_pointer_array_with_data()
       
   177 {
       
   178 
       
   179 	GPtrArray *gparray;
       
   180     const gint ARRAY_SIZE = 15;
       
   181 
       
   182     gint array[ARRAY_SIZE]= {99,88,77,33,44,11,66,22,0,39,1,9,100,2,73};
       
   183     gint sorted_array[ARRAY_SIZE]= { 0,1,2,9,11,22,33,39,44,66,73,77,88,99,100};
       
   184     int i;
       
   185     gboolean ret;
       
   186 
       
   187 	/* Test to sort the pointer array*/
       
   188 	gparray = g_ptr_array_new ();
       
   189 	if(gparray == NULL)
       
   190 	{
       
   191 		std_log(LOG_FILENAME_LINE, "Pointer Array not created");
       
   192 		assert_failed = 1;
       
   193 		g_ptr_array_free(gparray,TRUE);
       
   194 		return ;
       
   195 	}
       
   196 
       
   197 	for (i = 0; i < ARRAY_SIZE; i++)
       
   198 	{
       
   199 		g_ptr_array_add (gparray, &array[i]);
       
   200 		
       
   201 		std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, array[i]);
       
   202 	}
       
   203 
       
   204 	g_ptr_array_sort_with_data(gparray,psort_userdata, NULL);
       
   205 
       
   206 
       
   207 	/*Print the sorted Array*/
       
   208 	std_log(LOG_FILENAME_LINE, "SORTED ARRAY");
       
   209 
       
   210 	for(i=0;i<gparray->len;i++)
       
   211 	{
       
   212         gpointer val = g_ptr_array_index (gparray,i);
       
   213         std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, *((int*)val) );
       
   214 	}
       
   215 
       
   216 
       
   217 	ret = compare_pointer_array(gparray, sorted_array, ARRAY_SIZE);
       
   218 
       
   219     if(ret != TRUE)
       
   220     {
       
   221 		std_log(LOG_FILENAME_LINE, "Pointer Array Elements not sorted by g_ptr_array_sort");
       
   222 		assert_failed = 1;
       
   223 		g_ptr_array_free(gparray,TRUE);
       
   224 		return ;
       
   225 	}
       
   226 	g_ptr_array_free (gparray, TRUE);
       
   227 }
       
   228 
       
   229 
       
   230 int main (void)
       
   231 {
       
   232 	test_pointer_array_remove_range();
       
   233 	sort_pointer_array();
       
   234 	sort_pointer_array_with_data();
       
   235 
       
   236 	if(assert_failed)
       
   237 		std_log(LOG_FILENAME_LINE,"Test Failed");
       
   238 	else
       
   239 		std_log(LOG_FILENAME_LINE,"Test Successful");
       
   240 
       
   241 	create_xml(0);
       
   242 	return 0;
       
   243 }