]> andersk Git - libyaml.git/blob - tests/run-dumper.c
Make declarations before other statements
[libyaml.git] / tests / run-dumper.c
1 #include <yaml.h>
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #ifdef NDEBUG
8 #undef NDEBUG
9 #endif
10 #include <assert.h>
11
12 #define BUFFER_SIZE 65536
13 #define MAX_DOCUMENTS  16
14
15 int copy_document(yaml_document_t *document_to, yaml_document_t *document_from)
16 {
17     yaml_node_t *node;
18     yaml_node_item_t *item;
19     yaml_node_pair_t *pair;
20
21     if (!yaml_document_initialize(document_to, document_from->version_directive,
22                 document_from->tag_directives.start,
23                 document_from->tag_directives.end,
24                 document_from->start_implicit, document_from->end_implicit))
25         return 0;
26
27     for (node = document_from->nodes.start;
28             node < document_from->nodes.top; node ++) {
29         switch (node->type) {
30             case YAML_SCALAR_NODE:
31                 if (!yaml_document_add_scalar(document_to, node->tag,
32                             node->data.scalar.value, node->data.scalar.length,
33                             node->data.scalar.style)) goto error;
34                 break;
35             case YAML_SEQUENCE_NODE:
36                 if (!yaml_document_add_sequence(document_to, node->tag,
37                             node->data.sequence.style)) goto error;
38                 break;
39             case YAML_MAPPING_NODE:
40                 if (!yaml_document_add_mapping(document_to, node->tag,
41                             node->data.mapping.style)) goto error;
42                 break;
43             default:
44                 assert(0);
45                 break;
46         }
47     }
48
49     for (node = document_from->nodes.start;
50             node < document_from->nodes.top; node ++) {
51         switch (node->type) {
52             case YAML_SEQUENCE_NODE:
53                 for (item = node->data.sequence.items.start;
54                         item < node->data.sequence.items.top; item ++) {
55                     if (!yaml_document_append_sequence_item(document_to,
56                                 node - document_from->nodes.start + 1,
57                                 *item)) goto error;
58                 }
59                 break;
60             case YAML_MAPPING_NODE:
61                 for (pair = node->data.mapping.pairs.start;
62                         pair < node->data.mapping.pairs.top; pair ++) {
63                     if (!yaml_document_append_mapping_pair(document_to,
64                                 node - document_from->nodes.start + 1,
65                                 pair->key, pair->value)) goto error;
66                 }
67                 break;
68             default:
69                 break;
70         }
71     }
72     return 1;
73
74 error:
75     yaml_document_delete(document_to);
76     return 0;
77 }
78
79 int compare_nodes(yaml_document_t *document1, int index1,
80         yaml_document_t *document2, int index2, int level)
81 {
82     int k;
83     yaml_node_t *node1;
84     yaml_node_t *node2;
85     if (level++ > 1000) return 0;
86     node1 = yaml_document_get_node(document1, index1);
87     node2 = yaml_document_get_node(document2, index2);
88
89     assert(node1);
90     assert(node2);
91
92     if (node1->type != node2->type)
93         return 0;
94
95     if (strcmp((char *)node1->tag, (char *)node2->tag) != 0) return 0;
96
97     switch (node1->type) {
98         case YAML_SCALAR_NODE:
99             if (node1->data.scalar.length != node2->data.scalar.length)
100                 return 0;
101             if (strncmp((char *)node1->data.scalar.value, (char *)node2->data.scalar.value,
102                         node1->data.scalar.length) != 0) return 0;
103             break;
104         case YAML_SEQUENCE_NODE:
105             if ((node1->data.sequence.items.top - node1->data.sequence.items.start) !=
106                     (node2->data.sequence.items.top - node2->data.sequence.items.start))
107                 return 0;
108             for (k = 0; k < (node1->data.sequence.items.top - node1->data.sequence.items.start); k ++) {
109                 if (!compare_nodes(document1, node1->data.sequence.items.start[k],
110                             document2, node2->data.sequence.items.start[k], level)) return 0;
111             }
112             break;
113         case YAML_MAPPING_NODE:
114             if ((node1->data.mapping.pairs.top - node1->data.mapping.pairs.start) !=
115                     (node2->data.mapping.pairs.top - node2->data.mapping.pairs.start))
116                 return 0;
117             for (k = 0; k < (node1->data.mapping.pairs.top - node1->data.mapping.pairs.start); k ++) {
118                 if (!compare_nodes(document1, node1->data.mapping.pairs.start[k].key,
119                             document2, node2->data.mapping.pairs.start[k].key, level)) return 0;
120                 if (!compare_nodes(document1, node1->data.mapping.pairs.start[k].value,
121                             document2, node2->data.mapping.pairs.start[k].value, level)) return 0;
122             }
123             break;
124         default:
125             assert(0);
126             break;
127     }
128     return 1;
129 }
130
131 int compare_documents(yaml_document_t *document1, yaml_document_t *document2)
132 {
133     int k;
134
135     if ((document1->version_directive && !document2->version_directive)
136             || (!document1->version_directive && document2->version_directive)
137             || (document1->version_directive && document2->version_directive
138                 && (document1->version_directive->major != document2->version_directive->major
139                     || document1->version_directive->minor != document2->version_directive->minor)))
140         return 0;
141
142     if ((document1->tag_directives.end - document1->tag_directives.start) !=
143             (document2->tag_directives.end - document2->tag_directives.start))
144         return 0;
145     for (k = 0; k < (document1->tag_directives.end - document1->tag_directives.start); k ++) {
146         if ((strcmp((char *)document1->tag_directives.start[k].handle,
147                         (char *)document2->tag_directives.start[k].handle) != 0)
148                 || (strcmp((char *)document1->tag_directives.start[k].prefix,
149                     (char *)document2->tag_directives.start[k].prefix) != 0))
150             return 0;
151     }
152
153     if ((document1->nodes.top - document1->nodes.start) !=
154             (document2->nodes.top - document2->nodes.start))
155         return 0;
156
157     if (document1->nodes.top != document1->nodes.start) {
158         if (!compare_nodes(document1, 1, document2, 1, 0))
159             return 0;
160     }
161
162     return 1;
163 }
164
165 int print_output(char *name, unsigned char *buffer, size_t size, int count)
166 {
167     FILE *file;
168     char data[BUFFER_SIZE];
169     size_t data_size = 1;
170     size_t total_size = 0;
171     if (count >= 0) {
172         printf("FAILED (at the document #%d)\nSOURCE:\n", count+1);
173     }
174     file = fopen(name, "rb");
175     assert(file);
176     while (data_size > 0) {
177         data_size = fread(data, 1, BUFFER_SIZE, file);
178         assert(!ferror(file));
179         if (!data_size) break;
180         assert(fwrite(data, 1, data_size, stdout) == data_size);
181         total_size += data_size;
182         if (feof(file)) break;
183     }
184     fclose(file);
185     printf("#### (length: %ld)\n", (long)total_size);
186     printf("OUTPUT:\n%s#### (length: %ld)\n", buffer, (long)size);
187     return 0;
188 }
189
190 int
191 main(int argc, char *argv[])
192 {
193     int number;
194     int canonical = 0;
195     int unicode = 0;
196
197     number = 1;
198     while (number < argc) {
199         if (strcmp(argv[number], "-c") == 0) {
200             canonical = 1;
201         }
202         else if (strcmp(argv[number], "-u") == 0) {
203             unicode = 1;
204         }
205         else if (argv[number][0] == '-') {
206             printf("Unknown option: '%s'\n", argv[number]);
207             return 0;
208         }
209         if (argv[number][0] == '-') {
210             if (number < argc-1) {
211                 memmove(argv+number, argv+number+1, (argc-number-1)*sizeof(char *));
212             }
213             argc --;
214         }
215         else {
216             number ++;
217         }
218     }
219
220     if (argc < 2) {
221         printf("Usage: %s [-c] [-u] file1.yaml ...\n", argv[0]);
222         return 0;
223     }
224
225     for (number = 1; number < argc; number ++)
226     {
227         FILE *file;
228         yaml_parser_t parser;
229         yaml_emitter_t emitter;
230
231         yaml_document_t document;
232         unsigned char buffer[BUFFER_SIZE+1];
233         size_t written = 0;
234         yaml_document_t documents[MAX_DOCUMENTS];
235         size_t document_number = 0;
236         int done = 0;
237         int count = 0;
238         int error = 0;
239         int k;
240         memset(buffer, 0, BUFFER_SIZE+1);
241         memset(documents, 0, MAX_DOCUMENTS*sizeof(yaml_document_t));
242
243         printf("[%d] Loading, dumping, and loading again '%s': ", number, argv[number]);
244         fflush(stdout);
245
246         file = fopen(argv[number], "rb");
247         assert(file);
248
249         assert(yaml_parser_initialize(&parser));
250         yaml_parser_set_input_file(&parser, file);
251         assert(yaml_emitter_initialize(&emitter));
252         if (canonical) {
253             yaml_emitter_set_canonical(&emitter, 1);
254         }
255         if (unicode) {
256             yaml_emitter_set_unicode(&emitter, 1);
257         }
258         yaml_emitter_set_output_string(&emitter, buffer, BUFFER_SIZE, &written);
259         yaml_emitter_open(&emitter);
260
261         while (!done)
262         {
263             if (!yaml_parser_load(&parser, &document)) {
264                 error = 1;
265                 break;
266             }
267
268             done = (!yaml_document_get_root_node(&document));
269             if (!done) {
270                 assert(document_number < MAX_DOCUMENTS);
271                 assert(copy_document(&(documents[document_number++]), &document));
272                 assert(yaml_emitter_dump(&emitter, &document) || 
273                         (yaml_emitter_flush(&emitter) && print_output(argv[number], buffer, written, count)));
274                 count ++;
275             }
276             else {
277                 yaml_document_delete(&document);
278             }
279         }
280
281         yaml_parser_delete(&parser);
282         assert(!fclose(file));
283         yaml_emitter_close(&emitter);
284         yaml_emitter_delete(&emitter);
285
286         if (!error)
287         {
288             count = done = 0;
289             assert(yaml_parser_initialize(&parser));
290             yaml_parser_set_input_string(&parser, buffer, written);
291
292             while (!done)
293             {
294                 assert(yaml_parser_load(&parser, &document) || print_output(argv[number], buffer, written, count));
295                 done = (!yaml_document_get_root_node(&document));
296                 if (!done) {
297                     assert(compare_documents(documents+count, &document) || print_output(argv[number], buffer, written, count));
298                     count ++;
299                 }
300                 yaml_document_delete(&document);
301             }
302             yaml_parser_delete(&parser);
303         }
304
305         for (k = 0; k < document_number; k ++) {
306             yaml_document_delete(documents+k);
307         }
308
309         printf("PASSED (length: %ld)\n", (long)written);
310         print_output(argv[number], buffer, written, -1);
311     }
312
313     return 0;
314 }
This page took 0.063262 seconds and 5 git commands to generate.