sl@0: // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #define LOG_FILENAME_LINE __FILE__, __LINE__ sl@0: #include sl@0: #include sl@0: sl@0: #define LOG_FILE "c:\\logs\\array2_test_log.txt" sl@0: #include "std_log_result.h" sl@0: sl@0: sl@0: void create_xml(int result) sl@0: { sl@0: if(result) sl@0: assert_failed = 1; sl@0: sl@0: testResultXml("ptrarray-test"); sl@0: close_log_file(); sl@0: } sl@0: sl@0: static gint psort (gconstpointer a, gconstpointer b) sl@0: { sl@0: if(**(guint32**)a == **(guint32**)b) sl@0: return 0; sl@0: else sl@0: return **(guint32**)a < **(guint32**)b ? -1 : 1; sl@0: } sl@0: sl@0: sl@0: static gint psort_userdata (gconstpointer a, gconstpointer b, gpointer user_data) sl@0: { sl@0: if(**(guint32**)a == **(guint32**)b) sl@0: return 0; sl@0: else sl@0: return **(guint32**)a < **(guint32**)b ? -1 : 1; sl@0: } sl@0: sl@0: gboolean compare_pointer_array(GPtrArray *parray1, gint* array2, gint size ) sl@0: { sl@0: sl@0: int i; sl@0: int* val; sl@0: if ( size != parray1->len) sl@0: return FALSE; sl@0: for ( i = 0; i < size ; i++) sl@0: { sl@0: val = (int*)g_ptr_array_index(parray1, i) ; sl@0: if(val == NULL) sl@0: return FALSE; sl@0: if ( *val != array2[i]) sl@0: return FALSE; sl@0: } sl@0: sl@0: return TRUE; sl@0: sl@0: sl@0: } sl@0: void test_pointer_array_remove_range() sl@0: { sl@0: GPtrArray *gparray; sl@0: const gint ARRAY_SIZE = 15; sl@0: const gint ARRAY_SIZE_AFTER_REMOVE_RANGE = 12; /*removing 3 elements starting from index 3*/ sl@0: sl@0: gint array[ARRAY_SIZE]= {99,88,77,33,44,11,66,22,0,39,1,9,100,2,73}; sl@0: gint array_after_remove_range[ARRAY_SIZE_AFTER_REMOVE_RANGE ]= {99,88,77,66,22,0,39,1,9,100,2,73}; sl@0: gboolean ret; sl@0: int i; sl@0: sl@0: sl@0: gparray = g_ptr_array_new (); sl@0: if(gparray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Pointer Array Not created"); sl@0: assert_failed = 1; sl@0: return; sl@0: } sl@0: sl@0: /*Add elements to array*/ sl@0: for (i = 0; i < ARRAY_SIZE; i++) sl@0: { sl@0: g_ptr_array_add (gparray, &(array[i])); sl@0: std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, array[i]); sl@0: } sl@0: sl@0: g_ptr_array_remove_range(gparray,3,3); sl@0: sl@0: std_log(LOG_FILENAME_LINE, "AFTER DELETING THE RANGE"); sl@0: sl@0: /*Print the garray pointer->values*/ sl@0: for(i=0;ilen;i++) sl@0: { sl@0: gpointer val = g_ptr_array_index (gparray,i); sl@0: std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, *((int*)val)); sl@0: } sl@0: sl@0: ret = compare_pointer_array(gparray, array_after_remove_range, ARRAY_SIZE_AFTER_REMOVE_RANGE ); sl@0: sl@0: if(ret != TRUE) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Pointer Array Elements not properly deleted by g_ptr_array_remove_range"); sl@0: assert_failed = 1; sl@0: g_ptr_array_free(gparray,TRUE); sl@0: return ; sl@0: } sl@0: g_ptr_array_free (gparray, TRUE); sl@0: } sl@0: sl@0: sl@0: void sort_pointer_array() sl@0: { sl@0: sl@0: GPtrArray *gparray; sl@0: const gint ARRAY_SIZE = 15; sl@0: sl@0: gint array[ARRAY_SIZE]= {99,88,77,33,44,11,66,22,0,39,1,9,100,2,73}; sl@0: gint sorted_array[ARRAY_SIZE]= { 0,1,2,9,11,22,33,39,44,66,73,77,88,99,100}; sl@0: int i; sl@0: gboolean ret; sl@0: sl@0: /* Test to sort the pointer array*/ sl@0: gparray = g_ptr_array_new (); sl@0: if(gparray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Pointer Array not created"); sl@0: assert_failed = 1; sl@0: g_ptr_array_free(gparray,TRUE); sl@0: return ; sl@0: } sl@0: sl@0: for (i = 0; i < ARRAY_SIZE; i++) sl@0: { sl@0: g_ptr_array_add (gparray, &array[i]); sl@0: std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, array[i]); sl@0: } sl@0: sl@0: g_ptr_array_sort(gparray,psort); sl@0: sl@0: /*Print the sorted Array*/ sl@0: std_log(LOG_FILENAME_LINE, "SORTED ARRAY"); sl@0: sl@0: for(i=0;ilen;i++) sl@0: { sl@0: gpointer val = g_ptr_array_index (gparray,i); sl@0: std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, *((int*)val)); sl@0: } sl@0: sl@0: sl@0: ret = compare_pointer_array(gparray, sorted_array, ARRAY_SIZE); sl@0: if(ret != TRUE) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Pointer Array Elements not sorted by g_ptr_array_sort"); sl@0: assert_failed = 1; sl@0: g_ptr_array_free(gparray,TRUE); sl@0: return ; sl@0: } sl@0: g_ptr_array_free (gparray, TRUE); sl@0: } sl@0: sl@0: sl@0: void sort_pointer_array_with_data() sl@0: { sl@0: sl@0: GPtrArray *gparray; sl@0: const gint ARRAY_SIZE = 15; sl@0: sl@0: gint array[ARRAY_SIZE]= {99,88,77,33,44,11,66,22,0,39,1,9,100,2,73}; sl@0: gint sorted_array[ARRAY_SIZE]= { 0,1,2,9,11,22,33,39,44,66,73,77,88,99,100}; sl@0: int i; sl@0: gboolean ret; sl@0: sl@0: /* Test to sort the pointer array*/ sl@0: gparray = g_ptr_array_new (); sl@0: if(gparray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Pointer Array not created"); sl@0: assert_failed = 1; sl@0: g_ptr_array_free(gparray,TRUE); sl@0: return ; sl@0: } sl@0: sl@0: for (i = 0; i < ARRAY_SIZE; i++) sl@0: { sl@0: g_ptr_array_add (gparray, &array[i]); sl@0: sl@0: std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, array[i]); sl@0: } sl@0: sl@0: g_ptr_array_sort_with_data(gparray,psort_userdata, NULL); sl@0: sl@0: sl@0: /*Print the sorted Array*/ sl@0: std_log(LOG_FILENAME_LINE, "SORTED ARRAY"); sl@0: sl@0: for(i=0;ilen;i++) sl@0: { sl@0: gpointer val = g_ptr_array_index (gparray,i); sl@0: std_log(LOG_FILENAME_LINE, "Ptr Array element at index %d is %d",i, *((int*)val) ); sl@0: } sl@0: sl@0: sl@0: ret = compare_pointer_array(gparray, sorted_array, ARRAY_SIZE); sl@0: sl@0: if(ret != TRUE) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Pointer Array Elements not sorted by g_ptr_array_sort"); sl@0: assert_failed = 1; sl@0: g_ptr_array_free(gparray,TRUE); sl@0: return ; sl@0: } sl@0: g_ptr_array_free (gparray, TRUE); sl@0: } sl@0: sl@0: sl@0: int main (void) sl@0: { sl@0: test_pointer_array_remove_range(); sl@0: sort_pointer_array(); sl@0: sort_pointer_array_with_data(); sl@0: sl@0: if(assert_failed) sl@0: std_log(LOG_FILENAME_LINE,"Test Failed"); sl@0: else sl@0: std_log(LOG_FILENAME_LINE,"Test Successful"); sl@0: sl@0: create_xml(0); sl@0: return 0; sl@0: }