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 // -------------------------------------------------------------------------*/
10 #include "sjme/nvm/descriptor.h"
15 #include "sjme/util.h"
19 #define pair(s) s, strlen(s)
22 * Tests parsing of identifiers.
26 SJME_TEST_DECLARE(testDescIdentifier
)
29 sjme_desc_identifier result
;
31 sjme_stringPool stringPool
;
33 /* Determine the hash of the string. */
35 stringHash
= sjme_string_hash(string
);
38 if (sjme_error_is(test
->error
= sjme_stringPool_new(
39 test
->pool
, &stringPool
)) || stringPool
== NULL
)
40 return sjme_unit_fail(test
, "Could not create string pool?");
42 /* Valid identifier. */
43 if (sjme_error_is(test
->error
= sjme_desc_interpretIdentifier(
44 test
->pool
, stringPool
, &result
,
45 string
, strlen(string
))))
46 return sjme_unit_fail(test
, "Could not interpret identifier?");
48 /* Make sure it was calculated correctly. */
49 sjme_unit_equalI(test
, result
->hash
, stringHash
,
50 "Hash set incorrectly?");
51 sjme_unit_notEqualP(test
, result
->whole
, NULL
,
52 "Pointer not valid?");
53 sjme_unit_equalI(test
,
54 0, sjme_string_compareN(
55 string
, strlen(string
),
56 (sjme_lpcstr
)result
->whole
->chars
,
57 result
->whole
->length
),
58 "Incorrect string stored?");
59 sjme_unit_equalI(test
,
60 0, sjme_desc_compareIdentifierS(result
, "squirrel"),
61 "Identifier does not match?");
63 /* It must be able to be closed. */
64 if (sjme_error_is(test
->error
= sjme_closeable_close(
65 SJME_AS_CLOSEABLE(result
))))
66 return sjme_unit_fail(test
, "Could not close identifier?");
68 /* All of these are not valid. */
69 memset(&result
, 0, sizeof(result
));
70 sjme_unit_equalI(test
, SJME_ERROR_INVALID_IDENTIFIER
,
71 sjme_desc_interpretIdentifier(test
->pool
,
73 pair("squirrel.squirrel")),
74 "Name with '.' is valid?");
76 memset(&result
, 0, sizeof(result
));
77 sjme_unit_equalI(test
, SJME_ERROR_INVALID_IDENTIFIER
,
78 sjme_desc_interpretIdentifier(test
->pool
,
80 pair("squirrel;squirrel")),
81 "Name with ';' is valid?");
83 memset(&result
, 0, sizeof(result
));
84 sjme_unit_equalI(test
, SJME_ERROR_INVALID_IDENTIFIER
,
85 sjme_desc_interpretIdentifier(test
->pool
,
87 pair("squirrel[squirrel")),
88 "Name with '[' is valid?");
90 memset(&result
, 0, sizeof(result
));
91 sjme_unit_equalI(test
, SJME_ERROR_INVALID_IDENTIFIER
,
92 sjme_desc_interpretIdentifier(test
->pool
,
94 pair("squirrel/squirrel")),
95 "Name with '/' is valid?");
97 memset(&result
, 0, sizeof(result
));
98 sjme_unit_equalI(test
, SJME_ERROR_INVALID_IDENTIFIER
,
99 sjme_desc_interpretIdentifier(test
->pool
,
104 /* Close string pool. */
105 if (sjme_error_is(test
->error
= sjme_closeable_close(
106 SJME_AS_CLOSEABLE(stringPool
))))
107 return sjme_unit_fail(test
, "Could not close pool?");
110 return SJME_TEST_RESULT_PASS
;