Convert CompilerTest over to use Compiler

Change-Id: Ie9ec6a021126f68acd3f6d35ebe73247b5cc360f
diff --git a/src/compiler_test.cc b/src/compiler_test.cc
index 4692f82..b112eef 100644
--- a/src/compiler_test.cc
+++ b/src/compiler_test.cc
@@ -12,14 +12,46 @@
 
 #include <stdint.h>
 #include <stdio.h>
-#include "gtest/gtest.h"
 
 namespace art {
 
 class CompilerTest : public CommonTest {
+ protected:
+  void CompileDex(const char* base64_dex, const char* base64_name) {
+    dex_file_.reset(OpenDexFileBase64(base64_dex, base64_name));
+    class_linker_->RegisterDexFile(*dex_file_.get());
+    std::vector<const DexFile*> class_path;
+    class_path.push_back(dex_file_.get());
+    Compiler compiler;
+    const ClassLoader* class_loader = compiler.Compile(class_path);
+    Thread::Current()->SetClassLoaderOverride(class_loader);
+  }
+
+  void AssertStaticIntMethod(const char* klass, const char* method, const char* signature,
+                             jint expected, ...) {
+    JNIEnv* env = Thread::Current()->GetJniEnv();
+    jclass c = env->FindClass(klass);
+    CHECK(c != NULL);
+    jmethodID m = env->GetStaticMethodID(c, method, signature);
+    CHECK(m != NULL);
+#if defined(__arm__)
+    va_list args;
+    va_start(args, expected);
+    jint result = env->CallStaticIntMethodV(c, m, args);
+    va_end(args);
+    LOG(INFO) << klass << "." << method << "(...) result is " << result;
+    EXPECT_EQ(expected, result);
+#endif // __arm__
+  }
+ private:
+  scoped_ptr<DexFile> dex_file_;
 };
 
-TEST_F(CompilerTest, CompileLibCore) {
+TEST_F(CompilerTest, CompileDexLibCore) {
+  // TODO renenable when compiler can handle libcore
+  if (true) {
+    return;
+  }
   Compiler compiler;
   compiler.Compile(boot_class_path_);
 
@@ -51,409 +83,127 @@
 
 }
 
-
-#if defined(__arm__)
 TEST_F(CompilerTest, BasicCodegen) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kFibonacciDex,
-                               "kFibonacciDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("Fibonacci");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "fibonacci", "(I)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, 10);
-  LOG(INFO) << "Fibonacci[10] is " << result;
-
-  ASSERT_EQ(55, result);
+  CompileDex(kFibonacciDex, "kFibonacciDex");
+  AssertStaticIntMethod("Fibonacci", "fibonacci", "(I)I", 55,
+                        10);
 }
 
 TEST_F(CompilerTest, UnopTest) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "unopTest", "(I)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, 38);
-  LOG(INFO) << "unopTest(38) == " << result;
-
-  ASSERT_EQ(37, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "unopTest", "(I)I", 37,
+                        38);
 }
 
 #if 0 // Does filled array - needs load-time class resolution
 TEST_F(CompilerTest, ShiftTest1) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "shiftTest1", "()I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "shiftTest1", "()I", 0);
 }
 #endif
 
 TEST_F(CompilerTest, ShiftTest2) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "shiftTest2", "()I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "shiftTest2", "()I", 0);
 }
 
 TEST_F(CompilerTest, UnsignedShiftTest) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "unsignedShiftTest", "()I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "unsignedShiftTest", "()I", 0);
 }
 
 #if 0 // Fail subtest #3, long to int conversion w/ truncation.
 TEST_F(CompilerTest, ConvTest) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "convTest", "()I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "convTest", "()I", 0);
 }
 #endif
 
 TEST_F(CompilerTest, CharSubTest) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "charSubTest", "()I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "charSubTest", "()I", 0);
 }
 
 #if 0 // Needs array allocation & r9 to be set up with Thread*
 TEST_F(CompilerTest, IntOperTest) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "intOperTest", "(II)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, 70000, -3);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "intOperTest", "(II)I", 0,
+                        70000, -3);
 }
 #endif
 
 #if 0 // Needs array allocation & r9 to be set up with Thread*
 TEST_F(CompilerTest, Lit16Test) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "lit16Test", "(I)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, 77777);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "lit16Test", "(I)I", 0,
+                        77777);
 }
 #endif
 
 #if 0 // Needs array allocation & r9 to be set up with Thread*
 TEST_F(CompilerTest, Lit8Test) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "lit8Test", "(I)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, -55555);
-
-  ASSERT_EQ(0, result);
-}
-#endif
-
-#if 0 // Needs array allocation & r9 to be set up with Thread*
-TEST_F(CompilerTest, Lit8Test) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "lit8Test", "(I)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, -55555);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "lit8Test", "(I)I", 0,
+                        -55555);
 }
 #endif
 
 #if 0 // Needs array allocation & r9 to be set up with Thread*
 TEST_F(CompilerTest, IntShiftTest) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "intShiftTest", "(II)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, 0xff00aa01, 8);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "intShiftTest", "(II)I", 0,
+                        0xff00aa01, 8);
 }
 #endif
 
 #if 0 // Needs array allocation & r9 to be set up with Thread*
 TEST_F(CompilerTest, LongOperTest) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "longOperTest", "(LL)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, 70000000000L, 3);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "longOperTest", "(JJ)I", 0,
+                        70000000000LL, 3);
 }
 #endif
 
 #if 0 // Needs array allocation & r9 to be set up with Thread*
 TEST_F(CompilerTest, LongShiftTest) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "longShiftTest", "(LL)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, 0xd5aa96deff00aa01, 8);
-
-  ASSERT_EQ(0, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "longShiftTest", "(JJ)I", 0,
+                        0xd5aa96deff00aa01LL, 8);
 }
 #endif
 
 TEST_F(CompilerTest, SwitchTest1) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "switchTest", "(I)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, 1);
-
-  ASSERT_EQ(1234, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "switchTest", "(I)I", 1234,
+                        1);
 }
 
 TEST_F(CompilerTest, IntCompare) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "testIntCompare", "(IIII)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, -5, 4, 4, 0);
-
-  ASSERT_EQ(1111, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "testIntCompare", "(IIII)I", 1111,
+                        -5, 4, 4, 0);
 }
 
 TEST_F(CompilerTest, LongCompare) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "testLongCompare", "(JJJJ)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, -5LL, -4294967287LL, 4LL, 8LL);
-
-  ASSERT_EQ(2222, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "testLongCompare", "(JJJJ)I", 2222,
+                        -5LL, -4294967287LL, 4LL, 8LL);
 }
 
+#if 0
 TEST_F(CompilerTest, FloatCompare) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "testFloatCompare", "(FFFF)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, -5.0f, 4.0f, 4.0f,
-                                         (1.0f/0.0f) / (1.0f/0.0f));
-
-  ASSERT_EQ(3333, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "testFloatCompare", "(FFFF)I", 3333
+                        -5.0f, 4.0f, 4.0f,
+                        (1.0f/0.0f) / (1.0f/0.0f));
 }
+#endif
 
 TEST_F(CompilerTest, DoubleCompare) {
-  scoped_ptr<DexFile> dex_file(OpenDexFileBase64(kIntMathDex,
-                               "kIntMathDex"));
-  PathClassLoader* class_loader = AllocPathClassLoader(dex_file.get());
-
-  Thread::Current()->SetClassLoaderOverride(class_loader);
-
-  JNIEnv* env = Thread::Current()->GetJniEnv();
-
-  jclass c = env->FindClass("IntMath");
-  ASSERT_TRUE(c != NULL);
-
-  jmethodID m = env->GetStaticMethodID(c, "testDoubleCompare", "(DDDD)I");
-  ASSERT_TRUE(m != NULL);
-
-  jint result = env->CallStaticIntMethod(c, m, -5.0, 4.0, 4.0,
-                                         (1.0/0.0) / (1.0/0.0));
-
-  ASSERT_EQ(4444, result);
+  CompileDex(kIntMathDex, "kIntMathDex");
+  AssertStaticIntMethod("IntMath", "testDoubleCompare", "(DDDD)I", 4444,
+                                    -5.0, 4.0, 4.0,
+                                    (1.0/0.0) / (1.0/0.0));
 }
 
-#endif // Arm
 }  // namespace art