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 */
32 #include "sipe-utils.h"
33 #include "sipe-backend-debug.h"
34 #include "sipe-common.h"
36 /* stub functions for backend API */
37 void sipe_backend_debug(sipe_debug_level level
,
43 va_start(args
, format
);
44 msg
= g_strdup_vprintf(format
, args
);
47 printf("DEBUG %d: %s", level
, msg
);
52 static guint succeeded
= 0;
53 static guint failed
= 0;
54 static const gchar
*teststring
;
56 static sipe_xml
*assert_parse(const gchar
*s
, gboolean ok
)
58 sipe_xml
*xml
= sipe_xml_parse(s
, s
? strlen(s
) : 0);
60 teststring
= s
? s
: "(nil)";
62 if ((ok
&& xml
) || (!ok
&& !xml
)) {
65 printf("[%s]\nXML parse FAILED: %p\n",
72 static sipe_xml
*assert_child(const sipe_xml
*xml
, const gchar
*s
, gboolean ok
)
74 sipe_xml
*child
= sipe_xml_get_child(xml
, s
);
76 if ((ok
&& child
) || (!ok
&& !child
)) {
79 printf("[%s]\nXML child FAILED: %p '%s'\n",
80 teststring
, xml
, s
? s
: "(nil)");
86 static void assert_data(const sipe_xml
*xml
, const gchar
*s
)
88 gchar
*data
= sipe_xml_get_data(xml
);
90 if (sipe_strequal(s
, data
)) {
93 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
94 teststring
, data
? data
: "(nil)", s
? s
: "(nil)");
100 static void assert_attribute(const sipe_xml
*xml
,
101 const gchar
*key
, const gchar
*value
)
103 const gchar
*attr
= sipe_xml_get_attribute(xml
, key
);
105 if (sipe_strequal(value
, attr
)) {
108 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
109 teststring
, key
? key
: "(nil)",
110 attr
? attr
: "(nil)", value
? value
: "(nil)");
115 /* memory leak check */
116 static gsize allocated
= 0;
118 static gpointer
test_malloc(gsize n_bytes
)
120 gsize
*m
= malloc(sizeof(gsize
) + n_bytes
);
121 if (!m
) return(NULL
);
122 allocated
+= n_bytes
;
127 static void test_free(gpointer mem
)
136 static gpointer
test_realloc(gpointer mem
, gsize n_bytes
)
140 n
= test_malloc(n_bytes
);
142 memcpy(n
, mem
, n_bytes
);
149 static GMemVTable memory_leak_check
= {
158 int main(SIPE_UNUSED_PARAMETER
int argc
, SIPE_UNUSED_PARAMETER
char **argv
)
160 sipe_xml
*xml
, *child1
, *child2
;
164 * No idea why the memory leak checks work on some platforms
165 * but fail on others :-( Disable for now...
167 g_mem_set_vtable(&memory_leak_check
);
169 (void) memory_leak_check
;
173 xml
= assert_parse(NULL
, FALSE
);
175 xml
= assert_parse("", FALSE
);
177 xml
= assert_parse("<?xml version=\"1.0\" ?>", FALSE
);
181 xml
= assert_parse("<test></test>", TRUE
);
182 assert_data(xml
, NULL
);
184 xml
= assert_parse("<test>a</test>", TRUE
);
185 assert_data(xml
, "a");
187 xml
= assert_parse("<test>a\nb</test>", TRUE
);
188 assert_data(xml
, "a\nb");
192 xml
= assert_parse("<test>a<child>b</child></test>", TRUE
);
193 assert_data(xml
, "a");
194 child1
= assert_child(xml
, NULL
, FALSE
);
195 child1
= assert_child(xml
, "child", TRUE
);
196 assert_data(child1
, "b");
197 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
198 assert_data(child1
, NULL
);
201 xml
= assert_parse("<test>a<child/></test>", TRUE
);
202 assert_data(xml
, "a");
203 child1
= assert_child(xml
, "child", TRUE
);
204 assert_data(child1
, NULL
);
205 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
206 assert_data(child1
, NULL
);
209 xml
= assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE
);
210 assert_data(xml
, "a");
211 child1
= assert_child(xml
, "child", TRUE
);
212 assert_data(child1
, "b");
213 child1
= assert_child(child1
, "inner", TRUE
);
214 assert_data(child1
, "c");
215 child1
= assert_child(xml
, "child/inner", TRUE
);
216 assert_data(child1
, "c");
219 xml
= assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE
);
220 assert_data(xml
, "a");
221 child1
= assert_child(xml
, "child", TRUE
);
222 assert_data(child1
, "b");
223 child2
= assert_child(child1
, "inner/innerinner", TRUE
);
224 assert_data(child2
, "d");
225 child1
= assert_child(child1
, "inner", TRUE
);
226 assert_data(child1
, "c");
227 child1
= assert_child(child1
, "innerinner", TRUE
);
228 assert_data(child1
, "d");
229 child1
= assert_child(xml
, "child/inner", TRUE
);
230 assert_data(child1
, "c");
231 child1
= assert_child(xml
, "child/inner/innerinner", TRUE
);
232 assert_data(child1
, "d");
236 xml
= assert_parse("<test a=\"\">a</test>", TRUE
);
237 assert_data(xml
, "a");
238 assert_attribute(xml
, NULL
, NULL
);
239 assert_attribute(xml
, "a", "");
240 assert_attribute(xml
, "b", NULL
);
243 xml
= assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE
);
244 assert_data(xml
, "a");
245 assert_attribute(xml
, "a", "1");
246 assert_attribute(xml
, "b", "abc");
247 assert_attribute(xml
, "c", NULL
);
251 xml
= assert_parse("t", FALSE
);
253 xml
= assert_parse("<>", FALSE
);
255 xml
= assert_parse("<></>", FALSE
);
257 xml
= assert_parse("<test>", FALSE
);
261 printf("MEMORY LEAK: %" G_GSIZE_FORMAT
" still allocated\n", allocated
);
264 printf("MEMORY LEAK CHECK OK\n");
268 printf("Result: %d PASSED %d FAILED\n", succeeded
, failed
);