Debian Codegraph @

engine/read_only/tests/simple/marpa_m_test.c

raw
1 /*
2 * Copyright 2022 Jeffrey Kegler
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included
11 * in all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 * OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /* Libmarpa method test interface -- marpa_m_test */
23
24 #include "marpa_m_test.h"
25
26 Marpa_Symbol_ID S_invalid = -1, S_no_such = 42;
27 Marpa_Rule_ID R_invalid = -1, R_no_such = 42;
28
29 struct error_description_s
30 {
31 int error_code;
32 const char *name;
33 const char *suggested;
34 };
35
36 /* This is hard-copied in. It really should be generated from
37 * the Libmarpa source files to catch new error codes.
38 */
39 static const struct error_description_s error_description[] = {
40 {0, "MARPA_ERR_NONE", "No error"},
41 {1, "MARPA_ERR_AHFA_IX_NEGATIVE", "MARPA_ERR_AHFA_IX_NEGATIVE"},
42 {2, "MARPA_ERR_AHFA_IX_OOB", "MARPA_ERR_AHFA_IX_OOB"},
43 {3, "MARPA_ERR_ANDID_NEGATIVE", "MARPA_ERR_ANDID_NEGATIVE"},
44 {4, "MARPA_ERR_ANDID_NOT_IN_OR", "MARPA_ERR_ANDID_NOT_IN_OR"},
45 {5, "MARPA_ERR_ANDIX_NEGATIVE", "MARPA_ERR_ANDIX_NEGATIVE"},
46 {6, "MARPA_ERR_BAD_SEPARATOR", "Separator has invalid symbol ID"},
47 {7, "MARPA_ERR_BOCAGE_ITERATION_EXHAUSTED",
48 "MARPA_ERR_BOCAGE_ITERATION_EXHAUSTED"},
49 {8, "MARPA_ERR_COUNTED_NULLABLE",
50 "Nullable symbol on RHS of a sequence rule"},
51 {9, "MARPA_ERR_DEVELOPMENT", "Development error, see string"},
52 {10, "MARPA_ERR_DUPLICATE_AND_NODE", "MARPA_ERR_DUPLICATE_AND_NODE"},
53 {11, "MARPA_ERR_DUPLICATE_RULE", "Duplicate rule"},
54 {12, "MARPA_ERR_DUPLICATE_TOKEN", "Duplicate token"},
55 {13, "MARPA_ERR_YIM_COUNT", "Maximum number of Earley items exceeded"},
56 {14, "MARPA_ERR_YIM_ID_INVALID", "MARPA_ERR_YIM_ID_INVALID"},
57 {15, "MARPA_ERR_EVENT_IX_NEGATIVE", "Negative event index"},
58 {16, "MARPA_ERR_EVENT_IX_OOB", "No event at that index"},
59 {17, "MARPA_ERR_GRAMMAR_HAS_CYCLE", "Grammar has cycle"},
60 {18, "MARPA_ERR_INACCESSIBLE_TOKEN", "Token symbol is inaccessible"},
61 {19, "MARPA_ERR_INTERNAL", "MARPA_ERR_INTERNAL"},
62 {20, "MARPA_ERR_INVALID_AHFA_ID", "MARPA_ERR_INVALID_AHFA_ID"},
63 {21, "MARPA_ERR_INVALID_AIMID", "MARPA_ERR_INVALID_AIMID"},
64 {22, "MARPA_ERR_INVALID_BOOLEAN", "Argument is not boolean"},
65 {23, "MARPA_ERR_INVALID_IRLID", "MARPA_ERR_INVALID_IRLID"},
66 {24, "MARPA_ERR_INVALID_NSYID", "MARPA_ERR_INVALID_NSYID"},
67 {25, "MARPA_ERR_INVALID_LOCATION", "Location is not valid"},
68 {26, "MARPA_ERR_INVALID_RULE_ID", "Rule ID is malformed"},
69 {27, "MARPA_ERR_INVALID_START_SYMBOL",
70 "Specified start symbol is not valid"},
71 {28, "MARPA_ERR_INVALID_SYMBOL_ID", "Symbol ID is malformed"},
72 {29, "MARPA_ERR_I_AM_NOT_OK", "Marpa is in a not OK state"},
73 {30, "MARPA_ERR_MAJOR_VERSION_MISMATCH",
74 "Libmarpa major version number is a mismatch"},
75 {31, "MARPA_ERR_MICRO_VERSION_MISMATCH",
76 "Libmarpa micro version number is a mismatch"},
77 {32, "MARPA_ERR_MINOR_VERSION_MISMATCH",
78 "Libmarpa minor version number is a mismatch"},
79 {33, "MARPA_ERR_NOOKID_NEGATIVE", "MARPA_ERR_NOOKID_NEGATIVE"},
80 {34, "MARPA_ERR_NOT_PRECOMPUTED", "This grammar is not precomputed"},
81 {35, "MARPA_ERR_NOT_TRACING_COMPLETION_LINKS",
82 "MARPA_ERR_NOT_TRACING_COMPLETION_LINKS"},
83 {36, "MARPA_ERR_NOT_TRACING_LEO_LINKS",
84 "MARPA_ERR_NOT_TRACING_LEO_LINKS"},
85 {37, "MARPA_ERR_NOT_TRACING_TOKEN_LINKS",
86 "MARPA_ERR_NOT_TRACING_TOKEN_LINKS"},
87 {38, "MARPA_ERR_NO_AND_NODES", "MARPA_ERR_NO_AND_NODES"},
88 {39, "MARPA_ERR_NO_EARLEY_SET_AT_LOCATION",
89 "Earley set ID is after latest Earley set"},
90 {40, "MARPA_ERR_NO_OR_NODES", "MARPA_ERR_NO_OR_NODES"},
91 {41, "MARPA_ERR_NO_PARSE", "No parse"},
92 {42, "MARPA_ERR_NO_RULES", "This grammar does not have any rules"},
93 {43, "MARPA_ERR_NO_START_SYMBOL", "This grammar has no start symbol"},
94 {44, "MARPA_ERR_NO_TOKEN_EXPECTED_HERE",
95 "No token is expected at this earleme location"},
96 {45, "MARPA_ERR_NO_TRACE_YIM", "MARPA_ERR_NO_TRACE_YIM"},
97 {46, "MARPA_ERR_NO_TRACE_YS", "MARPA_ERR_NO_TRACE_YS"},
98 {47, "MARPA_ERR_NO_TRACE_PIM", "MARPA_ERR_NO_TRACE_PIM"},
99 {48, "MARPA_ERR_NO_TRACE_SRCL", "MARPA_ERR_NO_TRACE_SRCL"},
100 {49, "MARPA_ERR_NULLING_TERMINAL",
101 "A symbol is both terminal and nulling"},
102 {50, "MARPA_ERR_ORDER_FROZEN", "The ordering is frozen"},
103 {51, "MARPA_ERR_ORID_NEGATIVE", "MARPA_ERR_ORID_NEGATIVE"},
104 {52, "MARPA_ERR_OR_ALREADY_ORDERED", "MARPA_ERR_OR_ALREADY_ORDERED"},
105 {53, "MARPA_ERR_PARSE_EXHAUSTED", "The parse is exhausted"},
106 {54, "MARPA_ERR_PARSE_TOO_LONG",
107 "This input would make the parse too long"},
108 {55, "MARPA_ERR_PIM_IS_NOT_LIM", "MARPA_ERR_PIM_IS_NOT_LIM"},
109 {56, "MARPA_ERR_POINTER_ARG_NULL",
110 "An argument is null when it should not be"},
111 {57, "MARPA_ERR_PRECOMPUTED", "This grammar is precomputed"},
112 {58, "MARPA_ERR_PROGRESS_REPORT_EXHAUSTED",
113 "The progress report is exhausted"},
114 {59, "MARPA_ERR_PROGRESS_REPORT_NOT_STARTED",
115 "No progress report has been started"},
116 {60, "MARPA_ERR_RECCE_NOT_ACCEPTING_INPUT",
117 "The recognizer is not accepting input"},
118 {61, "MARPA_ERR_RECCE_NOT_STARTED",
119 "The recognizer has not been started"},
120 {62, "MARPA_ERR_RECCE_STARTED", "The recognizer has been started"},
121 {63, "MARPA_ERR_RHS_IX_NEGATIVE", "RHS index cannot be negative"},
122 {64, "MARPA_ERR_RHS_IX_OOB",
123 "RHS index must be less than rule length"},
124 {65, "MARPA_ERR_RHS_TOO_LONG", "The RHS is too long"},
125 {66, "MARPA_ERR_SEQUENCE_LHS_NOT_UNIQUE",
126 "LHS of sequence rule would not be unique"},
127 {67, "MARPA_ERR_SOURCE_TYPE_IS_AMBIGUOUS",
128 "MARPA_ERR_SOURCE_TYPE_IS_AMBIGUOUS"},
129 {68, "MARPA_ERR_SOURCE_TYPE_IS_COMPLETION",
130 "MARPA_ERR_SOURCE_TYPE_IS_COMPLETION"},
131 {69, "MARPA_ERR_SOURCE_TYPE_IS_LEO", "MARPA_ERR_SOURCE_TYPE_IS_LEO"},
132 {70, "MARPA_ERR_SOURCE_TYPE_IS_NONE", "MARPA_ERR_SOURCE_TYPE_IS_NONE"},
133 {71, "MARPA_ERR_SOURCE_TYPE_IS_TOKEN",
134 "MARPA_ERR_SOURCE_TYPE_IS_TOKEN"},
135 {72, "MARPA_ERR_SOURCE_TYPE_IS_UNKNOWN",
136 "MARPA_ERR_SOURCE_TYPE_IS_UNKNOWN"},
137 {73, "MARPA_ERR_START_NOT_LHS", "Start symbol not on LHS of any rule"},
138 {74, "MARPA_ERR_SYMBOL_VALUED_CONFLICT",
139 "Symbol is treated both as valued and unvalued"},
140 {75, "MARPA_ERR_TERMINAL_IS_LOCKED",
141 "The terminal status of the symbol is locked"},
142 {76, "MARPA_ERR_TOKEN_IS_NOT_TERMINAL",
143 "Token symbol must be a terminal"},
144 {77, "MARPA_ERR_TOKEN_LENGTH_LE_ZERO",
145 "Token length must greater than zero"},
146 {78, "MARPA_ERR_TOKEN_TOO_LONG", "Token is too long"},
147 {79, "MARPA_ERR_TREE_EXHAUSTED", "Tree iterator is exhausted"},
148 {80, "MARPA_ERR_TREE_PAUSED", "Tree iterator is paused"},
149 {81, "MARPA_ERR_UNEXPECTED_TOKEN_ID", "Unexpected token"},
150 {82, "MARPA_ERR_UNPRODUCTIVE_START", "Unproductive start symbol"},
151 {83, "MARPA_ERR_VALUATOR_INACTIVE", "Valuator inactive"},
152 {84, "MARPA_ERR_VALUED_IS_LOCKED",
153 "The valued status of the symbol is locked"},
154 {85, "MARPA_ERR_RANK_TOO_LOW", "Rule or symbol rank too low"},
155 {86, "MARPA_ERR_RANK_TOO_HIGH", "Rule or symbol rank too high"},
156 {87, "MARPA_ERR_SYMBOL_IS_NULLING", "Symbol is nulling"},
157 {88, "MARPA_ERR_SYMBOL_IS_UNUSED", "Symbol is not used"},
158 {89, "MARPA_ERR_NO_SUCH_RULE_ID", "No rule with this ID exists"},
159 {90, "MARPA_ERR_NO_SUCH_SYMBOL_ID", "No symbol with this ID exists"},
160 {91, "MARPA_ERR_BEFORE_FIRST_TREE",
161 "Tree iterator is before first tree"},
162 {92, "MARPA_ERR_SYMBOL_IS_NOT_COMPLETION_EVENT",
163 "Symbol is not set up for completion events"},
164 {93, "MARPA_ERR_SYMBOL_IS_NOT_NULLED_EVENT",
165 "Symbol is not set up for nulled events"},
166 {94, "MARPA_ERR_SYMBOL_IS_NOT_PREDICTION_EVENT",
167 "Symbol is not set up for prediction events"},
168 {95, "MARPA_ERR_RECCE_IS_INCONSISTENT",
169 "MARPA_ERR_RECCE_IS_INCONSISTENT"},
170 {96, "MARPA_ERR_INVALID_ASSERTION_ID", "Assertion ID is malformed"},
171 {97, "MARPA_ERR_NO_SUCH_ASSERTION_ID",
172 "No assertion with this ID exists"},
173 {98, "MARPA_ERR_HEADERS_DO_NOT_MATCH",
174 "Internal error: Libmarpa was built incorrectly"},
175 {99, "MARPA_ERR_NOT_A_SEQUENCE", "Rule is not a sequence"},
176 };
177
178 const char *
179 marpa_m_error_message (Marpa_Error_Code error_code)
180 {
181 unsigned int i;
182 for (i = 0;
183 i <
184 sizeof (error_description) / sizeof (struct error_description_s);
185 i++)
186 if (error_code == error_description[i].error_code)
187 return error_description[i].suggested;
188 printf ("No message yet for Marpa error code %d %s.\n", error_code,
189 error_description[i].name);
190 exit (1);
191 }
192
193 #define MSG_MAX 120
194 #define MSG_BUFLEN (MSG_MAX+2+1)
195
196 /* [Semicolon] SEParated MesSaGe
197 */
198 static char *
199 sep_msg (char *msg)
200 {
201 static char msg_buffer[MSG_BUFLEN];
202 if (!msg)
203 return (char *) "";
204 if (!*msg)
205 return (char *) "";
206 strcpy (msg_buffer, "; ");
207 strncat (msg_buffer, msg, MSG_MAX);
208 return msg_buffer;
209 }
210
211 /* Report success/failure on "normal" return value.
212 * "Normal" means negative is failure,
213 * non-negative is success
214 */
215 void
216 rv_std_report (API_test_data * td, const char *name, int rv_wanted,
217 Marpa_Error_Code err_wanted)
218 {
219 long rv_seen = td->rv_seen.long_rv;
220 int err_seen = marpa_g_error (td->g, NULL);
221 const char *err_msg = marpa_m_error_message (err_seen);
222 char *msg = sep_msg (td->msg);
223
224 if (rv_seen >= 0) {
225 if (rv_wanted >= 0) {
226 if (rv_wanted == rv_seen) {
227 ok (1, "%s() success as expected%s", name, msg);
228 } else {
229 ok (0,
230 "%s() success as expected but wrong value, wanted = %ld, got %ld; %s",
231 name, (long)rv_wanted, rv_seen, msg);
232 }
233 return;
234 }
235 ok (0, "%s() unexpected success; value wanted = %ld, got %ld; %s",
236 name, (long)rv_wanted, rv_seen, msg);
237 return;
238 }
239 /* If here, the call failed */
240 if (rv_wanted >= 0) {
241 ok (0,
242 "%s() unexpected failure; got return %ld, expected %ld; error = %s%s",
243 name, rv_seen, (long)rv_wanted, err_msg, msg);
244 return;
245 }
246 /* If here, the call failed and was expected to fail */
247 if (rv_wanted == rv_seen) {
248 ok (1, "%s() expected failure; value = %ld as expected; %s", name,
249 rv_seen, msg);
250 } else {
251 ok (0, "%s() expected failure; value wanted = %ld, but got %ld; %s",
252 name, (long)rv_wanted, rv_seen, msg);
253 }
254 if (err_wanted == err_seen) {
255 ok (1, "%s() expected failure; error as expected: '%s'; %s", name,
256 err_msg, msg);
257 } else {
258 const char *wanted_err_msg = marpa_m_error_message (err_wanted);
259 ok (0,
260 "%s() expected failure but unexpected error code: got '%s', expected '%s'; %s",
261 name, err_msg, wanted_err_msg, msg);
262 }
263 }
264
265 /* Report success/failure on "hidden" return value.
266 * "Hidden" means that the return value does not
267 * unambiguously indicate an error, and the error
268 * code must always be consulted as well.
269 */
270 void
271 rv_hidden_report (API_test_data * td, const char *name, int rv_wanted,
272 Marpa_Error_Code err_wanted)
273 {
274 long rv_seen = td->rv_seen.long_rv;
275 int err_seen = marpa_g_error (td->g, NULL);
276 const char *err_msg = marpa_m_error_message (err_seen);
277 char *msg = sep_msg (td->msg);
278 int success = err_seen == MARPA_ERR_NONE;
279 int success_wanted = err_wanted == MARPA_ERR_NONE;
280
281 if (success) {
282 if (success_wanted) {
283 if (rv_wanted == rv_seen) {
284 ok (1, "%s() success as expected%s", name, msg);
285 } else {
286 ok (0,
287 "%s() success as expected, but wrong value, wanted = %ld, got %ld; %s",
288 name, (long)rv_wanted, rv_seen, msg);
289 }
290 return;
291 }
292 ok (0, "%s() unexpected success; value wanted = %ld, got %ld%s",
293 name, (long)rv_wanted, rv_seen, msg);
294 return;
295 }
296 /* If here, the call failed */
297 if (success_wanted) {
298 ok (0,
299 "%s() unexpected failure; got return %ld, expected %ld; error = %s%s",
300 name, rv_seen, (long)rv_wanted, err_msg, msg);
301 ok (0, "%s() unexpected failure; error code: '%s'%s", name,
302 err_msg, msg);
303 return;
304 }
305 /* If here, the call failed and was expected to fail */
306 if (rv_wanted == rv_seen) {
307 ok (1, "%s() expected failure; value = %ld as expected%s", name,
308 rv_seen, msg);
309 } else {
310 ok (0, "%s() expected failure; value wanted = %ld, but got %ld%s",
311 name, (long)rv_wanted, rv_seen, msg);
312 }
313 if (err_wanted == err_seen) {
314 ok (1, "%s() expected failure; error as expected: '%s'%s", name,
315 err_msg, msg);
316 } else {
317 const char *wanted_err_msg = marpa_m_error_message (err_wanted);
318 ok (0,
319 "%s() expected failure but unexpected error code: got '%s', expected '%s'%s",
320 name, err_msg, wanted_err_msg, msg);
321 }
322 }
323
324 /* Report success/failure when the return value is
325 * an error code.
326 */
327 void
328 rv_code_report (API_test_data * td, const char *name,
329 Marpa_Error_Code err_seen, Marpa_Error_Code err_wanted)
330 {
331 const char *err_msg = marpa_m_error_message (err_seen);
332 char *msg = sep_msg (td->msg);
333
334 if (err_seen == MARPA_ERR_NONE) {
335 if (err_wanted == MARPA_ERR_NONE) {
336 ok (1, "%s() success as expected%s", name, msg);
337 } else {
338 ok (0, "%s() unexpected success; wanted error '%s' (%d) %s",
339 name, err_msg, err_seen, msg);
340 }
341 return;
342 }
343 /* If here, the call failed */
344 if (err_wanted == MARPA_ERR_NONE) {
345 ok (0, "%s() unexpected failure; error = %s%s", name,
346 err_msg, msg);
347 return;
348 }
349 /* If here, the call failed and was expected to fail */
350 if (err_wanted == err_seen) {
351 ok (1, "%s() expected failure; error as expected: '%s'%s", name,
352 err_msg, msg);
353 } else {
354 const char *wanted_err_msg = marpa_m_error_message (err_wanted);
355 ok (0,
356 "%s() expected failure but unexpected error code: got '%s', expected '%s'%s",
357 name, err_msg, wanted_err_msg, msg);
358 }
359 }
360
361 /* Report success/failure on pointer return value.
362 * NULL pointer indicates an error.
363 */
364 void
365 rv_ptr_report (API_test_data * td, const char *name,
366 Marpa_Error_Code err_wanted)
367 {
368 void *rv_seen = td->rv_seen.ptr_rv;
369 int err_seen = marpa_g_error (td->g, NULL);
370 const char *err_msg = marpa_m_error_message (err_seen);
371 const char *wanted_err_msg = marpa_m_error_message (err_wanted);
372 char *msg = sep_msg (td->msg);
373 int success = rv_seen ? 1 : 0;
374 int success_wanted = err_wanted == MARPA_ERR_NONE;
375
376 if (success) {
377 if (success_wanted) {
378 ok (1, "%s() success as expected%s", name, msg);
379 return;
380 }
381 ok (0, "%s() unexpected success; error wanted = %s%s", name,
382 wanted_err_msg, msg);
383 return;
384 }
385 /* If here, the call failed */
386 if (success_wanted) {
387 ok (0, "%s() unexpected failure; error = %s%s", name, err_msg,
388 msg);
389 return;
390 }
391 /* If here, the call failed and was expected to fail */
392 ok (1, "%s() expected failure%s", name, msg);
393 if (err_wanted == err_seen) {
394 ok (1, "%s() error as expected: '%s'", name, err_msg);
395 } else {
396 ok (0, "%s() unexpected error code: got '%s', expected '%s'",
397 name, err_msg, wanted_err_msg);
398 }
399 }