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