|
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/ |
|
2 #undef G_DISABLE_ASSERT |
|
3 #undef G_LOG_DOMAIN |
|
4 |
|
5 #include <stdio.h> |
|
6 #include <glib.h> |
|
7 |
|
8 #ifdef __SYMBIAN32__ |
|
9 #include "mrt2_glib2_test.h" |
|
10 #endif /*__SYMBIAN32__*/ |
|
11 |
|
12 |
|
13 static int depth = 0; |
|
14 |
|
15 static void |
|
16 indent (int extra) |
|
17 { |
|
18 |
|
19 } |
|
20 |
|
21 static void |
|
22 start_element_handler (GMarkupParseContext *context, |
|
23 const gchar *element_name, |
|
24 const gchar **attribute_names, |
|
25 const gchar **attribute_values, |
|
26 gpointer user_data, |
|
27 GError **error) |
|
28 { |
|
29 ++depth; |
|
30 } |
|
31 |
|
32 static void |
|
33 end_element_handler (GMarkupParseContext *context, |
|
34 const gchar *element_name, |
|
35 gpointer user_data, |
|
36 GError **error) |
|
37 { |
|
38 --depth; |
|
39 indent (0); |
|
40 } |
|
41 |
|
42 static void |
|
43 text_handler (GMarkupParseContext *context, |
|
44 const gchar *text, |
|
45 gsize text_len, |
|
46 gpointer user_data, |
|
47 GError **error) |
|
48 { |
|
49 indent (0); |
|
50 } |
|
51 |
|
52 |
|
53 static void |
|
54 passthrough_handler (GMarkupParseContext *context, |
|
55 const gchar *passthrough_text, |
|
56 gsize text_len, |
|
57 gpointer user_data, |
|
58 GError **error) |
|
59 { |
|
60 indent (0); |
|
61 } |
|
62 |
|
63 static void |
|
64 error_handler (GMarkupParseContext *context, |
|
65 GError *error, |
|
66 gpointer user_data) |
|
67 { |
|
68 g_print(" %s\n", error->message); |
|
69 g_assert(FALSE && "markup-test failed"); |
|
70 } |
|
71 |
|
72 static GMarkupParser parser = { |
|
73 start_element_handler, |
|
74 end_element_handler, |
|
75 text_handler, |
|
76 passthrough_handler, |
|
77 error_handler |
|
78 }; |
|
79 |
|
80 static int |
|
81 test_in_chunks (const gchar *contents, |
|
82 gint length, |
|
83 gint chunk_size) |
|
84 { |
|
85 GMarkupParseContext *context; |
|
86 int i = 0; |
|
87 |
|
88 context = g_markup_parse_context_new (&parser, 0, NULL, NULL); |
|
89 if(!context) |
|
90 { |
|
91 g_print("markup-test.c : g_markup_parse_context_new failed @ line : %d",__LINE__); |
|
92 g_assert(FALSE && "markup-test failed"); |
|
93 return 1; |
|
94 } |
|
95 |
|
96 while (i < length) |
|
97 { |
|
98 int this_chunk = MIN (length - i, chunk_size); |
|
99 |
|
100 if (!g_markup_parse_context_parse (context, |
|
101 contents + i, |
|
102 this_chunk, |
|
103 NULL)) |
|
104 { |
|
105 g_print("markup-test.c : g_markup_parse_context_parse failed "); |
|
106 g_markup_parse_context_free (context); |
|
107 g_assert(FALSE && "markup-test failed"); |
|
108 return 1; |
|
109 } |
|
110 |
|
111 i += this_chunk; |
|
112 } |
|
113 |
|
114 if (!g_markup_parse_context_end_parse (context, NULL)) |
|
115 { |
|
116 g_print("markup-test.c : g_markup_parse_context_end_parse failed "); |
|
117 g_assert(FALSE && "markup-test failed"); |
|
118 g_markup_parse_context_free (context); |
|
119 return 1; |
|
120 } |
|
121 |
|
122 g_markup_parse_context_free (context); |
|
123 |
|
124 return 0; |
|
125 } |
|
126 |
|
127 static int |
|
128 test_file (const gchar *filename) |
|
129 { |
|
130 gchar *contents; |
|
131 gsize length; |
|
132 GError *error; |
|
133 GMarkupParseContext *context; |
|
134 |
|
135 error = NULL; |
|
136 if (!g_file_get_contents (filename, |
|
137 &contents, |
|
138 &length, |
|
139 &error)) |
|
140 { |
|
141 g_assert(FALSE && "c:\\1.gmarkup not found"); |
|
142 g_print("%s\n", error->message); |
|
143 g_error_free (error); |
|
144 return 1; |
|
145 } |
|
146 |
|
147 context = g_markup_parse_context_new (&parser, 0, NULL, NULL); |
|
148 if(!context) |
|
149 { |
|
150 g_print("markup-test.c : g_markup_parse_context_new failed @ line : %d",__LINE__); |
|
151 g_assert(FALSE && "markup-test failed"); |
|
152 return 1; |
|
153 } |
|
154 |
|
155 if (!g_markup_parse_context_parse (context, contents, length, NULL)) |
|
156 { |
|
157 g_print("markup-test.c : g_markup_parse_context_parse failed "); |
|
158 g_assert(FALSE && "markup-test failed"); |
|
159 g_markup_parse_context_free (context); |
|
160 return 1; |
|
161 } |
|
162 |
|
163 if (!g_markup_parse_context_end_parse (context, NULL)) |
|
164 { |
|
165 g_print("markup-test.c : g_markup_parse_context_end_parse failed "); |
|
166 g_assert(FALSE && "markup-test failed"); |
|
167 g_markup_parse_context_free (context); |
|
168 return 1; |
|
169 } |
|
170 |
|
171 g_markup_parse_context_free (context); |
|
172 |
|
173 /* A byte at a time */ |
|
174 if (test_in_chunks (contents, length, 1) != 0) |
|
175 return 1; |
|
176 |
|
177 /* 2 bytes */ |
|
178 if (test_in_chunks (contents, length, 2) != 0) |
|
179 return 1; |
|
180 |
|
181 /*5 bytes */ |
|
182 if (test_in_chunks (contents, length, 5) != 0) |
|
183 return 1; |
|
184 |
|
185 /* 12 bytes */ |
|
186 if (test_in_chunks (contents, length, 12) != 0) |
|
187 return 1; |
|
188 |
|
189 /* 1024 bytes */ |
|
190 if (test_in_chunks (contents, length, 1024) != 0) |
|
191 return 1; |
|
192 |
|
193 return 0; |
|
194 } |
|
195 |
|
196 int |
|
197 main (int argc, |
|
198 char *argv[]) |
|
199 { |
|
200 int retval; |
|
201 |
|
202 #ifdef __SYMBIAN32__ |
|
203 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); |
|
204 g_set_print_handler(mrtPrintHandler); |
|
205 #endif /*__SYMBIAN32__*/ |
|
206 |
|
207 |
|
208 retval = test_file ("c:\\1.gmarkup"); |
|
209 |
|
210 #ifdef __SYMBIAN32__ |
|
211 testResultXml("markup-test"); |
|
212 #endif /* EMULATOR */ |
|
213 |
|
214 return retval; |
|
215 } |
|
216 |