buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "class_linker.h" |
| 4 | #include "common_test.h" |
| 5 | #include "dex_file.h" |
| 6 | #include "heap.h" |
| 7 | #include "object.h" |
| 8 | #include "scoped_ptr.h" |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | #include <stdio.h> |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | namespace art { |
| 15 | |
| 16 | class CompilerTest : public CommonTest { |
| 17 | }; |
| 18 | |
| 19 | #if defined(__arm__) |
| 20 | TEST_F(CompilerTest, BasicCodegen) { |
| 21 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kFibonacciDex, |
| 22 | "kFibonacciDex")); |
| 23 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 24 | |
| 25 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 26 | |
| 27 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 28 | |
| 29 | jclass c = env->FindClass("Fibonacci"); |
| 30 | ASSERT_TRUE(c != NULL); |
| 31 | |
| 32 | jmethodID m = env->GetStaticMethodID(c, "fibonacci", "(I)I"); |
| 33 | ASSERT_TRUE(m != NULL); |
| 34 | |
| 35 | jint result = env->CallStaticIntMethod(c, m, 10); |
| 36 | LOG(INFO) << "Fibonacci[10] is " << result; |
| 37 | |
| 38 | ASSERT_EQ(55, result); |
| 39 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame^] | 40 | |
| 41 | TEST_F(CompilerTest, UnopTest) { |
| 42 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 43 | "kIntMathDex")); |
| 44 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 45 | |
| 46 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 47 | |
| 48 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 49 | |
| 50 | jclass c = env->FindClass("IntMath"); |
| 51 | ASSERT_TRUE(c != NULL); |
| 52 | |
| 53 | jmethodID m = env->GetStaticMethodID(c, "unopTest", "(I)I"); |
| 54 | ASSERT_TRUE(m != NULL); |
| 55 | |
| 56 | jint result = env->CallStaticIntMethod(c, m, 38); |
| 57 | LOG(INFO) << "unopTest(38) == " << result; |
| 58 | |
| 59 | ASSERT_EQ(37, result); |
| 60 | } |
| 61 | |
| 62 | #if 0 // Does filled array - needs load-time class resolution |
| 63 | TEST_F(CompilerTest, ShiftTest1) { |
| 64 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 65 | "kIntMathDex")); |
| 66 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 67 | |
| 68 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 69 | |
| 70 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 71 | |
| 72 | jclass c = env->FindClass("IntMath"); |
| 73 | ASSERT_TRUE(c != NULL); |
| 74 | |
| 75 | jmethodID m = env->GetStaticMethodID(c, "shiftTest1", "()I"); |
| 76 | ASSERT_TRUE(m != NULL); |
| 77 | |
| 78 | jint result = env->CallStaticIntMethod(c, m); |
| 79 | |
| 80 | ASSERT_EQ(0, result); |
| 81 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 82 | #endif |
| 83 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame^] | 84 | #if 0 // Fails, needs 64-bit shift helper functions |
| 85 | TEST_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 | } |
| 104 | #endif |
| 105 | |
| 106 | TEST_F(CompilerTest, UnsignedShiftTest) { |
| 107 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 108 | "kIntMathDex")); |
| 109 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 110 | |
| 111 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 112 | |
| 113 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 114 | |
| 115 | jclass c = env->FindClass("IntMath"); |
| 116 | ASSERT_TRUE(c != NULL); |
| 117 | |
| 118 | jmethodID m = env->GetStaticMethodID(c, "unsignedShiftTest", "()I"); |
| 119 | ASSERT_TRUE(m != NULL); |
| 120 | |
| 121 | jint result = env->CallStaticIntMethod(c, m); |
| 122 | |
| 123 | ASSERT_EQ(0, result); |
| 124 | } |
| 125 | |
| 126 | #if 0 // Fail subtest #3, long to int conversion w/ truncation. |
| 127 | TEST_F(CompilerTest, ConvTest) { |
| 128 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 129 | "kIntMathDex")); |
| 130 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 131 | |
| 132 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 133 | |
| 134 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 135 | |
| 136 | jclass c = env->FindClass("IntMath"); |
| 137 | ASSERT_TRUE(c != NULL); |
| 138 | |
| 139 | jmethodID m = env->GetStaticMethodID(c, "convTest", "()I"); |
| 140 | ASSERT_TRUE(m != NULL); |
| 141 | |
| 142 | jint result = env->CallStaticIntMethod(c, m); |
| 143 | |
| 144 | ASSERT_EQ(0, result); |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | TEST_F(CompilerTest, CharSubTest) { |
| 149 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 150 | "kIntMathDex")); |
| 151 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 152 | |
| 153 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 154 | |
| 155 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 156 | |
| 157 | jclass c = env->FindClass("IntMath"); |
| 158 | ASSERT_TRUE(c != NULL); |
| 159 | |
| 160 | jmethodID m = env->GetStaticMethodID(c, "charSubTest", "()I"); |
| 161 | ASSERT_TRUE(m != NULL); |
| 162 | |
| 163 | jint result = env->CallStaticIntMethod(c, m); |
| 164 | |
| 165 | ASSERT_EQ(0, result); |
| 166 | } |
| 167 | |
| 168 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 169 | TEST_F(CompilerTest, IntOperTest) { |
| 170 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 171 | "kIntMathDex")); |
| 172 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 173 | |
| 174 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 175 | |
| 176 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 177 | |
| 178 | jclass c = env->FindClass("IntMath"); |
| 179 | ASSERT_TRUE(c != NULL); |
| 180 | |
| 181 | jmethodID m = env->GetStaticMethodID(c, "intOperTest", "(II)I"); |
| 182 | ASSERT_TRUE(m != NULL); |
| 183 | |
| 184 | jint result = env->CallStaticIntMethod(c, m, 70000, -3); |
| 185 | |
| 186 | ASSERT_EQ(0, result); |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 191 | TEST_F(CompilerTest, Lit16Test) { |
| 192 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 193 | "kIntMathDex")); |
| 194 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 195 | |
| 196 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 197 | |
| 198 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 199 | |
| 200 | jclass c = env->FindClass("IntMath"); |
| 201 | ASSERT_TRUE(c != NULL); |
| 202 | |
| 203 | jmethodID m = env->GetStaticMethodID(c, "lit16Test", "(I)I"); |
| 204 | ASSERT_TRUE(m != NULL); |
| 205 | |
| 206 | jint result = env->CallStaticIntMethod(c, m, 77777); |
| 207 | |
| 208 | ASSERT_EQ(0, result); |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 213 | TEST_F(CompilerTest, Lit8Test) { |
| 214 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 215 | "kIntMathDex")); |
| 216 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 217 | |
| 218 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 219 | |
| 220 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 221 | |
| 222 | jclass c = env->FindClass("IntMath"); |
| 223 | ASSERT_TRUE(c != NULL); |
| 224 | |
| 225 | jmethodID m = env->GetStaticMethodID(c, "lit8Test", "(I)I"); |
| 226 | ASSERT_TRUE(m != NULL); |
| 227 | |
| 228 | jint result = env->CallStaticIntMethod(c, m, -55555); |
| 229 | |
| 230 | ASSERT_EQ(0, result); |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 235 | TEST_F(CompilerTest, Lit8Test) { |
| 236 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 237 | "kIntMathDex")); |
| 238 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 239 | |
| 240 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 241 | |
| 242 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 243 | |
| 244 | jclass c = env->FindClass("IntMath"); |
| 245 | ASSERT_TRUE(c != NULL); |
| 246 | |
| 247 | jmethodID m = env->GetStaticMethodID(c, "lit8Test", "(I)I"); |
| 248 | ASSERT_TRUE(m != NULL); |
| 249 | |
| 250 | jint result = env->CallStaticIntMethod(c, m, -55555); |
| 251 | |
| 252 | ASSERT_EQ(0, result); |
| 253 | } |
| 254 | #endif |
| 255 | |
| 256 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 257 | TEST_F(CompilerTest, IntShiftTest) { |
| 258 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 259 | "kIntMathDex")); |
| 260 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 261 | |
| 262 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 263 | |
| 264 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 265 | |
| 266 | jclass c = env->FindClass("IntMath"); |
| 267 | ASSERT_TRUE(c != NULL); |
| 268 | |
| 269 | jmethodID m = env->GetStaticMethodID(c, "intShiftTest", "(II)I"); |
| 270 | ASSERT_TRUE(m != NULL); |
| 271 | |
| 272 | jint result = env->CallStaticIntMethod(c, m, 0xff00aa01, 8); |
| 273 | |
| 274 | ASSERT_EQ(0, result); |
| 275 | } |
| 276 | #endif |
| 277 | |
| 278 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 279 | TEST_F(CompilerTest, LongOperTest) { |
| 280 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 281 | "kIntMathDex")); |
| 282 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 283 | |
| 284 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 285 | |
| 286 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 287 | |
| 288 | jclass c = env->FindClass("IntMath"); |
| 289 | ASSERT_TRUE(c != NULL); |
| 290 | |
| 291 | jmethodID m = env->GetStaticMethodID(c, "longOperTest", "(LL)I"); |
| 292 | ASSERT_TRUE(m != NULL); |
| 293 | |
| 294 | jint result = env->CallStaticIntMethod(c, m, 70000000000L, 3); |
| 295 | |
| 296 | ASSERT_EQ(0, result); |
| 297 | } |
| 298 | #endif |
| 299 | |
| 300 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 301 | TEST_F(CompilerTest, LongShiftTest) { |
| 302 | scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex, |
| 303 | "kIntMathDex")); |
| 304 | PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get()); |
| 305 | |
| 306 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 307 | |
| 308 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 309 | |
| 310 | jclass c = env->FindClass("IntMath"); |
| 311 | ASSERT_TRUE(c != NULL); |
| 312 | |
| 313 | jmethodID m = env->GetStaticMethodID(c, "longShiftTest", "(LL)I"); |
| 314 | ASSERT_TRUE(m != NULL); |
| 315 | |
| 316 | jint result = env->CallStaticIntMethod(c, m, 0xd5aa96deff00aa01, 8); |
| 317 | |
| 318 | ASSERT_EQ(0, result); |
| 319 | } |
| 320 | #endif |
| 321 | |
| 322 | #endif // Arm |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 323 | } // namespace art |