1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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 // -------------------------------------------------------------------------*/
14 #include "sjme/util.h"
17 #include "sjme/nvm/descriptor.h"
19 /** The max permitted array size. */
23 * Represents a component entry.
27 typedef struct testDescFieldTypeComponentEntry
30 sjme_javaTypeId javaType
;
32 /** The cell count of this field. */
35 /** Is this an array? */
36 sjme_jboolean isArray
;
37 } testDescFieldTypeComponentEntry
;
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
46 typedef struct testDescFieldTypeEntry
48 /** The string for the entry. */
51 /** Interpretation of object type, if an object. */
52 sjme_lpcstr objectString
;
54 /** The number of dimensions. */
57 /** Component data. */
58 testDescFieldTypeComponentEntry components
[MAX_ARRAY
];
59 } testDescFieldTypeEntry
;
61 /** Entries for tests. */
62 static const testDescFieldTypeEntry testEntries
[] =
71 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
,
83 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR
,
95 SJME_JAVA_TYPE_ID_DOUBLE
,
107 SJME_JAVA_TYPE_ID_FLOAT
,
119 SJME_JAVA_TYPE_ID_INTEGER
,
131 SJME_JAVA_TYPE_ID_LONG
,
143 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR
,
155 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
,
167 SJME_JAVA_TYPE_ID_OBJECT
,
181 SJME_JAVA_TYPE_ID_OBJECT
,
186 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
,
198 SJME_JAVA_TYPE_ID_OBJECT
,
203 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR
,
215 SJME_JAVA_TYPE_ID_OBJECT
,
220 SJME_JAVA_TYPE_ID_DOUBLE
,
232 SJME_JAVA_TYPE_ID_OBJECT
,
237 SJME_JAVA_TYPE_ID_FLOAT
,
249 SJME_JAVA_TYPE_ID_OBJECT
,
254 SJME_JAVA_TYPE_ID_INTEGER
,
266 SJME_JAVA_TYPE_ID_OBJECT
,
271 SJME_JAVA_TYPE_ID_LONG
,
283 SJME_JAVA_TYPE_ID_OBJECT
,
288 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR
,
300 SJME_JAVA_TYPE_ID_OBJECT
,
305 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
,
317 SJME_JAVA_TYPE_ID_OBJECT
,
322 SJME_JAVA_TYPE_ID_OBJECT
,
334 SJME_JAVA_TYPE_ID_OBJECT
,
339 SJME_JAVA_TYPE_ID_OBJECT
,
344 SJME_JAVA_TYPE_ID_INTEGER
,
356 SJME_JAVA_TYPE_ID_OBJECT
,
361 SJME_JAVA_TYPE_ID_OBJECT
,
366 SJME_JAVA_TYPE_ID_OBJECT
,
378 #define pair(s) s, strlen(s)
381 * Tests parsing of class field descriptors.
385 SJME_TEST_DECLARE(testDescFieldType
)
387 const testDescFieldTypeEntry
* entry
;
388 sjme_desc_fieldType
* result
;
389 sjme_desc_binaryName
* objectResult
;
391 sjme_jint strLen
, strHash
, atEntry
, i
;
392 sjme_errorCode error
;
394 /* Go through every entry. */
395 for (atEntry
= 0; testEntries
[atEntry
].string
!= NULL
;
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. */
408 if (sjme_error_is(error
= sjme_desc_interpretFieldType(
409 test
->pool
, &result
, string
, strLen
)) ||
411 return sjme_unit_fail(test
, "Could not interpret %s (%d)?",
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
++)
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
,
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?");
461 if (sjme_error_is(sjme_desc_interpretBinaryName(
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
,
473 "Field %s has non-equal object?", string
);
478 /* Invalid fields. */
480 sjme_unit_equalI(test
, SJME_ERROR_INVALID_FIELD_TYPE
,
481 sjme_desc_interpretFieldType(test
->pool
,
486 sjme_unit_equalI(test
, SJME_ERROR_INVALID_FIELD_TYPE
,
487 sjme_desc_interpretFieldType(test
->pool
,
489 "Unknown type specifier is valid?");
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?");
498 sjme_unit_equalI(test
, SJME_ERROR_INVALID_FIELD_TYPE
,
499 sjme_desc_interpretFieldType(test
->pool
,
501 "Blank array is valid?");
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?");
510 sjme_unit_equalI(test
, SJME_ERROR_INVALID_BINARY_NAME
,
511 sjme_desc_interpretFieldType(test
->pool
,
513 "Only starting L is valid?");
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?");
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?");
528 return SJME_TEST_RESULT_PASS
;