Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | # |
| 3 | # generate a tester program for the API |
| 4 | # |
| 5 | import sys |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 6 | import os |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 7 | import string |
| 8 | try: |
| 9 | import libxml2 |
| 10 | except: |
| 11 | print "libxml2 python bindings not available, skipping testapi.c generation" |
| 12 | sys.exit(0) |
| 13 | |
| 14 | # |
| 15 | # Modules we don't want skip in API test |
| 16 | # |
| 17 | skipped_modules = [ "SAX", "SAX2", "xlink", "threads", "globals", |
Daniel Veillard | 0ea9c9f | 2004-11-05 14:30:41 +0000 | [diff] [blame^] | 18 | "xpathInternals", "parserInternals", "xmlmemory", |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 19 | "xmlversion", "debugXML", "xmlexports", "DOCBparser", |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 20 | |
| 21 | # temporary |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 22 | "xmlautomata", "xmlregexp", "c14n", |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 23 | |
| 24 | ] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 25 | |
| 26 | # |
| 27 | # Some function really need to be skipped for the tests. |
| 28 | # |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 29 | skipped_functions = [ |
| 30 | # block on I/O |
| 31 | "xmlFdRead", "xmlReadFd", "xmlCtxtReadFd", |
| 32 | "htmlFdRead", "htmlReadFd", "htmlCtxtReadFd", |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 33 | "xmlReaderNewFd", "xmlReaderForFd", |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 34 | "xmlIORead", "xmlReadIO", "xmlCtxtReadIO", |
| 35 | "htmlIORead", "htmlReadIO", "htmlCtxtReadIO", |
Daniel Veillard | 27f2010 | 2004-11-05 11:50:11 +0000 | [diff] [blame] | 36 | "xmlReaderNewIO", "xmlBufferDump", "xmlNanoFTPConnect", |
| 37 | "xmlNanoFTPConnectTo", |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 38 | # library state cleanup, generate false leak informations and other |
| 39 | # troubles, heavillyb tested otherwise. |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 40 | "xmlCleanupParser", "xmlRelaxNGCleanupTypes", "xmlSetListDoc", |
| 41 | "xmlSetTreeDoc", "xmlUnlinkNode", |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 42 | # hard to avoid leaks in the tests |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 43 | "xmlStrcat", "xmlStrncat", "xmlCatalogAddLocal", |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 44 | # unimplemented |
| 45 | "xmlTextReaderReadInnerXml", "xmlTextReaderReadOuterXml", |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 46 | "xmlTextReaderReadString", |
| 47 | # destructor |
Daniel Veillard | 27f2010 | 2004-11-05 11:50:11 +0000 | [diff] [blame] | 48 | "xmlListDelete", "xmlOutputBufferClose", "xmlNanoFTPClose", |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 49 | # deprecated |
| 50 | "xmlCatalogGetPublic", "xmlCatalogGetSystem", "xmlEncodeEntities", |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 51 | # allocators |
| 52 | "xmlMemFree", |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 53 | ] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 54 | |
| 55 | # |
| 56 | # Those functions have side effect on the global state |
| 57 | # and hence generate errors on memory allocation tests |
| 58 | # |
| 59 | skipped_memcheck = [ "xmlLoadCatalog", "xmlAddEncodingAlias", |
| 60 | "xmlSchemaInitTypes", "xmlNanoFTPProxy", "xmlNanoFTPScanProxy", |
| 61 | "xmlNanoHTTPScanProxy", "xmlResetLastError", "xmlCatalogConvert", |
| 62 | "xmlCatalogRemove", "xmlLoadCatalogs", "xmlCleanupCharEncodingHandlers", |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 63 | "xmlInitCharEncodingHandlers", "xmlCatalogCleanup", |
| 64 | "htmlParseFile" # loads the catalogs |
| 65 | ] |
| 66 | |
| 67 | # |
| 68 | # Extra code needed for some test cases |
| 69 | # |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 70 | extra_pre_call = { |
| 71 | "xmlSAXUserParseFile": |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 72 | "if (sax == (xmlSAXHandlerPtr)&xmlDefaultSAXHandler) user_data = NULL;", |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 73 | "xmlSAXUserParseMemory": |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 74 | "if (sax == (xmlSAXHandlerPtr)&xmlDefaultSAXHandler) user_data = NULL;", |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 75 | } |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 76 | extra_post_call = { |
| 77 | "xmlAddChild": |
| 78 | "if (ret_val == NULL) { xmlFreeNode(cur) ; cur = NULL ; }", |
| 79 | "xmlAddChildList": |
| 80 | "if (ret_val == NULL) { xmlFreeNodeList(cur) ; cur = NULL ; }", |
| 81 | "xmlAddSibling": |
| 82 | "if (ret_val == NULL) { xmlFreeNode(elem) ; elem = NULL ; }", |
| 83 | "xmlAddNextSibling": |
| 84 | "if (ret_val == NULL) { xmlFreeNode(elem) ; elem = NULL ; }", |
| 85 | "xmlAddPrevSibling": |
| 86 | "if (ret_val == NULL) { xmlFreeNode(elem) ; elem = NULL ; }", |
| 87 | "xmlDocSetRootElement": |
| 88 | "if (doc == NULL) { xmlFreeNode(root) ; root = NULL ; }", |
| 89 | "xmlReplaceNode": |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 90 | """if (cur != NULL) { |
| 91 | xmlUnlinkNode(cur); |
| 92 | xmlFreeNode(cur) ; cur = NULL ; } |
| 93 | if (old != NULL) { |
| 94 | xmlUnlinkNode(old); |
| 95 | xmlFreeNode(old) ; old = NULL ; } |
| 96 | ret_val = NULL;""", |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 97 | "xmlTextMerge": |
| 98 | """if ((first != NULL) && (first->type != XML_TEXT_NODE)) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 99 | xmlUnlinkNode(second); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 100 | xmlFreeNode(second) ; second = NULL ; }""", |
Daniel Veillard | 8a32fe4 | 2004-11-02 22:10:16 +0000 | [diff] [blame] | 101 | "xmlBuildQName": |
| 102 | """if ((ret_val != NULL) && (ret_val != ncname) && |
| 103 | (ret_val != prefix) && (ret_val != memory)) |
| 104 | xmlFree(ret_val); |
| 105 | ret_val = NULL;""", |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 106 | "xmlDictReference": "xmlDictFree(dict);", |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 107 | # Functions which deallocates one of their parameters |
| 108 | "xmlXPathConvertBoolean": """val = NULL;""", |
| 109 | "xmlXPathConvertNumber": """val = NULL;""", |
| 110 | "xmlXPathConvertString": """val = NULL;""", |
| 111 | "xmlSaveFileTo": """buf = NULL;""", |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 112 | "xmlSaveFormatFileTo": """buf = NULL;""", |
| 113 | "xmlIOParseDTD": "input = NULL;", |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 114 | "xmlRemoveProp": "cur = NULL;", |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 115 | } |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 116 | |
| 117 | modules = [] |
| 118 | |
| 119 | def is_skipped_module(name): |
| 120 | for mod in skipped_modules: |
| 121 | if mod == name: |
| 122 | return 1 |
| 123 | return 0 |
| 124 | |
| 125 | def is_skipped_function(name): |
| 126 | for fun in skipped_functions: |
| 127 | if fun == name: |
| 128 | return 1 |
| 129 | # Do not test destructors |
| 130 | if string.find(name, 'Free') != -1: |
| 131 | return 1 |
| 132 | return 0 |
| 133 | |
| 134 | def is_skipped_memcheck(name): |
| 135 | for fun in skipped_memcheck: |
| 136 | if fun == name: |
| 137 | return 1 |
| 138 | return 0 |
| 139 | |
| 140 | missing_types = {} |
| 141 | def add_missing_type(name, func): |
| 142 | try: |
| 143 | list = missing_types[name] |
| 144 | list.append(func) |
| 145 | except: |
| 146 | missing_types[name] = [func] |
| 147 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 148 | missing_functions = {} |
Daniel Veillard | 0ea9c9f | 2004-11-05 14:30:41 +0000 | [diff] [blame^] | 149 | missing_functions_nr = 0 |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 150 | def add_missing_functions(name, module): |
Daniel Veillard | 0ea9c9f | 2004-11-05 14:30:41 +0000 | [diff] [blame^] | 151 | global missing_functions_nr |
| 152 | |
| 153 | missing_functions_nr = missing_functions_nr + 1 |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 154 | try: |
| 155 | list = missing_functions[module] |
| 156 | list.append(name) |
| 157 | except: |
| 158 | missing_functions[module] = [name] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 159 | |
| 160 | # |
| 161 | # Provide the type generators and destructors for the parameters |
| 162 | # |
| 163 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 164 | def type_convert(str, name, info, module, function, pos): |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 165 | res = string.replace(str, " *", "_ptr") |
| 166 | res = string.replace(res, " ", "_") |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 167 | res = string.replace(res, "htmlNode", "xmlNode") |
| 168 | res = string.replace(res, "htmlDoc", "xmlDoc") |
| 169 | res = string.replace(res, "htmlParser", "xmlParser") |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 170 | if res == 'const_char_ptr': |
| 171 | if string.find(name, "file") != -1 or \ |
| 172 | string.find(name, "uri") != -1 or \ |
| 173 | string.find(name, "URI") != -1 or \ |
| 174 | string.find(info, "filename") != -1 or \ |
| 175 | string.find(info, "URI") != -1 or \ |
| 176 | string.find(info, "URL") != -1: |
| 177 | if string.find(function, "Save") != -1: |
| 178 | return('fileoutput') |
| 179 | return('filepath') |
| 180 | if res == 'void_ptr': |
| 181 | if module == 'nanoftp' and name == 'ctx': |
| 182 | return('xmlNanoFTPCtxtPtr') |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 183 | if function == 'xmlNanoFTPNewCtxt': |
| 184 | return('xmlNanoFTPCtxtPtr') |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 185 | if module == 'nanohttp' and name == 'ctx': |
| 186 | return('xmlNanoHTTPCtxtPtr') |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 187 | if function == 'xmlIOHTTPOpenW': |
| 188 | return('xmlNanoHTTPCtxtPtr') |
Daniel Veillard | 8a32fe4 | 2004-11-02 22:10:16 +0000 | [diff] [blame] | 189 | if string.find(name, "data") != -1: |
| 190 | return('userdata'); |
Daniel Veillard | d005b9e | 2004-11-03 17:07:05 +0000 | [diff] [blame] | 191 | if string.find(name, "user") != -1: |
| 192 | return('userdata'); |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 193 | if res == 'xmlDoc_ptr': |
| 194 | res = 'xmlDocPtr'; |
| 195 | if res == 'xmlNode_ptr': |
| 196 | res = 'xmlNodePtr'; |
| 197 | if res == 'xmlDict_ptr': |
| 198 | res = 'xmlDictPtr'; |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 199 | if res == 'xmlNodePtr' and pos != 0: |
| 200 | if (function == 'xmlAddChild' and pos == 2) or \ |
| 201 | (function == 'xmlAddChildList' and pos == 2) or \ |
| 202 | (function == 'xmlAddNextSibling' and pos == 2) or \ |
| 203 | (function == 'xmlAddSibling' and pos == 2) or \ |
| 204 | (function == 'xmlDocSetRootElement' and pos == 2) or \ |
| 205 | (function == 'xmlReplaceNode' and pos == 2) or \ |
| 206 | (function == 'xmlTextMerge') or \ |
| 207 | (function == 'xmlAddPrevSibling' and pos == 2): |
| 208 | return('xmlNodePtr_in'); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 209 | if res == 'const xmlBufferPtr': |
| 210 | res = 'xmlBufferPtr'; |
Daniel Veillard | 27f2010 | 2004-11-05 11:50:11 +0000 | [diff] [blame] | 211 | if res == 'xmlChar_ptr' and name == 'name' and \ |
| 212 | string.find(function, "EatName") != -1: |
| 213 | return('eaten_name') |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 214 | |
| 215 | return res |
| 216 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 217 | known_param_types = [] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 218 | |
| 219 | def is_known_param_type(name): |
| 220 | for type in known_param_types: |
| 221 | if type == name: |
| 222 | return 1 |
| 223 | return 0 |
| 224 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 225 | # |
| 226 | # Provide the type destructors for the return values |
| 227 | # |
| 228 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 229 | known_return_types = [] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 230 | |
| 231 | def is_known_return_type(name): |
| 232 | for type in known_return_types: |
| 233 | if type == name: |
| 234 | return 1 |
| 235 | return 0 |
| 236 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 237 | # |
| 238 | # Copy the beginning of the C test program result |
| 239 | # |
| 240 | |
| 241 | input = open("testapi.c", "r") |
| 242 | test = open('testapi.c.new', 'w') |
| 243 | |
| 244 | def compare_and_save(): |
| 245 | global test |
| 246 | |
| 247 | test.close() |
| 248 | input = open("testapi.c", "r").read() |
| 249 | test = open('testapi.c.new', "r").read() |
| 250 | if input != test: |
| 251 | os.system("rm testapi.c ; mv testapi.c.new testapi.c") |
| 252 | print("Updated testapi.c") |
| 253 | else: |
| 254 | print("Generated testapi.c is identical") |
| 255 | |
| 256 | line = input.readline() |
| 257 | while line != "": |
| 258 | if line == "/* CUT HERE: everything below that line is generated */\n": |
| 259 | break; |
| 260 | if line[0:15] == "#define gen_nb_": |
| 261 | type = string.split(line[15:])[0] |
| 262 | known_param_types.append(type) |
| 263 | if line[0:19] == "static void desret_": |
| 264 | type = string.split(line[19:], '(')[0] |
| 265 | known_return_types.append(type) |
| 266 | test.write(line) |
| 267 | line = input.readline() |
| 268 | input.close() |
| 269 | |
| 270 | if line == "": |
| 271 | print "Could not find the CUT marker in testapi.c skipping generation" |
| 272 | test.close() |
| 273 | sys.exit(0) |
| 274 | |
| 275 | print("Scanned testapi.c: found %d parameters types and %d return types\n" % ( |
| 276 | len(known_param_types), len(known_return_types))) |
| 277 | test.write("/* CUT HERE: everything below that line is generated */\n") |
| 278 | |
| 279 | |
| 280 | # |
| 281 | # Open the input API description |
| 282 | # |
| 283 | doc = libxml2.readFile('doc/libxml2-api.xml', None, 0) |
| 284 | if doc == None: |
| 285 | print "Failed to load doc/libxml2-api.xml" |
| 286 | sys.exit(1) |
| 287 | ctxt = doc.xpathNewContext() |
| 288 | headers = ctxt.xpathEval("/api/files/file") |
| 289 | |
| 290 | # |
| 291 | # Load the interfaces |
| 292 | # |
| 293 | for file in headers: |
| 294 | name = file.xpathEval('string(@name)') |
| 295 | if (name == None) or (name == ''): |
| 296 | continue |
| 297 | |
| 298 | # |
| 299 | # Some module may be skipped because they don't really consists |
| 300 | # of user callable APIs |
| 301 | # |
| 302 | if is_skipped_module(name): |
| 303 | continue |
| 304 | |
| 305 | # |
| 306 | # do not test deprecated APIs |
| 307 | # |
| 308 | desc = file.xpathEval('string(description)') |
| 309 | if string.find(desc, 'DEPRECATED') != -1: |
| 310 | print "Skipping deprecated interface %s" % name |
| 311 | continue; |
| 312 | |
| 313 | test.write("#include <libxml/%s.h>\n" % name) |
| 314 | modules.append(name) |
| 315 | |
| 316 | # |
| 317 | # Generate the callers signatures |
| 318 | # |
| 319 | for module in modules: |
| 320 | test.write("static int test_%s(void);\n" % module); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 321 | |
| 322 | # |
| 323 | # Generate the top caller |
| 324 | # |
| 325 | |
| 326 | test.write(""" |
| 327 | /** |
| 328 | * testlibxml2: |
| 329 | * |
| 330 | * Main entry point of the tester for the full libxml2 module, |
| 331 | * it calls all the tester entry point for each module. |
| 332 | * |
| 333 | * Returns the number of error found |
| 334 | */ |
| 335 | static int |
| 336 | testlibxml2(void) |
| 337 | { |
| 338 | int ret = 0; |
| 339 | |
| 340 | """) |
| 341 | |
| 342 | for module in modules: |
| 343 | test.write(" ret += test_%s();\n" % module) |
| 344 | |
| 345 | test.write(""" |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 346 | printf("Total: %d functions, %d tests, %d errors\\n", |
| 347 | function_tests, call_tests, ret); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 348 | return(ret); |
| 349 | } |
| 350 | |
| 351 | """) |
| 352 | |
| 353 | # |
| 354 | # How to handle a function |
| 355 | # |
| 356 | nb_tests = 0 |
| 357 | |
| 358 | def generate_test(module, node): |
| 359 | global test |
| 360 | global nb_tests |
| 361 | nb_cond = 0 |
| 362 | no_gen = 0 |
| 363 | |
| 364 | name = node.xpathEval('string(@name)') |
| 365 | if is_skipped_function(name): |
| 366 | return |
| 367 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 368 | # |
| 369 | # check we know how to handle the args and return values |
| 370 | # and store the informations for the generation |
| 371 | # |
| 372 | try: |
| 373 | args = node.xpathEval("arg") |
| 374 | except: |
| 375 | args = [] |
| 376 | t_args = [] |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 377 | n = 0 |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 378 | for arg in args: |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 379 | n = n + 1 |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 380 | rtype = arg.xpathEval("string(@type)") |
| 381 | if rtype == 'void': |
| 382 | break; |
| 383 | info = arg.xpathEval("string(@info)") |
| 384 | nam = arg.xpathEval("string(@name)") |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 385 | type = type_convert(rtype, nam, info, module, name, n) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 386 | if is_known_param_type(type) == 0: |
| 387 | add_missing_type(type, name); |
| 388 | no_gen = 1 |
| 389 | t_args.append((nam, type, rtype, info)) |
| 390 | |
| 391 | try: |
| 392 | rets = node.xpathEval("return") |
| 393 | except: |
| 394 | rets = [] |
| 395 | t_ret = None |
| 396 | for ret in rets: |
| 397 | rtype = ret.xpathEval("string(@type)") |
| 398 | info = ret.xpathEval("string(@info)") |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 399 | type = type_convert(rtype, 'return', info, module, name, 0) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 400 | if rtype == 'void': |
| 401 | break |
| 402 | if is_known_return_type(type) == 0: |
| 403 | add_missing_type(type, name); |
| 404 | no_gen = 1 |
| 405 | t_ret = (type, rtype, info) |
| 406 | break |
| 407 | |
Daniel Veillard | 0ea9c9f | 2004-11-05 14:30:41 +0000 | [diff] [blame^] | 408 | test.write(""" |
| 409 | static int |
| 410 | test_%s(void) { |
| 411 | int ret = 0; |
| 412 | |
| 413 | """ % (name)) |
| 414 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 415 | if no_gen == 1: |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 416 | add_missing_functions(name, module) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 417 | test.write(""" |
| 418 | /* missing type support */ |
| 419 | return(ret); |
| 420 | } |
| 421 | |
| 422 | """) |
| 423 | return |
| 424 | |
| 425 | try: |
| 426 | conds = node.xpathEval("cond") |
| 427 | for cond in conds: |
| 428 | test.write("#ifdef %s\n" % (cond.get_content())) |
| 429 | nb_cond = nb_cond + 1 |
| 430 | except: |
| 431 | pass |
| 432 | |
| 433 | # Declare the memory usage counter |
| 434 | no_mem = is_skipped_memcheck(name) |
| 435 | if no_mem == 0: |
| 436 | test.write(" int mem_base;\n"); |
| 437 | |
| 438 | # Declare the return value |
| 439 | if t_ret != None: |
| 440 | test.write(" %s ret_val;\n" % (t_ret[1])) |
| 441 | |
| 442 | # Declare the arguments |
| 443 | for arg in t_args: |
| 444 | (nam, type, rtype, info) = arg; |
| 445 | # add declaration |
| 446 | test.write(" %s %s; /* %s */\n" % (rtype, nam, info)) |
| 447 | test.write(" int n_%s;\n" % (nam)) |
| 448 | test.write("\n") |
| 449 | |
| 450 | # Cascade loop on of each argument list of values |
| 451 | for arg in t_args: |
| 452 | (nam, type, rtype, info) = arg; |
| 453 | # |
| 454 | test.write(" for (n_%s = 0;n_%s < gen_nb_%s;n_%s++) {\n" % ( |
| 455 | nam, nam, type, nam)) |
| 456 | |
| 457 | # log the memory usage |
| 458 | if no_mem == 0: |
| 459 | test.write(" mem_base = xmlMemBlocks();\n"); |
| 460 | |
| 461 | # prepare the call |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 462 | i = 0; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 463 | for arg in t_args: |
| 464 | (nam, type, rtype, info) = arg; |
| 465 | # |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 466 | test.write(" %s = gen_%s(n_%s, %d);\n" % (nam, type, nam, i)) |
| 467 | i = i + 1; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 468 | |
| 469 | # do the call, and clanup the result |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 470 | if extra_pre_call.has_key(name): |
| 471 | test.write(" %s\n"% (extra_pre_call[name])) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 472 | if t_ret != None: |
| 473 | test.write("\n ret_val = %s(" % (name)) |
| 474 | need = 0 |
| 475 | for arg in t_args: |
| 476 | (nam, type, rtype, info) = arg |
| 477 | if need: |
| 478 | test.write(", ") |
| 479 | else: |
| 480 | need = 1 |
| 481 | test.write("%s" % nam); |
Daniel Veillard | 8a32fe4 | 2004-11-02 22:10:16 +0000 | [diff] [blame] | 482 | test.write(");\n") |
| 483 | if extra_post_call.has_key(name): |
| 484 | test.write(" %s\n"% (extra_post_call[name])) |
| 485 | test.write(" desret_%s(ret_val);\n" % t_ret[0]) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 486 | else: |
| 487 | test.write("\n %s(" % (name)); |
| 488 | need = 0; |
| 489 | for arg in t_args: |
| 490 | (nam, type, rtype, info) = arg; |
| 491 | if need: |
| 492 | test.write(", ") |
| 493 | else: |
| 494 | need = 1 |
| 495 | test.write("%s" % nam) |
| 496 | test.write(");\n") |
Daniel Veillard | 8a32fe4 | 2004-11-02 22:10:16 +0000 | [diff] [blame] | 497 | if extra_post_call.has_key(name): |
| 498 | test.write(" %s\n"% (extra_post_call[name])) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 499 | |
Daniel Veillard | 8a32fe4 | 2004-11-02 22:10:16 +0000 | [diff] [blame] | 500 | test.write(" call_tests++;\n"); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 501 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 502 | # Free the arguments |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 503 | i = 0; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 504 | for arg in t_args: |
| 505 | (nam, type, rtype, info) = arg; |
| 506 | # |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 507 | test.write(" des_%s(n_%s, %s, %d);\n" % (type, nam, nam, i)) |
| 508 | i = i + 1; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 509 | |
| 510 | test.write(" xmlResetLastError();\n"); |
| 511 | # Check the memory usage |
| 512 | if no_mem == 0: |
| 513 | test.write(""" if (mem_base != xmlMemBlocks()) { |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 514 | printf("Leak of %%d blocks found in %s", |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 515 | xmlMemBlocks() - mem_base); |
| 516 | ret++; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 517 | """ % (name)); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 518 | for arg in t_args: |
| 519 | (nam, type, rtype, info) = arg; |
| 520 | test.write(""" printf(" %%d", n_%s);\n""" % (nam)) |
| 521 | test.write(""" printf("\\n");\n""") |
| 522 | test.write(" }\n") |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 523 | |
| 524 | for arg in t_args: |
| 525 | test.write(" }\n") |
| 526 | |
| 527 | # |
| 528 | # end of conditional |
| 529 | # |
| 530 | while nb_cond > 0: |
| 531 | test.write("#endif\n") |
| 532 | nb_cond = nb_cond -1 |
| 533 | |
| 534 | nb_tests = nb_tests + 1; |
| 535 | |
| 536 | test.write(""" |
Daniel Veillard | 3d97e66 | 2004-11-04 10:49:00 +0000 | [diff] [blame] | 537 | function_tests++; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 538 | return(ret); |
| 539 | } |
| 540 | |
| 541 | """) |
| 542 | |
| 543 | # |
| 544 | # Generate all module callers |
| 545 | # |
| 546 | for module in modules: |
| 547 | # gather all the functions exported by that module |
| 548 | try: |
| 549 | functions = ctxt.xpathEval("/api/symbols/function[@file='%s']" % (module)) |
| 550 | except: |
| 551 | print "Failed to gather functions from module %s" % (module) |
| 552 | continue; |
| 553 | |
| 554 | # iterate over all functions in the module generating the test |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 555 | i = 0 |
| 556 | nb_tests_old = nb_tests |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 557 | for function in functions: |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 558 | i = i + 1 |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 559 | generate_test(module, function); |
| 560 | |
| 561 | # header |
| 562 | test.write("""static int |
| 563 | test_%s(void) { |
| 564 | int ret = 0; |
| 565 | |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 566 | printf("Testing %s : %d of %d functions ...\\n"); |
| 567 | """ % (module, module, nb_tests - nb_tests_old, i)) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 568 | |
| 569 | # iterate over all functions in the module generating the call |
| 570 | for function in functions: |
| 571 | name = function.xpathEval('string(@name)') |
| 572 | if is_skipped_function(name): |
| 573 | continue |
| 574 | test.write(" ret += test_%s();\n" % (name)) |
| 575 | |
| 576 | # footer |
| 577 | test.write(""" |
| 578 | if (ret != 0) |
| 579 | printf("Module %s: %%d errors\\n", ret); |
| 580 | return(ret); |
| 581 | } |
| 582 | """ % (module)) |
| 583 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 584 | # |
| 585 | # Generate direct module caller |
| 586 | # |
| 587 | test.write("""static int |
| 588 | test_module(const char *module) { |
| 589 | """); |
| 590 | for module in modules: |
| 591 | test.write(""" if (!strcmp(module, "%s")) return(test_%s());\n""" % ( |
| 592 | module, module)) |
| 593 | test.write(""" return(0); |
| 594 | } |
| 595 | """); |
| 596 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 597 | print "Generated test for %d modules and %d functions" %(len(modules), nb_tests) |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 598 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 599 | compare_and_save() |
| 600 | |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 601 | missing_list = [] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 602 | for missing in missing_types.keys(): |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 603 | if missing == 'va_list' or missing == '...': |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 604 | continue; |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 605 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 606 | n = len(missing_types[missing]) |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 607 | missing_list.append((n, missing)) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 608 | |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 609 | def compare_missing(a, b): |
| 610 | return b[0] - a[0] |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 611 | |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 612 | missing_list.sort(compare_missing) |
Daniel Veillard | 0ea9c9f | 2004-11-05 14:30:41 +0000 | [diff] [blame^] | 613 | print "Missing support for %d functions and %d types see missing.lst" % (missing_functions_nr, len(missing_list)) |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 614 | lst = open("missing.lst", "w") |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 615 | lst.write("Missing support for %d types" % (len(missing_list))) |
| 616 | lst.write("\n") |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 617 | for miss in missing_list: |
| 618 | lst.write("%s: %d :" % (miss[1], miss[0])) |
| 619 | i = 0 |
| 620 | for n in missing_types[miss[1]]: |
| 621 | i = i + 1 |
| 622 | if i > 5: |
| 623 | lst.write(" ...") |
| 624 | break |
| 625 | lst.write(" %s" % (n)) |
| 626 | lst.write("\n") |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 627 | lst.write("\n") |
| 628 | lst.write("\n") |
| 629 | lst.write("Missing support per module"); |
| 630 | for module in missing_functions.keys(): |
| 631 | lst.write("module %s:\n %s\n" % (module, missing_functions[module])) |
Daniel Veillard | 1ba06bb | 2004-11-04 12:32:18 +0000 | [diff] [blame] | 632 | |
| 633 | lst.close() |
Daniel Veillard | b1b3a3e | 2004-11-03 23:25:47 +0000 | [diff] [blame] | 634 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 635 | |