os/ossrv/glib/tsrc/BC/src/byte_array_test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     3
*
sl@0
     4
* This library is free software; you can redistribute it and/or
sl@0
     5
* modify it under the terms of the GNU Lesser General Public
sl@0
     6
* License as published by the Free Software Foundation; either
sl@0
     7
* version 2 of the License, or (at your option) any later version.
sl@0
     8
*
sl@0
     9
* This library is distributed in the hope that it will be useful,
sl@0
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
* Lesser General Public License for more details.
sl@0
    13
*
sl@0
    14
* You should have received a copy of the GNU Lesser General Public
sl@0
    15
* License along with this library; if not, write to the
sl@0
    16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    17
* Boston, MA 02111-1307, USA.
sl@0
    18
*
sl@0
    19
* Description:
sl@0
    20
*
sl@0
    21
*/
sl@0
    22
sl@0
    23
sl@0
    24
#undef G_DISABLE_ASSERT
sl@0
    25
#undef G_LOG_DOMAIN
sl@0
    26
sl@0
    27
#include <stdio.h>
sl@0
    28
#include <string.h>
sl@0
    29
#include "glib.h"
sl@0
    30
sl@0
    31
#ifdef SYMBIAN
sl@0
    32
#include "mrt2_glib2_test.h"
sl@0
    33
#endif /*SYMBIAN*/
sl@0
    34
sl@0
    35
int sort_func(gconstpointer _a,gconstpointer _b,gpointer _user_data)
sl@0
    36
{
sl@0
    37
	const guint8 *a = _a;
sl@0
    38
	const guint8 *b = _b;
sl@0
    39
	
sl@0
    40
	gint *user_data = _user_data;
sl@0
    41
	
sl@0
    42
	if(*user_data == 1)
sl@0
    43
	{
sl@0
    44
		if((*a) > (*b))
sl@0
    45
			return -1;
sl@0
    46
		else if((*a) == (*b))
sl@0
    47
			return 0;
sl@0
    48
		else
sl@0
    49
			return 1;
sl@0
    50
	}
sl@0
    51
	else
sl@0
    52
	{
sl@0
    53
		if((*a) < (*b))
sl@0
    54
			return -1;
sl@0
    55
		else if(*a == *b)
sl@0
    56
			return 0;
sl@0
    57
		else
sl@0
    58
			return 1;	
sl@0
    59
	}
sl@0
    60
}
sl@0
    61
sl@0
    62
int ascending(gconstpointer _a,gconstpointer _b)
sl@0
    63
{
sl@0
    64
	const guint8 *a = _a;
sl@0
    65
	const guint8 *b = _b;
sl@0
    66
	
sl@0
    67
	if((*a) < (*b))
sl@0
    68
		return -1;
sl@0
    69
	else if(*a == *b)
sl@0
    70
		return 0;
sl@0
    71
	else
sl@0
    72
		return 1;
sl@0
    73
	
sl@0
    74
}
sl@0
    75
sl@0
    76
int main (int   argc,
sl@0
    77
      char *argv[])
sl@0
    78
{
sl@0
    79
	GByteArray *gbarray;
sl@0
    80
	gint i;
sl@0
    81
	int user_data = 1;
sl@0
    82
	
sl@0
    83
	#ifdef SYMBIAN
sl@0
    84
	g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
sl@0
    85
	#endif /*SYMBIAN*/
sl@0
    86
sl@0
    87
	gbarray = g_byte_array_new ();
sl@0
    88
	
sl@0
    89
	g_byte_array_prepend(gbarray,(guint8 *)"c",1);
sl@0
    90
	g_byte_array_prepend(gbarray,(guint8 *)"b",1);
sl@0
    91
	g_byte_array_prepend(gbarray,(guint8 *)"a",1);
sl@0
    92
	
sl@0
    93
	g_assert(gbarray->data[0] == 'a');
sl@0
    94
	g_assert(gbarray->data[1] == 'b');
sl@0
    95
	g_assert(gbarray->data[2] == 'c');
sl@0
    96
	
sl@0
    97
	g_byte_array_remove_index(gbarray,1);
sl@0
    98
	
sl@0
    99
	g_assert(gbarray->data[0] == 'a');
sl@0
   100
	g_assert(gbarray->data[1] == 'c');
sl@0
   101
	
sl@0
   102
	g_byte_array_append(gbarray,(guint8 *)"b",1);
sl@0
   103
	
sl@0
   104
	g_byte_array_remove_index_fast(gbarray,1);
sl@0
   105
	
sl@0
   106
	g_assert(gbarray->data[1] == 'b');
sl@0
   107
	
sl@0
   108
	g_byte_array_append(gbarray,(guint8 *)"c",1);
sl@0
   109
	
sl@0
   110
	g_byte_array_append(gbarray,(guint8 *)"d",1);
sl@0
   111
	
sl@0
   112
	g_byte_array_append(gbarray,(guint8 *)"e",1);
sl@0
   113
	
sl@0
   114
	g_byte_array_remove_range(gbarray,0,3);
sl@0
   115
	
sl@0
   116
	g_assert(gbarray->data[0] == 'd');
sl@0
   117
	g_assert(gbarray->data[1] == 'e');
sl@0
   118
	
sl@0
   119
	g_byte_array_set_size(gbarray,10);
sl@0
   120
	
sl@0
   121
	g_assert(gbarray->len == 10);
sl@0
   122
	
sl@0
   123
	g_byte_array_free(gbarray,TRUE);
sl@0
   124
	
sl@0
   125
	gbarray = g_byte_array_sized_new (10);
sl@0
   126
	
sl@0
   127
	g_assert(gbarray->len == 0);
sl@0
   128
	
sl@0
   129
	g_byte_array_append(gbarray,(guint8 *)"c",1);
sl@0
   130
	g_byte_array_append(gbarray,(guint8 *)"b",1);
sl@0
   131
	g_byte_array_append(gbarray,(guint8 *)"a",1);
sl@0
   132
	
sl@0
   133
	g_byte_array_sort(gbarray,ascending);
sl@0
   134
	
sl@0
   135
	g_assert(gbarray->data[0] == 'a');
sl@0
   136
	g_assert(gbarray->data[1] == 'b');
sl@0
   137
	g_assert(gbarray->data[2] == 'c');
sl@0
   138
	
sl@0
   139
	g_byte_array_sort_with_data(gbarray,sort_func,&user_data);
sl@0
   140
	
sl@0
   141
	g_assert(gbarray->data[0] == 'c');
sl@0
   142
	g_assert(gbarray->data[1] == 'b');
sl@0
   143
	g_assert(gbarray->data[2] == 'a');
sl@0
   144
	
sl@0
   145
	g_byte_array_free(gbarray,TRUE);
sl@0
   146
	
sl@0
   147
	#if SYMBIAN
sl@0
   148
  	testResultXml("byte_array_test");
sl@0
   149
  	#endif /* EMULATOR */		
sl@0
   150
	
sl@0
   151
	return 0;
sl@0
   152
}