From be4fc7990b91e1fb053df4a1388f13aaad9772f8 Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 17 Dec 2009 12:44:56 +0000 Subject: Ported to the new test framework. --- server/src/saxparser.cc | 224 +++++++++++++++++++++++------------------------- 1 file changed, 107 insertions(+), 117 deletions(-) (limited to 'server/src/saxparser.cc') diff --git a/server/src/saxparser.cc b/server/src/saxparser.cc index 9072ab6..2018645 100644 --- a/server/src/saxparser.cc +++ b/server/src/saxparser.cc @@ -167,6 +167,10 @@ unsigned int SAXParser::usedBytes() #ifdef TEST_SAXPARSER +//deps: log.cc debug.cc exception.cc +//cflags: -I.. +//libs: -lexpat +#include #define XMLFILE "/tmp/saxparsertest.xml" @@ -253,139 +257,125 @@ public: } }; -int main(int argc, char *argv[]) -{ - FILE *fp = fopen(XMLFILE, "w"); - if(!fp) { - printf("Could not write to %s\n", XMLFILE); - return 1; - } - fprintf(fp, xml); - fclose(fp); +TEST_BEGIN; - // Test callback parser - { - MyFileParser parser(XMLFILE); - parser.parse(); - } +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, xml); +fclose(fp); - // 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) { - printf("Buffer parser failed on size %d: %s [%s]\n", sz, e.what(), substr.c_str()); - } - pos += sz; - } +TEST_MSG("Test callback parser."); +{ + MyFileParser parser(XMLFILE); + parser.parse(); +} - if(!test) { - printf("Buffer parser failed on size %d\n", sz); - return 1; +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"); - if(!fp) { - printf("Could not write to %s\n", XMLFILE); - return 1; - } - fprintf(fp, xml_notrailingwhitespace); - fprintf(fp, xml_notrailingwhitespace); - fclose(fp); - // Test buffer parser with multiple documents in the same buffer - { - fp = fopen(XMLFILE, "r"); - if(!fp) { - printf("Could not write to %s\n", XMLFILE); - return 1; - } - 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); - } +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, xml_notrailingwhitespace); +fprintf(fp, 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); } } - if(numdocs != 2) { - printf("Failed to parse two documents.\n"); - return 1; - } - if(parser) delete parser; parser = NULL; - delete[] buf; } - fclose(fp); - } - - fp = fopen(XMLFILE, "w"); - if(!fp) { - printf("Could not write to %s\n", XMLFILE); - return 1; + TEST_EQUAL(numdocs, 2, "Test if 2 documents were parsed on docsize %d.", sz); + if(parser) delete parser; parser = NULL; + delete[] buf; } - fprintf(fp, xml_fail); fclose(fp); +} - // Test failure - { - MyFileParser parser(XMLFILE); - try { - parser.parse(); - } catch(Exception &e) { - goto goon; - } - printf("This test should fail...\n"); - return 1; - } - 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, xml_fail); +fclose(fp); - fp = fopen(XMLFILE, "w"); - if(!fp) { - printf("Could not write to %s\n", XMLFILE); - return 1; +TEST_MSG("Test failure"); +{ + MyFileParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + goto goon; } - fprintf(fp, xml_fail2); - fclose(fp); + TEST_TRUE(false, "This test should fail...\n"); +} +goon: - // Test failure - { - MyFileParser parser(XMLFILE); - try { - parser.parse(); - } catch(Exception &e) { - goto goonagain; - } - printf("This test should fail...\n"); - return 1; - } - goonagain: +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, xml_fail2); +fclose(fp); - unlink(XMLFILE); +// 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