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 */
33 #include "sipe-common.h"
34 #include "sipe-backend-debug.h"
36 #include "sipe-utils.h"
38 /* stub functions for backend API */
39 void sipe_backend_debug(sipe_debug_level level
,
45 va_start(args
, format
);
46 msg
= g_strdup_vprintf(format
, args
);
49 printf("DEBUG %d: %s", level
, msg
);
54 static guint succeeded
= 0;
55 static guint failed
= 0;
56 static const gchar
*teststring
;
58 static sipe_xml
*assert_parse(const gchar
*s
, gboolean ok
)
60 sipe_xml
*xml
= sipe_xml_parse(s
, s
? strlen(s
) : 0);
62 teststring
= s
? s
: "(nil)";
64 if ((ok
&& xml
) || (!ok
&& !xml
)) {
67 printf("[%s]\nXML parse FAILED: %p\n",
74 static const sipe_xml
*assert_child(const sipe_xml
*xml
, const gchar
*s
, gboolean ok
)
76 const sipe_xml
*child
= sipe_xml_child(xml
, s
);
78 if ((ok
&& child
) || (!ok
&& !child
)) {
81 printf("[%s]\nXML child FAILED: %p '%s'\n",
82 teststring
, xml
, s
? s
: "(nil)");
88 static void assert_data(const sipe_xml
*xml
, const gchar
*s
)
90 gchar
*data
= sipe_xml_data(xml
);
92 if (sipe_strequal(s
, data
)) {
95 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
96 teststring
, data
? data
: "(nil)", s
? s
: "(nil)");
102 static void assert_attribute(const sipe_xml
*xml
,
103 const gchar
*key
, const gchar
*value
)
105 const gchar
*attr
= sipe_xml_attribute(xml
, key
);
107 if (sipe_strequal(value
, attr
)) {
110 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
111 teststring
, key
? key
: "(nil)",
112 attr
? attr
: "(nil)", value
? value
: "(nil)");
117 static void assert_int_attribute(const sipe_xml
*xml
,
118 const gchar
*key
, gint value
, gint fallback
)
120 gint attr
= sipe_xml_int_attribute(xml
, key
, fallback
);
122 if ((attr
== value
) || (attr
== fallback
)) {
125 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
126 teststring
, key
? key
: "(nil)",
127 attr
, value
, fallback
);
132 static void assert_stringify(const sipe_xml
*xml
,
133 const gchar
*expected
)
135 gchar
*string
= sipe_xml_stringify(xml
);
137 if (sipe_strequal(string
, expected
)) {
140 printf("[%s]\nXML stringify FAILED: '%s' expected: '%s'\n",
141 teststring
, string
? string
: "(nil)", expected
);
148 /* memory leak check */
149 static gsize allocated
= 0;
151 static gpointer
test_malloc(gsize n_bytes
)
153 gsize
*m
= malloc(sizeof(gsize
) + n_bytes
);
154 if (!m
) return(NULL
);
155 allocated
+= n_bytes
;
160 static void test_free(gpointer mem
)
169 static gpointer
test_realloc(gpointer mem
, gsize n_bytes
)
173 n
= test_malloc(n_bytes
);
175 memcpy(n
, mem
, n_bytes
);
182 static GMemVTable memory_leak_check
= {
191 int main(SIPE_UNUSED_PARAMETER
int argc
, SIPE_UNUSED_PARAMETER
char **argv
)
194 const sipe_xml
*child1
, *child2
;
198 * No idea why the memory leak checks work on some platforms
199 * but fail on others :-( Disable for now...
201 g_mem_set_vtable(&memory_leak_check
);
203 (void) memory_leak_check
;
207 xml
= assert_parse(NULL
, FALSE
);
208 assert_stringify(xml
, NULL
);
210 xml
= assert_parse("", FALSE
);
212 xml
= assert_parse("<?xml version=\"1.0\" ?>", FALSE
);
216 xml
= assert_parse("<test></test>", TRUE
);
217 assert_data(xml
, NULL
);
218 assert_stringify(xml
, "<test/>");
220 xml
= assert_parse("<test/>", TRUE
);
221 assert_data(xml
, NULL
);
222 assert_stringify(xml
, teststring
);
224 xml
= assert_parse("<test>a</test>", TRUE
);
225 assert_data(xml
, "a");
226 assert_stringify(xml
, teststring
);
228 xml
= assert_parse("<test>a\nb</test>", TRUE
);
229 assert_data(xml
, "a\nb");
230 assert_stringify(xml
, teststring
);
234 xml
= assert_parse("<test>a<child>b</child></test>", TRUE
);
235 assert_data(xml
, "a");
236 child1
= assert_child(xml
, NULL
, FALSE
);
237 child1
= assert_child(xml
, "child", TRUE
);
238 assert_data(child1
, "b");
239 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
240 assert_data(child1
, NULL
);
241 assert_stringify(xml
, teststring
);
244 xml
= assert_parse("<test>a<child/></test>", TRUE
);
245 assert_data(xml
, "a");
246 child1
= assert_child(xml
, "child", TRUE
);
247 assert_data(child1
, NULL
);
248 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
249 assert_data(child1
, NULL
);
250 assert_stringify(xml
, teststring
);
253 xml
= assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE
);
254 assert_data(xml
, "a");
255 child1
= assert_child(xml
, "child", TRUE
);
256 assert_data(child1
, "b");
257 child1
= assert_child(child1
, "inner", TRUE
);
258 assert_data(child1
, "c");
259 child1
= assert_child(xml
, "child/inner", TRUE
);
260 assert_data(child1
, "c");
261 assert_stringify(xml
, teststring
);
264 xml
= assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE
);
265 assert_data(xml
, "a");
266 child1
= assert_child(xml
, "child", TRUE
);
267 assert_data(child1
, "b");
268 child2
= assert_child(child1
, "inner/innerinner", TRUE
);
269 assert_data(child2
, "d");
270 child1
= assert_child(child1
, "inner", TRUE
);
271 assert_data(child1
, "c");
272 child1
= assert_child(child1
, "innerinner", TRUE
);
273 assert_data(child1
, "d");
274 child1
= assert_child(xml
, "child/inner", TRUE
);
275 assert_data(child1
, "c");
276 child1
= assert_child(xml
, "child/inner/innerinner", TRUE
);
277 assert_data(child1
, "d");
278 assert_stringify(xml
, teststring
);
282 xml
= assert_parse("<test a=\"\">a</test>", TRUE
);
283 assert_data(xml
, "a");
284 assert_attribute(xml
, NULL
, NULL
);
285 assert_attribute(xml
, "a", "");
286 assert_attribute(xml
, "b", NULL
);
287 assert_stringify(xml
, teststring
);
290 xml
= assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE
);
291 assert_data(xml
, "a");
292 assert_attribute(xml
, "a", "1");
293 assert_int_attribute(xml
, "a", 1, 0);
294 assert_attribute(xml
, "b", "abc");
295 assert_attribute(xml
, "c", NULL
);
296 assert_int_attribute(xml
, "d", 100, 200);
297 /* the attribute order depends on glib hashing :-( */
298 assert_stringify(xml
, "<test b=\"abc\" a=\"1\">a</test>");
302 xml
= assert_parse("t", FALSE
);
304 xml
= assert_parse("<>", FALSE
);
306 xml
= assert_parse("<></>", FALSE
);
308 xml
= assert_parse("<test>", FALSE
);
310 xml
= assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE
);
314 printf("MEMORY LEAK: %" G_GSIZE_FORMAT
" still allocated\n", allocated
);
317 printf("MEMORY LEAK CHECK OK\n");
321 printf("Result: %d PASSED %d FAILED\n", succeeded
, failed
);