sl@0: /*
sl@0: * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0: *
sl@0: * This library is free software; you can redistribute it and/or
sl@0: * modify it under the terms of the GNU Lesser General Public
sl@0: * License as published by the Free Software Foundation; either
sl@0: * version 2 of the License, or (at your option) any later version.
sl@0: *
sl@0: * This library is distributed in the hope that it will be useful,
sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0: * Lesser General Public License for more details.
sl@0: *
sl@0: * You should have received a copy of the GNU Lesser General Public
sl@0: * License along with this library; if not, write to the
sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0: * Boston, MA 02111-1307, USA.
sl@0: *
sl@0: * Description:
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: 
sl@0: #undef G_DISABLE_ASSERT
sl@0: #undef G_LOG_DOMAIN
sl@0: 
sl@0: #include <stdio.h>
sl@0: #include <string.h>
sl@0: #include "glib.h"
sl@0: #include <stdlib.h>
sl@0: #include <glib/gprintf.h>
sl@0: 
sl@0: #ifdef SYMBIAN
sl@0: #include "mrt2_glib2_test.h"
sl@0: #endif /*SYMBIAN*/
sl@0: 
sl@0: void array_test()
sl@0: {
sl@0: 	GArray *garray;
sl@0: 	gint i;
sl@0: 	int a = 1;
sl@0: 	int b[3] = 
sl@0: 	{
sl@0: 		4,5,6
sl@0: 	};
sl@0: 		
sl@0: 	garray = g_array_new (FALSE,FALSE,sizeof(gint));
sl@0: 	
sl@0: 	g_array_append_val(garray,a);
sl@0: 	g_array_append_val(garray,a);
sl@0: 	g_array_append_val(garray,a);
sl@0: 	
sl@0: 	g_array_insert_vals(garray,0,b,3);
sl@0: 	
sl@0:     g_assert (g_array_index (garray, gint, 0) == 4);
sl@0:     g_assert (g_array_index (garray, gint, 1) == 5);
sl@0:     g_assert (g_array_index (garray, gint, 2) == 6);
sl@0: 	
sl@0: 	g_array_free(garray,TRUE);
sl@0: 	
sl@0: }
sl@0: 
sl@0: void g_ascii_strdown_test()
sl@0: {
sl@0: 	gchar str[] = "ABCd";
sl@0: 	gchar *str1;
sl@0: 	str1 = g_ascii_strdown(str,4);
sl@0: 	g_assert(str1[0] == 'a');
sl@0: 	g_assert(str1[1] == 'b');
sl@0: 	g_assert(str1[2] == 'c');
sl@0: 	g_assert(str1[3] == 'd');	
sl@0: }
sl@0: 
sl@0: void g_ascii_strup_test()
sl@0: {
sl@0: 	gchar str[] = "ABCd";
sl@0: 	gchar *str1;
sl@0: 	str1 = g_ascii_strup(str,4);
sl@0: 	g_assert(str1[0] == 'A');
sl@0: 	g_assert(str1[1] == 'B');
sl@0: 	g_assert(str1[2] == 'C');
sl@0: 	g_assert(str1[3] == 'D');	
sl@0: }
sl@0: 
sl@0: void g_fprintf_test()
sl@0: {
sl@0: 	FILE *fp;
sl@0: 	char *teststring = "testing";
sl@0: 	int retVal;
sl@0: 	fp = fopen("c:\\test.txt","w");
sl@0: 	if(fp)
sl@0: 	{
sl@0: 		g_assert(g_fprintf(fp,"%s",teststring) == strlen(teststring));
sl@0: 		fclose(fp);
sl@0: 	}
sl@0: }
sl@0: 
sl@0: void g_application_name_test()
sl@0: {
sl@0: 	gchar *app_name = "Test App";
sl@0: 	g_set_application_name(app_name);
sl@0: 	g_assert(!strcmp(g_get_application_name(),app_name));
sl@0: }
sl@0: 
sl@0: void g_listenv_test()
sl@0: {
sl@0: 	gchar **e = NULL;
sl@0: 	int i;
sl@0: 	e = g_listenv();
sl@0: 	g_assert(e!=NULL);
sl@0: 	g_strfreev(e);
sl@0: }
sl@0: 
sl@0: void g_direct_equal_test()
sl@0: {
sl@0: 	int *i,a=1,*j;
sl@0: 	i = &a;
sl@0: 	j=i;
sl@0: 	g_assert(g_direct_equal(i,j));
sl@0: 	j++;
sl@0: 	g_assert(!g_direct_equal(i,j));
sl@0: }
sl@0: 
sl@0: void g_direct_hash_test()
sl@0: {
sl@0: 	int *i,a=1,j;
sl@0: 	int hash_value;
sl@0: 	i = &a;	
sl@0: 	j = (gint)i;
sl@0: 	g_assert(g_direct_hash(i) == j);
sl@0: }
sl@0: 
sl@0: void g_bit_nth_lsf_test()
sl@0: {
sl@0: 	gulong mask = 15;
sl@0: 	
sl@0: 	// 15 is 00000000 ........ 1111. Therefore the position of first 1 should be 0
sl@0: 	g_assert(g_bit_nth_lsf(mask,-1) == 0);
sl@0: }
sl@0: 
sl@0: void g_bit_nth_msf_test()
sl@0: {
sl@0: 	gulong mask = 15;
sl@0: 	
sl@0: 	// 15 is 00000000 ........ 1111. Therefore the position of last 1 should be 3
sl@0: 	g_assert(g_bit_nth_msf(mask,-1) == 3);
sl@0: }
sl@0: 
sl@0: void g_basename_test()
sl@0: {
sl@0: 	const gchar *filename;
sl@0: 	filename = g_basename("c:\\test\\test.txt");
sl@0: 	g_assert(!strcmp(filename,"test.txt"));
sl@0: }
sl@0: 
sl@0: 
sl@0: void function()
sl@0: {
sl@0: 	return;
sl@0: }
sl@0: 
sl@0: void g_atexit_test()
sl@0: {
sl@0: 	g_atexit(function);
sl@0: }
sl@0: 
sl@0: void g_bit_storage_test()
sl@0: {
sl@0: 	g_assert(g_bit_storage(8) == 4);
sl@0: }
sl@0: 
sl@0: void g_filename_display_basename_test()
sl@0: {
sl@0: 	g_assert(!strcmp(g_filename_display_basename("c:/test/test.txt"),"test.txt"));
sl@0: }
sl@0: 
sl@0: void g_find_program_in_path_test()
sl@0: {
sl@0: 	char *program_name = g_find_program_in_path("misc_test.exe");
sl@0: }
sl@0: 
sl@0: void g_atomic_test()
sl@0: {
sl@0: 	int i = 5;
sl@0: 	int j;
sl@0: 	char *p,*q;
sl@0: 	
sl@0: 	j = g_atomic_int_get(&i);
sl@0: 	g_assert(j == i);
sl@0: 	
sl@0: 	p = g_malloc(1);
sl@0: 	q = g_atomic_pointer_get(&p);
sl@0: 	g_assert(p == q);
sl@0: 	
sl@0: 	g_free(p);
sl@0: }
sl@0: 
sl@0: void g_error_test()
sl@0: {
sl@0: 	GError err,*err_copy = NULL;
sl@0: 	err.domain = 1;
sl@0: 	err.code = 5;
sl@0: 	err.message = "test";
sl@0: 	err_copy = g_error_copy(&err);
sl@0: 	g_assert(err_copy->code == 5 && err_copy->code == 5 && !strcmp(err_copy->message,"test"));
sl@0: 	g_error_free(err_copy);
sl@0: 	err_copy = NULL;
sl@0: 	err_copy = g_error_new_literal(err.domain,err.code,"test is %s");
sl@0: 	g_assert(err_copy->code == 5 && err_copy->code == 5 && !strcmp(err_copy->message,"test is %s"));
sl@0: 	g_error_free(err_copy);
sl@0: 	err_copy = NULL;
sl@0: }
sl@0: 
sl@0: static guint
sl@0: my_hash (gconstpointer key)
sl@0: {
sl@0:   return (guint) *((const gint*) key);
sl@0: }
sl@0: 
sl@0: static gboolean
sl@0: my_hash_equal (gconstpointer a,
sl@0: 	       gconstpointer b)
sl@0: {
sl@0:   return *((const gint*) a) == *((const gint*) b);
sl@0: }
sl@0: 
sl@0: static gboolean find_first     (gpointer key, 
sl@0: 				gpointer value, 
sl@0: 				gpointer user_data)
sl@0: {
sl@0:   gint *v = value; 
sl@0:   gint *test = user_data;
sl@0:   return (*v == *test);
sl@0: }
sl@0: 
sl@0: gboolean func(gpointer key,gpointer value,gpointer user_data)
sl@0: {
sl@0: 	gint *key1 = (int *)key;
sl@0: 	gint *value1 = (int *)value;
sl@0: 	gint *user_data1 = (int *)user_data;
sl@0: 	if(*key1 == *user_data1 && *value1 == *user_data1)
sl@0: 		return TRUE;
sl@0: 	else
sl@0: 		return FALSE;
sl@0: }
sl@0: 
sl@0: void hash_test()
sl@0: {
sl@0: 	GHashTable *hash_table;
sl@0: 	gint i;
sl@0: 	gint value = 4;
sl@0: 	gint *pvalue; 
sl@0: 	int array[10];
sl@0: 	int count = 0;
sl@0: 
sl@0: 	hash_table = g_hash_table_new (my_hash, my_hash_equal);
sl@0: 	for (i = 0; i < 10; i++)
sl@0: 	{
sl@0: 	  array[i] = i;
sl@0: 	  g_hash_table_insert (hash_table, &array[i], &array[i]);
sl@0: 	}
sl@0: 	
sl@0: 	g_assert(g_hash_table_steal(hash_table,&value));
sl@0: 	
sl@0: 	pvalue = g_hash_table_find (hash_table, find_first, &value);
sl@0: 	
sl@0: 	//checks if g_hash_table_steal worked or not
sl@0: 	g_assert(pvalue == NULL);
sl@0: 	
sl@0: 	value = 5;
sl@0: 	
sl@0: 	pvalue = g_hash_table_find (hash_table, find_first, &value);
sl@0: 	
sl@0: 	count = g_hash_table_foreach_steal(hash_table,func,&value);
sl@0: 	
sl@0: 	pvalue = g_hash_table_find (hash_table, find_first, &value);
sl@0: 	
sl@0: 	g_assert(count == 1 && pvalue == NULL);
sl@0: 
sl@0: }
sl@0: 
sl@0: void check_version_test()
sl@0: {
sl@0:     const char *x = glib_check_version(2,8,3);
sl@0: 	g_assert(x == NULL);
sl@0: }
sl@0: 
sl@0: int main (int   argc,
sl@0:       char *argv[])
sl@0: {
sl@0: 	#ifdef SYMBIAN
sl@0: 	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: 	#endif /*SYMBIAN*/
sl@0: 	
sl@0: 	array_test();
sl@0: 	g_ascii_strup_test();
sl@0: 	g_ascii_strdown_test();
sl@0: 	g_fprintf_test();
sl@0: 	g_application_name_test();
sl@0: 	g_listenv_test();
sl@0: 	g_direct_equal_test();
sl@0: 	g_direct_hash_test();
sl@0: 	g_bit_nth_lsf_test();
sl@0: 	g_bit_nth_msf_test();
sl@0: 	g_basename_test();
sl@0: 	g_atexit_test();
sl@0: 	g_bit_storage_test();
sl@0: 	g_filename_display_basename_test();
sl@0: 	g_find_program_in_path_test();
sl@0: 	g_atomic_test();
sl@0: 	g_error_test();
sl@0: 	hash_test();
sl@0: 	check_version_test();
sl@0: 	
sl@0: 	#ifdef SYMBIAN
sl@0:   	testResultXml("misc_test");
sl@0:   	#endif /* EMULATOR */
sl@0: 	
sl@0: 	return 0;
sl@0: }