glib/tests/markup-test.c
changeset 18 47c74d1534e1
child 34 5fae379060a7
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #undef G_DISABLE_ASSERT
       
    19 #undef G_LOG_DOMAIN
       
    20 
       
    21 #include <stdio.h>
       
    22 #include <glib.h>
       
    23 
       
    24 #ifdef __SYMBIAN32__
       
    25 #include "mrt2_glib2_test.h"
       
    26 #endif /*__SYMBIAN32__*/
       
    27 
       
    28 
       
    29 static int depth = 0;
       
    30 
       
    31 static void
       
    32 indent (int extra)
       
    33 {
       
    34   int i = 0;
       
    35   while (i < depth)
       
    36     {
       
    37       fputs ("  ", stdout);
       
    38       ++i;
       
    39     }
       
    40 }
       
    41 
       
    42 static void
       
    43 start_element_handler  (GMarkupParseContext *context,
       
    44                         const gchar         *element_name,
       
    45                         const gchar        **attribute_names,
       
    46                         const gchar        **attribute_values,
       
    47                         gpointer             user_data,
       
    48                         GError             **error)
       
    49 {
       
    50   int i;
       
    51   
       
    52   indent (0);
       
    53   printf ("ELEMENT '%s'\n", element_name);
       
    54 
       
    55   i = 0;
       
    56   while (attribute_names[i] != NULL)
       
    57     {
       
    58       indent (1);
       
    59 
       
    60       printf ("%s=\"%s\"\n",
       
    61               attribute_names[i],
       
    62               attribute_values[i]);
       
    63       
       
    64       ++i;
       
    65     }
       
    66   
       
    67   ++depth;
       
    68 }
       
    69 
       
    70 static void
       
    71 end_element_handler    (GMarkupParseContext *context,
       
    72                         const gchar         *element_name,
       
    73                         gpointer             user_data,
       
    74                         GError             **error)
       
    75 {
       
    76   --depth;
       
    77   indent (0);
       
    78   printf ("END '%s'\n", element_name);
       
    79   }
       
    80 
       
    81 static void
       
    82 text_handler                      (GMarkupParseContext *context,
       
    83                         const gchar         *text,
       
    84                         gsize                text_len,
       
    85                         gpointer             user_data,
       
    86                         GError             **error)
       
    87 {
       
    88   indent (0);
       
    89   printf ("TEXT '%.*s'\n", (int)text_len, text);
       
    90 }
       
    91 
       
    92 
       
    93 static void
       
    94 passthrough_handler    (GMarkupParseContext *context,
       
    95                         const gchar         *passthrough_text,
       
    96                         gsize                text_len,
       
    97                         gpointer             user_data,
       
    98                         GError             **error)
       
    99 {
       
   100   indent (0);
       
   101 
       
   102   printf ("PASS '%.*s'\n", (int)text_len, passthrough_text);
       
   103 }
       
   104 
       
   105 static void
       
   106 error_handler          (GMarkupParseContext *context,
       
   107                         GError              *error,
       
   108                         gpointer             user_data)
       
   109 {
       
   110   fprintf (stderr, " %s\n", error->message);
       
   111 }
       
   112 
       
   113 static const GMarkupParser parser = {
       
   114   start_element_handler,
       
   115   end_element_handler,
       
   116   text_handler,
       
   117   passthrough_handler,
       
   118   error_handler
       
   119 };
       
   120 
       
   121 static const GMarkupParser silent_parser = {
       
   122   NULL,
       
   123   NULL,
       
   124   NULL,
       
   125   NULL,
       
   126   error_handler
       
   127 };
       
   128 
       
   129 static int
       
   130 test_in_chunks (const gchar *contents,
       
   131                 gint         length,
       
   132                 gint         chunk_size)
       
   133 {
       
   134   GMarkupParseContext *context;
       
   135   int i = 0;
       
   136   
       
   137   context = g_markup_parse_context_new (&silent_parser, 0, NULL, NULL);
       
   138 
       
   139   while (i < length)
       
   140     {
       
   141       int this_chunk = MIN (length - i, chunk_size);
       
   142 
       
   143       if (!g_markup_parse_context_parse (context,
       
   144                                          contents + i,
       
   145                                          this_chunk,
       
   146                                          NULL))
       
   147         {
       
   148           g_markup_parse_context_free (context);
       
   149           return 1;
       
   150         }
       
   151 
       
   152       i += this_chunk;
       
   153     }
       
   154       
       
   155   if (!g_markup_parse_context_end_parse (context, NULL))
       
   156     {
       
   157       g_markup_parse_context_free (context);
       
   158       return 1;
       
   159     }
       
   160 
       
   161   g_markup_parse_context_free (context);
       
   162 
       
   163   return 0;
       
   164 }
       
   165 
       
   166 static int
       
   167 test_file (const gchar *filename)
       
   168 {
       
   169   gchar *contents;
       
   170   gsize  length;
       
   171   GError *error;
       
   172   GMarkupParseContext *context;
       
   173   
       
   174   error = NULL;
       
   175   if (!g_file_get_contents (filename,
       
   176                             &contents,
       
   177                             &length,
       
   178                             &error))
       
   179     {
       
   180       fprintf (stderr, "%s\n", error->message);
       
   181       g_error_free (error);
       
   182       return 1;
       
   183     }
       
   184 
       
   185   context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
       
   186 
       
   187   if (!g_markup_parse_context_parse (context, contents, length, NULL))
       
   188     {
       
   189       g_markup_parse_context_free (context);
       
   190       return 1;
       
   191     }
       
   192 
       
   193   if (!g_markup_parse_context_end_parse (context, NULL))
       
   194     {
       
   195       g_markup_parse_context_free (context);
       
   196       return 1;
       
   197     }
       
   198 
       
   199   g_markup_parse_context_free (context);
       
   200 
       
   201   /* A byte at a time */
       
   202   if (test_in_chunks (contents, length, 1) != 0)
       
   203     return 1;
       
   204 
       
   205   /* 2 bytes */
       
   206   if (test_in_chunks (contents, length, 2) != 0)
       
   207     return 1;
       
   208 
       
   209   /*5 bytes */
       
   210   if (test_in_chunks (contents, length, 5) != 0)
       
   211     return 1;
       
   212   
       
   213   /* 12 bytes */
       
   214   if (test_in_chunks (contents, length, 12) != 0)
       
   215     return 1;
       
   216   
       
   217   /* 1024 bytes */
       
   218   if (test_in_chunks (contents, length, 1024) != 0)
       
   219     return 1;
       
   220 
       
   221   return 0;
       
   222 }
       
   223 
       
   224 int
       
   225 main (int   argc,
       
   226       char *argv[])
       
   227 {
       
   228   int retval;
       
   229   
       
   230   #ifdef __SYMBIAN32__
       
   231   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);
       
   232   g_set_print_handler(mrtPrintHandler);
       
   233   #endif /*__SYMBIAN32__*/
       
   234 
       
   235   if (argc > 1)
       
   236   {
       
   237     retval = test_file (argv[1]);
       
   238 	  #ifdef __SYMBIAN32__
       
   239   testResultXml("markup-test");
       
   240   #endif /* EMULATOR */
       
   241   return retval;
       
   242 }
       
   243   else
       
   244     {
       
   245       fprintf (stderr, "Give a markup file on the command line\n");
       
   246       return 1;
       
   247     }  
       
   248 }
       
   249