Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/extract/src/misc-test.c @ 3:2c135c81b16c
MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:44:09 +0200 |
| parents | b50eed0cc0ef |
| children |
comparison
equal
deleted
inserted
replaced
| 0:6015a75abc2d | 3:2c135c81b16c |
|---|---|
| 1 #include "memento.h" | |
| 2 #include "xml.h" | |
| 3 | |
| 4 #include <errno.h> | |
| 5 #include <stdio.h> | |
| 6 | |
| 7 | |
| 8 static int s_num_fails = 0; | |
| 9 | |
| 10 static void s_check( | |
| 11 int values_equal, | |
| 12 const char *text, | |
| 13 int ret, | |
| 14 const char *value_s, | |
| 15 int errno_, | |
| 16 const char *value_expected_s, | |
| 17 int errno_expected | |
| 18 ) | |
| 19 { | |
| 20 int ok; | |
| 21 | |
| 22 if (errno_expected) | |
| 23 ok = (ret == -1 && errno_ == errno_expected); | |
| 24 else | |
| 25 ok = (ret == 0 && values_equal); | |
| 26 | |
| 27 if (ok) | |
| 28 printf(" ok: "); | |
| 29 else | |
| 30 printf(" fail:"); | |
| 31 printf(" text=%16s", text); | |
| 32 if (errno_expected) | |
| 33 printf(" errno_expected=%6i", errno_expected); | |
| 34 else | |
| 35 printf(" value_expected=%6s", value_expected_s); | |
| 36 printf(". result: ret=%2i value=%6s errno=%3i", ret, value_s, errno_); | |
| 37 printf(".\n"); | |
| 38 if (!ok) | |
| 39 s_num_fails += 1; | |
| 40 } | |
| 41 | |
| 42 static void s_check_e( int e, const char* text) | |
| 43 { | |
| 44 if (e) | |
| 45 { | |
| 46 s_num_fails += 1; | |
| 47 printf( "Error: e=%i: %s\n", e, text); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 static void s_check_int(const char* text, int value_expected, int expected_errno) | |
| 52 { | |
| 53 int value; | |
| 54 int ret = extract_xml_str_to_int(text, &value); | |
| 55 char value_s[32]; | |
| 56 char value_expected_s[32]; | |
| 57 snprintf(value_s, sizeof(value_s), "%i", value); | |
| 58 snprintf(value_expected_s, sizeof(value_expected_s), "%i", value_expected); | |
| 59 s_check(value == value_expected, text, ret, value_s, errno, value_expected_s, expected_errno); | |
| 60 } | |
| 61 | |
| 62 static void s_check_uint(const char* text, unsigned expected_value, int expected_errno) | |
| 63 { | |
| 64 unsigned value; | |
| 65 int ret = extract_xml_str_to_uint(text, &value); | |
| 66 char value_s[32]; | |
| 67 char value_expected_s[32]; | |
| 68 snprintf(value_s, sizeof(value_s), "%u", value); | |
| 69 snprintf(value_expected_s, sizeof(value_expected_s), "%u", value); | |
| 70 s_check(value == expected_value, text, ret, value_s, errno, value_expected_s, expected_errno); | |
| 71 } | |
| 72 | |
| 73 static void s_check_xml_parse() | |
| 74 { | |
| 75 int e; | |
| 76 extract_buffer_t* buffer; | |
| 77 extract_xml_tag_t tag; | |
| 78 unsigned i; | |
| 79 const char* texts[] = { | |
| 80 "<foo a=1>text</foo>", | |
| 81 "< >", | |
| 82 "<foo bar=>", | |
| 83 "< bar=>", | |
| 84 "< =>", | |
| 85 }; | |
| 86 | |
| 87 extract_xml_tag_init( &tag); | |
| 88 | |
| 89 for (i=0; i<sizeof(texts) / sizeof(texts[0]); ++i) | |
| 90 { | |
| 91 const char* text = texts[i]; | |
| 92 printf("testing extract_xml_pparse_*(): %s\n", text); | |
| 93 e = extract_buffer_open_simple( | |
| 94 NULL /*alloc*/, | |
| 95 text, | |
| 96 strlen(text), | |
| 97 NULL /*handle*/, | |
| 98 NULL /*fn_close*/, | |
| 99 &buffer | |
| 100 ); | |
| 101 s_check_e( e, "extract_buffer_open_simple()"); | |
| 102 e = extract_xml_pparse_init( NULL /*alloc*/, buffer, NULL /*first_line*/); | |
| 103 s_check_e( e, "extract_xml_pparse_init()"); | |
| 104 | |
| 105 e = extract_xml_pparse_next( buffer, &tag); | |
| 106 s_check_e( e, "extract_xml_pparse_next()"); | |
| 107 s_check_e( tag.name ? 0 : 1, "tag.name is not null"); | |
| 108 | |
| 109 { | |
| 110 int j; | |
| 111 for (j=0; j<tag.attributes_num; ++j) | |
| 112 { | |
| 113 s_check_e( tag.attributes[j].name ? 0 : 1, "attribute is non-null"); | |
| 114 s_check_e( tag.attributes[j].value ? 0 : 1, "attribute is non-null"); | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 int main(void) | |
| 121 { | |
| 122 printf("testing extract_xml_str_to_int():\n"); | |
| 123 s_check_int("2", 2, 0); | |
| 124 s_check_int("-20", -20, 0); | |
| 125 s_check_int("-20b", 0, EINVAL); | |
| 126 s_check_int("123456789123", 0, ERANGE); | |
| 127 | |
| 128 printf("testing extract_xml_str_to_uint():\n"); | |
| 129 s_check_uint("2", 2, 0); | |
| 130 s_check_uint("-20", 0, ERANGE); | |
| 131 s_check_uint("-20b", 0, EINVAL); | |
| 132 s_check_uint("123456789123", 0, ERANGE); | |
| 133 | |
| 134 s_check_xml_parse(); | |
| 135 | |
| 136 printf("s_num_fails=%i\n", s_num_fails); | |
| 137 | |
| 138 if (s_num_fails) { | |
| 139 printf("Failed\n"); | |
| 140 return 1; | |
| 141 } | |
| 142 else { | |
| 143 printf("Succeeded\n"); | |
| 144 return 0; | |
| 145 } | |
| 146 } |
