blob: 4692f822606224de6da2a478a99d0b93eaf81450 [file] [log] [blame]
buzbeec143c552011-08-20 17:38:58 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "class_linker.h"
4#include "common_test.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07005#include "compiler.h"
buzbee9e0f9b02011-08-24 15:32:46 -07006#include "compiler_test.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07007#include "dex_cache.h"
buzbeec143c552011-08-20 17:38:58 -07008#include "dex_file.h"
9#include "heap.h"
10#include "object.h"
11#include "scoped_ptr.h"
12
13#include <stdint.h>
14#include <stdio.h>
15#include "gtest/gtest.h"
16
17namespace art {
18
19class CompilerTest : public CommonTest {
20};
21
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070022TEST_F(CompilerTest, CompileLibCore) {
23 Compiler compiler;
24 compiler.Compile(boot_class_path_);
25
26 // All libcore references should resolve
27 const DexFile* dex = java_lang_dex_file_.get();
28 DexCache* dex_cache = class_linker_->FindDexCache(*dex);
29 EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings());
30 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
31 String* string = dex_cache->GetResolvedString(i);
32 EXPECT_TRUE(string != NULL);
33 }
34 EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumTypes());
35 for (size_t i = 0; i < dex_cache->NumTypes(); i++) {
36 Class* type = dex_cache->GetResolvedType(i);
37 EXPECT_TRUE(type != NULL);
38 }
39 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumMethods());
40 for (size_t i = 0; i < dex_cache->NumMethods(); i++) {
41 // TODO: ClassLinker::ResolveMethod
42 // Method* method = dex_cache->GetResolvedMethod(i);
43 // EXPECT_TRUE(method != NULL);
44 }
45 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumFields());
46 for (size_t i = 0; i < dex_cache->NumFields(); i++) {
47 // TODO: ClassLinker::ResolveField
48 // Field* field = dex_cache->GetResolvedField(i);
49 // EXPECT_TRUE(field != NULL);
50 }
51
52}
53
54
buzbeec143c552011-08-20 17:38:58 -070055#if defined(__arm__)
56TEST_F(CompilerTest, BasicCodegen) {
57 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kFibonacciDex,
58 "kFibonacciDex"));
59 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
60
61 Thread::Current()->SetClassLoaderOverride(class_loader);
62
63 JNIEnv* env = Thread::Current()->GetJniEnv();
64
65 jclass c = env->FindClass("Fibonacci");
66 ASSERT_TRUE(c != NULL);
67
68 jmethodID m = env->GetStaticMethodID(c, "fibonacci", "(I)I");
69 ASSERT_TRUE(m != NULL);
70
71 jint result = env->CallStaticIntMethod(c, m, 10);
72 LOG(INFO) << "Fibonacci[10] is " << result;
73
74 ASSERT_EQ(55, result);
75}
buzbee3ea4ec52011-08-22 17:37:19 -070076
77TEST_F(CompilerTest, UnopTest) {
78 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
79 "kIntMathDex"));
80 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
81
82 Thread::Current()->SetClassLoaderOverride(class_loader);
83
84 JNIEnv* env = Thread::Current()->GetJniEnv();
85
86 jclass c = env->FindClass("IntMath");
87 ASSERT_TRUE(c != NULL);
88
89 jmethodID m = env->GetStaticMethodID(c, "unopTest", "(I)I");
90 ASSERT_TRUE(m != NULL);
91
92 jint result = env->CallStaticIntMethod(c, m, 38);
93 LOG(INFO) << "unopTest(38) == " << result;
94
95 ASSERT_EQ(37, result);
96}
97
98#if 0 // Does filled array - needs load-time class resolution
99TEST_F(CompilerTest, ShiftTest1) {
100 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
101 "kIntMathDex"));
102 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
103
104 Thread::Current()->SetClassLoaderOverride(class_loader);
105
106 JNIEnv* env = Thread::Current()->GetJniEnv();
107
108 jclass c = env->FindClass("IntMath");
109 ASSERT_TRUE(c != NULL);
110
111 jmethodID m = env->GetStaticMethodID(c, "shiftTest1", "()I");
112 ASSERT_TRUE(m != NULL);
113
114 jint result = env->CallStaticIntMethod(c, m);
115
116 ASSERT_EQ(0, result);
117}
buzbeec143c552011-08-20 17:38:58 -0700118#endif
119
buzbee3ea4ec52011-08-22 17:37:19 -0700120TEST_F(CompilerTest, ShiftTest2) {
121 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
122 "kIntMathDex"));
123 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
124
125 Thread::Current()->SetClassLoaderOverride(class_loader);
126
127 JNIEnv* env = Thread::Current()->GetJniEnv();
128
129 jclass c = env->FindClass("IntMath");
130 ASSERT_TRUE(c != NULL);
131
132 jmethodID m = env->GetStaticMethodID(c, "shiftTest2", "()I");
133 ASSERT_TRUE(m != NULL);
134
135 jint result = env->CallStaticIntMethod(c, m);
136
137 ASSERT_EQ(0, result);
138}
buzbee3ea4ec52011-08-22 17:37:19 -0700139
140TEST_F(CompilerTest, UnsignedShiftTest) {
141 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
142 "kIntMathDex"));
143 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
144
145 Thread::Current()->SetClassLoaderOverride(class_loader);
146
147 JNIEnv* env = Thread::Current()->GetJniEnv();
148
149 jclass c = env->FindClass("IntMath");
150 ASSERT_TRUE(c != NULL);
151
152 jmethodID m = env->GetStaticMethodID(c, "unsignedShiftTest", "()I");
153 ASSERT_TRUE(m != NULL);
154
155 jint result = env->CallStaticIntMethod(c, m);
156
157 ASSERT_EQ(0, result);
158}
159
160#if 0 // Fail subtest #3, long to int conversion w/ truncation.
161TEST_F(CompilerTest, ConvTest) {
162 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
163 "kIntMathDex"));
164 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
165
166 Thread::Current()->SetClassLoaderOverride(class_loader);
167
168 JNIEnv* env = Thread::Current()->GetJniEnv();
169
170 jclass c = env->FindClass("IntMath");
171 ASSERT_TRUE(c != NULL);
172
173 jmethodID m = env->GetStaticMethodID(c, "convTest", "()I");
174 ASSERT_TRUE(m != NULL);
175
176 jint result = env->CallStaticIntMethod(c, m);
177
178 ASSERT_EQ(0, result);
179}
180#endif
181
182TEST_F(CompilerTest, CharSubTest) {
183 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
184 "kIntMathDex"));
185 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
186
187 Thread::Current()->SetClassLoaderOverride(class_loader);
188
189 JNIEnv* env = Thread::Current()->GetJniEnv();
190
191 jclass c = env->FindClass("IntMath");
192 ASSERT_TRUE(c != NULL);
193
194 jmethodID m = env->GetStaticMethodID(c, "charSubTest", "()I");
195 ASSERT_TRUE(m != NULL);
196
197 jint result = env->CallStaticIntMethod(c, m);
198
199 ASSERT_EQ(0, result);
200}
201
202#if 0 // Needs array allocation & r9 to be set up with Thread*
203TEST_F(CompilerTest, IntOperTest) {
204 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
205 "kIntMathDex"));
206 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
207
208 Thread::Current()->SetClassLoaderOverride(class_loader);
209
210 JNIEnv* env = Thread::Current()->GetJniEnv();
211
212 jclass c = env->FindClass("IntMath");
213 ASSERT_TRUE(c != NULL);
214
215 jmethodID m = env->GetStaticMethodID(c, "intOperTest", "(II)I");
216 ASSERT_TRUE(m != NULL);
217
218 jint result = env->CallStaticIntMethod(c, m, 70000, -3);
219
220 ASSERT_EQ(0, result);
221}
222#endif
223
224#if 0 // Needs array allocation & r9 to be set up with Thread*
225TEST_F(CompilerTest, Lit16Test) {
226 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
227 "kIntMathDex"));
228 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
229
230 Thread::Current()->SetClassLoaderOverride(class_loader);
231
232 JNIEnv* env = Thread::Current()->GetJniEnv();
233
234 jclass c = env->FindClass("IntMath");
235 ASSERT_TRUE(c != NULL);
236
237 jmethodID m = env->GetStaticMethodID(c, "lit16Test", "(I)I");
238 ASSERT_TRUE(m != NULL);
239
240 jint result = env->CallStaticIntMethod(c, m, 77777);
241
242 ASSERT_EQ(0, result);
243}
244#endif
245
246#if 0 // Needs array allocation & r9 to be set up with Thread*
247TEST_F(CompilerTest, Lit8Test) {
248 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
249 "kIntMathDex"));
250 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
251
252 Thread::Current()->SetClassLoaderOverride(class_loader);
253
254 JNIEnv* env = Thread::Current()->GetJniEnv();
255
256 jclass c = env->FindClass("IntMath");
257 ASSERT_TRUE(c != NULL);
258
259 jmethodID m = env->GetStaticMethodID(c, "lit8Test", "(I)I");
260 ASSERT_TRUE(m != NULL);
261
262 jint result = env->CallStaticIntMethod(c, m, -55555);
263
264 ASSERT_EQ(0, result);
265}
266#endif
267
268#if 0 // Needs array allocation & r9 to be set up with Thread*
269TEST_F(CompilerTest, Lit8Test) {
270 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
271 "kIntMathDex"));
272 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
273
274 Thread::Current()->SetClassLoaderOverride(class_loader);
275
276 JNIEnv* env = Thread::Current()->GetJniEnv();
277
278 jclass c = env->FindClass("IntMath");
279 ASSERT_TRUE(c != NULL);
280
281 jmethodID m = env->GetStaticMethodID(c, "lit8Test", "(I)I");
282 ASSERT_TRUE(m != NULL);
283
284 jint result = env->CallStaticIntMethod(c, m, -55555);
285
286 ASSERT_EQ(0, result);
287}
288#endif
289
290#if 0 // Needs array allocation & r9 to be set up with Thread*
291TEST_F(CompilerTest, IntShiftTest) {
292 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
293 "kIntMathDex"));
294 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
295
296 Thread::Current()->SetClassLoaderOverride(class_loader);
297
298 JNIEnv* env = Thread::Current()->GetJniEnv();
299
300 jclass c = env->FindClass("IntMath");
301 ASSERT_TRUE(c != NULL);
302
303 jmethodID m = env->GetStaticMethodID(c, "intShiftTest", "(II)I");
304 ASSERT_TRUE(m != NULL);
305
306 jint result = env->CallStaticIntMethod(c, m, 0xff00aa01, 8);
307
308 ASSERT_EQ(0, result);
309}
310#endif
311
312#if 0 // Needs array allocation & r9 to be set up with Thread*
313TEST_F(CompilerTest, LongOperTest) {
314 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
315 "kIntMathDex"));
316 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
317
318 Thread::Current()->SetClassLoaderOverride(class_loader);
319
320 JNIEnv* env = Thread::Current()->GetJniEnv();
321
322 jclass c = env->FindClass("IntMath");
323 ASSERT_TRUE(c != NULL);
324
325 jmethodID m = env->GetStaticMethodID(c, "longOperTest", "(LL)I");
326 ASSERT_TRUE(m != NULL);
327
328 jint result = env->CallStaticIntMethod(c, m, 70000000000L, 3);
329
330 ASSERT_EQ(0, result);
331}
332#endif
333
334#if 0 // Needs array allocation & r9 to be set up with Thread*
335TEST_F(CompilerTest, LongShiftTest) {
336 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
337 "kIntMathDex"));
338 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
339
340 Thread::Current()->SetClassLoaderOverride(class_loader);
341
342 JNIEnv* env = Thread::Current()->GetJniEnv();
343
344 jclass c = env->FindClass("IntMath");
345 ASSERT_TRUE(c != NULL);
346
347 jmethodID m = env->GetStaticMethodID(c, "longShiftTest", "(LL)I");
348 ASSERT_TRUE(m != NULL);
349
350 jint result = env->CallStaticIntMethod(c, m, 0xd5aa96deff00aa01, 8);
351
352 ASSERT_EQ(0, result);
353}
354#endif
355
buzbee9e0f9b02011-08-24 15:32:46 -0700356TEST_F(CompilerTest, SwitchTest1) {
357 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
358 "kIntMathDex"));
359 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
360
361 Thread::Current()->SetClassLoaderOverride(class_loader);
362
363 JNIEnv* env = Thread::Current()->GetJniEnv();
364
365 jclass c = env->FindClass("IntMath");
366 ASSERT_TRUE(c != NULL);
367
368 jmethodID m = env->GetStaticMethodID(c, "switchTest", "(I)I");
369 ASSERT_TRUE(m != NULL);
370
371 jint result = env->CallStaticIntMethod(c, m, 1);
372
373 ASSERT_EQ(1234, result);
374}
375
376TEST_F(CompilerTest, IntCompare) {
377 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
378 "kIntMathDex"));
379 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
380
381 Thread::Current()->SetClassLoaderOverride(class_loader);
382
383 JNIEnv* env = Thread::Current()->GetJniEnv();
384
385 jclass c = env->FindClass("IntMath");
386 ASSERT_TRUE(c != NULL);
387
388 jmethodID m = env->GetStaticMethodID(c, "testIntCompare", "(IIII)I");
389 ASSERT_TRUE(m != NULL);
390
391 jint result = env->CallStaticIntMethod(c, m, -5, 4, 4, 0);
392
393 ASSERT_EQ(1111, result);
394}
395
396TEST_F(CompilerTest, LongCompare) {
397 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
398 "kIntMathDex"));
399 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
400
401 Thread::Current()->SetClassLoaderOverride(class_loader);
402
403 JNIEnv* env = Thread::Current()->GetJniEnv();
404
405 jclass c = env->FindClass("IntMath");
406 ASSERT_TRUE(c != NULL);
407
408 jmethodID m = env->GetStaticMethodID(c, "testLongCompare", "(JJJJ)I");
409 ASSERT_TRUE(m != NULL);
410
411 jint result = env->CallStaticIntMethod(c, m, -5LL, -4294967287LL, 4LL, 8LL);
412
413 ASSERT_EQ(2222, result);
414}
415
416TEST_F(CompilerTest, FloatCompare) {
417 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
418 "kIntMathDex"));
419 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
420
421 Thread::Current()->SetClassLoaderOverride(class_loader);
422
423 JNIEnv* env = Thread::Current()->GetJniEnv();
424
425 jclass c = env->FindClass("IntMath");
426 ASSERT_TRUE(c != NULL);
427
428 jmethodID m = env->GetStaticMethodID(c, "testFloatCompare", "(FFFF)I");
429 ASSERT_TRUE(m != NULL);
430
431 jint result = env->CallStaticIntMethod(c, m, -5.0f, 4.0f, 4.0f,
432 (1.0f/0.0f) / (1.0f/0.0f));
433
434 ASSERT_EQ(3333, result);
435}
436
437TEST_F(CompilerTest, DoubleCompare) {
438 scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
439 "kIntMathDex"));
440 PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
441
442 Thread::Current()->SetClassLoaderOverride(class_loader);
443
444 JNIEnv* env = Thread::Current()->GetJniEnv();
445
446 jclass c = env->FindClass("IntMath");
447 ASSERT_TRUE(c != NULL);
448
449 jmethodID m = env->GetStaticMethodID(c, "testDoubleCompare", "(DDDD)I");
450 ASSERT_TRUE(m != NULL);
451
452 jint result = env->CallStaticIntMethod(c, m, -5.0, 4.0, 4.0,
453 (1.0/0.0) / (1.0/0.0));
454
455 ASSERT_EQ(4444, result);
456}
457
buzbee3ea4ec52011-08-22 17:37:19 -0700458#endif // Arm
buzbeec143c552011-08-20 17:38:58 -0700459} // namespace art