Collect all of the various spread out 3rd party licenses to a single file.
[SquirrelJME.git] / nanocoat / tests / testDescFieldType.c
blob72312804fba01e7053d27b86ad91180ed39035e1
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"
18 /** The max permitted array size. */
19 #define MAX_ARRAY 8
21 /**
22 * Represents a component entry.
24 * @since 2024/02/22
26 typedef struct testDescFieldTypeComponentEntry
28 /** The Java type. */
29 sjme_javaTypeId javaType;
31 /** The cell count of this field. */
32 sjme_jint cells;
34 /** Is this an array? */
35 sjme_jboolean isArray;
36 } testDescFieldTypeComponentEntry;
38 /**
39 * A single entry that is compared against for the test, since there are
40 * many different possible scenarios to be tested, this reduces duplicate
41 * test code.
43 * @since 2024/02/14
45 typedef struct testDescFieldTypeEntry
47 /** The string for the entry. */
48 sjme_lpcstr string;
50 /** Interpretation of object type, if an object. */
51 sjme_lpcstr objectString;
53 /** The number of dimensions. */
54 sjme_jint numDims;
56 /** Component data. */
57 testDescFieldTypeComponentEntry components[MAX_ARRAY];
58 } testDescFieldTypeEntry;
60 /** Entries for tests. */
61 static const testDescFieldTypeEntry testEntries[] =
63 /* Non-array. */
65 "B",
66 NULL,
70 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE,
72 SJME_JNI_FALSE
77 "C",
78 NULL,
82 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR,
84 SJME_JNI_FALSE,
89 "D",
90 NULL,
94 SJME_JAVA_TYPE_ID_DOUBLE,
96 SJME_JNI_FALSE,
101 "F",
102 NULL,
106 SJME_JAVA_TYPE_ID_FLOAT,
108 SJME_JNI_FALSE,
113 "I",
114 NULL,
118 SJME_JAVA_TYPE_ID_INTEGER,
120 SJME_JNI_FALSE,
125 "J",
126 NULL,
130 SJME_JAVA_TYPE_ID_LONG,
132 SJME_JNI_FALSE,
137 "S",
138 NULL,
142 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR,
144 SJME_JNI_FALSE,
149 "Z",
150 NULL,
154 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE,
156 SJME_JNI_FALSE,
161 "LSqueak/In/Box;",
162 "Squeak/In/Box",
166 SJME_JAVA_TYPE_ID_OBJECT,
168 SJME_JNI_FALSE,
173 /* Array. */
175 "[B",
176 NULL,
180 SJME_JAVA_TYPE_ID_OBJECT,
182 SJME_JNI_TRUE,
185 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE,
187 SJME_JNI_FALSE,
192 "[C",
193 NULL,
197 SJME_JAVA_TYPE_ID_OBJECT,
199 SJME_JNI_TRUE,
202 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR,
204 SJME_JNI_FALSE,
209 "[D",
210 NULL,
214 SJME_JAVA_TYPE_ID_OBJECT,
216 SJME_JNI_TRUE,
219 SJME_JAVA_TYPE_ID_DOUBLE,
221 SJME_JNI_FALSE,
226 "[F",
227 NULL,
231 SJME_JAVA_TYPE_ID_OBJECT,
233 SJME_JNI_TRUE,
236 SJME_JAVA_TYPE_ID_FLOAT,
238 SJME_JNI_FALSE,
243 "[I",
244 NULL,
248 SJME_JAVA_TYPE_ID_OBJECT,
250 SJME_JNI_TRUE,
253 SJME_JAVA_TYPE_ID_INTEGER,
255 SJME_JNI_FALSE,
260 "[J",
261 NULL,
265 SJME_JAVA_TYPE_ID_OBJECT,
267 SJME_JNI_TRUE,
270 SJME_JAVA_TYPE_ID_LONG,
272 SJME_JNI_FALSE,
277 "[S",
278 NULL,
282 SJME_JAVA_TYPE_ID_OBJECT,
284 SJME_JNI_TRUE,
287 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR,
289 SJME_JNI_FALSE,
294 "[Z",
295 NULL,
299 SJME_JAVA_TYPE_ID_OBJECT,
301 SJME_JNI_TRUE,
304 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE,
306 SJME_JNI_FALSE,
311 "[LSqueak/In/Box;",
312 "Squeak/In/Box",
316 SJME_JAVA_TYPE_ID_OBJECT,
318 SJME_JNI_TRUE,
321 SJME_JAVA_TYPE_ID_OBJECT,
323 SJME_JNI_FALSE,
328 "[[I",
329 NULL,
333 SJME_JAVA_TYPE_ID_OBJECT,
335 SJME_JNI_TRUE,
338 SJME_JAVA_TYPE_ID_OBJECT,
340 SJME_JNI_TRUE,
343 SJME_JAVA_TYPE_ID_INTEGER,
345 SJME_JNI_FALSE,
350 "[[LSqueak/In/Box;",
351 "Squeak/In/Box",
355 SJME_JAVA_TYPE_ID_OBJECT,
357 SJME_JNI_TRUE,
360 SJME_JAVA_TYPE_ID_OBJECT,
362 SJME_JNI_TRUE,
365 SJME_JAVA_TYPE_ID_OBJECT,
367 SJME_JNI_FALSE,
372 /* End. */
373 {NULL}
376 /** String pair. */
377 #define pair(s) s, strlen(s)
380 * Tests parsing of class field descriptors.
382 * @since 2024/01/01
384 SJME_TEST_DECLARE(testDescFieldType)
386 const testDescFieldTypeEntry* entry;
387 const sjme_desc_fieldType* result;
388 const sjme_desc_binaryName* objectResult;
389 sjme_lpcstr string;
390 sjme_jint strLen, strHash, atEntry, i;
391 sjme_errorCode error;
393 /* Go through every entry. */
394 for (atEntry = 0; testEntries[atEntry].string != NULL;
395 atEntry++)
397 /* Get the entry to test. */
398 entry = &testEntries[atEntry];
400 /* Load in string details. */
401 string = entry->string;
402 strLen = strlen(string);
403 strHash = sjme_string_hash(string);
405 /* Parse the field type. */
406 result = NULL;
407 if (sjme_error_is(error = sjme_desc_interpretFieldType(
408 test->pool, &result, string, strLen)) ||
409 result == NULL)
410 return sjme_unit_fail(test, "Could not interpret %s (%d)?",
411 string, error);
413 /* Basic whole value and hash check. */
414 sjme_unit_equalI(test, result->hash, strHash,
415 "Hash of whole %s incorrect?", string);
416 sjme_unit_equalI(test, result->whole.length, strLen,
417 "Length of whole %s incorrect?", string);
418 sjme_unit_equalP(test, result->whole.pointer, string,
419 "Pointer of whole %s incorrect?", string);
421 /* Should have same number of dimensions. */
422 sjme_unit_equalI(test, result->numDims, entry->numDims,
423 "Field %s has incorrect number of dimensions?", string);
425 /* Match all components. */
426 for (i = 0; i <= entry->numDims; i++)
428 /* Debug. */
429 sjme_message("Field %s, fragment %d: %.*s",
430 string, i, result->components[i].fragment.length,
431 (char*)result->components[i].fragment.pointer);
433 /* Basic comparison. */
434 sjme_unit_equalI(test, result->components[i].javaType,
435 entry->components[i].javaType,
436 "Field %s has incorrect Java type?", string);
437 sjme_unit_equalI(test, result->components[i].cells,
438 entry->components[i].cells,
439 "Field %s has incorrect number of cells?", string);
440 sjme_unit_equalZ(test, result->components[i].isArray,
441 entry->components[i].isArray,
442 "Field %s has incorrect array state?", string);
444 /* The last entry will have the object string, if an object. */
445 if (i == entry->numDims && entry->objectString != NULL)
447 /* Should be an object type. */
448 sjme_unit_equalI(test, SJME_JAVA_TYPE_ID_OBJECT,
449 result->components[i].javaType,
450 "Not an object?");
452 /* String comparison should be valid. */
453 sjme_unit_equalI(test, 0, sjme_desc_compareBinaryNamePS(
454 &result->components[i].binaryName,
455 entry->objectString),
456 "Binary name of object is incorrect?");
458 /* Parse it. */
459 objectResult = NULL;
460 if (sjme_error_is(sjme_desc_interpretBinaryName(
461 test->pool,
462 &objectResult, entry->objectString,
463 strlen(entry->objectString))) ||
464 objectResult == NULL)
465 return sjme_unit_fail(test,
466 "Field %s has an invalid object string?", string);
468 /* Should be the same. */
469 sjme_unit_equalI(test, 0, sjme_desc_compareBinaryNameP(
470 &result->components[i].binaryName,
471 objectResult),
472 "Field %s has non-equal object?", string);
477 /* Invalid fields. */
478 result = NULL;
479 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
480 sjme_desc_interpretFieldType(test->pool,
481 &result, pair("")),
482 "Blank is valid?");
484 result = NULL;
485 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
486 sjme_desc_interpretFieldType(test->pool,
487 &result, pair("X")),
488 "Unknown type specifier is valid?");
490 result = NULL;
491 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
492 sjme_desc_interpretFieldType(test->pool,
493 &result, pair("ZI")),
494 "Two primitive types is valid?");
496 result = NULL;
497 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
498 sjme_desc_interpretFieldType(test->pool,
499 &result, pair("[")),
500 "Blank array is valid?");
502 result = NULL;
503 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
504 sjme_desc_interpretFieldType(test->pool,
505 &result, pair("[[")),
506 "Blank double array is valid?");
508 result = NULL;
509 sjme_unit_equalI(test, SJME_ERROR_INVALID_BINARY_NAME,
510 sjme_desc_interpretFieldType(test->pool,
511 &result, pair("L")),
512 "Only starting L is valid?");
514 result = NULL;
515 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
516 sjme_desc_interpretFieldType(test->pool,
517 &result, pair("LOops")),
518 "Object missing ending semicolon is valid?");
520 result = NULL;
521 sjme_unit_equalI(test, SJME_ERROR_INVALID_BINARY_NAME,
522 sjme_desc_interpretFieldType(test->pool,
523 &result, pair("L;")),
524 "Empty but specified object is valid?");
526 /* Success! */
527 return SJME_TEST_RESULT_PASS;