Adjust sourcize.c to use _snprintf if using MSVC.
[SquirrelJME.git] / nanocoat / tests / testDescIdentifier.c
blobafb8653094ed5668146e4c0b7c2a1e62775fb83b
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 "sjme/nvm/descriptor.h"
11 #include <string.h>
13 #include "mock.h"
14 #include "proto.h"
15 #include "sjme/util.h"
16 #include "test.h"
17 #include "unit.h"
19 #define pair(s) s, strlen(s)
21 /**
22 * Tests parsing of identifiers.
24 * @since 2024/02/04
26 SJME_TEST_DECLARE(testDescIdentifier)
28 sjme_lpcstr string;
29 sjme_desc_identifier result;
30 sjme_jint stringHash;
31 sjme_stringPool stringPool;
33 /* Determine the hash of the string. */
34 string = "squirrel";
35 stringHash = sjme_string_hash(string);
37 stringPool = NULL;
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,
72 stringPool, &result,
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,
79 stringPool, &result,
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,
86 stringPool, &result,
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,
93 stringPool, &result,
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,
100 stringPool, &result,
101 pair("")),
102 "Blank is valid?");
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?");
109 /* Success! */
110 return SJME_TEST_RESULT_PASS;