From b320cc10871217d51e3458bf85a22e7d50ee4aec Mon Sep 17 00:00:00 2001 From: Jonas Suhr Christensen Date: Fri, 30 Mar 2012 16:04:03 +0200 Subject: Added a bit of debug and implemented messagehandler. --- src/saxparser.cc | 204 +------------------------------------------------------ 1 file changed, 1 insertion(+), 203 deletions(-) (limited to 'src/saxparser.cc') diff --git a/src/saxparser.cc b/src/saxparser.cc index 14f204c..065a9d8 100644 --- a/src/saxparser.cc +++ b/src/saxparser.cc @@ -198,215 +198,13 @@ void SAXParser::characterData(std::string &) } #ifdef TEST_SAXPARSER -//deps: log.cc debug.cc exception.cc +//deps: //cflags: -I.. //libs: -lexpat #include -#define XMLFILE "/tmp/saxparsertest.xml" - -#include "exception.h" - -#include -#include -#include -#include -#include -#include - -static char xml[] = -"\n" -"\n" -" \n" -" \n" -" \n" -" \n" -" \n" -"\n \t\n\r" - ; - -static char xml_notrailingwhitespace[] = -"\n" -"\n" -" \n" -" \n" -" \n" -" \n" -" \n" -"" - ; - -static char xml_fail[] = -"\n" -"\n" -" \n" -"\n" - ; - -static char xml_fail2[] = -"\n" -"\n" -" \n" -"\n" -"this is junk\n" - ; - -class MyFileParser :public SAXParser { -public: - MyFileParser(const char *file) { - fd = open(file, O_RDONLY); - } - - int readData(char *data, size_t size) { - return read(fd, data, size); - } - - void startTag(std::string name, attributes_t &attributes) - { - //printf("<%s>\n", name.c_str()); - } - - void parseError(const char *buf, size_t len, std::string error, int lineno) - { - throw Exception(error); - } - -private: - int fd; -}; - -class MyBufferParser :public SAXParser { -public: - void startTag(std::string name, attributes_t &attributes) - { - //printf("<%s>\n", name.c_str()); - } - - void parseError(char *buf, size_t len, std::string error, int lineno) - { - throw Exception(error); - } -}; - TEST_BEGIN; -FILE *fp = fopen(XMLFILE, "w"); -TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written."); -if(!fp) TEST_FATAL("Could not write "XMLFILE); -fprintf(fp, "%s", xml); -fclose(fp); - -TEST_MSG("Test callback parser."); -{ - MyFileParser parser(XMLFILE); - parser.parse(); -} - -TEST_MSG("Test buffer parser."); -for(size_t sz = 1; sz < 1000; sz++) { - bool test = false; - MyBufferParser parser; - std::string buf = xml; - size_t pos = 0; - while(pos < buf.length()) { - std::string substr = buf.substr(pos, sz); - - try { - test |= parser.parse((char*)substr.c_str(), substr.length()); - } catch(Exception &e) { - TEST_TRUE(true, "Buffer parser failed on size %d: %s [%s]", - sz, e.what(), substr.c_str()); - } - pos += sz; - } - - TEST_TRUE(test, "Test buffer parser on %d bytes", sz); - } - -fp = fopen(XMLFILE, "w"); -TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written."); -if(!fp) TEST_FATAL("Could not write "XMLFILE); -fprintf(fp, "%s", xml_notrailingwhitespace); -fprintf(fp, "%s", xml_notrailingwhitespace); -fclose(fp); - -TEST_MSG("Test buffer parser with multiple documents in the same buffer."); -{ - fp = fopen(XMLFILE, "r"); - TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be read."); - if(!fp) TEST_FATAL("Could not read from "XMLFILE); - - for(size_t sz = 1; sz < 1000; sz++) { - MyBufferParser *parser = NULL; - rewind(fp); - size_t numdocs = 0; - char *buf = new char[sz + 1]; - memset(buf, 0, sz + 1); - size_t size; - while( (size = fread(buf, 1, sz, fp)) > 0) { - while(size) { - if(parser == NULL) { - parser = new MyBufferParser(); - } - if(parser->parse(buf, size)) { - - // Got one - numdocs++; - - size = size - parser->usedBytes(); - strcpy(buf, buf + parser->usedBytes()); - delete parser; parser = NULL; - } else { - size = 0; - memset(buf, 0, sz + 1); - } - } - } - TEST_EQUAL(numdocs, 2, "Test if 2 documents were parsed on docsize %d.", sz); - if(parser) delete parser; parser = NULL; - delete[] buf; - } - fclose(fp); -} - -fp = fopen(XMLFILE, "w"); -TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written."); -if(!fp) TEST_FATAL("Could not write "XMLFILE); -fprintf(fp, "%s", xml_fail); -fclose(fp); - -TEST_MSG("Test failure"); -{ - MyFileParser parser(XMLFILE); - try { - parser.parse(); - } catch(Exception &e) { - goto goon; - } - TEST_TRUE(false, "This test should fail...\n"); -} -goon: - -fp = fopen(XMLFILE, "w"); -TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written."); -if(!fp) TEST_FATAL("Could not write "XMLFILE); -fprintf(fp, "%s", xml_fail2); -fclose(fp); - -// Test failure -{ - MyFileParser parser(XMLFILE); - try { - parser.parse(); - } catch(Exception &e) { - goto goonagain; - } - TEST_TRUE(false, "This test should fail...\n"); -} -goonagain: - -unlink(XMLFILE); - TEST_END; #endif/*TEST_SAXPARSER*/ -- cgit v1.2.3