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