Source

libyaml / tests / test-reader.c

Diff from to

File tests/test-reader.c

 #include <yaml.h>
 
+YAML_DECLARE(int)
+yaml_parser_update_buffer(yaml_parser_t *parser, size_t length);
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
     {"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");
         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 ++;
             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");
     }
 
 int check_boms(void)
 {
-    yaml_parser_t *parser;
+    yaml_parser_t parser;
     int failed = 0;
     int k;
     printf("checking boms...\n");
         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;
 
 int check_long_utf8(void)
 {
-    yaml_parser_t *parser;
+    yaml_parser_t parser;
     int k = 0;
     int j;
     int failed = 0;
             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;
             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;
 
 int check_long_utf16(void)
 {
-    yaml_parser_t *parser;
+    yaml_parser_t parser;
     int k = 0;
     int j;
     int failed = 0;
             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;
             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;