From 625fcfe9edfbe8576ec6ef8d2d6adaed4bb86403 Mon Sep 17 00:00:00 2001 From: Kirill Simonov Date: Fri, 21 Jul 2006 13:50:32 +0000 Subject: Refactor internal and external API. --- tests/Makefile.am | 2 +- tests/run-parser.c | 46 +++++++++++++++++++++ tests/run-scanner.c | 46 +++++++++++++++++++++ tests/test-reader.c | 114 +++++++++++++++++++++++++-------------------------- tests/test-version.c | 5 +++ 5 files changed, 155 insertions(+), 58 deletions(-) create mode 100644 tests/run-parser.c create mode 100644 tests/run-scanner.c (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index 8f69907..e2f9e9c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,4 @@ AM_CPPFLAGS = -I$(top_srcdir)/include LDADD = $(top_builddir)/src/libyaml.la TESTS = test-version test-reader -check_PROGRAMS = test-version test-reader +check_PROGRAMS = test-version test-reader run-scanner run-parser diff --git a/tests/run-parser.c b/tests/run-parser.c new file mode 100644 index 0000000..16f4ce1 --- /dev/null +++ b/tests/run-parser.c @@ -0,0 +1,46 @@ +#include + +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + FILE *file; + yaml_parser_t parser; + yaml_event_t event; + int done = 0; + int count = 0; + + if (argc != 2) { + printf("Usage: %s file.yaml\n", argv[0]); + return 0; + } + file = fopen(argv[1], "rb"); + assert(file); + + assert(yaml_parser_initialize(&parser)); + + yaml_parser_set_input_file(&parser, file); + + while (!done) + { + assert(yaml_parser_parse(&parser, &event)); + + done = (event.type == YAML_STREAM_END_EVENT); + + yaml_event_delete(&event); + + count ++; + } + + yaml_parser_delete(&parser); + + fclose(file); + + printf("Parsing the file '%s': %d events\n", argv[1], count); + + return 0; +} + diff --git a/tests/run-scanner.c b/tests/run-scanner.c new file mode 100644 index 0000000..e3a67f2 --- /dev/null +++ b/tests/run-scanner.c @@ -0,0 +1,46 @@ +#include + +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + FILE *file; + yaml_parser_t parser; + yaml_token_t token; + int done = 0; + int count = 0; + + if (argc != 2) { + printf("Usage: %s file.yaml\n", argv[0]); + return 0; + } + file = fopen(argv[1], "rb"); + assert(file); + + assert(yaml_parser_initialize(&parser)); + + yaml_parser_set_input_file(&parser, file); + + while (!done) + { + assert(yaml_parser_scan(&parser, &token)); + + done = (token.type == YAML_STREAM_END_TOKEN); + + yaml_token_delete(&token); + + count ++; + } + + yaml_parser_delete(&parser); + + fclose(file); + + printf("Parsing the file '%s': %d tokens\n", argv[1], count); + + return 0; +} + diff --git a/tests/test-reader.c b/tests/test-reader.c index c5ce279..49dc874 100644 --- a/tests/test-reader.c +++ b/tests/test-reader.c @@ -1,5 +1,8 @@ #include +YAML_DECLARE(int) +yaml_parser_update_buffer(yaml_parser_t *parser, size_t length); + #include #include #include @@ -101,14 +104,15 @@ test_case boms[] = { {"no bom (utf-8)", "Hi is \xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!", 13}, {"bom (utf-8)", "\xef\xbb\xbfHi is \xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!", 13}, {"bom (utf-16-le)", "\xff\xfeH\x00i\x00 \x00i\x00s\x00 \x00\x1f\x04@\x04""8\x04""2\x04""5\x04""B\x04!", 13}, - {"bom (utf-16-be)", "\xfe\xff\x00H\x00i\x00 \x00i\x00s\x00 \x04\x1f\x04@\x04""8\x04""2\x04""5\x04""B!", 13} + {"bom (utf-16-be)", "\xfe\xff\x00H\x00i\x00 \x00i\x00s\x00 \x04\x1f\x04@\x04""8\x04""2\x04""5\x04""B!", 13}, + {NULL, NULL, 0} }; char *bom_original = "Hi is \xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82"; int check_utf8_sequences(void) { - yaml_parser_t *parser; + yaml_parser_t parser; int failed = 0; int k; printf("checking utf-8 sequences...\n"); @@ -121,10 +125,9 @@ int check_utf8_sequences(void) printf("\t%s:\n", title); while(1) { while (*end != '|' && *end != '!') end++; - parser = yaml_parser_new(); - assert(parser); - yaml_parser_set_input_string(parser, (unsigned char *)start, end-start); - result = yaml_parser_update_buffer(parser, end-start); + yaml_parser_initialize(&parser); + yaml_parser_set_input_string(&parser, (unsigned char *)start, end-start); + result = yaml_parser_update_buffer(&parser, end-start); if (result != check) { printf("\t\t- "); failed ++; @@ -132,22 +135,22 @@ int check_utf8_sequences(void) else { printf("\t\t+ "); } - if (!parser->error) { + if (!parser.error) { printf("(no error)\n"); } - else if (parser->error == YAML_READER_ERROR) { - if (parser->problem_value != -1) { + else if (parser.error == YAML_READER_ERROR) { + if (parser.problem_value != -1) { printf("(reader error: %s: #%X at %d)\n", - parser->problem, parser->problem_value, parser->problem_offset); + parser.problem, parser.problem_value, parser.problem_offset); } else { printf("(reader error: %s at %d)\n", - parser->problem, parser->problem_offset); + parser.problem, parser.problem_offset); } } if (*end == '!') break; start = ++end; - yaml_parser_delete(parser); + yaml_parser_delete(&parser); }; printf("\n"); } @@ -157,7 +160,7 @@ int check_utf8_sequences(void) int check_boms(void) { - yaml_parser_t *parser; + yaml_parser_t parser; int failed = 0; int k; printf("checking boms...\n"); @@ -169,28 +172,27 @@ int check_boms(void) char *end = start; while (*end != '!') end++; printf("\t%s: ", title); - parser = yaml_parser_new(); - assert(parser); - yaml_parser_set_input_string(parser, (unsigned char *)start, end-start); - result = yaml_parser_update_buffer(parser, end-start); + yaml_parser_initialize(&parser); + yaml_parser_set_input_string(&parser, (unsigned char *)start, end-start); + result = yaml_parser_update_buffer(&parser, end-start); if (!result) { - printf("- (reader error: %s at %d)\n", parser->problem, parser->problem_offset); + printf("- (reader error: %s at %d)\n", parser.problem, parser.problem_offset); failed++; } else { - if (parser->unread != check) { - printf("- (length=%d while expected length=%d)\n", parser->unread, check); + if (parser.unread != check) { + printf("- (length=%d while expected length=%d)\n", parser.unread, check); failed++; } - else if (memcmp(parser->buffer, bom_original, check) != 0) { - printf("- (value '%s' does not equal to the original value '%s')\n", parser->buffer, bom_original); + else if (memcmp(parser.buffer.start, bom_original, check) != 0) { + printf("- (value '%s' does not equal to the original value '%s')\n", parser.buffer.start, bom_original); failed++; } else { printf("+\n"); } } - yaml_parser_delete(parser); + yaml_parser_delete(&parser); } printf("checking boms: %d fail(s)\n", failed); return failed; @@ -200,7 +202,7 @@ int check_boms(void) int check_long_utf8(void) { - yaml_parser_t *parser; + yaml_parser_t parser; int k = 0; int j; int failed = 0; @@ -221,18 +223,17 @@ int check_long_utf8(void) buffer[k++] = '\xaf'; } } - parser = yaml_parser_new(); - assert(parser); - yaml_parser_set_input_string(parser, buffer, 3+LONG*2); + yaml_parser_initialize(&parser); + yaml_parser_set_input_string(&parser, buffer, 3+LONG*2); for (k = 0; k < LONG; k++) { - if (!parser->unread) { - if (!yaml_parser_update_buffer(parser, 1)) { - printf("\treader error: %s at %d\n", parser->problem, parser->problem_offset); + if (!parser.unread) { + if (!yaml_parser_update_buffer(&parser, 1)) { + printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset); failed = 1; break; } } - if (!parser->unread) { + if (!parser.unread) { printf("\tnot enough characters at %d\n", k); failed = 1; break; @@ -245,27 +246,27 @@ int check_long_utf8(void) ch0 = '\xd0'; ch1 = '\xaf'; } - if (parser->pointer[0] != ch0 || parser->pointer[1] != ch1) { + if (parser.buffer.pointer[0] != ch0 || parser.buffer.pointer[1] != ch1) { printf("\tincorrect UTF-8 sequence: %X %X instead of %X %X\n", - (int)parser->pointer[0], (int)parser->pointer[1], + (int)parser.buffer.pointer[0], (int)parser.buffer.pointer[1], (int)ch0, (int)ch1); failed = 1; break; } - parser->pointer += 2; - parser->unread -= 1; + parser.buffer.pointer += 2; + parser.unread -= 1; } if (!failed) { - if (!yaml_parser_update_buffer(parser, 1)) { - printf("\treader error: %s at %d\n", parser->problem, parser->problem_offset); + if (!yaml_parser_update_buffer(&parser, 1)) { + printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset); failed = 1; } - else if (parser->pointer[0] != '\0') { - printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser->pointer[0], parser->eof, parser->unread); + else if (parser.buffer.pointer[0] != '\0') { + printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser.buffer.pointer[0], parser.eof, parser.unread); failed = 1; } } - yaml_parser_delete(parser); + yaml_parser_delete(&parser); free(buffer); printf("checking a long utf8 sequence: %d fail(s)\n", failed); return failed; @@ -273,7 +274,7 @@ int check_long_utf8(void) int check_long_utf16(void) { - yaml_parser_t *parser; + yaml_parser_t parser; int k = 0; int j; int failed = 0; @@ -293,18 +294,17 @@ int check_long_utf16(void) buffer[k++] = '\x04'; } } - parser = yaml_parser_new(); - assert(parser); - yaml_parser_set_input_string(parser, buffer, 2+LONG*2); + yaml_parser_initialize(&parser); + yaml_parser_set_input_string(&parser, buffer, 2+LONG*2); for (k = 0; k < LONG; k++) { - if (!parser->unread) { - if (!yaml_parser_update_buffer(parser, 1)) { - printf("\treader error: %s at %d\n", parser->problem, parser->problem_offset); + if (!parser.unread) { + if (!yaml_parser_update_buffer(&parser, 1)) { + printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset); failed = 1; break; } } - if (!parser->unread) { + if (!parser.unread) { printf("\tnot enough characters at %d\n", k); failed = 1; break; @@ -317,27 +317,27 @@ int check_long_utf16(void) ch0 = '\xd0'; ch1 = '\xaf'; } - if (parser->pointer[0] != ch0 || parser->pointer[1] != ch1) { + if (parser.buffer.pointer[0] != ch0 || parser.buffer.pointer[1] != ch1) { printf("\tincorrect UTF-8 sequence: %X %X instead of %X %X\n", - (int)parser->pointer[0], (int)parser->pointer[1], + (int)parser.buffer.pointer[0], (int)parser.buffer.pointer[1], (int)ch0, (int)ch1); failed = 1; break; } - parser->pointer += 2; - parser->unread -= 1; + parser.buffer.pointer += 2; + parser.unread -= 1; } if (!failed) { - if (!yaml_parser_update_buffer(parser, 1)) { - printf("\treader error: %s at %d\n", parser->problem, parser->problem_offset); + if (!yaml_parser_update_buffer(&parser, 1)) { + printf("\treader error: %s at %d\n", parser.problem, parser.problem_offset); failed = 1; } - else if (parser->pointer[0] != '\0') { - printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser->pointer[0], parser->eof, parser->unread); + else if (parser.buffer.pointer[0] != '\0') { + printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser.buffer.pointer[0], parser.eof, parser.unread); failed = 1; } } - yaml_parser_delete(parser); + yaml_parser_delete(&parser); free(buffer); printf("checking a long utf16 sequence: %d fail(s)\n", failed); return failed; diff --git a/tests/test-version.c b/tests/test-version.c index ab4f93c..5982f7d 100644 --- a/tests/test-version.c +++ b/tests/test-version.c @@ -16,5 +16,10 @@ main(void) sprintf(buf, "%d.%d.%d", major, minor, patch); assert(strcmp(buf, yaml_get_version_string()) == 0); + /* Print structure sizes. */ + printf("sizeof(token) = %d\n", sizeof(yaml_token_t)); + printf("sizeof(event) = %d\n", sizeof(yaml_event_t)); + printf("sizeof(parser) = %d\n", sizeof(yaml_parser_t)); + return 0; } -- cgit v1.2.1