Source

libyaml / tests / run-emitter.c

Diff from to

File tests/run-emitter.c

 #define BUFFER_SIZE 65536
 #define MAX_EVENTS  1024
 
-int copy_event(yaml_event_t *event_to, yaml_event_t *event_from)
+int compare_events(const yaml_event_t *event, const yaml_event_t *model)
 {
-    switch (event_from->type)
+    yaml_char_t *event_anchor = NULL;
+    yaml_char_t *model_anchor = NULL;
+    yaml_char_t *event_tag = NULL;
+    yaml_char_t *model_tag = NULL;
+    yaml_char_t *event_value = NULL;
+    yaml_char_t *model_value = NULL;
+    size_t event_length = 0;
+    size_t model_length = 0;
+    int idx;
+
+    if (event->type != model->type)
+        return 0;
+
+    switch (event->type)
     {
-        case YAML_STREAM_START_EVENT:
-            return yaml_stream_start_event_initialize(event_to,
-                    event_from->data.stream_start.encoding);
-
-        case YAML_STREAM_END_EVENT:
-            return yaml_stream_end_event_initialize(event_to);
-
         case YAML_DOCUMENT_START_EVENT:
-            return yaml_document_start_event_initialize(event_to,
-                    event_from->data.document_start.version_directive,
-                    event_from->data.document_start.tag_directives.start,
-                    event_from->data.document_start.tag_directives.end,
-                    event_from->data.document_start.implicit);
-
-        case YAML_DOCUMENT_END_EVENT:
-            return yaml_document_end_event_initialize(event_to,
-                    event_from->data.document_end.implicit);
+            if (!event->data.document_start.version_directive !=
+                    !model->data.document_start.version_directive)
+                return 0;
+            if (event->data.document_start.version_directive) {
+                yaml_version_directive_t *event_version_directive =
+                    event->data.document_start.version_directive;
+                yaml_version_directive_t *model_version_directive =
+                    model->data.document_start.version_directive;
+                if (event_version_directive->major !=
+                        model_version_directive->major ||
+                        event_version_directive->minor !=
+                        model_version_directive->minor)
+                    return 0;
+            }
+            if (event->data.document_start.tag_directives.length !=
+                    model->data.document_start.tag_directives.length)
+                return 0;
+            for (idx = 0; idx < event->data.document_start.tag_directives.length; idx++) {
+                yaml_tag_directive_t *event_tag_directive =
+                    event->data.document_start.tag_directives.list + idx;
+                yaml_tag_directive_t *model_tag_directive =
+                    model->data.document_start.tag_directives.list + idx;
+                if (strcmp(event_tag_directive->handle, model_tag_directive->handle))
+                    return 0;
+                if (strcmp(event_tag_directive->prefix, model_tag_directive->prefix))
+                    return 0;
+            }
+            break;
 
         case YAML_ALIAS_EVENT:
-            return yaml_alias_event_initialize(event_to,
-                    event_from->data.alias.anchor);
+            event_anchor = event->data.alias.anchor;
+            model_anchor = model->data.alias.anchor;
+            break;
 
         case YAML_SCALAR_EVENT:
-            return yaml_scalar_event_initialize(event_to,
-                    event_from->data.scalar.anchor,
-                    event_from->data.scalar.tag,
-                    event_from->data.scalar.value,
-                    event_from->data.scalar.length,
-                    event_from->data.scalar.plain_implicit,
-                    event_from->data.scalar.quoted_implicit,
-                    event_from->data.scalar.style);
+            event_anchor = event->data.scalar.anchor;
+            model_anchor = model->data.scalar.anchor;
+            event_tag = event->data.scalar.tag;
+            model_tag = model->data.scalar.tag;
+            event_value = event->data.scalar.value;
+            model_value = model->data.scalar.value;
+            event_length = event->data.scalar.length;
+            model_length = model->data.scalar.length;
+            if (event->data.scalar.is_plain_implicit !=
+                    model->data.scalar.is_plain_implicit)
+                return 0;
+            if (event->data.scalar.is_quoted_implicit !=
+                    model->data.scalar.is_quoted_implicit)
+                return 0;
+            break;
 
         case YAML_SEQUENCE_START_EVENT:
-            return yaml_sequence_start_event_initialize(event_to,
-                    event_from->data.sequence_start.anchor,
-                    event_from->data.sequence_start.tag,
-                    event_from->data.sequence_start.implicit,
-                    event_from->data.sequence_start.style);
-
-        case YAML_SEQUENCE_END_EVENT:
-            return yaml_sequence_end_event_initialize(event_to);
+            event_anchor = event->data.sequence_start.anchor;
+            model_anchor = model->data.sequence_start.anchor;
+            event_tag = event->data.sequence_start.tag;
+            model_tag = model->data.sequence_start.tag;
+            if (event->data.sequence_start.is_implicit !=
+                    model->data.sequence_start.is_implicit)
+                return 0;
+            break;
 
         case YAML_MAPPING_START_EVENT:
-            return yaml_mapping_start_event_initialize(event_to,
-                    event_from->data.mapping_start.anchor,
-                    event_from->data.mapping_start.tag,
-                    event_from->data.mapping_start.implicit,
-                    event_from->data.mapping_start.style);
-
-        case YAML_MAPPING_END_EVENT:
-            return yaml_mapping_end_event_initialize(event_to);
+            event_anchor = event->data.mapping_start.anchor;
+            model_anchor = model->data.mapping_start.anchor;
+            event_tag = event->data.mapping_start.tag;
+            model_tag = model->data.mapping_start.tag;
+            if (event->data.mapping_start.is_implicit !=
+                    model->data.mapping_start.is_implicit)
+                return 0;
+            break;
 
         default:
-            assert(1);
+            break;
     }
 
-    return 0;
-}
-
-int compare_events(yaml_event_t *event1, yaml_event_t *event2)
-{
-    int k;
-
-    if (event1->type != event2->type)
+    if (!event_anchor != !model_anchor)
+        return 0;
+    if (event_anchor && strcmp(event_anchor, model_anchor))
         return 0;
 
-    switch (event1->type)
-    {
-        case YAML_STREAM_START_EVENT:
-            return 1;
-            /* return (event1->data.stream_start.encoding ==
-                    event2->data.stream_start.encoding); */
+    if (event_tag && !strcmp(event_tag, "!"))
+        event_tag = NULL;
+    if (model_tag && !strcmp(model_tag, "!"))
+        model_tag = NULL;
+    if (!event_tag != !model_tag)
+        return 0;
+    if (event_tag && strcmp(event_tag, model_tag))
+        return 0;
 
-        case YAML_DOCUMENT_START_EVENT:
-            if ((event1->data.document_start.version_directive && !event2->data.document_start.version_directive)
-                    || (!event1->data.document_start.version_directive && event2->data.document_start.version_directive)
-                    || (event1->data.document_start.version_directive && event2->data.document_start.version_directive
-                        && (event1->data.document_start.version_directive->major != event2->data.document_start.version_directive->major
-                            || event1->data.document_start.version_directive->minor != event2->data.document_start.version_directive->minor)))
-                return 0;
-            if ((event1->data.document_start.tag_directives.end - event1->data.document_start.tag_directives.start) !=
-                    (event2->data.document_start.tag_directives.end - event2->data.document_start.tag_directives.start))
-                return 0;
-            for (k = 0; k < (event1->data.document_start.tag_directives.end - event1->data.document_start.tag_directives.start); k ++) {
-                if ((strcmp((char *)event1->data.document_start.tag_directives.start[k].handle,
-                                (char *)event2->data.document_start.tag_directives.start[k].handle) != 0)
-                        || (strcmp((char *)event1->data.document_start.tag_directives.start[k].prefix,
-                            (char *)event2->data.document_start.tag_directives.start[k].prefix) != 0))
-                    return 0;
-            }
-            /* if (event1->data.document_start.implicit != event2->data.document_start.implicit)
-                return 0; */
-            return 1;
+    if (event_length != model_length)
+        return 0;
+    if (event_length && memcmp(event_value, model_value, event_length))
+        return 0;
 
-        case YAML_DOCUMENT_END_EVENT:
-            return 1;
-            /* return (event1->data.document_end.implicit ==
-                    event2->data.document_end.implicit); */
-
-        case YAML_ALIAS_EVENT:
-            return (strcmp((char *)event1->data.alias.anchor,
-                        (char *)event2->data.alias.anchor) == 0);
-
-        case YAML_SCALAR_EVENT:
-            if ((event1->data.scalar.anchor && !event2->data.scalar.anchor)
-                    || (!event1->data.scalar.anchor && event2->data.scalar.anchor)
-                    || (event1->data.scalar.anchor && event2->data.scalar.anchor
-                        && strcmp((char *)event1->data.scalar.anchor,
-                            (char *)event2->data.scalar.anchor) != 0))
-                return 0;
-            if ((event1->data.scalar.tag && !event2->data.scalar.tag
-                        && strcmp((char *)event1->data.scalar.tag, "!") != 0)
-                    || (!event1->data.scalar.tag && event2->data.scalar.tag
-                        && strcmp((char *)event2->data.scalar.tag, "!") != 0)
-                    || (event1->data.scalar.tag && event2->data.scalar.tag
-                        && strcmp((char *)event1->data.scalar.tag,
-                            (char *)event2->data.scalar.tag) != 0))
-                return 0;
-            if ((event1->data.scalar.length != event2->data.scalar.length)
-                    || memcmp(event1->data.scalar.value, event2->data.scalar.value,
-                        event1->data.scalar.length) != 0)
-                return 0;
-            if ((event1->data.scalar.plain_implicit != event2->data.scalar.plain_implicit)
-                    || (event2->data.scalar.quoted_implicit != event2->data.scalar.quoted_implicit)
-                    /* || (event2->data.scalar.style != event2->data.scalar.style) */)
-                return 0;
-            return 1;
-
-        case YAML_SEQUENCE_START_EVENT:
-            if ((event1->data.sequence_start.anchor && !event2->data.sequence_start.anchor)
-                    || (!event1->data.sequence_start.anchor && event2->data.sequence_start.anchor)
-                    || (event1->data.sequence_start.anchor && event2->data.sequence_start.anchor
-                        && strcmp((char *)event1->data.sequence_start.anchor,
-                            (char *)event2->data.sequence_start.anchor) != 0))
-                return 0;
-            if ((event1->data.sequence_start.tag && !event2->data.sequence_start.tag)
-                    || (!event1->data.sequence_start.tag && event2->data.sequence_start.tag)
-                    || (event1->data.sequence_start.tag && event2->data.sequence_start.tag
-                        && strcmp((char *)event1->data.sequence_start.tag,
-                            (char *)event2->data.sequence_start.tag) != 0))
-                return 0;
-            if ((event1->data.sequence_start.implicit != event2->data.sequence_start.implicit)
-                    /* || (event2->data.sequence_start.style != event2->data.sequence_start.style) */)
-                return 0;
-            return 1;
-
-        case YAML_MAPPING_START_EVENT:
-            if ((event1->data.mapping_start.anchor && !event2->data.mapping_start.anchor)
-                    || (!event1->data.mapping_start.anchor && event2->data.mapping_start.anchor)
-                    || (event1->data.mapping_start.anchor && event2->data.mapping_start.anchor
-                        && strcmp((char *)event1->data.mapping_start.anchor,
-                            (char *)event2->data.mapping_start.anchor) != 0))
-                return 0;
-            if ((event1->data.mapping_start.tag && !event2->data.mapping_start.tag)
-                    || (!event1->data.mapping_start.tag && event2->data.mapping_start.tag)
-                    || (event1->data.mapping_start.tag && event2->data.mapping_start.tag
-                        && strcmp((char *)event1->data.mapping_start.tag,
-                            (char *)event2->data.mapping_start.tag) != 0))
-                return 0;
-            if ((event1->data.mapping_start.implicit != event2->data.mapping_start.implicit)
-                    /* || (event2->data.mapping_start.style != event2->data.mapping_start.style) */)
-                return 0;
-            return 1;
-
-        default:
-            return 1;
-    }
+    return 1;
 }
 
 int print_output(char *name, unsigned char *buffer, size_t size, int count)
 int
 main(int argc, char *argv[])
 {
-    int number;
-    int canonical = 0;
-    int unicode = 0;
+    int idx;
+    int is_canonical = 0;
+    int is_unicode = 0;
 
-    number = 1;
-    while (number < argc) {
-        if (strcmp(argv[number], "-c") == 0) {
-            canonical = 1;
+    idx = 1;
+    while (idx < argc) {
+        if (strcmp(argv[idx], "-c") == 0) {
+            is_canonical = 1;
         }
-        else if (strcmp(argv[number], "-u") == 0) {
-            unicode = 1;
+        else if (strcmp(argv[idx], "-u") == 0) {
+            is_unicode = 1;
         }
-        else if (argv[number][0] == '-') {
-            printf("Unknown option: '%s'\n", argv[number]);
+        else if (argv[idx][0] == '-') {
+            printf("Unknown option: '%s'\n", argv[idx]);
             return 0;
         }
-        if (argv[number][0] == '-') {
-            if (number < argc-1) {
-                memmove(argv+number, argv+number+1, (argc-number-1)*sizeof(char *));
+        if (argv[idx][0] == '-') {
+            if (idx < argc-1) {
+                memmove(argv+idx, argv+idx+1, (argc-idx-1)*sizeof(char *));
             }
             argc --;
         }
         else {
-            number ++;
+            idx ++;
         }
     }
 
         return 0;
     }
 
-    for (number = 1; number < argc; number ++)
+    for (idx = 1; idx < argc; idx ++)
     {
         FILE *file;
-        yaml_parser_t parser;
-        yaml_emitter_t emitter;
+        yaml_parser_t *parser;
+        yaml_emitter_t *emitter;
         yaml_event_t event;
         unsigned char buffer[BUFFER_SIZE];
         size_t written = 0;
         yaml_event_t events[MAX_EVENTS];
         size_t event_number = 0;
-        int done = 0;
         int count = 0;
-        int error = 0;
-        int k;
+        int failed = 0;
+
         memset(buffer, 0, BUFFER_SIZE);
         memset(events, 0, MAX_EVENTS*sizeof(yaml_event_t));
 
-        printf("[%d] Parsing, emitting, and parsing again '%s': ", number, argv[number]);
+        printf("[%d] Parsing, emitting, and parsing again '%s': ", idx, argv[idx]);
         fflush(stdout);
 
-        file = fopen(argv[number], "rb");
+        file = fopen(argv[idx], "rb");
         assert(file);
 
-        assert(yaml_parser_initialize(&parser));
-        yaml_parser_set_input_file(&parser, file);
-        assert(yaml_emitter_initialize(&emitter));
-        if (canonical) {
-            yaml_emitter_set_canonical(&emitter, 1);
+        parser = yaml_parser_new();
+        assert(parser);
+
+        yaml_parser_set_file_reader(parser, file);
+
+        emitter = yaml_emitter_new();
+        assert(emitter);
+
+        if (is_canonical) {
+            yaml_emitter_set_canonical(emitter, 1);
         }
-        if (unicode) {
-            yaml_emitter_set_unicode(&emitter, 1);
+        if (is_unicode) {
+            yaml_emitter_set_unicode(emitter, 1);
         }
-        yaml_emitter_set_output_string(&emitter, buffer, BUFFER_SIZE, &written);
+        yaml_emitter_set_string_writer(emitter, buffer, BUFFER_SIZE, &written);
 
-        while (!done)
+        while (1)
         {
-            if (!yaml_parser_parse(&parser, &event)) {
-                error = 1;
+            if (!yaml_parser_parse_event(parser, &event))
+                failed = 1;
+
+            if (!event.type)
                 break;
-            }
 
-            done = (event.type == YAML_STREAM_END_EVENT);
             assert(event_number < MAX_EVENTS);
-            assert(copy_event(&(events[event_number++]), &event));
-            assert(yaml_emitter_emit(&emitter, &event) || 
-                    (yaml_emitter_flush(&emitter) && print_output(argv[number], buffer, written, count)));
+            yaml_event_duplicate(&(events[event_number++]), &event);
+            assert(yaml_emitter_emit_event(emitter, &event) || 
+                    (yaml_emitter_flush(emitter) &&
+                     print_output(argv[idx], buffer, written, count)));
             count ++;
         }
 
-        yaml_parser_delete(&parser);
-        assert(!fclose(file));
-        yaml_emitter_delete(&emitter);
+        yaml_parser_delete(parser);
+        fclose(file);
+        yaml_emitter_delete(emitter);
 
-        if (!error)
+        if (!failed)
         {
-            count = done = 0;
-            assert(yaml_parser_initialize(&parser));
-            yaml_parser_set_input_string(&parser, buffer, written);
+            count = 0;
+            parser = yaml_parser_new();
+            yaml_parser_set_string_reader(parser, buffer, written);
 
-            while (!done)
+            while (1)
             {
-                assert(yaml_parser_parse(&parser, &event) || print_output(argv[number], buffer, written, count));
-                done = (event.type == YAML_STREAM_END_EVENT);
-                assert(compare_events(events+count, &event) || print_output(argv[number], buffer, written, count));
-                yaml_event_delete(&event);
+                assert(yaml_parser_parse_event(parser, &event) ||
+                        print_output(argv[idx], buffer, written, count));
+                if (!event.type)
+                    break;
+                assert(compare_events(events+count, &event) ||
+                        print_output(argv[idx], buffer, written, count));
+                yaml_event_destroy(&event);
                 count ++;
             }
-            yaml_parser_delete(&parser);
+            yaml_parser_delete(parser);
         }
 
-        for (k = 0; k < event_number; k ++) {
-            yaml_event_delete(events+k);
+        while(event_number) {
+            yaml_event_destroy(events+(--event_number));
         }
 
         printf("PASSED (length: %d)\n", written);
-        print_output(argv[number], buffer, written, -1);
+        print_output(argv[idx], buffer, written, -1);
     }
 
     return 0;