1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/src/tscanner.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,146 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.6 +*
1.7 +* This library is free software; you can redistribute it and/or
1.8 +* modify it under the terms of the GNU Lesser General Public
1.9 +* License as published by the Free Software Foundation; either
1.10 +* version 2 of the License, or (at your option) any later version.
1.11 +*
1.12 +* This library is distributed in the hope that it will be useful,
1.13 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 +* Lesser General Public License for more details.
1.16 +*
1.17 +* You should have received a copy of the GNU Lesser General Public
1.18 +* License along with this library; if not, write to the
1.19 +* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.20 +* Boston, MA 02111-1307, USA.
1.21 +*
1.22 +* Description: ?Description
1.23 +*
1.24 +*/
1.25 +
1.26 +
1.27 +#undef G_DISABLE_ASSERT
1.28 +#undef G_LOG_DOMAIN
1.29 +
1.30 +
1.31 +#include <stdio.h>
1.32 +#include <string.h>
1.33 +#include <glib.h>
1.34 +#include <fcntl.h>
1.35 +#include <goption.h>
1.36 +#include <unistd.h>
1.37 +#include <glib/gstdio.h>
1.38 +
1.39 +#ifdef SYMBIAN
1.40 +#include "mrt2_glib2_test.h"
1.41 +#endif /*SYMBIAN*/
1.42 +
1.43 +#define C2P(c) ((gpointer) ((long) (c)))
1.44 +#define GINT_TO_POINTER(i) ((gpointer) (i))
1.45 +#define GPOINTER_TO_INT(p) ((gint) (p))
1.46 +#define TESTPASS 1
1.47 +#define TESTFAIL 0
1.48 +
1.49 +//Support for scanner
1.50 +void hash_func(gpointer key,gpointer value,gpointer user_data)
1.51 +{
1.52 +
1.53 +}
1.54 +
1.55 +void err_func(GScanner* scanner,gchar* message, gboolean error)
1.56 +{
1.57 + char s[] = "unexpected string constant \"efgh\", expected number (float) - error";
1.58 + g_assert(!strcmp(s,message));
1.59 +
1.60 +}
1.61 +
1.62 +//Test for scanner
1.63 +void tg_scanner_tests()
1.64 +{
1.65 + GScanner *scanner;
1.66 + int i,j,fd;
1.67 + const gint TOKEN_OPEN = G_TOKEN_LEFT_BRACE;
1.68 + const gint TOKEN_CLOSE = G_TOKEN_RIGHT_BRACE;
1.69 + const gint TOKEN_INT = G_TOKEN_INT;
1.70 + const gint TOKEN_CH = G_TOKEN_STRING;
1.71 +
1.72 + gint cur_pos,err;
1.73 + GTokenValue val;
1.74 + GTokenType token;
1.75 + gchar* g_scanner_cur_value_str;
1.76 + gpointer op;
1.77 + char* g_scanner_peek_next_token_str;
1.78 + gchar ip_text[]="(30)";
1.79 +
1.80 + err = TRUE;
1.81 + scanner = g_scanner_new(NULL);
1.82 + fd = g_open ("c:\scanfile.txt", O_RDONLY, 0);
1.83 + g_scanner_input_file (scanner, fd);
1.84 + scanner->config->symbol_2_token = TRUE;
1.85 + scanner->msg_handler = err_func;
1.86 +
1.87 + g_scanner_scope_add_symbol(scanner,0,"leftbrace",GUINT_TO_POINTER (TOKEN_OPEN));
1.88 + g_scanner_scope_add_symbol(scanner,0,"rightbrace",GUINT_TO_POINTER (TOKEN_CLOSE));
1.89 + g_scanner_scope_add_symbol(scanner,0,"intval",GUINT_TO_POINTER (TOKEN_INT));
1.90 + g_scanner_scope_add_symbol(scanner,0,"strval",GUINT_TO_POINTER (TOKEN_CH));
1.91 +
1.92 + for(i=0;i<10;i++)
1.93 + {
1.94 + token = g_scanner_get_next_token (scanner);
1.95 + cur_pos = g_scanner_cur_position (scanner);
1.96 + }
1.97 +
1.98 + //Tests begin here:
1.99 +
1.100 + g_assert(g_scanner_cur_line(scanner) == 2); //g_scanner_cur_line test
1.101 + g_assert(g_scanner_cur_position(scanner) == 11); //g_scanner_cur_position test
1.102 + g_assert(g_scanner_cur_token (scanner) == G_TOKEN_STRING); //g_scanner_cur_token test
1.103 + val = g_scanner_cur_value (scanner); //g_scanner_cur_value test
1.104 + g_scanner_cur_value_str = val.v_string;
1.105 + g_assert(!strcmp(g_scanner_cur_value_str,"efgh"));
1.106 + g_assert(g_scanner_peek_next_token(scanner) == G_TOKEN_LEFT_BRACE); //g_scanner_peek_next_token test
1.107 + g_assert(g_scanner_scope_lookup_symbol(scanner,0,"intval") == GUINT_TO_POINTER (TOKEN_INT)); //g_scanner_scope_lookup_symbol test
1.108 + g_assert(g_scanner_lookup_symbol(scanner,"intval") == GUINT_TO_POINTER (TOKEN_INT)); //g_scanner_lookup_symbol test
1.109 + g_assert(g_scanner_set_scope(scanner,14) == 0); //g_scanner_set_scope..set to 14
1.110 +
1.111 + g_scanner_scope_remove_symbol(scanner,14,"leftbrace");
1.112 + g_assert(g_scanner_lookup_symbol(scanner,"leftbrace") == NULL); //g_scanner_scope_remove_symbol test
1.113 +
1.114 + g_scanner_scope_foreach_symbol(scanner,14,hash_func,GUINT_TO_POINTER(G_TOKEN_LEFT_PAREN));
1.115 + g_assert(g_scanner_lookup_symbol(scanner,"intval") == NULL); //g_scanner_scope_foreach_symbol test
1.116 +
1.117 +
1.118 + g_scanner_unexp_token(scanner,G_TOKEN_FLOAT,NULL,NULL,"float","error",err);
1.119 +
1.120 + //Test for g_scanner_input_text
1.121 + g_scanner_set_scope(scanner,0);
1.122 + g_scanner_input_text(scanner,ip_text,strlen(ip_text));
1.123 + for(i=0;i<3;i++)
1.124 + {
1.125 + token = g_scanner_get_next_token (scanner);
1.126 + }
1.127 + g_assert(g_scanner_cur_token (scanner) == G_TOKEN_RIGHT_PAREN); //g_scanner_input_text test
1.128 +
1.129 + close(fd);
1.130 + g_scanner_destroy (scanner);
1.131 +
1.132 +}
1.133 +
1.134 +
1.135 +
1.136 +int main (int argc,char *argv[])
1.137 +{
1.138 +
1.139 + #ifdef SYMBIAN
1.140 +
1.141 + 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);
1.142 + #endif /*SYMBIAN*/
1.143 +
1.144 + tg_scanner_tests();
1.145 + #ifdef SYMBIAN
1.146 + testResultXml("tscanner");
1.147 +#endif /* EMULATOR */
1.148 + return 0;
1.149 +}
1.150 \ No newline at end of file