os/ossrv/glib/tsrc/BC/src/tscanner.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:  ?Description
    20 *
    21 */
    22 
    23 
    24 #undef G_DISABLE_ASSERT
    25 #undef G_LOG_DOMAIN
    26 
    27 
    28 #include <stdio.h>
    29 #include <string.h>
    30 #include <glib.h>
    31 #include <fcntl.h>
    32 #include <goption.h>
    33 #include <unistd.h>
    34 #include <glib/gstdio.h>
    35 
    36 #ifdef SYMBIAN
    37 #include "mrt2_glib2_test.h"
    38 #endif /*SYMBIAN*/
    39 
    40 #define	C2P(c)		((gpointer) ((long) (c)))
    41 #define GINT_TO_POINTER(i)	((gpointer)  (i))
    42 #define GPOINTER_TO_INT(p)	((gint)   (p))
    43 #define TESTPASS	1
    44 #define TESTFAIL	0
    45 
    46 //Support for scanner
    47 void hash_func(gpointer key,gpointer value,gpointer user_data)
    48 {
    49 	
    50 }
    51 
    52 void err_func(GScanner* scanner,gchar* message, gboolean error)
    53 {
    54 	char s[] = "unexpected string constant \"efgh\", expected number (float) - error";
    55 	g_assert(!strcmp(s,message));
    56 	
    57 }
    58 
    59 //Test for scanner
    60 void tg_scanner_tests()
    61 {
    62 	GScanner *scanner;
    63 	int i,j,fd;
    64 	const gint TOKEN_OPEN = G_TOKEN_LEFT_BRACE;
    65 	const gint TOKEN_CLOSE = G_TOKEN_RIGHT_BRACE;
    66 	const gint TOKEN_INT = G_TOKEN_INT;
    67 	const gint TOKEN_CH = G_TOKEN_STRING;
    68 	
    69 	gint cur_pos,err;
    70 	GTokenValue val;
    71 	GTokenType token;
    72 	gchar* g_scanner_cur_value_str;
    73 	gpointer op;
    74 	char* g_scanner_peek_next_token_str;
    75 	gchar ip_text[]="(30)";
    76 	
    77 	err = TRUE;
    78 	scanner = g_scanner_new(NULL);
    79 	fd = g_open ("c:\scanfile.txt", O_RDONLY, 0);
    80 	g_scanner_input_file (scanner, fd);
    81 	scanner->config->symbol_2_token = TRUE;
    82 	scanner->msg_handler = err_func;
    83 	
    84 	g_scanner_scope_add_symbol(scanner,0,"leftbrace",GUINT_TO_POINTER (TOKEN_OPEN));
    85 	g_scanner_scope_add_symbol(scanner,0,"rightbrace",GUINT_TO_POINTER (TOKEN_CLOSE));
    86 	g_scanner_scope_add_symbol(scanner,0,"intval",GUINT_TO_POINTER (TOKEN_INT));
    87 	g_scanner_scope_add_symbol(scanner,0,"strval",GUINT_TO_POINTER (TOKEN_CH));
    88 
    89 	for(i=0;i<10;i++)
    90 	{
    91 	token = g_scanner_get_next_token (scanner);
    92 	cur_pos = g_scanner_cur_position (scanner);
    93 	}
    94 	
    95 	//Tests begin here:
    96 	
    97 	g_assert(g_scanner_cur_line(scanner) == 2);  //g_scanner_cur_line test
    98 	g_assert(g_scanner_cur_position(scanner) == 11);	//g_scanner_cur_position test
    99 	g_assert(g_scanner_cur_token (scanner) == G_TOKEN_STRING); //g_scanner_cur_token test
   100 	val = g_scanner_cur_value (scanner); //g_scanner_cur_value test
   101 	g_scanner_cur_value_str = val.v_string;
   102 	g_assert(!strcmp(g_scanner_cur_value_str,"efgh"));
   103 	g_assert(g_scanner_peek_next_token(scanner) == G_TOKEN_LEFT_BRACE);	//g_scanner_peek_next_token test
   104 	g_assert(g_scanner_scope_lookup_symbol(scanner,0,"intval") == GUINT_TO_POINTER (TOKEN_INT));	//g_scanner_scope_lookup_symbol test
   105 	g_assert(g_scanner_lookup_symbol(scanner,"intval") == GUINT_TO_POINTER (TOKEN_INT));	//g_scanner_lookup_symbol test
   106 	g_assert(g_scanner_set_scope(scanner,14) == 0);	//g_scanner_set_scope..set to 14
   107 	
   108 	g_scanner_scope_remove_symbol(scanner,14,"leftbrace");
   109 	g_assert(g_scanner_lookup_symbol(scanner,"leftbrace") == NULL);	//g_scanner_scope_remove_symbol test
   110 	
   111 	g_scanner_scope_foreach_symbol(scanner,14,hash_func,GUINT_TO_POINTER(G_TOKEN_LEFT_PAREN));
   112 	g_assert(g_scanner_lookup_symbol(scanner,"intval") == NULL);	//g_scanner_scope_foreach_symbol test
   113 	
   114 	
   115 	g_scanner_unexp_token(scanner,G_TOKEN_FLOAT,NULL,NULL,"float","error",err);
   116 	
   117 	//Test for g_scanner_input_text
   118 	g_scanner_set_scope(scanner,0);
   119 	g_scanner_input_text(scanner,ip_text,strlen(ip_text));
   120 	for(i=0;i<3;i++)
   121 	{
   122 	token = g_scanner_get_next_token (scanner);
   123 	}
   124 	g_assert(g_scanner_cur_token (scanner) == G_TOKEN_RIGHT_PAREN); //g_scanner_input_text test
   125 	
   126 	close(fd);
   127 	g_scanner_destroy (scanner);
   128 	
   129 }
   130 
   131 
   132 
   133 int main (int argc,char *argv[])
   134 {
   135 
   136 	#ifdef SYMBIAN
   137  
   138  	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);
   139  	#endif /*SYMBIAN*/
   140  	
   141  	tg_scanner_tests();
   142  #ifdef SYMBIAN
   143   testResultXml("tscanner");
   144 #endif /* EMULATOR */
   145  	return 0;
   146 }