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"
20 * What to test and the expected values for method descriptors.
24 typedef struct testDescMethodTypeEntry
26 /** The string for the entry. */
29 /** Return value cells. */
30 sjme_jint returnCells
;
32 /** Argument cells. */
35 /** The field descriptors used, index zero is the return value. */
37 } testDescMethodTypeEntry
;
39 /** Entries for tests. */
40 static const testDescMethodTypeEntry testEntries
[] =
42 /* Void returning void. */
50 /* Void returning boolean. */
58 /* Void returning byte. */
66 /* Void returning short. */
74 /* Void returning char. */
82 /* Void returning int. */
90 /* Void returning long. */
98 /* Void returning float. */
106 /* Void returning double. */
114 /* Void returning object. */
119 "LSqueak/In/Box;\0\0"
122 /* Boolean returning void. */
130 /* Byte returning void. */
138 /* Short returning void. */
146 /* Char returning void. */
154 /* Int returning void. */
162 /* Long returning void. */
170 /* Float returning void. */
178 /* Double returning void. */
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. */
218 /* Long int returning double. */
231 #define pair(s) s, strlen(s)
234 * Tests parsing of method descriptors.
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
;
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. */
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. */
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?",
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
);
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?",
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)?",
311 result
->fields
.elements
[i
].fragment
.length
,
312 result
->fields
.elements
[i
].fragment
.pointer
, field
);
316 /* Invalid methods types. */
318 sjme_unit_equalI(test
, SJME_ERROR_INVALID_METHOD_TYPE
,
319 sjme_desc_interpretMethodType(test
->pool
,
324 sjme_unit_equalI(test
, SJME_ERROR_INVALID_METHOD_TYPE
,
325 sjme_desc_interpretMethodType(test
->pool
,
327 "Return only is valid?");
330 sjme_unit_equalI(test
, SJME_ERROR_INVALID_METHOD_TYPE
,
331 sjme_desc_interpretMethodType(test
->pool
,
332 &result
, pair("()")),
333 "Arguments only is valid?");
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?");
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?");
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?");
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?");
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?");
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?");
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?");
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?");
384 return SJME_TEST_RESULT_PASS
;