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