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