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