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"
18 /** The max permitted array size. */
22 * Represents a component entry.
26 typedef struct testDescFieldTypeComponentEntry
29 sjme_javaTypeId javaType
;
31 /** The cell count of this field. */
34 /** Is this an array? */
35 sjme_jboolean isArray
;
36 } testDescFieldTypeComponentEntry
;
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
45 typedef struct testDescFieldTypeEntry
47 /** The string for the entry. */
50 /** Interpretation of object type, if an object. */
51 sjme_lpcstr objectString
;
53 /** The number of dimensions. */
56 /** Component data. */
57 testDescFieldTypeComponentEntry components
[MAX_ARRAY
];
58 } testDescFieldTypeEntry
;
60 /** Entries for tests. */
61 static const testDescFieldTypeEntry testEntries
[] =
70 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
,
82 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR
,
94 SJME_JAVA_TYPE_ID_DOUBLE
,
106 SJME_JAVA_TYPE_ID_FLOAT
,
118 SJME_JAVA_TYPE_ID_INTEGER
,
130 SJME_JAVA_TYPE_ID_LONG
,
142 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR
,
154 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
,
166 SJME_JAVA_TYPE_ID_OBJECT
,
180 SJME_JAVA_TYPE_ID_OBJECT
,
185 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
,
197 SJME_JAVA_TYPE_ID_OBJECT
,
202 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR
,
214 SJME_JAVA_TYPE_ID_OBJECT
,
219 SJME_JAVA_TYPE_ID_DOUBLE
,
231 SJME_JAVA_TYPE_ID_OBJECT
,
236 SJME_JAVA_TYPE_ID_FLOAT
,
248 SJME_JAVA_TYPE_ID_OBJECT
,
253 SJME_JAVA_TYPE_ID_INTEGER
,
265 SJME_JAVA_TYPE_ID_OBJECT
,
270 SJME_JAVA_TYPE_ID_LONG
,
282 SJME_JAVA_TYPE_ID_OBJECT
,
287 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR
,
299 SJME_JAVA_TYPE_ID_OBJECT
,
304 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
,
316 SJME_JAVA_TYPE_ID_OBJECT
,
321 SJME_JAVA_TYPE_ID_OBJECT
,
333 SJME_JAVA_TYPE_ID_OBJECT
,
338 SJME_JAVA_TYPE_ID_OBJECT
,
343 SJME_JAVA_TYPE_ID_INTEGER
,
355 SJME_JAVA_TYPE_ID_OBJECT
,
360 SJME_JAVA_TYPE_ID_OBJECT
,
365 SJME_JAVA_TYPE_ID_OBJECT
,
377 #define pair(s) s, strlen(s)
380 * Tests parsing of class field descriptors.
384 SJME_TEST_DECLARE(testDescFieldType
)
386 const testDescFieldTypeEntry
* entry
;
387 const sjme_desc_fieldType
* result
;
388 const sjme_desc_binaryName
* objectResult
;
390 sjme_jint strLen
, strHash
, atEntry
, i
;
391 sjme_errorCode error
;
393 /* Go through every entry. */
394 for (atEntry
= 0; testEntries
[atEntry
].string
!= NULL
;
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. */
407 if (sjme_error_is(error
= sjme_desc_interpretFieldType(
408 test
->pool
, &result
, string
, strLen
)) ||
410 return sjme_unit_fail(test
, "Could not interpret %s (%d)?",
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
++)
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
,
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?");
460 if (sjme_error_is(sjme_desc_interpretBinaryName(
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
,
472 "Field %s has non-equal object?", string
);
477 /* Invalid fields. */
479 sjme_unit_equalI(test
, SJME_ERROR_INVALID_FIELD_TYPE
,
480 sjme_desc_interpretFieldType(test
->pool
,
485 sjme_unit_equalI(test
, SJME_ERROR_INVALID_FIELD_TYPE
,
486 sjme_desc_interpretFieldType(test
->pool
,
488 "Unknown type specifier is valid?");
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?");
497 sjme_unit_equalI(test
, SJME_ERROR_INVALID_FIELD_TYPE
,
498 sjme_desc_interpretFieldType(test
->pool
,
500 "Blank array is valid?");
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?");
509 sjme_unit_equalI(test
, SJME_ERROR_INVALID_BINARY_NAME
,
510 sjme_desc_interpretFieldType(test
->pool
,
512 "Only starting L is valid?");
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?");
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?");
527 return SJME_TEST_RESULT_PASS
;