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: #include sl@0: #define LOG_FILE "c:\\logs\\array1_test_log.txt" sl@0: #include "std_log_result.h" sl@0: #define LOG_FILENAME_LINE __FILE__, __LINE__ 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("array-test"); sl@0: close_log_file(); sl@0: } sl@0: sl@0: static gint sort (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 sort_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: sl@0: sl@0: /*Will return TRUE if input GArray and int[] array 2 are same else FALSE*/ sl@0: gboolean compare_array(GArray* array1, gint* array2, gint size) sl@0: { sl@0: sl@0: int i; sl@0: if ( size != array1->len) sl@0: return FALSE; sl@0: for ( i = 0; i < size ; i++) sl@0: { sl@0: if ( g_array_index(array1, gint, i) != array2[i]) sl@0: return FALSE; sl@0: } sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: sl@0: void test_remove_array_index() sl@0: { sl@0: GArray *garray; sl@0: sl@0: const gint ARRAY_SIZE = 7; sl@0: const gint ARRAY_SIZE_AFTER_REMOVE_INDEX = 6; sl@0: const gint ARRAY_SIZE_AFTER_REMOVE_INDEX_FAST = 5; sl@0: sl@0: gint array[ARRAY_SIZE] = {4,5,6,7,8,9,10}; sl@0: gint array_after_remove_index_1[ARRAY_SIZE_AFTER_REMOVE_INDEX]= {4,6,7,8,9,10}; /*array after removing index 1*/ sl@0: gint array_after_remove_index_fast_2[ARRAY_SIZE_AFTER_REMOVE_INDEX_FAST] = {4,6,10,8,9}; /* array after removing index 2 fast. Input is array after removing index 1 */ sl@0: sl@0: int i; sl@0: gboolean ret; sl@0: sl@0: garray = g_array_new (FALSE,FALSE,sizeof(gint)); sl@0: if(garray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Array not created"); sl@0: assert_failed = 1; sl@0: return ; sl@0: } sl@0: sl@0: /*Insert values into array*/ sl@0: g_array_insert_vals(garray,0,array,ARRAY_SIZE); sl@0: sl@0: /* test for deleting single element in an array. Removing the element with index 1*/ sl@0: std_log(LOG_FILENAME_LINE, "Delete array element at index 1"); sl@0: garray = g_array_remove_index(garray,1); sl@0: sl@0: if(garray == NULL ) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "NULL return by g_array_remove_index"); sl@0: assert_failed = 1; sl@0: return ; sl@0: } sl@0: sl@0: /*Print the array elements after remove*/ sl@0: for(i=0; ilen; i++) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Current array element at index %d is %d", i,g_array_index(garray, gint, i)); sl@0: } sl@0: sl@0: /*Check if the array size is now 4 and element at index 1 is not 5 after removal*/ sl@0: sl@0: ret = compare_array(garray, array_after_remove_index_1, ARRAY_SIZE_AFTER_REMOVE_INDEX); sl@0: if ( !ret) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Array Element not properly deleted by g_array_remove_index"); sl@0: assert_failed = 1; sl@0: g_array_free(garray,TRUE); sl@0: return ; sl@0: } sl@0: sl@0: sl@0: /* Test to remove index element 2 using g_array_remove_index_fast*/ sl@0: sl@0: std_log(LOG_FILENAME_LINE, "Delete array element at index 2"); sl@0: garray =g_array_remove_index_fast(garray,2); sl@0: if(garray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "NULL return by g_array_remove_index_fast"); sl@0: assert_failed = 1; sl@0: return ; sl@0: } sl@0: sl@0: for(i=0; ilen; i++) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Current array element at index %d is %d", i,g_array_index(garray, gint, i)); sl@0: } sl@0: sl@0: ret = compare_array(garray, array_after_remove_index_fast_2, ARRAY_SIZE_AFTER_REMOVE_INDEX_FAST); sl@0: if ( !ret) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Array Element not properly deleted by g_array_remove_index_fast"); sl@0: assert_failed = 1; sl@0: g_array_free(garray,TRUE); sl@0: return; sl@0: } sl@0: g_array_free(garray,TRUE); sl@0: } sl@0: sl@0: sl@0: void test_test_remove_array_index_range() sl@0: { sl@0: const int ARRAY_SIZE = 10; sl@0: const int ARRAY_SIZE_AFTER_REMOVE_INDEX_RANGE = 8; sl@0: sl@0: gint array[ARRAY_SIZE]= {10,5,16,7,11,0,20,1,9,8}; sl@0: gint array_after_remove_index_range[ARRAY_SIZE_AFTER_REMOVE_INDEX_RANGE] = {10,5,16,0,20,1,9,8}; /*after removing 2 elements from index 3*/ sl@0: sl@0: gboolean ret; sl@0: GArray* garray; sl@0: int i; sl@0: sl@0: garray = g_array_new (FALSE,FALSE,sizeof(gint)); sl@0: if(garray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Array not created"); sl@0: assert_failed = 1; sl@0: return ; sl@0: } sl@0: sl@0: g_array_insert_vals(garray,0,array,ARRAY_SIZE); sl@0: for(i=0; ilen;i++) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Current array elements %d is %d", i,g_array_index(garray, gint, i)); sl@0: } sl@0: sl@0: garray = g_array_remove_range(garray,3,2); /*remove two elements from index 3 */ sl@0: sl@0: if(garray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE,"Elements not deleted properly by g_array_remove_range"); sl@0: assert_failed = 1; sl@0: return ; sl@0: sl@0: } sl@0: sl@0: /*print the array elements */ sl@0: for(i=0; ilen;i++) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Curent array element(after deletion) %d is %d", i,g_array_index(garray, gint, i)); sl@0: } sl@0: sl@0: sl@0: ret = compare_array(garray, array_after_remove_index_range, ARRAY_SIZE_AFTER_REMOVE_INDEX_RANGE); sl@0: if(!ret) sl@0: { sl@0: std_log(LOG_FILENAME_LINE,"Elements not deleted properly"); sl@0: assert_failed = 1; sl@0: g_array_free(garray,TRUE); sl@0: return ; sl@0: } sl@0: g_array_free(garray,TRUE); sl@0: } sl@0: sl@0: sl@0: void test_sort_array() sl@0: { sl@0: sl@0: GArray *garray; sl@0: const int ARRAY_SIZE = 11; sl@0: gint array[ARRAY_SIZE] = {10,5,16,7,11,0,20,1,9,8,9}; sl@0: gint sort_array[ARRAY_SIZE] = {0,1,5,7,8,9,9,10,11,16,20}; sl@0: sl@0: gboolean ret; sl@0: int i; sl@0: sl@0: /* Test for sorting the array elements */ sl@0: sl@0: garray = g_array_new (FALSE,FALSE,sizeof(gint)); sl@0: sl@0: if(garray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Array not created"); sl@0: assert_failed = 1; sl@0: return ; sl@0: } sl@0: g_array_insert_vals(garray,0,array,ARRAY_SIZE); sl@0: sl@0: g_array_sort(garray, sort); sl@0: sl@0: if(garray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Array not sorted"); sl@0: assert_failed = 1; sl@0: return ; sl@0: } sl@0: sl@0: std_log(LOG_FILENAME_LINE,"SORTED ARRAY"); sl@0: sl@0: for(i=0;ilen;i++) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Element %d is %d", i,g_array_index(garray, gint, i)); sl@0: } sl@0: sl@0: ret = compare_array(garray, sort_array, ARRAY_SIZE); sl@0: sl@0: if(!ret) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Array not sorted correctly"); sl@0: assert_failed = 1; sl@0: return ; sl@0: } sl@0: sl@0: g_array_free(garray,TRUE); sl@0: sl@0: sl@0: garray = g_array_new (FALSE,FALSE,sizeof(gint)); sl@0: if(garray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Array not created"); sl@0: return ; sl@0: } sl@0: g_array_insert_vals(garray,0,array,ARRAY_SIZE); sl@0: sl@0: g_array_sort_with_data (garray, sort_userdata, NULL); sl@0: sl@0: if(garray == NULL) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Array not sorted with user data"); sl@0: assert_failed = 1; sl@0: return ; sl@0: } sl@0: sl@0: std_log(LOG_FILENAME_LINE,"SORTED ARRAY WITH USERDATA"); sl@0: for(i=0;ilen;i++) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Element %d is %d", i,g_array_index(garray, gint, i)); sl@0: } sl@0: sl@0: ret = compare_array(garray, sort_array, ARRAY_SIZE); sl@0: if(!ret) sl@0: { sl@0: std_log(LOG_FILENAME_LINE, "Array not sorted correctly with user data"); sl@0: assert_failed = 1; sl@0: return ; sl@0: } sl@0: g_array_free(garray,TRUE); sl@0: } sl@0: sl@0: sl@0: int main (void) sl@0: { sl@0: test_test_remove_array_index_range(); sl@0: test_sort_array(); sl@0: test_remove_array_index(); 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: } sl@0: sl@0: