os/ossrv/glib/tsrc/BC/src/markup_test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
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:
sl@0
    20
*
sl@0
    21
*/
sl@0
    22
sl@0
    23
sl@0
    24
sl@0
    25
/* This test case is dependent on the markup filr 1.gmarkup placed in the c dir.
sl@0
    26
   Make sure that the file is present before this test case is run. Export the file
sl@0
    27
   */
sl@0
    28
sl@0
    29
#undef G_DISABLE_ASSERT
sl@0
    30
#undef G_LOG_DOMAIN
sl@0
    31
sl@0
    32
#include <stdio.h>
sl@0
    33
#include <glib.h>
sl@0
    34
sl@0
    35
#ifdef SYMBIAN
sl@0
    36
#include "mrt2_glib2_test.h"
sl@0
    37
#endif /*SYMBIAN*/
sl@0
    38
sl@0
    39
static int depth = 0;
sl@0
    40
sl@0
    41
sl@0
    42
static void
sl@0
    43
start_element_handler  (GMarkupParseContext *context,
sl@0
    44
                        const gchar         *element_name,
sl@0
    45
                        const gchar        **attribute_names,
sl@0
    46
                        const gchar        **attribute_values,
sl@0
    47
                        gpointer             user_data,
sl@0
    48
                        GError             **error)
sl@0
    49
{
sl@0
    50
  ++depth;
sl@0
    51
}
sl@0
    52
sl@0
    53
static void
sl@0
    54
end_element_handler    (GMarkupParseContext *context,
sl@0
    55
                        const gchar         *element_name,
sl@0
    56
                        gpointer             user_data,
sl@0
    57
                        GError             **error)
sl@0
    58
{
sl@0
    59
  --depth;
sl@0
    60
}
sl@0
    61
sl@0
    62
static void
sl@0
    63
text_handler           (GMarkupParseContext *context,
sl@0
    64
                        const gchar         *text,
sl@0
    65
                        gsize                text_len,
sl@0
    66
                        gpointer             user_data,
sl@0
    67
                        GError             **error)
sl@0
    68
{
sl@0
    69
sl@0
    70
}
sl@0
    71
sl@0
    72
sl@0
    73
static void
sl@0
    74
passthrough_handler    (GMarkupParseContext *context,
sl@0
    75
                        const gchar         *passthrough_text,
sl@0
    76
                        gsize                text_len,
sl@0
    77
                        gpointer             user_data,
sl@0
    78
                        GError             **error)
sl@0
    79
{
sl@0
    80
}
sl@0
    81
sl@0
    82
static void
sl@0
    83
error_handler          (GMarkupParseContext *context,
sl@0
    84
                        GError              *error,
sl@0
    85
                        gpointer             user_data)
sl@0
    86
{
sl@0
    87
  g_assert(FALSE && "markup_test failed");
sl@0
    88
  g_print(" %s\n", error->message);
sl@0
    89
}
sl@0
    90
sl@0
    91
static GMarkupParser parser = {
sl@0
    92
  start_element_handler,
sl@0
    93
  end_element_handler,
sl@0
    94
  text_handler,
sl@0
    95
  passthrough_handler,
sl@0
    96
  error_handler
sl@0
    97
};
sl@0
    98
sl@0
    99
static int
sl@0
   100
test_in_chunks (const gchar *contents,
sl@0
   101
                gint         length,
sl@0
   102
                gint         chunk_size)
sl@0
   103
{
sl@0
   104
  GMarkupParseContext *context;
sl@0
   105
  int i = 0;
sl@0
   106
  int line_number,char_number;
sl@0
   107
  
sl@0
   108
  context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
sl@0
   109
sl@0
   110
  while (i < length)
sl@0
   111
    {
sl@0
   112
      int this_chunk = MIN (length - i, chunk_size);
sl@0
   113
sl@0
   114
      if (!g_markup_parse_context_parse (context,
sl@0
   115
                                         contents + i,
sl@0
   116
                                         this_chunk,
sl@0
   117
                                         NULL))
sl@0
   118
        {
sl@0
   119
          g_markup_parse_context_free (context);
sl@0
   120
          return 1;
sl@0
   121
     
sl@0
   122
        }
sl@0
   123
    
sl@0
   124
	g_assert(g_markup_parse_context_get_element(context) == NULL || !strcmp(g_markup_parse_context_get_element(context),"foobar"));
sl@0
   125
	
sl@0
   126
	g_markup_parse_context_get_position(context,&line_number,&char_number);
sl@0
   127
	
sl@0
   128
	g_assert(line_number == 1 && char_number == 25);
sl@0
   129
    
sl@0
   130
    i += this_chunk;
sl@0
   131
    }
sl@0
   132
      
sl@0
   133
  if (!g_markup_parse_context_end_parse (context, NULL))
sl@0
   134
    {
sl@0
   135
      g_markup_parse_context_free (context);
sl@0
   136
      return 1;
sl@0
   137
    }
sl@0
   138
sl@0
   139
  g_markup_parse_context_free (context);
sl@0
   140
sl@0
   141
  return 0;
sl@0
   142
}
sl@0
   143
sl@0
   144
static int
sl@0
   145
test_file (const gchar *filename)
sl@0
   146
{
sl@0
   147
  gchar *contents;
sl@0
   148
  gsize  length;
sl@0
   149
  GError *error;
sl@0
   150
  GMarkupParseContext *context;
sl@0
   151
  
sl@0
   152
  error = NULL;
sl@0
   153
  if (!g_file_get_contents (filename,
sl@0
   154
                            &contents,
sl@0
   155
                            &length,
sl@0
   156
                            &error))
sl@0
   157
    {
sl@0
   158
      g_assert(FALSE && "c:\\1.gmarkup not found");
sl@0
   159
      fprintf (stderr, "%s\n", error->message);
sl@0
   160
      g_error_free (error);
sl@0
   161
      return 1;
sl@0
   162
    }
sl@0
   163
sl@0
   164
  context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
sl@0
   165
sl@0
   166
  if (!g_markup_parse_context_parse (context, contents, length, NULL))
sl@0
   167
    {
sl@0
   168
      g_markup_parse_context_free (context);
sl@0
   169
      return 1;
sl@0
   170
    }
sl@0
   171
sl@0
   172
  if (!g_markup_parse_context_end_parse (context, NULL))
sl@0
   173
    {
sl@0
   174
      g_markup_parse_context_free (context);
sl@0
   175
      return 1;
sl@0
   176
    }
sl@0
   177
sl@0
   178
  g_markup_parse_context_free (context);
sl@0
   179
sl@0
   180
  if (test_in_chunks (contents, length, 24) != 0)
sl@0
   181
    return 1;
sl@0
   182
  
sl@0
   183
  return 0;
sl@0
   184
}
sl@0
   185
sl@0
   186
void g_markup_printf_escaped_test()
sl@0
   187
{
sl@0
   188
	const char *store = "Fortnum & Mason";
sl@0
   189
	const char *item = "Tea";
sl@0
   190
	char *output;
sl@0
   191
 
sl@0
   192
	output = g_markup_printf_escaped ("<purchase>"
sl@0
   193
                                  "<store>%s</store>"
sl@0
   194
                                  "<item>%s</item>"
sl@0
   195
                                  "</purchase>",
sl@0
   196
                                  store, item);
sl@0
   197
                                  
sl@0
   198
    g_assert(!strcmp(output,"<purchase><store>Fortnum &amp; Mason</store><item>Tea</item></purchase>"));
sl@0
   199
}
sl@0
   200
sl@0
   201
int
sl@0
   202
main (int   argc,
sl@0
   203
      char *argv[])
sl@0
   204
{
sl@0
   205
  
sl@0
   206
	#ifdef SYMBIAN
sl@0
   207
	
sl@0
   208
	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
   209
	g_set_print_handler(mrtPrintHandler);
sl@0
   210
	#endif /*SYMBIAN*/
sl@0
   211
  
sl@0
   212
	test_file ("c:\\1.gmarkup");
sl@0
   213
	g_markup_printf_escaped_test();
sl@0
   214
	
sl@0
   215
	#ifdef SYMBIAN
sl@0
   216
  	testResultXml("markup_test");
sl@0
   217
  	#endif /* EMULATOR */
sl@0
   218
	
sl@0
   219
	return 0;
sl@0
   220
}
sl@0
   221