Path: blob/main/contrib/libyaml/tests/run-dumper.c
39507 views
#include <yaml.h>12#include <stdlib.h>3#include <stdio.h>4#include <string.h>56#ifdef NDEBUG7#undef NDEBUG8#endif9#include <assert.h>1011#define BUFFER_SIZE 6553612#define MAX_DOCUMENTS 161314int copy_document(yaml_document_t *document_to, yaml_document_t *document_from)15{16yaml_node_t *node;17yaml_node_item_t *item;18yaml_node_pair_t *pair;1920if (!yaml_document_initialize(document_to, document_from->version_directive,21document_from->tag_directives.start,22document_from->tag_directives.end,23document_from->start_implicit, document_from->end_implicit))24return 0;2526for (node = document_from->nodes.start;27node < document_from->nodes.top; node ++) {28switch (node->type) {29case YAML_SCALAR_NODE:30if (!yaml_document_add_scalar(document_to, node->tag,31node->data.scalar.value, node->data.scalar.length,32node->data.scalar.style)) goto error;33break;34case YAML_SEQUENCE_NODE:35if (!yaml_document_add_sequence(document_to, node->tag,36node->data.sequence.style)) goto error;37break;38case YAML_MAPPING_NODE:39if (!yaml_document_add_mapping(document_to, node->tag,40node->data.mapping.style)) goto error;41break;42default:43assert(0);44break;45}46}4748for (node = document_from->nodes.start;49node < document_from->nodes.top; node ++) {50switch (node->type) {51case YAML_SEQUENCE_NODE:52for (item = node->data.sequence.items.start;53item < node->data.sequence.items.top; item ++) {54if (!yaml_document_append_sequence_item(document_to,55node - document_from->nodes.start + 1,56*item)) goto error;57}58break;59case YAML_MAPPING_NODE:60for (pair = node->data.mapping.pairs.start;61pair < node->data.mapping.pairs.top; pair ++) {62if (!yaml_document_append_mapping_pair(document_to,63node - document_from->nodes.start + 1,64pair->key, pair->value)) goto error;65}66break;67default:68break;69}70}71return 1;7273error:74yaml_document_delete(document_to);75return 0;76}7778int compare_nodes(yaml_document_t *document1, int index1,79yaml_document_t *document2, int index2, int level)80{81int k;82yaml_node_t *node1;83yaml_node_t *node2;84if (level++ > 1000) return 0;85node1 = yaml_document_get_node(document1, index1);86node2 = yaml_document_get_node(document2, index2);8788assert(node1);89assert(node2);9091if (node1->type != node2->type)92return 0;9394if (strcmp((char *)node1->tag, (char *)node2->tag) != 0) return 0;9596switch (node1->type) {97case YAML_SCALAR_NODE:98if (node1->data.scalar.length != node2->data.scalar.length)99return 0;100if (strncmp((char *)node1->data.scalar.value, (char *)node2->data.scalar.value,101node1->data.scalar.length) != 0) return 0;102break;103case YAML_SEQUENCE_NODE:104if ((node1->data.sequence.items.top - node1->data.sequence.items.start) !=105(node2->data.sequence.items.top - node2->data.sequence.items.start))106return 0;107for (k = 0; k < (node1->data.sequence.items.top - node1->data.sequence.items.start); k ++) {108if (!compare_nodes(document1, node1->data.sequence.items.start[k],109document2, node2->data.sequence.items.start[k], level)) return 0;110}111break;112case YAML_MAPPING_NODE:113if ((node1->data.mapping.pairs.top - node1->data.mapping.pairs.start) !=114(node2->data.mapping.pairs.top - node2->data.mapping.pairs.start))115return 0;116for (k = 0; k < (node1->data.mapping.pairs.top - node1->data.mapping.pairs.start); k ++) {117if (!compare_nodes(document1, node1->data.mapping.pairs.start[k].key,118document2, node2->data.mapping.pairs.start[k].key, level)) return 0;119if (!compare_nodes(document1, node1->data.mapping.pairs.start[k].value,120document2, node2->data.mapping.pairs.start[k].value, level)) return 0;121}122break;123default:124assert(0);125break;126}127return 1;128}129130int compare_documents(yaml_document_t *document1, yaml_document_t *document2)131{132int k;133134if ((document1->version_directive && !document2->version_directive)135|| (!document1->version_directive && document2->version_directive)136|| (document1->version_directive && document2->version_directive137&& (document1->version_directive->major != document2->version_directive->major138|| document1->version_directive->minor != document2->version_directive->minor)))139return 0;140141if ((document1->tag_directives.end - document1->tag_directives.start) !=142(document2->tag_directives.end - document2->tag_directives.start))143return 0;144for (k = 0; k < (document1->tag_directives.end - document1->tag_directives.start); k ++) {145if ((strcmp((char *)document1->tag_directives.start[k].handle,146(char *)document2->tag_directives.start[k].handle) != 0)147|| (strcmp((char *)document1->tag_directives.start[k].prefix,148(char *)document2->tag_directives.start[k].prefix) != 0))149return 0;150}151152if ((document1->nodes.top - document1->nodes.start) !=153(document2->nodes.top - document2->nodes.start))154return 0;155156if (document1->nodes.top != document1->nodes.start) {157if (!compare_nodes(document1, 1, document2, 1, 0))158return 0;159}160161return 1;162}163164int print_output(char *name, unsigned char *buffer, size_t size, int count)165{166FILE *file;167char data[BUFFER_SIZE];168size_t data_size = 1;169size_t total_size = 0;170if (count >= 0) {171printf("FAILED (at the document #%d)\nSOURCE:\n", count+1);172}173file = fopen(name, "rb");174assert(file);175while (data_size > 0) {176data_size = fread(data, 1, BUFFER_SIZE, file);177assert(!ferror(file));178if (!data_size) break;179assert(fwrite(data, 1, data_size, stdout) == data_size);180total_size += data_size;181if (feof(file)) break;182}183fclose(file);184printf("#### (length: %ld)\n", (long)total_size);185printf("OUTPUT:\n%s#### (length: %ld)\n", buffer, (long)size);186return 0;187}188189int190main(int argc, char *argv[])191{192int number;193int canonical = 0;194int unicode = 0;195196number = 1;197while (number < argc) {198if (strcmp(argv[number], "-c") == 0) {199canonical = 1;200}201else if (strcmp(argv[number], "-u") == 0) {202unicode = 1;203}204else if (argv[number][0] == '-') {205printf("Unknown option: '%s'\n", argv[number]);206return 0;207}208if (argv[number][0] == '-') {209if (number < argc-1) {210memmove(argv+number, argv+number+1, (argc-number-1)*sizeof(char *));211}212argc --;213}214else {215number ++;216}217}218219if (argc < 2) {220printf("Usage: %s [-c] [-u] file1.yaml ...\n", argv[0]);221return 0;222}223224for (number = 1; number < argc; number ++)225{226FILE *file;227yaml_parser_t parser;228yaml_emitter_t emitter;229230yaml_document_t document;231unsigned char buffer[BUFFER_SIZE+1];232size_t written = 0;233yaml_document_t documents[MAX_DOCUMENTS];234size_t document_number = 0;235int done = 0;236int count = 0;237int error = 0;238int k;239memset(buffer, 0, BUFFER_SIZE+1);240memset(documents, 0, MAX_DOCUMENTS*sizeof(yaml_document_t));241242printf("[%d] Loading, dumping, and loading again '%s': ", number, argv[number]);243fflush(stdout);244245file = fopen(argv[number], "rb");246assert(file);247248assert(yaml_parser_initialize(&parser));249yaml_parser_set_input_file(&parser, file);250assert(yaml_emitter_initialize(&emitter));251if (canonical) {252yaml_emitter_set_canonical(&emitter, 1);253}254if (unicode) {255yaml_emitter_set_unicode(&emitter, 1);256}257yaml_emitter_set_output_string(&emitter, buffer, BUFFER_SIZE, &written);258yaml_emitter_open(&emitter);259260while (!done)261{262if (!yaml_parser_load(&parser, &document)) {263error = 1;264break;265}266267done = (!yaml_document_get_root_node(&document));268if (!done) {269assert(document_number < MAX_DOCUMENTS);270assert(copy_document(&(documents[document_number++]), &document));271assert(yaml_emitter_dump(&emitter, &document) ||272(yaml_emitter_flush(&emitter) && print_output(argv[number], buffer, written, count)));273count ++;274}275else {276yaml_document_delete(&document);277}278}279280yaml_parser_delete(&parser);281assert(!fclose(file));282yaml_emitter_close(&emitter);283yaml_emitter_delete(&emitter);284285if (!error)286{287count = done = 0;288assert(yaml_parser_initialize(&parser));289yaml_parser_set_input_string(&parser, buffer, written);290291while (!done)292{293assert(yaml_parser_load(&parser, &document) || print_output(argv[number], buffer, written, count));294done = (!yaml_document_get_root_node(&document));295if (!done) {296assert(compare_documents(documents+count, &document) || print_output(argv[number], buffer, written, count));297count ++;298}299yaml_document_delete(&document);300}301yaml_parser_delete(&parser);302}303304for (k = 0; k < document_number; k ++) {305yaml_document_delete(documents+k);306}307308printf("PASSED (length: %ld)\n", (long)written);309print_output(argv[number], buffer, written, -1);310}311312return 0;313}314315316