6 #include <mono/metadata/appdomain.h>
7 #include <mono/metadata/assembly.h>
8 #include <mono/metadata/debug-helpers.h>
9 #include <mono/metadata/object.h>
10 #include <mono/jit/jit.h>
11 #include <mono/utils/mono-logger.h>
13 extern void* mono_aot_module_mscorlib_info
;
14 extern void* mono_aot_module_System_Core_info
;
15 extern void* mono_aot_module_System_info
;
16 extern void* mono_aot_module_Mono_Posix_info
;
17 extern void* mono_aot_module_System_Configuration_info
;
18 extern void* mono_aot_module_System_Security_info
;
19 extern void* mono_aot_module_System_Xml_info
;
20 /* extern void* mono_aot_module_System_Threading_info; */
21 extern void* mono_aot_module_Mono_Security_info
;
22 extern void* mono_aot_module_Mono_Simd_info
;
23 extern void* mono_aot_module_TestDriver_info
;
25 extern void* mono_aot_module_basic_info
;
26 extern void* mono_aot_module_basic_float_info
;
27 extern void* mono_aot_module_basic_long_info
;
28 extern void* mono_aot_module_basic_calls_info
;
29 extern void* mono_aot_module_basic_simd_info
;
30 extern void* mono_aot_module_objects_info
;
31 extern void* mono_aot_module_arrays_info
;
32 extern void* mono_aot_module_basic_math_info
;
33 extern void* mono_aot_module_exceptions_info
;
34 extern void* mono_aot_module_devirtualization_info
;
35 extern void* mono_aot_module_generics_info
;
36 extern void* mono_aot_module_generics_variant_types_info
;
37 extern void* mono_aot_module_basic_simd_info
;
38 extern void* mono_aot_module_gc_stress_info
;
39 extern void* mono_aot_module_imt_big_iface_test_info
;
40 extern void* mono_aot_module_make_imt_test_info
;
41 /* extern void* mono_aot_module_thread_stress_info; */
42 extern void* mono_aot_module_iltests_info
;
44 extern void mono_aot_register_module(void *aot_info
);
45 extern void mono_aot_init(void);
46 extern void mono_jit_set_aot_only(mono_bool aot_only
);
47 extern MonoDomain
* mini_init (const char *filename
, const char *runtime_version
);
49 int run_all_test_methods(MonoClass
*klass
) {
51 MonoMethod
*mm
= NULL
;
54 printf("Running test methods without reflection\n");
55 while (NULL
!= (mm
= mono_class_get_methods(klass
, &iter
))) {
57 const char *name
= mono_method_get_name(mm
);
59 if (strncmp(name
, "test_", 5)) continue;
60 printf("=== Test %d, method %s\n", count
, mono_method_get_name(mm
));
61 expected_result
= strtol(name
+ 5, &end
, 10);
63 printf(" warning: could not determine expected return value\n");
66 MonoObject
*mo
= mono_runtime_invoke(mm
, NULL
, NULL
, NULL
);
67 int *ret
= mono_object_unbox(mo
);
68 if (ret
&& *ret
== expected_result
) {
72 printf(" FAILED, expected %d, returned %p, %d\n", expected_result
, ret
,
73 ret
!= NULL
? *ret
: 0);
78 printf("============================================\n");
79 printf("Final count: %d tests, %d pass, %.2f%%\n", count
, passed
,
80 (double)passed
/ count
* 100.0);
82 printf("no test methods found.\n");
87 #if defined(__native_client__)
88 extern void* mono_aot_module_nacl_info
;
89 extern char* nacl_mono_path
;
90 char *load_corlib_data() {
92 static char *corlib_data
= NULL
;
93 if (corlib_data
) return corlib_data
;
95 mscorlib
= fopen("mscorlib.dll", "r");
96 if (NULL
!= mscorlib
) {
99 if (0 == stat("mscorlib.dll", &st
)) {
101 printf("reading mscorlib.dll, size %ld\n", size
);
102 corlib_data
= malloc(size
);
103 if (corlib_data
!= NULL
) {
104 while (fread(corlib_data
, 1, size
, mscorlib
) != 0) ;
105 if (!ferror(mscorlib
)) {
106 mono_set_corlib_data(corlib_data
, size
);
108 perror("error reading mscorlib.dll");
113 perror("Could not allocate memory");
116 perror("stat error");
124 /* Initialize Mono. Must run only once per process */
125 MonoDomain
*init_mono(char *mname
) {
126 MonoDomain
*domain
= NULL
;
128 mono_jit_set_aot_only(1);
129 mono_aot_register_module(mono_aot_module_mscorlib_info
);
130 mono_aot_register_module(mono_aot_module_TestDriver_info
);
131 mono_aot_register_module(mono_aot_module_System_Core_info
);
132 mono_aot_register_module(mono_aot_module_System_info
);
133 mono_aot_register_module(mono_aot_module_Mono_Posix_info
);
134 mono_aot_register_module(mono_aot_module_System_Configuration_info
);
135 mono_aot_register_module(mono_aot_module_System_Security_info
);
136 mono_aot_register_module(mono_aot_module_System_Xml_info
);
137 mono_aot_register_module(mono_aot_module_Mono_Security_info
);
138 /* mono_aot_register_module(mono_aot_module_System_Threading_info); */
139 mono_aot_register_module(mono_aot_module_Mono_Simd_info
);
141 mono_aot_register_module(mono_aot_module_basic_info
);
142 mono_aot_register_module(mono_aot_module_basic_float_info
);
143 mono_aot_register_module(mono_aot_module_basic_long_info
);
144 mono_aot_register_module(mono_aot_module_basic_calls_info
);
145 mono_aot_register_module(mono_aot_module_basic_simd_info
);
146 mono_aot_register_module(mono_aot_module_objects_info
);
147 mono_aot_register_module(mono_aot_module_arrays_info
);
148 mono_aot_register_module(mono_aot_module_basic_math_info
);
149 mono_aot_register_module(mono_aot_module_exceptions_info
);
150 mono_aot_register_module(mono_aot_module_devirtualization_info
);
151 mono_aot_register_module(mono_aot_module_generics_info
);
152 mono_aot_register_module(mono_aot_module_generics_variant_types_info
);
153 mono_aot_register_module(mono_aot_module_gc_stress_info
);
154 mono_aot_register_module(mono_aot_module_imt_big_iface_test_info
);
155 mono_aot_register_module(mono_aot_module_iltests_info
);
157 /* mono_aot_register_module(mono_aot_module_make_imt_test_info); */
158 /* mono_aot_register_module(mono_aot_module_thread_stress_info); */
159 #if defined(__native_client__)
161 mono_aot_register_module(mono_aot_module_nacl_info
);
164 /* Test file-less shortcut for loading mscorlib metadata */
166 nacl_mono_path
= strdup(".");
168 /* Uncomment the following if something is going wrong */
169 /* mono_trace_set_level_string("info"); */
170 domain
= mono_jit_init(mname
);
171 if (NULL
== domain
) {
172 printf("ERROR: mono_jit_init failure\n");
178 /* Run all tests from one assembly file */
179 int try_one(char *mname
, MonoDomain
*domain
) {
186 MonoString
*monostring_arg
;
187 MonoArray
*arg_array
;
188 int *failures
= NULL
;
189 const int kUseTestDriver
= 1;
192 char *cstr_arg
= "--timing";
194 ma
= mono_domain_assembly_open(domain
, mname
);
196 printf("ERROR: could not open mono assembly\n");
200 mi
= mono_assembly_get_image(ma
);
202 printf("ERROR: could not get assembly image\n");
206 monostring_arg
= mono_string_new(domain
, cstr_arg
);
207 mc
= mono_class_from_name(mono_get_corlib(), "System", "String");
209 printf("ERROR: could not mono string class\n");
213 // to pass a string argument, change the 0 to a 1 and uncomment
214 // mono_array_setref below
215 arg_array
= mono_array_new(domain
, mc
, 0);
216 //mono_array_setref(arg_array, 0, monostring_arg);
219 if (!kUseTestDriver
) {
220 mc
= mono_class_from_name(mi
, "", "Tests");
222 printf("could not open Tests class\n");
225 test_count
= run_all_test_methods(mc
);
227 /* If run_all_test_methods didn't find any tests, try Main */
228 if (kUseTestDriver
|| test_count
== 0) {
229 mmd
= mono_method_desc_new("Tests:Main()", 1);
230 mm
= mono_method_desc_search_in_image(mmd
, mi
);
232 mmd
= mono_method_desc_new("Tests:Main(string[])", 1);
233 mm
= mono_method_desc_search_in_image(mmd
, mi
);
235 printf("Couldn't find Tests:Main() or Tests:Main(string[])\n");
240 mo
= mono_runtime_invoke(mm
, NULL
, args
, NULL
);
241 failures
= mo
!= NULL
? mono_object_unbox(mo
) : NULL
;
242 if (NULL
== failures
|| *failures
!= 0) {
243 printf("--------------------> Failed");
246 return failures
!= NULL
? failures
: 1;
249 int main(int argc
, char *argv
[]) {
254 printf("no test specified; running basic.exe\n");
255 printf("================================\n");
256 domain
= init_mono("basic.exe");
257 try_one("basic.exe", domain
);
259 domain
= init_mono(argv
[1]);
261 for (i
= 1; i
< argc
; i
++) {
262 printf("\nRunning tests from %s:\n", argv
[i
]);
263 printf("===============================\n\n");
264 failures
+= try_one(argv
[i
], domain
);
267 mono_jit_cleanup(domain
);