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/classy.h"
18 #include "classes.zip.h"
20 typedef sjme_errorCode (*testRunFunc
)(sjme_test
* test
,
21 sjme_class_info info
);
23 typedef struct testClassInfo
27 sjme_lpcstr binaryName
;
32 #define TRY_CLASS(what, useFunc) \
34 SJME_TOKEN_STRING(what) ".class", \
35 "java/lang/" SJME_TOKEN_STRING(what), \
39 static const testClassInfo testClassInfos
[] =
41 TRY_CLASS(Appendable
, NULL
),
42 TRY_CLASS(ArithmeticException
, NULL
),
43 TRY_CLASS(ArrayIndexOutOfBoundsException
, NULL
),
44 TRY_CLASS(ArrayStoreException
, NULL
),
45 TRY_CLASS(AssertionError
, NULL
),
46 TRY_CLASS(AutoCloseable
, NULL
),
47 TRY_CLASS(Boolean
, NULL
),
48 TRY_CLASS(Byte
, NULL
),
49 TRY_CLASS(__CanSetPrintStream__
, NULL
),
50 TRY_CLASS(Character
, NULL
),
51 TRY_CLASS(CharSequence
, NULL
),
52 TRY_CLASS(ClassCastException
, NULL
),
53 TRY_CLASS(Class
, NULL
),
54 TRY_CLASS(ClassFormatError
, NULL
),
55 TRY_CLASS(ClassNotFoundException
, NULL
),
56 TRY_CLASS(Cloneable
, NULL
),
57 TRY_CLASS(CloneNotSupportedException
, NULL
),
58 TRY_CLASS(Comparable
, NULL
),
59 TRY_CLASS(Deprecated
, NULL
),
60 TRY_CLASS(Double
, NULL
),
61 TRY_CLASS(Enum
, NULL
),
62 TRY_CLASS(Error
, NULL
),
63 TRY_CLASS(Exception
, NULL
),
64 TRY_CLASS(Float
, NULL
),
65 TRY_CLASS(FunctionalInterface
, NULL
),
66 TRY_CLASS(IllegalAccessException
, NULL
),
67 TRY_CLASS(IllegalArgumentException
, NULL
),
68 TRY_CLASS(IllegalMonitorStateException
, NULL
),
69 TRY_CLASS(IllegalStateException
, NULL
),
70 TRY_CLASS(IllegalThreadStateException
, NULL
),
71 TRY_CLASS(IncompatibleClassChangeError
, NULL
),
72 TRY_CLASS(IndexOutOfBoundsException
, NULL
),
73 TRY_CLASS(InstantiationException
, NULL
),
74 TRY_CLASS(Integer
, NULL
),
75 TRY_CLASS(__InternMini__$__InternMicro__
, NULL
),
76 TRY_CLASS(__InternMini__
, NULL
),
77 TRY_CLASS(InterruptedException
, NULL
),
78 TRY_CLASS(Iterable
, NULL
),
79 TRY_CLASS(LinkageError
, NULL
),
80 TRY_CLASS(Long
, NULL
),
81 TRY_CLASS(Math
, NULL
),
82 TRY_CLASS(NegativeArraySizeException
, NULL
),
83 TRY_CLASS(NoClassDefFoundError
, NULL
),
84 TRY_CLASS(NoSuchFieldError
, NULL
),
85 TRY_CLASS(NullPointerException
, NULL
),
86 TRY_CLASS(Number
, NULL
),
87 TRY_CLASS(NumberFormatException
, NULL
),
88 TRY_CLASS(Object
, NULL
),
89 TRY_CLASS(OutOfMemoryError
, NULL
),
90 TRY_CLASS(Override
, NULL
),
91 TRY_CLASS(Runnable
, NULL
),
92 TRY_CLASS(Runtime
, NULL
),
93 TRY_CLASS(RuntimeException
, NULL
),
94 TRY_CLASS(RuntimePermission
, NULL
),
95 TRY_CLASS(SecurityException
, NULL
),
96 TRY_CLASS(SecurityManager
, NULL
),
97 TRY_CLASS(Short
, NULL
),
98 TRY_CLASS(StackOverflowError
, NULL
),
99 TRY_CLASS(__Start__
, NULL
),
100 TRY_CLASS(StringBuffer
, NULL
),
101 TRY_CLASS(StringBuilder
, NULL
),
102 TRY_CLASS(String
, NULL
),
103 TRY_CLASS(StringIndexOutOfBoundsException
, NULL
),
104 TRY_CLASS(SuppressWarnings
, NULL
),
105 TRY_CLASS(System
, NULL
),
106 TRY_CLASS(Thread
, NULL
),
107 TRY_CLASS(Throwable
, NULL
),
108 TRY_CLASS(UnsupportedClassVersionError
, NULL
),
109 TRY_CLASS(UnsupportedOperationException
, NULL
),
110 TRY_CLASS(VirtualMachineError
, NULL
),
115 * Tests parsing of classes from a set of sample classes.
119 SJME_TEST_DECLARE(testClassParse
)
121 const testClassInfo
* testInfo
;
123 sjme_zip_entry zipEntry
;
124 sjme_stream_input in
;
125 sjme_class_info info
;
126 sjme_stringPool stringPool
;
128 /* Setup string pool. */
130 if (sjme_error_is(test
->error
= sjme_stringPool_new(
131 test
->pool
, &stringPool
)) || stringPool
== NULL
)
132 return sjme_unit_fail(test
, "Could not create string pool.");
134 /* Load the Zip that is full of classes. */
136 if (sjme_error_is(test
->error
= sjme_zip_openMemory(
138 (sjme_pointer
)&classes_zip__bin
[0],
139 classes_zip__len
)) || zip
== NULL
)
140 return sjme_unit_fail(test
, "Could not open Zip.");
142 /* Go through and load every single class. */
143 for (testInfo
= &testClassInfos
[0]; testInfo
->binaryName
!= NULL
;
147 sjme_message(">>> %s", testInfo
->fileName
);
150 memset(&zipEntry
, 0, sizeof(zipEntry
));
151 if (sjme_error_is(test
->error
= sjme_zip_locateEntry(
152 zip
, &zipEntry
, testInfo
->fileName
)))
153 return sjme_unit_fail(test
, "Could not locate %s",
158 if (sjme_error_is(test
->error
= sjme_zip_entryRead(
159 &zipEntry
, &in
)) || in
== NULL
)
160 return sjme_unit_fail(test
, "Could not open %s",
163 /* Load the class. */
165 if (sjme_error_is(test
->error
= sjme_class_parse(test
->pool
,
166 in
, stringPool
, &info
)) ||
168 return sjme_unit_fail(test
, "Could not parse %s: %d",
169 testInfo
->fileName
, test
->error
);
171 /* Run individual test on it? */
172 if (testInfo
->testRun
!= NULL
)
173 if (sjme_error_is(test
->error
= testInfo
->testRun(
175 return sjme_unit_fail(test
, "Class test unit failed %s: %d",
176 testInfo
->fileName
, test
->error
);
178 /* Close the class. */
179 if (sjme_error_is(test
->error
= sjme_closeable_close(
180 SJME_AS_CLOSEABLE(info
))))
181 return sjme_unit_fail(test
, "Could not close class.");
183 /* Close the entry. */
184 if (sjme_error_is(test
->error
= sjme_closeable_close(
185 SJME_AS_CLOSEABLE(in
))))
186 return sjme_unit_fail(test
, "Could not close entry.");
190 if (sjme_error_is(test
->error
= sjme_closeable_close(
191 SJME_AS_CLOSEABLE(zip
))))
192 return sjme_unit_fail(test
, "Could not close Zip.");
194 /* Close the string pool. */
195 if (sjme_error_is(test
->error
= sjme_closeable_close(
196 SJME_AS_CLOSEABLE(stringPool
))))
197 return sjme_unit_fail(test
, "Could not close string pool.");
200 return SJME_TEST_RESULT_PASS
;