Compile fixes.
[SquirrelJME.git] / nanocoat / tests / testDescFieldType.c
blobeac0b03decbd57b682639bfbc4d00519abf671f0
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 /** The max permitted array size. */
20 #define MAX_ARRAY 8
22 /**
23 * Represents a component entry.
25 * @since 2024/02/22
27 typedef struct testDescFieldTypeComponentEntry
29 /** The Java type. */
30 sjme_javaTypeId javaType;
32 /** The cell count of this field. */
33 sjme_jint cells;
35 /** Is this an array? */
36 sjme_jboolean isArray;
37 } testDescFieldTypeComponentEntry;
39 /**
40 * A single entry that is compared against for the test, since there are
41 * many different possible scenarios to be tested, this reduces duplicate
42 * test code.
44 * @since 2024/02/14
46 typedef struct testDescFieldTypeEntry
48 /** The string for the entry. */
49 sjme_lpcstr string;
51 /** Interpretation of object type, if an object. */
52 sjme_lpcstr objectString;
54 /** The number of dimensions. */
55 sjme_jint numDims;
57 /** Component data. */
58 testDescFieldTypeComponentEntry components[MAX_ARRAY];
59 } testDescFieldTypeEntry;
61 /** Entries for tests. */
62 static const testDescFieldTypeEntry testEntries[] =
64 /* Non-array. */
66 "B",
67 NULL,
71 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE,
73 SJME_JNI_FALSE
78 "C",
79 NULL,
83 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR,
85 SJME_JNI_FALSE,
90 "D",
91 NULL,
95 SJME_JAVA_TYPE_ID_DOUBLE,
97 SJME_JNI_FALSE,
102 "F",
103 NULL,
107 SJME_JAVA_TYPE_ID_FLOAT,
109 SJME_JNI_FALSE,
114 "I",
115 NULL,
119 SJME_JAVA_TYPE_ID_INTEGER,
121 SJME_JNI_FALSE,
126 "J",
127 NULL,
131 SJME_JAVA_TYPE_ID_LONG,
133 SJME_JNI_FALSE,
138 "S",
139 NULL,
143 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR,
145 SJME_JNI_FALSE,
150 "Z",
151 NULL,
155 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE,
157 SJME_JNI_FALSE,
162 "LSqueak/In/Box;",
163 "Squeak/In/Box",
167 SJME_JAVA_TYPE_ID_OBJECT,
169 SJME_JNI_FALSE,
174 /* Array. */
176 "[B",
177 NULL,
181 SJME_JAVA_TYPE_ID_OBJECT,
183 SJME_JNI_TRUE,
186 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE,
188 SJME_JNI_FALSE,
193 "[C",
194 NULL,
198 SJME_JAVA_TYPE_ID_OBJECT,
200 SJME_JNI_TRUE,
203 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR,
205 SJME_JNI_FALSE,
210 "[D",
211 NULL,
215 SJME_JAVA_TYPE_ID_OBJECT,
217 SJME_JNI_TRUE,
220 SJME_JAVA_TYPE_ID_DOUBLE,
222 SJME_JNI_FALSE,
227 "[F",
228 NULL,
232 SJME_JAVA_TYPE_ID_OBJECT,
234 SJME_JNI_TRUE,
237 SJME_JAVA_TYPE_ID_FLOAT,
239 SJME_JNI_FALSE,
244 "[I",
245 NULL,
249 SJME_JAVA_TYPE_ID_OBJECT,
251 SJME_JNI_TRUE,
254 SJME_JAVA_TYPE_ID_INTEGER,
256 SJME_JNI_FALSE,
261 "[J",
262 NULL,
266 SJME_JAVA_TYPE_ID_OBJECT,
268 SJME_JNI_TRUE,
271 SJME_JAVA_TYPE_ID_LONG,
273 SJME_JNI_FALSE,
278 "[S",
279 NULL,
283 SJME_JAVA_TYPE_ID_OBJECT,
285 SJME_JNI_TRUE,
288 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR,
290 SJME_JNI_FALSE,
295 "[Z",
296 NULL,
300 SJME_JAVA_TYPE_ID_OBJECT,
302 SJME_JNI_TRUE,
305 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE,
307 SJME_JNI_FALSE,
312 "[LSqueak/In/Box;",
313 "Squeak/In/Box",
317 SJME_JAVA_TYPE_ID_OBJECT,
319 SJME_JNI_TRUE,
322 SJME_JAVA_TYPE_ID_OBJECT,
324 SJME_JNI_FALSE,
329 "[[I",
330 NULL,
334 SJME_JAVA_TYPE_ID_OBJECT,
336 SJME_JNI_TRUE,
339 SJME_JAVA_TYPE_ID_OBJECT,
341 SJME_JNI_TRUE,
344 SJME_JAVA_TYPE_ID_INTEGER,
346 SJME_JNI_FALSE,
351 "[[LSqueak/In/Box;",
352 "Squeak/In/Box",
356 SJME_JAVA_TYPE_ID_OBJECT,
358 SJME_JNI_TRUE,
361 SJME_JAVA_TYPE_ID_OBJECT,
363 SJME_JNI_TRUE,
366 SJME_JAVA_TYPE_ID_OBJECT,
368 SJME_JNI_FALSE,
373 /* End. */
374 {NULL}
377 /** String pair. */
378 #define pair(s) s, strlen(s)
381 * Tests parsing of class field descriptors.
383 * @since 2024/01/01
385 SJME_TEST_DECLARE(testDescFieldType)
387 const testDescFieldTypeEntry* entry;
388 sjme_desc_fieldType* result;
389 sjme_desc_binaryName* objectResult;
390 sjme_lpcstr string;
391 sjme_jint strLen, strHash, atEntry, i;
392 sjme_errorCode error;
394 /* Go through every entry. */
395 for (atEntry = 0; testEntries[atEntry].string != NULL;
396 atEntry++)
398 /* Get the entry to test. */
399 entry = &testEntries[atEntry];
401 /* Load in string details. */
402 string = entry->string;
403 strLen = strlen(string);
404 strHash = sjme_string_hash(string);
406 /* Parse the field type. */
407 result = NULL;
408 if (sjme_error_is(error = sjme_desc_interpretFieldType(
409 test->pool, &result, string, strLen)) ||
410 result == NULL)
411 return sjme_unit_fail(test, "Could not interpret %s (%d)?",
412 string, error);
414 /* Basic whole value and hash check. */
415 sjme_unit_equalI(test, result->hash, strHash,
416 "Hash of whole %s incorrect?", string);
417 sjme_unit_equalI(test, result->whole.length, strLen,
418 "Length of whole %s incorrect?", string);
419 sjme_unit_equalP(test, result->whole.pointer, string,
420 "Pointer of whole %s incorrect?", string);
422 /* Should have same number of dimensions. */
423 sjme_unit_equalI(test, result->numDims, entry->numDims,
424 "Field %s has incorrect number of dimensions?", string);
426 /* Match all components. */
427 for (i = 0; i <= entry->numDims; i++)
429 /* Debug. */
430 sjme_message("Field %s, fragment %d: %.*s",
431 string, i, result->components[i].fragment.length,
432 (char*)result->components[i].fragment.pointer);
434 /* Basic comparison. */
435 sjme_unit_equalI(test, result->components[i].javaType,
436 entry->components[i].javaType,
437 "Field %s has incorrect Java type?", string);
438 sjme_unit_equalI(test, result->components[i].cells,
439 entry->components[i].cells,
440 "Field %s has incorrect number of cells?", string);
441 sjme_unit_equalZ(test, result->components[i].isArray,
442 entry->components[i].isArray,
443 "Field %s has incorrect array state?", string);
445 /* The last entry will have the object string, if an object. */
446 if (i == entry->numDims && entry->objectString != NULL)
448 /* Should be an object type. */
449 sjme_unit_equalI(test, SJME_JAVA_TYPE_ID_OBJECT,
450 result->components[i].javaType,
451 "Not an object?");
453 /* String comparison should be valid. */
454 sjme_unit_equalI(test, 0, sjme_desc_compareBinaryNamePS(
455 &result->components[i].binaryName,
456 entry->objectString),
457 "Binary name of object is incorrect?");
459 /* Parse it. */
460 objectResult = NULL;
461 if (sjme_error_is(sjme_desc_interpretBinaryName(
462 test->pool,
463 &objectResult, entry->objectString,
464 strlen(entry->objectString))) ||
465 objectResult == NULL)
466 return sjme_unit_fail(test,
467 "Field %s has an invalid object string?", string);
469 /* Should be the same. */
470 sjme_unit_equalI(test, 0, sjme_desc_compareBinaryNameP(
471 &result->components[i].binaryName,
472 objectResult),
473 "Field %s has non-equal object?", string);
478 /* Invalid fields. */
479 result = NULL;
480 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
481 sjme_desc_interpretFieldType(test->pool,
482 &result, pair("")),
483 "Blank is valid?");
485 result = NULL;
486 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
487 sjme_desc_interpretFieldType(test->pool,
488 &result, pair("X")),
489 "Unknown type specifier is valid?");
491 result = NULL;
492 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
493 sjme_desc_interpretFieldType(test->pool,
494 &result, pair("ZI")),
495 "Two primitive types is valid?");
497 result = NULL;
498 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
499 sjme_desc_interpretFieldType(test->pool,
500 &result, pair("[")),
501 "Blank array is valid?");
503 result = NULL;
504 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
505 sjme_desc_interpretFieldType(test->pool,
506 &result, pair("[[")),
507 "Blank double array is valid?");
509 result = NULL;
510 sjme_unit_equalI(test, SJME_ERROR_INVALID_BINARY_NAME,
511 sjme_desc_interpretFieldType(test->pool,
512 &result, pair("L")),
513 "Only starting L is valid?");
515 result = NULL;
516 sjme_unit_equalI(test, SJME_ERROR_INVALID_FIELD_TYPE,
517 sjme_desc_interpretFieldType(test->pool,
518 &result, pair("LOops")),
519 "Object missing ending semicolon is valid?");
521 result = NULL;
522 sjme_unit_equalI(test, SJME_ERROR_INVALID_BINARY_NAME,
523 sjme_desc_interpretFieldType(test->pool,
524 &result, pair("L;")),
525 "Empty but specified object is valid?");
527 /* Success! */
528 return SJME_TEST_RESULT_PASS;