Compile fixes.
[SquirrelJME.git] / nanocoat / tests / testDescMethodType.c
blob8ed1bb220a38762d1cd8b1aa7efeda9d48f4a3ef
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include <string.h>
12 #include "mock.h"
13 #include "proto.h"
14 #include "sjme/util.h"
15 #include "test.h"
16 #include "unit.h"
17 #include "sjme/nvm/descriptor.h"
19 /**
20 * What to test and the expected values for method descriptors.
22 * @since 2024/02/18
24 typedef struct testDescMethodTypeEntry
26 /** The string for the entry. */
27 sjme_lpcstr string;
29 /** Return value cells. */
30 sjme_jint returnCells;
32 /** Argument cells. */
33 sjme_jint argCells;
35 /** The field descriptors used, index zero is the return value. */
36 sjme_lpcstr fields;
37 } testDescMethodTypeEntry;
39 /** Entries for tests. */
40 static const testDescMethodTypeEntry testEntries[] =
42 /* Void returning void. */
44 "()V",
47 "V\0\0"
50 /* Void returning boolean. */
52 "()Z",
55 "Z\0\0"
58 /* Void returning byte. */
60 "()B",
63 "B\0\0"
66 /* Void returning short. */
68 "()S",
71 "S\0\0"
74 /* Void returning char. */
76 "()C",
79 "C\0\0"
82 /* Void returning int. */
84 "()I",
87 "I\0\0"
90 /* Void returning long. */
92 "()J",
95 "J\0\0"
98 /* Void returning float. */
100 "()F",
103 "F\0\0"
106 /* Void returning double. */
108 "()D",
111 "D\0\0"
114 /* Void returning object. */
116 "()LSqueak/In/Box;",
119 "LSqueak/In/Box;\0\0"
122 /* Boolean returning void. */
124 "(Z)V",
127 "V\0Z\0\0"
130 /* Byte returning void. */
132 "(B)V",
135 "V\0B\0\0"
138 /* Short returning void. */
140 "(S)V",
143 "V\0S\0\0"
146 /* Char returning void. */
148 "(C)V",
151 "V\0C\0\0"
154 /* Int returning void. */
156 "(I)V",
159 "V\0I\0\0"
162 /* Long returning void. */
164 "(J)V",
167 "V\0J\0\0"
170 /* Float returning void. */
172 "(F)V",
175 "V\0F\0\0"
178 /* Double returning void. */
180 "(D)V",
183 "V\0D\0\0"
186 /* Object returning void. */
188 "(LSqueak/In/Box;)V",
191 "V\0LSqueak/In/Box;\0\0"
194 /* Long long returning object. */
196 "(JJ)LSqueak/In/Box;",
199 "LSqueak/In/Box;\0J\0J\0\0"
202 /* Object returning long. */
204 "(LSqueak/In/Box;)J",
207 "J\0LSqueak/In/Box;\0\0"
210 /* Int long returning double. */
212 "(IJ)D",
215 "D\0I\0J\0\0"
218 /* Long int returning double. */
220 "(JI)D",
223 "D\0J\0I\0\0"
226 /* End. */
227 {NULL},
230 /** String pair. */
231 #define pair(s) s, strlen(s)
234 * Tests parsing of method descriptors.
236 * @since 2024/01/01
238 SJME_TEST_DECLARE(testDescMethodType)
240 const testDescMethodTypeEntry* entry;
241 sjme_desc_methodType* result;
242 sjme_desc_fieldType* field;
243 sjme_list_sjme_lpcstr* fieldStrings;
244 sjme_lpcstr string, subString;
245 sjme_jint strLen, subStrLen, strHash, atEntry, i;
247 /* Go through every entry. */
248 for (atEntry = 0; testEntries[atEntry].string != NULL;
249 atEntry++)
251 /* Get the entry to test. */
252 entry = &testEntries[atEntry];
254 /* Load in string details. */
255 string = entry->string;
256 strLen = strlen(string);
257 strHash = sjme_string_hash(string);
259 /* Interpret method entry. */
260 result = NULL;
261 if (sjme_error_is(sjme_desc_interpretMethodType(test->pool,
262 &result, string, strLen)) || result == NULL)
263 return sjme_unit_fail(test, "Could not interpret %s?", string);
265 /* Basic whole value and hash check. */
266 sjme_unit_equalI(test, result->hash, strHash,
267 "Hash of whole %s incorrect?", string);
268 sjme_unit_equalI(test, result->whole.length, strLen,
269 "Length of whole %s incorrect?", string);
270 sjme_unit_equalP(test, result->whole.pointer, string,
271 "Pointer of whole %s incorrect?", string);
273 /* Cells should match. */
274 sjme_unit_equalI(test, result->returnCells, entry->returnCells,
275 "Return cells of %s incorrect?", string);
276 sjme_unit_equalI(test, result->argCells, entry->argCells,
277 "Argument cells of %s incorrect?", string);
279 /* Parse recorded fields. */
280 fieldStrings = NULL;
281 if (sjme_error_is(sjme_list_flattenArgNul(test->pool,
282 &fieldStrings, entry->fields) ||
283 fieldStrings == NULL))
284 return sjme_unit_fail(test, "Could not parse fields of %s?",
285 string);
287 /* Count should match. */
288 sjme_unit_equalI(test, fieldStrings->length, result->fields.length,
289 "Incorrect field count for %s?", string);
291 /* Match each individual field. */
292 for (i = 0; i < fieldStrings->length; i++)
294 /* Get string information. */
295 subString = fieldStrings->elements[i];
296 subStrLen = strlen(subString);
298 /* Parse */
299 field = NULL;
300 if (sjme_error_is(sjme_desc_interpretFieldType(
301 test->pool, &field, subString,
302 subStrLen)) || field == NULL)
303 return sjme_unit_fail(test, "Could not parse field %s in %s?",
304 subString, string);
306 /* Should be the same field. */
307 sjme_unit_equalI(test, 0, sjme_desc_compareFieldC(
308 &result->fields.elements[i], field),
309 "Decoded field %s is incorrect in %s (%.*s == %s)?",
310 subString, string,
311 result->fields.elements[i].fragment.length,
312 result->fields.elements[i].fragment.pointer, field);
316 /* Invalid methods types. */
317 result = NULL;
318 sjme_unit_equalI(test, SJME_ERROR_INVALID_METHOD_TYPE,
319 sjme_desc_interpretMethodType(test->pool,
320 &result, pair("")),
321 "Blank is valid?");
323 result = NULL;
324 sjme_unit_equalI(test, SJME_ERROR_INVALID_METHOD_TYPE,
325 sjme_desc_interpretMethodType(test->pool,
326 &result, pair("V")),
327 "Return only is valid?");
329 result = NULL;
330 sjme_unit_equalI(test, SJME_ERROR_INVALID_METHOD_TYPE,
331 sjme_desc_interpretMethodType(test->pool,
332 &result, pair("()")),
333 "Arguments only is valid?");
335 result = NULL;
336 sjme_unit_equalI(test, SJME_ERROR_INVALID_METHOD_TYPE,
337 sjme_desc_interpretMethodType(test->pool,
338 &result, pair("()II")),
339 "Double return type is valid?");
341 result = NULL;
342 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
343 sjme_desc_interpretMethodType(test->pool,
344 &result, pair("()[")),
345 "Unspecified array return is valid?");
347 result = NULL;
348 sjme_unit_equalI(test, SJME_ERROR_INVALID_BINARY_NAME,
349 sjme_desc_interpretMethodType(test->pool,
350 &result, pair("()L")),
351 "Unspecified object return is valid?");
353 result = NULL;
354 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
355 sjme_desc_interpretMethodType(test->pool,
356 &result, pair("()LOops")),
357 "Unclosed object return is valid?");
359 result = NULL;
360 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
361 sjme_desc_interpretMethodType(test->pool,
362 &result, pair("([)V")),
363 "Unspecified array is valid?");
365 result = NULL;
366 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
367 sjme_desc_interpretMethodType(test->pool,
368 &result, pair("(L)V")),
369 "Unspecified object is valid?");
371 result = NULL;
372 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
373 sjme_desc_interpretMethodType(test->pool,
374 &result, pair("(LOops)V")),
375 "Unclosed object is valid?");
377 result = NULL;
378 sjme_unit_equalI(test, SJME_ERROR_INVALID_METHOD_TYPE,
379 sjme_desc_interpretMethodType(test->pool,
380 &result, pair("V()")),
381 "Wrong order is valid?");
383 /* Success! */
384 return SJME_TEST_RESULT_PASS;