2 * @file sipe-xml-tests.c
6 * Copyright (C) 2010 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* Tests for sipe-xml.c */
35 #include "sipe-common.h"
36 #include "sipe-backend-debug.h"
38 #include "sipe-utils.h"
40 /* stub functions for backend API */
41 void sipe_backend_debug(sipe_debug_level level
,
47 va_start(args
, format
);
48 msg
= g_strdup_vprintf(format
, args
);
51 printf("DEBUG %d: %s", level
, msg
);
56 static guint succeeded
= 0;
57 static guint failed
= 0;
58 static const gchar
*teststring
;
60 static sipe_xml
*assert_parse(const gchar
*s
, gboolean ok
)
62 sipe_xml
*xml
= sipe_xml_parse(s
, s
? strlen(s
) : 0);
64 teststring
= s
? s
: "(nil)";
66 if ((ok
&& xml
) || (!ok
&& !xml
)) {
69 printf("[%s]\nXML parse FAILED: %p\n",
76 static const sipe_xml
*assert_child(const sipe_xml
*xml
, const gchar
*s
, gboolean ok
)
78 const sipe_xml
*child
= sipe_xml_child(xml
, s
);
80 if ((ok
&& child
) || (!ok
&& !child
)) {
83 printf("[%s]\nXML child FAILED: %p '%s'\n",
84 teststring
, xml
, s
? s
: "(nil)");
90 static void assert_data(const sipe_xml
*xml
, const gchar
*s
)
92 gchar
*data
= sipe_xml_data(xml
);
94 if (sipe_strequal(s
, data
)) {
97 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
98 teststring
, data
? data
: "(nil)", s
? s
: "(nil)");
104 static void assert_attribute(const sipe_xml
*xml
,
105 const gchar
*key
, const gchar
*value
)
107 const gchar
*attr
= sipe_xml_attribute(xml
, key
);
109 if (sipe_strequal(value
, attr
)) {
112 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
113 teststring
, key
? key
: "(nil)",
114 attr
? attr
: "(nil)", value
? value
: "(nil)");
119 static void assert_int_attribute(const sipe_xml
*xml
,
120 const gchar
*key
, gint value
, gint fallback
)
122 gint attr
= sipe_xml_int_attribute(xml
, key
, fallback
);
124 if ((attr
== value
) || (attr
== fallback
)) {
127 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
128 teststring
, key
? key
: "(nil)",
129 attr
, value
, fallback
);
134 static void assert_stringify(const sipe_xml
*xml
,
135 const gchar
*expected
)
137 gchar
*string
= sipe_xml_stringify(xml
);
139 if (sipe_strequal(string
, expected
)) {
142 printf("[%s]\nXML stringify FAILED: '%s' expected: '%s'\n",
143 teststring
, string
? string
: "(nil)", expected
);
150 /* memory leak check */
151 static gsize allocated
= 0;
153 static gpointer
test_malloc(gsize n_bytes
)
155 gsize
*m
= malloc(sizeof(gsize
) + n_bytes
);
156 if (!m
) return(NULL
);
157 allocated
+= n_bytes
;
162 static void test_free(gpointer mem
)
171 static gpointer
test_realloc(gpointer mem
, gsize n_bytes
)
175 n
= test_malloc(n_bytes
);
177 memcpy(n
, mem
, n_bytes
);
184 static GMemVTable memory_leak_check
= {
193 int main(SIPE_UNUSED_PARAMETER
int argc
, SIPE_UNUSED_PARAMETER
char **argv
)
196 const sipe_xml
*child1
, *child2
;
200 * No idea why the memory leak checks work on some platforms
201 * but fail on others :-( Disable for now...
203 g_mem_set_vtable(&memory_leak_check
);
205 (void) memory_leak_check
;
209 xml
= assert_parse(NULL
, FALSE
);
211 xml
= assert_parse("", FALSE
);
213 xml
= assert_parse("<?xml version=\"1.0\" ?>", FALSE
);
217 xml
= assert_parse("<test></test>", TRUE
);
218 assert_data(xml
, NULL
);
219 assert_stringify(xml
, "<test/>");
221 xml
= assert_parse("<test/>", TRUE
);
222 assert_data(xml
, NULL
);
223 assert_stringify(xml
, teststring
);
225 xml
= assert_parse("<test>a</test>", TRUE
);
226 assert_data(xml
, "a");
227 assert_stringify(xml
, teststring
);
229 xml
= assert_parse("<test>a\nb</test>", TRUE
);
230 assert_data(xml
, "a\nb");
231 assert_stringify(xml
, teststring
);
235 xml
= assert_parse("<test>a<child>b</child></test>", TRUE
);
236 assert_data(xml
, "a");
237 child1
= assert_child(xml
, NULL
, FALSE
);
238 child1
= assert_child(xml
, "child", TRUE
);
239 assert_data(child1
, "b");
240 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
241 assert_data(child1
, NULL
);
242 assert_stringify(xml
, teststring
);
245 xml
= assert_parse("<test>a<child/></test>", TRUE
);
246 assert_data(xml
, "a");
247 child1
= assert_child(xml
, "child", TRUE
);
248 assert_data(child1
, NULL
);
249 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
250 assert_data(child1
, NULL
);
251 assert_stringify(xml
, teststring
);
254 xml
= assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE
);
255 assert_data(xml
, "a");
256 child1
= assert_child(xml
, "child", TRUE
);
257 assert_data(child1
, "b");
258 child1
= assert_child(child1
, "inner", TRUE
);
259 assert_data(child1
, "c");
260 child1
= assert_child(xml
, "child/inner", TRUE
);
261 assert_data(child1
, "c");
262 assert_stringify(xml
, teststring
);
265 xml
= assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE
);
266 assert_data(xml
, "a");
267 child1
= assert_child(xml
, "child", TRUE
);
268 assert_data(child1
, "b");
269 child2
= assert_child(child1
, "inner/innerinner", TRUE
);
270 assert_data(child2
, "d");
271 child1
= assert_child(child1
, "inner", TRUE
);
272 assert_data(child1
, "c");
273 child1
= assert_child(child1
, "innerinner", TRUE
);
274 assert_data(child1
, "d");
275 child1
= assert_child(xml
, "child/inner", TRUE
);
276 assert_data(child1
, "c");
277 child1
= assert_child(xml
, "child/inner/innerinner", TRUE
);
278 assert_data(child1
, "d");
279 assert_stringify(xml
, teststring
);
283 xml
= assert_parse("<test a=\"\">a</test>", TRUE
);
284 assert_data(xml
, "a");
285 assert_attribute(xml
, NULL
, NULL
);
286 assert_attribute(xml
, "a", "");
287 assert_attribute(xml
, "b", NULL
);
288 assert_stringify(xml
, teststring
);
291 xml
= assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE
);
292 assert_data(xml
, "a");
293 assert_attribute(xml
, "a", "1");
294 assert_int_attribute(xml
, "a", 1, 0);
295 assert_attribute(xml
, "b", "abc");
296 assert_attribute(xml
, "c", NULL
);
297 assert_int_attribute(xml
, "d", 100, 200);
298 /* the attribute order depends on glib hashing :-( */
299 assert_stringify(xml
, "<test b=\"abc\" a=\"1\">a</test>");
303 xml
= assert_parse("t", FALSE
);
305 xml
= assert_parse("<>", FALSE
);
307 xml
= assert_parse("<></>", FALSE
);
309 xml
= assert_parse("<test>", FALSE
);
311 xml
= assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE
);
315 printf("MEMORY LEAK: %" G_GSIZE_FORMAT
" still allocated\n", allocated
);
318 printf("MEMORY LEAK CHECK OK\n");
322 printf("Result: %d PASSED %d FAILED\n", succeeded
, failed
);