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.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
,
136 gchar
*string
= sipe_xml_stringify(xml
);
138 va_start(args
, expected
);
139 while (expected
-- > 0) {
140 const gchar
*alternative
= va_arg(args
, const gchar
*);
141 if (sipe_strequal(string
, alternative
)) {
145 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
146 alternative
? alternative
: "(nil)");
152 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
153 teststring
, string
? string
: "(nil)");
161 /* memory leak check */
162 static gsize allocated
= 0;
164 static gpointer
test_malloc(gsize n_bytes
)
166 gsize
*m
= malloc(sizeof(gsize
) + n_bytes
);
167 if (!m
) return(NULL
);
168 allocated
+= n_bytes
;
173 static void test_free(gpointer mem
)
182 static gpointer
test_realloc(gpointer mem
, gsize n_bytes
)
186 n
= test_malloc(n_bytes
);
188 memcpy(n
, mem
, n_bytes
);
195 static GMemVTable memory_leak_check
= {
204 int main(SIPE_UNUSED_PARAMETER
int argc
, SIPE_UNUSED_PARAMETER
char **argv
)
207 const sipe_xml
*child1
, *child2
;
211 * No idea why the memory leak checks work on some platforms
212 * but fail on others :-( Disable for now...
214 g_mem_set_vtable(&memory_leak_check
);
216 (void) memory_leak_check
;
220 xml
= assert_parse(NULL
, FALSE
);
221 assert_stringify(xml
, 1, NULL
);
223 xml
= assert_parse("", FALSE
);
225 xml
= assert_parse("<?xml version=\"1.0\" ?>", FALSE
);
229 xml
= assert_parse("<test></test>", TRUE
);
230 assert_data(xml
, NULL
);
231 assert_stringify(xml
, 1, "<test/>");
233 xml
= assert_parse("<test/>", TRUE
);
234 assert_data(xml
, NULL
);
235 assert_stringify(xml
, 1, teststring
);
237 xml
= assert_parse("<test>a</test>", TRUE
);
238 assert_data(xml
, "a");
239 assert_stringify(xml
, 1, teststring
);
241 xml
= assert_parse("<test>a\nb</test>", TRUE
);
242 assert_data(xml
, "a\nb");
243 assert_stringify(xml
, 1, teststring
);
247 xml
= assert_parse("<test>a<child>b</child></test>", TRUE
);
248 assert_data(xml
, "a");
249 child1
= assert_child(xml
, NULL
, FALSE
);
250 child1
= assert_child(xml
, "child", TRUE
);
251 assert_data(child1
, "b");
252 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
253 assert_data(child1
, NULL
);
254 assert_stringify(xml
, 1, teststring
);
257 xml
= assert_parse("<test>a<child/></test>", TRUE
);
258 assert_data(xml
, "a");
259 child1
= assert_child(xml
, "child", TRUE
);
260 assert_data(child1
, NULL
);
261 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
262 assert_data(child1
, NULL
);
263 assert_stringify(xml
, 1, teststring
);
266 xml
= assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE
);
267 assert_data(xml
, "a");
268 child1
= assert_child(xml
, "child", TRUE
);
269 assert_data(child1
, "b");
270 child1
= assert_child(child1
, "inner", TRUE
);
271 assert_data(child1
, "c");
272 child1
= assert_child(xml
, "child/inner", TRUE
);
273 assert_data(child1
, "c");
274 assert_stringify(xml
, 1, teststring
);
277 xml
= assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE
);
278 assert_data(xml
, "a");
279 child1
= assert_child(xml
, "child", TRUE
);
280 assert_data(child1
, "b");
281 child2
= assert_child(child1
, "inner/innerinner", TRUE
);
282 assert_data(child2
, "d");
283 child1
= assert_child(child1
, "inner", TRUE
);
284 assert_data(child1
, "c");
285 child1
= assert_child(child1
, "innerinner", TRUE
);
286 assert_data(child1
, "d");
287 child1
= assert_child(xml
, "child/inner", TRUE
);
288 assert_data(child1
, "c");
289 child1
= assert_child(xml
, "child/inner/innerinner", TRUE
);
290 assert_data(child1
, "d");
291 assert_stringify(xml
, 1, teststring
);
295 xml
= assert_parse("<test a=\"\">a</test>", TRUE
);
296 assert_data(xml
, "a");
297 assert_attribute(xml
, NULL
, NULL
);
298 assert_attribute(xml
, "a", "");
299 assert_attribute(xml
, "b", NULL
);
300 assert_stringify(xml
, 1, teststring
);
303 xml
= assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE
);
304 assert_data(xml
, "a");
305 assert_attribute(xml
, "a", "1");
306 assert_int_attribute(xml
, "a", 1, 0);
307 assert_attribute(xml
, "b", "abc");
308 assert_attribute(xml
, "c", NULL
);
309 assert_int_attribute(xml
, "d", 100, 200);
310 /* the attribute order depends on glib hashing :-( */
311 assert_stringify(xml
, 2, teststring
, "<test b=\"abc\" a=\"1\">a</test>");
315 xml
= assert_parse("t", FALSE
);
317 xml
= assert_parse("<>", FALSE
);
319 xml
= assert_parse("<></>", FALSE
);
321 xml
= assert_parse("<test>", FALSE
);
323 xml
= assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE
);
327 printf("MEMORY LEAK: %" G_GSIZE_FORMAT
" still allocated\n", allocated
);
330 printf("MEMORY LEAK CHECK OK\n");
334 printf("Result: %d PASSED %d FAILED\n", succeeded
, failed
);