Update comment at isds_get_box_list_archive()
[libisds.git] / test / offline / isds-interpret_matches.c
blob8c9a424f8c68cb6df1af13bc7f80ea6b58c57591
1 #include "../test.h"
2 #include <stddef.h> /* For ptrdiff_t */
3 #include "isds.c"
5 static int compare_lists(const char *label,
6 const xmlChar *expected_string, const struct isds_list *expected_list,
7 const xmlChar* string, const struct isds_list *list) {
8 const struct isds_list *expected_item, *item;
9 ptrdiff_t expected_offset, offset;
10 int i;
12 TEST_POINTER_DUPLICITY(expected_list, list);
13 for (i = 1, expected_item = expected_list, item = list;
14 NULL != expected_item && NULL != item;
15 i++, expected_item = expected_item->next, item = item->next) {
16 expected_offset = (xmlChar *)expected_item->data - expected_string;
17 offset = (xmlChar *)item->data - string;
18 if (expected_offset != offset)
19 FAIL_TEST("%d. %s match offsets differ: expected=%td, got=%td",
20 i, label, expected_offset, offset);
22 if (NULL != expected_item && NULL == item)
23 FAIL_TEST("%s match offsets list is missing %d. item", label, i);
24 if (NULL == expected_item && NULL != item)
25 FAIL_TEST("%s match offsets list has superfluous %d. item", label, i);
27 return 0;
31 static int test_interpret_matches(const xmlChar *input_string,
32 isds_error expected_error,
33 const xmlChar *expected_string,
34 const struct isds_list *expected_starts,
35 const struct isds_list *expected_ends,
36 struct isds_list **starts,
37 struct isds_list **ends) {
38 isds_error err;
39 xmlChar *string = NULL;
41 if (NULL != input_string) {
42 string = xmlStrdup(input_string);
43 if (NULL == string)
44 ABORT_UNIT("Could not duplicate a string");
45 TEST_DESTRUCTOR(xmlFree, string);
48 err = interpret_matches(string, starts, ends);
50 if (expected_error != err)
51 FAIL_TEST("interpret_matches() returned wrong error code: "
52 "expected=%s, got=%s",
53 isds_strerror(expected_error), isds_strerror(err));
54 if (err != IE_SUCCESS)
55 PASS_TEST;
57 TEST_STRING_DUPLICITY((const char *)expected_string, (const char *)string);
58 if (NULL != starts) {
59 if (compare_lists("start", expected_string, expected_starts,
60 string, *starts))
61 return 1;
63 if (NULL != ends) {
64 if (compare_lists("end", expected_string, expected_ends,
65 string, *ends))
66 return 1;
69 PASS_TEST;
73 #define START_MARKER "|$*HL_START*$|"
74 #define END_MARKER "|$*HL_END*$|"
76 int main(void) {
77 xmlChar *input, *expected_output;
78 struct isds_list expected_starts = { .data = NULL, .next = NULL };
79 struct isds_list expected_ends = { .data = NULL, .next = NULL };
80 struct isds_list expected_starts2 = { .data = NULL, .next = NULL };
81 struct isds_list expected_ends2 = { .data = NULL, .next = NULL };
82 struct isds_list *starts = NULL, *ends = NULL;
83 INIT_TEST("interpret_matches string conversion");
85 if (isds_init())
86 ABORT_UNIT("isds_init() failed\n");
88 TEST("NULL start", test_interpret_matches, BAD_CAST NULL,
89 IE_INVAL, BAD_CAST NULL, NULL, NULL, NULL, &ends);
90 TEST("NULL ends", test_interpret_matches, BAD_CAST NULL,
91 IE_INVAL, BAD_CAST NULL, NULL, NULL, &starts, NULL);
92 TEST("NULL string", test_interpret_matches, BAD_CAST NULL,
93 IE_SUCCESS, BAD_CAST NULL, NULL, NULL, &starts, &ends);
94 TEST("Empty string", test_interpret_matches, BAD_CAST "",
95 IE_SUCCESS, BAD_CAST "", NULL, NULL, &starts, &ends);
97 /* No matches */
98 input = BAD_CAST "foo";
99 TEST((const char *)input, test_interpret_matches, input,
100 IE_SUCCESS, input, NULL, NULL, &starts, &ends);
102 /* One match is the whole empty string */
103 input = BAD_CAST START_MARKER END_MARKER;
104 expected_output = BAD_CAST "";
105 expected_starts.data = expected_output;
106 expected_ends.data = expected_output;
107 TEST((const char *)input, test_interpret_matches, input,
108 IE_SUCCESS, expected_output,
109 &expected_starts, &expected_ends, &starts, &ends);
111 /* One match is the whole string */
112 input = BAD_CAST START_MARKER "MATCH" END_MARKER;
113 expected_output = BAD_CAST "MATCH";
114 expected_starts.data = expected_output;
115 expected_ends.data = expected_output + 5;
116 TEST((const char *)input, test_interpret_matches, input,
117 IE_SUCCESS, expected_output,
118 &expected_starts, &expected_ends, &starts, &ends);
120 /* One match in the beginning */
121 input = BAD_CAST START_MARKER "MATCH" END_MARKER "after";
122 expected_output = BAD_CAST "MATCH" "after";
123 expected_starts.data = expected_output;
124 expected_ends.data = expected_output + 5;
125 TEST((const char *)input, test_interpret_matches, input,
126 IE_SUCCESS, expected_output,
127 &expected_starts, &expected_ends, &starts, &ends);
129 /* One match at the end */
130 input = BAD_CAST "before" START_MARKER "MATCH" END_MARKER;
131 expected_output = BAD_CAST "before" "MATCH";
132 expected_starts.data = expected_output + 6;
133 expected_ends.data = expected_output + 6+ 5;
134 TEST((const char *)input, test_interpret_matches, input,
135 IE_SUCCESS, expected_output,
136 &expected_starts, &expected_ends, &starts, &ends);
138 /* One empty match in the middle */
139 input = BAD_CAST "before" START_MARKER END_MARKER "after";
140 expected_output = BAD_CAST "before" "after";
141 expected_starts.data = expected_output + 6;
142 expected_ends.data = expected_output + 6;
143 TEST((const char *)input, test_interpret_matches, input,
144 IE_SUCCESS, expected_output,
145 &expected_starts, &expected_ends, &starts, &ends);
147 /* One non-empty match in the middle */
148 input = BAD_CAST "before" START_MARKER "MATCH" END_MARKER "after";
149 expected_output = BAD_CAST "before" "MATCH" "after";
150 expected_starts.data = expected_output + 6;
151 expected_ends.data = expected_output + 6 + 5;
152 TEST((const char *)input, test_interpret_matches, input,
153 IE_SUCCESS, expected_output,
154 &expected_starts, &expected_ends, &starts, &ends);
156 /* Only a start marker. This is ill but still acceptable by the
157 * specification. */
158 input = BAD_CAST "before" START_MARKER "MATCH";
159 expected_output = BAD_CAST "before" "MATCH";
160 expected_starts.data = expected_output + 6;
161 TEST((const char *)input, test_interpret_matches, input,
162 IE_SUCCESS, expected_output,
163 &expected_starts, NULL, &starts, &ends);
165 /* Only an end marker. This is ill but still acceptable by the
166 * specification. */
167 input = BAD_CAST "MATCH" END_MARKER "after";
168 expected_output = BAD_CAST "MATCH" "after";
169 expected_ends.data = expected_output + 5;
170 TEST((const char *)input, test_interpret_matches, input,
171 IE_SUCCESS, expected_output,
172 NULL, &expected_ends, &starts, &ends);
174 /* Two matches in the middle */
175 input = BAD_CAST "before" START_MARKER "AMATCH1" END_MARKER "mid"
176 START_MARKER "AMATCH02" END_MARKER "after";
177 expected_output = BAD_CAST "before" "AMATCH1" "mid" "AMATCH02" "after";
178 expected_starts.data = expected_output + 6;
179 expected_starts.next = &expected_starts2;
180 expected_ends.data = expected_output + 6 + 7;
181 expected_ends.next = &expected_ends2;
182 expected_starts2.data = expected_output + 6 + 7 + 3;
183 expected_ends2.data = expected_output + 6 + 7 + 3 + 8;
184 TEST((const char *)input, test_interpret_matches, input,
185 IE_SUCCESS, expected_output,
186 &expected_starts, &expected_ends, &starts, &ends);
188 isds_list_free(&starts);
189 isds_list_free(&ends);
190 isds_cleanup();
191 SUM_TEST();