glib/tests/array-test1.c
author hgs
Thu, 05 Aug 2010 23:23:13 +0530
changeset 50 79045913e4e9
child 72 403e7f6ed6c5
permissions -rw-r--r--
201031
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50
hgs
parents:
diff changeset
     1
// Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     2
// All rights reserved.
hgs
parents:
diff changeset
     3
// This component and the accompanying materials are made available
hgs
parents:
diff changeset
     4
// under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     5
// which accompanies this distribution, and is available
hgs
parents:
diff changeset
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     7
//
hgs
parents:
diff changeset
     8
// Initial Contributors:
hgs
parents:
diff changeset
     9
// Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    10
//
hgs
parents:
diff changeset
    11
// Contributors:
hgs
parents:
diff changeset
    12
//
hgs
parents:
diff changeset
    13
// Description:
hgs
parents:
diff changeset
    14
//
hgs
parents:
diff changeset
    15
hgs
parents:
diff changeset
    16
hgs
parents:
diff changeset
    17
#undef G_DISABLE_ASSERT
hgs
parents:
diff changeset
    18
#undef G_LOG_DOMAIN
hgs
parents:
diff changeset
    19
hgs
parents:
diff changeset
    20
#include <glib.h>
hgs
parents:
diff changeset
    21
#define LOG_FILE "c:\\logs\\array1_test_log.txt"
hgs
parents:
diff changeset
    22
#include "std_log_result.h"
hgs
parents:
diff changeset
    23
#define LOG_FILENAME_LINE __FILE__, __LINE__
hgs
parents:
diff changeset
    24
hgs
parents:
diff changeset
    25
hgs
parents:
diff changeset
    26
void create_xml(int result)
hgs
parents:
diff changeset
    27
{
hgs
parents:
diff changeset
    28
    if(result)
hgs
parents:
diff changeset
    29
        assert_failed = 1;
hgs
parents:
diff changeset
    30
hgs
parents:
diff changeset
    31
    testResultXml("array-test");
hgs
parents:
diff changeset
    32
    close_log_file();
hgs
parents:
diff changeset
    33
}
hgs
parents:
diff changeset
    34
hgs
parents:
diff changeset
    35
static gint sort (gconstpointer a, gconstpointer b)
hgs
parents:
diff changeset
    36
{
hgs
parents:
diff changeset
    37
	if(*(guint32*)a == *(guint32*)b)
hgs
parents:
diff changeset
    38
		return 0;
hgs
parents:
diff changeset
    39
	else
hgs
parents:
diff changeset
    40
		return *(guint32*)a < *(guint32*)b ? -1 : 1;
hgs
parents:
diff changeset
    41
}
hgs
parents:
diff changeset
    42
hgs
parents:
diff changeset
    43
hgs
parents:
diff changeset
    44
static gint sort_userdata (gconstpointer a, gconstpointer b, gpointer user_data)
hgs
parents:
diff changeset
    45
{
hgs
parents:
diff changeset
    46
	  if(*(guint32*)a == *(guint32*)b)
hgs
parents:
diff changeset
    47
			return 0;
hgs
parents:
diff changeset
    48
	  else
hgs
parents:
diff changeset
    49
			return *(guint32*)a < *(guint32*)b ? -1 : 1;
hgs
parents:
diff changeset
    50
}
hgs
parents:
diff changeset
    51
hgs
parents:
diff changeset
    52
hgs
parents:
diff changeset
    53
hgs
parents:
diff changeset
    54
/*Will return TRUE if input GArray and int[] array 2 are same else FALSE*/
hgs
parents:
diff changeset
    55
gboolean compare_array(GArray* array1, gint* array2, gint size)
hgs
parents:
diff changeset
    56
{
hgs
parents:
diff changeset
    57
hgs
parents:
diff changeset
    58
    int i;
hgs
parents:
diff changeset
    59
	if ( size != array1->len)
hgs
parents:
diff changeset
    60
		return FALSE;
hgs
parents:
diff changeset
    61
	for ( i = 0; i < size ; i++)
hgs
parents:
diff changeset
    62
	{
hgs
parents:
diff changeset
    63
		if ( g_array_index(array1, gint, i) != array2[i])
hgs
parents:
diff changeset
    64
			return FALSE;
hgs
parents:
diff changeset
    65
	}
hgs
parents:
diff changeset
    66
hgs
parents:
diff changeset
    67
	return TRUE;
hgs
parents:
diff changeset
    68
}
hgs
parents:
diff changeset
    69
hgs
parents:
diff changeset
    70
hgs
parents:
diff changeset
    71
void test_remove_array_index()
hgs
parents:
diff changeset
    72
{
hgs
parents:
diff changeset
    73
    GArray *garray;
hgs
parents:
diff changeset
    74
hgs
parents:
diff changeset
    75
    const gint ARRAY_SIZE = 7;
hgs
parents:
diff changeset
    76
    const gint ARRAY_SIZE_AFTER_REMOVE_INDEX = 6;
hgs
parents:
diff changeset
    77
    const gint ARRAY_SIZE_AFTER_REMOVE_INDEX_FAST = 5;
hgs
parents:
diff changeset
    78
hgs
parents:
diff changeset
    79
    gint array[ARRAY_SIZE] = {4,5,6,7,8,9,10};
hgs
parents:
diff changeset
    80
    gint array_after_remove_index_1[ARRAY_SIZE_AFTER_REMOVE_INDEX]= {4,6,7,8,9,10};  /*array after removing index 1*/
hgs
parents:
diff changeset
    81
    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 */
hgs
parents:
diff changeset
    82
hgs
parents:
diff changeset
    83
    int i;
hgs
parents:
diff changeset
    84
    gboolean ret;
hgs
parents:
diff changeset
    85
hgs
parents:
diff changeset
    86
    garray = g_array_new (FALSE,FALSE,sizeof(gint));
hgs
parents:
diff changeset
    87
    if(garray == NULL)
hgs
parents:
diff changeset
    88
	{
hgs
parents:
diff changeset
    89
      std_log(LOG_FILENAME_LINE, "Array not created");
hgs
parents:
diff changeset
    90
      assert_failed = 1;
hgs
parents:
diff changeset
    91
	  return ;
hgs
parents:
diff changeset
    92
	}
hgs
parents:
diff changeset
    93
hgs
parents:
diff changeset
    94
	/*Insert values into array*/
hgs
parents:
diff changeset
    95
    g_array_insert_vals(garray,0,array,ARRAY_SIZE);
hgs
parents:
diff changeset
    96
hgs
parents:
diff changeset
    97
    /* test for deleting single element in an array. Removing the element with index 1*/
hgs
parents:
diff changeset
    98
    std_log(LOG_FILENAME_LINE, "Delete array element at index 1");
hgs
parents:
diff changeset
    99
    garray = g_array_remove_index(garray,1);
hgs
parents:
diff changeset
   100
hgs
parents:
diff changeset
   101
	if(garray == NULL )
hgs
parents:
diff changeset
   102
	{
hgs
parents:
diff changeset
   103
		std_log(LOG_FILENAME_LINE, "NULL return by g_array_remove_index");
hgs
parents:
diff changeset
   104
		assert_failed = 1;
hgs
parents:
diff changeset
   105
		return ;
hgs
parents:
diff changeset
   106
	}
hgs
parents:
diff changeset
   107
hgs
parents:
diff changeset
   108
    /*Print the array elements after remove*/
hgs
parents:
diff changeset
   109
    for(i=0; i<garray->len; i++)
hgs
parents:
diff changeset
   110
    {
hgs
parents:
diff changeset
   111
          std_log(LOG_FILENAME_LINE, "Current array element at index %d is %d", i,g_array_index(garray, gint, i));
hgs
parents:
diff changeset
   112
	}
hgs
parents:
diff changeset
   113
hgs
parents:
diff changeset
   114
	/*Check if the array size is now 4 and element at index 1 is not 5 after removal*/
hgs
parents:
diff changeset
   115
hgs
parents:
diff changeset
   116
	ret = compare_array(garray, array_after_remove_index_1, ARRAY_SIZE_AFTER_REMOVE_INDEX);
hgs
parents:
diff changeset
   117
	if ( !ret)
hgs
parents:
diff changeset
   118
	{
hgs
parents:
diff changeset
   119
		std_log(LOG_FILENAME_LINE, "Array Element not properly deleted by g_array_remove_index");
hgs
parents:
diff changeset
   120
		assert_failed = 1;
hgs
parents:
diff changeset
   121
		g_array_free(garray,TRUE);
hgs
parents:
diff changeset
   122
		return ;
hgs
parents:
diff changeset
   123
	}
hgs
parents:
diff changeset
   124
hgs
parents:
diff changeset
   125
hgs
parents:
diff changeset
   126
	/* Test to remove index element 2 using g_array_remove_index_fast*/
hgs
parents:
diff changeset
   127
hgs
parents:
diff changeset
   128
    std_log(LOG_FILENAME_LINE, "Delete array element at index 2");
hgs
parents:
diff changeset
   129
    garray =g_array_remove_index_fast(garray,2);
hgs
parents:
diff changeset
   130
    if(garray == NULL)
hgs
parents:
diff changeset
   131
    {
hgs
parents:
diff changeset
   132
		std_log(LOG_FILENAME_LINE, "NULL return by g_array_remove_index_fast");
hgs
parents:
diff changeset
   133
		assert_failed = 1;
hgs
parents:
diff changeset
   134
		return ;
hgs
parents:
diff changeset
   135
	}
hgs
parents:
diff changeset
   136
hgs
parents:
diff changeset
   137
    for(i=0; i<garray->len; i++)
hgs
parents:
diff changeset
   138
	{
hgs
parents:
diff changeset
   139
		std_log(LOG_FILENAME_LINE, "Current array element at index %d is %d", i,g_array_index(garray, gint, i));
hgs
parents:
diff changeset
   140
	}
hgs
parents:
diff changeset
   141
hgs
parents:
diff changeset
   142
	ret = compare_array(garray, array_after_remove_index_fast_2, ARRAY_SIZE_AFTER_REMOVE_INDEX_FAST);
hgs
parents:
diff changeset
   143
	if ( !ret)
hgs
parents:
diff changeset
   144
	{
hgs
parents:
diff changeset
   145
		std_log(LOG_FILENAME_LINE, "Array Element not properly deleted by g_array_remove_index_fast");
hgs
parents:
diff changeset
   146
		assert_failed = 1;
hgs
parents:
diff changeset
   147
		g_array_free(garray,TRUE);
hgs
parents:
diff changeset
   148
		return;
hgs
parents:
diff changeset
   149
	}
hgs
parents:
diff changeset
   150
    g_array_free(garray,TRUE);
hgs
parents:
diff changeset
   151
}
hgs
parents:
diff changeset
   152
hgs
parents:
diff changeset
   153
hgs
parents:
diff changeset
   154
void test_test_remove_array_index_range()
hgs
parents:
diff changeset
   155
{
hgs
parents:
diff changeset
   156
	const int ARRAY_SIZE = 10;
hgs
parents:
diff changeset
   157
	const int ARRAY_SIZE_AFTER_REMOVE_INDEX_RANGE = 8;
hgs
parents:
diff changeset
   158
hgs
parents:
diff changeset
   159
	gint array[ARRAY_SIZE]=															{10,5,16,7,11,0,20,1,9,8};
hgs
parents:
diff changeset
   160
	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*/
hgs
parents:
diff changeset
   161
hgs
parents:
diff changeset
   162
	gboolean ret;
hgs
parents:
diff changeset
   163
	GArray* garray;
hgs
parents:
diff changeset
   164
	int i;
hgs
parents:
diff changeset
   165
hgs
parents:
diff changeset
   166
	garray = g_array_new (FALSE,FALSE,sizeof(gint));
hgs
parents:
diff changeset
   167
	if(garray == NULL)
hgs
parents:
diff changeset
   168
	{
hgs
parents:
diff changeset
   169
		  std_log(LOG_FILENAME_LINE, "Array not created");
hgs
parents:
diff changeset
   170
		  assert_failed = 1;
hgs
parents:
diff changeset
   171
		  return ;
hgs
parents:
diff changeset
   172
	}
hgs
parents:
diff changeset
   173
hgs
parents:
diff changeset
   174
	g_array_insert_vals(garray,0,array,ARRAY_SIZE);
hgs
parents:
diff changeset
   175
	for(i=0; i<garray->len;i++)
hgs
parents:
diff changeset
   176
	{
hgs
parents:
diff changeset
   177
		std_log(LOG_FILENAME_LINE, "Current array elements %d is %d", i,g_array_index(garray, gint, i));
hgs
parents:
diff changeset
   178
	}
hgs
parents:
diff changeset
   179
hgs
parents:
diff changeset
   180
	garray = g_array_remove_range(garray,3,2); /*remove two elements from index 3 */
hgs
parents:
diff changeset
   181
hgs
parents:
diff changeset
   182
	if(garray == NULL)
hgs
parents:
diff changeset
   183
	{
hgs
parents:
diff changeset
   184
		std_log(LOG_FILENAME_LINE,"Elements not deleted properly by g_array_remove_range");
hgs
parents:
diff changeset
   185
		assert_failed = 1;
hgs
parents:
diff changeset
   186
		return ;
hgs
parents:
diff changeset
   187
hgs
parents:
diff changeset
   188
	}
hgs
parents:
diff changeset
   189
hgs
parents:
diff changeset
   190
	/*print the array elements */
hgs
parents:
diff changeset
   191
	for(i=0; i<garray->len;i++)
hgs
parents:
diff changeset
   192
	{
hgs
parents:
diff changeset
   193
		std_log(LOG_FILENAME_LINE, "Curent array element(after deletion) %d is %d", i,g_array_index(garray, gint, i));
hgs
parents:
diff changeset
   194
	}
hgs
parents:
diff changeset
   195
hgs
parents:
diff changeset
   196
hgs
parents:
diff changeset
   197
	ret = compare_array(garray, array_after_remove_index_range, ARRAY_SIZE_AFTER_REMOVE_INDEX_RANGE);
hgs
parents:
diff changeset
   198
	if(!ret)
hgs
parents:
diff changeset
   199
	{
hgs
parents:
diff changeset
   200
		std_log(LOG_FILENAME_LINE,"Elements not deleted properly");
hgs
parents:
diff changeset
   201
		assert_failed = 1;
hgs
parents:
diff changeset
   202
		g_array_free(garray,TRUE);
hgs
parents:
diff changeset
   203
		return ;
hgs
parents:
diff changeset
   204
	}
hgs
parents:
diff changeset
   205
	g_array_free(garray,TRUE);
hgs
parents:
diff changeset
   206
}
hgs
parents:
diff changeset
   207
hgs
parents:
diff changeset
   208
hgs
parents:
diff changeset
   209
void test_sort_array()
hgs
parents:
diff changeset
   210
{
hgs
parents:
diff changeset
   211
hgs
parents:
diff changeset
   212
	GArray *garray;
hgs
parents:
diff changeset
   213
	const int ARRAY_SIZE = 11;
hgs
parents:
diff changeset
   214
    gint array[ARRAY_SIZE] = {10,5,16,7,11,0,20,1,9,8,9};
hgs
parents:
diff changeset
   215
	gint sort_array[ARRAY_SIZE] = {0,1,5,7,8,9,9,10,11,16,20};
hgs
parents:
diff changeset
   216
hgs
parents:
diff changeset
   217
	gboolean ret;
hgs
parents:
diff changeset
   218
	int i;
hgs
parents:
diff changeset
   219
hgs
parents:
diff changeset
   220
    /* Test for sorting the array elements */
hgs
parents:
diff changeset
   221
hgs
parents:
diff changeset
   222
	garray = g_array_new (FALSE,FALSE,sizeof(gint));
hgs
parents:
diff changeset
   223
hgs
parents:
diff changeset
   224
	if(garray == NULL)
hgs
parents:
diff changeset
   225
	{
hgs
parents:
diff changeset
   226
		std_log(LOG_FILENAME_LINE, "Array not created");
hgs
parents:
diff changeset
   227
		assert_failed = 1;
hgs
parents:
diff changeset
   228
		return ;
hgs
parents:
diff changeset
   229
	}
hgs
parents:
diff changeset
   230
	g_array_insert_vals(garray,0,array,ARRAY_SIZE);
hgs
parents:
diff changeset
   231
hgs
parents:
diff changeset
   232
	g_array_sort(garray, sort);
hgs
parents:
diff changeset
   233
hgs
parents:
diff changeset
   234
	if(garray == NULL)
hgs
parents:
diff changeset
   235
	{
hgs
parents:
diff changeset
   236
		std_log(LOG_FILENAME_LINE, "Array not sorted");
hgs
parents:
diff changeset
   237
		assert_failed = 1;
hgs
parents:
diff changeset
   238
		return ;
hgs
parents:
diff changeset
   239
	}
hgs
parents:
diff changeset
   240
hgs
parents:
diff changeset
   241
	std_log(LOG_FILENAME_LINE,"SORTED ARRAY");
hgs
parents:
diff changeset
   242
hgs
parents:
diff changeset
   243
	for(i=0;i<garray->len;i++)
hgs
parents:
diff changeset
   244
	{
hgs
parents:
diff changeset
   245
		std_log(LOG_FILENAME_LINE, "Element %d is %d", i,g_array_index(garray, gint, i));
hgs
parents:
diff changeset
   246
	}
hgs
parents:
diff changeset
   247
hgs
parents:
diff changeset
   248
	ret = compare_array(garray, sort_array, ARRAY_SIZE);
hgs
parents:
diff changeset
   249
hgs
parents:
diff changeset
   250
	if(!ret)
hgs
parents:
diff changeset
   251
	{
hgs
parents:
diff changeset
   252
		std_log(LOG_FILENAME_LINE, "Array not sorted correctly");
hgs
parents:
diff changeset
   253
		assert_failed = 1;
hgs
parents:
diff changeset
   254
		return ;
hgs
parents:
diff changeset
   255
	}
hgs
parents:
diff changeset
   256
hgs
parents:
diff changeset
   257
	g_array_free(garray,TRUE);
hgs
parents:
diff changeset
   258
hgs
parents:
diff changeset
   259
hgs
parents:
diff changeset
   260
    garray = g_array_new (FALSE,FALSE,sizeof(gint));
hgs
parents:
diff changeset
   261
	if(garray == NULL)
hgs
parents:
diff changeset
   262
	{
hgs
parents:
diff changeset
   263
      std_log(LOG_FILENAME_LINE, "Array not created");
hgs
parents:
diff changeset
   264
	  return ;
hgs
parents:
diff changeset
   265
	}
hgs
parents:
diff changeset
   266
	g_array_insert_vals(garray,0,array,ARRAY_SIZE);
hgs
parents:
diff changeset
   267
hgs
parents:
diff changeset
   268
	g_array_sort_with_data (garray, sort_userdata, NULL);
hgs
parents:
diff changeset
   269
hgs
parents:
diff changeset
   270
	if(garray == NULL)
hgs
parents:
diff changeset
   271
	{
hgs
parents:
diff changeset
   272
		std_log(LOG_FILENAME_LINE, "Array not sorted with user data");
hgs
parents:
diff changeset
   273
		assert_failed = 1;
hgs
parents:
diff changeset
   274
		return ;
hgs
parents:
diff changeset
   275
	}
hgs
parents:
diff changeset
   276
hgs
parents:
diff changeset
   277
	std_log(LOG_FILENAME_LINE,"SORTED ARRAY WITH USERDATA");
hgs
parents:
diff changeset
   278
	for(i=0;i<garray->len;i++)
hgs
parents:
diff changeset
   279
	{
hgs
parents:
diff changeset
   280
		std_log(LOG_FILENAME_LINE, "Element %d is %d", i,g_array_index(garray, gint, i));
hgs
parents:
diff changeset
   281
	}
hgs
parents:
diff changeset
   282
hgs
parents:
diff changeset
   283
	ret = compare_array(garray, sort_array, ARRAY_SIZE);
hgs
parents:
diff changeset
   284
	if(!ret)
hgs
parents:
diff changeset
   285
	{
hgs
parents:
diff changeset
   286
		std_log(LOG_FILENAME_LINE, "Array not sorted correctly with user data");
hgs
parents:
diff changeset
   287
		assert_failed = 1;
hgs
parents:
diff changeset
   288
		return ;
hgs
parents:
diff changeset
   289
	}
hgs
parents:
diff changeset
   290
    g_array_free(garray,TRUE);
hgs
parents:
diff changeset
   291
}
hgs
parents:
diff changeset
   292
hgs
parents:
diff changeset
   293
hgs
parents:
diff changeset
   294
int main (void)
hgs
parents:
diff changeset
   295
{
hgs
parents:
diff changeset
   296
	test_test_remove_array_index_range();
hgs
parents:
diff changeset
   297
	test_sort_array();
hgs
parents:
diff changeset
   298
	test_remove_array_index();
hgs
parents:
diff changeset
   299
hgs
parents:
diff changeset
   300
	if(assert_failed)
hgs
parents:
diff changeset
   301
		std_log(LOG_FILENAME_LINE,"Test Failed");
hgs
parents:
diff changeset
   302
	else
hgs
parents:
diff changeset
   303
		std_log(LOG_FILENAME_LINE,"Test Successful");
hgs
parents:
diff changeset
   304
hgs
parents:
diff changeset
   305
    create_xml(0);
hgs
parents:
diff changeset
   306
    return 0;
hgs
parents:
diff changeset
   307
}
hgs
parents:
diff changeset
   308
hgs
parents:
diff changeset
   309